Create Dimension Programmatically

Hi community,

I have been working on creating linear dimension between some family instances while some problems baffles me a lot.

I have the sample Revit file and dynamo file with more detailed comments uploaded at the bottom, :point_down:

Latest problem: Invalid number of references.

What I am trying to do is creating a linear dimension between the left edges on two boxes (1st and 4th) in a section view.

Like this:

What I have accomplished is create the dimension by referencing two detail lines drawn on the boxes.

Using method: NewDimension(view, line, referenceArray)

I sketched a model line on the wall serving as the second argument for the method.

image

Code in Python node:

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
sel = uidoc.Selection

col = FilteredElementCollector(doc)

#Preparing input from dynamo to revit
c_1 = UnwrapElement(IN[0]).GeometryCurve
c_2 = UnwrapElement(IN[1]).GeometryCurve
direction_curve = UnwrapElement(IN[2]).GeometryCurve

ref_array = ReferenceArray()
ref_array.Append(c_1.GetEndPointReference(0))
ref_array.Append(c_2.GetEndPointReference(0))

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

doc.Create.NewDimension(doc.ActiveView, direction_curve, ref_array)

TransactionManager.Instance.TransactionTaskDone()

OUT = 0

Creation succeed:

From:

To:

But I really want to use the native references on the box instead of these detail lines.

So, I tried to retrieve the box geometry specific to the section view, and use one of the lines I have as reference.

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
sel = uidoc.Selection

col = FilteredElementCollector(doc)

#Preparing input from dynamo to revit
elem = UnwrapElement(IN[0])

opt = Options()
opt.ComputeReferences = True
opt.IncludeNonVisibleObjects = True
opt.View = doc.ActiveView

lst = list(elem.get_Geometry(opt))
solid = lst[0]

geo_instance = lst[1]
instance_geo = list(geo_instance.GetInstanceGeometry())

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

TransactionManager.Instance.TransactionTaskDone()

OUT = solid.ToProtoType(), instance_geo[1:]

I separate the solid from the lines and convert it to ProtoGeometry just for previewing where the lines are on the box.

Now, left edges obtained.

Creation failed due to:

image

Line 53, the NewDimension() method, seems these two native references are not valid for creating dimension.

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
sel = uidoc.Selection

col = FilteredElementCollector(doc)

#Preparing input from dynamo to revit
ref_array = ReferenceArray()
ref_array.Append(IN[0].Reference)
ref_array.Append(IN[1].Reference)

direction_line = UnwrapElement(IN[2]).GeometryCurve

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

dimension = doc.Create.NewDimension(doc.ActiveView, direction_line, ref_array)

TransactionManager.Instance.TransactionTaskDone()

OUT = dimension.ToDSType(True)

Really appreciate ur help!:slightly_smiling_face:

Family & Dynamo Script & Revit File



Hi, you need to get reference planes in your family
Use for example FamilyInstance.GetReferences(element, FamilyInstanceReferenceType.Left)

Here you can find all reference types https://www.revitapidocs.com/2018/ab424c61-4b80-9dcd-3f9a-7b35fa670edf.htm

1 Like

Hi @jshial,

Maybe not the answer youโ€™re waiting for, but itโ€™s easier using a package.


2 Likes

Works like a charm! Thx!

Thx, helpful as always!