1.過濾器
1.1ElementClassFilter?
ElementClassFilter的作用是根據(jù)類,來過濾Revit文檔中的元素,即獲取到符合傳入類的元素。ElementClassFilter的構造函數(shù)定義是:ElementClassFilter(Type type)??。
使用實例:
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc);//建立收集器
ElementClassFilter classFilter = new ElementClassFilter(typeof(Wall));//利用ElementClassFilter對收集器進行過濾
collector = collector.WherePasses(classFilter);
//將過濾得到的圖元轉換為id
IList<ElementId> elid = new List<ElementId>();
foreach (Element el in collector)
{
elid.Add(el.Id);
}
//將圖元設置為選中狀態(tài)
uidoc.Selection.SetElementIds(elid);
return Result.Succeeded;
}

測試結果
1.2ElementCategoryFilter??
ElementCategoryFilter即元素類別過濾器。Category(類別)!所有基類為Element類的元素都擁有一個Category屬性用于表示這個元素屬于的類別或者子類別,詳見上一篇數(shù)據(jù)結構。
ElementCategoryFilter?? categoryFilter = new ElementCategoryFilter??(BuiltInCategory.OST_Walls);//將元素的類別作為傳入?yún)?shù)來過濾元素
collector = collector.WherePasses(categoryFilter);
元素的類別可以采用如圖方法查看:

查看元素類別

測試結果
2.選擇過濾器
在執(zhí)行命令的過程中,讓用戶自行選擇構件或構建集。
主函數(shù)中的實現(xiàn):
ISelectionFilter WallFilter = new WallSelectionFilter();
IList<Reference> elementList = uidoc.Selection.PickObjects(ObjectType.Element, WallFilter, "選擇墻");
接口的實現(xiàn):
public class WallSelectionFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if(elem is Wall && elem.Name == "常規(guī) - 200mm")
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}

選擇過濾器