1 Reply Latest reply on Dec 7, 2017 1:18 AM by David Stockdale

    Change Note Checker

    Adon Nicholls

      Hi Everyone,

       

      I've recently started at a company in an EPDM role but have limited to non existent experience in coding although I have now managed to write a code for an API to automatically create Non Conformance Reports from a file card. I wanted to write another API which would make lives easier for my colleagues checking change notes.

       

      For the moment they need to check whether files have been successfully saved in a certain location for our MRP Department. The API Would check this folder for the files; PDF,DXF etc., going off the part number and the revision number of the Solidworks file and then, if the files were present, check a box on a file card. If the file wasn't present it would give an error message (I can make the message appear using the try method)

       

      I'm kind of stuck on how to approach the problem, and as I've had no training, I don't really understand the online API Guide.

       

      Here's what I have so far.

       

      PREState Hook Class:

       

      Change Note Checker Class:

       

       

      Any help would be greatly appreciated

        • Re: Change Note Checker
          David Stockdale

          The following will hopefully help.

           

          I didn't include any details on how to look for the file (MSDN has a ton of information on that) but if you still need help, you'll probably need to provide the specifics of your file naming scheme.  That being said, the following details how to check out the file, update a variable on the card (check the box), and check the file back in.

           

          Also, the EdmFileLock class is something I wrote to help make sure that my code cleans up after itself.  Essentially, it checks out the file upon creation, and if "CheckinFile()" is never called because an exception was thrown, it will undo the checkout automatically.  I have provided it's implementation in case you'd like to use it.

          Of course, you'll have to translate this to VB.net

           

           

                  public static void DoChangeNoteChecker(IEdmVault18 vault, IEdmFile5 fileRef, string MRPDir, string VarName)
                  {
                      
                      // logic to check if the file you're looking for exists in the MRPDir
                      // based on the file name and revision (per your spec)
                      // probably use .net functionality in System.IO namespace...
                      // also, revision and local path of the file can be obtained from fileRef
          
          
                      bool fileExists = true;// based on your logic above
                      if (!fileExists)
                      {
                          // report and exit
                          return;
                      }
          
          
                      // otherwise, check out the file, update the "check" the checkbox, and check back in
                      using (var fileLock = new EdmFileLock((IEdmFile12)fileRef, parentFolder))
                      {
                          // we've entered a "transaction scope" that will ensure that if
                          // anything throws, we clean up after ourselves (checkout is undone)
          
          
                          object checkBoxVal;
                          IEdmEnumeratorVariable10 varEnumerator = (IEdmEnumeratorVariable10)fileRef.GetEnumeratorVariable();
                          if (varEnumerator.GetVar2(VarName, "@", parentFolder.ID, out checkBoxVal))
                          {
                              // file exists, variable exists, check the box!
                              object checkedValue = "1";
                              varEnumerator.SetVar(VarName, "@", ref checkedValue);
                          }
          
          
                          // flush and close the file
                          varEnumerator.CloseFile(true);
                          fileLock.CheckinFile();
                      } 
          
          
          
          
          
          
                  }
          

           

          EdmFileLock implementation:

           

              public class EdmFileLock : IDisposable
              {
                  private IEdmFile12 m_file;
                  private int m_parentFolderID;
                  private bool m_disposed;
                  private const int PARENT_WIND_HANDLE = 0; // check in/out silently
          
          
                  public EdmFileLock(IEdmFile12 EdmFile, IEdmFolder5 ParentFolder)
                  {
                      if (EdmFile == null)
                          throw new ArgumentNullException("Edm File object cannot be null");
                      if (ParentFolder == null)
                          throw new ArgumentNullException("Edm Folder object cannot be null");
          
          
                      m_file = EdmFile;
                      m_parentFolderID = ParentFolder.ID;
                      m_file.LockFile(m_parentFolderID, PARENT_WIND_HANDLE);
                      m_disposed = false;
                  }
          
          
                  private bool FileCheckedIn
                  {
                      get;set;
                  }
          
          
                  public void CheckinFile()
                  {
                      IEdmVault18 theVault = m_file.Vault as IEdmVault18;
                      IEdmBatchUnlock2 unlocker = (IEdmBatchUnlock2)theVault.CreateUtility(EdmUtility.EdmUtil_BatchUnlock);
                      EdmSelItem[] itemsToUnlock = new EdmSelItem[] { 
                          new EdmSelItem() { mlDocID = m_file.ID, mlProjID= m_parentFolderID } 
                      };
          
          
                      unlocker.AddSelection((EdmVault5)theVault, ref itemsToUnlock);
                      unlocker.CreateTree(PARENT_WIND_HANDLE, (int)EdmUnlockBuildTreeFlags.Eubtf_Nothing);
                      unlocker.UnlockFiles(PARENT_WIND_HANDLE);
                      FileCheckedIn = true;
                  }
          
          
                  public void Dispose()
                  {
                      if (!(m_disposed || FileCheckedIn))
                      {
                          // files failed to check in properly either because
                          // that operation failed or because an exception
                          // was thrown while in our scope.  Clean up by undoing the checkout
                          m_file.UndoLockFile(PARENT_WIND_HANDLE);
                      }
          
          
                      m_disposed = true;
                  }
              }