Resources
Summary
Select Elements from Linked Models?
We covered selection for our regular projects.
But when we work with linked model we need to make a few things differently. And while it's not a rocket science, there are a few things I wanted to show you to save you from another headache!
Let's look at how to pick objects from linked models.
PickObjects for Linked Elements
Firstly, let's remember how do we PickObjects.
As you remember this method has a few variations with or without ISelectionFilter, but all of them require ObjectType Enumeration.
And one of the Members is LinkedElement. That's exactly what we will need.
So let's write a short snippet to PickObjects and provide ObjectType.LinkedElement.
💡 Keep in mind that it returns a List[Reference], so you need to convert to elements with GetElement.
👀Now let's look at what is being printed in the console.
We got a bunch of RevitLinkInstances.
That's not exactly what we expected…
Get Element from RevitLinkInstances
So how do we convert RevitLinkInstance to an actual element?
First of all the elements that we have selected are located in another Linked Project. So we need to use Document instance of that specific project instead when we use doc.GetElement !
And here is how we do that.
1️⃣ PickObjects
2️⃣ Get RevitLinkInstance with doc.GetElement
3️⃣ Get Linked Doc by using GetLinkDocument
4️⃣ Get Linked Element by using GetElementwith ref.LinkedElementId
👇 So here is the code snippet to do all that!
And when we use it, that's the results we can get.
So that's what you need to Pick Linked Elements.
But what about using ISelectionFilter for that?
ISelectionFilter for Linked Elements
Now let's have a quick reminder of ISelectionFilter.
We need to create a custom class and inherit this Interface. There are 2 methods we can override to control what is allowed to be selected:
Last time we only used AllowElement, because we worked with the main Document. This time we will have to use AllowReference as well, because Linked Elements are references to the linked document.
Here is a template of ISelectionFilter, we will break down AllowReference in a moment.
💡 As you remember these methods have to return True/False to filter out elements which are allowed to be selected. And remember how we got Linked Elements just a moment ago, because we will have to do the same thing here as well.
💡 Also, notice that AllowReference has different arguments!.
Let's make a snippet to only pick linked rooms. Here is how we would do that:
Learn by Doing: Pick Linked Rooms
We will start by creating an ISelectionFilter for Linked Rooms.
We will set AllowElements to True, and then configure AllowReference
Inside, we will need to access our linked elements similar as I showed you earlier.
And then by getting actual elements in linked model we can start making our tests and see if these elements are Rooms or anything else you would like to check.
Here is the Snippet for ISelectionFilter to Pick Linked Rooms.
Now let's use that filter together with PickObjects.
And that's how we can Pick Linked Objects with Revit API !
You will see that we can't select anything except for rooms in the Linked Project.
Final Snippet
Happy Coding!
I hope you find it useful in your future tools.
Here is the Final snippet, together!
💟 Credits to Early-Birds
I want to say huge thanks to a few Early-Birds in the course for this lesson:
Mohamed Bedair
Andreas Draxl
Nikita
They started discussing this topic in the community and shared the code that we all can use now. Thanks guys, you are the reason we have this lesson now!
⌨️Happy Coding!
⭐Bonus Snippet
Now let's have a look at a really cool tool that Mohamed Bedair has started after the lesson.
This script has a cool functionality, it will ask user:
Which Linked Revit Project can be selected?
What Categories of Elements can be selected?
💡Imagine working on a project with many linked Revit files, and you need to select certain categories from a certain Revit project. That tool will be a game changer.
If you are excited about this modification, tell thanks to Mohamed Bedair for sharing it with everyone!
P.S. I've refactored it a little to my style, but the main code is from Mohamed!
HomeWork
Give these snippet a try.
Additionally, try to modify it a little to select different kind of linked elements and see how it works.
💪 Theory is great, but nothing beats practice to learn something!
⌨️ Happy Coding!