Revit并没有直接开放基于族获取实例元素的过滤器。不过有两个相关的过滤器,一个是FamilySymbolFilter(ElementId familyId),可以基于族ID过滤出所有这个族的族类型,另一个是FamilyInstanceFilter(Document document,ElementId familySymbolId),可以基于族类型ID过滤出所有这个族类型的实例元素。结合使用两者就能基于族获取实例元素,以下为代码:
1 using System.Text; 2 using System.Threading.Tasks; 3 using Autodesk.Revit.DB; 4 using Autodesk.Revit.UI; 5 using Autodesk.Revit.ApplicationServices; 6 using Autodesk.Revit.UI.Selection; 7 using Autodesk.Revit.Attributes; 8 using System.Windows.Forms; 9 using System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using System.Diagnostics; 13 using System.IO; 14 15 16 17 namespace Xincubus 18 { 19 [Transaction(TransactionMode.Manual)] 20 public class TestParameterPass : IExternalCommand 21 { 22 public Result Execute(ExternalCommandData document, ref string message, ElementSet elements) 23 { 24 UIApplication uiapp = document.Application; 25 Document doc = uiapp.ActiveUIDocument.Document; 26 string test = ""; 27 string familyName = "检查井"; 28 //收集所有族 29 FilteredElementCollector familiyCollector = new FilteredElementCollector(doc); 30 familiyCollector.OfClass(typeof(Family)).ToElements(); 31 ElementId familyID = new ElementId(-1); 32 //寻找特定名称的族 33 foreach (var family in familiyCollector) 34 { 35 if (family.Name == familyName) 36 { 37 familyID = family.Id; 38 } 39 } 40 if (familyID != new ElementId(-1)) 41 { 42 //收集族类型 43 familiyCollector = new FilteredElementCollector(doc); 44 familiyCollector.WherePasses(new FamilySymbolFilter(familyID)); 45 } 46 //收集以上族类型的族 47 List<Element> familyInstances = new List<Element>(); 48 foreach (var familySymbol in familiyCollector) 49 { 50 FilteredElementCollector collector = new FilteredElementCollector(doc); 51 collector.OfCategory(BuiltInCategory.OST_MechanicalEquipment).WhereElementIsNotElementType(); 52 collector.WherePasses(new FamilyInstanceFilter(doc, familySymbol.Id)); 53 familyInstances.AddRange(collector); 54 } 55 56 57 58 59 60 61 62 63 test += familyInstances.Count.ToString(); 64 MessageBox.Show("项目中共有" + test + "个" + familyName + "的族实例"); 65 return Result.Succeeded; 66 } 67 } 68 }
当然,也可遍历所有收集器中的实例元素,依靠 FamilyInstance.Symbol.Family来筛选出需要的元素。
鉴于FamilyInstanceFilter(Document document,ElementId familySymbolId)是一个慢速过滤器,遍历法应该也满不了多少。
时间: 2024-10-01 02:20:40