Programmatically hiding elements in views

Programmatically hiding elements in views

alinadCX2LJ
Participant Participant
675 Views
3 Replies
Message 1 of 4

Programmatically hiding elements in views

alinadCX2LJ
Participant
Participant

Hi,

I have been trying to hide the host element of the door in the attached family. For some reason my code is not able to do it, however, I am able to hide the wall as a user in the RevitUI.

Extremely appreciate every help in advance!

 

Document document = App.RevitAddIn.RevitEnvironment.GetActiveDocument(true);
if (document != null)
{
    View activeView = document.ActiveView;
    //For simplification, I selected every wall category since there is only one instance
    //of a wall in this FamilyFile
    Element e = new FilteredElementCollector(document, activeView.Id)
                                                                .OfCategory(BuiltInCategory.OST_Walls)
                                                                .WhereElementIsNotElementType()
                                                                .ToElements().FirstOrDefault();

    using (var transaction = new Transaction(document, "Hide a wall"))
    {
        try
        {
            transaction.Start();
                    
            if(e!=null && !e.IsHidden(activeView) && e.CanBeHidden(activeView))
            {
                activeView.HideElements(new List<ElementId>() { e.Id });
                transaction.Commit();
            }

        }
        catch (Exception ex)
        {
            transaction.RollBack();
            throw new Exception("Hiding a wall.", ex);
        }
    }
Accepted solutions (1)
676 Views
3 Replies
Replies (3)
Message 2 of 4

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @alinadCX2LJ ,

 

Could you please tell me which Revit version you are using?

Recently, I handled a case similar to the case you raised here

https://forums.autodesk.com/t5/revit-api-forum/view-hideelements-does-not-work-in-revit-2023/m-p/111... 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 4

alinadCX2LJ
Participant
Participant

Thanks for the fast reply!

I experienced the issue in at least three Revit versions- 2021, 2022 & 2023. I haven't been able to test the other ones.

0 Likes
Message 4 of 4

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @alinadCX2LJ ,

 

I am able to reproduce the issue in Revit 2023 but not in Revit 2022 and Revit 2021.

For now, You can use the workaround shared by the Revit Engineering team.

Please see the link I shared in my previous post for the workaround.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes