Boost Your BIM – making Revit even better

February 10, 2013

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

Filed under: Rooms — Harry Mattison @ 10:24 am

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

10 Comments »

  1. [...] experience might at least improve the situation for him. I wrote to Harry and a day or so later we’ve got this to consider. [...]

    Pingback by Change Door From and Door To | BIM i praksis — February 12, 2013 @ 3:43 am

  2. [...] experience might at least improve the situation for him. I wrote to Harry and a day or so later we’ve got this to consider. [...]

    Pingback by Change Door From and Door To | Revit Architecture India — February 12, 2013 @ 12:58 pm

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

    Comment by Dan Tartaglia — February 14, 2013 @ 9:20 am

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

    Comment by Jeff — March 12, 2013 @ 1:53 pm

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

    Comment by Martin Romby — January 29, 2014 @ 10:32 am


RSS feed for comments on this post. TrackBack URI

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

The WordPress Classic Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 972 other followers