RevitAPI: Create dimension on detail component

中文链接

By Aaron Lu

Below are 2 detail components, each is actually a detail line in the family.

CreateDimensionOnDetailComponent_0

What dimension we want to create is like this:

CreateDimensionOnDetailComponent

We know the Document.Create.NewDimension method is:

Dimension NewDimension(View view,
Line line, ReferenceArray references)

First 2 arguments are easy: one is the view to create on, the other is the position of the dimension,

So the last one ReferenceArray is the references of the geometry object on these 2 components, but how to get them?

Obviously, using (FamilyInstance.Location as LocationCurve).Curve.Reference, we can only get null reference

So, only Element.get_Geometry way is left, then here is the code:

private static Line GetReferenceOfDetailComponent(Element element,
View view)
{
Options options = new Options();
options.ComputeReferences = true;
options.IncludeNonVisibleObjects = true;
if (view != null)
options.View = view;
else
options.DetailLevel = ViewDetailLevel.Fine;
var geoElem = element.get_Geometry(options);
foreach (var item in geoElem)
{
Line line = item as Line;
if (line != null)
{
//in this case, code will never be executed to here
}
else
{
GeometryInstance geoInst = item as GeometryInstance;
if (geoInst != null)
{
GeometryElement geoElemTmp =
geoInst.GetSymbolGeometry();
foreach (GeometryObject geomObjTmp in geoElemTmp)
{
Line line2 = geomObjTmp as Line;
if (line2 != null)
{
//check if it is what you want
if (line2.GraphicsStyleId.IntegerValue == 355)
{
return line2;
}
}
}
}
}
}
return null;
}

Note:

  • Options.ComputeReferences must be set to true, otherwise the Reference of any geometry object is null
  • When iterating GeometryElement, if we fount a GeometryInstance, then call GetSymbolGeometry rather than GetInstanceGeometry to get more geometry objects, otherwise, they are still not suitable to create dimension.

Comments

One response to “RevitAPI: Create dimension on detail component”

  1. I’d like to thank the author for writing such an insightful and informative blog post about revit api dimensions that is not just useful to the readers but also revealing.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading