Isolate Elements

#🧬 Function
def isolate_elements(list_el_ids, view):
    """Isolate Provided Elements in a View by using .Excluding method in FEC."""
    
    # 🔎 Get Elements To Hide with .Excluding(isolate_elems)
    List_isolate_el_ids = List[ElementId](list_el_ids)
    hide_elems          = FilteredElementCollector(doc, view.Id)\
                                         .Excluding(List_isolate_el_ids)\
                                         .WhereElementIsNotElementType()\
                                         .ToElements()
    
    #🔬Ensure Can be Hidden                                     
    hide_elem_ids       = [el.Id for el in hide_elems if el.CanBeHidden(view)]

    #🚫 Hide Elements
    List_hide_el_ids = List[ElementId](hide_elem_ids)
    view.HideElements(List_hide_el_ids)

#--------------------------------------------------
# 🎯 Main
with Transaction(doc, 'Isolate Elements') as t:
    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, doc.ActiveView)


    t.Commit() #🔒
# -*- 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'


⌨️ Happy Coding!
Erik Frits