Search

SOLIDWORKS Macros: comprehensive guide


If you are SOLIDWORKS user, most likely you know about SOLIDWORKS macros. SOLIDWORKS macros are great automation tools which enable running the code utilising SOLIDWORKS API.

If you are not a developer you can still successfully use macros in your day-to-day job as there are thousands of them available in the internet.

Automatically Run Macro On Model Open

Since I have published the guide to Automatically Run Macro On Rebuild I was asked several times of how to run the macro every time the document opens.


You might want to do this to update your SOLIDWORKS custom properties (e.g. from external text file), add some log entries, update tree etc.

Of course you can do this with SOLIDWORKS add-ins, but what if it is required to easily share the model with colleagues or customers and add-in might not be an option? In this blog article I will describe two ways to do this directly from VBA macros without add-ins or any additional software.

How To Call Functions Of SOLIDWORKS add-in


SOLIDWORKS add-ins are great options for automating SOLIDWORKS. But what if the add-in itself needs to be automated? This enables a whole new functionality as now add-in functions can be called from macros, add-insstand-alone applications or even scripts. So effectively the add-in is exposing API itself.

Storing Data In SOLIDWORKS 3rd Party Storage And Store

When developing SOLIDWORKS add-ins it might be required to store custom data in the model. This might include storing of custom options, attributes or additional model tree (e.g. electrical, inspection etc.)

SOLIDWORKS enables storing the data directly within main model stream. Which makes it easy to serialise and deserialise different data using popular frameworks such as Binary Serialization, Xml Serialization, Data Contract Serialisation, JSON Serialisation etc. This approach provides the most performance efficient and simple way of working with data.

Data can be stored into 2 main components: 3rd Party Storage and 3rd Party Storage Store.

Basic Concepts of Automating Drawings Using SOLIDWORKS API

Automating drawings using SOLIDWORKS API is one of the most common tasks when it comes to macro development. This is however one of the most challenging sections of development for SOLIDWORKS.

Usually the macros for drawing would

  • Automatically add dimensions to views
  • Insert and optimise the view locations
  • Insert annotations, tables and balloons
  • Create sketch segments and points (additional drawing symbols)

In this blog post I will introduce you to the basic concepts of SOLIDWORKS API for drawings.

Understanding API Object Model

SOLIDWORKS API object model contains hundreds of interfaces and thousands of methods and properties. When starting developing applications or macros for SOLIDWORKS it might be confusions to understand all the relationships to find the correct route of retrieving the pointer to the required object.

Usually the pointer to the root object (ISldWorks) is provided (either via calling the Application.SldWorks property in the macro or from the app parameter in the ConnectToSw function).

SOLIDWORKS API Command Doesn't Exist? Need plan B


SOLIDWORKS API is a powerful toolset covering majority of SOLIDWORKS functions. However in some cases certain APIs are not available. This could potentially put the whole development project at risk as workaround is not always possible.

In this post I will discuss alternative solutions of calling SOLIDWORKS commands when API cannot be used (either not available or faulty).