Hide Elements by Selection from a Linked Model

Hello,
I am trying to hide elements from a linked model by selection. This is in case we are not owners of the linked model and hence cannot assign parameters inside the linked model. In this particular case, there is no parameter that I can use to filter elements and have to resort to manually hiding elements. This is related to showing the sequence of construction. The error I am receiving is this:

**One of the elements cannot be hidden. **
Parameter name: elementIdSet

Code Below:

doc = revit.doc
uidoc = revit.uidoc


class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, category_name, document):
        self.category_name = category_name
        self.document = document

    def AllowElement(self, element):
        some_type = self.document.GetElement(element.GetTypeId())
        type_name = some_type.FamilyName

        if type_name == "Linked Revit Model":
            return True
        else:
            if element.Category.Name == self.category_name:
                return True
            else:
                return False

    def AllowReference(self, ref, point):
        element = self.document.GetElement(ref)
        some_type = revit.doc.GetElement(element.GetTypeId())
        type_name = some_type.FamilyName

        if type_name == "Linked Revit Model":
            li = clr.Convert(element, type(element))
            linked_document = li.GetLinkDocument()
            element = linked_document.GetElement(ref.LinkedElementId)

        if element.Category.Name == self.category_name:
            return True
        else:
            return False

def HideElements(view, elements):
    elsids_can_be_hidden = []
    for i in elements:
            elsids_can_be_hidden.append(i.LinkedElementId)
    
    clist_ids = List[DB.ElementId](elsids_can_be_hidden)
    view.HideElements(clist_ids)
    return None

with forms.WarningBar(title="Pick elements in linked model"):
          wall_collector_link=uidoc.Selection.PickObjects(ObjectType.LinkedElement,CustomISelectionFilter("Structural Framing", doc))
          listoflinkedelemeid = []
          for i in wall_collector_link:
              listoflinkedelemeid.append(i.LinkedElementId)
      
          try:
      
              mylist = List[DB.ElementId](listoflinkedelemeid)
              activeView = revit.uidoc.ActiveGraphicalView
              activeView.HideElements(mylist)
      
          except Exception as e:
              print (e)

I doubt this is possible at all.
I looked into it a while back and did not find a way.

Have you tried the CanBeHidden method on your elements?

I tried that first and that did not catch anything.

I thought so too after researching online but still wanted to ask in case someone has found a way. Thank you.

A not very elegant way, if the linked is workshared would be to create a routine that:

  1. Retains the selection of elements to hide
  2. Opens the linked model
  3. Puts the elements into a new workset “to be hidden”
  4. Reload the linked model and hides the newly created workset.

Everyone - I was able to do it using code below:

doc = revit.doc
uidoc = revit.uidoc

class LinkInstanceSelectionFilter(ISelectionFilter):
    def AllowElement(self, element):
        return isinstance(element, DB.RevitLinkInstance)
    def AllowReference(self, reference, position):
        return True

sel = revit.uidoc.Selection
selection_filter = LinkInstanceSelectionFilter()
selected_reference = sel.PickObject(ObjectType.Element, selection_filter, "Select a linked model")

# Get the RevitLinkInstance element
link_instance = revit.doc.GetElement(selected_reference.ElementId)

# Get the linked document from the selected RevitLinkInstance
linked_doc = link_instance.GetLinkDocument()
if linked_doc is None:
    raise Exception("Failed to retrieve the linked document.")

# Step 2: User selects elements in the linked model
with forms.WarningBar(title="Pick elements in linked model"):
    linked_sel_filter = LinkedElementSelectionFilter()
    selected_refs = uidoc.Selection.PickObjects(ObjectType.LinkedElement, linked_sel_filter)
    linked_element_ids = [ref.LinkedElementId for ref in selected_refs]

# Step 3: Create link references for selected elements
refs = [DB.Reference(linked_doc.GetElement(eid)).CreateLinkReference(link_instance) for eid in linked_element_ids]

# Step 4: Select these references in the UI
uidoc.Selection.SetReferences(refs)

# Step 5: Hide the selected elements in the active view
uidoc.Application.PostCommand(
    RevitCommandId.LookupPostableCommandId(PostableCommand.HideElements)
)
1 Like