最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

Revit二次开发实战04(元素搜集过滤器FilteredElementCollector)

来源:博客园

Revit二次开发实战


【资料图】

FilteredElementCollector元素搜集过滤器

1、创建搜集器

FilteredElementCollector(Document);搜集文档中所有元素;

FilteredElementCollector(Document,List);在指定id集合中搜集;

FilteredElementCollector(Document,ElementId viewId);在指定视图中搜集;

2、调用过滤函数

OfCategoryId(newElementId(BuiltInCategory.OST_Doors));

OfCategory(BuiltInCategory.OST_Doors);

OfClass(typeof(Wall));

WherePasses(newRoomFilter());

WherePasses(intersectsFilter);

源码

using Autodesk.Revit.Attributes;using Autodesk.Revit.DB;using Autodesk.Revit.DB.Architecture;using Autodesk.Revit.UI;using Autodesk.Revit.UI.Selection;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace RevitHello{    [Transaction(TransactionMode.Manual)]    class CElement : IExternalCommand    {        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)        {            //获取当前UI文档            UIDocument uidoc = commandData.Application.ActiveUIDocument;            //获取当前文档            Document doc = uidoc.Document;            //创建搜集过滤器            //■构造函数1,搜集文档中所有元素            FilteredElementCollector collector1 = new FilteredElementCollector(doc);            //按照类别进行过滤            FilteredElementCollector filtered1 = collector1.OfCategory(BuiltInCategory.OST_Doors);            ShowElements(filtered1, uidoc, "filtered1");            //选择多个对象,并获取其id集合            var refs = uidoc.Selection.PickObjects(ObjectType.Element);            List ids = new List();            refs.ToList().ForEach(r => ids.Add(r.ElementId));            //■构造函数2,搜集集合中的元素            FilteredElementCollector collector2 = new FilteredElementCollector(doc, ids);            //按照类别Id进行过滤 所有的门            FilteredElementCollector filtered2 = collector2.OfCategoryId(new ElementId(BuiltInCategory.OST_Doors));            ShowElements(filtered2, uidoc, "filtered2");            //获取当前文档的当前视图            View view = uidoc.ActiveView;            //■构造函数3,搜集视图中的元素            FilteredElementCollector collector3 = new FilteredElementCollector(doc, view.Id);            //按照class的Type类型进行过滤            FilteredElementCollector filtered3 = collector3.OfClass(typeof(Wall));            ShowElements(filtered3, uidoc, "filtered3");            FilteredElementCollector collector4 = new FilteredElementCollector(doc);            List filtered4 = collector4.WherePasses(new RoomFilter()).Cast().ToList();            ShowElements(filtered4, uidoc, "filtered4");            //获取一个包围盒,并过滤出与该包围盒相交的元素            var refe = uidoc.Selection.PickObject(ObjectType.Element, new DoorSelectionFilter());            var ele = doc.GetElement(refe);            var box = ele.get_BoundingBox(view);            BoundingBoxIntersectsFilter intersectsFilter = new BoundingBoxIntersectsFilter(new Outline(box.Min, box.Max));            FilteredElementCollector collector5 = new FilteredElementCollector(doc);            var filtered5 = collector5.WherePasses(intersectsFilter).ToElements();            ShowElements(filtered5, uidoc, "filtered5");            return Result.Succeeded;        }        //把输出元素集合信息封装成一个函数        void ShowElements(IEnumerable elements, UIDocument uidoc, string title)        {                      StringBuilder sb = new StringBuilder();            sb.AppendLine($"Count={elements.Count()}");            List ids = new List();            elements.ToList().ForEach((e) =>            {                sb.AppendLine($"Id={e.Id},Name={e.Name},Category={e.Category.Name}");                ids.Add(e.Id);            });            //高亮显示所有过滤的对象            uidoc.Selection.SetElementIds(ids);            //对话框显示所有过滤对象的信息            TaskDialog.Show(title, sb.ToString());        }    }    class DoorSelectionFilter : ISelectionFilter    {        public bool AllowElement(Element elem)        {            if (elem.Category.Id.IntegerValue==(int)BuiltInCategory.OST_Doors) return true;            return false;        }        public bool AllowReference(Reference reference, XYZ position)        {            return true;        }    }}

关键词: