Revit 二次开发---空间过滤(空间查询)

来源:互联网 发布:谁用过淘宝上的处女血 编辑:程序博客网 时间:2024/06/06 02:39

第一种,通过选取一个要素,查询与之相交的要素,代码如下

public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)        {            m_document = revit.Application.ActiveUIDocument;            m_application = revit.Application;            Selection sel = m_document.Selection;            try            {                Reference eRe = m_document.Selection.PickObject(ObjectType.Element, "按ESC键取消该命令");                Element element= m_document.Document.GetElement(eRe);                if (element != null)                {                    //相交过滤                    FilteredElementCollector collector = new FilteredElementCollector(m_document.Document);                    ElementIntersectsElementFilter eleIntersectFilter = new ElementIntersectsElementFilter(element, false);                    collector.WherePasses(eleIntersectFilter);                    ICollection<ElementId> intersectElementid = new List<ElementId>();                    List<ElementId> excludes = new List<ElementId>();                    excludes.Add(element.Id);                    collector.Excluding(excludes);                    //添加要素至选择集                    string mes = "";                    foreach (Element item in collector)                    {                        intersectElementid.Add(item.Id);                        mes += "\n\t" + "类型:" + item.Name;                    }                    sel.SetElementIds(intersectElementid);                    TaskDialog.Show("相交信息", mes);                }                return Result.Succeeded;            }            catch (Exception ex)            {                message = ex.Message;                return Autodesk.Revit.UI.Result.Failed;            }        }

第二种,通过拾取建筑上的一个点,以改点为中心,创建一个指定边长的Solid,并查询与该Solid相交的要素,代码如下:

 public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)        {            UIApplication app = commandData.Application;            Document doc = app.ActiveUIDocument.Document;            //pick a point to draw solid            Selection sel = app.ActiveUIDocument.Selection;            XYZ pt = sel.PickPoint("Please pick a point to get the close walls");            // 以拾取的pt为中心点创建 solids            GeoCreateClass geometryCreation = GeoCreateClass.getInstance(commandData.Application.Application);            Solid solid = geometryCreation.CreateCenterbasedBox(pt,3);            FilteredElementCollector collector = new FilteredElementCollector(doc);            ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solid);            collector.WherePasses(solidFilter);            //添加至选择集            ICollection<ElementId> intersectElementid = new List<ElementId>();            string mes = "";            foreach (Element item in collector)            {                intersectElementid.Add(item.Id);                mes += "\n\t" + "类型:" + item.Name;            }            sel.SetElementIds(intersectElementid);            TaskDialog.Show("相交信息", mes);            return Result.Succeeded;        }
public Solid CreateCenterbasedBox(XYZ center, double edgelength)        {            double halfedgelength = edgelength / 2.0;            List<CurveLoop> profileloops = new List<CurveLoop>();            CurveLoop profileloop = new CurveLoop();            profileloop.Append(Line.CreateBound(               new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),               new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));            profileloop.Append(Line.CreateBound(               new XYZ(center.X - halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),               new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength)));            profileloop.Append(Line.CreateBound(               new XYZ(center.X + halfedgelength, center.Y + halfedgelength, center.Z - halfedgelength),               new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));            profileloop.Append(Line.CreateBound(               new XYZ(center.X + halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength),               new XYZ(center.X - halfedgelength, center.Y - halfedgelength, center.Z - halfedgelength)));            profileloops.Add(profileloop);            XYZ extrusiondir = new XYZ(0, 0, 1); // orthogonal            double extrusiondist = edgelength;            return GeometryCreationUtilities.CreateExtrusionGeometry(profileloops, extrusiondir, extrusiondist);        }


0 0