3 Replies Latest reply on Jan 9, 2018 10:36 AM by Alex Burnett

    Read Data Card properties via API

    Sanya Shmidt


      Can anyone point me in the right direction (or give a simple example) of how to read a Variable from EPDM using API.

      I can do that w/out any problems utilizing SW custom properties, but its taking too much time to open the assembly with SW. I thought there should be an easier way of doing that.


      I found an example :

      2014 SOLIDWORKS API Help - Access File Card Variables Example (VB.NET)


      but for some reason when I choose the part and click on Get Variables I`m getting a error:


      HRESULT=0X80040203 You dont have permission to perform this action...


      Thank you.

        • Re: Read Data Card properties via API
          Alex Burnett

          I haven't done this with a standalone program outside of solidworks but my code is very similar, although in c#, to the code in the example you posted.


          In order to best help you, can you describe how you need this to function? Do you already know the file name in the vault? Do you already know the name of the variable that you want to pull from the card? Additionally, it may be a good idea to check that you don't have the file checked out and update to latest version before this macro gets the variable.


          If you're able to tell us how you want to access the variable and use it then that would help point you in the right direction.


          Example code: This is from a function I wrote to check the drawing data card and ensure the "Sheet1", "Sheet2", etc. tabs match the default "@" tab. If they are different, then it returns true indicating that I need to modify the data card. As a side note, if you need to modify the EPDM data card then you will need to close the solidworks file if it is currently open or you will get errors. It took hours and a couple headaches to get that sorted.


          This is code from within a function that takes a ModelDoc2 modelIn variable.


          IEdmVault8 vault;
          IEdmFile5 efile;
          IEdmFolder5 efolder;
          IEdmEnumeratorVariable5 varEnum;
          IEdmVariableMgr5 varMgr;
          IEdmVariableValue6 varVal;
          IEdmStrLst5 cfgList;
          IEdmPos5 cfgNum;
          vault = (IEdmVault8)(new EdmVault5());
          efile = default(IEdmFile5);
          efolder = default(IEdmFolder5);
          varEnum = default(IEdmEnumeratorVariable5);
          cfgList = default(IEdmStrLst5);
          varMgr = default(IEdmVariableMgr5);
          cfgNum = default(IEdmPos5);
          varVal = default(IEdmVariableValue6);
               int handle = Convert.ToInt32(0);
               // Automatically log in to vault
               if (!vault.IsLoggedIn)
                    Trace.WriteLine("Not logged in, auto log in");
                    vault.LoginAuto(mVaultView, handle);
               efile = vault.GetFileFromPath(modelIn.GetPathName(), out efolder);
               if(efile == null)
                    return false; // Not if vault if null
               IEdmEnumeratorVariable5 ie5 = (IEdmEnumeratorVariable5)efolder;
               if (efile != null && efile.IsLocked)
                    varEnum = efile.GetEnumeratorVariable();
                    object[] valueList = null;
                    varEnum.GetUpdateVars(efile.LockedInFolderID, out valueList);
                    cfgList = efile.GetConfigurations(efile.CurrentVersion);
                    varMgr = (IEdmVariableMgr5)efile.Vault;
                    string msg = "Card variables for " + efile.Name + System.Environment.NewLine + System.Environment.NewLine;
                    foreach (var value in valueList)
                         cfgNum = cfgList.GetHeadPosition();
                         varVal = (IEdmVariableValue6)value;
                         string defaultTabValue = string.Empty;
                         defaultTabValue = modelIn.GetCustomProperty(varVal.VariableName, null, true); //This is a custom function that gets the value of this property from the custom property table
                         for (int i = 0; i < cfgList.Count; i++)
                              string name = cfgList.GetNext(cfgNum);
                              string cfgValue = varVal.GetValue(name);
                              if (string.Equals(name, mDefaultCfgName))
                              msg += "CHECK: VarName:\t" + varVal.VariableName + "\tCfgName:\t" + name + "\tValue:\t" + cfgValue + "\tdVal:\t" + defaultTabValue + System.Environment.NewLine;
                              if (!string.Equals(defaultTabValue, cfgValue)) // If values between custom property table and tab don't match
                                   if (!string.Equals(string.Empty, cfgValue) || (!string.Equals(string.Empty, defaultTabValue) && string.Equals(string.Empty, cfgValue)))  // If the value on the data card is not blank.
                                        allBlank = false;
                                   returnVal = true;

          Edit: Fixed code to re-add intentations

            • Re: Read Data Card properties via API
              Sanya Shmidt

              Thank you, I`ll try the code.

              We do have a list of files and yes we know what variables need to be pulled (just 4 of them)

                • Re: Read Data Card properties via API
                  Alex Burnett

                  Sanya Shmidt wrote:


                  Thank you, I`ll try the code.

                  We do have a list of files and yes we know what variables need to be pulled (just 4 of them)

                  If that's the case then you can replace the contents of  foreach (var value in valueList) block with your targeted search for the variables. If you're only searching on the "@" tab then the configuration for loop inside there is unnecessary and will add time to your search.


                  All of this can be translated to VBA (VB.NET) if that's what you're more comfortable with. There are online converters all over that place that do a pretty good job.