Document Closes Before Addin Can Respond to DocumentClosing Event

Document Closes Before Addin Can Respond to DocumentClosing Event

TheRealChrisHildebran
Advocate Advocate
791 Views
4 Replies
Message 1 of 5

Document Closes Before Addin Can Respond to DocumentClosing Event

TheRealChrisHildebran
Advocate
Advocate

We want to gather some data and store it in an external database. We are initiating these methods in the DocumentClosing Event Handler.

The problem is that some Documents close before methods are called to gather and save data from the Document.

This behavior seems to happen with small documents used for testing. For large documents, it seems the code has time to execute before the Document is disposed of. Not sure what the tipping point is.

I've thought about initiating a Transaction first thing after the event fires, but this seems wrong, hacky and is not permitted.

Does anyone know how to stall, postpone, or otherwise ask Revit to wait?

0 Likes
792 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

Have you tried calling DocumentClosingEventArgs.Cancel to then do your thing and after that close the document?

 

You should check 'Cancellable' RevitAPI.chm states that this event often can be cancelled unless it is triggered by application closing. You can't make changes to document but should be able to read from it during.

 

You could also potentially create command binding on 'close' command (which would probably be cleaner), I've not checked that this binding exists however.

 

 

Message 3 of 5

TheRealChrisHildebran
Advocate
Advocate

Well i havent chased it all the way down but it looks promising! 

 

Found the Command id in the Keyboard SHortcuts .xml export. 

 

Debugging show a valid object and Revit didnt oppose.

 

Next step is to put the event handler to use with what im hoping to implement.

 

var commandBinding = this.UIApp.CreateAddInCommandBinding(RevitCommandId.LookupCommandId("ID_REVIT_FILE_CLOSE"));

			commandBinding.BeforeExecuted += new EventHandler<BeforeExecutedEventArgs>(this.EventHandler);

2021-02-10_14-27-37.png

Thanks for sharing! 

0 Likes
Message 4 of 5

TheRealChrisHildebran
Advocate
Advocate

Using Command Binding would work if the user solely used the Close button in the Revit Ribbon. The command is not triggered using the Views tab "X" close button.

0 Likes
Message 5 of 5

RPTHOMAS108
Mentor
Mentor

@TheRealChrisHildebran  that is a shame, what news on cancelling the event, doing something, then closing with API?

0 Likes