Revit二次开发 风管连接

Revit机电核心功能风管连接,往往建模的时候基本都能用到的功能。首先先讲讲思路,
1.管道连接就是管道的Connector的连接,只要Connector的顺序对和布管系统配置设置好,就可以实现管道连接了。
2.判断风管管件的类型和风管是否在正确位置上,比如是中/底/顶对齐,还是中/左/右对齐,根据对齐方式选择正确的管件
3.风管四通有4个Connector要判断正确顺序,这个就要区分好(重点)
以上就是实现风管连接的思路,直接上代码。

 UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection sel = uidoc.Selection;
            Transaction trs = new Transaction(doc, "风管连接");
            string familyfile = FilePathData.FittingFamilyfilepath;//族放置的文件夹

            try
            {
                //选择第一个风管
                var reference0 = sel.PickObject(ObjectType.Element, new MEPFilter(), "选择第一个主管");
                var pipecurve0 = doc.GetElement(reference0) as MEPCurve;

                var neartpoint0 = Tools.Nearpoint(reference0.GlobalPoint, pipecurve0);
                var farpoint0 = Tools.Farpoint(reference0.GlobalPoint, pipecurve0);
                var dirline0 = Line.CreateBound(neartpoint0, farpoint0);

                var dir0 = dirline0.Direction.Normalize();
                var zdir = Line.CreateBound(neartpoint0, neartpoint0.PointZOffset(neartpoint0.Z + 1)).Direction.Normalize();
                var dotdir = dir0.CrossProduct(zdir);

                var connector0 = reference0.GlobalPoint.ThisNearConnector(pipecurve0);
                var farcon0 = farpoint0.ThisNearConnector(pipecurve0);
         

                //选择第二个风管
                var reference1 = sel.PickObject(ObjectType.Element, new MEPFilter(), "选择第二个主管");
                var pipecurve1 = doc.GetElement(reference1) as MEPCurve;

                var connector1 = reference1.GlobalPoint.ThisNearConnector(pipecurve1);
                var neartpoint1 = Tools.Nearpoint(reference1.GlobalPoint, pipecurve1);
                var farpoint1 = Tools.Farpoint(reference1.GlobalPoint, pipecurve1);
                var dirline1 = Line.CreateBound(neartpoint1, farpoint1).Direction.Normalize();


                //选择第三个风管
                var reference2 = sel.PickObject(ObjectType.Element, new MEPFilter(), "选择第一个支管");
                var pipecurve2 = doc.GetElement(reference2) as MEPCurve;
                var connector2 = reference2.GlobalPoint.ThisNearConnector(pipecurve2);

                var neartpoint2 = Tools.Nearpoint(reference2.GlobalPoint, pipecurve2);
                var farpoint2 = Tools.Farpoint(reference2.GlobalPoint, pipecurve2);
                var dirline2 = Line.CreateBound(neartpoint2, farpoint2).Direction.Normalize();
                var dirline2Z = Line.CreateBound(neartpoint2, neartpoint2.PointZOffset(neartpoint2.Z + 1)).Direction.Normalize();
                var dirline2cross = dirline2.CrossProduct(dirline2Z);

                //选择第四个风管
                var reference3 = sel.PickObject(ObjectType.Element, new MEPFilter(), "选择第二个支管");
                var pipecurve3 = doc.GetElement(reference3) as MEPCurve;
                var connector3 = reference3.GlobalPoint.ThisNearConnector(pipecurve3);

                bool flagdir = dir0.IsSameDirection(dirline2cross);
                string familynameside = "矩形圆角变径四通-水平中-竖直中";

                //如果项目没有族则加载族
                var sybolfilter = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_DuctFitting).OfClass(typeof(FamilySymbol)).ToList();
                Tools.SearchFamilySybolLoadFamily(doc, familyfile, familynameside, sybolfilter);
                trs.Start();
                //设置布管系统配置
                RoutingPreferenceManager Rpm = (reference0.GetElement(doc) as Duct).DuctType.RoutingPreferenceManager;
                FilteredElementCollector collector = new FilteredElementCollector(doc);

                if (familynameside != null)
                {
                    var familysymbol = collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_DuctFitting).ToList().FirstOrDefault(x => (x as FamilySymbol).FamilyName.Contains(familynameside));
                    var familysymbolID = (familysymbol as FamilySymbol).Id;

                    //设置类型
                    RoutingPreferenceRule routinPFrule = new RoutingPreferenceRule(familysymbolID, familynameside);
                    Rpm.AddRule(RoutingPreferenceRuleGroupType.Crosses, routinPFrule, 0);
                }
                else
                {
                    MessageBox.Show("找不到族" + familynameside);
                    return Result.Cancelled;
                }
                //MessageBox.Show(familynameside.ToString());

                //创建管件
                FamilyInstance ft = null;

                if (flagdir)
                {
                    ft = doc.Create.NewCrossFitting(connector0, connector1, connector2, connector3);
                }
                else
                {
                    ft = doc.Create.NewCrossFitting(connector0, connector1, connector3, connector2);
                }
              
                Rpm.RemoveRule(RoutingPreferenceRuleGroupType.Crosses, 0);

                trs.Commit();
            }

            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                if (trs.GetStatus() == TransactionStatus.Started)
                {
                    trs.RollBack();
                }
            }
            return Result.Succeeded;

Revit二次开发 风管连接_第1张图片
一位Bin工程人员的二开学习小小成果,继续努力,后面有干货继续分享。

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