Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Auto dimension with python

ellliaz
Participant

Auto dimension with python

ellliaz
Participant
Participant

 

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def get_lower_points(element):
    bounding_box = element.get_BoundingBox(uidoc.ActiveView)
    if bounding_box:
        min_point = bounding_box.Min
        max_point = bounding_box.Max
        lower_left_point = XYZ(min_point.X, min_point.Y, min_point.Z)
        lower_right_point = XYZ(max_point.X, min_point.Y, min_point.Z)
        return lower_left_point, lower_right_point



# Get the selected elements in the active view
selected_element_ids = uidoc.Selection.GetElementIds()

for element in selected_element_ids:
    # Get the elements using their ElementIds
    element1 = doc.GetElement(element)
    

    # Create a ReferenceArray to store the references
    refArr = ReferenceArray()
    refArr.Append(Reference.ParseFromStableRepresentation(doc, element1.UniqueId + ":0:INSTANCE:" + element1.Symbol.UniqueId + ":1:SURFACE"))
    refArr.Append(Reference.ParseFromStableRepresentation(doc, element1.UniqueId + ":0:INSTANCE:" + element1.Symbol.UniqueId + ":1:SURFACE"))

    # Retrieve the lower points of the bounding boxes
    lower_left_point, lower_right_point = get_lower_points(element1)
    
    
    

    # Use the converted value to set the point
    startPoint = lower_left_point


    endPoint = lower_right_point


     
    startPoint, endPoint

    # Start a transaction
    TransactionManager.Instance.EnsureInTransaction(doc)

    # Create the dimension
    doc.Create.NewDimension(doc.ActiveView, Line.CreateBound(startPoint, endPoint), refArr)

    # Commit the transaction
    TransactionManager.Instance.TransactionTaskDone()

 

Hello everyone,

i'm trying to automate the creation of dimensions for every selected element i've in the view. The script should create width dimension based on the bounding box of the element but instead its creating a 0 dimension in the lower center of the family. I know it's a little messy as written, but what is wrong?

 

 

0 Likes
Reply
633 Views
4 Replies
Replies (4)

RPTHOMAS108
Mentor
Mentor

You are adding two exact same references to the refence array (no difference between lines 32 & 33).

 

Regardless I'm almost certain that parsing references from a string is not the best approach in this instance. You are making assumptions about the order in terms references and their numbered parts. That order isn't going to be consistent for all geometry you deal with.

ellliaz
Participant
Participant

hi thanks for the reply, i was using " refArr.Append(Reference.ParseFromStableRepresentation(doc, element.UniqueId + ":0:INSTANCE:" + element.Symbol.UniqueId + ":1:SURFACE"))" because was the only method i could find to get correct referencearray. I mean the code is correctly iterating through each selected family and creating a dimension, tough in the wrong place. It should be a problem about the points?

0 Likes

RPTHOMAS108
Mentor
Mentor

To have a non zero dimension you need two references in the array that are not the same i.e. the dimension is measured between 'reference 1' and 'reference 2' (not refence 1 and reference 1).

 

Correctly iterating the families and adding a zero dimension for a random reference you come across isn't any measure of success for your task. I don't understand how you are deciding on the numbered parts of the reference? The usual approach is to analyse geometry and obtain references you identify from that. Building references from strings is only suitable for items that can't be obtained from geometry and where there is known logic as to how the references are numbered e.g. fill grids.

 

So unless you elaborate on your task I can't really understand where the strings used in the references are coming from?

0 Likes

ellliaz
Participant
Participant

i've seen from https://forums.autodesk.com/t5/revit-api-forum/extracting-reference-from-boundingbox-xyz/td-p/989692... that using the bounding box to get the points for dimensioning is wrong, also i was using in the wrong way the refarray.

 

It would be a better approach to use the left and right reference planes inside the family to get the dimension? i can't manage to find any information on how getreferences method works though. 

 

 

 

 

0 Likes