Simplify Your Life With Simple Macros

Quite often when the word ‘automation’ comes up in CAD discussions, we think of elaborate equations and programs that allow a user to enter in a few values and a factory is generated. While that actuality is quite real and expensive, what is overlooked is the power of the SolidWorks macro to automate repetitive tasks. Simple tasks one does throughout the day that may take a minute or two here or there can add up to a fair bit of time (and tedium) over a period of weeks or months. This article provides an example of a simple macro that might be useful to prepare documents generated through a ‘save as’ command.

[Disclaimer: The source code provided in this example is for demonstration purposes only. DASI Solutions claims no liability to your data due to use or misuse of the information provided. While experimenting or developing macros for use in your environment you should never operate with live or production data. We at DASI Solutions encourage you to use the macro/API functionality of SolidWorks, but remember to keep your data safe and backed up. This blog article assumes a little knowledge of programming and hardly scratches the surface of what is possible with SolidWorks macro/API functionality. There are plenty of fine reference books available in the market to help with learning and understanding VisualBasic, C#, and C++.]

Clean Up Custom Properties
The problem we are trying to solve is that when we save a copy of a file that has all of its custom property information filled out, the new file has custom property data that matches the first. Sometimes data might not get updated in a proper manner and erroneous data might result for this new document.

The first step of designing a macro is to determine what we need the macro to do. Here’s a simple approach:

  1. Connect to SolidWorks
  2. Connect to the active document
  3. Get a list of all FILE properties
  4. Update the file properties to a hyphen (or other default value)
  5. Get a list of all configurations
  6. Get a list of properties for EACH configuration
  7. Update the properties in EACH configuration to a hyphen (or other default value)
Results in something like this (numbered circles coordinate with steps at left):

Source Code For Macro

Source Code for Macro (click to enlarge)

To begin a new macro, click Tools -> Macro -> New …

How to create a new macro.

Create a New Macro

Relate the numbers below with Source Code for Macro and follow along.
0. This section sets up the declarative items for our code. It lets the program know which types of objects we want to talk to in SolidWorks. Here we have the application, a model, and a custom property manager.

  1. Have the macro get the SolidWorks application that initiated the macro.
  2. Have the macro connect to the active document from the application we just connected with.
  3. The macro will use this in two different ways. Here we are telling the custom property manager to connect with the current model’s custom property manager relating to an empty (“”) configuration. This means the current model’s FILE properties.
  4. We want to make sure we have custom properties to update, so we verify that the count is greater than 0 (‘If oCustPropMgr.Count > 0 then’). If we have custom properties to interact with, then get a list of them (‘vPropertyNames = oCustPropMgr.GetNames’). The next section is a for … next loop. Every custom property in our list will have an index associated with it, starting with 0 to the maximum (UBOUND = upper bound) of the list.
  5. Property List (as an array)

    Property List (as an array)

    The section where we update the custom property references the list above such that vPropertyNames(0) = “DrawnBy”, vPropertyNames(1) = “DrawnDate”, etc. In the end, the command will be oCustPropMgr.Set “DrawnBy”, “-“ after the string substitutions have occurred, setting the value for the FILE custom property of DrawnBy to a hyphen! Loops are an essential process to programming in any programming language and are very powerful once you are used to them.

  6. In this step, we’re getting the names of all the configurations associated with this model (vConfigNames = oModel.GetConfigurationNames).Steps 6, 7, and 8 are very similar to steps 3 and 4 with one more added step of complexity. In step 3, we retrieved a list of file custom properties and in step 4, we updated each property. Here, we need to do the same step for the custom properties IN EACH CONFIGURATION. So, we’re looping within a loop! The first loop processes each configuration and the loop within that loop will process all of the custom properties for that configuration!
  7. Begin the outer loop of cycling through each configuration.
  8. Tell the custom property manager to connect with the current model’s custom property manager relating to the specified configuration. Since it is not “” this time, it’s looking for a specific name, like “Default” or “Long”.
  9. This step is identical to step 4. Loop through each property and set the value to hyphen.

That’s it! After you run this macro, you should see the following result:

Comparison of properties.

Comparison of Properties (click to enlarge

Furthermore you might wish to expand upon this code to…
(in order of increasing difficulty)

  1. … update ‘DrawnDate’ to today’s date instead of a hyphen.
  2. … update ‘DrawnBy’ with the user’s computer login, or a defined name.
  3. … update ‘PartNo’ with the next logical part number for your company.
  4. … verify that the custom properties are filled out to your company’s standards.

Where can I get more information about SolidWorks API?
Right within SolidWorks! Under the help menu, select ‘API Help’ or from the SolidWorks Corporate API website at

How to get to API help.

API Help

Interested in learning more about customizing SolidWorks?
Request a class in “SolidWorks API Fundamentals” for advanced training by a certified SolidWorks trainer at DASI Solutions.

Until next time…

Royce Cole

Royce Cole
DASI Solutions

This entry was posted in Tips & Tricks and tagged , , , , , . Bookmark the permalink.