Hide Table of Contents

Creating Menu Commands (C#)

This topic shows how to create a C# add-in that adds menu commands to the context-sensitive menus of File Explorer vault views.

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.
     

  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 another version. See Using .NET Framework for Add-in Applications for more information.
     

  4. Type a project name in Name.
     

  5. Click Browse and navigate to the folder where to create the project.
     

  6. Click OK.
     

  7. Right-click the project name in the Solution Explorer and click Add Reference.
     

    1. Click Browse in the left-side 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. On the Application tab, click Assembly Information.
       

    2. De-select Make assembly COM-Visible.
       

    3. On the Build tab, select Any CPU for Platform target, de-select Prefer 32-bit, and select Register for COM interop.
       

  9. Save the project.

  10.  

  11. Double-click Class1.cs in the Solution Explorer to open the code window.
     

  12. At the top of the code window, type:
     

    using EPDM.Interop.epdm;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;


     

  13. Replace:

    public class Class1

     with:
     

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

    public class Class1 : IEdmAddIn5
     

  14. 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.
     

  15. Implement IEdmAddIn5::GetAddInInfo by adding the following code to Class1:

  16. public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
    {
           //Specify information to display in the add-in's Properties dialog box 
           poInfo.mbsAddInName = "Menu command sample";
           poInfo.mbsCompany = "SOLIDWORKS Corporation";
           poInfo.mbsDescription = "Adds menu command items";
           poInfo.mlAddInVersion = 1;

           //Specify the minimum required version of SOLIDWORKS PDM Professional
           poInfo.mlRequiredVersionMajor = 5;
           poInfo.mlRequiredVersionMinor = 2;
     
           //Register menu commands; SOLIDWORKS PDM Professional passes command IDs, 1000 and 1001, 
           //to IEdmAddIn5::OnCmd to indicate which command the user selects
           poCmdMgr.AddCmd(1000, "First command", (int)EdmMenuFlags.EdmMenu_Nothing, "This is the first command""First command", 0, 99);
           poCmdMgr.AddCmd(1001, "Second command", (int)EdmMenuFlags.EdmMenu_MustHaveSelection, "This is the second command""Second command", 1, 99);
    }

    The flag EdmMenuFlags.EdmMenu_MustHaveSelection means that the second command is only available if the user has selected one or more files or folders.

  17. IEdmAddIn5::OnCmd is called when a menu command is selected by the user. Implement IEdmAddIn5::OnCmd by adding the following code to Class1:
     

  18.       public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
          {
            //Handle the menu command
            {
                    string CommandName = null;
                    if (poCmd.mlCmdID == 1000)
                    {
                        CommandName = "The first command.";
                    }
                    else
                    {
                        CommandName = "The second command.";
                    }
                    //Retrieve the bounds of the array containing the selected files and folders 
                    int index = 0;
                    int last = 0;
                    index = ppoData.GetLowerBound(0);
                    last = ppoData.GetUpperBound(0);
                    string StrID = null;
     
                    //Create a message showing the names and IDs of all selected files and folders 
                    string message = null;
                    message = "You have selected the following files and folders: " + "\r\n";
                    while (index <= last)
                    {
                        if (((EdmCmdData)ppoData.GetValue(index)).mlObjectID1 == 0)
                        {
                            message = message + "Folder: (ID=";
                            StrID = ((EdmCmdData)ppoData.GetValue(index)).mlObjectID2.ToString();
                            message = message + StrID + ") ";
                        }
                        else
                        {
                            message = message + "File: (ID=";
                            StrID = ((EdmCmdData)ppoData.GetValue(index)).mlObjectID1.ToString();
                            message = message + StrID + ") ";
                        }
     
                        message = message + ((EdmCmdData)ppoData.GetValue(index)).mbsStrData1 + "\r\n";
                        index = index + 1;
                    }
     
                    //Display the message
                    EdmVault5 v = default(EdmVault5);
                    v = (EdmVault5)poCmd.mpoVault;
                    v.MsgBox(poCmd.mlParentWnd, message, EdmMBoxType.EdmMbt_OKOnly, CommandName);
            }
          }

  19. Click Build > Build Solution to build the add-in.

  20. Install the add-in 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 click New Add-in.
       
    4. Browse to project_path\project_name\project_name\bin\Debug, click project_name.dll and EPDM.Interop.epdm.dll.
       
    5. Click Open.
       
    6. Click OK.
       
    7. Click OK.


     

  21. Right-click inside a vault view in File Explorer. First command appears in the context-sensitive menu.
     

  22. Right-click a file in the vault view and click Second command. The add-in displays a dialog similar to the following:

 

 

Complete Source Code

// Class1.cs
 

using System;
using System.Collections.Generic;
using System.Text;
using EPDM.Interop.epdm;
using System.Runtime.InteropServices;
 
 
namespace CreateMenuCommand_CSharp
{

    [Guid(""), ComVisible(true)]    // See step 13 above to create the GUID
    public class Class1 : IEdmAddIn5
    {
        #region IEdmAddIn5 Members
 
        public void GetAddInInfo(ref EdmAddInInfo poInfo, IEdmVault5 poVault, IEdmCmdMgr5 poCmdMgr)
        {
             
            poInfo.mbsAddInName = "Menu command sample";
            poInfo.mbsCompany = "SOLIDWORKS Corporation";
            poInfo.mbsDescription = "Adds menu command items";
            poInfo.mlAddInVersion = 1;
            poInfo.mlRequiredVersionMajor = 5;
            poInfo.mlRequiredVersionMinor = 2;
 
            
            poCmdMgr.AddCmd(1000, "First command", (int)EdmMenuFlags.EdmMenu_Nothing, "This is the first command""First command", 0, 99);
            poCmdMgr.AddCmd(1001, "Second command", (int)EdmMenuFlags.EdmMenu_MustHaveSelection, "This is the second command""Second command", 1, 99);
        }
 
        public void OnCmd(ref EdmCmd poCmd, ref EdmCmdData[] ppoData)
        {
            
            {
                string CommandName = null;
                if (poCmd.mlCmdID == 1000)
                {
                    CommandName = "The first command.";
                }
                else
                {
                    CommandName = "The second command.";
                }
                 
                int index = 0;
                int last = 0;
                index = ppoData.GetLowerBound(0);
                last = ppoData.GetUpperBound(0);
                string StrID = null;
 
                 
                string message = null;
                message = "You have selected the following files and folders: " + "\r\n";
                while (index <= last)
                {
                    if (((EdmCmdData)ppoData.GetValue(index)).mlObjectID1 == 0)
                    {
                        message = message + "Folder: (ID=";
                        StrID = ((EdmCmdData)ppoData.GetValue(index)).mlObjectID2.ToString();
                        message = message + StrID + ") ";
                    }
                    else
                    {
                        message = message + "File: (ID=";
                        StrID = ((EdmCmdData)ppoData.GetValue(index)).mlObjectID1.ToString();
                        message = message + StrID + ") ";
                    }
 
                    message = message + ((EdmCmdData)ppoData.GetValue(index)).mbsStrData1 + "\r\n";
                    index = index + 1;
                }
 
                
                EdmVault5 v = default(EdmVault5);
                v = (EdmVault5)poCmd.mpoVault;
                v.MsgBox(poCmd.mlParentWnd, message, EdmMBoxType.EdmMbt_OKOnly, CommandName);
            }
        }
 
        #endregion
    }
}

 



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 Menu Commands (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) 2023 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.