Guide · CAD Automation

SolidWorks Automation

Macros, API and Add-ins — A Complete Guide

Macro, API, or add-in? Which layer solves which job, which language to pick, and where to start — gathered on one page.

SolidWorks APIVBA MacroC# Add-inPythonBatch Processing
In This Article

SolidWorks automation means handing repetitive CAD work to software through the SolidWorks API. It has three layers: VBA macros that can be recorded and live in a single file, the API itself where you address the object model directly, and C#/.NET add-ins that load into SolidWorks and run inside it. All three use the same API; what differs is where the code lives and how it is distributed.

01 / 06

What is SolidWorks automation?

Changing a dimension and updating the drawing, exporting hundreds of files to PDF, filling in custom property fields one at a time — all of it can be done by hand. Automation means having SolidWorks do the same work on command from code. The thing that carries that command is the SolidWorks API.

The API is the layer that lets you reach every object inside SolidWorks — application, document, feature, dimension, configuration — from code. Macros and add-ins both use this same layer; the difference is not API access but how the code is stored and shipped.

02 / 06

Macro, API or add-in? A decision table

They are not alternatives to each other; you pick by the scale of the job.

VBA MacroC# / .NET Add-inPython (COM)
Where it runsInside SolidWorks, a .swp fileA DLL loaded into SolidWorksOutside, as its own process
InstallationHand over the file and run itNeeds registration and an installerPython plus pywin32
User interfaceSimple formCommandManager, PropertyManager tabNone, or your own
Use it whenOne desk, quick jobTeam-wide, versioned productBatch work, data processing, integrations
MaintenanceGets harder as files multiplyVersion control and tests are possibleEasy at script level

A practical rule: if the job stays on one person's desk and runs a few times a week, a macro is enough. Once the same job spreads across a team, needs a user interface, and has to be versioned, it is time to move to an add-in. If you will process data outside SolidWorks — generating hundreds of variants from an Excel list, say — Python is usually the shortest path.

03 / 06

How do you write a SolidWorks macro? A learning order

The order matters: each step builds on the one before it.

  1. Record with the Macro RecorderDo the job by hand and record it. The generated code is messy, but it shows you which interfaces get called — it is the fastest way to search the API.
  2. Read the object modelSldWorks → ModelDoc2 → document type → Feature / Configuration / Dimension. You cannot skip a step in that chain; internalising the hierarchy matters more than memorising methods.
  3. Throw away selection-based codeA recorded macro reaches objects through whatever the user had selected, and that is brittle. Move to code that reaches objects by name or by identity.
  4. Add error handlingClosed file, missing configuration, renamed feature — a macro meets all of these in production. Do not swallow errors silently; report them.
  5. Bind it to a button and a shortcutA macro that is hard to launch does not get used. Add a toolbar button or assign a keyboard shortcut.
  6. Move to an add-in when you ship itOnce the same macro exists in three versions on three machines, it is a product. A C# add-in solves versioning and installation.
04 / 06

What can you automate in SolidWorks?

  • Building a complete assembly and its configurations from entered dimensions
  • Generating drawing sheets, views and dimensioning automatically
  • Packaging PDF, DWG, DXF and flat-pattern output in one command with standard naming
  • Filling custom property fields consistently with the bill of materials
  • Batch saving, converting and reporting across hundreds of files in a folder
  • Checking design rules: missing properties, wrong material, non-standard dimensions

The most direct gain is eliminating the repeated modelling of the same work. The second — often more valuable — gain is standardisation: when output naming, folder structure and revision numbering are part of the automation, the files that reach production no longer vary by whoever made them.

05 / 06

How is AI changing SolidWorks automation?

06 / 06

From macro to product: how to set the project up

Frequently Asked

What is a SolidWorks macro?

A SolidWorks macro is a VBA program that runs inside SolidWorks and automates CAD work through the API. It can be recorded with the Macro Recorder or written by hand, lives in a single .swp file, and runs without any installation.

What is the SolidWorks API?

The SolidWorks API is the COM-based interface layer that lets you reach the SolidWorks object model from code. You start at the SldWorks.Application root and travel through ModelDoc2, PartDoc, AssemblyDoc and DrawingDoc to features, dimensions and configurations. Macros and add-ins both use this same API.

What is the difference between a SolidWorks macro and an add-in?

Both use the same API; the difference is distribution. A macro is a single file, needs no installation, and is fast for one user. An add-in is a DLL loaded into SolidWorks: it can create its own toolbar and PropertyManager tab, can be versioned, and is deployed team-wide through an installer.

Which language should I learn for SolidWorks automation?

VBA to begin with, because the Macro Recorder emits VBA directly and the object model you learn carries over to every other language. C# / .NET if you are building a product for a team. Python with pywin32 is usually the shortest path when you are processing data outside SolidWorks or doing batch work.

How do you run a SolidWorks macro?

Through Tools → Macro → Run, selecting the .swp file. For macros you use often, Tools → Customize lets you add a toolbar button or assign a keyboard shortcut, so the macro runs in one click instead of being hunted for in a menu.

Why is my SolidWorks macro not working?

Three common causes: the recorded macro produced selection-based code and the object is no longer selected; ModelDoc2 was accessed with no open document; or the referenced feature has been renamed. The fix for all three is the same — reach objects by name or identity rather than by selection.

How It Looks in Production