It’s uncommon to have free time without overtime work, even just to write a few lines of code.
Recently, I needed to frequently toggle the visibility of a section box. To simplify this process, I decided to create a button that can easily show or hide the section box.
This approach can also be applied to quickly switch visibility for other similar elements.
In this case, the target is the section box, so the solution involves directly hiding or showing the element.
Here is the core code:
View activeView = uidoc.ActiveView;
// Filter for section boxes
FilteredElementCollector elemCollector = new FilteredElementCollector(doc);
elemCollector.OfCategory(BuiltInCategory.OST_SectionBox);
Element sectionBox = null;
// Find section boxes that can be hidden in the current view
foreach (Element e in elemCollector)
{
if (e.CanBeHidden(activeView))
{
sectionBox = e;
continue;
}
}
List<ElementId> sectionBoxIds = new List<ElementId>();
sectionBoxIds.Add(sectionBox.Id);
using (Transaction tran = new Transaction(doc, “Quick Hide Tool”))
{
tran.Start();
// Check if the section box is hidden in the current view
if (sectionBox.IsHidden(activeView))
{
// Unhide section box
activeView.UnhideElements(sectionBoxIds);
}
else
{
// Hide section box
activeView.HideElements(sectionBoxIds);
}
tran.Commit();
}













Must log in before commenting!
Sign Up