Learn about Selection class in Revit API Docs
Learn the basics of Revit API Selection
Code Practical Examples
Are you Ready?
Selection Class
To control selection in Revit with python we need to use Selection Class.
It has plenty of helpful methods. So let's go through most used ones for selection and understand how to use them.
Methods:
GetElementIds
PickElementsByRectangle
PickObject
PickObjects
SetElementIds
PickBox
PickPoint
Get Selection Instance
To use any of these methods we need to get an instance of Selection Class.
Luckily, there is a property .Selection in our uidoc instance. So it's very simple to get it
💡We can also specify type of a variable so pyCharm can give us better autosuggestions by using '#type: …' syntax.
👆 We will need that for all the snippet below!
1️⃣ GetElementIds Method
This method will return list of ElementId of your current selection in Revit UI. I love using it, as it can be a very quick approach to select elements.
Since this method returns us ElementIds, don't forget to turn them into actual elements, so we can read their properties and methods.
💡Optionally, we can also introduce very basic filter by checking type of elements.
2️⃣ PickObject Method
This is great for selecting a single element.
But be aware that it returns a Reference. Luckily, we can use the same doc.GetElement() method and provide reference instead of an ElementId.
💡 We can also create a for/while loop and ask user to select single element over and over and do something with it. For example we could change color overrides of each element one by one by clicking on them.
3️⃣ PickObjects Method
This method is similar to single PickObject. But this time we would need a list comprehension to convert all references to actual elements.
4️⃣ PickElementsByRectangle Method
This method is great, because it's a single line of code, and it already returns you list of Elements instead of ElementIds.
But unfortunately, there is no confirmation needed on your selection. So if you selected wrong elements, you would need to run your tool again to correct it. And if you want to fix that, PickObjects is going to solve that issue for you.
5️⃣PickBox Method
Alternatively, we can ask user to Pick a Box. PickedBox has properties to get 2 points:
6️⃣ PickPoint Method
We can also ask user to PickPoint to get XYZ that represent points in Revit API. This might be useful for many tools in the future.
💡 Keep in mind that Revit API stores coordinates with Internal Coordinate System and uses Feet as internal units. So you might need to convert both systems and units when you work with them.
7️⃣ SetElementIds Method
Lastly, we can also modify the Revit UI selection by using SetElementIds Method. You will notice that it takes a single argument - ICollection[ElementId].
💡This is a special container from .NET framework that specifies the type of element that can be added. This provides better control since any other type would give an error if we try to add it to such container.
In python we can use System.Collection.Generic.List since it's based on the same parent class. Here is how to import it:
To use it, we need to use square brackets to specify allowed types and then provide python list in parenthesis to convert into List[T].
Here is an example to get all walls in the project and select them in Revit UI.
💡 EXTRA: FEC Anatomy
I will go a little ahead of myself and mention FilteredElementCollector.
It's a class to get elements from Revit projects by using various filters. It's one of the most used classes and there is going to be a whole module about it. For now I just want to mention the Anatomy of it, so you get a little bit more familiar with it.
P.S.
It might look intimidating because it's very long, but it's one of the most repetitive classes. So don't be afraid, you can copy and modify very little to get different results. But be patient, I will teach you more about it soon.
Spent the next 10-20 minutes practicing selecting elements in Revit API. Try to use different methods, and it's best if you code it from scratch instead of copying the final solutions.
Try using these methods:
You need to practice writing code yourself.
⌨️ Happy Coding!
This is an older lesson that cover the same lesson materials. It might be useful if you'd like to understand this lesson better.
⌨️ Happy Coding!