Here’s my second installment on some of the differences you’ll encounter when using C# to program Inventor’s API. The first post is here.
Property Name Differences
If a property has arguments then C# automatically converts that property into one or two methods so that there is a method that represents the read version of the property and another for the write version.
Here’s an example VBA program that calls some properties. The first is the Transformation property of the ComponentOccurrence object. It reads this property and then later also sets the value of the property. The program also uses the Cell property of the Matrix object. This property has arguments to allow you to specify the row and column of the cell you want to get or set.
' Get the selected occurrence from the active document.
' This will fail if an occurrence is not currently selected.
Set occ = asmDoc.SelectSet.Item(1)
' Get the transform from the selected occurrence.
Dim transform As Matrix
Set transform = occ.Transformation
' Display the current X translation value.
Debug.Print "X translation: " & transform.Cell(1, 4)
' Modify the X translation to be an additional 1 cm.
transform.Cell(1, 4) = transform.Cell(1, 4) + 1
' Assign the modified matrix back to the occurence.
occ.Transformation = transform
Here’s the equivalent code in C#. Because the Transformation property doesn’t have any arguments, you use it the same way as in the VBA code above. The Cell property has arguments and doesn’t exist in C# but is instead accessed by the get_Cell and set_Cell methods. The get_Cell method takes the row and column as input arguments and returns the value of the cell as a return value. To set the cell I call the set_Cell method. This has three arguments, the first two are the row and column and the third is the new value of the cell. The get_Cell and set_Cell methods don’t actually exist in the Inventor API, but are automatically created by C# as part of its COM interop implementation.
// Get the selected occurrence from the active document.
// This will fail if an occurrence is not currently selected.
occ = (ComponentOccurrence) asmDoc.SelectSet;
// Get the transform from the selected occurrence.
transform = occ.Transformation;
// Display the current X translation value.
"X translation: " + transform.get_Cell(1, 4));
// Modify the X translation to be an additional 1 cm.
transform.set_Cell( 1, 4, transform.get_Cell(1, 4) + 1.0 );
// Assign the modified matrix back to the occurence.
occ.Transformation = transform;
This inconsistency between VB and C# is relatively easy to catch because you’ll get an error saying that the specified property doesn’t exist. That should be the reminder to look for the get_XXX or set_XXX version instead.
Methods that Return Values as Arguments
In C# you need to explicitly specify that an argument will return a value when making a method call. Here’s the VBA example of calling the GetCoordinateSystem method of the Matrix object. All four arguments are return values. The VBA version of the code doesn’t do anything special to indicate they are return values.
Dim origin As Point
Dim xAxis As Vector
Dim yAxis As Vector
Dim zAxis As Vector
Call transform.GetCoordinateSystem(origin, xAxis, yAxis, zAxis)
Here’s the equivalent C# code. Notice that I have to qualify each argument that will return a value with the “out” keyword to indicate that the argument is being passed by reference and will return a value.
transform.GetCoordinateSystem(out origin, out xAxis,
out yAxis, out zAxis);
This issue is also relatively easy to catch and fix since it shows itself as a compile error. Intellisense provides the best clue about what the problem is. Below is the feedback for the code above before I added the “out” keyword to each argument. Notice that each argument has the “out” keyword in the signature shown in the intellisense window. Typically there may be some input arguments and one or two output arguments. It’s only the output arguments that you need to add the “out” keyword to.