4.6 Macros
Design automation offers two benefits. First, it enhances productivity. Second, it helps with repetitive tasks that are mundane. For example, if you follow the same design process over and over, automating it would be the logical thing to do.
Macros aid in design automation. Macros are also viewed as a way to customize your CAD/CAM system. A macro is a short computer program that is used to repeat commonly performed operations. That computer program is generated automatically by a CAD/CAM system in the background during use of the system, from the time you turn on the macro until you turn it off. You can use the macro after creation over and over, with different input values (e.g., new dimensions). After you create a macro, you can use it for recording, edit it, run it, pause it, stop it, and assign it to a shortcut key (hotkey) or to a menu item. When you assign a macro to a shortcut key or to a menu item, you can specify which method (function) of the macro to run. Click Tools > Macro to access the macro menu. SolidWorks saves the macro file in the same part folder and uses .swp as the file extension, with the default names Macro1.swp, Macro2.swp, and so on, unless you change them.
A higher level of automation than using macros is to use Visual Basic (VB) or another programming language to perform full automation and have better control of the automation. As a matter of fact, VB is the programming engine behind macros. Instead of writing the macro VB code, the SolidWorks macro interface enables us to generate the code automatically while you perform the design tasks as usual. It is this VB code that you save in a file when you save the macro. You can use the VB editor to edit and tweak the VB code.
Macro VB code uses the SolidWorks API (application programming interface). The code makes calls to API functions. Think of the API as a gateway between the application you want to write and SolidWorks code that has been already written. In other words, the API provides access to the SolidWorks geometric engine.
You can learn VB programming by creating multiple macros, studying their generated VB code, and expanding on it. VB is an object-oriented programming (OOP) language that requires knowledge and understanding of object-oriented design and how objects are defined and implemented.
The programming approach could be useful for creating an entire assembly from a few parameters. You can write a program to define some variables and store them in a row in a design table. Each row represents a new version of the assembly. You delete all rows from the table and keep the last row, which is the new assembly.
Figure 4.9 VB editor window