SolidWorks Tips and Things

Subscribe | Daily Tips | Unsubscribe
SolidWorks Tips Home | 3D iDesign, Inc Home
#1 | #2 | #3 | #4 | #5 | #6 | #7 | #8 | #9 | #10 | #11 | #12 | #13 | #14| #15| #16|
#1 | #2 | #3 | #4 | #5 | #6 |
subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link | subglobal7 link
subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link | subglobal8 link

Macro Basics

API TIP #1    2/15/2001

 

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:

  1. Exit and then restart SolidWorks.  This will refresh the list of available macros.
  2. Open an assembly and then go to Tools, Customize.  Select the Keyboard tab.
  3. 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:

 

 

 

 

 

SolidWorks Daily Text Tips

Chat here it stays so you can get updates each time you visit back.


Get your own Chat Box! Go Large!

Click to see how 3D iDesign, Inc can help your company get to market faster. Click Here to view the vast services Engineering needs.
Product Design
2D to 3D
Art to Part
Reverse Engineering
Customer SolidWorks and PDMWorks Programming
Stress Analysis
CNC Programming

Current SolidWorks Service Pack
The current SolidWorks 2009 Service Pack is 1.0
The current SolidWorks 2008 Service Pack is 5.0
The current SolidWorks 2007 Service Pack is SP 5.0
The current SolidWorks 2007 x64 Service Pack is SP5.0
The current SolidWorks 2006 Service Pack is SP5.1
The current SolidWorks 2006 x64 Service Pack is SP5.1
The current PDMWorks 2006 Service Pack is SP5.1
The current SolidWorks 2005 Service Pack is SP5.0
The current PDMWorks 2005 Service Pack is SP5.0

Click here to login and download from SolidWorks Web site

Quick Links SolidWorks
3D iDesign, Inc Engineering Services
Video Cards Testing

Please support this site and donate any amount you would like if this site helped you out.

 

For more books visit here

New book from the author of SolidWorks Tips and Things API Tips Section Mike Spens pick one up today

About Us | Contact Us | ©2007 3D iDesign, Inc ©

SolidWorks and SolidWorks Applications are registered trademarks of SolidWorks Corporation. All other names may be trademarks of their respective owners. SolidWorks Tips and Things is not affiliated with or sanctioned by the SolidWorks Corporation