Revit-API-05

/

/

/

revit-api/05-revit-api-selection

Element Selection with Revit API

...

Revit-API-05

/

/

/

revit-api/05-revit-api-selection

Element Selection with Revit API

...

Summary

We're Gonna Look at the Selection Class in the Revit API Documentation

And before I'm gonna show you any code and how to do all of that with Revit API, we're gonna go to Revit API documentation. We're gonna have a closer look at the selection class.

So open any browser that you use and go to revit api docs.com. This is online version of rated API documentation, which was created by Kui Tele Rico. You could use just the latest version. Let's say that I look for the selection class.

Now we selected our selection class and you can see where does it come from. It comes from Autodesk Creative UI selection that might be useful. The name of the class, description of the class. Then syntax in this case doesn't tell us much. Then there are remarks and if you're lucky, there are gonna be some examples. This case, this example is an overkill. You can translate it with chat GPT if you want to. But I want you to point your attention right here. This is the only example really that you need. And these two lines are gonna get the element selection of the current document. And I know it's shore, but if you're gonna ignore the type hinting, so it's this, this, this and this, then that's gonna be fine. This is gonna be like Python.

And first of all, you need to get selection related to your document. And by document I mean Revit project. 'cause you can have multiple projects open and you need to get a selection related to specific project. And usually you provide UI doc from the active document. So it's not an issue. Once you're gonna get this selection, you'll be able to use its method like get element IDs, pick objects and I'm gonna show you more in a second. But you can see that first you need selection, then you're gonna use it. And also usually people write UI dog selection instead of defining selection itself. What we're gonna do both ways, don't worry.

Now let's click right here on this selection class, select methods. And we're gonna have look at this methods. You'll also notice that on the left you have only like maybe 10 different methods, but on the right you have lots of different methods. And this is because you have a lot of inherited methods from other elements. Like for example, this case inherited from object and you won't need it, don't worry.

Now let's just have a look. You have your get element IDs to get your current selection, you have your pick box to get kind of area and pick elements by rectangle. Pick single object, pick multiple objects, pick a point and set element IDs. These are the most common ones you need.

Let's start with get element IDs and it's one of the simpler ones to use as well. In here you can see the class where it comes from. Some description is gonna return you ideas of elements that are currently selected within the project. And in this case, in case of methods, this syntax is actually very useful. First of all, we can see how to use the method and it doesn't have any arguments so we just call it and that's it. That's great. But in here before you can see what is being returned. By using this method, this case, you're gonna get eye collection of element IDs and this eye collection, just think of it as a list. But in Python list you can put anything. You can put their text number, different classes. But in that net usually you define what kinda elements can go inside this list. And in this case it's an I collection where only element IDs can be. So there are gonna be only element IDs.

Alright, the thing enough with documentation for now, we're gonna come back to this along the lesson, but now let's just go to Revit and actually start coding 'cause this is what you are waiting for.

Reusing an Existing Button in Revit

Now first of all, we need a button and I'm just gonna reuse one of the existing buttons. So hold alt, click on it and you're gonna open it right here. You can go and kind of copy the button, change the name, restart your power Revit. But that's not really necessary. I'm just gonna use one of the placeholders.

And first of all, let's clean it up. I'm gonna remove the description. We're gonna write tutorial about Revit API selection and init. We're gonna write selection basics. Now let's have a look. You have a few imports, this just generic imports. We won't use any from here so you could remove it but it's good to always keep it. And this one I will mention a bit later, it's related to the type list. We'll need it in the end. Then in here there are the variables. We have our app for the application, then we have UI doc for the UI interface of your project. And then doc that relates to your project. This is just the common one to have them in every script. 'cause you often need doc or UI doc app is rarely used, but when you need it, it's good to already have it. And I'm gonna remove that one because this is from the placeholder. So let's begin coding and we're gonna get our current selection first of all.

Getting Our Current Selection

So to get our current selection, first of all it's good to get selection. So selection equals UI doc selection as you remember from the example. And also the AI assistant is helping us pretty good here. Now when you wanna use the methods, you're gonna write selection and you're gonna call any methods. And in the beginning I don't have any suggestions except for the AI assistant, which is doing great job. But for beginners, I also recommend that sometimes it's good to specify that this one is the type of selection. You'll highlight that this is not important. So we can import it from Autodesk, create it UI import UI selection, import selection. If you're gonna do that, this will unlock the auto complete because now pie charm knows that this selection variable have a type of selection and you also imported it so it knows what it is.

