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:
||Results in something like this (numbered circles coordinate with steps at left):|
To begin a new macro, click Tools -> Macro -> New …
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.
- Have the macro get the SolidWorks application that initiated the macro.
- Have the macro connect to the active document from the application we just connected with.
- 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.
- 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.
- 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!
- Begin the outer loop of cycling through each configuration.
- 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”.
- This step is identical to step 4. Loop through each property and set the value to hyphen.
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.
That’s it! After you run this macro, you should see the following result:
Furthermore you might wish to expand upon this code to…
(in order of increasing difficulty)
- … update ‘DrawnDate’ to today’s date instead of a hyphen.
- … update ‘DrawnBy’ with the user’s computer login, or a defined name.
- … update ‘PartNo’ with the next logical part number for your company.
- … 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 http://www.solidworks.com/sw/support/apisupport.htm.
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…