1 [TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] 2 public class FindFurnitureInARoom : IExternalCommand 3 { 4 public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements) 5 { 6 try 7 { 8 UIApplication app = commandData.Application; 9 Document document = app.ActiveUIDocument.Document; 10 11 Selection sel = app.ActiveUIDocument.Selection; 12 Reference ref1 = sel.PickObject(ObjectType.Element, "Please pick a room"); 13 Room room = document.GetElement(ref1) as Room; 14 15 if (room != null) 16 { 17 ParameterValueProvider provider = new ParameterValueProvider(new ElementId(BuiltInParameter.ELEM_ROOM_ID)); 18 FilterNumericRuleEvaluator evaluator = new FilterNumericEquals(); 19 20 FilterElementIdRule rule = new FilterElementIdRule(provider, evaluator, room.Id); 21 ElementParameterFilter filter = new ElementParameterFilter(rule); 22 23 FilteredElementCollector collector = new FilteredElementCollector(document); 24 collector.WherePasses(filter); 25 26 sel = app.ActiveUIDocument.Selection; 27 sel.Elements.Clear(); 28 foreach (Element elem in collector) 29 sel.Elements.Add(elem); 30 31 TaskDialog.Show("The number", "The number of components = " + collector.Count().ToString()); 32 return Result.Succeeded; 33 } 34 else 35 { 36 messages = "请选择一个房间!"; 37 return Result.Failed; 38 } 39 } 40 catch (Exception e) 41 { 42 messages = e.Message; 43 return Result.Failed; 44 } 45 } 46 }
ElementParameterFilter():过滤出符合一个或多个参数值的元素。
1.首先要确定依据什么参数来过滤,即用ParameterValueProvider()来确定provider。
2.其次确定评估方式,相等还是大于小于。即用FilterNumericRuleEvaluator()来确定numeric的评估方式,是FilterNumericEqual还是FilterNumericLess之类的。
3.最后确定被用来作比较的数值。此处是room的ID。
综上三点便形成了过滤的规则FilterElementIdRule(用什么,通过什么方式,和什么(作比较))。
接着利用这个rule来过滤。
另外,此处过滤原理:每一个房间对应一个ID,而处于这个房间内部的对象。
PS:此例中门内开与外开不同,向内开启则会算中room中一个图元,外开不然。
代码主体摘自叶雄进老师。