2

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!

JayW
  • 23
  • 3

3 Answers3

2

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)
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • Wow, that's really helpful, I'll try it out right away. Thanks! – JayW Jul 07 '17 at 13:30
  • One more question since I'm just getting into Python: if I'm using the Python node inside of Dynamo, and not building an addin, would I insert the "__uiControlledApplication__.DialogBoxShowing += on_dialog_open" code into the startup line of the Python node instead of the startup part of the addin? – JayW Jul 07 '17 at 13:33
  • @JayW i'm not very used to Dynamo, you may ask this one [on the forum](https://forum.dynamobim.com) to be sure. What you need is the reference to `Autodesk.Revit.UI.UIApplication` Class or `Autodesk.Revit.UI.UIControlledApplication`, the last is only reachable at startup – PRMoureu Jul 07 '17 at 16:01
1

To answer your first question. Try to read this tutorial from Pierre Moureu : https://github.com/PMoureu/samples-Python-RPS/tree/master/Tutorial-IUpdater. He his subscribing to a IUpdater.

Cyril Waechter
  • 517
  • 4
  • 16
1

(sorry not enough reputation to add this as comment to the response by PRMoureu...)

To expand on handling Dialogs a little...

Subscribing to DialogBoxShowing is hugely powerful, we have just rolled out a Dialog Suppressor to silence the ever-frustrating 'Would you like to join walls to the floor you just made' and 'Would you like to attach these walls to the roof'. It can also be used to see what errors users are commonly getting etc.

  • To investigate a Dialogs message text: event.Message
  • To reply 'Cancel' to the Dialog: event.OverrideResult(0)
  • To reply 'Yes' to the Dialog: event.OverrideResult(1)
  • To reply 'OK' to the Dialog: event.OverrideResult(6)
Callum
  • 578
  • 3
  • 8
  • now i wonder if i give the right numbers in fact, i cannot test it now, but maybe we should use `int(TaskDialogResult.CommandLink1)` – PRMoureu Jul 06 '17 at 22:00
  • 1
    you really deserve some +1 for this one, i messed up the numbers... updated now – PRMoureu Jul 07 '17 at 04:56