Message 1 of 11
Getting neighboring curtain panel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Given a certain Panel element within a Wall element, how may I retrieve the adjacent Panel in a particular direction? The code below works but only when the adjacent BoundingBoxes overlap the original BoundingBox, which is not guaranteed. It feels like the answer should implement the Wall.CurtainGrid.GetPanel(uGridLineId, vGridLineId) method but I don't know a quick way of retrieving that the intersecting CurtainGridLine elements when simply provided a Panel element.
public static Panel GetAdjacentPanel(this Panel panel, AdjacentDirection direction)
{
Panel result = null;
if (panel.Host is Wall wall && wall.CurtainGrid != null)
{
// get the center of the panel's bounding box
var panelBoundingBox = panel.get_BoundingBox(null);
var panelCenter = (panelBoundingBox.Max + panelBoundingBox.Min) / 2;
// determine the top of the wall
var wallBoundingBox = wall.get_BoundingBox(null);
var zMax = direction == AdjacentDirection.Above ? wallBoundingBox.Max.Z : wallBoundingBox.Min.Z;
var collector = new FilteredElementCollector(panel.Document);
// filter by a bounding box intersection
var outline = new Outline(panelCenter, new XYZ(panelCenter.X, panelCenter.Y, zMax));
var filter = new BoundingBoxIntersectsFilter(outline);
collector.WherePasses(filter);
// typical quick filters for curtain panels
var categoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_CurtainWallPanels);
collector.WherePasses(categoryFilter);
collector.OfClass(typeof(FamilyInstance));
// select the closest panel that isn't the original panel
result = collector
.Where(e => e.Id != panel.Id)
.OrderBy(e =>
{
var bb = e.get_BoundingBox(null);
var ctr = (bb.Max + bb.Min) / 2;
return ctr.DistanceTo(panelCenter);
})
.FirstOrDefault() as Panel;
}
return result;
}