C#Revit二次开发之-一键切换构件连接顺序 SwitchJoinOrder

  在Revit建筑结构模型中正确的梁板柱连接关系应该是柱切梁,梁切板,但实际建模过程中经常会碰到梁板柱连接关系混乱的情况,当模型较大时,将梁板柱连接顺序调整至正确的关系需要花费大量人工。因此使用需要使用一些开发手段解决此类问题。

以下是解决此问题的相关代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace SwitchStructureConnection
{
    [Transaction(TransactionMode.Manual)]
    public class ConnectionGeometry : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uIDocument = commandData.Application.ActiveUIDocument;
            Document doc = uIDocument.Document;
            FilteredElementCollector list_column = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralColumns).OfClass(typeof(FamilyInstance));
            FilteredElementCollector list_beam = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralFraming).OfClass(typeof(FamilyInstance));
            Transaction transaction = new Transaction(doc, "连接几何关系");
            transaction.Start();
            foreach (Element column in list_column)
            {
                List<Element> column_box_eles = Get_Boundingbox_eles(doc, column, 1.01);
                //TaskDialog.Show("柱子", column_box_eles.Count.ToString());
                foreach (Element ele in column_box_eles)
                {
                    if(ele.Category.Name =="结构框架"|| ele.Category.Name == "楼板")
                    {
                        JudgeConnection(doc, column, ele);
                    }
                    
                }
            }
            foreach (Element beam in list_beam)
            {
                List<Element> beam_box_eles = Get_Boundingbox_eles(doc, beam, 1.01);
                //TaskDialog.Show("梁", beam_box_eles.Count.ToString());
                foreach (Element ele in beam_box_eles)
                {
                    if (ele.Category.Name == "楼板")
                    {
                        JudgeConnection(doc, beam, ele);
                    }
                }
            }
            transaction.Commit();
            return Result.Succeeded;
        }

        private static List<Element> Get_Boundingbox_eles(Document doc, Element element,double offset)
        {
            //获取元素boundingbox相交的元素
            List<Element> eles_list = new List<Element>();
            XYZ element_max_boundingBox = element.get_BoundingBox(doc.ActiveView).Max;
            XYZ element_min_boundingBox = element.get_BoundingBox(doc.ActiveView).Min;
            Outline element_Outline = new Outline(element_min_boundingBox, element_max_boundingBox);
            //element_Outline.Scale(offset);
            FilteredElementCollector element_collector = new FilteredElementCollector(doc);
            ElementClassFilter elementClassFilter_beam = new ElementClassFilter(typeof(FamilyInstance));
            ElementClassFilter elementClassFilter_floor = new ElementClassFilter(typeof(Floor));
            LogicalOrFilter logicalOr = new LogicalOrFilter(elementClassFilter_beam,elementClassFilter_floor);
            element_collector.WherePasses(logicalOr).WherePasses(new BoundingBoxIntersectsFilter(element_Outline,offset));
            foreach (Element near_ele in element_collector)
            {
                eles_list.Add(near_ele as Element);
            }
            return eles_list;
        }

        private static void JudgeConnection(Document doc, Element ele1st, Element ele2st)
        {
            try
            {
                //尝试连接几何
                JoinGeometryUtils.JoinGeometry(doc, ele1st, ele2st);
            }
            catch
            {
                Boolean ifJoined = JoinGeometryUtils.AreElementsJoined(doc, ele1st, ele2st);
                if (ifJoined)
                {
                    //判断连接关系是否正确,若不正确切换连接关系
                    Boolean if1stCut2st = JoinGeometryUtils.IsCuttingElementInJoin(doc, ele1st, ele2st);
                    if (if1stCut2st != true)
                    {
                        try
                        {
                            JoinGeometryUtils.SwitchJoinOrder(doc, ele2st, ele1st);
                        }
                        catch
                        {
                            //跳过----小帅帅呆了
                        }
                    }
                }
            }
        }
    }
}

 

posted @ 2020-11-07 21:18  帅某人是也  阅读(528)  评论(0编辑  收藏  举报