Getting Started with the SolidWorks API
A beginner-friendly introduction to SolidWorks API development using C# and .NET. Covers setting up your project, connecting to SolidWorks, and automating your first task.
Prerequisites
Before you start, you'll need:
- SolidWorks installed (any recent version — 2020+)
- Visual Studio 2022 (Community edition is free)
- Basic familiarity with C# and .NET
Step 1: Create a Console Application
Open Visual Studio and create a new Console App (.NET Framework) project. The SolidWorks API uses COM Interop, which works best with .NET Framework (not .NET Core/5+).
Step 2: Add References
Right-click References in Solution Explorer and add:
- SolidWorks.Interop.sldworks — the main API library
- SolidWorks.Interop.swconst — enumerations and constants
These DLLs are typically found in the SolidWorks installation folder (e.g., C:\Program Files\SolidWorks Corp\SolidWorks).
Step 3: Connect to SolidWorks
var swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
var model = (ModelDoc2)swApp.ActiveDoc;
Console.WriteLine($"Active document: {model.GetTitle()}");This connects to an already-running SolidWorks instance and gets the active document. Make sure SolidWorks is open with a file loaded before running your code.
Step 4: Do Something Useful
Let's read all custom properties from the active document:
var ext = model.Extension;
var mgr = ext.CustomPropertyManager[""];
object names, types, values, resolved;
mgr.GetAll3(ref names, ref types, ref values, ref resolved);
var nameArray = (string[])names;
var valueArray = (string[])resolved;
for (int i = 0; i < nameArray.Length; i++)
Console.WriteLine($"{nameArray[i]} = {valueArray[i]}");Common Pitfalls
- COM threading: The SolidWorks API is STA (Single-Threaded Apartment). Mark your Main method with [STAThread]
- Null references: Many API calls return null instead of throwing exceptions — always check return values
- Version differences: Some methods are only available in newer API versions. Check the API help file for version notes
- Memory leaks: Release COM objects with Marshal.ReleaseComObject() when done, especially in loops
Next Steps
Once you're comfortable with the basics, explore:
- Stand-alone applications vs. add-ins (add-ins run inside SolidWorks for better performance)
- Feature traversal — walking the FeatureManager tree
- Drawing automation — creating views, dimensions, and annotations programmatically
- PDM API — interacting with SolidWorks PDM vault files