Writing Macros to Automate Key Workflows at a Startup

Many users find the idea of creating and managing macros for any program to be intimidating and frustrating. But SOLIDWORKS comes with a built-in macro creator that makes it easy. You may not even know about it.

The benefit of automating repetitive tasks is two-fold:

  1. If you add the use of macros to your work process, you increase productivity as it decreases your design time by eliminating or reducing keystrokes.
  2. If you become proficient at creating and managing macros, you become more valuable to your employer. This translates into better raises and more job security.

SOLIDWORKS Help includes a section specifically about the SOLIDWORKS API, which uses Visual Basic as its foundation.

To access the VBA environment inside of SOLIDWORKS, you simply locate Macro under Tools in the menu.

Codestack.net is a free resource users can access to download some ready-made macros for their own use. I am using some of their code to illustrate how the VBA environment works.

To create a new macro, simply go to the menu and select Tools > Macro > New.

You will be prompted to give your macro a name and browse to where you want to save it. If you are working in a team environment, place all macros in a folder on a network so your team can access the macros.

SOLIDWORKS automatically launches the Microsoft Visual Basic environment. This may look intimidating, especially if you have no experience coding. However, VBA is one of the more user-friendly programming interfaces. In fact, the code automatically fills in as you go.

Let’s start with what is in the right-hand pane.

You can have more than one subroutine in a program, and you can download “code snippets” to use in your macros. You start with a main program, and this is assigned to (General). You can call subroutines or smaller programs from the General program.

The first line is Dim swApp As Object. This means define a variable called swApp. The variable type is an object, meaning a software application.

Normally you define all the variables at the very top of your program or macro. That way, you get all your variables identified. Since you just started the program, VBA automatically created the first variable for you.

You have your first subroutine called “main.” It is announced as Sub main (). You can list any variables inside the parenthesis, but a lot of coders don’t bother with that because if you forget to list a variable inside the parenthesis and later add it or use it, you will get an error.

The last line in the code is End Sub to indicate the end of the subroutine. All code should be written between the words Sub and End Sub. You can write or add snippets outside of Sub and End Sub that might be called by the main subroutine, but they should all start with Sub and then use End Sub to indicate the end of the code.

You see the line Set swApp = Application.SldWorks.

Earlier you defined the variable swApp. Now the variable swApp is assigned to the software application SOLIDWORKS. If you hadn’t assigned the software application, how would the code know which application to use to run the code?

I add another variable CLEAR_PROPERTIES, which is a constant. If I set this to true, then any existing properties in the file will be deleted. If I set this to false, any existing properties will be retained.

As you type, VBA will provide you with a list of possible entries. To accept an entry, simply click the Tab key.

The next line of code means that the code will process all components in an assembly.

You need to define the type of document that will be affected by this code. Because we are working inside the SOLIDWORKS API, SOLIDWORKS provides key terms to be used in any code.

When you define a variable, you need to set what type of variable it will be. In this case, let’s say the file name for the model will be a string, meaning any combination of letters and numbers.

I like to add comments as I code, so that I understand what each line of code is doing. This helps me locate bugs and figure out where I went wrong. To add a comment, start with an apostrophe (‘) and then write the comment. Notice that comments are in green to make it easier to identify and read them.

If you look in the left pane, there are two folders being accessed by the code. Because you are working inside SOLIDWORKS, you automatically have access to all SOLIDWORKS objects, i.e., models, drawings and assemblies. The modules folder lists the code being used.

Click on the Object Browser icon. Select SldWorks from the drop-down list. You will see a list of classes available for use in any macro.

If you select one of the Classes, you will see in the right-hand pane all the available commands or actions associated with that class. For example, you can set a custom property to be editable or lock it so it cannot be changed, or you can get the names for all the custom properties.

You also have the ability to create forms that your users can access to quickly assign values to any variables you designate in your code.

If you want to create a form, you can use the tools in the MsForms Object Browser to add lists, textboxes, etc. to build up the form. I highly recommend referencing a book such as Visual Basic for Dummies to help you get comfortable with creating easy-to-use dialog boxes for your users.

The controls that are included in the SOLIDWORKS VBA are a bit limited. To add a control to the user form, simply drag and drop it onto the palette.

There are also websites with free controls you can use in your forms. For example, if you wanted a list of all the states or countries, there are free controls you can download and then add to your form. That way you don’t have to reinvent the wheel. You can also download a timer control which shows where your macro is in processing a batch of drawings.

You can change the name on the UserForm using the Custom Properties palette. Simply type the desired name in the caption field.

In this example, I have created a simple user form where the user will browse for the text file to be used to import the custom properties and then run the macro.

I want to associate the command button with the Open icon with a specific sub-routine. Simply select the command button, right click and select View Code.

Copy and paste the code snippet, which opens a file dialog box that allows you to search for a CSV file to be used.

Once you have created or downloaded the code you want to use, you will probably want to create a button to add to your SOLIDWORKS ribbon, so you can quickly run the desired macro.

To add a custom tool:

  1. Bring up the Customize dialog.
  2. Open the Commands tab.
  3. Highlight Macro.
  4. Select the New Macro button.

I like to place my macros on the Tools ribbon so that they are easy to locate. Drag the New Macro button to the Tools ribbon.

Click the “…” button to browse for the macro you wish to assign to the button. The method field will automatically fill in with the main subroutine. You can type in a tool tip to help you identify what the macro does.  You can assign any bit map (.bmp) image for your macro.

To create a .bmp image, use any image software such as Windows Paint. Keep in mind that the icon will be very low resolution, so don’t use a lot of detail when creating the bmp file. I used Paint to create this image and simply drew the letters C and P to indicate custom properties.

This is what my macro button looks like.


About the Author

Elise Moss has worked in Silicon Valley for the past thirty years as a designer and mechanical engineer. She is currently traveling the United States with her husband and two horses exploring backroads and historical trails. She is writing about her horse travels on her blog shakespeareantrails.substack.com. Her professional website is www. mossdesigns.com. She hopes to write a book on the SOLIDWORKS API at some point.

Recent Articles

Related Stories

Enews Subscribe