revit二次开发之找到与管道连接的连接器名称

原文链接
版权声明:本文为转载文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

很多时候我们都需要知道管道连接了哪些连接件,以下的代码就能帮助解决这个需求。

PS:无论是查找管道,或者连接件,只需改动几行代码就能实现。

 public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document doc = app.ActiveUIDocument.Document;
            Selection sel = app.ActiveUIDocument.Selection;

            Transaction ts = new Transaction(doc, "revit");
            ts.Start();

            Reference refPipe = sel.PickObject(ObjectType.Element, "pipe");
            Pipe pipe = doc.GetElement(refPipe) as Pipe;
            ConnectorSetIterator csi = pipe.ConnectorManager.Connectors.ForwardIterator();
            while (csi.MoveNext())
            {
                Connector conn = csi.Current as Connector;
                if (conn.IsConnected == true)//是否有连接
                {
                    ConnectorSet connectorSet = conn.AllRefs;//找到所有连接器连接的连接器【这句很重要】
                    ConnectorSetIterator csiChild = connectorSet.ForwardIterator();
                    while (csiChild.MoveNext())
                    {
                        Connector connected = csiChild.Current as Connector;
                        if (null != connected && connected.Owner.UniqueId != conn.Owner.UniqueId)
                        {
                            // look for physical connections 
                            if (connected.ConnectorType == ConnectorType.End ||
                                connected.ConnectorType == ConnectorType.Curve ||
                                connected.ConnectorType == ConnectorType.Physical)
                            {
                                //判断是不是管件
                                if (connected.Owner is FamilyInstance)
                                {
                                    TaskDialog.Show("管道所连接的连接件名称是:", connected.Owner.Name);
                                }
                            }
                        }
                    }
                }
            }

            ts.Commit();

            return Result.Succeeded;
        }

你可能感兴趣的:(revit二次开发之找到与管道连接的连接器名称)