Room Occupancy

Shifali asked a

question
on
reading and writing the room occupancy:

Question: We get the document from ExternalCommandData object’s Application and the project information in document.
We need to store the construction type of the current document or project file.
Moreover, we get the Room elements of the document but we need to store the occupancy type of each room for applying the International Building Code for compliance reports of the project.

We also need access to the BuildingConstruction and BuildingType in the document ProjectInformation.gbXMLSettings.

Answer: I started by looking at the room occupancy issue, and found that this is actually very simple and straightforward and has been covered in numerous previous posts dealing with the

exploration of element parameters
.
Still, since this is a very typical and recurring issue, I will happily revisit it.

I started up Revit and created a new model with a room or two.
Left clicking one of these, and selecting Element Properties… I see the parameter that I presume you are looking for listed as Occupancy under the Identity Data heading.
So that is where to find this data in the user interface.
I entered an arbitrary string value there in order to start exploring how to access the same data programmatically.

Next, I start up

RvtMgdDbg

(for 2010)
and use that to look at the room element parameters through the API via Add-Ins > RvtMgdDbg > Snoop Current Selection… > Parameters > Occupancy.
It shows the same string I just entered, so I seem to have found the required data.
Since I would like to access it language independently, I need to determine the built-in parameter enumeration value for this parameter, if one exists.
If I don’t care about that, I can use the language dependent parameter name “Occupancy” to identify the data.
The built-in parameter can be determined by using the Built-in Enums Snoop or Built-in Enums Map buttons in RvtMgdDbg, which show me that the built-in parameter I am looking for is ROOM_OCCUPANCY.

I can also look at the element parameters and see the associated built-in parameters using the Revit API introduction labs

built-in parameter checker
.

Both of these tools show me that the parameter is string valued and read-write, so there should be no problem modifying it.

I implemented a new Building Coder sample command CmdSetRoomOccupancy to demonstrate making use of this to read and write the parameter and do nothing else.
For experienced Revit developers, this is a rather trivial command, since all it does is read and write the value of a single element parameter.
Still, it should be useful to point people to in the future who raise this frequently asked question.
The command retrieves either the currently selected rooms or all rooms in the model if nothing has been preselected using my GetSelectedElementsOrAll utility method.
For each room, its occupancy parameter is read and incremented.
This is achieved using the BumpStringSuffix helper method.

BumpStringSuffix increments the numerical suffix of a given string.
If the string already ends in a sequence of digits representing a number, it returns a string with the number incremented by one.
Otherwise, it returns the original string with a suffix “1” appended.
In case the original string is null, “1” is returned:


static char[] _digits = null;
static string BumpStringSuffix( string s )
{
  if( null == s || 0 == s.Length )
  {
    return "1";
  }
  if( null == _digits )
  {
    _digits = new char[] {
      '0', '1', '2', '3', '4',
      '5', '6', '7', '8', '9'
    };
  }
  int n = s.Length;
  string t = s.TrimEnd( _digits );
  if( t.Length == n )
  {
    t += "1";
  }
  else
  {
    n = t.Length;
    n = int.Parse( s.Substring( n ) );
    ++n;
    t += n.ToString();
  }
  return t;
}

Here is the method BumpOccupancy which makes use of BumpStringSuffix to increment the room occupancy parameter on a given room element.
It reads the value of the element ROOM_OCCUPANCY parameter.
If it ends in a number, it increments that number, otherwise it appends “1”:


static void BumpOccupancy( Element e )
{
  Parameter p = e.get_Parameter(
    BuiltInParameter.ROOM_OCCUPANCY );
 
  if( null == p )
  {
    Debug.Print(
      "{0} has no room occupancy parameter.",
      Util.ElementDescription( e ) );
  }
  else
  {
    string occupancy = p.AsString();
 
    string newOccupancy = BumpStringSuffix(
      occupancy );
 
    p.Set( newOccupancy );
  }
}

Now we can apply the BumpOccupancy method to all or the currently selected rooms in the model.
Here is the entire code of our external command Execute method which does so:


public CmdResult Execute(
  ExternalCommandData commandData,
  ref string message,
  ElementSet elements )
{
  Application app = commandData.Application;
  Document doc = app.ActiveDocument;
 
  List<Element> rooms = new List<Element>();
  if( !Util.GetSelectedElementsOrAll(
    rooms, doc, typeof( Room ) ) )
  {
    Selection sel = doc.Selection;
    message = ( 0 < sel.Elements.Size )
      ? "Please select some room elements."
      : "No room elements found.";
    return CmdResult.Failed;
  }
  foreach( Room room in rooms )
  {
    BumpOccupancy( room );
  }
  return CmdResult.Succeeded;
}

