Revit二次开发 管道弯头、三通、四通的生成代码

原文链接:https://blog.csdn.net/qq_39412924/article/details/83110866

当进行管道生成的时候,发现二次开发基本教程中仅有弯头的示例,于是我补充上三通与四通的生成示例。

弯头的生成最简单,书中有教程,且弯头的生成对于角度的要求较低。

using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

[Transaction(TransactionMode.Manual)]
    class NewElbow : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            Selection sel = uiApp.ActiveUIDocument.Selection;

            //选取两个管
            var reference1 = sel.PickObject(ObjectType.Element, "请选择第1个管");
            MEPCurve duct1 = doc.GetElement(reference1) as MEPCurve;

            var reference2 = sel.PickObject(ObjectType.Element, "请选择第2个管");
            MEPCurve duct2 = doc.GetElement(reference2) as MEPCurve;

            //弯头
            ConnectTwoDuctsWithElbow(doc, duct1, duct2);

            return Result.Succeeded;
        }
        /// <summary>
        /// 连接管道
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="duct1"></param>
        /// <param name="duct2"></param>
        public static void ConnectTwoDuctsWithElbow(Document doc, MEPCurve duct1, MEPCurve duct2)
        {
            double minDistance = double.MaxValue;
            Connector connector1, connector2;
            connector1 = connector2 = null;

            foreach (Connector con1 in duct1.ConnectorManager.Connectors)
            {
                foreach (Connector con2 in duct2.ConnectorManager.Connectors)
                {
                    var dis = con1.Origin.DistanceTo(con2.Origin);
                    if (dis < minDistance)
                    {
                        minDistance = dis;
                        connector1 = con1;
                        connector2 = con2;
                    }

                }
            }

            if (connector1 != null && connector2 != null)
            {
                using (Transaction tran = new Transaction(doc))
                {
                    tran.Start("1033067630");

                    var elbow = doc.Create.NewElbowFitting(connector1, connector2);

                    tran.Commit();
                }
            }
        }
    }

接下是三通,三通最需要注意的事角度!!角度!!角度!!,第二根管应该垂直于第一根管,不能超出1°!极为严格!

还有主次顺序问题。

[Transaction(TransactionMode.Manual)]
    class NewTeeFitting : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            Selection sel = uiApp.ActiveUIDocument.Selection;

            //选取两个管道
            var reference1 = sel.PickObject(ObjectType.Element, "请选择第1个管");
            MEPCurve duct1 = doc.GetElement(reference1) as MEPCurve;

            var reference2 = sel.PickObject(ObjectType.Element, "请选择第2个管");
            MEPCurve duct2 = doc.GetElement(reference2) as MEPCurve;


            Curve curve1 = (doc.GetElement(reference1).Location as LocationCurve).Curve;

            Curve curve2 = (doc.GetElement(reference2).Location as LocationCurve).Curve;

            var ductList = GetMainDuct(duct1, duct2);

            MEPCurve MainDuct = ductList[0];//the main Pipe
            MEPCurve LessDuct = ductList[1];// the minor Pipe

            MEPCurve duct3 = null;//the main pipe

            IntersectionResultArray intersectPoint = new IntersectionResultArray();

            var x = curve1.Intersect(curve2, out intersectPoint);

            if (x == SetComparisonResult.Overlap)
            {
                using (Transaction tran = new Transaction(doc))
                {
                    tran.Start("1033067630");

                    var OrginPoint = intersectPoint.get_Item(0).XYZPoint;

                    var elementId = PlumbingUtils.BreakCurve(doc, MainDuct.Id, OrginPoint);

                    duct3 = doc.GetElement(elementId) as MEPCurve;

                    tran.Commit();
                }
            }


            ConnectTwoDuctsWithElbow(doc, MainDuct, duct3, LessDuct);

            return Result.Succeeded;
        }

        /// <summary>
        /// 区分主要管道与次要管道
        /// </summary>
        /// <param name="duct1"></param>
        /// <param name="duct2"></param>
        /// <returns></returns>
        public static List<MEPCurve> GetMainDuct(MEPCurve duct1, MEPCurve duct2)
        {
            List<MEPCurve> mEPCurves = new List<MEPCurve>();

            Curve curve1 = (duct1.Location as LocationCurve).Curve;
            Curve curve2 = (duct2.Location as LocationCurve).Curve;

            List<double> disList = new List<double>();
            double dis = double.MaxValue;
            XYZ pointA1 = curve1.GetEndPoint(0);
            XYZ pointA2 = curve1.GetEndPoint(1);
            XYZ pointB1 = curve2.GetEndPoint(0);
            XYZ pointB2 = curve2.GetEndPoint(1);

            var distance1 = curve2.Distance(pointA1);
            var distance2 = curve2.Distance(pointA2);
            var distance3 = curve1.Distance(pointB1);
            var distance4 = curve1.Distance(pointB2);

            disList.Add(distance1);
            disList.Add(distance2);
            disList.Add(distance3);
            disList.Add(distance4);

            dis = disList.Min();
            var x = disList.IndexOf(dis);

            if (x < 2)
            {
                mEPCurves.Add(duct2);
                mEPCurves.Add(duct1);
                return mEPCurves;
            }
            else
            {
                mEPCurves.Add(duct1);
                mEPCurves.Add(duct2);
                return mEPCurves;
            }


        }

        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="duct1">主要管道1</param>
        /// <param name="duct2">主要管道2</param>
        /// <param name="duct3">次要管道</param>
        public static void ConnectTwoDuctsWithElbow(Document doc, MEPCurve duct1, MEPCurve duct2, MEPCurve duct3)
        {
            double minDistance = double.MaxValue;
            Connector connector1, connector2, connector3;
            connector1 = connector2 = connector3 = null;

            foreach (Connector con1 in duct1.ConnectorManager.Connectors)
            {
                foreach (Connector con2 in duct2.ConnectorManager.Connectors)
                {
                    var dis = con1.Origin.DistanceTo(con2.Origin);
                    if (dis < minDistance)
                    {
                        minDistance = dis;
                        connector1 = con1;
                        connector2 = con2;
                    }

                }
            }

            minDistance = double.MaxValue;//重置
            foreach (Connector con3 in duct3.ConnectorManager.Connectors)
            {
                var dis = con3.Origin.DistanceTo(connector1.Origin);
                if (dis < minDistance)
                {
                    minDistance = dis;
                    connector3 = con3;
                }
            }


            if (connector1 != null && connector2 != null && connector3 != null)
            {
                using (Transaction tran = new Transaction(doc))
                {
                    tran.Start("xx");

                    var elbow = doc.Create.NewTeeFitting(connector1, connector2, connector3);

                    tran.Commit();
                }
            }
        }
    }

最后就是4通,依旧是角度问题要注意,且连接器内参数,第一个和第二个参数共线,第三个和第四个参数共线(即在同一根线上)

[Transaction(TransactionMode.Manual)]
    class NewCross : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            MEPCurve duct3, duct4;
            duct3 = duct4 = null;
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
            Selection sel = uiApp.ActiveUIDocument.Selection;

            //选取两个风管
            var reference1 = sel.PickObject(ObjectType.Element, "请选择第1个风管");
            MEPCurve duct1 = doc.GetElement(reference1) as MEPCurve;

            var reference2 = sel.PickObject(ObjectType.Element, "请选择第2个风管");
            MEPCurve duct2 = doc.GetElement(reference2) as MEPCurve;

            Curve curve1 = (doc.GetElement(reference1).Location as LocationCurve).Curve;

            Curve curve2 = (doc.GetElement(reference2).Location as LocationCurve).Curve;

            IntersectionResultArray intersectPoint = new IntersectionResultArray();

            var x = curve1.Intersect(curve2, out intersectPoint);

            if (x == SetComparisonResult.Overlap)
            {
                using (Transaction tran = new Transaction(doc))
                {
                    tran.Start("x");

                    var OrginPoint = intersectPoint.get_Item(0).XYZPoint;

                    var elementId3 = PlumbingUtils.BreakCurve(doc, duct1.Id, OrginPoint);

                    duct3 = doc.GetElement(elementId3) as MEPCurve;//duct1 and duct3 in one line 

                    var elementId4 = PlumbingUtils.BreakCurve(doc, duct2.Id, OrginPoint);

                    duct4 = doc.GetElement(elementId4) as MEPCurve;//duct2 and duct4 in one line

                    tran.Commit();
                }
            }

            ConnectTwoDuctsWithElbow(doc, duct1, duct3, duct2, duct4);//四通

            return Result.Succeeded;
        }
        /// <summary>
        /// 创建连接,四通前2个共线,后2个共线
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="duct1"></param>
        /// <param name="duct2"></param>
        /// <param name="duct3"></param>
        public static void ConnectTwoDuctsWithElbow(Document doc, MEPCurve duct1, MEPCurve duct2, MEPCurve duct3, MEPCurve duct4)
        {
            double minDistance = double.MaxValue;
            Connector connector1, connector2, connector3, connector4;
            connector1 = connector2 = connector3 = connector4 = null;

            foreach (Connector con1 in duct1.ConnectorManager.Connectors)
            {
                foreach (Connector con2 in duct2.ConnectorManager.Connectors)
                {
                    var dis = con1.Origin.DistanceTo(con2.Origin);
                    if (dis < minDistance)
                    {
                        minDistance = dis;
                        connector1 = con1;
                        connector2 = con2;
                    }

                }
            }

            minDistance = double.MaxValue;//重置
            foreach (Connector con3 in duct3.ConnectorManager.Connectors)
            {
                var dis = con3.Origin.DistanceTo(connector1.Origin);
                if (dis < minDistance)
                {
                    minDistance = dis;
                    connector3 = con3;
                }
            }

            minDistance = double.MaxValue;//重置
            foreach (Connector con4 in duct4.ConnectorManager.Connectors)
            {
                var dis = con4.Origin.DistanceTo(connector1.Origin);
                if (dis < minDistance)
                {
                    minDistance = dis;
                    connector4 = con4;
                }
            }


            if (connector1 != null && connector2 != null && connector3 != null && connector4 != null)
            {
                using (Transaction tran = new Transaction(doc))
                {
                    tran.Start("xx");

                    var elbow = doc.Create.NewCrossFitting(connector1, connector2, connector3, connector4);

                    tran.Commit();
                }
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值