OnSave Event handling for multiple Add-Ins

OnSave Event handling for multiple Add-Ins

catot
Advocate Advocate
803 Views
8 Replies
Message 1 of 9

OnSave Event handling for multiple Add-Ins

catot
Advocate
Advocate

I'm having some issues with the Document OnSave Event when developing multiple Add-Ins.

 

At first I created an Add-In and implemented the OnSave event with success (Add-In_1).

But now, I'm developing a second Add-In to also implement the OnSave event in that as well (Add-In_2).

These are completely separate Add-Ins, but I've copied over for example the code handling the OnSave event.

 

When debugging Add-In_2 and stepping into the code as I save the document, I've discovered that this is happening:

  1. Add-In_1 trigger its OnSave event with kBefore eventTiming.
  2. Add-In_2 trigger its OnSave event with kBefore eventTiming.
  3. Inventor displays the Save Dialouge, I press OK to continue. (so far so good)
  4. Add-In_1 trigger its OnSave event with kBefore eventTiming again. (why?)
  5. Add-In_2 trigger its OnSave event with kBefore eventTiming again. (why?)
  6. Add-In_1 trigger its OnSave event with kAfter eventTiming.
  7. Add-In_2 trigger its OnSave event with kAfter eventTiming.
  8. Add-In_1 trigger its OnSave event with kAfter eventTiming again.
  9. Add-In_2 trigger its OnSave event with kAfter eventTiming again.

So, I'm looking for some advice and best practice of how to handle this and avoid the duplicate events firing, I'm clearly doing something wrong. Any feedback is appreciated, thanks.

0 Likes
Accepted solutions (1)
804 Views
8 Replies
Replies (8)
Message 2 of 9

bradeneuropeArthur
Mentor
Mentor

Hi,

 

Are you using DocumentEvents or ApplicationEvents "OnSave"?

 

Regards,

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 9

catot
Advocate
Advocate

I'm using ApplicationEvents. Maybe I should not?

0 Likes
Message 4 of 9

bradeneuropeArthur
Mentor
Mentor

Hi,

 

This should work:

Please check the following:

  • There definitely is a module or code that uses a document.save method when the file is saved.
    Try to find this piece.

After this we need to change this module to a more friendly piece of code.

 

Regards,

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 5 of 9

catot
Advocate
Advocate

Hi,

 

Thanks for looking into this.

 

I've been through the solutions for both Add-Ins, and I can not find any calls to Save the Inventor document apart from the save that the user performs.

 

I've posted some C# code below that is included in both Add-Ins, maybe something is off here?

 

private Inventor.ApplicationEvents m_appEvents;

// the code below is within the Activate() method in the StandardAddInServer class. m_appEvents = g_inventorApplication.ApplicationEvents; m_appEvents.OnSaveDocument += new ApplicationEventsSink_OnSaveDocumentEventHandler(m_appEvents_OnSaveDocument); //

private void m_appEvents_OnSaveDocument(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context,
out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventHandled;

if (BeforeOrAfter == EventTimingEnum.kBefore)
{
// Operation is performed
}
}

 

0 Likes
Message 6 of 9

bradeneuropeArthur
Mentor
Mentor

Hi,

 

You said you did not use any code with .save etc.

But are you using commandmanager commands like:

Dim CmdMan As ControlDefinition
Set CmdMan = ThisApplication.CommandManager.ControlDefinitions.Item("Command Name")

CmdMan.Execute
CmdMan.Execute2 (True)

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 7 of 9

catot
Advocate
Advocate

I do not use CommandManager for anything except adding buttons to the ribbon.

0 Likes
Message 8 of 9

bradeneuropeArthur
Mentor
Mentor

Hi,

 

What happens if you unload first the one add- in and than the other?

Still having the events twice?

 

Regards,

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

Message 9 of 9

catot
Advocate
Advocate
Accepted solution

I followed your suggestion now, and tested the Add-Ins one by one, and eventually found out what the problem was.

 

In the code block handling the save I had a check for document type, as I wanted to perform the operation on a drawing document only. The problem was that I was checking the active document type, and not in fact the document passed into the event handler. This became a problem as the add-in that was having this mistake was modifying the part belonging to the drawings as well, which in turn marked that part for save. So the duplicate events that followed was the save events for that part. 

 

It was a simple mistake, but this is what you learn from, right?

Thanks for your support and interest @bradeneuropeArthur