如何调用Revit API实现管道打断?
之前写过关于管线打断的功能(详见我第上一编文章),可实现管线打断,使用的过程中发现风管如果存在有风道末端的时候,管线打断后移动或者偏移分管时会提示 风道末端需附着到风管。如何解决这个问题,我查看了资料,可以调用Revit API实现管道打断。直接上代码:
public class TestCreatEndOfTheDuct : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
Selection sel = uidoc.Selection;
Reference reference = sel.PickObject(ObjectType.PointOnElement);
//选择Pipe上的第一个点(GlobalPoint获取点击的点)
var reffirstpoint = reference.GlobalPoint;
//选择Pipe上的第二个点
reference = sel.PickObject(ObjectType.PointOnElement);
var refsecondpoint = reference.GlobalPoint;
var elem = doc.GetElement(reference) as MEPCurve;
var pickpoint1 = (elem.Location as LocationCurve).Curve.Project(reffirstpoint).XYZPoint;
var pickpoint2 = (elem.Location as LocationCurve).Curve.Project(refsecondpoint).XYZPoint;
Transaction tr = new Transaction(doc);
tr.Start("打断管线");
var newid = MechanicalUtils.BreakCurve(doc, elem.Id, pickpoint1); //MechanicalUtils.BreakCurve 打断管线
var newelem = doc.GetElement(newid) as MEPCurve;
var newlocationCurve = newelem.Location as LocationCurve;
var startpoint = newlocationCurve.Curve.GetEndPoint(0);
//打断后保留的原MEPcurve
var locationCurve = elem.Location as LocationCurve;
var endpoint = locationCurve.Curve.GetEndPoint(1);
var psfir = Tools.Nearpoint(startpoint, pickpoint1, pickpoint2);
var psend = Tools.Nearpoint(endpoint, pickpoint1, pickpoint2);
//打断后管道位置
var newline1 = Line.CreateBound(psend, endpoint);//打断后保留的原MEPcurve
var newline2 = Line.CreateBound(startpoint, psfir); //newcurve line
//Trans管道位置
locationCurve.Curve = newline1;
newlocationCurve.Curve = newline2;
tr.Commit();
}
catch (Exception)
{
}
return Result.Succeeded;
}
}
方法:
public static class Tools
{
public static XYZ Nearpoint(XYZ pickpoint, XYZ firpiont, XYZ secpoint)
{
if (firpiont.DistanceTo(pickpoint) > secpoint.DistanceTo(pickpoint))
{
return secpoint;
}
else
{
return firpiont;
}
}
}