Obtaining References from edges in Python

Obtaining References from edges in Python

Kevin.Bell
Advisor Advisor
2,319 Views
4 Replies
Message 1 of 5

Obtaining References from edges in Python

Kevin.Bell
Advisor
Advisor

Hi,

 

I'm (trying) to put together a Python routine that will automatically create dimensions on a square face of a generic model. i.e. the user select the face and dimensions appear...

 

I'm writing Python code using Dynamo.

 

I've got some of the code working, obtaining the edges from a user selected 3D face, this returns the 4 edges (which are Edge class objects), it should simply be a case of obtaining the edge's reference that can be used to create the dimensions.

 

The Edge class has a Property which should return a reference to the edge - but when I run the code, it returns Null - and I can't see why.

 

This is the code:

 

 # Dynamo
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
selobject		= UnwrapElement(IN[0])	# Object to select

#Get user to pick a face
selob = uidoc.Selection.PickObject(Selection.ObjectType.PointOnElement, "Pick something now")

#Get Id of element thats picked
selobid = selob.ElementId

#Get element thats picked
getob = doc.GetElement(selobid)

#Get face thats picked
getface = getob.GetGeometryObjectFromReference(selob)

#Get edges of face (returns a list the first object is the list of edges)
edgeloops = getface.EdgeLoops

#Select the first edge
dimedge1 = edgeloops[0][0]

#Select the third edge (the one opposite the first)
dimedge2 = edgeloops[0][2]

#Obtain a reference of the first edge
edgeref1 = dimedge1.Reference

#Obtain a reference of the thord edge
edgeref2 = dimedge2.Reference

#Assign your output to the OUT variable.
OUT = [selob, selobid, getob, getface, edgeloops, dimedge1, dimedge2, edgeref1, edgeref2]

If you execute it you'll see that edgeref1 and edgeref2 variables both return Null.

 

Any idea why?

 

Thanks.

 

0 Likes
2,320 Views
4 Replies
Replies (4)
Message 2 of 5

aignatovich
Advisor
Advisor

Hi, Kevin.

 

I've found solution for your problem. The main idea is to get geometry with ComputeReference = True and find appropriate face by reference

 

Sorry, I don't know Python too much, so I've created an addin in C#. You may get it from https://github.com/CADBIMDeveloper/PutRevitDimensionsToSquareFaces

0 Likes
Message 3 of 5

aignatovich
Advisor
Advisor

Hi again!

I've fixed the code in the repository, there were a lot of tricks with revit references to make this work as expected with families, sorry, I've tested it only with floors at the first time, now it works with family instances too

0 Likes
Message 4 of 5

Kevin.Bell
Advisor
Advisor

Hi aignatovich, I really appreciate you doing some work on this!

 

I can't look at the code at the moment as am out of the office, but will check it out soon.

 

ComputeReference = True is not something I've come across, I'll have to read up on that.

 

Thanks.

0 Likes
Message 5 of 5

aignatovich
Advisor
Advisor

Hi again!

There is another simpler solution, if you use Revit 2018 and can prepare your family for dimensioning. You can add reference planes, xLeft, xRight, yTop, yBottom:

def CreateDimension(instance, refNames, direction):
	references = ReferenceArray()
	
	for x in refNames:
		references.Append(instance.GetReferenceByName(x))
	
	origin = instance.Location.Point
	
	transform = instance.GetTotalTransform()
	transform.Origin = XYZ.Zero
	
	dimensionDirection = transform.OfPoint(direction)
	
	dimensionLine = Line.CreateUnbound(origin, dimensionDirection)
	
	doc.Create.NewDimension(doc.ActiveView, dimensionLine, references)

famInst = selection[0]

tx = Transaction(doc, "create dimensions")
tx.Start()

CreateDimension(famInst, ["xLeft", "xRight"], XYZ.BasisX)
CreateDimension(famInst, ["yBottom", "yTop"], XYZ.BasisY)

tx.Commit()
0 Likes