Hi,
I just got back to this issue and realized Jim's response wants me to open each link in memory to process. There can be numorous Revit links and feel this will take took long. I'm currently using a combination of Mustafa's and Jim's suggestions as well as my approach. (Mustafa mentioned: Room.IsPointInRoom and Jim's room filtering). This is what I have so far, it works in my test models but need to verify on an actual project RVT (you know how that is). Please let me know if you see a more streamlined approach:
private void ProccessStart(UIApplication m_app)
{
try
{
// Make sure no other RVTs are open in Revit
if (MultipleRVTsOpen(m_app) == true)
{
TaskDialog.Show("Process Stopped", "Please only have one file open when running this tool");
return;
}
// Iterate through each document
foreach (Document oDocument in m_app.Application.Documents)
{
// Only process links
if (oDocument.IsLinked)
{
// Create a room collection from rooms in the current link
RoomFilter oRoomFilter = new RoomFilter();
FilteredElementCollector oFilteredElemCol = new FilteredElementCollector(oDocument);
oFilteredElemCol.WherePasses(oRoomFilter).WhereElementIsNotElementType();
// Iterate through each room
foreach (Room oRoom in oFilteredElemCol)
{
// Only process placed rooms
if (RoomIsPlaced(oRoom) == false)
continue;
// Get all LightingFixtures in the current room
List<FamilyInstance> oFamilyInstances =
LightingFixtsFromRoom(oRoom, m_app.ActiveUIDocument.Document);
}
}
}
}
catch (Exception ex)
{
TaskDialog.Show("Error", ex.Message);
}
}
private bool MultipleRVTsOpen(UIApplication m_app)
{
int intCntr = 0;
try
{
// Iterate through each document
foreach (Document oDocument in m_app.Application.Documents)
{
// Skip linked RVTs and families
if (!oDocument.IsLinked && !oDocument.IsFamilyDocument)
{
intCntr++;
if (intCntr > 1)
return true;
}
}
return false;
}
catch
{
return true;
}
}
private bool RoomIsPlaced(Room oRoom)
{
try
{
// Make sure the room does not contain a location or area value
if (null != oRoom.Location && Math.Round(oRoom.Area) != 0.0)
return true;
else
return false;
}
catch
{
return false;
}
}
private List<FamilyInstance> LightingFixtsFromRoom(Room oRoom, Document oDocument)
{
List<FamilyInstance> oFamilyInstances = new List<FamilyInstance>();
try
{
// Create a LightingFixture/FamilyIntance collection that exist in the active document
FilteredElementCollector oFilteredElemCol = new FilteredElementCollector(oDocument)
.WhereElementIsNotElementType()
.WhereElementIsViewIndependent()
.OfCategory(BuiltInCategory.OST_LightingFixtures)
.OfClass(typeof(FamilyInstance));
// Iterate through each instance
foreach (FamilyInstance oFamilyInstance in oFilteredElemCol)
{
try
{
// Get the boudning box of the instance
BoundingBoxXYZ oBoundingBoxXYZ = oFamilyInstance.get_BoundingBox(null);
if (oBoundingBoxXYZ == null)
continue;
// Get the center point of the instance (except Z, want to make sure recessed lights are found)
XYZ oCenterPoint = new XYZ((oBoundingBoxXYZ.Min.X + oBoundingBoxXYZ.Max.X) / 2,
(oBoundingBoxXYZ.Min.Y + oBoundingBoxXYZ.Max.Y) / 2,
(oBoundingBoxXYZ.Min.Z));
// Determine if the point exists within the bounding box/room
if (oRoom.IsPointInRoom(oCenterPoint) == true)
TaskDialog.Show("Got lights", "Light family name: " +
oFamilyInstance.Symbol.FamilyName + ", Room name: " + oRoom.Name);
}
catch
{
// continue processing
}
}
return oFamilyInstances;
}
catch
{
return oFamilyInstances = new List<FamilyInstance>();
}
}