ThisApplication in VB .Net ?

ThisApplication in VB .Net ?

TONELLAL
Collaborator Collaborator
2,997 Views
6 Replies
Message 1 of 7

ThisApplication in VB .Net ?

TONELLAL
Collaborator
Collaborator

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

0 Likes
Accepted solutions (1)
2,998 Views
6 Replies
Replies (6)
Message 2 of 7

Ralf_Krieg
Advisor
Advisor

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.


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 7

Anonymous
Not applicable

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

0 Likes
Message 4 of 7

TONELLAL
Collaborator
Collaborator

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 ?

0 Likes
Message 5 of 7

Anonymous
Not applicable

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

0 Likes
Message 6 of 7

ekinsb
Alumni
Alumni
Accepted solution

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 and Fusion 360 API Expert
Mod the Machine blog
Message 7 of 7

TONELLAL
Collaborator
Collaborator

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

0 Likes