ONSave Event not being triggered unless breakpoint is set

ONSave Event not being triggered unless breakpoint is set

cstaley
Participant Participant
255 Views
2 Replies
Message 1 of 3

ONSave Event not being triggered unless breakpoint is set

cstaley
Participant
Participant

I have an addin that does some additional processing on save of a drawing.  The whole thing is working great in my development environment.  On a client machine, my onSave code is not being run.  I tried installing Visual Studio on the client and the only time I can hit my onSave code is if I set a breakpoint on the line in Activate where the event is registered.  I have a log entry right after the registration, so I can see that the code is getting there, but can't figure out why the registration doesn't happen without a breakpoint.

 

Here is the event registration:

public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
{
_inventorApp = addInSiteObject.Application;
Buttons.invButton.InventorApplication = _inventorApp;

_inventorApp.ApplicationEvents.OnSaveDocument += new ApplicationEventsSink_OnSaveDocumentEventHandler(onSaveEvent); //I need a breakpoint here in order to get to the event code
GenUtils.Logger.WriteLine("OnSave Event handler registered");
ControlDefinitions ctrlDefs = _inventorApp.CommandManager.ControlDefinitions;

etc....

 

 

 

Here is my event handler:

private void onSaveEvent(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
GenUtils.Logger.WriteLine("OnSave Event handler triggered");
if (BeforeOrAfter == EventTimingEnum.kAfter)
{
Document doc = Addin._inventorApp.ActiveDocument;
if (doc.DocumentType == DocumentTypeEnum.kDrawingDocumentObject)
InventorHelper.DoXML();
}
HandlingCode = HandlingCodeEnum.kEventNotHandled;

}

 

 

Any ideas?

0 Likes
Accepted solutions (1)
256 Views
2 Replies
Replies (2)
Message 2 of 3

Michael.Navara
Advisor
Advisor
Accepted solution

You need to keep reference to the ApplicationEvent as long as the addin is active. I keep this reference in field at the addin class level

 

[Guid("C0717AA7-C127-4AAD-8290-34353FA2511B")]
public class MyAddIn : ApplicationAddInServer
{
    private Application inventor;
    private ApplicationEvents applicationEvents;

    public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
    {
        inventor = addInSiteObject.Application;
        applicationEvents = inventor.ApplicationEvents;
        applicationEvents.OnSaveDocument += ApplicationEvents_OnSaveDocument;
    }

    public void Deactivate()
    {
        applicationEvents.OnSaveDocument -= ApplicationEvents_OnSaveDocument;
        applicationEvents = null;
        inventor = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();
    }

    public void ExecuteCommand(int commandId)
    {
    }

    public object Automation => null;

    private void ApplicationEvents_OnSaveDocument(_Document documentObject, EventTimingEnum beforeOrAfter, NameValueMap context, out HandlingCodeEnum handlingCode)
    {
        //TODO: Handle save event here
        handlingCode = HandlingCodeEnum.kEventNotHandled;
    }
}
0 Likes
Message 3 of 3

cstaley
Participant
Participant

That did the trick.  Thanks!

0 Likes