Inventor API Fundamentals 004 – Connecting to the API

In the last posting I discussed the object model and that it provides a structured way to access the various objects that make up Inventor’s API. In order to use the object model you need to do two things; reference the library that describes Inventor’s object model and gain access to the Application object. (The Application object is the top-most object in the object model and through it you can access all the other objects.)

How you reference the API and access the Application object will differ slightly for the different languages. These are discussed below along with example code that demonstrates getting the Application object and calling its Caption property.

Inventor’s VBA

Because the VBA that’s delivered with Inventor is integrated with Inventor, it provides the easiest access to Inventor’s object model. In Inventor’s VBA, the Inventor API library is already referenced and it provides an easy way of accessing the Application object by providing a global property called ThisApplication, which returns the Application object. All you need to display Inventor’s caption with Inventor’s VBA is the code below.

MsgBox ThisApplication.Caption

External Application

External Application refers to any application that runs outside of Inventor. Typically these are Windows applications (.exe programs) but this also includes VBA programs written using VBA in another application (i.e. Word or Excel). This term does not refer to Add-In applications.

How you Reference Inventor’s API varies for each of the programming environments and is discussed below for each one. The mechanics of gaining access to the Application object also varies with the different programming languages; however, for all languages there are two basic ways to connect. The first is to see if Inventor is running and get the Application object associated with it. The second is to start Inventor and get the associated Application object. Both are useful and which one to use depends on what you’re trying to accomplish. Connecting to a running instance of Inventor is probably the most common. Starting Inventor is common for batch processing types of programs.


VBA / VB 6

Referencing the API

Use the “References…” command (in the Tools menu in VBA and the Project menu in VB 6) to add a reference to Inventor’s object library, as shown below.

References
Referencing the Inventor Object Library in VBA and VB 6.

Connecting to a Running Instance of Inventor

Dim inventorApp As Inventor.Application

' Attempt to get a reference to a running instance of Inventor.
On Error Resume Next
Set inventorApp = GetObject(, "Inventor.Application")
If Err Then
    MsgBox "Inventor must be running."
    Exit Sub
End If
On Error GoTo 0

MsgBox inventorApp.Caption

Starting an Instance of Inventor

Dim inventorApp As Inventor.Application

On Error Resume Next
Set inventorApp = CreateObject("Inventor.Application")
If Err Then
    MsgBox "Error starting Inventor."
    Exit Sub
End If
On Error GoTo 0

' Make Inventor visible.
inventorApp.Visible = True

MsgBox inventorApp.Caption

VB.Net

Referencing the API

The .Net languages use something called an interop assembly to be able to call a COM Automation API. With Inventor 2009, Inventor installs an interop assembly (Primary Interop Assembly or PIA) for all developers to use. You reference this into your application using the “Add Reference…” command and selecting Autodesk.Inventor.Interop from the .Net tab as shown below.

InteropReference
Referencing the Inventor Object Library in VB.Net

For versions of Inventor previous to Inventor 2009 you need to select “Autodesk Inventor Object Library” from the COM tab of the Add Reference dialog. This will generate a local interop assembly for your project.

ReferenceNet
Referencing the Inventor Object Library in VB.Net (Inventor 2008 and earlier)

Connecting to a Running Instance of Inventor

The VBA / VB6 code above will work in VB.Net, but VB.Net also provides the try catch style of error handling which is usually nicer than the older On Error error handling.

Dim inventorApp As Inventor.Application

' Attempt to get a reference to a running instance of Inventor.
Try
    inventorApp = GetObject(, "Inventor.Application")
Catch ex As Exception
    MsgBox("Inventor must be running.")
    Exit Sub
End Try

MsgBox(inventorApp.Caption)

Starting an Instance of Inventor

Dim inventorApp As Inventor.Application
Try
    inventorApp = CreateObject("Inventor.Application")
Catch ex As Exception
    MsgBox("Error starting Inventor.")
    Exit Sub
End Try

inventorApp.Visible = True

MsgBox(inventorApp.Caption)

C#

Referencing the API

Referencing the API in a C# application is exactly the same as for Visual Basic and the instructions above apply. You’ll need to add the following to the top of your project.

using System.Runtime.InteropServices;

Connecting to a Running Instance of Inventor

