I realize this thread is old but I'm having an issue with something similar.
I'm having the user select a room, and I'm trying to get the ceiling of said room. I originally tried a filtered element collector grabbing ceilings, and then checking to see if the center point of the ceiling exists in my room's bounding box. I had an issue with the iterator saying it was out of elements, so i'm presuming none of the elements passed the comparison. Here's the snippet.
FilteredElementCollector ceilings = new FilteredElementCollector(doc).OfClass(typeof(Ceiling)).OfCategory(BuiltInCategory.OST_Ceilings);
Element selectedCeiling = null;
if (ceilings.Count() != 0)
{
foreach (Ceiling f in ceilings)
{
XYZ ceilingCenter = GetElementCenter(f);
BoundingBoxXYZ roomBox = selectedRoom.get_BoundingBox(null);
if (roomBox.Max.X >= ceilingCenter.X && roomBox.Min.X <= ceilingCenter.X && roomBox.Max.Y >= ceilingCenter.Y && roomBox.Min.Y <= ceilingCenter.Y && roomBox.Max.Z >= ceilingCenter.Z && roomBox.Min.Z <= ceilingCenter.Z)
{
selectedCeiling = f as Element;
}
selectedCeiling = f;
XYZ ceilingCenter = GetElementCenter(f);
}
}From there I searched the forums and saw this thread recommending using a reference intersector to ray cast into the ceiling. So, I tried this method as posted above, but I keep getting null references. I'm not sure if my type is wrong in the ElementClassFilter, or I thought maybe the co ordinate system in Revit might be y up instead of z up, but nothing seems to work. Here is the second snippet.
XYZ roomCenter = GetElementCenter(selectedRoom);
XYZ rayDirection = new XYZ(0, 0, 1);
FilteredElementCollector collector = new FilteredElementCollector(doc);
Func<View3D, bool> isNotTemplate = v3 => !(v3.IsTemplate);
View3D View3D = collector.OfClass(typeof(View3D)).Cast<View3D>().First<View3D>(isNotTemplate);
ElementClassFilter ceilingFilter = new ElementClassFilter(typeof(Ceiling));
BoundingBoxXYZ roomBox = selectedRoom.get_BoundingBox(View3D);
ReferenceIntersector ceilingIntersector = new ReferenceIntersector(ceilingFilter, FindReferenceTarget.Element, View3D);
ReferenceWithContext referenceWithContext = ceilingIntersector.FindNearest(roomCenter, rayDirection);
Reference ceilingRef = referenceWithContext.GetReference();
Element selectedCeiling = doc.GetElement(ceilingRef) as Element;
Thanks for this,