Inventor Customization

Inventor Customization

Reply
Mentor
TONELLAL
Posts: 162
Registered: ‎01-17-2010
Message 1 of 7 (1,225 Views)
Accepted Solution

ThisApplication in VB .Net ?

1225 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

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.

Mentor
krieg
Posts: 176
Registered: ‎11-22-2009
Message 2 of 7 (1,217 Views)

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.

Valued Contributor
planglais
Posts: 62
Registered: ‎03-10-2011
Message 3 of 7 (1,205 Views)

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

Mentor
TONELLAL
Posts: 162
Registered: ‎01-17-2010
Message 4 of 7 (1,193 Views)

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 ?

Valued Contributor
planglais
Posts: 62
Registered: ‎03-10-2011
Message 5 of 7 (1,188 Views)

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

ADN Support Specialist
ekinsb
Posts: 351
Registered: ‎07-21-2006
Message 6 of 7 (1,171 Views)

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
Developer Technical Services
Autodesk Developer Network
Autodesk, Inc.
Mentor
TONELLAL
Posts: 162
Registered: ‎01-17-2010
Message 7 of 7 (1,163 Views)

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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.