Rebar by Curve - internal error

Dear Pro Developers, I’m trying to write some node to automate rebars modelling.
Most of the thing are done bust I’m struggling creating the real rebar element in Revit.
I tried to use also the node from DynamoForRebar package but I’m getting the same error.

The real problem is in the method RebarByCurves

public static class Create
    {
        public static Document doc = DocumentManager.Instance.CurrentDBDocument;
        public static UIApplication uiapp = DocumentManager.Instance.CurrentUIApplication;
        public static UIDocument uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument;
        public static Functions f = new Functions();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Host"></param>
        /// <param name="Path"></param>
        /// <param name="PolyCurve"></param>
        /// <param name="DistanceFromStart"></param>
        /// <param name="DistanceFromEnd"></param>
        /// <param name="DistanceBetweenRebars"></param>
        /// <returns></returns>
        [IsVisibleInDynamoLibrary(true)]
        [MultiReturn(new[] { "Distances", "Parameters", "Points", "CoordinateSystems", "Shapes" })]
        public static Dictionary<string, object> DistributionByShapeAndPath(
            Revit.Elements.Element Host, 
            ads.Curve Path,
            ads.PolyCurve PolyCurve, 
            double DistanceFromStart = 0, 
            double DistanceFromEnd = 0,
            double DistanceBetweenRebars = 200)

        {

            string msg = "Executed";
            Autodesk.Revit.DB.Element host = doc.GetElement(Host.UniqueId.ToString());
            List<double> distances = new List<double>();
            List<double> crvParameters = new List<double>();
            List<ads.Point> points = new List<ads.Point>();
            List<CoordinateSystem> css = new List<CoordinateSystem>();
            ads.Point appPoint = PolyCurve.StartPoint;
            ads.Vector pCurveNormal = PolyCurve.Normal;
            ads.Plane pCurvePlane = ads.Plane.ByOriginNormal(appPoint, pCurveNormal);
            List<ads.Geometry> shapes = new List<ads.Geometry>();

            ads.Point intersectPoint = Path.Intersect(pCurvePlane).First() as ads.Point;
            CoordinateSystem csIntersectParam = Path.CoordinateSystemAtParameter(Path.ParameterAtPoint(intersectPoint));


            try
            {
                    double crvLen = Path.Length;

                    for (double dist = DistanceFromStart; dist < (crvLen - DistanceFromEnd); dist += DistanceBetweenRebars)
                    {
                        distances.Add(dist);
                        crvParameters.Add(Path.ParameterAtSegmentLength(dist));
                        points.Add(Path.PointAtSegmentLength(dist));
                        css.Add(Path.CoordinateSystemAtDistance(dist));
                        shapes.Add(PolyCurve.Transform(csIntersectParam, Path.CoordinateSystemAtDistance(dist)));
                        Vector normal = Path.NormalAtParameter(Path.ParameterAtSegmentLength(dist));
                        XYZ nVector = new XYZ(normal.X, normal.Y, normal.Z);
                    }
            }
            catch (Exception ex)
            {
                msg = "Error: " + ex.Message;
            }

            return new Dictionary<string, object>
            {
                { "Distances", distances },
                { "Parameters", crvParameters },
                { "Points", points },
                { "CoordinateSystems", css },
                { "Shapes", shapes },
                { "Message", msg },
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Host"></param>
        /// <param name="RebarStyle"></param>
        /// <param name="RebarBarType"></param>
        /// <param name="RebarHookTypeAtStart"></param>
        /// <param name="RebarHookTypeAtEnd"></param>
        /// <param name="RebarHookOrientationAtStart"></param>
        /// <param name="RebarHookOrientationAtEnd"></param>
        /// <param name="PolyCurves"></param>
        /// <returns></returns>
        [IsVisibleInDynamoLibrary(true)]
        [MultiReturn(new[] { "Curves" })]
        public static Dictionary<string, object> RebarByCurves(
            Revit.Elements.Element Host,
            string RebarStyle,
            Revit.Elements.Element RebarBarType,
            Revit.Elements.Element RebarHookTypeAtStart,
            Revit.Elements.Element RebarHookTypeAtEnd,
            string RebarHookOrientationAtStart,
            string RebarHookOrientationAtEnd,
            List<ads.PolyCurve> PolyCurves
            )
        {
            string msg = "Executed";


            ElementId hostId = new ElementId(Host.Id);
            Autodesk.Revit.DB.Element h = doc.GetElement(hostId);

            RebarBarType rebType = RebarBarType.InternalElement as RebarBarType;
            RebarHookType startHook = RebarHookTypeAtStart.InternalElement as RebarHookType;
            RebarHookType endHook = RebarHookTypeAtEnd.InternalElement as RebarHookType;
            RebarHookOrientation rebHookOrStart, rebHookOrEnd;
            RebarStyle rebStyle;

            if (RebarStyle == "Standard") { rebStyle = Autodesk.Revit.DB.Structure.RebarStyle.Standard; } else { rebStyle = Autodesk.Revit.DB.Structure.RebarStyle.StirrupTie; } 
            List<List<Autodesk.Revit.DB.Curve>> revitCurves = new List<List<Autodesk.Revit.DB.Curve>>();

            if (RebarHookOrientationAtStart == "Left") { rebHookOrStart = RebarHookOrientation.Left; } else { rebHookOrStart = RebarHookOrientation.Right; }
            if (RebarHookOrientationAtEnd == "Left") { rebHookOrEnd = RebarHookOrientation.Left; } else { rebHookOrEnd = RebarHookOrientation.Right; }

            try
            {
     
                string str = "";
                foreach (var p in PolyCurves)
                {
                    XYZ normal = GeometryPrimitiveConverter.ToRevitType(p.Normal);
                    var curves = p.Curves();
                    List<Autodesk.Revit.DB.Curve> convCurves = new List<Autodesk.Revit.DB.Curve>();

                    foreach (var c in curves)
                    {
                        convCurves.Add(ProtoToRevitCurve.ToRevitType(c));
                        str += c.ToString();
                    }
                    try
                    {
                        using (Transaction tx = new Transaction(doc, "CreateRebar"))
                        {
                            tx.Start("CreateRebar");
                            Rebar reb = Rebar.CreateFromCurves(
                                doc,
                                rebStyle,
                                rebType,
                                startHook,
                                endHook,
                                h,
                                normal,
                                convCurves,
                                rebHookOrStart,
                                rebHookOrEnd,
                                true,
                                false
                                );
                            tx.Commit();
                            tx.Dispose();
                        }

                    }
                    catch (Exception ex)
                    {
                        msg = "Error 2: " + ex.Message;
                    }
                    revitCurves.Add(convCurves);
                }
            }
            catch (Exception ex)
            {
                msg = "Error: " + ex.Message;
            }

            return new Dictionary<string, object>
            {
                {"Curves", revitCurves },
                { "Message", msg },
            };
        }

    }
}

The github repository is available at this link

This is the screenshot from the workspace

This is the Revit model (very easy a beam and a rebar)

2 Likes

Debugging is the first action you need to take as you’ll be able to identify the nature and cause of the exception. My suspicion is a possible curve conversion error when calling ToRevitType(); I’ve had to write my own curve conversion methods for some curve types instead of using ToRevitType() as the conversion process is inconsistent, like ellipse arcs for example.

I tend to steer clear of the ProtoGeometry library in ZT nodes if it can be avoided, as the Revit API is faster and more powerful. One minor comment; use Dynamos TransactionManager class as its purpose built for Revit interop and should handle any transaction issues without needing to implement your own object disposal.

1 Like

Thanks for the suggestions but it was even easier…that was only fault of the AdjustSelftIntersection option in the node to get the centerline curves.
Now it is working perfectly!

3 Likes

Hi @Cesare_Caoduro3,

Could you please explain more about the error?
I got the same warning when i try to generate Rebar with my own python code.

Thank you in advance

It’s just an option to avoid self intersecting bar shapes, so I’ve only set the option to false and, it worked fine

Hi!

Can anyone see what I am doing wrong? Untitled