Revit二次开发之“显示或隐藏视图中未选中的元素”

方法是先隐藏全部元素,再显示选中的。 
转自: http://revit.5d6d.com/thread-1094-1-1.html
复制代码
// 显示或隐藏视图中未选中的元素
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
// [Journaling(JournalingMode.NoCommandData)]
public   class  HideUnHideUnSelectInView : IExternalCommand
{
    
public  Result Execute(ExternalCommandData commandData,  ref   string  message, ElementSet elements)
    {
        UIDocument uiDoc 
=  commandData.Application.ActiveUIDocument;
        Selection selection 
=  uiDoc.Selection;
        Autodesk.Revit.DB.View view 
=  uiDoc.Document.ActiveView;

        Transaction ts 
=   new  Transaction(uiDoc.Document,  " HideUnHide " );
        ts.Start();

        
// 先把所有可隐藏的元素隐藏,再把选中的元素显示
        ElementSet elementSet  =   new  ElementSet();
        
// 遍历风管
        FilteredElementCollector collectorDuct  =   new  FilteredElementCollector(uiDoc.Document);
        collectorDuct.OfClass(
typeof (Duct)).OfCategory(BuiltInCategory.OST_DuctCurves); // 风管,直接用类名
         foreach  (Element el  in  collectorDuct)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历墙
        FilteredElementCollector collectorWall  =   new  FilteredElementCollector(uiDoc.Document);
        collectorWall.OfClass(
typeof (Wall)).OfCategory(BuiltInCategory.OST_Walls); // 墙,直接用类名
         foreach  (Element el  in  collectorWall)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历窗
        FilteredElementCollector collectorWindow  =   new  FilteredElementCollector(uiDoc.Document);
        collectorWindow.OfClass(
typeof (FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows); // 窗,类型是FamilyInstance
         foreach  (Element el  in  collectorWindow)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历门
        FilteredElementCollector collectorDoor  =   new  FilteredElementCollector(uiDoc.Document);
        collectorDoor.OfClass(
typeof (FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors); // 门,类型是FamilyInstance
         foreach  (Element el  in  collectorDoor)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        view.Hide(elementSet);

        
// 判断在此视图下是否可以隐藏
         foreach  (Element el  in  selection.Elements)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Clear();
                elementSet.Insert(el);
                view.Unhide(elementSet);
            }
        }

        ts.Commit();
        
return  Result.Succeeded;
    }
}

// 显示全部元素
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
// [Journaling(JournalingMode.NoCommandData)]
public   class  ShowAllInView : IExternalCommand
{
    
public  Result Execute(ExternalCommandData commandData,  ref   string  message, ElementSet elements)
    {
        UIDocument uiDoc 
=  commandData.Application.ActiveUIDocument;
        Selection selection 
=  uiDoc.Selection;
        Autodesk.Revit.DB.View view 
=  uiDoc.Document.ActiveView;

        Transaction ts 
=   new  Transaction(uiDoc.Document,  " HideUnHide " );
        ts.Start();

        
// 先把所有可隐藏的元素隐藏,再把选中的元素显示
        ElementSet elementSet  =   new  ElementSet();
        
// 遍历风管
        FilteredElementCollector collectorDuct  =   new  FilteredElementCollector(uiDoc.Document);
        collectorDuct.OfClass(
typeof (Duct)).OfCategory(BuiltInCategory.OST_DuctCurves); // 风管,直接用类名
         foreach  (Element el  in  collectorDuct)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历墙
        FilteredElementCollector collectorWall  =   new  FilteredElementCollector(uiDoc.Document);
        collectorWall.OfClass(
typeof (Wall)).OfCategory(BuiltInCategory.OST_Walls); // 墙,直接用类名
         foreach  (Element el  in  collectorWall)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历窗
        FilteredElementCollector collectorWindow  =   new  FilteredElementCollector(uiDoc.Document);
        collectorWindow.OfClass(
typeof (FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows); // 窗,类型是FamilyInstance
         foreach  (Element el  in  collectorWindow)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        
// 遍历门
        FilteredElementCollector collectorDoor  =   new  FilteredElementCollector(uiDoc.Document);
        collectorDoor.OfClass(
typeof (FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors); // 门,类型是FamilyInstance
         foreach  (Element el  in  collectorDoor)
        {
            
if  (el.CanBeHidden(view)  ==   true )
            {
                elementSet.Insert(el);
            }
        }
        view.Unhide(elementSet);

        ts.Commit();
        
return  Result.Succeeded;
    }
}
复制代码

你可能感兴趣的:(Revit,隐藏或显示视图中的元素)