Revit—线管连接桥架

       Revit中,是支持线管连接桥架无配件桥架与桥架之间相互连接的。

      如果你项目中需要类似的功能,这篇文章大概率能解决你的问题。Revit没有显示的API进行类似的操作。但是在一些你可能熟悉的API中提供了类似隐藏的功能。比如这次我们用到的就是NewTeeFitting这个函数。

      函数签名: public FamilyInstance NewTeeFitting(Connector connector1, Connector connector2, Connector connector3);

      当我们进行线管与桥架之间的连接时,需要如下的参数限定。

      connector1和connector2必须相同,且是距离实际连接点较远的主管上的连接件

      connector3是侧管上靠近实际连接点的连接件

 Revit—线管连接桥架_第1张图片

       实际调用API时:

      doc.Create.NewTeeFitting(mainConnector, mainConnector, lateralConnector);

     参数名称和上图标识对应。

     具体的测试代码如下:

  [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    public class ConduitJoinCableTrayTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uiDoc = commandData.Application.ActiveUIDocument;
            var doc = commandData.Application.ActiveUIDocument.Document;
            Conduit conduit = null;
            CableTray cableTray = null;
            try
            {
                var elementId = uiDoc.Selection.PickObject(ObjectType.Element, new TypeSelectionFilter(),"请选择线管").ElementId;
                conduit = doc.GetElement(elementId) as Conduit;
                elementId = uiDoc.Selection.PickObject(ObjectType.Element, new TypeSelectionFilter(), "请选择桥架").ElementId;
                cableTray = doc.GetElement(elementId) as CableTray;
            }
            catch (Exception ex)
            {

                return Result.Cancelled;
            }
            if (conduit == null|| cableTray==null)
            {
                return Result.Cancelled;
            }
            Transaction tran = new Transaction(doc, "ConduitJoinCableTray");
            try
            {
                tran.Start();
                var conduitCurve = (conduit.Location as LocationCurve).Curve as Line;
                var cableTrayCurve = (cableTray.Location as LocationCurve).Curve as Line;
                var unBoundConduit = conduitCurve.Clone();
                unBoundConduit.MakeUnbound();
                var unBoundCableTray = cableTrayCurve.Clone();
                unBoundCableTray.MakeUnbound();
                var inter = unBoundConduit.Intersect(unBoundCableTray, out IntersectionResultArray result);
                XYZ intersectionPoint = null;
                if (null != result)
                {
                    foreach (IntersectionResult iResult in result)
                    {
                        intersectionPoint=iResult.XYZPoint;
                        break;
                    }
                }
                if(intersectionPoint==null)
                {
                    return Result.Succeeded;
                }
                XYZ cableUsePoint = null;//取远处点
                if((cableTrayCurve.GetEndPoint(0)- intersectionPoint).GetLength() > (cableTrayCurve.GetEndPoint(1) - intersectionPoint).GetLength())
                {
                    cableUsePoint = cableTrayCurve.GetEndPoint(0);
                }
                else
                {
                    cableUsePoint = cableTrayCurve.GetEndPoint(1);
                }
                XYZ conduitUsePoint = null;//取近处点
                if ((conduitCurve.GetEndPoint(0) - intersectionPoint).GetLength() > (conduitCurve.GetEndPoint(1) - intersectionPoint).GetLength())
                {
                    conduitUsePoint = conduitCurve.GetEndPoint(1);
                }
                else
                {
                    conduitUsePoint = conduitCurve.GetEndPoint(0);
                }
                var mainConnector = cableTray.GetConnectorByOrigin(cableUsePoint);
                var lateralConnector = conduit.GetConnectorByOrigin(conduitUsePoint);
                doc.Create.NewTeeFitting(mainConnector, mainConnector, lateralConnector);
                tran.Commit();
                return Result.Succeeded;
            }
            catch (Exception e)
            {
                if (tran.GetStatus() == TransactionStatus.Started)
                {
                    tran.RollBack();
                    tran.Dispose();
                }
                TaskDialog.Show("错误", e.StackTrace.ToString());
                //return Result.Cancelled;
            }
            return Result.Succeeded;
        }
    }

 

你可能感兴趣的:(Revit二次开发)