Inventor.Application inventorApp = null;
try
{
    // Attempt to get a reference to a running instance of Inventor.
    inventorApp =(Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
}
catch
{
    MessageBox.Show("Unable to connect to Inventor.");
    return;
}

MessageBox.Show(inventorApp.Caption);

Starting an Instance of Inventor

Inventor.Application inventorApp = null;

try
{
    // Start Inventor.
    System.Type oType = System.Type.GetTypeFromProgID("Inventor.Application");
    inventorApp = (Inventor.Application)System.Activator.CreateInstance(oType);

    // Make Inventor visible.
    inventorApp.Visible = true;
}
catch
{
    MessageBox.Show("Unable to start Inventor.");
    return;
}

MessageBox.Show(inventorApp.Caption);

Visual C++

Referencing the API

Referencing the API for an unmanaged VC++ project is done by including a header file provided by Inventor’s SDK. This header file imports Inventor’s object library and includes some other useful header files.

#include "InventorUtils.h"

Connecting to a Running Instance of Inventor

There are different flavors of VC++ (managed and unmanaged) and different ways to use Inventor’s API from VC++. The samples below represent one style.

HRESULT Result = NOERROR;

CLSID inventorClsid;
Result = CLSIDFromProgID (L"Inventor.Application", &inventorClsid);
if (FAILED(Result)) return Result;

CComPtr pInventorAppUknown;
Result = ::GetActiveObject (inventorClsid, NULL, &pInventorAppUknown);
if (FAILED (Result)){
    MessageBox(0, _T("Could not connect to Inventor."), _T("Error"), 0);
    return Result;
}

// QueryInterface for the Inventor Application object.
CComPtr pInventorApp;
Result = pInventorAppUknown->QueryInterface(__uuidof(Inventor::Application),
                                          (void **) &pInventorApp);
if (FAILED(Result)) return Result;

// Get and display the caption.
CComBSTR bstrCaption;
Result = pInventorApp->get_Caption(&bstrCaption);
if (SUCCEEDED(Result))
    MessageBox(0, bstrCaption, _T("Inventor Caption"), 0);

Starting an Instance of Inventor

HRESULT Result = NOERROR;

CLSID inventorClsid;
Result = CLSIDFromProgID (L"Inventor.Application", &inventorClsid);
if (FAILED(Result)) return Result;

CComPtr pInventorAppUknown;
Result = CoCreateInstance(inventorClsid, NULL, CLSCTX_LOCAL_SERVER,
                          __uuidof(IUnknown), (void **) &pInventorAppUknown);
if (FAILED (Result)) {
    MessageBox(0, _T("Could not start Inventor."), _T("Error"), 0);
    return Result;
}

// QueryInterface for the Inventor Application object.
CComPtr pInventorApp;
Result = pInventorAppUknown->QueryInterface(__uuidof(Inventor::Application),
                                          (void **) &pInventorApp);
if (FAILED(Result)) return Result;

// Make Inventor visible.
pInventorApp->Visible = VARIANT_TRUE;

// Get and display the caption.
CComBSTR bstrCaption;
Result = pInventorApp->get_Caption(&bstrCaption);
if (SUCCEEDED(Result))
    MessageBox(0, bstrCaption, _T("Inventor Caption"), 0);

Comments

5 responses to “Inventor API Fundamentals 004 – Connecting to the API”

  1. Hi Brian
    Thanks, great to finally see a Inventor API blog. Looking forward to follow your posts.
    Cheers
    Tore

  2. Great Post Brian !!!
    Looking forward for more posts from you :)
    And thanks for covering C# code as well. Please try to give C# code examples in future posts as well.

  3. Anyone have a clue how to rename the part # / member name fields in a case where
    I-assemblies and I-parts need to change per a new B.O.M. system , but have already been used in multiple higher level assemblies? This is a difficult thing to find info on, I think “VBA.net” may be our only hope??

  4. Eric Mathews Avatar
    Eric Mathews

    Hello,
    Great information. I’m following the steps provided and I’m not getting the desired results. I am using VB.net 2008 Express. One thing is, my Inventor did not install the “Autodesk.Inventor.Interop”. In order to get this file I have to install the SDK and then I can get it from: C:\Program Files\Autodesk\Inventor 2009\SDK\DeveloperTools\References.
    Here is my code:
    Imports Inventor
    Public Class Inventor
    Public Sub LoadInv()
    Dim app As Inventor.application
    End Sub
    End Class
    I get the following error: Type ‘Inventor.application’ is not defined.
    I think I’m getting close but I’m have some problem somewhere.

  5. Saeheal Fraubard Avatar
    Saeheal Fraubard

    Hi Brian
    Thanks, I got a lot help from this post with useing C++ API and asking your help.
    I’m in the problem with Opening and Saving Preversion Inventor file with API. Dlg box(Check box) is popping up for warning.
    Is there anyway to open or save in defalt mode? (In SolidWorks API, there’s Silent Option, but I can’t find any Option in Inventor open or save API)
    Looking forward to meet your posts.
    Cheers
    Saeheal

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading