Advertisement

Saturday, January 5, 2019

Using the Revit API to Retrieve Rebars Attached to a Structural Element

From the The Building Coder website:

Retrieve Rebars Attached to Structural Element

Question: How can I get all associated rebars which attach to a structural element such as a column by picking that?

Answer: Jeremy initially suggested workarounds making use of filtered element collectors. Unfortunately, that was not very helpful in this case.

Happily, Einar Raknes came to the rescue pointing out the real solution for this:

You can use the  RebarHostData  class and the  GetRebarsInHost  method to retrieve all rebars associated with a rebar host.

To make sure you pick a valid Rebar Host; you can optionally create a selection filter for it like this:

  public class RebarHostSelectionFilter : ISelectionFilter
  {
    public bool AllowElement( Element e )
    {
      return null != RebarHostData
        .GetRebarHostData( e );
    }

    public bool AllowReference( Reference r, XYZ p )
    {
      return false;
    }
  }

Pick a rebar host, and retrieve the list of rebars from it like this:

  ref1 = uidoc.Selection.PickObject( 
    ObjectType.Element, 
    new RebarHostSelectionFilter(), 
    "Pick a rebar host" );

  Element rebarHost = doc.GetElement( ref1 );

  IList<Rebar> rebarsInHost = RebarHostData
    .GetRebarHostData( rebarHost )
    .GetRebarsInHost();

Many thanks to Einar for pointing this out!



There's more information available on the The Building Coder blog.

No comments: