Problem with CreateNewDimension using python

Problem with CreateNewDimension using python

Gabali
Observer Observer
451 Views
2 Replies
Message 1 of 3

Problem with CreateNewDimension using python

Gabali
Observer
Observer

Hi, I'm still new to coding so any ideas will be a huge help for me.
I've written code in Python with the help of Pyrevit to auto-create dimensions between the interior face of the core of walls in a project .. there's no error in the code but the created dimensions are not what you would expect one is hidden and one is not properly aligned as shown in the attached image. any ideas why?
here is the code:

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


# Get the current document and view
doc = __revit__.ActiveUIDocument.Document
view = doc.ActiveView


# Get the walls in the active view
walls = FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements()

lines = []
for wall in walls:

# Get the location line of the wall
wallCrv = wall.Location.Curve
sp = wallCrv.GetEndPoint(0)
ep = wallCrv.GetEndPoint(1)
wallDir = ep - sp
wallW = wall.Width

#get core width
wall_type = doc.GetElement(wall.GetTypeId()) # Get the wall type
compound_structure = wall_type.GetCompoundStructure() # Get the compound structure
corewidth = 0 # Initialize the variable to store the sum of core layer widths for the current wall
noncorewidth = 0

coreLayerFound = False
# Loop through each layer and check if it is a core layer
for layer_index in range(compound_structure.LayerCount):
layer_function = compound_structure.GetLayerFunction(layer_index) # Get the layer function

if layer_function == MaterialFunctionAssignment.Structure: # Check if it is a core layer
layer_width = compound_structure.GetLayerWidth(layer_index) # Get the layer width
corewidth += layer_width # Add the layer width to the corewidth variable
coreLayerFound = True
else:
layer_width = compound_structure.GetLayerWidth(layer_index) # Get the layer width
if coreLayerFound:
noncorewidth += layer_width

off = (wallW/2) - noncorewidth
# Create a line from the face points
if wall.Flipped :
walloff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(off)
else:
walloff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(off)

spo = sp + walloff
epo = ep + walloff
line = Line.CreateBound(epo,spo)

t = Transaction(doc, "detailLine")
t.Start()
Dline = doc.Create.NewDetailCurve(view,line)
t.Commit()
lines.append(Dline)


pairs = []
refArray = DB.ReferenceArray()
for i in range(len(lines)):
line1 = lines[i]
dir1= line1.GeometryCurve.Direction



for j in range(i+1,len(lines)):
line2 = lines[j]
dir2=line2.GeometryCurve.Direction
if dir1.IsAlmostEqualTo(dir2) or dir1.IsAlmostEqualTo(-dir2):
pairs.append((line1,line2))


for pair in pairs:
p1 = pair[0].GeometryCurve.GetEndPoint(0)
p2 = pair[1].GeometryCurve.GetEndPoint(0)
refArray.Append(DB.Reference(pair[0]))
refArray.Append(DB.Reference(pair[1]))
# Get the offset vector perpendicular to the wall direction
Doff = wallDir.CrossProduct(XYZ.BasisZ).Normalize().Multiply(-off*4)
#p1 = p1 + Doff
#p2 = p2 + Doff
DimLine = DB.Line.CreateBound(p1,p2)
t = Transaction(doc, "dim")
t.Start()
Dime = doc.Create.NewDimension(view,DimLine,refArray)
t.Commit()

 

 

 



Gabali_0-1703066114114.png

 

 




0 Likes
Accepted solutions (1)
452 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Try to create model lines displaying the points that you are calculating first, before creating dimensions. Then you will be able to see what you are doing and maybe understand what the problem might be.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 3

Gabali
Observer
Observer
Thanks a lot for your answer 🙂 ... it helped a lot .. managed to solve the problem .. got new problems to work on
0 Likes