NHCAD
NHCAD

Fun Projects in Mechanical Design & Custom Programming
Lebanon, NH


NHCAD Home

CAD(Computer Design)
Free CAD Software
SolidWorks
SolidWorks Macros
SolidWorks Custom Properties

CNC / Steppers
5804 Stepper Driver Circuit
Floppy Stepper Motors
DIY CD Plotter
DIY CNC Mill

J2ME Mobile Programs
Sprtint Wireless Toolkit
Device / ESN Activation
Favorite Apps
JPGViewer

1964 Oday Javelin
1964 Oday Javelin
Centerboard
Rudder
Tiller
Transom
Hull
Topside
Deck
Details
First Sail
SolidWorks Macros & Examples

  • Example 1 - TODAY.SWB will enter the current date into your drawing.
  • Example 2 - FACES2MODELCOLOR.SWB will parse all model faces, setting their color to 'use model color'
  • Example 3 - CAVITYFACES.SWB will select all faces that belong to a cavity feature
  • Example 4 - CHG_DIM.SWB will prompt for a data file and import dimensional values into your model
  • Example 5 - 3DPOINTS.SWB will prompt for a data file, start a 3D sketch, and automate the creation of 3d points connected by lines
  • Example 6 - BATCH OPEN.SWB will prompt for a data file, start a 3D sketch, and automate the creation of 3d points connected by lines
  • Example 7 - CONFIG PROPS.SWB will write several custom properties to active configuration
  • Example 8 - PLACE TEXT.SWB will place text at an X Y location on a drawing
  • Example 9 - SCALE1.SWB will resize your part file to show it somewhat *to scale* on your screen
  • Example 10 - EDITOR.SWB is designed to aid in building text using special characters
  • Example 11 - Converter for VRML1.0 to VRML2.0
  • Example 12 - ROOT.SWB is used to solve for one dimension by changing another
  • Example 13 - SW_SIZE.SWB will set the width & height (pixels) of your SW window


SolidWorks MACROS - Tutorial and Examples

While SolidWorks offers a great degree of customization through API function calls, the only built in programming you can do is with macros. The other programming languages that you could use with SolidWorks are VBA, VB, and VC++ (Visual Basic for Applications as in Excel, Word, and Access, Visual Basic, and Visual C++). Unfortunately the other programming languages all require additional software that will cost more money. The purpose of this article is to explore macros specifically because they are included with SolidWorks and this article should not exclude anybody that has SolidWorks.

It happened to me, and it probably happened to you too. You recorded a macro only to find that it did not play back properly – if at all. Your first mistake is using the built in macro recorder, it is pretty useless. It is OK for capturing some snippets of code and seeing what function is called. From there you can look up the function in the SolidWorks API help file and continue working on another program for instance.

Do not underestimate the power of macros! They really are functional, you just need to know what their limitations and abilities are. Here a few things to keep in mind when writing macros:

  1. Macros CAN be assigned to the SolidWorks menus or function keys – something Visual Basic alone cannot do. Woohoo!
  2. Macros use VB 3.0 style code. Visual Basic commands unique to 4.0+ may not be supported. There is still a great deal of functionality in Visual Basic 3.0.
  3. Macros have a limited ability to interact with the user. You will not be able to pop up dialog boxes with radio buttons, check boxes, command buttons, drop down menus, or ActiveX controls. You CAN prompt for a single entry at a time using InputBox() or display messages to the screen using MsgBox().
  4. Macros CANNOT receive variant arrays back from an API function. You cannot get the current color scheme (Color Table) of the active document. You CAN specify an array as an argument to write those properties. So you can SET the color scheme, but you cannot GET the current color scheme. This limitation applies to all SolidWorks API functions that return a variant array.

When you write macros, you will have to find or acquire the following three resources:

  1. \SolidWorks\samples\appcomm\Api_help.hlp
  2. The SolidWorks API help file is very useful. It makes a better reference than a beginner tutorial but contains some very useful information none the less. There are hundreds of functions listed in the API help file, you will have to refer to it and understand the format of it as your programs become more complicated.

  3. \SolidWorks\samples\appcomm\swconst.bas
  4. The Swconst.bas file is an ASCII file that you can open and view using any text editor like notepad. It is intended to be used with Visual Basic programs as an add-in module. The information inside it is necessary for writing macros too, but you will have edit and look up the values manually.

  5. Get your hands on a Visual Basic book or help file. You may be able to find a used VB3.0 book around for pennies or even a current book will do. The core commands for writing loops, using variables and arrays, and file Input / Output (I/O) have not changed in a long time. Additionally the Internet is a great source for sample code snippets with Visual Basic.