-
获取翻弯后产生的所有管道节点坐标
-
创建所有节点构成的管道定位线
-
创建管道
-
获取所有管道上的连接器
-
遍历所有连接器,得到相同位置处,不同宿主的连接器
-
创建连接器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.DB.Plumbing; namespace test { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class TestCommand: IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Document doc = uiDoc.Document; //交互选择第一个点 Reference reference = uiDoc.Selection.PickObject(ObjectType.PointOnElement,new Pipefilter()); XYZ point1 = reference.GlobalPoint; //交互选择第二个点 Reference reference2 = uiDoc.Selection.PickObject(ObjectType.PointOnElement,new Pipefilter()); XYZ point2 = reference2.GlobalPoint; //获取管道 Pipe pipe = doc.GetElement(reference) as Pipe; //获取管道定位线 LocationCurve locationcurve = pipe.Location as LocationCurve; //获取管道定位起点终点 XYZ start = locationcurve.Curve.GetEndPoint(0); XYZ end = locationcurve.Curve.GetEndPoint(1); //上面的点是管道表面的点,需要投影到管道中心定位线上 XYZ propoint1 = locationcurve.Curve.Project(point1).XYZPoint; XYZ propoint2 = locationcurve.Curve.Project(point2).XYZPoint; //创建一个列表来存储所有的线 List<Line> lines = creatlines(start, end, propoint1, propoint2, 40); //创建一个管道列表来存储生成的所有管道 List<Pipe> pipes = new List<Pipe>(); using (Transaction transaction = new Transaction(doc)) { transaction.Start("创建管道"); //通过遍历生成的所有定位线来创建管道 foreach (Line line in lines) { Pipe newpipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, pipe.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Pipe; LocationCurve newpipelocationcurve = newpipe.Location as LocationCurve; newpipelocationcurve.Curve = line; pipes.Add(newpipe); } doc.Delete(pipe.Id); transaction.Commit(); } //获取所有管道上所有的连接器 List<Connector> connectors = new List<Connector>(); foreach (Pipe pipe1 in pipes) { ConnectorSet connects = pipe1.ConnectorManager.Connectors; foreach (Connector connector in connects) { connectors.Add(connector); } } //遍历连接器列表,找到同一位置处不同管道上的连接器,进而创建弯头 for (int i = connectors.Count - 1; i > 0; i--) { Connector con = connectors[i]; Connector co = nearconnector(con, connectors); if (co != null) { using (Transaction transaction2 = new Transaction(doc)) { transaction2.Start("创建弯头"); doc.Create.NewElbowFitting(con, co); transaction2.Commit(); } } } return Result.Succeeded; } /// <summary> /// 该方法传入翻弯时产生的所有管道节点,返回翻弯生成的所有管道定位线 /// </summary> /// <param name="管道起点start"></param> /// <param name="管道终点end"></param> /// <param name="交互选择的第一点propoint1"></param> /// <param name="交互选择的第二点propoint2"></param> /// <param name="偏移值(翻弯高度)offsetvalue"></param> /// <returns></returns> private List<Line> creatlines(XYZ start, XYZ end, XYZ propoint1, XYZ propoint2, double offsetvalue) { List<Line> lines = new List<Line>(); Line thispipecurve = Line.CreateBound(propoint1, near(start, end, propoint1)); lines.Add(thispipecurve); Line newpipecurve = Line.CreateBound(propoint2, near(start, end, propoint2)); lines.Add(newpipecurve); XYZ propoint1offset = propoint1 + new XYZ(0, 0, offsetvalue); XYZ propoint2offset = propoint2 + new XYZ(0, 0, offsetvalue); Line offsetline = Line.CreateBound(propoint1offset,propoint2offset); lines.Add(offsetline); Line line1 = Line.CreateBound(propoint1, propoint1offset); lines.Add(line1); Line line2 = Line.CreateBound(propoint2, propoint2offset); lines.Add(line2); return lines; } //判断两个交互选择得到的点离管道的两端分别那个最近 public XYZ near(XYZ startpoint,XYZ endpoint,XYZ pickedpoint) { if (startpoint.DistanceTo(pickedpoint) <endpoint.DistanceTo(pickedpoint)) { return startpoint; } return endpoint; } //创建一个过滤器,使选点的时候只在管道上选择 public class Pipefilter : ISelectionFilter { public bool AllowElement(Element elem) { if (elem is Pipe) { return true; } return false; } public bool AllowReference(Reference reference, XYZ position) { return true; } } //遍历弯头,得到相同位置处另一个管道的连接器 public Connector nearconnector(Connector con,List<Connector> connectors) { foreach (Connector connector in connectors) { if (con.Origin.IsAlmostEqualTo(connector.Origin)&&con.Owner.Id!=connector.Owner.Id) { return connector; } } return null; } } }
Revit二次开发 ----> 管道翻弯
于 2021-07-16 23:47:17 首次发布