4 Replies Latest reply on Apr 17, 2017 3:01 PM by Joe Pickens

    Solidworks PDM API Run Template and get the resulting file

    Joe Pickens

      In the program i am making i am trying to execute a template in pdm and then access the created files. I used the following example as my outline to accomplish what i want.

       

      2016 api - Execute Template and Return Data Example (C#)

       

      The issue i am having is once i run the template and get the file path of the created files. To my program these files don't exist. So when I use this code to get my file it comes back null "PDMFile = vault2.GetFileFromPath(filepath, out PDMFolder);" I have also tried adding the file to the vault and it still errors out.

       

      I put a pause in the code so i can check the vault to see if the file exists in the vault after the template is run and it was there and i was able to open the file, and resumed running my code and it still cant get the file.

       

      Has anyone through APIi run a template and try to access the file?

        • Re: Solidworks PDM API Run Template and get the resulting file
          Lee CS Young

          Run your template manually as the user that will be executing your program. Make sure it works that way first.

           

          I've done this in numerous add-ins. Here's some sample code.

           

          internal class TemplateManager
              {
                  public List<string> GetTemplateNames(IEdmVault12 vault)
                  {
                      var templateMgr = (IEdmTemplateMgr5)vault;
                      var pos = templateMgr.GetFirstTemplatePosition();
                      var template = default(IEdmTemplate53);
                      var templates = new List<string>();
                      while (!pos.IsNull)
                      {
                          template = (IEdmTemplate53)templateMgr.GetNextTemplate(pos);
                          templates.Add(template.GetMenuString());
                      }
                      return templates;
                  }
                  public List<IEdmData> RunTemplate(IEdmVault12 vault, int parentHnd, int folderID, string templateName)
                  {            
                      int templateID = 0;
                      var templateMgr = (IEdmTemplateMgr5)vault;
                      try
                      {
                         
                          var pos = templateMgr.GetFirstTemplatePosition();
                          var template = default(IEdmTemplate53);
                          var foundTemplate = default(IEdmTemplate53);
                          string foundTemplateName = string.Empty;
                          while (!pos.IsNull)
                          {
                              template = (IEdmTemplate53)templateMgr.GetNextTemplate(pos);
                              foundTemplateName = template.GetMenuString();
                              if (foundTemplateName == templateName)
                              {
                                  foundTemplate = template;
                                  break;
                              }                    
                          }
                          // we dont' have permissions or the template is missing                    
                          if (foundTemplate != null && (foundTemplateName == templateName))
                          {
                              var data = Array.CreateInstance(typeof(IEdmData), 0);
                              foundTemplate.RunEx(parentHnd, folderID, out data);
                              return data.OfType<IEdmData>().ToList();
                          }
                      }
                      finally
                      {
                          templateMgr = null;
                      }
                      return null;
                  }
              }
          

          And how I call it..

           

          TemplateManager templateManager = new TemplateManager();
          List<IEdmData> files = templateManager.RunTemplate((IEdmVault12)poCmd.mpoVault, poCmd.mlParentWnd, folderID, templateName);
          if (files?.Count > 0)
          {
            foreach (EdmData item in files)
            {
            switch (item.Type)
            {
            case EdmDataType.EdmData_Nothing:
            break;
            case EdmDataType.EdmData_File:
            break;
            case EdmDataType.EdmData_Folder:
            break;
            case EdmDataType.EdmData_Variable:
            break;
            case EdmDataType.EdmData_Link:
            break;
            default:
            break;
            }
            }
          }