Cancel OnSaveDocumentEvent

Cancel OnSaveDocumentEvent

psaarloos
Collaborator Collaborator
891 Views
7 Replies
Message 1 of 8

Cancel OnSaveDocumentEvent

psaarloos
Collaborator
Collaborator

Hi,

 

I ran into something odd when developing Inventor add-ins. In the OnSaveDocumentEvent, during the 'beforeOrAfter = kBefore', I cancel the event by returning 'handlingCode = HandlingCodeEnum.kEventCanceled'. This seems to work correctly. The next time the OnSaveDocumentEvent in the add-in is called, the 'beforeOrAfter' property has a value 'kAbort' and the file is not saved to disk. So far so good....

 

Now I have another add-in. This add-in is also listening to ApplicationEvents (OnSaveDocumentEvent). When a file is being saved, the OnSaveDocumentEvent is being called in both add-ins, but the second add-in doesn't 'know' the event was cancelled in the first add-in.

 

Does anybody know how to solve this issue?

 

Regards,

Pim

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
892 Views
7 Replies
Replies (7)
Message 2 of 8

JelteDeJong
Mentor
Mentor

Just some thoughts.

Both of the addins can be the first to get the event trigger there for i think you will need some common class/function that is called by both addins. Then if that function gets called by both addins it can activate its own event. both addin should be lissening to that event. Only when the new event is triggerd then the addins should proceed as if the OnSaveDocumentEvent was triggerd.

Creating this common class/function is a bit tricky. the best i could think of is using 1 of the addins as master and expose the new class/function through the function Automation.  (the function that each addin needs to implement by the "ApplicationAddInServer" interface.)

 

The slave addin could have function something like this.

 

private void onSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
  Inventor.ApplicationAddIns.ItemById(MasterAddinID);
  dynamic automation = iLogicAddin.Automation;
  SaveDocumentEvents events = (SaveDocumentEvents)automation;
  events.OnSaveDocumentEvent += doStuffOnSlaveAddin()
  if (......)
  {
     events.OnSaveDocumentEventWasTriggerd(Tactic.Proceed);
  } else {
     events.OnSaveDocumentEventWasTriggerd(Tactic.Cancel);
  }
}

 

(this code is not tested and not optimised at all but i hope it is good enough to make my point)

Also the Master addin would call the "SaveDocumentEvents.OnSaveDocumentEventWasTriggerd(...)" in more or less the same way.

The "SaveDocumentEvents"-class should vigger out that it got a call from each addin that is listening. Then it should send out a new event telling each addon to do something (or not)

 

Let me know if this would work for you.

 

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 3 of 8

psaarloos
Collaborator
Collaborator

Hi Jelte,

 

Thanks a lot for your suggestion. The case I described actually occurs in a combination were a customer is using an Inventor add-in, developed and sold by our company together with their own add-in for some specific customization... That makes it impossible for both add-ins to call a general function.

 

I would expect that as soon as the HandlingCode of an event is returned as cancelled, calling other event handlers would be aborted. Any other ideas? Can I throw a certain exception for example?

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 4 of 8

JelteDeJong
Mentor
Mentor

i assume that you checked that the custom addon is also triggerd on the before timing event. I did have some ideas but when i recreated the situation all my ideas failed. except for your own sugestion. throwing an exeption. see code below.

Dim docEvents As DocumentEvents
    Private appEvents As ApplicationEvents

    Sub Main()
        Dim doc As Document = ThisDoc.Document
        appEvents = ThisApplication.ApplicationEvents
        AddHandler appEvents.OnSaveDocument, AddressOf OnSaveEventCadacAddin
        AddHandler appEvents.OnSaveDocument, AddressOf OnSaveEventCustomAddin
        While True
            Thread.Sleep(500)
        End While
    End Sub

    Private Sub OnSaveEventCustomAddin(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
        MsgBox("Custom save action: " & BeforeOrAfter.ToString())
    End Sub

    Private Sub OnSaveEventCadacAddin(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
        HandlingCode = HandlingCodeEnum.kEventCanceled
        MsgBox("Cadac save action: " & BeforeOrAfter.ToString())
        Throw New Exception()
    End Sub

This will stop the custom event from happening. But will only work if the Cadac event/addon is registered before the Custom event/addon is registered. To test this with addons you could try the following.

Manually unload the custom addon (probaly you need to restart Inventor) then load your addon first. After that reload the custom addon.

Now you can try to throw a exception in your save event function. If that stops the custom addon then you have a starting point. In that case you could always unload the custom addon when inventor closes and make your Cadac addon load the custom addon.

Any way if you manage to get it to work this way. I dont think it would be very stable. (for eaxample what happens if inventor crashes and the custom addon is not unloaded and is loaded before your addon on the next startup.) therefore this is far from a great solution.

 

If this is not good enough for you (i would not blame you) then maybe it is possible to undo the things that the custom addon does. For example

  • if the custom addon write some values to the iProperties you could save the original values on the userInputEvents.OnActivateCommand (AppFileSaveCmd, ..... ) and writ them back on the cancel event.
  • if some model changes are made in 1 transaction you could try to run the undo command ThisApplication.TransactionManager.UndoTransaction()

 

 

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 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@psaarloos,

 

Please provide non confidential source code (2 addins) to reproduce the behavior.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 6 of 8

psaarloos
Collaborator
Collaborator

Hi Chandra,

 

I've attached a Visual Studio solution to illustrate the issue. In the solution there are 2 add-ins. InventorAddIn1 and InventorAddIn2. Please build the project and start debugging.

 

1) Start new Part document

2) Save Part document

3) The OnSaveDocument event of InventorAddIn2 will get triggered first. Press the 'Cancel' button in the form that appears to mimic cancelling the event.

4) The OnSaveDocument event of InventorAddIn1 will then get triggered (beforeOrAfter = kBefore). This add-in will also show a form.

5) Press the 'Ok' button to run the code in the beforeOrAfter=kBefore statement. In this part of the code a custom property will be created in the document if it doesn't exist yet.

6) You'll will see the OnSaveDocument event in InventorAddIn2 will be called with beforeOrAfter=kAfter, while it was cancelled. It should be kAbort.

 

After quite some testing it turns out that if you comment the part of the code in the OnSaveDocument event of InventorAddIn1, where the custom property is being created, the event will be aborted correctly. Please let me know if you can reproduce the same behavior.

 

In the real world InventorAddIn2 would be our 'product', while InventorAddIn1 would be a custom add-in of our customer. In my opinion creating a custom property shouldn't affect the event behavior.

 

Thanks in advance for your help!

 

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 7 of 8

psaarloos
Collaborator
Collaborator

Hi,

 

I did some more testing. Not adding a custom property is causing an issue when the OnSaveDocument event is cancelled in another add-in, but it's just changing a custom property value during the OnSaveDocument!

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 8 of 8

chandra.shekar.g
Autodesk Support
Autodesk Support

@psaarloos,

 

Thanks for data and reproducible steps,

 

Issue is able to reproduce. For further investigation, a change request(INVGEN-40480) is created with engineering team.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes