Python: Dimension stability issue

Good morning, I have been trying to learn how to dimension elements, using for my case a electrical Conduit Fitting to Conduit Fitting, using its Geometry Origin and Direction for the NewDimension creation, and the StableRepresentation of its Center point as the references. I have spent the last week scouring what is already online for examples in C#, and GeniusLocis nodes, and finally have a grasp on what I am doing somewhat having tested many different ways to achieve this unsuccessfully prior :rofl:. I can get this to work in multiple variations of code changing up the line and references, where If the dimensions are horizontal or vertical I am able to move the dimension away from the element, but when I make it at an angle, then attempt to move it, it reverts to the horizontal distance instead of the hypotenuse from center of bend to center of bend I need… See images below, first one has the move commented out, second, move is uncommented.

Any suggestions on what I can do to make the references more stable at an angle?

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
import Autodesk 
from Autodesk.Revit.DB import *
import traceback

doc = DocumentManager.Instance.CurrentDBDocument
activeView = doc.ActiveView

result = []

#Options for Geometry Selection
opt1 = Options()
opt1.ComputeReferences = True
opt1.IncludeNonVisibleObjects = True
opt1.View = activeView

#Create Ref Array
refArray = ReferenceArray()

#Conduit Offset
conFit = doc.GetElement(ElementId(1036830))
conFit1 = doc.GetElement(ElementId(1036834))

conGeom = conFit.get_Geometry(opt1)
for c in conGeom:
	if "Line" in c.ToString():
		eP0 = c.Origin
		eP1 = c.Direction


		
#Does Center to Center for Conduit Fittings
conRef = Reference.ParseFromStableRepresentation(doc, conFit.UniqueId + ":0:INSTANCE:" + conFit.Symbol.UniqueId + ":1:SURFACE")
#Does Center to Center for Conduit Fittings
conRef1 = Reference.ParseFromStableRepresentation(doc, conFit1.UniqueId + ":0:INSTANCE:" + conFit1.Symbol.UniqueId + ":1:SURFACE")


refArray.Append(conRef)
refArray.Append(conRef1)
TransactionManager.Instance.EnsureInTransaction(doc)
if None not in refArray and refArray != 1:
	try:
		line = Line.CreateUnbound(eP0, eP1)
		newDimension = doc.Create.NewDimension(activeView, line, refArray)
		result.Add("Dimension Created")
	except:
		message = traceback.format_exc() 
		result.Add([message, "Create Dimension Failed"])	
		
TransactionManager.Instance.EnsureInTransaction(doc)
distanceFromObject = 5#can be changed to input later 
location = XYZ(0,distanceFromObject,0)			
ElementTransformUtils.MoveElement(doc,newDimension.Id,location)
TransactionManager.Instance.TransactionTaskDone()


try:
	OUT = result
	
except:
	OUT = result


I can keep stability on any Conduit/Fitting, just not from a Conduit Fitting to a Conduit Fitting:

the dimensions without the move function are closest to the elements, the second run with the move command back in is the further our section.

Another iteration, that has some much different results, and has some looping in it it shouldn’t. Using the guts from from GeniusLoci and MEPOver Nodes along with my adjustments.

import clr
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
from System.Collections.Generic import List
import collections
import traceback

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

result = []

activeView = doc.ActiveView
#conduitFEC = FilteredElementCollector(doc, activeView.Id).OfCategory(BuiltInCategory.OST_Conduit).WhereElementIsNotElementType().ToElements()
#conduitFittingFEC = FilteredElementCollector(doc, activeView.Id).OfCategory(BuiltInCategory.OST_ConduitFitting).WhereElementIsNotElementType().ToElements()	

def nextElements(elem):
	listout = []
	if elem.GetType() == Connector:
		conn = elem
		for c in conn.AllRefs:
			if c.Owner.Id.Equals(elem.Owner.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
		return listout
	try:
		connectors = elem.ConnectorManager.Connectors
	except:
		connectors = elem.MEPModel.ConnectorManager.Connectors
	for conn in connectors:
		for c in conn.AllRefs:			
			if c.Owner.Id.Equals(elem.Id):
				continue
			elif isinstance(c.Owner,MEPSystem):
				continue
			elif c.Owner.Id.Equals(ownerId):
				continue
			else:
				newelem = c.Owner
			listout.append(newelem)
	return listout


	
def collector(elem):
	cont = 0
	elements = nextElements(elem)
	for x in elements:
		if x.Id in lookup:
			cont += 1
		else:
			item = doc.GetElement(x.Id)

			lookup[x.Id] = item
			collector(x)
	if cont == len(elements):
		return elem
		




sel = uidoc.Selection
ot = Selection.ObjectType.Element
elemParamRef = sel.PickObjects(ot, "Select Elements to Start Dimension From")
elements = [doc.GetElement(ePR) for ePR in elemParamRef]
#For each selected element:
runId = []
t = []




for e in elements:
	refArray = ReferenceArray()
	#listout = []

	try:
	#Conduits
		connectors = e.ConnectorManager.Connectors
		for conn in connectors:
			for c in conn.AllRefs:
				lookup = collections.OrderedDict()
				ownerId = c.Owner.Id
				collector(c)
				#listout.append(lookup.Values)
					
	except:
		try:
	#Conduit Fittings
			connectors = e.MEPModel.ConnectorManager.Connectors
			for conn in connectors:
				for c in conn.AllRefs:
					lookup = collections.OrderedDict()
					ownerId = c.Owner.Id
					collector(c)
					#listout.append(lookup.Values)
						
		except:
			result.Add("Something Failed")	

	combined = [e]
	combined.extend(lookup.Values)
	i=0
	while i < len(combined):
		refArray.Clear() 
		for lV in lookup.Values:
			opt1 = Options()
			opt1.ComputeReferences = True
			opt1.IncludeNonVisibleObjects = True
			opt1.View = activeView
			
			try:
				conGeom = lV.get_Geometry(opt1)
				for cG in conGeom:
					if "Line" in cG.ToString():
						eP0 = cG.GetEndPoint(0)
						eP1 = cG.GetEndPoint(1)
				conRef = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.UniqueId + ":2:LINEAR")
				conRef1 = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.UniqueId + ":1:LINEAR")
				refArray.Append(conRef)
				refArray.Append(conRef1)
				if None not in refArray and refArray != 1:
					try:
						line = Line.CreateBound(eP0, eP1)
						TransactionManager.Instance.EnsureInTransaction(doc)
						newDimension = doc.Create.NewDimension(activeView, line, refArray)
						TransactionManager.Instance.TransactionTaskDone()
						result.Add("Dimension Created")
					except:
						message = traceback.format_exc() 
						result.Add([message, "Create Dimension Failed"])
						
				TransactionManager.Instance.EnsureInTransaction(doc)
				distanceToObject = 6#can be changed to input later 
				location = XYZ(0,distanceToObject,0)			
				ElementTransformUtils.MoveElement(doc,newDimension.Id,location)
				TransactionManager.Instance.TransactionTaskDone()
				i += 1				
		
				
			except:
				conGeom = lV.get_Geometry(opt1)
				for cG in conGeom:
					if "Line" in cG.ToString():
						eP0 = cG.Origin
						eP1 = cG.Direction
				conRef = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.Symbol.UniqueId + ":0:LINEAR")
				conRef1 = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.Symbol.UniqueId + ":1:LINEAR")
				#conRef2 = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.Symbol.UniqueId + ":2:LINEAR")
				refArray.Append(conRef)
				refArray.Append(conRef1)
				#refArray.Append(conRef2)
				if None not in refArray and refArray != 1:
					try:
						line = Line.CreateUnbound(eP0, eP1)
						TransactionManager.Instance.EnsureInTransaction(doc)
						newDimension = doc.Create.NewDimension(activeView, line, refArray)
						TransactionManager.Instance.TransactionTaskDone()
						result.Add("Dimension Created")
					except:
						message = traceback.format_exc() 
						result.Add([message, "Create Dimension Failed"])
						
				TransactionManager.Instance.EnsureInTransaction(doc)
				distanceToObject = 6#can be changed to input later 
				location = XYZ(0,distanceToObject,0)			
				ElementTransformUtils.MoveElement(doc,newDimension.Id,location)
				TransactionManager.Instance.TransactionTaskDone()
				refArray.Clear() 
				
				conGeom = lV.get_Geometry(opt1)
				for cG in conGeom:
					if "Line" in cG.ToString():
						eP0 = cG.Origin
						eP1 = cG.Direction
				conRef = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.Symbol.UniqueId + ":0:LINEAR")
				conRef2 = Reference.ParseFromStableRepresentation(doc, lV.UniqueId + ":0:INSTANCE:" + lV.Symbol.UniqueId + ":2:LINEAR")
				refArray.Append(conRef)
				refArray.Append(conRef2)
				if None not in refArray and refArray != 1:
					try:
						line = Line.CreateUnbound(eP0, eP1)
						TransactionManager.Instance.EnsureInTransaction(doc)
						newDimension = doc.Create.NewDimension(activeView, line, refArray)
						TransactionManager.Instance.TransactionTaskDone()
						result.Add("Dimension Created")
					except:
						message = traceback.format_exc() 
						result.Add([message, "Create Dimension Failed"])
						
				TransactionManager.Instance.EnsureInTransaction(doc)
				distanceToObject = 6#can be changed to input later 
				location = XYZ(0,distanceToObject,0)			
				ElementTransformUtils.MoveElement(doc,newDimension.Id,location)
				TransactionManager.Instance.TransactionTaskDone()
				refArray.Clear() 
				i += 1				
		
		 
				


try:
	OUT = result, lV
	
except:
	OUT = result