In some of the previous postings I've talked about some of the advantages and disadvantages of the various languages. One thing I mentioned was the use of VBA to prototype programs. A colleague stopped by my office earlier today asking for some help with a program he's writing for a customer. Apparently the customer, and now him, have been struggling with getting the program to work.
Here's a simplified description of what they were trying to do. While working in a drawing that contains retrieved dimensions they want to be able to get to the associated tolerance information from the part. I didn't immediately know the answer to this either but was able to figure it out in a couple of minutes. The reason I was able to do this is because I know how to use VBA as a prototyping tool. The good part is that you can learn how too. It's an extremely powerful tool when programming Inventor that you can take advantage of regardless of what language you're writing your program in.
I'll go through the procedure step-by-step that I used to figure out the solution to this specific problem. Although this looks at a specific case, the principles demonstrated here are generally applicable to any case. Below is the example for this case for this case which consists of a drawing view with three retrieved dimensions. Given a dimension we want to get the tolerance information from the associated parameter in the model.
- Have Inventor running with the document open that contains the type of data you want to query.
- Select the object you're interested in. In this case it's one of the dimensions.
- Open the VBA environment and write the following code. (I would write it in a module in your application project then you can use it again later.)
Public Sub DebugObject()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oObj As Object
Set oObj = oDoc.SelectSet.Item(1)
- Run the VBA macro. You'll get an error like the one shown below if there isn't an entity selected. Just stop the program, select the desired entity and run it again.
- The macro will stop execution at the Stop statement. (You could have also put a break in the code but the Stop statement is convenient.)
- Right-click the mouse anywhere within the variable oObj and select the "Add Watch..." command.
- Click OK and accept the defaults in the "Add Watch" dialog.
- The Watch window should appear, if it wasn't already shown, and the variable "oObj" will be displayed. The watch window shows three interesting things; Expression, Value, and Type. The expression is the variable or property we're watching, the value is the value of that expression, and the type is the type of the variable or object the expression results in. In this example we can see the type of oObj is a DiameterGeneralDimension. You'll often see object types prefixed with "IRx" and you can just ignore the prefix. The Value is empty because a DiameterGeneralDimension doesn't have an explicit value.
- Click on the "+" next to oObj to expand the object. Now all of the properties of the DiameterGeneralDimension are displayed. Each property also shows it's value and type. Some of the properties return objects and have a "+" next to them so you can expand them and see the properties associated with that object. In this case there are two properties that are interesting for the problem we're trying to solve; Retrieved and RetrievedFrom. The Retrieved property indicates if this drawing dimension was created by retrieving a dimension from the model. You can see in the debug window that the value is True for the currently selected dimension. The RetrievedFrom property returns the model dimension this was retrieved from. In this case the type of object returned is a FeatureDimension.
- Expanding the RetrievedFrom property you see the properties of the FeatureDimension object. One of these is the Parameter property which returns a ModelParameter object. Expanding this we can see the Tolerance property. Finally, expanding on this we can see the upper and lower tolerance values. All of this is shown below.
- Here's the corresponding code that was written based on what was learned from the debugger window.
Public Sub GetDimTolerance()
' Get the active document. Assumes it's a drawing.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Get the selected dimension, with error handling to
' gracefully error out if a dimension isn't selected.
On Error Resume Next
Dim oDim As GeneralDimension
Set oDim = oDrawDoc.SelectSet.Item(1)
If Err Then
MsgBox "A drawing dimension must be selected."
' Check to see if the selected dimension was retrieved.
If oDim.Retrieved Then
' Get the associated model dimension.
Dim oFeatureDim As FeatureDimension
Set oFeatureDim = oDim.RetrievedFrom
' Get the associated model tolerance.
Dim oTol As Tolerance
Set oTol = oDim.RetrievedFrom.Parameter.Tolerance
' Display the tolerance value.
Debug.Print "Tol: " & oTol.Upper & "/" & oTol.Lower
The specifics of what the code is doing in this case isn't important but the method we used to arrived at this code is. The debug window provides a "live" view of the object model that you can traverse through to see how objects are connected through their various properties. Anytime you want to investigate how to use the API to access a certain object or to get to particular information.