最近遇到一種狀況需要經常切換剖面框的可見性,於是想將剖面框的顯示與隱藏做成一個按鈕,方便切換。
其他類似元素想做成快速切換可見性應該可以使用類似做法。
這次的隱藏對象是剖面框,所以我直接就隱藏元素了。
以下核心代碼:
View activeView = uidoc.ActiveView;
//過濾剖面框
FilteredElementCollector elemCollector = new FilteredElementCollector(doc);
elemCollector.OfCategory(BuiltInCategory.OST_SectionBox);
Element sectionBox = null;
//找到當前視圖中可以隱藏的剖面框
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, "快速隱藏工具"))
{
tran.Start();
//判斷當前視圖中剖面框是否被隱藏
if (sectionBox.IsHidden(activeView))
{
//取消隱藏
activeView.UnhideElements(sectionBoxIds);
}
else
{
//隱藏
activeView.HideElements(sectionBoxIds);
}
tran.Commit();
}
//過濾剖面框
FilteredElementCollector elemCollector = new FilteredElementCollector(doc);
elemCollector.OfCategory(BuiltInCategory.OST_SectionBox);
Element sectionBox = null;
//找到當前視圖中可以隱藏的剖面框
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, "快速隱藏工具"))
{
tran.Start();
//判斷當前視圖中剖面框是否被隱藏
if (sectionBox.IsHidden(activeView))
{
//取消隱藏
activeView.UnhideElements(sectionBoxIds);
}
else
{
//隱藏
activeView.HideElements(sectionBoxIds);
}
tran.Commit();
}
轉自:https://blog.csdn.net/imfour/article/details/79007851