Add-In with event example to replace auto-run macros in 2014

Inventor 2014 no longer supports VBA auto-run macros. Here is an excerpt from the Inventor API help file:

Autodesk Inventor 2014 removes support for VBA auto-run macros that are embedded in Autodesk Inventor documents. The VBA macros are AutoOpen, AutoNew, AutoSave, AutoClose, and AutoEdit. They can severely impact Autodesk Inventor performance, and pose a possible security risk. The suggested alternative to auto macros is an add-in. Add-ins don't have the performance and security issues of auto macros and provide much better source control.

If you are using VBA auto-run Macros you will need to start using events to get the same functionality. In one case the AutoSave macro was being used to add the time and day to custom iProperties. This stopped working in Inventor 2014. To get the behavior back an Add-In with an OnSaveDocument event needed to be used. Here is the VB.NET (VS 2010) project that creates an Add-In that that can be used to replace the behavior of the AutoSave VBA macro.

Download AddIn_With_Saved_Event 

There are instructions for creating an Add-In in the API help file. 

image

.

Here is the event callback:

Private Sub m_ApplicationEvents_OnSaveDocument _
      (ByVal DocumentObject As Inventor._Document,
  ByVal BeforeOrAfter As Inventor.EventTimingEnum,
           ByVal Context As Inventor.NameValueMap,
  ByRef HandlingCode As Inventor.HandlingCodeEnum)
 
    If BeforeOrAfter = EventTimingEnum.kBefore Then
        Try
            'Ensure active document is a Drawing
            If m_inventorApplication.
                           ActiveDocumentType = _
       DocumentTypeEnum.kDrawingDocumentObject Then
 
                'Get the custom iProperties
                Dim oPropSet As PropertySet = _
m_inventorApplication.ActiveDocument.PropertySets _
          ("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
 
                Dim bDateAdded As Boolean = False
                Dim bTimeAdded As Boolean = False
 
                Dim prop As Inventor.Property
                For Each prop In oPropSet
                    If prop.Name = "SysDate" Then
                        prop.Value = _
                     Format(Date.Now, "MMM-d-yy")
 
                        bDateAdded = True
                    End If
 
                    If prop.Name = "SysTime" Then
                        prop.Value = _
                  Format(Date.Now, "hh:mm:ss tt")
 
                        bTimeAdded = True
                    End If
                Next
 
                ' If the drawing does not have the 
                ' SysDate custom property
                If bDateAdded = False Then
                    oPropSet.Add(Format(Date.Now, _
                            "MMM-d-yy"), "SysDate")
                End If
 
                ' If the drawing does not have the 
                ' SysTime custom property
                If bTimeAdded = False Then
                    oPropSet.Add(Format(Date.Now, _
                         "hh:mm:ss tt"), "SysTime")
                End If
 
            End If ' if document is a drawing
        Catch ex As Exception
            MsgBox(ex.ToString())
 
        End Try
    End If 'if before save
 
End Sub

 

-Wayne


Comments

6 responses to “Add-In with event example to replace auto-run macros in 2014”

  1. Hello,
    Can you please send me the vb.net 2010 template for inventor 2013 add-in. i have one but it is not loading into inventor properly.
    thanks & regards,
    Sanjay

  2. Adriano Avatar
    Adriano

    Please help me. Drawing I had a file with a macro in VBA in Inventor, but the 2014 version it does not work. This Macro pulled the name, date and time of the moment he would do the printing.
    Could you help me?

  3. Wayne Brill Avatar
    Wayne Brill

    Hello Adriano,
    Perhaps you could use a command event. This blog article is about Vault but the idea of using the event would be similar.
    http://adndevblog.typepad.com/manufacturing/2012/05/printing-watermark-on-inventor-file-while-checking-it-in-vault.html
    You could also ask this question on the discussion group:
    http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120
    The community could have a good suggestion for this. Also my colleagues and I do answer questions there. (upload a file with the macro that works in 2013)
    I am not finding a working example to provide at this time.
    Thanks,
    Wayne

  4. In 2007 there was a download link for templates ‘VB 2008’ and ‘C++ 2008’. Is VB 2008 still supported in Inventor 2014, or is there already an ‘inventor add-in template’ for VB 2010?
    Thanks in advance

  5. Wayne Brill Avatar
    Wayne Brill

    Hi Rene,
    Here is an excerpt from the 2014 Readme.txt for the Wizard. This is in this directory on my system:
    C:\Users\Public\Documents\Autodesk\Inventor 2014\SDK\DeveloperTools\Tools\Wizards

    >
    Visual Basic.NET, Visual C#.NET:

    1- Introduction

    This wizard implements the templates for VC#.NET and VB.NET that help in generating Inventor AddIn
    applications. The wizard is integrated into Microsoft Visual Studio .NET (8.0, 9.0 and 10.0). In order to
    use the wizard, you would need to have one of these versions of Microsoft Visual Studio installed.
    << <<
    According to this the Inventor 2014 Add-In Wizard should install on Visual Studio 2010. (VS 10)
    If the wizard does not install please make a post on the Inventor Discussion group. My team does try to ensure that every post in this disucssion group gets an answer.
    http://forums.autodesk.com/t5/Inventor-Customization/bd-p/120
    Thanks,
    Wayne

  6. Wayne Brill Avatar
    Wayne Brill

    For some reason my reply is missing this.
    If the wizard does not install please make a post on the Inventor Discussion group. My team does try to ensure that every post in this disucssion group gets an answer.
    http://forums.autodesk.com/t5/Inventor-Customization/bd-p/120
    Thanks,
    Wayne

Leave a Reply to Wayne BrillCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading