通过复制要打断的管
再用pickpoint的点和原来的起点终点
来创建线 改原管和复制出来的管
代码如下:
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Break_MEPCurve
{
[TransactionAttribute(TransactionMode.Manual)]
class CreatePipe : IExternalCommand
{
UIDocument uIDocument = null;
Document document = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Application application = uiApp.Application;
uIDocument = uiApp.ActiveUIDocument;
document = uIDocument.Document;
using (Transaction transaction = new Transaction(document, "创建管"))
{
transaction.Start();
//按escpickpoint报错,利用这一特性退出循环
try
{
while (true)
{
//获得管
Reference reference1 = uIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new MEPCurveFilter());
MEPCurve mepCurve1 = document.GetElement(reference1) as MEPCurve;
//打断点
XYZ breakPoint1 = uIDocument.Selection.PickPoint();
XYZ breakPoint2 = breakPoint2 = uIDocument.Selection.PickPoint();
//打断一个点
if (false)
{
BreakMEPCurve(mepCurve1, breakPoint1);
document.Regenerate();//文档刷新
}
//打断俩个点
if (true)
{
BreakMEPCurves(mepCurve1, breakPoint1, breakPoint2);
document.Regenerate();//文档刷新
}
}
}
catch (Exception ex)
{
TaskDialog.Show("提示", "打断结束");
}
transaction.Commit();
}
return Result.Succeeded;
}
/// <summary>
///一个点 打断
/// </summary>
public void BreakMEPCurve(MEPCurve mepCurve, XYZ breakPoint)
{
//复制管
ICollection<ElementId> ids = ElementTransformUtils.CopyElement(document, mepCurve.Id, new XYZ(0, 0, 0));//向量不改变
ElementId id = ids.FirstOrDefault();
MEPCurve mepCurveCopy = document.GetElement(id) as MEPCurve;
//获取开始的点(0)
Curve curve = (mepCurve.Location as LocationCurve).Curve;
XYZ startXYZ = curve.GetEndPoint(0);
XYZ endXYZ = curve.GetEndPoint(1);
//判断选择的点是否在管身上
breakPoint = curve.Project(breakPoint).XYZPoint; //点映射到线上得到线上点并重新赋值给断点
//缩短原来的管到断点
Line line1 = Line.CreateBound(startXYZ, breakPoint);
//复制管 从断点到原管的终点(1)
Line line2 = Line.CreateBound(breakPoint, endXYZ);
//解除原管(1)点与弯头的链接
Connector connect = null;
foreach (Connector item in mepCurve.ConnectorManager.Connectors)
{
bool isBreak = false;
if (item.Id == 1 && item.IsConnected)
{
foreach (Connector item2 in item.AllRefs)
{
if (item2.Owner is FamilyInstance)
{
item.DisconnectFrom(item2);
connect = item2;
isBreak = true;
break;
}
}
}
if (isBreak)
{
break;
}
}
//复制的管链接原管的连接器
foreach (Connector item in mepCurveCopy.ConnectorManager.Connectors)
{
if (item.Id == 1)
{
item.ConnectTo(connect);
}
}
//改原管和复制的管
(mepCurve.Location as LocationCurve).Curve = line1;
(mepCurveCopy.Location as LocationCurve).Curve = line2;
}
/// <summary>
/// 两个点 打断
/// </summary>
public void BreakMEPCurves(MEPCurve mepCurve1, XYZ breakPoint1, XYZ breakPoint2)
{
//复制
ICollection<ElementId> ids = ElementTransformUtils.CopyElement(document, mepCurve1.Id, new XYZ(0, 0, 0));
ElementId id = ids.FirstOrDefault();
MEPCurve mepCurveCopy = document.GetElement(id) as MEPCurve;
ICollection<ElementId> ids2 = ElementTransformUtils.CopyElement(document, mepCurveCopy.Id, new XYZ(0, 0, 0));
ElementId id2 = ids2.FirstOrDefault();
MEPCurve mepCurveCopy2 = document.GetElement(id2) as MEPCurve;
//获取原管的起始点
Curve curve = (mepCurve1.Location as LocationCurve).Curve;
XYZ startPoint = curve.GetEndPoint(0);
XYZ endPoint = curve.GetEndPoint(1);
//映射点到管
breakPoint1 = curve.Project(breakPoint1).XYZPoint;
breakPoint2 = curve.Project(breakPoint2).XYZPoint;
//原管(1)处的连接器
Connector connect = null;
//解除原管(1)dian与弯管的位置连接
foreach (Connector item in mepCurve1.ConnectorManager.Connectors)
{
bool isBreak = false;
if (item.Id == 1 && item.IsConnected)
{
foreach (Connector item2 in item.AllRefs)
{
if (item2.Owner is FamilyInstance)
{
item.DisconnectFrom(item2);
connect = item2;
isBreak = true;
break;
}
}
}
if (isBreak)
{
break;
}
}
//复制的管连原来的连接器
foreach (Connector item in mepCurveCopy.ConnectorManager.Connectors)
{
if (item.Id == 1)
{
item.ConnectTo(connect);
break;
}
}
//如果起点和breakPoint2近
if (startPoint.DistanceTo(breakPoint1) > startPoint.DistanceTo(breakPoint2))
{
XYZ item = breakPoint1;
breakPoint1 = breakPoint2;
breakPoint2 = item;
}
//改原管
Line line1 = Line.CreateBound(startPoint, breakPoint1);
(mepCurve1.Location as LocationCurve).Curve = line1;
//改复制的管
Line line2 = Line.CreateBound(breakPoint2, endPoint);
(mepCurveCopy.Location as LocationCurve).Curve = line2;
if (startPoint.DistanceTo(breakPoint1) > startPoint.DistanceTo(breakPoint2))
{
XYZ item = breakPoint1;
breakPoint2 = breakPoint1;
breakPoint1 = item;
}
Line line3 = Line.CreateBound(breakPoint1, breakPoint2);
(mepCurveCopy2.Location as LocationCurve).Curve = line3;
}
/// <summary>
/// MEPCurve过滤器
/// </summary>
public class MEPCurveFilter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem is MEPCurve)
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return true;
}
}
/// <summary>
/// 毫米转英尺
/// </summary>
/// <param name="var">毫米</param>
/// <returns>英尺</returns>
public double ToFoot(double var)
{
double foot = UnitUtils.ConvertToInternalUnits(var, DisplayUnitType.DUT_MILLIMETERS);
return foot;
}
到revit里