Chat here it stays so you can get updates each time you visit back. |
|
Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs. |
Current SolidWorks Service Pack |
|
Quick Links SolidWorks |
Please support this site and donate any amount you would like if this site helped you out.
|
|
|
New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today
|
One of the best places to start working with the SolidWorks API (application programming interface) is to use its macro recording capability. When you record a macro in SolidWorks, you are able to record the Basic code required to accomplish many tasks. There are some limitations to what is recorded in a macro, but most of the basic functionality is automatic.
For this example we want to create a keyboard shortcut to perform a distance mate between two selected faces, edges or vertices in an assembly. As a result, we will be able to pre-select two objects and then type a keyboard shortcut that activates the macro. The only dialog we will see is one to prompt us for the distance value between the two selected objects.
Recording the macro
Our goal in recording this macro is to capture only the act of applying the distance mate between two pre-selected objects.
1. Open an assembly containing at least a couple parts to be mated with a distance mate.
2. Select two faces to apply the distance between. DO NOT select the mate tool yet.
3. Start recording the macro by selecting Tools, Macro, Record or by clicking () on the Macro toolbar.
4. Use the mate tool to create a distance mate of 1mm.
5. Stop the macro by selecting Tools, Macro, Stop or by clicking on the Macro toolbar.
6. Save the macro with the name distancemate.swb to a new (or existing) folder named macros under your SolidWorks installation directory (typically C:\Program Files\SolidWorks\macros).
Modifying the macro
Once we have created a basic macro it is typical to make modifications either to add functionality or to make it general enough to be used everywhere[1]. At this point we would like to modify the macro to prompt the user for a distance value for the mate.
1. Edit the macro by selecting Tools, Macro, Edit or by clicking on the Macro toolbar. Browse to the distancemate.swb macro and open it.
2. Modify the "Sub main( )" text to match that shown below, leaving the declarations as is (all of the "Dim . As .").
3. Close and save the macro.
Now, what did we really do to the macro?
· The variable "Dist" is declared as a double floating point number so we can store user input distances in that variable.
· We must disable the Part.ClearSelection method because we want the two objects we had pre-selected to remain selected prior to adding the mate.
· In order to prompt the user for a distance for the mate we use the InputBox method. This VB method displays a dialog box on the screen allowing the user to type in any value. We've prompted them to "Please enter the distance in meters" because most API calls require input in meters.
· Next, we've disabled the recorded mate since we want to apply a mate with a value other than 1mm. Instead, we've added a new line of code, replacing the 0.001-meter value with the "Dist" variable.
If you want to find out more about the AddMate method, search the index in the SolidWorks API Help Topics.
Running the Macro
There are several ways to run the distancemate macro once it is recorded and modified. One of the best ways is to add a keyboard shortcut in SolidWorks to run the macro. That is why we saved the macro into the macros folder in the SolidWorks installation directory. To establish the keyboard shortcut:
- Exit and then restart SolidWorks. This will refresh the list of available macros.
- Open an assembly and then go to Tools, Customize. Select the Keyboard tab.
- From the list of categories, select Macros. In the commands list, you will see distancemate.swb. Select it and add a new keyboard shortcut. I will use D in this example.
Let's test the macro. Open any assembly where you could apply a distance mate. Pre-select two items that would be valid for a distance mate and type your keyboard shortcut. You will be presented with a dialog to enter the distance value. Type it in and voila!
Summary
As you can see, making macros in SolidWorks can be a quick and easy way to automate repetitive tasks. You could use the same method to create shortcut keys to apply other mate types, add standard text to drawings, create quick converted entity extrusions, or anything else you can imagine!
[1] SolidWorks macros are simply ASCII text files containing Basic code. They are identical to a standard Visual Basic (VB) Module other than having a different file extension. If you wish to use the macro in a VB or VBA (Visual Basic for Applications) project, simply rename the file with the .bas extension rather than .vbp.
Mike Spens
mikespens@solidworkstips.com
For more from this author look into:




