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

几何实体的创建方法之一:
构成封闭底面,指定拉伸方向与拉伸高度。GeometryCreationUtilities
//自创几何实体相交法
[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(001);
        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;
    }
}
url:http://greatverve.cnblogs.com/p/GeometryCreationUtilities.html
posted @ 2013-11-10 17:18  大气象  阅读(5432)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com