View Full Version : Which room is my element in?
2008-02-26, 04:42 PM
I thought I saw somewhere that elements in Revit (say a desk for example) exposed the room they are located in via the API. How would I go about accessing that information?
I have gotten it to give me the category of the elements, and I noticed that "FamilyInstance" objects have a "Room" property, but when I try to pull out the anything about the room I keep getting an "Object Reference not set to an Instance of an Object" error. What am I doing wrong?
Here is my code:
If TypeOf elem Is Autodesk.Revit.Elements.FamilyInstance Then
If elem.Category.Name.ToString = "Plumbing Fixtures" Then
Dim e As Autodesk.Revit.Elements.FamilyInstance = elem
If Not e.Room Is Nothing Then
Catch ex As Exception
2008-02-26, 05:26 PM
Ok, I think I found the issue after trying to sift through the API Help. From the description of the familyinstance.room property:
"Returns the Room in which an instance is located AT THE LAST PHASE OF THE PROJECT."
Our default template has 3 phases, Existing, New Construction, and Project Completion. We don't really use project completion much, so we can just delete it. I will have to add a warning in there to make sure it's the last phase...
FYI for anyone else trying to do this...
2008-02-26, 11:19 PM
FYI - I can't recall how it works in VB.NET, but the C# version of the API has two options:
- If you just ask for the room, you get it based on the last phase. As you saw, this can be problematic - particularly if your Rooms exist only in the New Construction phase.
- There is another option, Room( phase ) where you specify a particular phase.
For our production apps, I usually wind up scanning the model and determining what phase(s) the Rooms are in, and passing that phase as input to the FamilyInstance.Room property.
2008-02-27, 06:18 PM
Ok, another question. How do I get the phase of the room? I tried casting the element as a room and getting the .PhaseCreated.Name property, but it doesn't seem to work. I can get it to tell me the name and number of the room, and I can verify that it's placed (by looking at my file), but the .PhaseCreated property comes back null. Where am I going wrong?
2008-02-27, 07:54 PM
hmm. ok, update on this, I realized that there is a built in parameter for ROOM_PHASE, ROOM_PHASE_ID, and ROOM_PHASE_NAME. I thought this was my answer, however I tried it and it didn't work. I try the phase name and i get a null string returned. For the other 2 I just get an error. This happens for all 3 rooms in my test project.
This doesn't seem like it should be that hard, what am I missing...
2008-02-28, 11:18 AM
I always use ROOM_PHASE, which is an ElementId type parameter, something like:
Parameter p = room.get_Parameter(BuiltInParameter.ROOM_PHASE);
if (p != null)
ElementId eid = p.AsElementId();
Element elemPhase = App.ActiveDocument.get_Element(ref eid);
2008-02-28, 10:55 PM
Ok, I was using that parameter, but I was not getting it the right way. After looking at your code I think I've got it. I have to get going so I can't do extensive testing, but I think it's working, so THANKS!
2008-03-03, 03:00 PM
Woo Hoo, it works! it's alive, ALIVE!!!!! ok, sorry, got a little carried away there. It's working now though, thanks for your help!
2008-03-08, 05:47 PM
So what was the final code?
2008-03-10, 03:30 PM
Sorry, here it is:
If TypeOf elem Is Autodesk.Revit.Elements.Room Then
If elem.Category.Name = "Rooms" Then
'Test for correct phase
Dim p As Parameter = elem.Parameter(Parameters.BuiltInParameter.ROOM_PHASE)
If Not p Is Nothing Then
Dim rID As Autodesk.Revit.ElementId = p.AsElementId
Dim rmPhase As Autodesk.Revit.Element = commandData.Application.ActiveDocument.Element(rID)
If rmPhase.Name = phs.Name Then
Hopefully that makes sense... Basically got the room phase parameter, stored it as an element ID, then got that element by it's id and compared it to a previously stored phase that the user chose (phs). After the else is other code for other stuff, but this is the section relevant to this thread...
Powered by vBulletin® Version 4.1.11 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.