Hide elements in linked file

Hide elements in linked file

Anonymous
Not applicable
8,218 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,219 Views
24 Replies
Replies (24)
Message 2 of 25

Revitalizer
Advisor
Advisor
Accepted solution

Hi sanjaymann,

 

elements of the linked file are not listed the UI selection since the top level element is still the relating RevitLinkInstance.
There is no relating ElementId you could use with View.Hide method.

So no way to solve your problem.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 3 of 25

Anonymous
Not applicable

Dear Revitalizer,

 

I got your point but I think if revit is able to do the same thing manually. It should be able to it via code too. When you select an element in the linked doc and hide it manually revit might be keeping in mind some sort of name , id, geometry that it has to hide in the linked doc.

 

Thanks & Regards

Sanjay Pandey

0 Likes
Message 4 of 25

Revitalizer
Advisor
Advisor

Hi sanjaymann,

 

no, there are many things that can be done manually but not via code.

For example, the Material creation API: you can define colors and transparency values, but you cannot define textures.

The amount of the available API functions increases every year, but at the moment, there are still many missing ones.

 

In this forum, I see many postings of people who say "but there must be a solution for my problem".

They must accept that some things are not possible to perform, yet.

Of course, sometimes there are workarounds, less or more ugly attempts to bypass problems.

Often they use unsupported functions, so it ist strongly recommended not to use them in productive code.

 

For your problem, I've spent some thoughts about several ways to solve it, but at least, none of the approaches will work properly.

So, there is neither a real solution nor a workaround, from my side.

 

 

Best regards,

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 5 of 25

CADdaddy.com
Collaborator
Collaborator

This thread is a few years old. Does anybody know if this functionality has been added to Revit API yet?

 

James

Message 6 of 25

SONA-ARCHITECTURE
Advocate
Advocate

HI,

 

Does anywhone can confirm that il the Revit API 2018, it's still not possible to hide walls (or an other category) in linked files?

Thx

Pierre NAVARRA
SONA-Architecture.
http://www.sona-architecture.com
https://fr.linkedin.com/in/pierre-navarra-62032a107
Message 7 of 25

sgermanoZ2Y2F
Enthusiast
Enthusiast

Bump - any updates on being able to hide elements in links via api?

0 Likes
Message 8 of 25

james.levieux
Advocate
Advocate

Hi All,

 

Checking in again on this issue with release 2022.  No joy.

 

This is a very important functionality that we need for our interior design firm.  We work on large hotel projects within the architect's linked model.  If we are working on a design option within the architects model, we need to be able to remove certain bits of the architectural work so we can draw our revised layout proposals.  This is extremely painful to do under the current circumstances.  It is crucial to our work to be able to hide and unhide element within linked models.

 

James

 

//get a view in document
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfCategory(BuiltInCategory.OST_Views);
views = collector.ToElements();
View view = (View)views[0];

//get the linked_doc
Element elem = new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance)).ToElements()[0];
RevitLinkInstance linked_instance = elem as RevitLinkInstance;
Document linked_doc = linked_instance.GetLinkDocument();

//container to hold element ids
IList<ElementId> IDs = new List<ElementId>();

//Select objects in linked model
IList<Reference> reflist = m_uidoc.Selection.PickObjects(ObjectType.LinkedElement, "Pick elements in linked model");

//get id for each linked element
foreach (Reference rfr in reflist)
{
    Element currentE = linked_doc.GetElement(rfr.LinkedElementId);
    if (currentE.CanBeHidden(view))
    {
        //This line is skipped because the current linked element does not contain a CanBeHidden parameter
        IDs.Add(rfr.LinkedElementId);
    }
    //added this line to collect the IDs even though they haven't been checked for CanBeHidden
    IDs.Add(rfr.LinkedElementId);
}
//Hide doesn't work.  Error msg: "One of the elements cannot be hidden"
view.HideElements(IDs);
return Result.Succeeded;

 

0 Likes
Message 9 of 25

dhanait_ajay13
Enthusiast
Enthusiast

i am also searching for that, Does anybody know if this functionality has been added to Revit API yet?

0 Likes
Message 10 of 25

james.levieux
Advocate
Advocate

Yes, it was added in this latest 2023 version.

 

James

Message 11 of 25

dhanait_ajay13
Enthusiast
Enthusiast

i trying in revit 2023 but still i am not getting any method or solution for this issue

0 Likes
Message 12 of 25

james.levieux
Advocate
Advocate

I may have misled you.  What was added was: Autodesk.Revit.UI.Selection.SetReferences() which allows you to easily add linked elements to a selection set.  It certainly solves the selection difficulties.  I actually haven't yet tried to use this to programmatically hide elements.  Please give that a try and let me know if it works!

 

