今天在研究在管道上添加閥門的代碼實作,我的思路是:
1)在拾取的管道的點的位置,打斷管道
2)在拾取的點的位置建立閥門執行個體;
3)管道和閥門實作連接配接件連接配接
下面是第一個問題的解決:
private void BreakPipe(Document doc, Selection selection)
{
Reference reference = selection.PickObject(ObjectType.Element);
Pipe pipe = doc.GetElement(reference) as Pipe;
LocationCurve pipeLocationCurve = pipe.Location as LocationCurve;
XYZ pickPoint = reference.GlobalPoint;
XYZ projectPickPoint = pipeLocationCurve.Curve.Project(pickPoint).XYZPoint;
using (Transaction trans = new Transaction(doc))
{
trans.Start("Break Pipe");
//取得管道新的定位基線
Curve newCurve1 = Line.CreateBound(pipeLocationCurve.Curve.GetEndPoint(0), projectPickPoint);
Curve newCurve2 = Line.CreateBound(projectPickPoint, pipeLocationCurve.Curve.GetEndPoint(1));
//複制管道
Pipe copyPipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, pipe.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Pipe;
LocationCurve copyPipeLocationCurve = copyPipe.Location as LocationCurve;
//設定管道的新基線
pipeLocationCurve.Curve = newCurve1;
copyPipeLocationCurve.Curve = newCurve2;
trans.Commit();
}
}