Here is
version 1.1.0.54
of the complete Building Coder sample source code and Visual Studio solution including the new command.

So, I would hope that this amply answers the question on accessing and modifying the room occupancy, and also serves as a model for further simple parameter setting explorations.

Turning to look at the BuildingConstruction and BuildingType values in the ProjectInformation gbXMLSettings, I once again resort to RvtMgdDbg.
I select Add-Ins > RvtMgdDbg > Snoop Db… > ProjectInfo > Project Information 69280 > gbXML Settings … and crash.
Well, first a message was displayed saying that this is only available in Revit MEP.

Ok, I restarted in Revit MEP and navigated through the same path down to the gbXML settings, which I can enter and explore this time.
I see the building construction and building type properties.
The former is an object, the latter displays a value kOffice.

Actually, I also see en entry for the gbXMLParamElem in the root directory of the RvtMgdDbg database snoop:

Snoop gbXMLParamElem

The element id shows me that this is the same object as the one stored in the project information.

So obviously there is no problem accessing this data either.
gbXMLParamElem is one of the classes defined by the Revit API and can be accessed using the standard Revit

element filters
,
for instance.
The desired properties are both members of this class:

  • BuildingConstruction returns the Project Information Building Construction object.
  • BuildingType gets or sets the Project Information Building Type.

Comments

6 responses to “Room Occupancy”

  1. Hi, sir.
    Your blog is really useful for me. May I ask you a question, the default color is great, and I want to know how to switch the color to other. Thanks

  2. Dear Owen,
    Great to hear that the blog is useful to you.
    Did you see my answer to your previous question?
    http://thebuildingcoder.typepad.com/blog/2009/09/detail-lines.html?cid=6a00e553e1689788330120a5460951970b#comment-6a00e553e1689788330120a5460951970b
    Here is what I had to say then, and I am not much the wiser today:
    Regarding the colour settings, I do not know how to set line colour completely freely. In Revit, things are generally managed in a systematic and parametric manner, which ensures that the model remains well structured. In this case, you can modify model line colours by changing line style settings. I did the following to test this:
    – Create a couple of model lines
    – For each one of these, right click for the context menu and select Element Properties… > Line style and pick a different line style.
    – Right click somewhere in empty space in the current view to bring up the view context menu, and select View Properties… > Visibility/Graphics Overrides > Edit… > Model Categories > Visibility and search for the Lines entry. Click on the little plus sign to display the lines subcategories.
    – For each of the line styles you selected above, find the corresponding subcategory and change the colour displayed in its Projection/Surface Lines column.
    In addition to this, there are also two posts that deal with the colour of Revit elements:
    http://thebuildingcoder.typepad.com/blog/2009/03/more-questions.html
    http://thebuildingcoder.typepad.com/blog/2009/04/revit-api-cases-1.html
    I hope this helps.
    Cheers, Jeremy.

  3. Dear Jeremy,
    I went through above reply for Room Occupancy type and Construction. I got your solution for room occupancy. But for the construction, You have suggested to use Revit MEP. We have Revit Architecture 2010 only and we dont have Revit MEP installed. Moreover, our plug in is for Revit Architecture 2010 only. Can you please tell us how we can find it for Revit Architecture 2010 using Revit API
    Regards,
    Shifali

  4. Dear Shifali,
    I just tried accessing that data in RAC both through the project information and by directly reading the gbXMLParamElem element in RvtMgdDbg, and it crashes in both cases. I have not debugged RvtMgdbg to find out what exactly is causing the crash as described in
    http://thebuildingcoder.typepad.com/blog/2009/08/fixing-rvtmgddbg-for-mep-connectors.html
    Nor have I tried to access the element myself using a simple element filter. I suggest you do that yourself and let us know what you find out. Thank you!
    Cheers, Jeremy.

  5. This is impressive. I’ve been searching for tutorials like this for almost a week and i tend to not understand all of them except your post. It’s a good thing you made it in a step by step process. Thanks.

  6. I got your solution for room occupancy. But for the construction, You have suggested to use Revit MEP. We have Revit Architecture 2010 only and we dont have Revit MEP installed. Moreover, our plug in is for Revit Architecture 2010 only.

Leave a Reply to UGG AustraliaCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading