Macro to quickly fix the To/From Room parameter for doors

Steve, the wizard of revitoped.blogspot.com, suggested:

It would be cool to use the API to allow a user to pick a door and room to resolve the to/from settings. A user was lamenting the labor hassle of fixing to/from settings because people are mirroring and copying doors which doesn’t necessarily inherit the correct room ownership.

This macro works as follows

  • Select a door or door tag
  • Select the To Room (either the Room or the Room Tag)
  • Repeat as desired, press the ESC key when done
public void FlipRoom()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    FamilyInstance door = null;
    string doorMark = "";
    while (true) // create an infinite loop that will be ended by the "return" statement in the catch block
    {
        try
        {
            Element element = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, "Select a door or door tag"));

            if (element is FamilyInstance) // if door is selected
                door = element as FamilyInstance;
            else if (element is IndependentTag) // if door tag is selected
            {
                IndependentTag tag = element as IndependentTag;
                door = doc.GetElement(tag.TaggedLocalElementId) as FamilyInstance;
            }
            doorMark = door.get_Parameter("Mark").AsString();
        }
        catch
        {
            return; // end the command when the user presses ESC when prompted for a door
        }

        // after the user has selected a door, prompt for selection of a room
        Element e = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, "Select To Room for door " + doorMark));

        // Get the room element from the selected object
        Room room = null;
        if (e is RoomTag)
        {
            RoomTag rt = e as RoomTag;
            room = rt.Room;
        }
        else if (e is Room)
        {
            room = e as Room;
        }

        if (room != null && door.ToRoom.Id.IntegerValue != room.Id.IntegerValue) // if the selected room is not already the To Room
        {
            using (Transaction t = new Transaction(doc,"Door " + doorMark + " to room = " + room.Number))
            {
                t.Start();
                door.FlipFromToRoom(); // Flip the settings of "From Room" and "To Room" for the door 
                t.Commit();
            }
        }
    }
}
About these ads

16 thoughts on “Macro to quickly fix the To/From Room parameter for doors

  1. Great idea for a tool! We started a project in our firm to determine what add-ins (Revit, Autocad, Rhino/GH, ect) we could develop that would help with user productivity. This idea did not come up yet but I see it’s value.

    Thanks,
    Dan

  2. Hi Harry, I tried to copy/paste this code and I got this error when I “Build” the macro, any idea what is happening?

    Error 1 The best overloaded method match for ‘Autodesk.Revit.DB.Document.GetElement(Autodesk.Revit.DB.Reference)’ has some invalid arguments C:\ProgramData\Autodesk\Revit\Macros\2012\Architecture\VstaMacros\AppHookup\TagDoorToRM\Source\TagDoorToRM\ThisApplication.cs 56 32 TagDoorToRM
    Error 2 Argument ’1′: cannot convert from ‘Autodesk.Revit.DB.ElementId’ to ‘Autodesk.Revit.DB.Reference’ C:\ProgramData\Autodesk\Revit\Macros\2012\Architecture\VstaMacros\AppHookup\TagDoorToRM\Source\TagDoorToRM\ThisApplication.cs 56 47 TagDoorToRM

  3. Hi’ Harry,

    When I try to build your solution, I get these errors:

    ‘RoomTag’ does not contain a definition for ‘Room’ and no extension method ‘Room’ accepting a first argument of type ‘RoomTag’ could be found (are you missing a using directive or an assembly reference?)
    ‘RoomTag’ is inaccessible due to its protection level
    The as operator must be used with a reference type or nullable type (‘RoomTag’ is a non-nullable value type)
    The type or namespace name ‘Room’ could not be found (are you missing a using directive or an assembly reference?)

    I’ve put using Autodesk.Revit.UI.Selection; in my code as you explained in earlier post – but still no luck.
    I’ve tried to change the Target Framework as someone metioned on stackoverflow.com – but still no luck.

    I’m in Revit 2013.

    Any help would be much appreciated.
    Thanks!

    /Martin

  4. Hi Harry,

    Is there a way to make Revit recognize rooms through a link? What i mean is that when I have all of the rooms in one revit model the from/to parameter works just fine. However, when I try to extract information from doors that are in linked models (no rooms are located in linked models) I am getting nothing. Is it possible to make doors recognize their relationships to rooms in linked models?

    Konrad

      • Host file contains rooms. Linked file contains doors. I would like to know if there is a way to make those doors display To/From room properties of rooms from host file. Right now when I am queering door information through a link I am getting that the parameter to/from room is null/none. Ideas? Thank you!

        • FamilyInstance.FromRoom and FamilyInstance.ToRoom are read-only, so you won’t be able to use the API to set their values. If it was helpful to put this data into some other parameter, it could be possible to do some geometry investigation to figure this out.

          • The only way I found is to ‘Bind the link’ (select the link and press ‘Bind Link’ in the menu).
            From that point all rooms are set op correctly on the elements (doors, windows, …)

            Only drawback, this is not a great solution because ofcourse you lose all the advantages of using a link as it is converted to a group.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s