Rebar Element C# Scripting

Hi there.
I am very new to the Rhino.Inside, and I just followed the C# script document on the official webpage.

I found out the “EnqueueAction()” is not the way to use anymore, and I followed this article for bringing a sphere(Brep) to Revit through “DirectShape”.

It works fine. Then I want to make a custom C# script for taking Curve as an input and create a Rebar in Revit.
Here’s the piece of code works(CreateDriectShape)

private void RunScript(double Radius, bool Trigger, ref object sphere)
  {
    if(Trigger == false) return;
    if(Radius < Revit.VertexTolerance * 10.0) return;

    var doc = Revit.ActiveDBDocument;
    using(var tx = new DB.Transaction(doc, Component.NickName))
    {
      tx.Start();
      sphere = CreateSphere(doc, Radius);
      tx.Commit();
    }
  }

  // <Custom additional code> 
  private Brep _sphere;

  private DB.DirectShape CreateSphere(DB.Document doc, double radius){
    var sphere = new Rhino.Geometry.Sphere(Point3d.Origin, radius);
    var brep = sphere.ToBrep();

    var revitCategory = new DB.ElementId((int) DB.BuiltInCategory.OST_GenericModel);

    var ds = DB.DirectShape.CreateElement(doc, revitCategory);
    var shapeList = new List<DB.GeometryObject>(){brep.ToSolid()};
    ds.AppendShape(shapeList);
    return ds;
  }

I looked the RevitAPI doc, and found this static function(Rebar.CreateFromCurves()).
https://www.revitapidocs.com/2022/b020c9d5-6026-b9fa-7e23-f6a7ec2cede3.html
Here’s the code I am currently trying

if(Trigger == false) return;

    host = HostElement as DB.Element;

    using(var tx = new DB.Transaction(Revit.ActiveDBDocument, Component.NickName))
    {

      tx.Start();
      //DB.Structure.Rebar rebar = DB.Structure.Rebar.CreateFromCurves(Revit.ActiveDBDocument, Autodesk.Revit.DB.Structure.RebarStyle.Standard, barType, hookType, hookType, HostElement as DB.Element, origin, curves, DB.Structure.RebarHookOrientation.Right, DB.Structure.RebarHookOrientation.Left, true, true);
      rebar_result = CreateRebar(Revit.ActiveDBDocument, host);
      tx.Commit();
    }

  }

  // <Custom additional code> 
  DB.Element host;
  private DB.Structure.Rebar CreateRebar(DB.Document doc, DB.Element hostElement){
    // Define the rebar geometry information - Line rebar

    DB.XYZ origin = new Point3d(0, 0, 0).ToXYZ();
    DB.XYZ normal = new DB.XYZ(0, 0, 1);
    // Create rebar 900' long
    DB.XYZ rebarLineEnd = new DB.XYZ(origin.X, origin.Y, origin.Z + 900);
    DB.Line rebarLine = DB.Line.CreateBound(origin, rebarLineEnd);

    // Create the line rebar
    IList<DB.Curve> curves = new List<DB.Curve>();
    curves.Add(rebarLine);

    // creating input parameters
    var barType = DB.Structure.RebarBarType.Create(Revit.ActiveDBDocument);
    var hookType = DB.Structure.RebarHookType.Create(Revit.ActiveDBDocument, Math.PI, 10);
    var rebar = DB.Structure.Rebar.CreateFromCurves(Revit.ActiveDBDocument, Autodesk.Revit.DB.Structure.RebarStyle.Standard, barType, hookType, hookType, hostElement as DB.Element, origin, curves, DB.Structure.RebarHookOrientation.Right, DB.Structure.RebarHookOrientation.Left, true, true);
    return rebar;
  }

I have never seen this error message before, and I found nothing after searching on the web.
Basically, I try to do the example code shown in RevitAPI doc but in Grasshopper C# Scripting. Since there’s a lot wall-related components can create Wall element right from Grasshopper to Revit(so does the DirectShape example works), I figured it out this should not be a problem conceptually. The only thing I know led this error is from the Rebar.CreateFromCurve(args…) line.

Any suggestion will be helpful. Thanks.

1 Like

I just figured out the way to achieve it and what the error referred to.
That said something was wrong with the input parameter “norm”. Though I am not sure why on the RevitAPI doc they put “origin” as the input, the length was zero and that’s the main reason why it failed.

 private void RunScript(object HostElement, bool Trigger, ref object rebar_result)
  {
    if(Trigger == false) return;
    host = HostElement as DB.Element;
    using(var txx = new DB.Transaction(Revit.ActiveDBDocument, Component.NickName))
    {
      txx.Start();
      rebar_result = CreateRebar(Revit.ActiveDBDocument, host);
      txx.Commit();
    }

  }

  // <Custom additional code> 
  DB.Element host;
  private DB.Structure.Rebar CreateRebar(DB.Document doc, DB.Element hostElement){
    // Define the rebar geometry information - Line rebar

    DB.XYZ origin = new Point3d(0, 0, 0).ToXYZ();
    DB.XYZ normal = new DB.XYZ(0, 0, 1);
    // Create rebar 9' long
    DB.XYZ rebarLineEnd = new DB.XYZ(origin.X, origin.Y, origin.Z + 9);
    DB.Line rebarLine = DB.Line.CreateBound(origin, rebarLineEnd);

    // Create the line rebar
    IList<DB.Curve> curves = new List<DB.Curve>();
    curves.Add(rebarLine);

    // creating input parameters
    var barType = DB.Structure.RebarBarType.Create(Revit.ActiveDBDocument);
    var hookType = DB.Structure.RebarHookType.Create(Revit.ActiveDBDocument, Math.PI, 10);
    var rebar = DB.Structure.Rebar.CreateFromCurves(Revit.ActiveDBDocument, Autodesk.Revit.DB.Structure.RebarStyle.Standard, barType, hookType, hookType, hostElement as DB.Element, new DB.XYZ(0, 1, 0), curves, DB.Structure.RebarHookOrientation.Right, DB.Structure.RebarHookOrientation.Left, true, true);
    return rebar;
  }

I just paste my code in case someone might find it helpful. Thanks

2 Likes