I am working on a Dynamo script in Revit to organize and number elements in a project systematically. Specifically, I am working with MEP elements and need to assign sequential numbers to elements based on their level and location.
The Problem
Despite sorting the elements by level, Dynamo seems to assign numbers to the elements in a random order when considering their spatial location. I want the numbering to follow a logical progression starting from a specific point (e.g., the lowest X or Y coordinate) and then continue in an orderly fashion, rather than appearing arbitrary.
I tried using List.Sort
with Element.Location
to sort elements based on their coordinates (X, Y, or Z). However, I am either missing something or not implementing this correctly, as the numbering still does not follow the expected spatial order.
The Question:
How can I ensure that elements are numbered sequentially, starting from a specific spatial point and following an orderly progression based on their location (e.g., left to right or bottom to top)?Specifically:
- How can I extract the location data (X, Y, Z) of elements and use it to sort them correctly?
- Is there a reliable way to combine sorting by level and sorting by location to achieve this?
If anyone has an example or advice on ensuring both sorting and numbering are done logically and systematically, I’d greatly appreciate the help!
TKN_IE_Adding Parameter to the element.dyn (51.8 KB)
It’s highly unlikely you want to number stuff this way.
As a thought exercise, take ten marbles, and throw them at once into an empty room that is 100 x 100. By your system:
- The marble at (0,100) happens before the marble at (1,0). I hate this system as you have no idea how on the X or Y axis you need to go from the previous element to find the next.
- Marbles which all have X values of 1 all happen at the same time so you can get [(1,85), (1,2),(1,99),(1,0)]
The lack of constraint or order in the Y axis means that you don’t have meaningful relationships between the two means that ‘top to bottom’ or ‘left to right’ don’t really work, and you need to use both.
As such the process likely wants to go something like this:
- Get all things to renumber.
- Group the things by their level.
- Sort the groups by the associated level’s elevation
- Group the groups by the X value.
- Sort the nested groups by the associated X value.
- Sort the elements in each innermost group by the associated Y value. You should now have a list of lists of lists of elements (L4 if my mental exercise is good).
- Flatten each list at level 3 - this should be a list of lists of elements sorted by their X and then Y values.
- Renumber based on this order.
Personally I prefer first grouping by level, then grid quadrant, then by parameter of the primary grid, and finally sorting by parameter on the secondary grid. The level and gris marks get incorporated into the element’s mark to ensure we can always find ‘where’ something lives in the drawings or in real life (assuming you can count columns).
1 Like
Thank you so much for your detailed explanation! It really helped clarify the approach I needed to take.
I followed your suggestion and extracted the X coordinates of my elements. I used List.SortByKey
to sort the list based on their positions along the X-axis. This gave me an initial order, but I noticed something interesting: the base point of my project was set to (0,0) in Revit, and my project was rotated, which affected the sorting.
I’m currently working on adjusting the coordinate system or aligning the rotation to fix this. Once I’ve resolved the issue, I’ll share my complete solution here for others who might encounter a similar challenge.
Thanks again for your help—it’s greatly appreciated!
1 Like