0 Replies Latest reply on Dec 22, 2015 1:27 PM by Jim Sculley

    ModelDoc2::GetPathName -- Strange Behavior

    Jim Sculley

      Suddenly, ModelDoc2::GetPathName() has decided to alter the path ever so slightly in some code I have written.  Everything was working fine when suddenly, my EPDM add-in started exhibiting unexpected behavior.  I finally tracked it down to a simple method I wrote to get the ModelDoc2 for an EPDM file that is open in SolidWorks:

       

       /// <summary
       /// Retrieves the SOLIDWORKS ModelDoc2 object for an EPDM file open in SOLIDWORKS.
       /// Open files do not have to be visible in their own window.  Any referenced file
       /// that is not suppressed or lightweight is an open file
       /// </summary>
       /// <param name="file">the EPDM file</param>
       /// <returns>null, if SOLIDWORKS is not running or the specified document is not open.
       /// Otherwise, it returns the ModelDoc2 object associated with the file.</returns>
       public static ModelDoc2 getOpenSolidWorksDocument(IEdmFile11 file)
       {
           if (!solidworksIsRunning())
           {
               return null;
           }
           object openDocs = getSolidWorks().GetDocuments();
           if (openDocs == null)
           {
               return null;
           }
           foreach (object nextDocObj in (object[])openDocs)
           {
               ModelDoc2 nextDoc = (ModelDoc2)nextDocObj;
               if (nextDoc.GetPathName().Equals(file.GetLocalPath(Utilities.getFileFolder(file).ID)))
               {
                   return nextDoc;
               }
           }
           return null;
       }
      

       

      The method was returning 'null' even though I was certain that the file was open in SOLIDWORKS.  I set a breakpoint in the code and then used the Immediate window to see what was going on:

       

      The value returned by nextDoc.GetPathName() was

      "C:\\_2016SP0\\Engineering\\Projects\\123456-TES PROJECT\\4. Mechanical WIP\\ASSY RELEASE TESt\\C.SLDASM"

      and the value returned by file.GetLocalPath(Utilities.getFileFolder(file).ID) was

      "C:\\_2016SP0\\Engineering\\Projects\\123456-TES PROJECT\\4. Mechanical WIP\\ASSY RELEASE TEST\\C.SLDASM"

       

      It took me a couple of minutes to spot the problem.  For some reason, GetPathName() decided to change the upper case T to lower case for the ASSY RELEASE TEST folder.  This folder is not now, nor has it even been  ASSY RELEASE TESt.

       

      The fix is simple.  I just change the comparison to this:

       

      (nextDoc.GetPathName().Equals(file.GetLocalPath(Utilities.getFileFolder(file).ID),StringComparison.OrdinalIgnoreCase)
      

       

      but the mind boggles as to why it would happen in the first place.