Calling revit events API out of context error

i will start by saying i read every Jeremy Tammik post i found with no real clarity. i understand how to do it for an external command.

I have a node model i am writing, i need to subscribe to documentclosing event but it throws the API out of context bs. i cannot get it to work in my node model in C#.

        > var curUiApp = DocumentManager.Instance.CurrentUIApplication;
        var curApp = curUiApp.Application;
        currentDoc = curApp.OpenDocumentFile(file);
        currentDoc.DocumentClosing += CurrentDoc_DocumentClosing; // <---throws API out of context error here

Events are a bit difficult in Dynamo as it’s severely hijacking the idling event. Further to the difficulty here is working with background opened documents. (Dynamo pretty much only wants to work on the current file)

However, I have been able to use events in the past by force closing Dynamo’s open transaction.

The code is something along the lines of:

TransactionManager.Instance.ForceCloseTransaction()

And would go right before your event subscription I believe.

—-
If that’s still not working another option would be to try to force Dynamo to handle that new document and put itself in valid context with this https://github.com/DynamoDS/DynamoRevit/blob/feee34e79ec10fe5d3e8694fe20a574cff7f686c/src/Libraries/RevitServices/Persistence/DocumentManager.cs#L112

2 Likes

thanks for the reply, i did give the simple solution a shot, but that did not work, but would have been amazing if it did. thanks for the other link, i tried to implement something like that, the issue is that i can open the doc and trigger set the event in an api accessible manner the issue is everything that has to happen when the event fires has to do with the dynamo node. i need to figure out how to subscribe to the event from within the node model. maybe i have to use it the revit event to just trigger another event that i can subscribe too. idk