[TransactionAttribute(TransactionMode.Manual)]
public class Test : IExternalCommand
{
Autodesk.Revit.ApplicationServices.Application application;
Autodesk.Revit.DB.Document document;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication revit = commandData.Application;
application = revit.Application;
UIDocument uiDocument = revit.ActiveUIDocument;
document = uiDocument.Document;
using (Transaction transaction = new Transaction(document, "风管打断"))
{
transaction.Start();
DuctSelectionFilter ductSelectionFilter=new DuctSelectionFilter();
IList<Duct> ductList=new List<Duct>();
//选取一个风管
Duct duct = PickMEPCurve(uiDocument, ductSelectionFilter, "请选择一个风管") as Duct;
XYZ pointOnDuct = PickPointOnMEPCurve(uiDocument, duct as MEPCurve, "请选择一个打断点");
IList<MEPCurve> mepList = SliceMEPCurveIntoTwo(duct as MEPCurve, pointOnDuct, document);
transaction.Commit();
}
}
}
//风管选择过滤器
class DuctSelectionFilter : ISelectionFilter
{
public bool AllowElement(Element element)
{
if (element is Duct)
{
return true;
}
return false;
}
public bool AllowReference(Reference refer, XYZ point)
{
return false;
}
}
//选择一个机电管件
MEPCurve PickMEPCurve(UIDocument uiDocument,ISelectionFilter selectionFilter,string promptTextWhilePickingElement)
{
Selection selection = uiDocument.Selection;
Reference mepReference = selection.PickObject(ObjectType.Element, selectionFilter, promptTextWhilePickingElement);
return uiDocument.Document.GetElement(mepReference) as MEPCurve;
}
//在管件上选择一个打断点
XYZ PickPointOnMEPCurve(UIDocument uiDocument,MEPCurve mep,string promptTextWhilePickingPoint)
{
Selection selection = uiDocument.Selection;
XYZ selectedPoint = selection.PickPoint(ObjectSnapTypes.Nearest, promptTextWhilePickingPoint);
LocationCurve locationCurve = mep.Location as LocationCurve;
IntersectionResult intersectionResult = locationCurve.Curve.Project(selectedPoint);
return intersectionResult.XYZPoint;
}
//打断管件
IList<MEPCurve> SliceMEPCurveIntoTwo(MEPCurve mepCurve, XYZ point, Document document)
{
IList<MEPCurve> mepCurveList = new List<MEPCurve>();
LocationCurve locationCurve = mepCurve.Location as LocationCurve;
XYZ start_point = locationCurve.Curve.GetEndPoint(0);
XYZ end_point = locationCurve.Curve.GetEndPoint(1);
Curve line1 = Line.CreateBound(start_point, point);
Curve line2 = Line.CreateBound(point, end_point);
//原地复制原机电管线
MEPCurve newMEPCurve = document.GetElement(ElementTransformUtils.CopyElement(document, mepCurve.Id, new XYZ(0, 0, 0)).ElementAt(0)) as MEPCurve;
LocationCurve newLocationCurve = newMEPCurve.Location as LocationCurve;
//为复制的机电管线指定基线
newLocationCurve.Curve = line1;
//断开原机电管线的连接,建立新机电管线的连接
ConnectorSetIterator connectorSetIterator = mepCurve.ConnectorManager.Connectors.ForwardIterator();
while (connectorSetIterator.MoveNext())
{
Connector connector = connectorSetIterator.Current as Connector;
if (connector.Origin.IsAlmostEqualTo(start_point))
{
ConnectorSet tempConnectorSet = connector.AllRefs;
ConnectorSetIterator tempConnectorSetIterator = tempConnectorSet.ForwardIterator();
while (tempConnectorSetIterator.MoveNext())
{
Connector tempConnector = tempConnectorSetIterator.Current as Connector;
if (tempConnector != null)
{
if (tempConnector.ConnectorType == ConnectorType.End ||
tempConnector.ConnectorType == ConnectorType.Curve ||
tempConnector.ConnectorType == ConnectorType.Physical)
{
if (tempConnector.Owner.UniqueId != mepCurve.UniqueId)
{
connector.DisconnectFrom(tempConnector);
ConnectorSetIterator newConnectorSetIterator = newMEPCurve.ConnectorManager.Connectors.ForwardIterator();
while (newConnectorSetIterator.MoveNext())
{
Connector newConnector = newConnectorSetIterator.Current as Connector;
if (newConnector.Origin.IsAlmostEqualTo(start_point))
{
newConnector.ConnectTo(tempConnector);
}
}
}
}
}
}
}
}
//缩短原机电管线
locationCurve.Curve = line2;
mepCurveList.Add(newMEPCurve);
mepCurveList.Add(mepCurve);
return mepCurveList;
}