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;

 

namespace 拾取图元的面

{

[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;//获取活动文档

 

//选取面并画出面的四根线,重难点就在于transform

//1,利于reference获取面的主体元素

//2,并利用元素实例获取transform

//3,然后利用transform.ofpoint和transform.ofvector转换成项目内坐标

 

 

 

Reference refer = uidoc.Selection.PickObject(ObjectType.Face);

 

Element e = doc.GetElement(refer.ElementId);//第一步获取参照元素,通过elementid转换为

Transform transform = null;

PlanarFace face = e.GetGeometryObjectFromReference(refer) as PlanarFace;

 

if (e != null)//获取transform

{

 

GeometryElement geoEle = e.get_Geometry(new Options());

 

foreach (GeometryObject gObj in geoEle)

 

{

GeometryInstance gInst = gObj as GeometryInstance;

if (null != gInst)

{

transform = gInst.Transform;

}

}

}

 

Transaction trans = new Transaction(doc);

FaceNewModelLine(doc, face, transform);

 

return Result.Succeeded;

}

 

 

private void FaceNewModelLine(Document RevitDoc, PlanarFace face, Transform trans)

//根据PlanarFace创建平面模型线

{

 

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 (trans != null)//转换坐标

{

plane = Plane.CreateByNormalAndOrigin(trans.OfVector(face.FaceNormal), trans.OfPoint(face.Origin));

XYZ xyz1 = line.GetEndPoint(0);

XYZ xyz2 = line.GetEndPoint(1);

 

line = Line.CreateBound(trans.OfPoint(xyz1), trans.OfPoint(xyz2));

}

 

else

{

plane = Plane.CreateByNormalAndOrigin(face.FaceNormal, face.Origin);

 

 

}

 

 

using (Transaction transaction = new Transaction(RevitDoc))

{

transaction.Start("Create Model Line");

//创建sketchPlane

 

 

SketchPlane modelSketch = SketchPlane.Create(RevitDoc, plane); //创建模型线

RevitDoc.Create.NewModelCurve(line, modelSketch);

transaction.Commit();

 

}

}

}

}

}

 

}

}

 

你可能感兴趣的:(revit二次开发(拾取面,并创建面周围的模型线))