PDA

View Full Version : Which room is my element in?



sfaust
2008-02-26, 05: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
MsgBox(elem.Category.Name.ToString)
Try
If elem.Category.Name.ToString = "Plumbing Fixtures" Then
Dim e As Autodesk.Revit.Elements.FamilyInstance = elem
MsgBox(e.Room.Name.ToString)
If Not e.Room Is Nothing Then
MsgBox("found one")
elemset.Add(elem)
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If

sfaust
2008-02-26, 06: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...

mmason.65315
2008-02-27, 12:19 AM
Steve,

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.

Best Regards,
Matt

sfaust
2008-02-27, 07: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?

sfaust
2008-02-27, 08: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...

mmason.65315
2008-02-28, 12:18 PM
Steve,

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);
}


-Matt

sfaust
2008-02-28, 11: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!

sfaust
2008-03-03, 04: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!

Brockster
2008-03-08, 06:47 PM
So what was the final code?

Brock

sfaust
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
rmset.Add(elem)
End If
End If
End If
Else

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...