Revit 二次开发打断管线(转载)

[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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值