VBA/API - Part 3
By Neil MunroLast month, in the second part of this three-part tutorial, we added code to our macro to place an occurrence of a Marker part at theassembly center of gravity (COG). In this tutorial, we'll add a number of additional procedures.One procedure will update the position of the Marker part to the assembly COG, in response to the user clicking the Update button. We'llalso add a procedure to set the scale of the Marker part. Finally, we'll add code to set the visibility of origin geometry (planes, axes,center point) in the Marker part. Along the way, we will look at methods you can use to find and fix programming errors (debugging).The hierarchy of objects for our program is shown in Figure 1.
For those diehards who have coded their way through the previous tutorials, continue on with your existing files. If you are just joiningus, download Cog_B.zip (129 KB) and extract the files.Cog_B.zip
(zip - 129Kb)
You can download the files used in this tutorial from here.Figure 1: Assembly COG object hierarchy.The zipped file contains the following:Marker.iptCOG_B.ivbPlace Marker.ipt in a folder included in the current project file.The COG_B.ivb project file includes all code up to the end of last month's tutorial. Rename this file to COG.ivb and load the projectthrough the Autodesk Inventor™ VBA IDE. Here's how:From the Tools drop-down menu on the Autodesk Inventor main menu bar, select Macro > Visual Basic Editor, which opens theVBA editing environment.1.From the VBA drop-down menu, select File > Load Project. Browse to the folder containing COG.ivb and load it.2.
Updating Marker Position
Although it is possible to automatically update the position of the Marker part in response to events that occur in Autodesk Inventor, we'llkeep it simple by only updating the position of the Marker part when the user clicks the Update button on our form. The code required toupdate the part position is almost identical to the code in the InsertMarker subroutine. We'll create a transient matrix to hold theassembly COG position, and then use that data to move the Marker part to the current assembly COG.Although the Marker part is grounded after insertion, there is nothing to prevent the user from ungrounding it, moving or rotating it, oreven assembling the Marker part to other assembly components. The matrix used to transform the Marker part restores the orientation(rotation) of the part to match the assembly coordinate system (zero rotation about all axes). Our update procedure will not check forassembly constraints, but you could add code to check for, and then delete, any assembly constraints on the Marker part prior tomoving it.1. Create a new private subroutine named UpdateMarker.2. Cut the first six lines (not including comments) of code from the InsertMarker procedure,and then paste them into the UpdateMarker procedure.
Autodesk - Autodesk Inventor Services & Support - VBA/API - Part 3http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=234694...1 of 71/21/2010 6:35 PM