LearnRevitAPI
Oct 30, 2024
👉 Master Selection in Revit API
You need to master Selection
if you want to use Revit API🪄.Getting elements is the first step to test your code, so it makes perfect sense to begin with Selection.And it's not so complicated.
Let me teach you the basics in just a few minutes.
Selection Class
Let's begin with the documentation. I know it sounds boring, but coding without docs is like sailing with wind... Not very fun.
There is a Selection Class
in Revit API which allows you:
Get user selection
Prompt Selection methods
Set new selection in Revit's UI
Here is the screenshot on Methods:
This class is located in Autodesk.Revit.UI.Selection
Namespace.
By exploring this class you will learn about various available methods for selection. And in general they are self-explanatory.
How to use Selection methods?
To use any Selection
methods, you first need to get an instance of Selection related to your active project.
Luckily, there is a Selection property in uidoc variable.
Here is how to get it
Now you can use this variable to apply any methods.
💡Importing Selection
and making type-hinting with #type:..
. is optional to get auto-complete.
1. GetElementIds()
Let's begin with the most basic one - GetElementIds
This method returns the ids of the elements that are currently selected within the project.
Here is how to use it:
get
selection
instanceGetElementIds
(current selection)convert
ElementIds
toElements
filter to
Walls
(optional)
And that's it.
2. PickObjects()
Let's look at the second most used method - PickObjects
.
This will prompt user to select elements and confirm the selection before we will get them in the code.
Here is how it works:
get
selection
instanceimport
ObjectType
(it's necessary as argument)get References by using
PickObjects
convert
References
toElements
Check results
💡 Once you start selecting elements in Revit UI, you need to click on Finish
in the top left corner to confirm selection.
3. PickElementsByRectangle()
Another way we could prompt selection is to use PickElementsByRectangle
method.
This one is very simple, doesn't take any arguments and it returns you a list of Elements, so you don't even need to convert anything.
Here is how it works:
get
selection
instancePickElementsByRectangle
that's it
4. SetElementIds()
Lastly, let me show you how to change user selection in Revit UI with the code. It has a lot of use-cases:
Filter Selection
Select elements with warnings
Select elements that match certain criteria
and so on...
Keep in mind that SetElementIds method needs a type list. List[ElementId]()
, so don't forget to convert your python list to type list if necessary. (let me know if I should write about it too)
In case of FilteredElementCollector
this is not necessary, since it already returns a typed list of element Ids.
Here is a simple example to select all walls:
get
selection
instanceget
ElementIds
of all WallsSetElementIds
💡No need to use Transaction
. We are not making any changes, we just modify selection in Revit UI.
Summary
And that's the basics of Selection in Revit API in just a few minutes.
It might sounded complicated when we started, but now once you have all these code examples - it's easy.
👉 Test these snippets in Revit and start using them in your tools.
Testimonial Highlight
I love hearing about your experience with my E-Books, StarterKits, Newsletter and other lessons I make.
Here is the recent one from Philipp:
Want to share your story?
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 7400+ people!
◼️BLACK FRIDAY DEAL 20% OFF. Ends in: