7 Replies Latest reply on Apr 3, 2017 12:46 PM by Tim Webb

    Getting Variable Values from Menu Command C#

    Sean Donnelly

      This code is in C#. I am still learning and am very new. I am trying to work with the examples in the EPDM examples on the help website, but still can't quite figure out how to actually tell it to use the file I selected. I know I need to do something between my EnumeratorVariable vars and the vars.GetVar, but don't understand the connection between the two. The standalone makes sense to me, but not the PDM task method. Can anybody help me? If you were feeling generous perhaps you could also help with my next step: right click menu command on selected file executes a template with variables filled in already from the file you selected. One step at a time though...

       

       

      public void OnCmd(ref EdmCmd poCmd, ref Array ppoData)

      {

      //Handle the menu command

      {

      string Config = "";

      EdmVault5 vault = default(EdmVault5);

      vault = (EdmVault5)poCmd.mpoVault;

       

      string message = "File created by: ";

      object CreatedBy = "";

       

      IEdmEnumeratorVariable5 vars = default(IEdmEnumeratorVariable5);

      vars.GetVar("CreatedBy",Config, out CreatedBy);

      message += CreatedBy;

       

      vault.MsgBox(poCmd.mlParentWnd, message);

      }

      }

              #endregion

      }

      }

        • Re: Getting Variable Values from Menu Command C#
          Tim Webb

          Hey Sean,

          Search for the EdmCmdData structure in the API help. It can shed some light on the structure your add-in will receive when it's called.

           

          Here's a briefing:

          EdmCmdType.EdmCmd_Menu

          The user has activated a menu item or toolbar button that your add-in has added. This command returns a combination of refresh flags in the mlEdmRefreshFlags member of the EdmCmd structure.

           

           

           

           

          EdmCmdData Members

          Type

          Description

          mlObjectID1

          integer

          ID of file; 0 if a folder is selected

          mlObjectID2

          integer

          ID of folder; 0 if a file is selected

          mlObjectID3

          integer

          ID of parent folder of the selected file or folder

          mbsStrData1

          string

          Name of file or folder, not the full path

           

          Here's a good start for you:

          1. First, in your GetAddInInfo section, you need to make sure you have a hook (i.e datacard button press, menu selected, post state change, etc.) so when the OnCmd is called, your addin is called. Set your menu command to have an ID of 100 (but it can be any number) and only allow a single file to be selected.
                   //Register hooks
                   //Register a menu command
                   poCmdMgr.AddCmd(100, "C# Add-in", (int)EdmMenuFlags.EdmMenu_OnlySingleSelection);
          2. Second, after registering this menu command, you will receive all the menu calls in PDM so you need to check to ensure your addin is responding ONLY to the hook you expected to be called by with menu command ID = 100. In the code below, it's making sure it's a menu command AND the menu ID is 100, so you know it's your add-in that needs to respond. Then for a menu command, using the EdmCmdData structure guide above, get the mlObjectID (File ID) and mbsStrData1 (file name).
            public void OnCmd(ref EdmCmd poCmd, ref System.Array ppoData)
                {
                    if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
                    {
                        if (poCmd.mlCmdID == 100)
                        {
                                if (((EdmCmdData)ppoData.GetValue(index)).mlObjectID1 != 0)
                                {
                                    message = message + "File: (ID=";
                                    StrID = ((EdmCmdData)ppoData.GetValue(0)).mlObjectID1.ToString();
                                    message = message + StrID + ") ";
                                }
                                message = message + ((EdmCmdData)ppoData.GetValue(0)).mbsStrData1 + Constants.vbLf;
                        }
                    }
                }

          Check the brackets in my code, I wrote this without a code editor.

           

          Hope this helps,

          Tim CEPA

          Believe in The Q!

            • Re: Getting Variable Values from Menu Command C#
              Tim Webb

              I'm back at my desk now. Try this, I think it might be a little closer to what you need and without the errors. LOL

               

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using EdmLib;
              using Microsoft.VisualBasic;
              using System.Windows.Forms;
              
              
              namespace ClassLibrary2
              {
                  public class Class1 : IEdmAddIn5
              
              
                  {
                      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 = 10;
                          poInfo.mlRequiredVersionMinor = 0;
              
              
                          //Register a menu command
                          poCmdMgr.AddCmd(100, "C# Add-in", (int)EdmMenuFlags.EdmMenu_OnlySingleSelection);
                      }
              
              
                      public void OnCmd(ref EdmCmd poCmd, ref Array ppoData)
                      {
                          if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu)
                          {
                              if (poCmd.mlCmdID == 100)
                              {
                                  string Mymessage = null;
                                  string StrID = null;
                                  if (((EdmCmdData)ppoData.GetValue(0)).mlObjectID1 != 0)
                                  {
                                      Mymessage = Mymessage + "File: (ID=";
                                      StrID = ((EdmCmdData)ppoData.GetValue(0)).mlObjectID1.ToString();
                                      Mymessage = Mymessage + StrID + ") ";
                                      Mymessage = Mymessage + ((EdmCmdData)ppoData.GetValue(0)).mbsStrData1 + System.Environment.NewLine;
                                      MessageBox.Show(Mymessage);
                                  }
                              }
                          }
                      }
                  }
              }
              

               

              Hope this helps,

              Tim CEPA

              Believe in The Q!

                • Re: Getting Variable Values from Menu Command C#
                  Sean Donnelly

                  This is helpful, but still similar to the online samples. I am trying to utilize the GetVar() function and get variables from the file in question. How do I go about getting it in this context? I know there are standalone applications that define aFile and say aFile.GetVar("myVariable","@", out var). This aFile is defined by the filepath defined by the user. How do i go about defining the file in a menu add-in setting?

                    • Re: Getting Variable Values from Menu Command C#
                      Sean Donnelly

                      To be a little more specific, I am trying to use the IEdmEnumeratorVariable5 (or if you know of a way better please tell me).

                      What I've found is this, but it doesn't quite help me out because I need to define the file I think in the Enumerator right? I'm looking for some guidance.

                       

                      IEdmEnumeratorVariable5 vars = default(IEdmEnumeratorVariable5);

                                              vars.GetVar("CreatedBy", "@", out CreatedBy);

                                              message += CreatedBy;

                        • Re: Getting Variable Values from Menu Command C#
                          Tim Webb

                          I can help but had to run to a client site for a quick meeting. I'll help you when I get back. You need to get the file object first then set the enumeratorvariable to it. I'll get some code when I get back And see if I can explain it better next time

                            • Re: Getting Variable Values from Menu Command C#
                              Sean Donnelly

                              Hi Tim, did you get a chance to look at this? Thanks a lot!

                                • Re: Getting Variable Values from Menu Command C#
                                  Tim Webb

                                  Yes I did...this morning! My apologies for the delay.

                                  Take a look at this code: I'm including the entire add-in code. Tested it and here are my results:

                                   

                                  Here is file 200-17-001251.sldprt in the explorer

                                  2.png

                                  Here's my datacard for the file:

                                  1.png

                                  Here's the add-in message box:

                                  1.png

                                  Here's the complete add-in code:

                                   

                                  using System;
                                  using System.Collections.Generic;
                                  using System.Linq;
                                  using System.Text;
                                  using System.Threading.Tasks;
                                  using EdmLib;
                                  using Microsoft.VisualBasic;
                                  using System.Windows.Forms;
                                  namespace ClassLibrary2
                                  {
                                      public class Class1 : IEdmAddIn5
                                      {
                                          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 = 10;
                                              poInfo.mlRequiredVersionMinor = 0;
                                              //Register a menu command
                                              poCmdMgr.AddCmd(100, "C# Add-in", (int)EdmMenuFlags.EdmMenu_OnlySingleSelection);
                                          }
                                          public void OnCmd(ref EdmCmd poCmd, ref Array ppoData)
                                          {
                                              if (poCmd.meCmdType == EdmCmdType.EdmCmd_Menu) //This makes sure the addin was called on a menu right click
                                              {
                                                  if (poCmd.mlCmdID == 100) //This makes sure the menu command ID was 100, which means your addin was the one called
                                                  {
                                                      //Let's get moving, it's our turn!
                                                      IEdmVault12 myVault = (IEdmVault12)poCmd.mpoVault; //Cast the vault to an Iedmvault12 object for latest functionality
                                                      string Mymessage = null;//instantiate a string message
                                                      string StrID = null;//instantiate the ID of the object
                                                      if ((((EdmCmdData)ppoData.GetValue(0)).mlObjectID1) != 0) //mlObjectID1 will be 0 if a folder was selected and the ID of the file if a file was selected during a menu command
                                                      {
                                                          Mymessage = Mymessage + "File: (ID=";
                                                          IEdmFile5 myfile = (IEdmFile10)myVault.GetObject(EdmObjectType.EdmObject_File, (((EdmCmdData)ppoData.GetValue(0)).mlObjectID1));//Get the file object
                                                          IEdmEnumeratorVariable10 myvars = default(IEdmEnumeratorVariable10); //Set the enumeratorvariable object
                                                          myvars = (IEdmEnumeratorVariable10)myfile.GetEnumeratorVariable(); //Cast the enumeratorvariable object to the file
                                                          object PartNumberObj = null; //Create a variable to hold the model no.
                                                          myvars.GetVar("Model No.", "@", out PartNumberObj); //Use the GetVar method
                                                          myvars.CloseFile(false); //Always close the file when done accessing card variables to avoid runtime errors
                                                          Mymessage = Mymessage + "File ID: " + myfile.ID + System.Environment.NewLine; //Create a message with the File ID
                                                          Mymessage = Mymessage + "Model No. variable: " + PartNumberObj + System.Environment.NewLine; //Add the model no. variable
                                                          Mymessage = Mymessage + "File name: " + ((EdmCmdData)ppoData.GetValue(0)).mbsStrData1 + System.Environment.NewLine; //Add the file name
                                                          MessageBox.Show(Mymessage); //Show the message box!
                                                      }
                                                  }
                                              }
                                          }
                                      }
                                  }
                                  

                                   

                                  [Edit]: Forgot to add comments and I've exported the sample add-in for you to import my version for testing and comparison! Enjoy!

                                   

                                  Hope this helps!

                                  Tim CEPAhttps://equivaq.com/

                                  Believe in The Q!