Building a SolidWorks Add-in from Scratch in C#
The complete guide to creating a SolidWorks add-in — COM registration, ISwAddin interface, command groups, TaskPane UI, event callbacks, and deployment to other machines.
Why Build an Add-in?
Macros and standalone apps are fine for simple automation. But add-ins run inside the SolidWorks process, giving you:
- Speed: No COM marshaling overhead. Direct access to the API objects.
- UI integration: Add menus, toolbar buttons, and TaskPane panels that look native.
- Event handling: React to document open, save, rebuild, selection changes, and more.
- Persistent state: The add-in stays loaded for the entire SolidWorks session.
Step 1: Project Setup
Create a Class Library (.NET Framework) project. Add-ins must target .NET Framework (4.7.2+), not .NET 5+.
Add references to:
- SolidWorks.Interop.sldworks
- SolidWorks.Interop.swconst
- SolidWorks.Interop.swpublished (contains ISwAddin interface)
Mark the assembly as COM-visible in project properties, and add a strong name key.
Step 2: Implement ISwAddin
[ComVisible(true)]
[Guid("YOUR-GUID-HERE")]
public class MyAddin : ISwAddin
{
private SldWorks _swApp;
private int _addinCookie;
public bool ConnectToSW(object thisSW, int cookie)
{
_swApp = (SldWorks)thisSW;
_addinCookie = cookie;
_swApp.SetAddinCallbackInfo2(0, this, cookie);
// Create command groups, TaskPanes, register events
return true;
}
public bool DisconnectFromSW()
{
// Cleanup: remove commands, release COM objects
return true;
}
}Step 3: Register the Add-in
SolidWorks discovers add-ins through the Windows registry. Add these registration attributes:
[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
var key = Registry.LocalMachine.CreateSubKey(
@"SOFTWARE\SolidWorks\Addins\{" + t.GUID + "}");
key.SetValue(null, 0); // 0 = not loaded at startup
key.SetValue("Title", "My Add-in");
key.SetValue("Description", "My custom add-in");
}Then run regasm:
regasm /codebase MyAddin.dllStep 4: Add Commands and Menus
In ConnectToSW, create a command group with buttons:
var cmdGroup = _swApp.GetCommandManager(_addinCookie)
.CreateCommandGroup2(0, "My Tools", "My custom tools",
"Tooltip", -1, true, ref errors);
cmdGroup.AddCommandItem2("Run Tool",
-1, "Run my tool", "Run my tool",
0, "OnRunTool", "CanRunTool",
0, (int)swCommandItemType_e.swToolbarItem);
cmdGroup.Activate();The callback strings ("OnRunTool", "CanRunTool") are method names on your add-in class that SolidWorks will call via reflection.
Step 5: Add a TaskPane
For a rich UI panel, create a WPF UserControl and host it in a TaskPane:
var taskPane = _swApp.CreateTaskpaneView3(
iconPath, // 16x16 and 20x20 bitmaps
"My Panel");
taskPane.AddControl(
"MyAddin.MyUserControl",
"");The UserControl must be COM-visible with its own GUID.
Step 6: Handle Events
Register for document events in ConnectToSW:
_swApp.ActiveDocChangeNotify += OnActiveDocChanged;
_swApp.FileOpenPostNotify += OnFileOpened;For document-specific events (rebuild, save, selection), register on each ModelDoc2:
((ModelDoc2)doc).DestroyNotify2 += OnDocClosing;Debugging
- Build the project, run regasm, then start SolidWorks
- In Visual Studio: Debug > Attach to Process > select SLDWORKS.exe
- Set breakpoints in your code — they'll hit when your add-in methods are called
- Use Debug.Print or a log file for output (Console.WriteLine doesn't show in SolidWorks)
Deployment
To install on another machine:
- Copy the DLL and all dependencies to a folder
- Run regasm /codebase on the target machine (requires admin rights)
- SolidWorks will discover the add-in at next startup
For production deployment, create an MSI installer that handles regasm and file copying automatically.