Revit通過過濾器來區分不同的元素,這些過濾器為應用程序獲取不同的元素提供了方便靈活的接口。在使用過濾器的過程中,會應用到一個常用元素FilteredElementCollector類,我們稱之為“收集器”,指定對應的收集器以后,再傳入對用的過濾器,對需要的元素進行過濾。
1、收集器
常用的收集器,有以下幾個:
1.1 元素收集器,FilteredElementCollector
通過傳入文檔、視圖和元素集合,實現對元素進行收集。
FilteredElementCollector collector = new FilteredElementCollector(doc); collector.WherePasses(new ElementClassFilter(typeof(FamilyInstance))); var sphereElements = from element in collector where element.Name == "sphere" select element; if (sphereElements.Count() == 0) { TaskDialog.Show("Error", "Sphere family must be loaded"); return; }
1.2 工作集元素收集器,FilteredWorksetCollector
用於在工作集中,文檔中過濾出指定工作集的元素,其代碼如下:
FilteredWorksetCollector col = new FilteredWorksetCollector(doc); _AllWorksetList = col.Where(p => p.Kind == WorksetKind.UserWorkset).OrderBy(p => p.Name).ToList(); foreach (Workset ws in _AllWorksetList) { checkedListBox1.Items.Add(ws.Name); _SumCount++; }
收集器通過方法WherePasses實現對元素的過濾。
2、過濾器
revit的過濾器無非三種類別:
Autodesk.Revit.DB..::..ElementFilter
Autodesk.Revit.DB..::..ElementLogicalFilter
Autodesk.Revit.DB..::..ElementQuickFilter
Autodesk.Revit.DB..::..ElementSlowFilter
名稱 |
類型 |
作用 |
備注 |
BoundingBoxContainsPointFilter |
快速過濾器 |
其邊界盒包含了給定點的元素 |
無 |
BoundingBoxIntersectsFilter |
快速過濾器 |
與指定邊界盒相交的元素 |
無 |
BoundingBoxIsInsideFilter |
快速過濾器 |
在指定邊界盒之內的元素 |
無 |
ElementCategoryFilter |
快速過濾器 |
通過指定的類別過濾 |
無 |
ElementClassFilter |
快速過濾器 |
指定元素類進行過濾 |
無 |
ElementDesignOptionFilter |
快速過濾器 |
元素的詳細程度過濾 |
無 |
ElementIsCurveDrivenFilter |
快速過濾器 |
元素是線驅動過濾器 |
無 |
ElementIsElementTypeFilter |
快速過濾器 |
指定元素類型進行驅動 |
無 |
ElementMulticategoryFilter |
快速過濾器 |
元素的多類別過濾 |
無 |
ElementMulticlassFilter |
快速過濾器 |
元素的多類過濾 |
無 |
ElementOwnerViewFilter |
快速過濾器 |
視圖檢索 |
無 |
ElementStructuralTypeFilter |
快速過濾器 |
元素的結構類型 |
無 |
ElementWorksetFilter |
快速過濾器 |
指定工作集的元素過濾 |
無 |
ExclusionFilter |
快速過濾器 |
排除過濾器 |
無 |
ExtensibleStorageFilter |
快速過濾器 |
Schema類型的排除計算器 |
無 |
FamilySymbolFilter |
快速過濾器 |
族過濾器 |
無 |
案例:
案例1:查找包含指定點的牆體元素BoundingBoxContainsPointFilter
BoundingBoxContainsPointFilter notContainFilter = new BoundingBoxContainsPointFilter(basePnt, true); collector = new FilteredElementCollector(document); IList<Element> notContainFounds = collector.OfClass(typeof(Wall)).WherePasses(notContainFilter).ToElements();
案例2:查找指定區域相交的元素對象BoundingBoxIntersectsFilter
Outline myOutLn = new Outline(new XYZ(0, 0, 0), new XYZ(100, 100, 100)); //創建輪廓 BoundingBoxIntersectsFilter filter = new BoundingBoxIntersectsFilter(myOutLn); derived from ElementType FilteredElementCollector collector = new FilteredElementCollector(document); IList<Element> elements = collector.WherePasses(filter).ToElements();
案例3:過濾查找指定區域內的元素
Outline myOutLn = new Outline(new XYZ(0, 0, 0), new XYZ(100, 100, 100)); //創建邊界 BoundingBoxIsInsideFilter filter = new BoundingBoxIsInsideFilter(myOutLn); derived from ElementType FilteredElementCollector collector = new FilteredElementCollector(document); IList<Element> elements = collector.WherePasses(filter).ToElements();
案例4:通過元素的類別進行過濾指定元素
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Walls); FilteredElementCollector collector = new FilteredElementCollector(document); IList<Element> walls = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements(); String prompt = "The walls in the current document are:\n"; foreach (Element e in walls) { prompt += e.Name + "\n"; } TaskDialog.Show("Revit", prompt);
案例5:通過元素的類型進行過濾ElementClassFilter
ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance)); FilteredElementCollector collector = new FilteredElementCollector(document); collector.WherePasses(filter); var query = from element in collector where element.Name == "60\" x 30\" Student" select element; List<FamilyInstance> familyInstances = query.Cast<FamilyInstance>().ToList<FamilyInstance>();
案例6:
2.2 ElementSlowFilter 慢過濾器常用在一個圖形比較方面
慢過濾器要求首先獲得元素並在內存中展開。因此,最好將慢過濾器與至少一個ElementQuickFilter耦合,這應使擴展的元素數量最小化,以便根據該濾波器設置的標准進行評估。
名稱 |
類型 |
作用 |
備注 |
RoomFilter |
慢過濾器 |
查找房間 |
無 |
RoomTagFilter |
慢過濾器 |
查找房間標記 |
無 |
AreaFilter |
慢過濾器 |
查找區域 |
無 |
AreaTagFilter |
慢過濾器 |
查找區域標記 |
無 |
CurveElementFilter |
慢過濾器 |
指定元素類進行過濾 |
無 |
ElementIntersectsFilter |
慢過濾器 |
查找相關信息 |
無 |
ElementLevelFilter |
慢過濾器 |
查找標高信息 |
無 |
ElementParameterFilter |
慢過濾器 |
通過元素的參數 |
無 |
ElementPhaseStatusFilter |
慢過濾器 |
和指定階段狀態聯系的元素 |
無 |
FamilyInstanceFilter |
慢過濾器 |
族實例查找 |
無 |
SpaceFilter |
慢過濾器 |
空間查找 |
無 |
SpaceTagFilter |
慢過濾器 |
空間標記查找 |
無 |
PrimaryDesignOptionMemberFilter |
慢過濾器 |
主選項所擁有的元素 |
無 |
FamilyStructuralMaterialTypeFilter |
慢過濾器 |
組的結構性材質過濾器 |
無 |
StructuralInstanceUsageFilter |
慢過濾器 |
結構實例 |
無 |
StructuralMaterialTypeFilter |
慢過濾器 |
結構性材質 |
無 |
2.3 ElementLogicalFilter 邏輯過濾器,是實現過濾器的組合和復雜應用
邏輯過濾器是實現組合和復雜過濾的過濾器。
名稱 |
類型 |
作用 |
備注 |
LogicalAndFilter |
邏輯過濾器 |
邏輯與 |
|
LogicalOrFilter |
邏輯過濾器 |
邏輯或 |
主要解決過濾中的“或”與“與”的問題,and就是所有滿足其中一個條件即可,OR就是要滿足所有的條件。