Develop First Solidworks Addin by C#

Develop First Solidworks Addin by C#

Software Platform Configuration

Setup: VS2022 + SolidWorks 2023. Select Class Library with the .NET Framework 4.8.

Create a new project: Open VS2022 → click Create a new project; search for Class Library; select Class Library (.NET Framework); click Next.

Framework: .NET Framework 4.8 (or 4.0)

Delete the default class file. In the Solution Explorer, right-click Class1.cs → Delete; create new classes later as needed.

Create a References folder and copy the DLL files.

Right-click the project → Add → New Folder, name it References. In File Explorer, copy the following four DLLs from SolidWorks’ API\redist directory:

  • SolidWorks.Interop.sldworks.dll
  • SolidWorks.Interop.swpublished.dll
  • SolidWorks.Interop.swconst.dll
  • solidworkstools.dll

Paste them into the project’s References folder. Right-click the References node → Add Reference… Click Browse, then select the three DLLs inside the References folder. Click OK.

Under the References node, select each of the four DLLs one by one. In the Properties window, configure these settings:

PropertyValue
Embed Interop TypesFalse
Copy LocalTrue (set to True for debugging; switch to False for release builds)

System.Windows.Forms reference may be required at times, as Windows-related resources are used.

Writing add-in code.

Create the main add-in class: Right-click the project → Add → Class, name it MainAddin.cs.

Configure project properties.

Right-click the project → Properties.
On the Build tab:
Platform target: Any CPU
Check Register for COM interop.

On the Debug tab:
Start external program: C:\Program Files\SolidWorks Corp\SolidWorks\SLDWORKS.exe

Code Demo:

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyFirstSwAddin
{
    [ComVisible(true)]
    [Guid("YOUR GUID")] // Replace this with your generated unique GUID
    public class MainAddin : ISwAddin
    {
        private ISldWorks _swApp;
        private int _cookie;

        // Event: Establish connection with SolidWorks ========================
        public bool ConnectToSW(object ThisSW, int Cookie)
        {
            _swApp = (ISldWorks)ThisSW;
            _cookie = Cookie;
            _swApp.SendMsgToUser("Add-in loaded successfully!");
            return true;
        }

        // Event: Disconnect from SolidWorks ===================================
        public bool DisconnectFromSW()
        {
            if (_swApp != null)
            {
                _swApp.SendMsgToUser("Add-in unloaded.");
                _swApp = null;
            }
            return true;
        }

        // DLL Registration Function ===========================================
        [ComRegisterFunction]
        public static void RegisterFunction(Type t)
        {
            try
            {
                string guid = "{" + t.GUID.ToString() + "}";
                using (var key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(
                    @"SOFTWARE\SolidWorks\AddIns\" + guid))
                {
                    key.SetValue(null, 1);
                    key.SetValue("Title", "Snail Add-in");
                    key.SetValue("Description", "Snail Add-in: your great design assistant!");
                }

                using (var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
                    @"Software\SolidWorks\AddInsStartup\" + guid))
                {
                    // Set the second parameter to 1 to auto-load the add-in when SolidWorks starts
                    key.SetValue(null, 1, Microsoft.Win32.RegistryValueKind.DWord);
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Registration failed: " + e.Message);
            }
        }

        // DLL Unregistration Function =========================================
        [ComUnregisterFunction]
        public static void UnregisterFunction(Type t)
        {
            try
            {
                string guid = "{" + t.GUID.ToString() + "}";
                Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(
                    @"SOFTWARE\SolidWorks\AddIns\" + guid);
                Microsoft.Win32.Registry.CurrentUser.DeleteSubKeyTree(
                    @"Software\SolidWorks\AddInsStartup\" + guid);
            }
            catch { }
        }
    }
}

1 comment on "Develop First Solidworks Addin by C#"

Leave a comment

Your email address will not be published.