UIDocument uidoc = commandData.Application.ActiveUIDocument;

IList<Reference> elist =uidoc.Selection.PickObjects(ObjectType.LinkedElement, "Pick elements");

//newly exposed API in Revit 2023:Autodesk.Revit.UI.Selection.SetReferences()
if (elist.Count >0 )uidoc.Selection.SetReferences(refElemLinked);

 

 

0 Likes
Message 13 of 25

zefreestijl
Advocate
Advocate

Hi, I'm currently using API in 2024, 

tried HideCategory, HideElements, IsolateElements and IsolateCategory,

all the functions hide the entire model instead of the element I've given,

 

is there a way to hide linked model by elements / category like how we do in Revit App?

 

Thanks in advance :]

 

0 Likes
Message 14 of 25

lvirone
Enthusiast
Enthusiast

Hello everyone,

 

I've found a solution:

 

//Select elements using UIDocument and then use PostCommand "HideElements"

//elemsFromRevitLinkInstance is "List<Element>", there are the elements you want to hide in the link
var refs = elemsFromRevitLinkInstance.Select(x => new Reference(x).CreateLinkReference(revitLinkInstance)).ToList();

uidoc.Selection.SetReferences(refs);

uidoc.Application.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.HideElements));

 

Message 15 of 25

rmc9WR3X
Advocate
Advocate

@lvirone Can you provide a description of the process for using your code to hide elements only in a linked model? I am not familiar with the API and deploying a script like this.

0 Likes
Message 16 of 25

lvirone
Enthusiast
Enthusiast

Hello,

 

Here is an sample: Select the first RevitLinkInstance, retrieve the floors, hide the floors

                //Get a link
                var filter = new ElementClassFilter(typeof(RevitLinkInstance));
                var firstInstanceLink = (RevitLinkInstance)new FilteredElementCollector(doc).WherePasses(filter).ToElements().First();

                //Get its floors
                filter = new ElementClassFilter(typeof(Floor));
                var elemsFromRevitLinkInstance = new FilteredElementCollector(firstInstanceLink.GetLinkDocument()).WherePasses(filter).ToElements();

                //Isolate them
                var refs = elemsFromRevitLinkInstance.Select(x => new Reference(x).CreateLinkReference(firstInstanceLink)).ToList();

                uidoc.Selection.SetReferences(refs);
                uidoc.Application.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.HideElements));

 

Message 17 of 25

jeremy_tammik
Alumni
Alumni

Thank you Lorenzo, for the good idea and sharing your solution here and in the Revit Idea Station!

 

I added a note in The Building Coder to make your posts more discoverable:

    

https://thebuildingcoder.typepad.com/blog/2024/06/revit-2025-api-video-and-hiding-linked-elements.ht...

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 18 of 25

JeremyvU2GS6
Explorer
Explorer

This is awesome. Just checking, if I have 30 views with linked elements that need hiding. I have to open each view and do the post command, end and start process again ay? No quicker ways?

0 Likes
Message 19 of 25

lvirone
Enthusiast
Enthusiast

Hi @JeremyvU2GS6 

 

Yes unfortunately its a change in one view, and take care that hiding elements using this method made them difficult to unhide later. If you have such needs i suggest you to create filters as much as possible, though its not always possible. Its better to duplicate the view where you need to hide the elements in link, then when the user want a refresh you delete that view and regenerate a new one. Beware it will not be real time, if the link change then the user needs to update all the views again by using your command.

 

Trick to test: In a normal code without postCommand you have 30 views to change, its best to ask the user the views he want the change and you'll just apply the same code for each view !! BUT !! the postCommand will only apply to the activeView, I didn't try it but this post suggest we can change it: https://forums.autodesk.com/t5/revit-api-forum/change-activeview/td-p/5330067. If you succeed to change the activeView, it will still be complex: You need to create a custom command with your select process and apply change in the activeView with the postCommand, then in a second command change the activeView and call your custom command which you need to search if its possible, I suggest you this ressource: https://thebuildingcoder.typepad.com/blog/2013/10/programmatic-custom-add-in-external-command-launch.... So you'll have a succession of PostCommand with your custom command each following by a PostCommand with the Revit HideElements. Still I'm not sure it will works, I'm not sure when the PostCommand will be launched. Its very a complex task when you use a PostCommand, you need to mimick what the user do manually, but what I propose is worth a try.

 

I hope this help you until we have a better way to hide elements in links, good luck !

0 Likes
Message 20 of 25

JeremyvU2GS6
Explorer
Explorer

Got it working! It's slow but works. Means I can schedule linked elements now. Was using some idle process. Still just working on restarting the rest of the process after it's done. Appreciate the response! Thank you!