Unable to hide element category in a view in Revit file

1.2k Views Asked by At

I want to hide certain elements in the view. I managed to hid (with view..HideCategoryTemporary) all the elements I wanted except the marked one in the picture attached. 3D_House_before_hide

Element snoop This element is a building section of category OST_Viewers. Manually hiding the element category via the view works, but fetching all OST_Viewers in the code and hiding them does not work.

The following code contain the building section elements in addition to the grids,

FilteredElementCollector viewers_sections = new FilteredElementCollector(doc, v_id).OfCategory(BuiltInCategory.OST_Viewers);
FilteredElementCollector grids = new FilteredElementCollector(doc, v_id).OfCategory(BuiltInCategory.OST_Grids);

FilteredElementCollector elements_to_be_hidden = new FilteredElementCollector(doc, v_id);
elements_to_be_hidden.UnionWith(viewers_sections).UnionWith(grids)

foreach (Element e in elements_to_be_hidden)
{
     cur_view.HideCategoryTemporary(e.Category.Id);
}

I've checked that viewers_sections contains the mentioned building sections however it is not hidden from the view. After hide

How do I hide these building sections?

2

There are 2 best solutions below

2
Eason Kang On BEST ANSWER

Please use View#SetCategoryHidden instead to turn off the visibility of the category, the result of the View#HideCategoryTemporary will be reset after closing the file. Here is the working example:

var gridCate = this.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Grids);
var sectionsCate = this.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Sections);

using(var trans = new Transaction(this.Document))
{
    trans.Start("Hide Grids & Secions");
    this.ActiveView.SetCategoryHidden(gridCate.Id, true);
    this.ActiveView.SetCategoryHidden(sectionsCate.Id, true);
    trans.Commit();
}
0
Rocker On
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

import System
from System.Collections.Generic import *

document = DocumentManager.Instance.CurrentDBDocument
uiDocument = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

view = uiDocument.ActiveGraphicalView

instance = UnwrapElement(IN[0])

def HideElements(view, instance) :

    ids = List[ElementId]()
    
    if not instance.IsHidden(view) and instance.CanBeHidden(view) :
        ids.Add(instance.Id)
        
    TransactionManager.Instance.EnsureInTransaction(document)
    
    view.HideElements(ids)
    
    TransactionManager.Instance.TransactionTaskDone()
    
    return None

HideElements(view, instance)

The above one is the python code for revit dynamo.. Similarly for C# you can use View view = uidoc.ActiveGraphicalView; to get view and inside the transaction use view.HideElements("Your Element ID List"); to hide the element in the Active View