Revit二次开发 ----> 管道翻弯

  1. 获取翻弯后产生的所有管道节点坐标

  2. 创建所有节点构成的管道定位线

  3. 创建管道

  4. 获取所有管道上的连接器

  5. 遍历所有连接器,得到相同位置处,不同宿主的连接器

  6. 创建连接器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using Autodesk.Revit.DB.Plumbing;
    
    namespace test
    {
        [Transaction(TransactionMode.Manual)]
        [Regeneration(RegenerationOption.Manual)]
        public class TestCommand: IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
            {
                UIDocument uiDoc = commandData.Application.ActiveUIDocument;
                Document doc = uiDoc.Document;
    
    
                //交互选择第一个点
                Reference reference = uiDoc.Selection.PickObject(ObjectType.PointOnElement,new Pipefilter());
                XYZ point1 = reference.GlobalPoint;
    
    
                //交互选择第二个点
                Reference reference2 = uiDoc.Selection.PickObject(ObjectType.PointOnElement,new Pipefilter());
                XYZ point2 = reference2.GlobalPoint;
    
                //获取管道
                Pipe pipe = doc.GetElement(reference) as Pipe;
    
                //获取管道定位线
                LocationCurve locationcurve = pipe.Location as LocationCurve;
    
                //获取管道定位起点终点
                XYZ start = locationcurve.Curve.GetEndPoint(0);
                XYZ end = locationcurve.Curve.GetEndPoint(1);
    
                //上面的点是管道表面的点,需要投影到管道中心定位线上
                XYZ propoint1 = locationcurve.Curve.Project(point1).XYZPoint;
                XYZ propoint2 = locationcurve.Curve.Project(point2).XYZPoint;
    
    
                //创建一个列表来存储所有的线
                List<Line> lines = creatlines(start, end, propoint1, propoint2, 40);
    
                //创建一个管道列表来存储生成的所有管道
                List<Pipe> pipes = new List<Pipe>();
    
                using (Transaction transaction = new Transaction(doc))
                {
                    transaction.Start("创建管道");
                    
    
                    //通过遍历生成的所有定位线来创建管道
                    foreach (Line line in lines)
                    {
                        Pipe newpipe = doc.GetElement(ElementTransformUtils.CopyElement(doc, pipe.Id, new XYZ(0, 0, 0)).ElementAt(0)) as Pipe;
                        LocationCurve newpipelocationcurve = newpipe.Location as LocationCurve;
                        newpipelocationcurve.Curve = line;
                        pipes.Add(newpipe);
                    }
    
                    doc.Delete(pipe.Id);
    
                    transaction.Commit();
    
                }
    
                //获取所有管道上所有的连接器
                List<Connector> connectors = new List<Connector>();
                foreach (Pipe pipe1 in pipes)
                {
                    ConnectorSet connects = pipe1.ConnectorManager.Connectors;
                    foreach (Connector connector in connects)
                    {
                        connectors.Add(connector);
                    }
                }
    
    
                //遍历连接器列表,找到同一位置处不同管道上的连接器,进而创建弯头
                for (int i = connectors.Count - 1; i > 0; i--)
                {
                    Connector con = connectors[i];
                    Connector co = nearconnector(con, connectors);
                        
                    if (co != null)
                    {
                        using (Transaction transaction2 = new Transaction(doc))
                        {
                            transaction2.Start("创建弯头");
                            doc.Create.NewElbowFitting(con, co);
                            transaction2.Commit();
                        }
                    }
                }
    
                return Result.Succeeded;
            }
    
    
            /// <summary>
            /// 该方法传入翻弯时产生的所有管道节点,返回翻弯生成的所有管道定位线
            /// </summary>
            /// <param name="管道起点start"></param>
            /// <param name="管道终点end"></param>
            /// <param name="交互选择的第一点propoint1"></param>
            /// <param name="交互选择的第二点propoint2"></param>
            /// <param name="偏移值(翻弯高度)offsetvalue"></param>
            /// <returns></returns>
            private List<Line> creatlines(XYZ start, XYZ end, XYZ propoint1, XYZ propoint2, double offsetvalue)
            {
                List<Line> lines = new List<Line>();
                Line thispipecurve = Line.CreateBound(propoint1, near(start, end, propoint1));
                lines.Add(thispipecurve);
                Line newpipecurve = Line.CreateBound(propoint2, near(start, end, propoint2));
                lines.Add(newpipecurve);
    
                XYZ propoint1offset = propoint1 + new XYZ(0, 0, offsetvalue);
                XYZ propoint2offset = propoint2 + new XYZ(0, 0, offsetvalue);
    
                Line offsetline = Line.CreateBound(propoint1offset,propoint2offset);
                lines.Add(offsetline);
    
                Line line1 = Line.CreateBound(propoint1, propoint1offset);
                lines.Add(line1);
    
                Line line2 = Line.CreateBound(propoint2, propoint2offset);
                lines.Add(line2);
    
                return lines;
    
            }
    
    
            //判断两个交互选择得到的点离管道的两端分别那个最近
            public XYZ near(XYZ startpoint,XYZ endpoint,XYZ pickedpoint)
            {
                if (startpoint.DistanceTo(pickedpoint) <endpoint.DistanceTo(pickedpoint))
                {
                    return startpoint;
                }
                return endpoint;
            }
    
    
            //创建一个过滤器,使选点的时候只在管道上选择
            public class Pipefilter : ISelectionFilter
            {
                public bool AllowElement(Element elem)
                {
                    if (elem is Pipe)
                    {
                        return true;
                    }
                    return false;
                }
    
                public bool AllowReference(Reference reference, XYZ position)
                {
                    return true;
                }
            }
    
    
            //遍历弯头,得到相同位置处另一个管道的连接器
            public Connector nearconnector(Connector con,List<Connector> connectors)
            {
                foreach (Connector connector in connectors)
                {
                    if (con.Origin.IsAlmostEqualTo(connector.Origin)&&con.Owner.Id!=connector.Owner.Id)
                    {
                        return connector;
                    }
                }
                return null;
            }
    
    
        }
    }
    
    

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Revit二次开发中,可以使用Revit API来实现管道的碰撞检测和自动翻弯功能。具体实现方法包括以下几个步骤: 1. 获取所有的管道对象,并对它们进行分类和分组。 2. 遍历所有的管道,检测管道是否与其他管道或墙体碰撞。 3. 如果管道碰撞,则根据碰撞的位置和方向计算出管道需要翻弯的位置和方向。 4. 创建翻弯管道对象,并将其放置在计算出的位置和方向上。 下面是一个简单的示例代码,用于实现简单的管道碰撞自动翻弯功能: ```csharp // 获取当前文档 Document doc = uidoc.Document; // 定义一个过滤器,过滤出所有的管道和墙体 ElementCategoryFilter pipeFilter = new ElementCategoryFilter(BuiltInCategory.OST_Pipe); ElementCategoryFilter wallFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls); LogicalOrFilter filter = new LogicalOrFilter(pipeFilter, wallFilter); // 获取所有符合条件的管道和墙体 List<Element> elements = new FilteredElementCollector(doc) .WherePasses(filter) .ToList(); // 将管道分组 List<Element> pipes = elements.Where(e => e.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Pipe).ToList(); // 遍历所有的管道 foreach (Element pipe in pipes) { // 检测管道是否与其他管道或墙体碰撞 bool isCollided = false; foreach (Element e in elements) { if (e.Id == pipe.Id) continue; if (pipe.get_Geometry(new Options()).Intersect(e.get_Geometry(new Options())) != null) { isCollided = true; break; } } if (!isCollided) continue; // 计算管道需要翻弯的位置和方向 // TODO: 计算管道需要翻弯的位置和方向 // 创建翻弯管道对象 // TODO: 创建翻弯管道对象并放置在计算出的位置和方向上 } ``` 上述代码中,我们首先使用过滤器获取所有的管道和墙体对象,并将管道分组。然后,我们遍历所有的管道,检测管道是否与其他管道或墙体碰撞。如果管道碰撞,则需要根据碰撞的位置和方向计算出管道需要翻弯的位置和方向。最后,我们创建翻弯管道对象,并将其放置在计算出的位置和方向上。需要注意的是,上述代码仅为示例代码,实际应用中可能需要根据具体需求进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值