Here is section twelve of the Inventor API training where you see how Apprentice is used to access Inventor Data without having to use Inventor. You also learn about the Inventor viewer control. This is the fourth post with the Inventor API training material. (Section one is here).
Here is the agenda for this section. Be sure to see the Lab instructions at the end of this section. (completed samples in VB.NET and C# are available)
Apprentice Definition
Apprentice Functionalities
Apprentice Guidelines
Apprentice vs. Inventor: Differences
Saving Files with Apprentice
Inventor View Control
Apprentice Definition
This diagram shows where the API for Apprentice fits in . You use a stand alone client application, reference the Inventor API and use the apprentice classes to access Inventor data.
Apprentice provides a subset of the Inventor API and is
free as it is installed as part of Inventor View. A simple way to think of Apprentice is that it's a smaller version of Autodesk Inventor that does not have a user interface. Because there is not a user interface the only way to access its functionality is by using its API. Apprentice can be very efficient for certain applications because it's smaller than the complete Autodesk Inventor application and because it runs in the same process as your application.
Apprentice Functionalities
Apprentice provides read-only access to the following:
Assembly structure
B-Rep
Drawing sheets and views (limited access)
iParts
iAssemblies
BOM
Apprentice provides read / write access to the following:
iProperties
attributes
file references
Apprentice – Guidelines
Apprentice should NOT be used in-process with Inventor such as in an Add-In or through Inventor’s VBA. (If the Add-In is an exe type then it is ok).
In past releases a type library specifically for Apprentice was delivered with both Autodesk Inventor and Apprentice. This type library contained the limited set of objects supported only by Apprentice. Now the Autodesk Inventor type library contains all of the Apprentice functionality and is used to access Apprentice. (You add a reference to Inventor.Interop for an Apprentice project). A version of the old Apprentice type library is still supplied for legacy reasons, but this will likely be discontinued in future releases.
Apprentice vs. Inventor: differences
Apprentice is instantiated using a “new ApprenticeServerComponent”. (Instantiating Inventor would be done using something like CreateObject)
The ApprenticeServerComponent supports a few methods and properties that are unique to Apprentice.
Open and Close methods, which are used to open and close documents within Apprentice
DisplayAffinity, which is used to optimize the behavior of Apprentice for viewer applications
MinimizeFileSize, which compresses files by removing versions
FileSaveAs, used to save files
The document objects used within Apprentice are different from the document objects used in Autodesk Inventor. (Apprentice does not have a Documents collection) In Inventor there are the PartDocument, AssemblyDocument, DrawingDocument, and PresentationDocument objects. In Apprentice, the ApprenticeServerDocument object represents the part, assembly, and presentation documents and the ApprenticeServerDrawingDocument represents the drawing document.
Note: Apprentice cannot save a file from a previous version. You would need to migrate the file prior to saving it with Apprentice. (To migrate a file, open it with Inventor, save it and close it).
VB.NET Example that uses Apprentice to open an ipt file
Private Sub ApprenticeSample()
' Create Apprentice.
Dim oApprentice As ApprenticeServerComponent
oApprentice = New ApprenticeServerComponent
' Open a document.
Dim oDoc As ApprenticeServerDocument
oDoc = oApprentice.Open("C:TempPart1.ipt")
MsgBox("Opened: " & oDoc.DisplayName)
End Sub
Saving Files with Apprentice
Use FlushToFile if the code is only modifying iProperties. This is more efficient because the document is not written back.
VB.NET Example that uses FlushToFile after changing iProps
Private Sub SetProperty(ByVal author As String)
Dim oApprenticeDoc As ApprenticeServerDocument
oApprenticeDoc = mApprenticeServer.Open _
("c:TempMyPart.ipt")
'Get "Inventor Summary Information" PropertySet
Dim oPropertySet As PropertySet
oPropertySet = oApprenticeDoc.PropertySets _
("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}")
'Get Author property
Dim oProperty As Inventor.Property =
oPropertySet.Item("Author")
oProperty.Value = author
oApprenticeDoc.PropertySets.FlushToFile()
oApprenticeDoc.Close()
End Sub
VB.NET Example that uses the FileSaveAs to make a copy
Public Sub saveToNewFile()
' Create an instance of Apprentice.
Dim apprentice As New _
Inventor.ApprenticeServerComponent()
' Open a part
Dim appDoc As _
Inventor.ApprenticeServerDocument _
= apprentice.Open _
("C:TempPart1.ipt")
' Save the file to a new name
Dim myFileSaveAs As FileSaveAs =
apprentice.FileSaveAs
myFileSaveAs.AddFileToSave _
(appDoc, "C:TempPart1_update.ipt")
myFileSaveAs.ExecuteSaveCopyAs()
appDoc.Close()
End Sub
Transient Camera
You can create image files by getting the camera from the TransientObjects of the ApprenticeServerComponent CreateCamera method.
VB.NET example that opens a part and creates a jpg file
Public Sub saveMyImage()
' Create an instance of Apprentice.
Dim apprentice As New _
Inventor.ApprenticeServerComponent()
' Open the specified file.
Dim appDoc As _
Inventor.ApprenticeServerDocument _
= apprentice.Open("C:TempPart1.ipt")
' Create a camera.
Dim cam As Inventor.Camera =
apprentice.TransientObjects.CreateCamera
' Associate the camera with the part's
' component definition.
cam.SceneObject =
appDoc.ComponentDefinition
' Set the camera to the desired
' orientation and position.
cam.ViewOrientationType =
Inventor.ViewOrientationTypeEnum.
kIsoTopRightViewOrientation
cam.Fit()
cam.ApplyWithoutTransition()
'cam.Apply()
Dim oTOs As TransientObjects
oTOs = apprentice.TransientObjects
Dim oTopColor As Color
oTopColor =
oTOs.CreateColor(255, 0, 0)
Dim oBottomColor As Color
oBottomColor =
oTOs.CreateColor(255, 255, 255)
' Create an jpg file
cam.SaveAsBitmap("C:TempPart1.jpg",
800, 600, oTopColor, oBottomColor)
End Sub
Inventor View Control
You can use a control that is installed with Inventor View to view Inventor files in your own application. The control is named InventorViewCtrl.ocx. To add it to your project in Visual Studio right click on the Toolbox and select “Choose Items…” On the COM Components tab select it. Once it is in the Toolbox select it and place it on your form. 
Note: To work with Inventor View control of 2013 use .NET framework 3.5
Before Inventor 2012, InventorViewCtrl.ocx was registered automatically with product. Starting from 2012, the registry-free was introduced. So plug-in developers have to link the corresponding manifest files to their application if they want to use the OCX.
Two options:
1. Use mt.exe to generate manifest file by yourself and link the manifest file to your application.
2. Use old style. But you have to register InventorViewCtrl.ocx manually by running “Regsvr32 InventorViewCtrl.ocx”.
See the post here for more information about this:
Lab work with Apprentice API
Create a Windows Forms application that has two buttons a label and a TextBox. When one of the buttons is clicked, provide the user with a way to select a part file (ipt). Once the file is selected create a jpg from the file. When the other button is selected open a file and update the Author iProperty with the value in the TextBox.
Lab – use the view control
Create a Windows Forms application that has the viewer control and a button. When the user clicks the button display the file in the viewer control.
Here are examples of the completed Labs. (VB.NET and C#) The zip also contains the ppt we use for the training.
Download Inventor_Training_Module_12_Samples
-Wayne





Leave a Reply