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);
}
}
只是找牆的話,其實用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);
}
}
}
-
碰撞檢測:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;//類Selection使用
namespace Collision
{
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Transaction trans = new Transaction(doc,"Excomm");
trans.Start();
Selection select = uiDoc.Selection;
Reference r = select.PickObject(ObjectType.Element, "選擇需要檢查的牆");
Element column = doc.GetElement(r);
FilteredElementCollector collect = new FilteredElementCollector(doc);
//ElementIntersectionFilter沖突檢查
ElementIntersectsElementFilter iFilter = new ElementIntersectsElementFilter(column,false);
collect.WherePasses(iFilter);
List<ElementId> excludes = new List<ElementId>();
excludes.Add(column.Id);
collect.Excluding(excludes);
List<ElementId> ids = new List<ElementId>();
select.SetElementIds(ids);
foreach(Element elem in collect)//遍歷每一個元素
{
ids.Add(elem.Id);//將elem的Id添加到List中
}
select.SetElementIds(ids);
trans.Commit();
return Result.Succeeded;
}
}
}篩選出和該元素相交的元素之BoundingBoxIntersectsFilter
//假設元素為ee
BoundingBoxXYZ box = ee.get_BoundingBox(doc.ActiveView);
//創建outline,通過boundingboxintersect過濾器
Outline myOutLn = new Outline(box.Min, box.Max);
BoundingBoxIntersectsFilter boxee = new BoundingBoxIntersectsFilter(myOutLn);
FilteredElementCollector collector = new FilteredElementCollector(doc);//過濾出相交元素,假設用到的是板類型
IList<Element> elements = collector.OfClass(typeof(Floor)).WherePasses(boxee).ToElements();