And first let's try to get the current selection. We want the IDs of anything in our project that is currently selected:

#1️⃣ Get Current Selection
sel_elem_ids = uidoc.Selection.GetElementIds()
sel_elements = [doc.GetElement(e_id) for e_id in sel_elem_ids]

This will give you a list of selected elements. If you have nothing selected in Revit, it’s going to be an empty list.

Prompting the User to Pick Objects

Now let's also push it to the next step. Let's say that you created a button and you need some selection. First of all, you're gonna check does user have anything selected? And if not, we wanna prompt user selection in another way. So we're gonna write here something like prompt pick objects if nothing is selected, oh and by the way this one, these are just windows emoji. You can hold window key, click on the dot and you're gonna open this kind of menu where you can select emojis.

To prompt new selection if nothing is currently selected, you can do:

#2️⃣ Prompt PickObjects if nothing is selected
if not sel_elements:
    ref_picked_objects = uidoc.Selection.PickObjects(ObjectType.Element, 'Select Elements')
    sel_elements       = [doc.GetElement(ref) for ref in ref_picked_objects]

And that's it—two ways of capturing user selection: from an existing selection or by prompting them to pick objects in Revit. Once you confirm your selection in the Revit UI, you can then have Revit hand you those elements for further processing.

Filtering for Walls

Next I'm gonna show you how to modify user selection in Revit. And for that we will also filter elements a little. Let's say that we wanna select bunch of elements. We're gonna filter walls and we wanna set our selection two walls. So first of all, let's just come here and write:

#3️⃣ Filter Walls
sel_walls    = [el for el in sel_elements if type(el) == Wall]
sel_wall_ids = [el.Id for el in sel_walls]

Now when you pick a bunch of different elements, you’ll end up with only walls in your sel_walls list. If you printed them out in Python, you’d see that everything else is filtered out.

Converting Python Lists to .NET Typed Lists and Setting the Selection

How do we set these walls to our selection? Let's go and have a look. We're gonna use our selection class methods and we need to look at the set element IDs. And here you can see the argument. We need to provide a list of element IDs. And this is I collection. It means it's a typed list so it has to be a list that can only contain element id. You see in Python list we can provide anything. You can put their numbers text, we can put, I dunno, different classes. But in that net there is this I collection or I list where you can specify the only elements that can be added. If you add anything else, it's gonna kinda give you a warning saying you cannot do that. And also you can see void. It means that it's not returning anything.

So we come back and see we have this:

#🔁 Convert Python list to .NET Typed List
List_sel_wall_ids = List[ElementId](sel_wall_ids)

Once we convert it, we can do this final step:

#4️⃣
uidoc.Selection.SetElementIds(List_sel_wall_ids)

Handling Errors

You might notice that if you don’t convert the Python list to a typed list, Revit will throw an error: Expected I collection element Id got a list. Don’t worry, you’re not going to break your project. It’s just Revit telling you, “Hey, I need a typed list, not just a Python list.” Revit’s API is pretty secure, and if you crash something, it just goes back to before you started your script.

Congratulations on Learning Revit API Selection

Alright guys, and this are the basics of selection with Ravi a p. As you can see, it's not that complicated. There are a few more examples and you can actually look inside my code library you have access to these code samples. But here there is BA basics on selection and you can see here is how to get selected elements. We already did it. Then how to pick elements, bar rectangle. You can see it's very simple how to pick an object. Here is like how to pick single object, how to pick multiple objects, how to pick a point, pick box, and set element IDs. Pretty much everything we covered, in case you are really curious about how I selection filter works, I'm just gonna show you a snippet right here. You can just copy and try it on your own. This is gonna limit selection to your walls.

And yeah, I can, congratulations, you've just learned the first REIT API concept selection. And before go any further, I want you to go and test it out yourself. Open any sample project in REIT and create a new button for selection and try using the code that I showed you. Uh, try to get selected elements, prompt pick objects, maybe filter some elements and maybe try to adjust it to your own needs a little bit and print results to make sure that it works. And once everything works, you're ready for the next lesson where we'll start looking inside the selected elements and we're gonna read some values. So hi coding and I'm gonna see you very soon.

HomeWork

⌨️ Happy Coding!

Questions:

What should I write in __init__.py file?

What should I write in __init__.py file?

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API