Create stirrup rebar by given model curves via Revit API

By Joe Ye

As creating a stirrup rebar shape family is not so easy. Users would like to sketch up the shape of the stirrup within a column. This article introduce how to create a stirrup by several model curves within a column.

 

using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;         using Autodesk.Revit.ApplicationServices;    using Autodesk.Revit.UI;    using Autodesk.Revit.DB.Structure;    using Autodesk.Revit.DB;    using Autodesk.Revit.UI.Selection;         namespace MyCreateRebar    {      [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]      [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]      public class Command1 : IExternalCommand      {        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)        {          UIApplication uiApp = commandData.Application;          Selection sel = uiApp.ActiveUIDocument.Selection;          Application app = uiApp.Application;          Document doc = uiApp.ActiveUIDocument.Document;               RebarBarType barType = null;          RebarHookType hookType = null;               Transaction transaction = new Transaction(doc, "CreateRebar Tool");          try          {            transaction.Start();                 IList curves1 = new List();            ElementSet eleset = sel.Elements;            foreach (Element e in eleset)            {              if (e is ModelLine)              {                Line l = (e as ModelLine).GeometryCurve as Line;                Line line = app.Create.NewLineBound(l.get_EndPoint(0), l.get_EndPoint(1));                curves1.Add(line);              }            }                 foreach (RebarBarType bt in doc.RebarBarTypes)            {              barType = bt;              break;            }            foreach (RebarHookType ht in doc.RebarHookTypes)            {              hookType = ht;            }                 {                   Reference hasPickOne = sel.PickObject(ObjectType.Element);              FamilyInstance hostFi = doc.get_Element(hasPickOne.ElementId) as FamilyInstance;              Rebar rebar = CreateRebar1(uidoc, curves1, hostFi, barType, hookType);            }          }          catch (Exception ex)          {            message += ex.Message;            return Autodesk.Revit.UI.Result.Failed;          }          finally          {            transaction.Commit();          }               return Result.Succeeded;        }             Rebar CreateRebar1(            Autodesk.Revit.UI.UIDocument uidoc,            IList curves,            FamilyInstance column,            RebarBarType barType,            RebarHookType hookType)        {               LocationPoint location = column.Location as LocationPoint;          XYZ origin = location.Point;               Rebar rebar = Rebar.CreateFromCurves(uidoc.Document,             RebarStyle.StirrupTie, barType, hookType, hookType,            column, new XYZ(0, 0, 1), curves,             RebarHookOrientation.Left, RebarHookOrientation.Left, true, true);               return rebar;        }      }

Comments

2 responses to “Create stirrup rebar by given model curves via Revit API”

  1. Manuel Avatar
    Manuel

    It works well when your curves are lines. Have you ever tried to create a lapped circular stirrup? i tried it giving always valid curves (the ones that produce a valid rebarShape in the sketch mode) but i got a message: “Unable to create a rebarShape on the given curves”.

  2. Franklin Avatar
    Franklin

    Good day, I try to obtain the curves of a group of rebars and create them separately, but when creating them with the CreateFromCurves method I do not know where I get the XYZ norm, for each rebar.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading