Hide Table of Contents

This topic shows how to create a debug add-in using Visual C# in Microsoft Visual Studio.

NOTE: Because SOLIDWORKS PDM Professional cannot force a reload of add-ins if they are written in .NET, all client machines must be restarted to ensure that the latest version of the add-in is used.

  1. Start Microsoft Visual Studio or another version that supports debugging.
     
  2. Click File > New > Project > Visual C# > Windows Desktop > Class Library (.NET Framework).
     
  3. In the .NET Framework dropdown at the top of the New Project dialog, keep the default version (recommended) or select .NET Framework 4.5 or later. See Using .NET Framework for Add-in Applications for more information.
     
  4. Type the name of your project in Name.
     
  5. Click Browse and navigate to the folder where to create your project.
     
  6. Click OK.

    Class1.cs containing empty class, Class1, is created.
     
  7. Right-click the project name in the Solution Explorer and click Add > Reference.
     
    1. Click Browse in the left-hand panel, navigate to and select EPDM.Interop.epdm.dll, and click OK.
    2. Right-click Epdm.Interop.epdm in the Solution Explorer, select Properties, and set Embed Interop Types to False to handle methods that pass arrays of structures.
       
    3. Click Assemblies > Framework in the left-hand panel, select System.Windows.Forms, and click OK.
       
    4. Click Close.
  8. Right-click the project name in the Solution Explorer and click Properties.
     
    1. Click Application > Assembly Information.
       
    2. De-select Make assembly COM-Visible.
       
    3. On the Build tab, select AnyCPU for the target platform, de-select Prefer 32-bit and select Register for COM interop.
  9. Make this a Debug Add-in:
     
    1. Click the Debug tab.
       
    2. Click Start external program and type C:\Windows\System32\notepad.exe in the text field.

     

  10. If creating this add-in on a 64-bit computer, edit project_path\project_name\project_name\project_name.csproj in Notepad:
     
    1. Insert the following line below <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> and below  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">.

      <PlatformTarget>AnyCPU</PlatformTarget>
       
    2. Save the file and exit Notepad.
     
  11. Double-click Class1.cs in the Solution Explorer to open the code window.
     
  12. At the top of the code window, type:

    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using EPDM.Interop.epdm;
     
  13. Replace:

  14. public class Class1

     with:


    [Guid(""), ComVisible(true)]

    public class Class1 : IEdmAddIn5

  15. To populate the GUID attribute above, click Tools > Create GUID in the IDE, select GUID Format 5, click Copy, and click Exit. Populate [Guid(""), ...] with the copied string.
     
  16. Implement IEdmAddIn5::GetAddInInfo by adding the following to Class1:
     

        public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
        {
            //Specify information to display in the add-in's Properties dialog box
            poInfo.mbsAddInName = "C# Add-in";
            poInfo.mbsCompany = "My Company";
            poInfo.mbsDescription = "Menu add-in that shows a message box.";
            poInfo.mlAddInVersion = 1;
     
            //Specify the minimum required version of SolidWorks PDM Professional
            poInfo.mlRequiredVersionMajor = 6;
            poInfo.mlRequiredVersionMinor = 4;
     
            // Register a menu command
            poCmdMgr.AddCmd(1, "C# Add-in", (int)EdmMenuFlags.EdmMenu_Nothing);
     
        }

  17. Implement IEdmAddIn5::OnCmd by adding the following to Class1:
  18. public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
    {
     
            // Handle the menu command
            if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
            {
                if (poCmd.mlCmdID == 1) {
                    System.Windows.Forms.MessageBox.Show("C# Add-in");
                }
            }
     }

  19. Implement your own window handle wrapper by right-clicking the project name in the Solution Explorer and clicking Add > Class:
     
    1. Type WindowHandle.cs in Name.
       
    2. Click Add.
       
    3. Replace the code in the code window with the following code.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
     
    namespace Addin_CSharp
    {
        //Wrapper class to use SOLIDWORKS PDM Professional as the parent window
        class WindowHandle : IWin32Window
        {
            private IntPtr mHwnd;
     
            public WindowHandle(int hWnd)
            {
                mHwnd = new IntPtr(hWnd);
            }
            public IntPtr Handle
            {
                get { return mHwnd; }
            }
        }
    }

    Your add-in uses the new wrapper in the menu command handler to show the message box by calling System.Windows.Forms.MessageBox.Show in Class1::OnCmd in Class1.cs.

  20. Click Build > Build Solution to build the add-in.
     
  21. Install the add-in for debugging through the SOLIDWORKS PDM Professional Administration tool:
     
    1. Open the SOLIDWORKS PDM Professional Administration tool.
       
    2. Expand the vault where you want to install this add-in and log in as Admin.
       
    3. Right-click Add-ins and select Debug Add-ins.
       
    4. Click Add Add-in, browse to project_path\project_name\project_name\bin\Debug, click project_name.dll, and click Open.

      Your add-in's name, path, and class ID should appear in the Add-ins installed for debugging on this machine list.
       
    5. Click OK.
       
  22. In Microsoft Visual Studio, click Debug > Start Debugging or press F5.
     
  23. Open Notepad and click File > Open.
     
  24. In the Open dialog, click the name of the vault where you installed this add-in.
     
  25. Right-click inside the vault view and click C# Add-in.

    Displays the message, C# Add-in.
     
  26. Click OK to close the message box.
     
  27. Click Cancel.
     
  28. Close Notepad.

    Complete Source Code

    //Class1.cs


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using EPDM.Interop.epdm;
     
    [Guid(""), ComVisible(true)]    // See step 14 above to create the GUID
    public class Class1 : IEdmAddIn5
    {
     
        public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
        {
            
            poInfo.mbsAddInName = "C# Add-in";
            poInfo.mbsCompany = "My Company";
            poInfo.mbsDescription = "Menu add-in that shows a message box.";
            poInfo.mlAddInVersion = 1;

            poInfo.mlRequiredVersionMajor = 6;
            poInfo.mlRequiredVersionMinor = 4;
     
            poCmdMgr.AddCmd(1, "C# Add-in", (int)EdmMenuFlags.EdmMenu_Nothing);
     
        }
     

        public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
        {
             
            if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
            {
                if (poCmd.mlCmdID == 1)
                {
                    System.Windows.Forms.MessageBox.Show("C# Add-in");
                }
            }
        }
    }

    //WindowHandle.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
     
    namespace Addin_CSharp
    {
        class WindowHandle : IWin32Window
        {
            private IntPtr mHwnd;
     
            public WindowHandle(int hWnd)
            {
                mHwnd = new IntPtr(hWnd);
            }
            public IntPtr Handle
            {
                get { return mHwnd; }
            }
        }
    }



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Creating Add-ins (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2021 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.