Search code examples
revit-apirevitpythonshellpyrevit

How to automatically handle DialogBoxShowing event in Python(Revit Dynamo)?


How do I subscribe to Revit events in Python (Dynamo)?

Specifically DialogBoxShowing so I can see if it is "Export with Temporary Hide/Isolate" warning and select "Leave the Temporary Isolate Mode on and Export"?

It is done in C# here:

http://thebuildingcoder.typepad.com/blog/2013/03/export-wall-parts-individually-to-dxf.html

See sub-heading: Handling and Dismissing a Warning Message

Thanks!


Solution

  • To make it more simple than in the tutorial :

    Inside Revit, with RevitPythonShell, the event subscribing part can be very easy.

    An event handler is just a callable with two arguments : senderand event. Then the event, or the sender gives parameters to play with, DialogId and OverrideResultin our case.

    To keep the Building Coder example, let's go with :

    def on_dialog_open(sender, event):
        try:
            if event.DialogId == 'TaskDialog_Really_Print_Or_Export_Temp_View_Modes':
                event.OverrideResult(1002) 
                # 1001 call TaskDialogResult.CommandLink1
                # 1002 call TaskDialogResult.CommandLink2
                # int(TaskDialogResult.CommandLink2) to check the result
        except Exception as e:
            pass #print(e) # uncomment this to debug 
    

    You only need to plug this function to an event with the following syntax :

    __uiControlledApplication__.DialogBoxShowing += on_dialog_open

    This can be done in the startup file of RevitPythonShell :

    C:\Users\USERNAME\AppData\Roaming\RevitPythonShell2017\startup.py

    (Or in the startup part of your addin)

    The better way is to unregister the handler if you don't need it anymore, i.e. when Revit is closing (check the tutorial for more details) :

    __uiControlledApplication__.DialogBoxShowing -= on_dialog_open


    If you want to try this within the console, you can use :

    def on_dialog_open(sender, event):
        # [...]
    
    __revit__.DialogBoxShowing += on_dialog_open 
    

    And after you try an export:

    __revit__.DialogBoxShowing -= on_dialog_open


    EDIT : shortcuts for result commands (thanks Callum !)

    ('Cancel', 2)
    ('Close', 8)
    ('CommandLink1', 1001)
    ('CommandLink2', 1002)
    ('CommandLink3', 1003)
    ('CommandLink4', 1004)
    ('No', 7)
    ('None', 0)
    ('Ok', 1)
    ('Retry', 4)
    ('Yes', 6)