• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk Inventor Customization

    Reply
    Distinguished Contributor
    Posts: 120
    Registered: ‎01-17-2010
    Accepted Solution

    ThisApplication in VB .Net ?

    148 Views, 6 Replies
    02-12-2013 05:01 AM

    Hello,

    I have a question which shouild be very simple for programmers...

    I create an add-in with VB .Net 2012, which use a form. When I click on a button, I launch a sub in another module.

    How can I have, in this sub, the equivalent of ThisApplication in VBA ? I think I have to get the active Inventor, but I missed something... For the moment, when I debug, the add-in ends on " m_inventorApplication = addInSiteObject.Application".

       

    Sub renum(ByVal direction AsString)

           

    Dim m_inventorApplication As Inventor.Application

    Dim oDoc AsDrawingDocument

     

            m_inventorApplication = addInSiteObject.Application

            oDoc = m_inventorApplication.ActiveDocument

     

    '.............

     

    End Sub

     

     

     

    PrivateFunction addInSiteObject() AsObject

     ThrowNewNotImplementedException   

    End Function

    Please use plain text.
    Mentor
    Posts: 174
    Registered: ‎11-22-2009

    Re: ThisApplication in VB .Net ?

    02-12-2013 09:27 AM in reply to: TONELLAL

    Hello

     

    Paste following code in your form right under Public class ....

     

    Private m_inventorApp As Inventor.Application
    
        Public Property InventorApplication() As Inventor.Application
            Get
                Return m_inventorApp
            End Get
            Set(ByVal value As Inventor.Application)
                m_inventorApp = value
            End Set
        End Property

     So you can access m_inventorApp elsewhere in your Sub's.

    Please use plain text.
    Valued Contributor
    planglais
    Posts: 53
    Registered: ‎03-10-2011

    Re: ThisApplication in VB .Net ?

    02-12-2013 07:59 PM in reply to: TONELLAL

    I work with Inventor 2011 and VB.Net 2010, so I hope it's work the same.

     

    In the StandardAddInServer class add a property and initialise it in the Activate sub:

     

    ' Inventor application object
    Property InventorApplication As Inventor.Application
    
    Public Sub Activate(ByVal addInSiteObject As ApplicationAddInSite, ByVal firstTime As Boolean) Implements ApplicationAddInServer.Activate
                ' Initialize application object
                Me.InventorApplication = addInSiteObject.Application
    
    ...
    End Sub
    
    

    When you instantiate the form, you must do something like this:

    Dim myForm as new Form1
    myForm.Show

     In the Form1 code, create the same property, as above, for the Inventor application and before showing the form, set this property:

    Dim myForm as new Form1
    myForm.InventorApplication = Me.InventorApplication
    myForm.Show

     You can now work with Me.InventorApplication anywhere in the Form1 class code.

     

     

    Pascal

    Please use plain text.
    Distinguished Contributor
    Posts: 120
    Registered: ‎01-17-2010

    Re: ThisApplication in VB .Net ?

    02-13-2013 07:14 AM in reply to: planglais

    Thanks for your answers !

    Krieg, your solution provoque a CER...

    I found another solution :

    In the module code : 

     

    Imports System.Runtime.InteropServices

    m_inventorApplication = Marshal.GetActiveObject("Inventor.application")

     

    What is the difference ?

    Please use plain text.
    Valued Contributor
    planglais
    Posts: 53
    Registered: ‎03-10-2011

    Re: ThisApplication in VB .Net ?

    02-13-2013 07:29 AM in reply to: TONELLAL

    The Marshal.GetActiveObject method will return an instance of the application.  If you have more than once, you don't know which one it will returns.

     

    If you have only one instance, it will works but I use this method only when I don't have choice.  By example, when I create an EXE application and I want to instantiate the Inventor.Application object.

     

    Pascal

    Please use plain text.
    Employee
    Posts: 152
    Registered: ‎07-21-2006

    Re: ThisApplication in VB .Net ?

    02-14-2013 03:38 PM in reply to: TONELLAL

    Here's what I do in an add-in so I have access to the Inventor Application object from anywhere in the project.

     

    I add the code below in one of the files of the project.  Often this will be in the .vb file where the add-in class is defined.  This code goes at the very bottom of the file, even outside the Namespace.

     

    Public Module Globals
        Public g_inventorApplication As Inventor.Application
    End Module

     

    Next I delete the declaration of m_inventorApplication at the top of the add-in class and rename m_inventorApplication to g_inventorApplication wherever it was used.  I could even skip the renaming step if I left the "m_" name but I like identifying the name as being a global variable.  In either case I need to delete the declaration at the top of the class. 

     

    Now anywhere in the project where you need to use the Application object you can use g_inventorApplication.



    Brian Ekins
    Inventor API Product Designer
    Manufacturing Solutions
    Autodesk, Inc.
    Please use plain text.
    Distinguished Contributor
    Posts: 120
    Registered: ‎01-17-2010

    Re: ThisApplication in VB .Net ?

    02-15-2013 12:48 AM in reply to: ekinsb

    Thanks to everyone for the answers ! It's clearer now.

    Please use plain text.