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 Windows Explorer vault views.

NOTE: Because SOLIDWORKS Enterprise PDM 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 2010.
     

  2. Click File > New > Project > Visual C# > Class Library.
     

  3. Select .NET Framework 2.0 in the dropdown at the top of the dialog.
     

  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 COM in the left-side panel, click PDMWorks Enterprise nnnn Type Library, and click Add.
       

    2. Click Assemblies > Framework in the left-side panel, click Microsoft.VisualBasic,  and click Add.
       

    3. Click Close.
       

  8. Right-click the project name in the Solution Explorer and click Properties.
     

    1. Click Application > Assembly Information.
       

    2. Click Make assembly COM-Visible.
       

    3. Click OK.

  9.  

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

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

    using EdmLib;
    using Microsoft.VisualBasic;

     

  12. Replace:

    public class Class1

     with:
     

    public class Class1 : IEdmAddIn5
     

  13. In the code, right-click IEdmAddIn5 and click Implement Interface > Implement Interface.
     

  14. Replace the add-in's implementation of IEdmAddIn5::GetAddInInfo with the following code:

  15. 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 Enterprise PDM
           poInfo.mlRequiredVersionMajor = 5;
           poInfo.mlRequiredVersionMinor = 2;
     
           //Register menu commands; SOLIDWORKS Enterprise PDM 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.

  16. IEdmAddIn5::OnCmd is called when a menu command is selected by the user. Replace the add-in's implementation of IEdmAddIn5::OnCmd with the following code:
     

  17.       public void OnCmd(ref EdmCmd poCmd, ref Array 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 = Information.LBound(ppoData);
                    last = Information.UBound(ppoData);
                    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: " + Constants.vbLf;
                    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 + Constants.vbLf;
                        index = index + 1;
                    }
     
                    //Display the message
                    EdmVault5 v = default(EdmVault5);
                    v = (EdmVault5)poCmd.mpoVault;
                    v.MsgBox(poCmd.mlParentWnd, message, EdmMBoxType.EdmMbt_OKOnly, CommandName);
            }
          }

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

  19. Install the add-in through the SOLIDWORKS Enterprise PDM Administration tool:
     
    1. Open the SOLIDWORKS Enterprise PDM 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 Interop.EdmLib.dll.
       
    5. Click Open.
       
    6. Click OK.
       
    7. Click OK.


     

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

  21. 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 EdmLib;
using Microsoft.VisualBasic;
 
 
namespace CreateMenuCommand_CSharp
{
    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 Array ppoData)
        {
            
            {
                string CommandName = null;
                if (poCmd.mlCmdID == 1000)
                {
                    CommandName = "The first command.";
                }
                else
                {
                    CommandName = "The second command.";
                }
                 
                int index = 0;
                int last = 0;
                index = Information.LBound(ppoData);
                last = Information.UBound(ppoData);
                string StrID = null;
 
                 
                string message = null;
                message = "You have selected the following files and folders: " + Constants.vbLf;
                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 + Constants.vbLf;
                    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) 2015 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.