管线打断

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 PipeInterrupt
{
    /// <summary>
    /// 实现一键打断管线
    /// </summary>
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //选取需要打断的线,点选位置即为端点
            MEPCurveSelectionFilter filter = new MEPCurveSelectionFilter();
            Reference reference = uidoc.Selection.PickObject(ObjectType.Element, filter, "点选管线");
            XYZ pickPoint = reference.GlobalPoint;
            //获取选择的管线对象
            MEPCurve mEPCurve = doc.GetElement(reference) as MEPCurve;
            //获取原来的定位线、端点
            Line line = (mEPCurve.Location as LocationCurve).Curve as Line;
            XYZ p1 = line.GetEndPoint(0);
            XYZ p2 = line.GetEndPoint(1);
            //获取管道起终点的连接件
            ConnectorSet conSet = mEPCurve.ConnectorManager.Connectors;
            Connector connectorStart = ConnectorAtPoint(conSet, p1);
            Connector connectorEnd = ConnectorAtPoint(conSet, p2);
            //关键步骤1:获得与指定连接件相连的连接件
            Connector fittingConstart = GetConToConctor(connectorStart);
            Connector fittingConEnd = GetConToConctor(connectorEnd);
            //将拾取点投影到管线定位线上获取投影点
            XYZ newPoint = line.Project(pickPoint).XYZPoint;
            //将线以投影点为界分成两段新定位线
            Line line1 = Line.CreateBound(newPoint, p1);
            Line line2 = Line.CreateBound(newPoint, p2);
            //新建并开启事务
            Transaction transaction = new Transaction(doc, "管线打断");
            transaction.Start();
            //关键步骤2:复制管线,并将其定位线设为新定位线
            MEPCurve mEPCurve1 = CopyMEPToLine(doc, mEPCurve, line1);
            MEPCurve mEPCurve2 = CopyMEPToLine(doc, mEPCurve, line2);
            //删除原来的管线
            doc.Delete(mEPCurve.Id);
            //关键步骤3:恢复原有连接
            //获取新管线在原两端点处的Connector
            ConnectorSet newConSet1 = mEPCurve1.ConnectorManager.Connectors;
            ConnectorSet newConSet2 = mEPCurve2.ConnectorManager.Connectors;
            Connector newConStart = ConnectorAtPoint(newConSet1, p1);
            Connector newConEnd = ConnectorAtPoint(newConSet2, p2);
            //恢复与原管件的连接
            newConStart.ConnectTo(fittingConstart);
            newConEnd.ConnectTo(fittingConEnd);
            //提交事务
            transaction.Commit();
            return Result.Succeeded;
        }
        #region 管线定位线拾取过滤
        class MEPCurveSelectionFilter: ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
               
                if (elem is MEPCurve && !(elem is InsulationLiningBase)) 
                    return true;
                else
                    return false;
            }
            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;//返回true时表示内容可以被边、点、面选中
            }
        }
        #endregion

        //获取指定点处的连接件
        public Connector ConnectorAtPoint(ConnectorSet connectorSet, XYZ point)
        {
            //遍历连接件集合
            foreach (Connector connector in connectorSet)
            {
                //距离容差为1mm
                if (connector.Origin.DistanceTo(point) < 1 / 304.8)
                {
                    //返回该连接件
                    return connector;
                }
            }
            //如果没有匹配到,则返回null;
            return null;
        }
        //获取与管线相连的管件连接件
        public Connector GetConToConctor(Connector connector)
        {
            foreach (Connector con in connector.AllRefs)
            {
                //仅选择管件
                if (con.Owner is FamilyInstance)
                {
                    return con;
                }
            }
            return null;
        }

        //复制管线并设定新的定位线
        public MEPCurve CopyMEPToLine(Document doc, MEPCurve mepCurve, Line line)
        {
            //从原位复制一份
            ElementId copyID = ElementTransformUtils.CopyElement(doc, mepCurve.Id, XYZ.Zero).First();
            //获得复制的对象
            MEPCurve mEPCurveNew = doc.GetElement(copyID) as MEPCurve;
            //设置新对象的定位线
            (mEPCurveNew.Location as LocationCurve).Curve = line;
            return mEPCurveNew;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值