Hide elements in linked file

Hide elements in linked file

Anonymous
Not applicable
8,233 Views
24 Replies
Message 1 of 25

Hide elements in linked file

Anonymous
Not applicable

Hi,

 

Manually this is possible in revit. I have main doc and a linked doc. Now I select one element from main doc and one element from linked doc; I do a right click and Hide In View - Elements. Both the selected element gets hidden.

 

Trying to do the same via code:

 

 

FilteredElementCollector links = new FilteredElementCollector(Command.doc).OfCategory(BuiltInCategory.OST_RvtLinks);
            foreach (Document d in Command.app.Documents)
            {

                coll = new FilteredElementCollector(d);
                coll.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true)));
                elemSet = new ElementSet();
                
                foreach (Element el in coll)
                {

                    try
                    {
                        if (el.Id == new ElementId(elementid1) || el.Id == new ElementId(elementid2)
                        {
                           

                            IList<ElementId> ilds = new List<ElementId>();
                            ilds.Add(el.Id);

                            ICollection<ElementId> elemIdsColl = (ICollection<ElementId>)ilds.Cast<ElementId>().ToList();

                          

                            Command.doc.ActiveView.HideElements(ilds);

                        }
                    }
                    catch (Exception sd)
                    {

                        string fg = sd.ToString();
                    }

                }

 

The code hides only the element of the main doc but does not hide the element of the linked doc.

 

 

Thanks & Regards

Sanjay Pandey

 

Accepted solutions (1)
8,234 Views
24 Replies
Replies (24)
Message 21 of 25

lvirone
Enthusiast
Enthusiast

@JeremyvU2GS6 happy to help. So you achieved to change the activeView then calling multiple postCommand one after another ? Or did you found another way ? Thanks

 

Yes, PostCommand is not the most optimized way to do stuff, so it can be slow. When you touch to UI it can slow the process too.

0 Likes
Message 22 of 25

JeremyvU2GS6
Explorer
Explorer

Yup watch it open each view and operate restarting each time. I can post code later if you need 

Message 23 of 25

lvirone
Enthusiast
Enthusiast

@JeremyvU2GS6 Thank you for confirming the process I described works, happy to have helped !

0 Likes
Message 24 of 25

DollyPortis-WPM
Participant
Participant

Here is the python code to select elements by selection and hide them:

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)
)
0 Likes
Message 25 of 25

sragan
Collaborator
Collaborator

Here is my code for a Revit 2025 Macro using the new VS Code Editor, and letting the user pick the elements to hide:

 

public void HideLinkedElements()
        {
            UIDocument uidoc = this.ActiveUIDocument;  
            Document doc = this.ActiveUIDocument.Document;

            IList<Reference> elist = uidoc.Selection.PickObjects(ObjectType.LinkedElement, "Pick Elements");
            if (elist.Count > 0) uidoc.Selection.SetReferences(elist);
            //TaskDialog.Show("Number of Elements", elist.Count.ToString());
            uidoc.Application.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.HideElements));
            //uidoc.Application.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.HideCategory));
            //uidoc.Application.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.HalftoneOrUnderlay));
            //TaskDialog.Show("Hidden", "Hidden");

        }

 

You have the option of using HideCategory or HideElements.   The HalftoneOrUnderlay setting opens the halftone dialog box, but it only seems to work for the entire link.

 

It is a little slow - a couple of times I didn't think it worked because I didn't wait a second or two.   And sometimes it doesn't seem to work when only selecting one element.   It seems like either just selecting elements or Tab-Selecting does the same thing, except sometimes Tab-select seems to work better with single element selections.

 

Also, it's not that hard to unhide elements - just select "Revel Hidden Elements", and pick and right click to "unhide".

0 Likes