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.
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.
Referencing the Inventor Object Library in VBA and VB 6.
Connecting to a Running Instance of Inventor
' 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
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.
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.
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.
' 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
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.
Connecting to a Running Instance of Inventor
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
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.
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.
CLSID inventorClsid;
Result = CLSIDFromProgID (L"Inventor.Application", &inventorClsid);
if (FAILED(Result)) return Result;
CComPtr
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
Result = pInventorAppUknown->QueryInterface(__uuidof(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
CLSID inventorClsid;
Result = CLSIDFromProgID (L"Inventor.Application", &inventorClsid);
if (FAILED(Result)) return Result;
CComPtr
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
Result = pInventorAppUknown->QueryInterface(__uuidof(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);


Subscribe