Revit二次開發知識分享(九)判斷牆與牆是否相連

2020-10-03 12:01:45

在運用的時候,會遇到想找收集相連結的牆,用API提供的方法的話,總是會出錯。會漏掉一些自己需要的牆,因此,下面封裝了一個可以計算牆的是否連線的方法,先上程式碼後講解Just Do It!!!
在這裡插入圖片描述

        public bool IsConnected(Wall judgeWall, Wall proWall)
        {
            bool result = false;
            Line judgeLocationLine = ((judgeWall.Location as LocationCurve).Curve as Line).SetZ();
            Line proLocationLine = ((proWall.Location as LocationCurve).Curve as Line).SetZ();
            //只對平行的處理
            if (!judgeLocationLine.Direction.IsParallel(proLocationLine.Direction)) return result;
            var judgeGeo = judgeWall.get_Geometry(new Options());
            Face jugeDownFace = GeoEx.GetGeoEleDownFace(judgeGeo);
            var proGeo = proWall.get_Geometry(new Options());
            Face proDownFace = GeoEx.GetGeoEleDownFace(proGeo);
            List<Solid> solidList = new List<Solid>();
            //收集幾何體
            double judgeSolidSurfaceArea = 0;
            double prosolidSurfaceArea = 0;
            List<Solid> judgeSolids = GeoEx.GetSolids(judgeGeo);
            List<Solid> proSolids = GeoEx.GetSolids(proGeo);
            foreach (var judgeSolid in judgeSolids)
            {
                judgeSolidSurfaceArea += judgeSolid.SurfaceArea;
                solidList.Add(judgeSolid);
            }
            foreach (var proSolid in proSolids)
            {
                prosolidSurfaceArea += proSolid.SurfaceArea;
                solidList.Add(proSolid);
            }
            Solid fuseSolid = SolidBatchBoolean(solidList,BooleanOperationsType.Union);
            if (judgeSolidSurfaceArea + prosolidSurfaceArea - fuseSolid.SurfaceArea > 0)
            {
                result = true;
            }
            return result;
        }
 public Solid SolidBatchBoolean(List<Solid> solids, BooleanOperationsType booleanOperationsType)
        {
            Solid firstSolid = solids[0];
            solids.RemoveAt(0);
            //對所有的幾何體進行融合
            foreach (var oneSoild in solids)
            {
                try
                {
                    firstSolid = GeoEx.SolidBooleanOperation(firstSolid, oneSoild, booleanOperationsType);
                }
                catch
                {

                }

            }
            return firstSolid;
        }

案例中是隻對當前狀況下
思路:
取兩個牆的Solid,進行布林運算的並集運算,然後計算新的solid的表面積,最後對錶面積進行數值上的比較。如果表面積大於兩個solid的話說明是相連線的。
這次分享就這些了,關注我,日常更新小技巧