Regardless of discipline, Archi, Structural or MEP, labeling elements in a Revit model has always an essential yet painful part of the documentation process. This post will walk you through how to perform a simple yet powerful sort and label process.
The columns in the layout below will be used for demonstration, the same concept could be extended to other types of elements as well (with a tweak of course).
Label using Dynamo
With a few simple nodes, we can write data to each element’s parameter (“Mark” parameter was used in this case, but you could use any instance parameter fitting to your case).
A simple tag can be created to annotate the corresponding mark on each column. After tagging all Columns yields the following. Now, the sequence of results are random which is rather meaningless, but we could perform some sorting to rearrange the labels.
Python sorted( ) method
There are ways to perform sorting in Dynamo by using nodes such as SortByKey or SortByFunction. But I found that rather inefficient and messy in comparison to Python, especially when performing nested sorting functions. After I discovered the sorted( ) method in Python, I rarely used nodes for sorting again. The sorted( ) method’s syntax and parameters are as follows.
sorted( iterable, key= key, reverse= bool )
|Iterable (Required)||The sequence to sort, list, dictionary, tuple etc.|
|Key (Optional)||A Function to execute to decide sequencing. Default is None|
|Reverse (Optional)||A Boolean type. True will sort descending, False will sort ascending. Default is False|
Now, that we have a general understanding of sorted( ) method, let’s implement sorting of the columns by its X-coordinates, then by its Y-coordinates. The latter looks like this.
import clr import math clr.AddReference('ProtoGeometry') from Autodesk.DesignScript.Geometry import * columns = IN sortedElements = sorted (columns,key=lambda col:( math.ceil( col.GetLocation().X ), math.ceil( col.GetLocation().Y ))) OUT = sortedElements
That’s it! The results are as follows. If you are curious to understand a little more about the code, do read on.
There’s essentially only 1 line of code, but it could be rather intimidating to beginners. Don’t worry I was there not long ago, but I’ll try my best to walk you through.
The first parameter
columns , which is an iterable, refers to the list of columns we want to sort.
The second parameter, which requires keys, is the tricky one.
lambda col: allows us to access individual column’s properties in the iterable
columns , using
col as an identifier (You could define any identifier you want).
col.GetLocation( ).X retrieves the X-coordinate and
col.GetLocation( ).Y retrieves the Y-coordinate. These anonymous functions are the most important part of the entire line of code, as it sorts the list by X then by Y. If you want to sort it by Y then by X, just simply reverse the order.
math.ceil helps negate the minor dimension inaccuracies present in Revit, which is inevitable even if you aligned the columns perfectly.
Now, with this understanding, you could nest even more sorting logic to achieve your objective by simply adding another key in the function. Perhaps sorting by level, or Z-coordinates or even by type depending on your case.
If you have such encounters or a big idea you wish to implement and require some help, I’d love to hear about it. Drop me a DM and let’s discuss! Else, as always, happy coding!