• solidworks
  • c#
  • api
  • automation

SolidWorks API C# Example: Your First Standalone Program

Vikraman N3 min read

VBA macros live inside SolidWorks. A C# program lives outside it: a normal Windows application that connects to SolidWorks, drives it, and can be given to other engineers as an .exe. This example does three useful things in one short program — connect, list every feature of the open part, and change a dimension — and explains the two lines that trip up every beginner.

1. Set up the project (once)

  1. In Visual Studio, create a Console App (.NET Framework) project — the SolidWorks API is COM, and .NET Framework 4.x is the supported route for a standalone program. Set the platform target to x64 (SolidWorks is 64-bit).
  2. Add references: Project → Add Reference → COM (or browse to the SolidWorks install folder, api\redist) and pick SolidWorks.Interop.sldworks and SolidWorks.Interop.swconst. Set Embed Interop Types to False on both.
  3. Start SolidWorks and open any part with a few features before you run the program.

2. The program

using System;
using System.Runtime.InteropServices;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

class Program
{
    static void Main()
    {
        // 1. Connect to the running SolidWorks (starts one if none is running)
        SldWorks swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
        swApp.Visible = true;

        // 2. The open document must be a part
        ModelDoc2 model = (ModelDoc2)swApp.ActiveDoc;
        if (model == null || model.GetType() != (int)swDocumentTypes_e.swDocPART)
        {
            Console.WriteLine("Open a part first.");
            return;
        }
        Console.WriteLine("Part: " + model.GetTitle());

        // 3. Walk the feature tree
        Feature feat = (Feature)model.FirstFeature();
        while (feat != null)
        {
            Console.WriteLine($"{feat.GetTypeName2(),-22} {feat.Name}");
            feat = (Feature)feat.GetNextFeature();
        }

        // 4. Change a dimension by its full name and rebuild
        //    "D1@Sketch1" is the first dimension of Sketch1; use the name shown when you double-click the dimension
        Dimension dim = (Dimension)model.Parameter("D1@Sketch1");
        if (dim != null)
        {
            double before = dim.SystemValue;          // metres
            dim.SystemValue = before * 1.5;           // 50 % bigger
            model.EditRebuild3();
            Console.WriteLine($"D1@Sketch1: {before * 1000:0.##} mm -> {dim.SystemValue * 1000:0.##} mm");
        }
        else
        {
            Console.WriteLine("No dimension named D1@Sketch1 in this part.");
        }
    }
}

Run it with SolidWorks open: the console prints the part's name, every feature with its type (ProfileFeature, Extrusion, Fillet…), and the dimension before and after. Switch to SolidWorks and the part has already rebuilt.

3. The two lines that matter

Marshal.GetActiveObject("SldWorks.Application") attaches to the SolidWorks that is already running. To start SolidWorks instead, use Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) — the course covers when to use which (a batch tool that runs overnight starts its own; an engineer's helper attaches to the one on screen).

dim.SystemValue is in metres, like everything in the SolidWorks API. dim.Value returns the document's display units instead. Mixing the two is the C# version of the millimetres-vs-metres bug every macro writer meets.

4. From one dimension to a configurator

The same Parameter(...)/SystemValue pair, in a loop over a table of sizes, with model.SaveAs3(...) at the end of each pass, is a part configurator: one program, a hundred variants. Add swApp.OpenDoc6(...) to open each file from a folder and you have batch processing. Add a Windows Form and it becomes an add-in-style tool. That path — object model, feature tree, dimensions, batch, add-in — is the second half of the SolidWorks API & Automation Course; the SolidWorks API + C# course adds the C# foundations for engineers who have not programmed before.

Common errors

ErrorCauseFix
COMException: Operation unavailable on GetActiveObjectSolidWorks is not running (or runs as a different user / elevation)Start SolidWorks first, or use CreateInstance
BadImageFormatExceptionProject built as x86Platform target x64
model.Parameter(...) returns nullDimension name differs (D2@Sketch1, D1@Boss-Extrude1)Double-click the dimension in SolidWorks to read its full name
Values look 1000× offValue vs SystemValueUse SystemValue (metres) and convert at the edges