Subscribing to Revit API events (.Net event) in Iron Python

Good morning all,

Does anyone have experience in subscribing to Revit API events in Iron Python? I’m trying to subscribe to the Idling Event under UIApplication Class. Here is some documentation:

Iron Python - Accessing .NET events
Revit API - Idling Event

And here is my code. I’m pretty sure I’m close to the solution but can’t seem to get it figured out.

import sys
pyt_path = r'C:\Python27\Lib'
sys.path.append(pyt_path)
import clr

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference ('RevitAPIUI')
from Autodesk.Revit.UI import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
app = uiapp.Application


#Postable Command
command = PostableCommand.MergeSurfaces
merge = RevitCommandId.LookupPostableCommandId(command)

def Merge():
uiapp.PostCommand(merge)

def Idle(sender, IdlingEventsArgs):
print 'Idling'

#Post
TransactionManager.Instance.EnsureInTransaction(doc)
Merge()
TransactionManager.Instance.TransactionTaskDone()

#Subscribe Idling Event

uiapp.Idling += Idle

OUT = None

and here is the error I’m getting:

Thanks for any help in advance!

As far as I know you cannot subscribe to events from within Dynamo.

1 Like

Any particular reason why not?

I’m in middle of searching and there is definitely examples of people using iron python to subscribe to .net events. I’m just having trouble applying it to my case.

If I am not mistaken, part of the reason is that Dynamo itself is heavily hijacking the Idling event. Thoughts @Michael_Kirschner2?

2 Likes

Thanks for sharing John. I had not considered this, although, that would make sense. Do you have any references to this topic? That may end my obsession with getting this figured out.

I imagine it should be in the source code somewhere, this class seems to be what fires off when dynamo is launched.

Also, I could be wrong about the events (and hope I am), because they are very useful! However, I have found that if I need events that I am most likely needed a complete Revit addin. Because having reliance on a Dynamo script and events seems scary.

2 Likes

Well that is unfortunate, that eliminates a lot of possibilities within Dynamo. I also found some discussion about this topic in this thread.

Thank you for the help, I have been stuck on this for a few days so I’m glad to discover why I’ve been having trouble. Have a good day!

1 Like

Pretty easy to do it in pyRevit if you are looking for something in between a full blown C# adding and a dynamo script

http://www.revitapidocs.com/code/#Python_RegisterEventHandler-py

4 Likes

What is the difference between using pyRevit and writing a Macro in Python? I haven’t looked into it yet as I’ve only been using Dynamo and Python within dynamo.

You can subscribe to events from Dynamo.
But it’s a little tricky to unsubscribe from him.
You can do this through iUpdater.

2 Likes