Revit API创建几何实体Solid并找到与之相交的元素

发布时间 2023-09-06 15:13:13作者: 杉木2019
//自创几何实体相交法
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class FindIntersectWallsByGeometry : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {

        UIApplication app = commandData.Application;
        Document doc = app.ActiveUIDocument.Document;
        Transaction trans = new Transaction(doc, "ExComm");
        trans.Start();

        //pick a point to draw solid在屏幕上选择一点,找到附近的墙。
        Selection sel = app.ActiveUIDocument.Selection;
        XYZ pt = sel.PickPoint("Please pick a point to get the close walls");

        //XYZ pttemp1 = sel.PickPoint(Autodesk.Revit.UI.Selection.ObjectSnapTypes.None, "Pick leader end...");
        //XYZ pttemp2 = sel.PickPoint(Autodesk.Revit.UI.Selection.ObjectSnapTypes.None, "Pick leader elbow...");

        //
        double dBoxLength = 3;
        //Z值不变,以选择的点为中心,找到矩形四个点。
        XYZ pt1 = new XYZ(pt.X - dBoxLength / 2, pt.Y - dBoxLength / 2, pt.Z);
        XYZ pt2 = new XYZ(pt.X + dBoxLength / 2, pt.Y - dBoxLength / 2, pt.Z);
        XYZ pt3 = new XYZ(pt.X + dBoxLength / 2, pt.Y + dBoxLength / 2, pt.Z);
        XYZ pt4 = new XYZ(pt.X - dBoxLength / 2, pt.Y + dBoxLength / 2, pt.Z);
        //创建四条线。
        Line lineBottom = app.Application.Create.NewLineBound(pt1, pt2);
        Line lineRight = app.Application.Create.NewLineBound(pt2, pt3);
        Line lineTop = app.Application.Create.NewLineBound(pt3, pt4);
        Line lineLeft = app.Application.Create.NewLineBound(pt4, pt1);
        //封闭曲线
        CurveLoop profile = new CurveLoop();
        profile.Append(lineBottom);
        profile.Append(lineRight);
        profile.Append(lineTop);
        profile.Append(lineLeft);

        List<CurveLoop> loops = new List<CurveLoop>();
        loops.Add(profile);
        //创建实体的方法(底面,拉伸方向,拉伸高度)
        XYZ vector = new XYZ(0, 0, 1);
        Solid solid = GeometryCreationUtilities.CreateExtrusionGeometry(loops, vector, 10);

        //相交过滤器
        FilteredElementCollector collector = new FilteredElementCollector(doc);
        ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solid);

        collector.WherePasses(solidFilter);

        sel.Elements.Clear();
        //Add these interseting element to the selection
        foreach (Element elem in collector)
        {
            sel.Elements.Add(elem);
        }

        trans.Commit();
        return Result.Succeeded;
    }
}

 

  转自博客Revit API创建几何实体Solid并找到与之相交的元素 - 大气象 - 博客园 (cnblogs.com)