0 Replies Latest reply on Jul 19, 2017 4:17 PM by Sean Donnelly

    Explorer Crashing with List<EdmSelItem> - C# PDM Pro API

    Sean Donnelly

      Hello, I'm trying to right some C# code to get the latest version of all the files associated with a file when it's being opened through my API. I was receiving errors in Visual Studio when I was using an array, and someone recommended I choose to do a list instead since I did not have a definite amount I want in the array. List is more flexible. I can also post the version of this with the Array if that would be preferred.

       

      So here's the issue. My code is for Card Buttons that will go find a DXF/PDF/SLDDRW based on a variable value on the card. you select it, it opens it. simple. It works when I don't try to get fancy with with getting the latest on all the files to ensure the user opens the latest part model and assembly files with the SLDDRW. Otherwise, you have the latest drawing by default, but not the parts.

       

      Where it went wrong is when I added the Get Latest code for IEdmBatchGet, it gave me the error below in the screen shot saying it can't connect EdmSelItem to ref System.Array. (this code is not used below, it is an old iteration but I can post this if necessary)

       

      I then asked someone for help who advised me to use a list instead. I've stretched this source a little bit and they are asking me to start debugging, which I'm trying to figure out how to properly do, but for now was curious if anyone here had a solution while I work towards that.

       

      The moment I add the List<EdmSelItem>, when I test the add-in and use the search in windows explorer it will crash immediately when I run the search (via search play button, which has a command of "ok"). I have the List in an area with if statements where it shouldn't even be seen, but it crashes it somehow. the error code isn't on the PDM site either. It's 0x080131163 and says unknown.

       

      List layout:

      List<EdmSelItem> getterSelections = new List<EdmSelItem>();
      

       

       

      using System;
      using System.IO;
      using System.Collections.Generic;
      using System.Text;
      using System.Diagnostics;
      using EdmLib;
      using System.Runtime.InteropServices;
      using System.Drawing;
      using Microsoft.VisualBasic;
      using System.Windows.Forms;
      
      
      namespace FocalPointTasks
      {
          class CardButton
          {
              public void DwgDxfButton(ref EdmCmd poCmd, ref System.Array ppoData)
              {
                  IEdmVault5 edmVault = poCmd.mpoVault as IEdmVault5;
                  IEdmVault12 myVault = (IEdmVault12)poCmd.mpoVault; //Cast the vault to an Iedmvault12 object for latest functionality 
      
      
                  String comment = poCmd.mbsComment;                      
      
      
                  if (comment.Contains("OpenDxf:") || comment.Contains("OpenSlddrw:") || comment.Contains("OpenPdf:"))
                  {
                      IEdmBatchGet batchGetter;
                      IEdmSelectionList6 fileList = null;
                      EdmSelectionObject poSel;
                      Array ppoSelection; // ultimately this will be set by converting the list to array just for passing
                      List<EdmSelItem> getterSelections = new List<EdmSelItem>();
                      IEdmPos5 aPos;
                      //EdmSelItem[] ppoSelection = new EdmSelItem[100];
      
      
                      string VarName = null;
                      int index = comment.IndexOf(":");
                      string VarValue = null;
                      string proto = "-PROTOTYPE";
                      string SearchFile = null;
                      string type = null;
                      string FileId = null;
                      string FolderId = null;
                      //string FilePath = null;
      
      
                      if (index > 0)
                      {
                          type = comment.Substring(0, index);
                          VarName = Strings.Right(comment, Strings.Len(comment) - (index + 1));
                      }
      
      
                      IEdmEnumeratorVariable5 vars = default(IEdmEnumeratorVariable5);
                      vars = (IEdmEnumeratorVariable5)poCmd.mpoExtra;
                      object VarVal = null;
                      vars.GetVar(VarName, "", out VarVal);
      
      
                      VarValue = VarVal.ToString();
      
      
                      string dwgNoExt = null;
                      string dwgExt = null;
      
      
                      if (Path.HasExtension(VarValue))
                      {
                          dwgNoExt = Path.GetFileNameWithoutExtension(VarValue);
                          dwgExt = Path.GetExtension(VarValue);
                      }
                      else
                      {
                          dwgNoExt = VarValue;
                      }
      
      
                      if (type == "OpenDxf")
                      {
                          if (dwgExt != null)
                          {
                              if (dwgExt.Equals(".DWG", StringComparison.OrdinalIgnoreCase))
                              {
                                  SearchFile = dwgNoExt + ".DWG";
                              }
                              else
                              {
                                  SearchFile = dwgNoExt + ".DXF";
                              }
                          }
                          else
                          {
                              SearchFile = dwgNoExt + ".DXF";
                          }
                      }
                      else if (type == "OpenSlddrw")
                      {
                          if (dwgNoExt.Contains(proto))
                          {
                              index = dwgNoExt.IndexOf(proto);
                              if (index > 0)
                              {
                                  SearchFile = dwgNoExt.Substring(0, index) + ".SLDDRW";
                              }
                              else
                              {
                                  SearchFile = dwgNoExt + ".SLDDRW";
                              }
                          }
                          else
                          {
                              SearchFile = dwgNoExt + ".SLDDRW";
                          }
                      }
                      else if (type == "OpenPdf")
                      {
                          SearchFile = dwgNoExt + ".PDF";
                      }
                      else
                      {
                          MessageBox.Show("Button Message Not Proper. Contact Administrator.");
                          return;
                      }
      
      
                      //Search for the file in the vault
                      IEdmSearch5 Search = default(IEdmSearch5);
                      Search = (IEdmSearch5)myVault.CreateUtility(EdmUtility.EdmUtil_Search);
      
      
                      Search.FindFolders = false;
                      Search.FindFiles = true;
      
      
                      //Search.StartFolderID = 29611;
                      //this searches every file in vault, instead of finding the one i want
                      Search.FileName = SearchFile;//.Equals(SearchFile, StringComparison.OrdinalIgnoreCase);
      
      
                      IEdmSearchResult5 Result = default(IEdmSearchResult5);
                      Result = Search.GetFirstResult();
      
      
                      int i = 0;
                      int fileCount = 0;
                      string filename = null;
                      string str;
                      bool retVal;
      
      
                      if (Result != null)
                      {
                          while (Result != null)
                          {
                              filename = Result.Name;
      
      
                              var item = new EdmSelItem();
                              item.mlDocID = Result.ID;
                              item.mlProjID = Result.ParentFolderID;
                              ////ppoSelection.SetValue(item, i); // <<< forget this!
                              getterSelections.Add(item); // <<< no longer need index i
      
      
                              Result = Search.GetNextResult();
                          }
                      }
                      else
                      {
                          MessageBox.Show("The listed drawing may not exist for this option.\r\nTry searching using the PDM Search to confirm.");
                          return;
                      }
      
      
                      //setup batch get
                      batchGetter = (IEdmBatchGet)myVault.CreateUtility(EdmUtility.EdmUtil_BatchGet);
      
      
                      // Add selections to the batch of files to check out
                      ppoSelection = getterSelections.ToArray();
                      batchGetter.AddSelection((EdmVault5)myVault, ref ppoSelection);
                      
                      if ((batchGetter != null))
                      {
                          if (Path.GetExtension(filename).Equals(".insp", StringComparison.CurrentCultureIgnoreCase))
                          {
                              //do not prompt user.
                              batchGetter.CreateTree(0, (int)EdmGetCmdFlags.Egcf_Nothing);
      
      
                              batchGetter.GetFiles(0, null);
                          }
                          else
                          {
                              batchGetter.CreateTree(0, (int)EdmGetCmdFlags.Egcf_Lock);
      
      
                              fileCount = batchGetter.FileCount;
                              fileList = (IEdmSelectionList6)batchGetter.GetFileList((int)EdmGetFileListFlag.Egflf_GetLocked + (int)EdmGetFileListFlag.Egflf_GetFailed + (int)EdmGetFileListFlag.Egflf_GetRetrieved + (int)EdmGetFileListFlag.Egflf_GetUnprocessed);
      
      
                              aPos = fileList.GetHeadPosition();
      
      
                              str = "Getting " + fileCount + " files: ";
                              while (!(aPos.IsNull))
                              {
                                  fileList.GetNext2(aPos, out poSel);
                                  str = str + Constants.vbLf + poSel.mbsPath;
                              }
      
      
                              //MsgBox(str)
      
      
                              retVal = batchGetter.ShowDlg(0);
                              // No dialog if checking out only one file
      
      
                              if ((retVal))
                              {
                                  batchGetter.GetFiles(0, null);
                              }
                          }
                      }
      
      
                      string openFilepath = "Conisio://" + myVault.Name + "/open?projectid=" + FolderId + "&documentid=" + FileId + "&objecttype=1";
      
      
                      Process.Start(openFilepath);
                  }
              }
          }
      }