// 显示或隐藏视图中选中的元素 [Transaction(TransactionMode.Manual)][Regeneration(RegenerationOption.Manual)] // [Journaling(JournalingMode.NoCommandData)] public class HideUnHideSelectInView : IExternalCommand{ public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Selection selection = uiDoc.Selection; if (selection.Elements.Size == 0 ) { MessageBox.Show( " 请选择 " ); return Result.Cancelled; } Transaction ts = new Transaction(uiDoc.Document, " HideUnHide " ); ts.Start(); Autodesk.Revit.DB.View view = uiDoc.Document.ActiveView; // 判断在此视图下是否可以隐藏 foreach (Element el in selection.Elements) { if (el.CanBeHidden(view) == true ) { ElementSet elementSet = new ElementSet(); elementSet.Clear(); elementSet.Insert(el); if (el.IsHidden(view) == true ) { view.Unhide(elementSet); } else { view.Hide(elementSet); } } } ts.Commit(); return Result.Succeeded; }}