天天看點

Revit二次開發:由房間擷取房間的牆

之前用的方法是由房間邊界構成的Solid,計算與該Solid相交的Element,然後判斷是否為牆。相對來說這個方法比較通用,可以檢索出房間的樓闆、窗戶等各種構件。

SpatialElementBoundaryOptions se=new SpatialElementBoundaryOptions();
                se.SpatialElementBoundaryLocation = SpatialElementBoundaryLocation.Center;
                SpatialElementGeometryCalculator calculator =new  SpatialElementGeometryCalculator(document,se);
                Solid solid = calculator.CalculateSpatialElementGeometry(room)?.GetGeometry();
                var list = new FilteredElementCollector(document).WhereElementIsNotElementType().
                    WherePasses(new ElementIntersectsSolidFilter(solid)).ToList();

                foreach (var element in list)
                {
                    Wall wall=element as Wall;
                    if (wall!=null)
                    {
                        wallsOfRoom.Add(wall);
                    }
                }
           

看了Jeremy大神的部落格,才知道,如果隻是找牆的話,其實用BoundarySegment.ElementId,就可以直接得到構成這個邊界部分的元素(牆)。

IList<IList<BoundarySegment>> loops = 
                    room.GetBoundarySegments(new SpatialElementBoundaryOptions());
                foreach (IList<BoundarySegment> loop in loops)
                {
                    foreach (BoundarySegment segment in loop)
                    {
                        Wall wall =document.GetElement(segment.ElementId)  as Wall;
                        if (wall != null)
                        {
                            wallsOfRoom.Add(wall);
                        }
                    }
                }