Revit二次开发打短管(WPF)

[Transaction(TransactionMode.Manual)]
    public class BreakMEPCurveCmd : IExternalCommand
    {
        UIDocument uIDocument = null;
        Document document = null;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uIApp = commandData.Application;
            uIDocument = uIApp.ActiveUIDocument;
            document = uIDocument.Document;
        }
    }

创建外部事件实例

 ExternalEventExample externalEventExample = new ExternalEventExample(commandData);

注册事件

 ExternalEvent externalEvent = ExternalEvent.Create(externalEventExample);

创建窗体对象

BreakMEPCurveForm breakMEPCurveForm = new BreakMEPCurveForm(this,externalEvent);

打开窗体

breakMEPCurveForm.Show();

 return Result.Succeeded;

外部事件

 /// <summary>
    /// 外部事件
    /// </summary>
    public class ExternalEventExample : IExternalEventHandler
    {
        UIDocument uIDocument = null;
        Document document = null;
        public ExternalEventExample(ExternalCommandData commandData)
        {
            UIApplication uIApp = commandData.Application;
            uIDocument = uIApp.ActiveUIDocument;
            document = uIDocument.Document;
        }
        public static int id = -1;
        public void Execute(UIApplication app)
        {
            if (id == 0)
            {
                BreakMEPCurve();
            }
            else if (id == 1)
            {
                BreakMEPCurves();
            }
        }

        public string GetName()
        {
            return "name";
        }

一个点打断

        /// <summary>
        /// 一个点打断
        /// </summary>
        /// <param name="mepCurve">原管</param>
        /// <param name="breakPoint">打断点</param>
        /// <returns>复制的</returns>
        public MEPCurve BreakMEPCurve()
        {
            try
            {


                Reference reference = uIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new MEPCurveFilter());
                MEPCurve mepCurve = document.GetElement(reference) as MEPCurve;
                XYZ breakPoint = reference.GlobalPoint;

                MEPCurve mepCurveCopy = null;
                using (Transaction trans = new Transaction(document, "tran"))
                {
                    trans.Start();


                    //复制管
                    ICollection<ElementId> ids = ElementTransformUtils.CopyElement(document, mepCurve.Id, new XYZ(0, 0, 0));
                ElementId id = ids.FirstOrDefault();
                mepCurveCopy = document.GetElement(id) as MEPCurve;

                //获取开始的点
                Curve curve = (mepCurve.Location as LocationCurve).Curve;
                XYZ startPoint = curve.GetEndPoint(0);
                XYZ endPoint = curve.GetEndPoint(1);
                //点映射到线
                breakPoint = curve.Project(breakPoint).XYZPoint;
                //缩短原来的管道断点
                Line line1 = Line.CreateBound(startPoint, breakPoint);
                //复制管 从断点到原管的终点
                Line line2 = Line.CreateBound(breakPoint, endPoint);

                //解除原管与弯头的连接
                ConnectorSet connectorSets = mepCurve.ConnectorManager.Connectors;
                Connector connector = null;
                foreach (Connector item in connectorSets)
                {
                    bool isBreak = false;
                    if (item.Id == 1 && item.IsConnected)
                    {
                        foreach (Connector item2 in item.AllRefs)
                        {
                            if (item2.Owner is FamilyInstance)
                            {
                                item.DisconnectFrom(item2);
                                connector = item2;
                                isBreak = true;
                                break;
                            }
                        }
                    }
                    if (isBreak)
                    {
                        break;
                    }
                }
                //如果点错在
                if (connector != null)
                {
                    //复制的管连接原来的连接器
                    foreach (Connector item in mepCurveCopy.ConnectorManager.Connectors)
                    {
                        if (item.Id == 1)
                        {
                            item.ConnectTo(connector);
                        }
                    }
                }

            //改原管和复制的管
            (mepCurve.Location as LocationCurve).Curve = line1;
                (mepCurveCopy.Location as LocationCurve).Curve = line2;




                    trans.Commit();
                }


                return mepCurveCopy;
            }
            catch (Exception ex)
            {

                TaskDialog.Show("错误", ex.Message);
            }
            return null;
        }

两个点 打断

        /// <summary>
        /// 两个点 打断
        /// </summary>
        /// <param name="mEPCurve">原管</param>
        /// <param name="breakPoint1">打断点1</param>
        /// <param name="breakPoint2">打断点2</param>
        /// <returns>复制管</returns>
        public MEPCurve BreakMEPCurves()
        {
            try
            {



                Reference reference = uIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new MEPCurveFilter());
                MEPCurve mEPCurve = document.GetElement(reference) as MEPCurve;
                XYZ breakPoint1 = reference.GlobalPoint;
                XYZ breakPoint2 = uIDocument.Selection.PickPoint();


                MEPCurve mEPCurvecCopy1 = null;
                using (Transaction transaction = new Transaction(document,"tran"))
                {
                    transaction.Start();



                //复制1
                ICollection<ElementId> ids1 = ElementTransformUtils.CopyElement(document, mEPCurve.Id, new XYZ(0, 0, 0));
                ElementId id1 = ids1.FirstOrDefault();
                mEPCurvecCopy1 = document.GetElement(id1) as MEPCurve;
                    
                复制2
                //ICollection<ElementId> ids2 = ElementTransformUtils.CopyElement(document, mEPCurve.Id, new XYZ(0, 0, 0));
                //ElementId id2 = ids2.FirstOrDefault();
                //MEPCurve mEPCurveCopy2 = document.GetElement(id2) as MEPCurve;

                //获取原管的起始点
                Curve curve = (mEPCurve.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 connector = 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);
                                connector = item2;
                                isBreak = true;
                                break;
                            }
                        }
                    }
                    if (isBreak)
                    {
                        break;
                    }
                }
                if (connector != null)
                {
                    //复制的管连原来的连接器
                    foreach (Connector item in mEPCurvecCopy1.ConnectorManager.Connectors)
                    {
                        if (item.Id == 1)
                        {
                            item.ConnectTo(connector);
                            break;
                        }
                    }
                }
                //如果起点和breakPoint2近
                if (startPoint.DistanceTo(breakPoint1) > startPoint.DistanceTo(breakPoint2))
                {
                    XYZ item = breakPoint1;
                    breakPoint1 = breakPoint2;
                    breakPoint2 = item;
                }
                //改原管
                Line line1 = Line.CreateBound(startPoint, breakPoint1);
                (mEPCurve.Location as LocationCurve).Curve = line1;
                //改复制的管
                Line line2 = Line.CreateBound(breakPoint2, endPoint);
                (mEPCurvecCopy1.Location as LocationCurve).Curve = line2;

                    // Line line3 = Line.CreateBound(breakPoint1, breakPoint2);
                    // (mEPCurveCopy2.Location as LocationCurve).Curve = line3;

                    transaction.Commit();
                }

                return mEPCurvecCopy1;
            }
            catch (Exception ex)
            {

                TaskDialog.Show("错误", ex.Message);
            }
            return null;
        }
    }

WPF(BreakMEPCurveForm.xaml)

    /// <summary>
    /// BreakMEPCurveForm.xaml 的交互逻辑
    /// </summary>
    public partial class BreakMEPCurveForm : Window
    {
        BreakMEPCurveCmd breakMEPCurveCmd = null;
        ExternalEvent externalEvent = null;
        public BreakMEPCurveForm(BreakMEPCurveCmd breakMEPCurveCmd,ExternalEvent externalEvent)
        {
            this.breakMEPCurveCmd = breakMEPCurveCmd;
            this.externalEvent = externalEvent;
            InitializeComponent();
            this.Topmost = true;
        }
        /// <summary>
        /// 单点打断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            ExternalEventExample.id = 0;
            externalEvent.Raise();
            //breakMEPCurveCmd.BreakMEPCurve();
        }
        /// <summary>
        /// 两点打断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            ExternalEventExample.id = 1;
            externalEvent.Raise();
            //breakMEPCurveCmd.BreakMEPCurves();
        }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周杰伦fans

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值