Oct 30, 2022

Isolate Elements with Revit API and python

There is no method to permanently isolate elements. So I will show you how to use .Excluding method of FilteredElementCollector to hide everything except elements we need.

❓How to Isolate Elements with Revit API

So you have come to the point where you wanted to isolate your elements and you could not find the right method. You might have seen IsolateElementsTemporary method but that's not exactly what we want.

There are probably many different ways to Isolate your elements, but I like to get elements I want to isolate, and then get elements to hide using .Excluding method of FilteredElementCollector to get all elements except for the ones I want to isolate and hide them (if possible).

# -*- coding: utf-8 -*-
# ⬇️ Imports
from Autodesk.Revit.DB import *
import clr

clr.AddReference('System')
from System.Collections.Generic import List

# 📦 Variables
uidoc       = __revit__.ActiveUIDocument
doc         = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView


# 🎯 Function
def isolate_elements(elements, view):
    # type:(List[ElementId], View) -> None
    """Function to Isolate Elements in the given View."""

    # ❌Elements to Hide
    hide_elem = FilteredElementCollector(doc, view.Id)\
        .Excluding(elements)\
        .WhereElementIsNotElementType().ToElements()

    # 🔎 Filter Elements that can not be hidden.
    hide_elem_ids = [e.Id for e in hide_elem 
                      if e.CanBeHidden(view)]

    # 🔅 Isolate Elements
    view.HideElements(List[ElementId](hide_elem_ids))


# 🎯 Main
t = Transaction(doc, 'Isolate Elements')
t.Start()

# 🔦 Get Elements to Isolate
elements_to_isolate = [doc.GetElement(e_id) 
        for e_id in uidoc.Selection.GetElementIds()]


List_isolate_ids = List[ElementId](elements_to_isolate)

# ✅ Isolate Elements
isolate_elements(List_isolate_ids, active_view)
t.Commit()

__author__ = 'Erik Frits'

💡 I am getting selected elements just as an example, adjust to your own needs!

Want to Learn More?

🔥 Amazing Revit API Resources!

Join Revit API

Newsletter Today!

Join Us!

which is already read by 4500+ people!

Get short Revit API Lessons and Tips directly in your mail!