revit二次开发获取元素所有的曲线

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Structure;

namespace Revit开发模板29
{
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;//获取活动文档

            //选择元素
            Reference refer = uidoc.Selection.PickObject(ObjectType.Element);
            Element ele = doc.GetElement(refer);

            //元素几何选项
            Options opt = new Options();
            opt.ComputeReferences = true;
            opt.DetailLevel = ViewDetailLevel.Fine;
            GeometryElement e = ele.get_Geometry(opt);

            //获取面
            foreach (GeometryObject obj in e)
            {
                if (obj is GeometryInstance)//对象是geometryinstance
                {
                    GeometryInstance geoInstance = obj as GeometryInstance;
                    GeometryElement geoElement = geoInstance.GetSymbolGeometry();//为了获取面上的id,但是面上的id只有GetSymbolGeometry才是实例
                    foreach (GeometryObject obj2 in geoElement)//再把元素转换为实体
                    {
                        if (obj2 is Solid)
                        {
                            Solid solid2 = obj2 as Solid;
                            foreach (var face in solid2.Faces)
                            {
                                if (face is PlanarFace)
                                {
                                    PlanarFace face2 = face as PlanarFace;
                                    FaceNewModelLine(doc, face2.Reference);
                                }
                            }
                        }
                    }
                }
                if (obj is Solid)//对象是实体
                {
                    Solid solid2 = obj as Solid;
                    foreach (var face in solid2.Faces)
                    {
                        if (face is PlanarFace)
                        {
                            PlanarFace face2 = face as PlanarFace;
                            FaceNewModelLine(doc, face2.Reference);
                        }
                    }
                }
            }


            return Result.Succeeded;
        }

        /// 
        /// 
        /// 
        /// 传入Document
        /// 传入面引用
        private void FaceNewModelLine(Document doc, Reference refer)
        //根据PlanarFace创建平面模型线
        {
            Element element = doc.GetElement(refer.ElementId);//第一步获取参照元素,通过elementid转换为
            Transform transform = null;
            PlanarFace face = element.GetGeometryObjectFromReference(refer) as PlanarFace;

            if (element != null)//获取transform
            {
                GeometryElement geoEle = element.get_Geometry(new Options());

                foreach (GeometryObject gObj in geoEle)

                {
                    GeometryInstance gInst = gObj as GeometryInstance;
                    if (null != gInst)
                    {
                        transform = gInst.Transform;
                    }
                }
            }
            foreach (EdgeArray e in face.EdgeLoops)
            {
                foreach (Edge e2 in e)
                {

                    if (e2.AsCurve() is Line)
                    {

                        Line line = (e2.AsCurve()) as Line;//因为怕存在Arc曲线无法转换


                        Plane plane = null;
                        if (transform != null)//转换坐标
                        {
                            plane = Plane.CreateByNormalAndOrigin(transform.OfVector(face.FaceNormal), transform.OfPoint(face.Origin));
                            XYZ xyz1 = line.GetEndPoint(0);
                            XYZ xyz2 = line.GetEndPoint(1);
                            line = Line.CreateBound(transform.OfPoint(xyz1), transform.OfPoint(xyz2));
                        }
                        else
                        {
                            plane = Plane.CreateByNormalAndOrigin(face.FaceNormal, face.Origin);
                        }
                        using (Transaction transaction = new Transaction(doc))
                        {
                            transaction.Start("Create Model Line");
                            //创建sketchPlane
                            SketchPlane modelSketch = SketchPlane.Create(doc, plane);
                            doc.Create.NewModelCurve(line, modelSketch); //创建模型线
                            transaction.Commit();
                        }
                    }
                }
            }
        }




    }
}


 

你可能感兴趣的:(revit二次开发获取元素所有的曲线)