Continuing with the iProperty theme of some recent posts I want to talk about the ability to work with iProperties without using Inventor. You can do this using a component known as Apprentice. Apprentice doesn’t have a user-interface but is a component use can only use through programming. Apprentice is installed as part of Inventor and is also installed as part of Inventor View (www.autodesk.com/inventorview). This means if Inventor or Inventor View is installed on a machine you’ll have access to the Apprentice component and any Apprentice dependent program will work. The fact that it’s available with Inventor View is interesting since Inventor View is free.
What is Apprentice?
Apprentice is a programming component that provides access to Inventor files. Using Apprentice you can open Inventor files, query information from them, and in a few cases, edit data. Apprentice supports a small subset of the functionality that Inventor supports and is primarily read-only with just a few exceptions. The primary things it provides access to are:
- Geometry of solids and surfaces (B-Rep)
- Assembly structure
- File references
- Bill Of Material
- Printing
- Attributes
- iProperties
You’ll notice that there are a lot of things missing. Apprentice is designed to be smaller and faster than Inventor and achieves this by providing having less code and operating on smaller portions of a document. Even things that are exposed in Apprentice will often have limitations that aren’t present in Inventor. For example, let’s say you have an extrude feature that you’ve added an attribute to in Inventor. Using Apprentice, you can open that part and you can query for all attributes and get the attribute set associated with the feature. However when you try and get the feature from the attribute set it’s not available because Apprentice doesn’t support features. Apprentice is good at what it does but does have limitations you need to be aware of before attempting to use it in a project.
Using Apprentice
Apprentice uses much of the same code and components as Inventor. Because of this there are conflicts if you try running both of them within the same process. Remember that Apprentice always runs within the process of another program.
This means:
- You can’t use Apprentice with Inventor’s VBA.
- You can’t use Apprentice in an Add-In.
Using VBA within another application, for example Excel, is ok since Apprentice will be running within the Excel process and not Inventor’s process. Another common use of Apprentice is within standalone exe programs where Apprentice will be running within the exe process. The example that part of this post is a standalone exe.
Another limitation of Apprentice is that it can’t migrate files. Migration occurs when you open a file that is older than the current version of Inventor. For example, if I have an Inventor 2009 file on disk and open it with Inventor 2010 the data is migrated to the current version of Inventor. Inventor is smart and only migrates what’s necessary based on what you’re currently doing with the file. However, if you save the file, the entire file must be migrated to the current version before it is saved. Apprentice can open and query older files but nothing can be changed and saved because that would require migration. You can use the NeedsMigrating property to determine if a file can be edited and saved. This is demonstrated in the sample at the bottom of this post.
Connecting to Apprentice
Here’s an example of how to use Apprentice within a VB Express Windows forms application. To enable the use of Apprentice in your program you need to add a reference to the Inventor Object Library. The Inventor Object Library is used for both Inventor and Apprentice even though Apprentice only supports a small subset of the entire library. You can add the reference using the Project | Add Reference… command. In the example below I’ve select the version 14.0.0 (Inventor 2010) interop.
Below is the minimum code needed to load Apprentice. This declares and creates a new ApprenticeServerComponent object. The ApprenticeServerComponent object is similar to the Inventor Application object. It is the top-level object for Apprentice.
Dim apprentice As New Inventor.ApprenticeServerComponent
It’s also common to declare the variable and then create apprentice separately, like shown below. I do this in the sample program where the variable is declared as a member variable so I can reuse it.
Dim apprentice As Inventor.ApprenticeServerComponent
apprentice = New Inventor.ApprenticeServerComponent
When you create the ApprenticeServerComponent object the Apprentice dll’s are loaded into your process and you can access the apprentice functionality through the properties and methods of the object.
Once you have an instance of the ApprenticeServerComponent, the next step is to open an Inventor document. You do this using the Open method of the ApprenticeServerComponent object, as shown below.
' Open a file using Apprentice.
Dim apprenticeDoc As Inventor.ApprenticeServerDocument
apprenticeDoc = apprentice.Open(Filename)
Apprentice supports the ApprenticeServerDocument and ApprenticeServerDrawingDocument types. The ApprenticeServerDocument object can represent all document types. the ApprenticeServerDrawingDocument can be used to access some drawing specific functionality.
Using iProperties With Apprentice
Once you have the document, accessing iProperties is the same in Apprentice as it is in Inventor. The example below gets the summary information property set and changes the value of the “Company” property. The previous posts on using the API with iProperties also apply to Apprentice.
' Get the "Summary Information" property set.
Dim summaryPropSet As Inventor.PropertySet
summaryPropSet = apprenticeDoc.PropertySets.Item( _
"Inventor Document Summary Information")
' Get the "Company" property.
Dim companyProp As Inventor.Property
companyProp = summaryPropSet.Item("Company")
' Change the value of the iProperty.
companyProp.Value = "Widgets R Us"
Saving Changes With Apprentice
When you use Inventor to change iProperties, you save the changes by saving the document. In Apprentice there’s a much more efficient way of saving iProperty changes. Instead of saving the entire document it’s possible to just save the iProperties. You do this using the FlushToFile method of the PropertySet object, as illustrated below.
apprenticeDoc.PropertySets.FlushToFile()
Example Program
Here’s an example program written using Visual Basic 2008 Express. It demonstrates the functionality discussed above. It’s a simple program that gets all of the part files in a specified directory, changes an iProperty value and saves it. Even though it’s simple it does demonstrates the functionality needed to access iProperties using Apprentice. You’ll need to modify it to fit your specific requirements.