Resources
Summary
Beginning the Revit API Journey
To get or modify user's selection we will be using Selection Class from Revit API. And it's not a complicated one.
Let's look at this class in Revit API Docs.
Revit API Docs: Selection Class
Once you start exploring Selection Class, there is not so much you need. As I've mentioned previously, you will often find code examples in Revit API docs. They are not always great, but you can often extract something useful from them.
In this case, the example is a little confusing, however there are 2 very important lines we could use.
Notice that first of all we need to get Selection related to our Revit project.
And only then, we can use it to Get or Set current selection and prompt various selection methods.
In this example GetElementIds method will return a list of Ids from selected elements.
Selection Examples
Let's go over all simple Selection methods and I will provide you simple code snippets that you can copy and try out yourself.
Also, keep in mind that some methods might take multiple arguments, but I will use each with the simplest option. You will figure out the rest when you need it.

Also, keep in mind that these methods might return you different things (Ids, References, XYZ points and so on…)
For example GetElementIds()
will return ICollection[ElementId]
, which means a list of ids.
You can see what is being return in front of the method name in the Docs.
Let's look into these methods one by one.
Selection.GetElementIds()
Returns the ids of the elements that are currently selected within the project.

Selection.PickElementsByRectangle()
Selection.PickObject(ObjectType)
⚠️Make sure to import ObjectType to use it as an argument.
💡ObjectType Enumeration
ObjectType Enumeration is a of allowed types to select from for selection operations.
It's used for PickObject and PickObjects methods.
Selection.PickObjects()
⚠️Make sure to import ObjectType to use it as an argument.
Selection.PickPoint()
Selection.PickBox()
⚠️Make sure to import PickBoxStyle to use it as an argument.
Selection.SetElementIds()
Change selection in Revit UI.
⚠️You need to use .NET typed list with ElementId elements inside of it.
List[ElementId]()
Notice that you need to provide ICollection[ElementId]
. It just means that you need to use a typed list from .NET framework. And don't worry it's just extra line of code.
Firstly, look at this example where I get all walls with FEC class (you will learn about it later). In this case, it returns typed list of Ids, so I can use it directly.
Selection.SetElementIds() - Example 2
However, if you create your list of element ids differently, then you would need to convert it into typed list. And it's just a few steps.
Get python list of ElementIds
Import .NET List
Convert Python list in .NET List
You will notice an unusual syntax of using List[SpecifiedTypeName]()
. That's how you create typed list and specify what kind of elements can go inside. If you try to add anything else, it will give you a warning.
Think of it as a bouncer that checks you elements before the join the party.
HomeWork
Now it's your turn to select elements with Revit API.
Scroll through the code examples and try to use them yourself:
Get User Selection
PickObject
PickObjects
Never assume that just by looking at someone doing it will be enough to learn something. You need to get your hands dirty and actually execute code and see what you get.
Practice is the best way to learn and mistakes are the greatest teachers.
⌨️ Happy Coding!