C# addin before save event/initial save

C# addin before save event/initial save

gert-leonvanlier
Collaborator Collaborator
1,630 Views
4 Replies
Message 1 of 5

C# addin before save event/initial save

gert-leonvanlier
Collaborator
Collaborator

I've written an addin in C#. The addin is working perfectly and the user can start several actions from the toolbar. One particular action I would like to add to the addin is to launch a form (that is also started using a button on the toolbar) when the user saves a document for the first time. I've read about the before save event and found some examples, but I can't figure out how to implement it in my code. I do not understand how it works. Can anyone point me in the right direction or have some examples in C# or even VB.net?

Furhtermore I am looking for info on how to see if a document is saved for the first time.

 

Thanks in advance.

0 Likes
Accepted solutions (2)
1,631 Views
4 Replies
Replies (4)
Message 2 of 5

JelteDeJong
Mentor
Mentor
Accepted solution

I have an addin "ShowFullyConstraint" in the app store but it's an opensource project. In that addon I use the on save event. (it removes marks from the browsertree.) Anyway you can use it as an example. Here a link to the file that you should have a look at. LINK

The important thing here is are the activation(...) an onSaving(...) method:

public class StandardAddInServer : Inventor.ApplicationAddInServer
    {
        public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
        {
            inventor = addInSiteObject.Application;
            inventor.ApplicationEvents.OnSaveDocument += onSaving;
        }
private void onSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
        {
            if (BeforeOrAfter == EventTimingEnum.kBefore)
            {
                startUnMarking(DocumentObject);
            }
            HandlingCode = HandlingCodeEnum.kEventNotHandled;
        }

 

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

Message 3 of 5

gert-leonvanlier
Collaborator
Collaborator

Thank you very much. That solved a great deal of my issue. I am now able to launch my form whenever the document is saved.

 

Now looking on how to do is at the first/initial save.

0 Likes
Message 4 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @gert-leonvanlier 

I'm not very experienced with C#, but shouldn't something like this work? Check the documents FileSaveCounter-property and if it's 0 before save you know it's the first save 🙂

 

 Private void onSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, Out HandlingCodeEnum HandlingCode)
    {
        If(BeforeOrAfter == EventTimingEnum.kBefore && _Document.FileSaveCounter == 0)
            {
            startUnMarking(DocumentObject);
        }
        HandlingCode = HandlingCodeEnum.kEventNotHandled;
    }
Message 5 of 5

gert-leonvanlier
Collaborator
Collaborator

Everything works. Thank you both!

 

It has become this:

public class StandardAddInServer : ApplicationAddInServer
        {
            #region Load AddIn

            // This method is called by Inventor when it loads the AddIn. The AddInSiteObject provides access  
            // to the Inventor Application object. The FirstTime flag indicates if the AddIn is loaded for
            // the first time. However, with the introduction of the ribbon this argument is always true.
            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 \"MHS Tools\"" + System.Environment.NewLine + System.Environment.NewLine + ex.Message);
                }

            }

private void onSaving(_Document DocumentObject, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
            {
                if (BeforeOrAfter == EventTimingEnum.kBefore)
                {
                    if (DocumentObject.FileSaveCounter == 0)
                    {
                        CommandFunctions.RunMHSPropertyEditor();
                    }
                }
                else
                {
                    isSaving = false;
                }
                HandlingCode = HandlingCodeEnum.kEventNotHandled;
            }