Figure 4: CogTool subroutine.Let's examine the newly displayed tools.The list of procedures enables you to quickly access a particular named procedure in the module (you can define many differentprocedures in a module).The Code window is where you type in the instructions associated with a procedure (a subroutine is one of a number of proceduraltypes). For now, we'll ignore the Option Explicit code shown in the Code window.
Add a Subroutine to the Module
Now, let's create a subroutine in Module1, which Autodesk Inventor will recognize as a macro and which we'll use to start our assemblyCOG tracker.1. From the Visual Basic Editor menu, select Insert > Procedure.2. Enter
in the Name textbox. Note that the default settings are to create a Public subroutine. These are two of the requirementsfor a macro, so click OK to create the subroutine. Your Code window should now match Figure 4.Figure 5: Insert UserForm.A subroutine contains code to perform a specific task and can be called from other code. Other things of interest about subroutine codeinclude:To be recognized as a macro, a subroutine must be public.The subroutine code is typed into the area before the End Sub statement.The parentheses at the end of the subroutine declaration can contain arguments that are passed to (and can be returned from) thesubroutine. To be recognized as a macro, the subroutine must have no arguments.An example of a subroutine with arguments is shown below.
Public Sub AddTwo (Int1 as Integer, Int2 as Integer, Ans as Integer)Ans = Int1 + Int2End Sub
Other program code could call this subroutine using the following syntax:
Call AddTwo (5, 6, MyAns)
The Int1 and Int2 arguments are assigned the values 5 and 6 respectively. The Ans argument is linked to the MyAns variable in atwo-way relationship. MyAns is assigned the value of Ans at the end of the subroutine (11 in this case).
Displaying the UI for Our Macro
We have in place all the elements to define the macro, so let's add some instructions to actually perform something. We are planning tobuild a UI for our macro, so we'll add code to display our user interface. But before we add the code to show our UI, let's add thecontainer (UserForm) for the user interface.1. In the Project Explorer window, right-click UserProject1 and select Insert > UserForm from the shortcut menu that appears (seeFigure 5).
Autodesk - Autodesk Inventor Services & Support - VBA/API - Part 1http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=234582...of 111/21/2010 6:36 PM