一点打断
- 交互获取点
- 基于原来管道复制一个管道
- 两个管道的定位
实现代码如下
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 refer = uiDoc.Selection.PickObject(ObjectType.PointOnElement);
Pipe pipe = doc.GetElement(refer) as Pipe;
LocationCurve locationCurve = pipe.Location as LocationCurve;
XYZ pickpoint = refer.GlobalPoint;
//投影点
XYZ touyingpoint = locationCurve.Curve.Project(pickpoint).XYZPoint;
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("一点打断");
Curve curve1 = Line.CreateBound(locationCurve.Curve.GetEndPoint(0),touyingpoint);
Curve curve2 = Line.CreateBound(locationCurve.Curve.GetEndPoint(1),touyingpoint);
Pipe copypipe = doc.GetElement(ElementTransformUtils.CopyElement(doc,pipe.Id,new XYZ(0,0,0)).ElementAt(0)) as Pipe;
LocationCurve copypipelocationcurve = copypipe.Location as LocationCurve;
locationCurve.Curve = curve1;
copypipelocationcurve.Curve = curve2;
transaction.Commit();
}
return Result.Succeeded;
}
}
}
两点打断
思路同上
注意:两点打断需要判断选择的两点与管道端点的距离
实现代码如下
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 练习
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class b06管线两点打断 : 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);
Reference reference1 = uiDoc.Selection.PickObject(ObjectType.PointOnElement);
Pipe pipe = doc.GetElement(reference) as Pipe;
LocationCurve locationCurve = pipe.Location as LocationCurve;
XYZ Pickpoint = reference.GlobalPoint;
XYZ Pickpoint2 = reference1.GlobalPoint;
//管道上的投影点是
XYZ touyingPoint = locationCurve.Curve.Project(Pickpoint).XYZPoint;
XYZ touyingPoint2 = locationCurve.Curve.Project(Pickpoint2).XYZPoint;
using (Transaction transaction = new Transaction(doc))
{
transaction.Start("两点打断");
//复制管道
Pipe CopyPipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, pipe.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Pipe;
//创建每一个管道的定位线
Curve curve = Line.CreateBound(zuijing(locationCurve.Curve.GetEndPoint(0),locationCurve.Curve.GetEndPoint(1),touyingPoint),touyingPoint);
Curve curve1 = Line.CreateBound(zuijing(locationCurve.Curve.GetEndPoint(0), locationCurve.Curve.GetEndPoint(1), touyingPoint2), touyingPoint2);
locationCurve.Curve = curve;
LocationCurve locationcurve2 = CopyPipe.Location as LocationCurve;
locationcurve2.Curve = curve1;
transaction.Commit();
}
return Result.Succeeded;
}
public XYZ zuijing(XYZ startpoint,XYZ endpoint,XYZ curentpoint)
{
if (startpoint.DistanceTo(curentpoint)<endpoint.DistanceTo(curentpoint))
{
return startpoint;
}
return endpoint;
}
}
}
PS:非科班出身,代码不够规范,轻喷!!!