API onDocumentChange not triggering

API onDocumentChange not triggering

gert-leonvanlier
Collaborator Collaborator
672 Views
5 Replies
Message 1 of 6

API onDocumentChange not triggering

gert-leonvanlier
Collaborator
Collaborator

I have the following code. When saving, closing or opening, the application events are triggered, but the change event does not. Does anyone have a clue?

 

public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
            {
                try
                {
                    // Initialize AddIn members.
                    Globals.invApp = addInSiteObject.Application;

                    Globals.invApp.ApplicationEvents.OnDocumentChange += onChange;
                    Globals.invApp.ApplicationEvents.OnSaveDocument += onSaving;
                    Globals.invApp.ApplicationEvents.OnCloseDocument += onClosing;
                    Globals.invApp.ApplicationEvents.OnOpenDocument += onOpen;

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unexpected failure in the activation of the add-in " + System.Environment.NewLine + System.Environment.NewLine + ex.Message);
                }

            }

            private bool isSaving = false;

            private void onChange(_Document DocumentObject, EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange, NameValueMap Context, out HandlingCodeEnum HandlingCode)
            {
                if (BeforeOrAfter == EventTimingEnum.kAfter)
                {
                    MessageBox.Show("Hello");
                }
                HandlingCode = HandlingCodeEnum.kEventNotHandled;
            }

            private void onOpen(_Document DocumentObject, string FullDocumentName, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
            {
                if (BeforeOrAfter == EventTimingEnum.kAfter)
                {
                    MessageBox.Show("Hello");
isSaving);
                }
                HandlingCode = HandlingCodeEnum.kEventNotHandled;
            }

            private void onClosing(_Document DocumentObject, string FullDocumentName, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
            {
                if (BeforeOrAfter == EventTimingEnum.kBefore)
                {
                    MessageBox.Show("Hello");
                }
                HandlingCode = HandlingCodeEnum.kEventNotHandled;
            }

            private void onSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
            {
                if (BeforeOrAfter == EventTimingEnum.kBefore)
                {
                    MessageBox.Show("Hello");
                }
                HandlingCode = HandlingCodeEnum.kEventNotHandled;
            }
0 Likes
Accepted solutions (1)
673 Views
5 Replies
Replies (5)
Message 2 of 6

bradeneuropeArthur
Mentor
Mentor

This is a DocumentEvent!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 6

gert-leonvanlier
Collaborator
Collaborator

But using the Event Watcher there is also OnDocumentChange in ApplicationEvents. It is also listed as an object of ApplicationEvents when I program.

 

But I will try DocumentEvents too.

 

EventWatcher_fFKYl5O7k5.pngdevenv_9JRcZcff61.png

0 Likes
Message 4 of 6

JelteDeJong
Mentor
Mentor

I have very similar code that does work.  The main difference is that in my code the event object (Globals.invApp.ApplicationEvents.OnDocumentChange) is in the same object as where the event handlers are created.  I know that the events objects are very sensitive for the garbage collector.  (Check this post

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 6

gert-leonvanlier
Collaborator
Collaborator

@JelteDeJong wrote:

I have very similar code that does work.  The main difference is that in my code the event object (Globals.invApp.ApplicationEvents.OnDocumentChange) is in the same object as where the event handlers are created.  I know that the events objects are very sensitive for the garbage collector.  (Check this post

 

 


@JelteDeJongI read the other post, but I don't really understand what it means. And then in particular this senctence: "When subscribing to events from .NET, you need to also hold on to a reference to the xxxEvents object (in this case, ApplicationEvents). Otherwise, the temporary reference you used to subscribe to the event gets garbage-collected."

0 Likes
Message 6 of 6

gert-leonvanlier
Collaborator
Collaborator
Accepted solution

I foudn the issue after I came across this post this morning. Ater I set Embed Interop Types to False and run the code, the onChange event was triggered.

 

alsMYPkSG8.png