5 Replies Latest reply on Feb 13, 2014 7:20 AM by Nazar Chaykin

    SolidWorks crash when renaming part.

    Arne Morten Fredriksen

      Hi,

       

      What I'm trying to achive is to rename a part that is open in SolidWorks using the API. I'm using EPDM as well and the check in is done from the EPDM addin in SolidWorks. It's datacard has been modified to trigger the PostUnlock event.

      The code in the PostUnlock event close the part in SolidWorks, renames the part and opens it in SolidWorks with it's new name.

      When looking in the EPDM-addin in SolidWorks I can see that the file has been opened correctly, apparently, and its also checked in with it's new name. But as soon as my addin is finished doing its work, SolidWorks crash. When looking in the file vault in windows explorer everything looks fine though.

       

      Here is my code that I'm using for renaming the file:

       

       

           Select Case poCmd.meCmdType

       

              Case EdmCmdType.EdmCmd_PostUnlock

       

       

                  'Connect to SolidWorks.

                  Dim oSolidWorksApplication As SldWorks.SldWorks

                  oSolidWorksApplication = GetObject(, "SldWorks.Application")

       

                  'Get file- and folder objects.

                  Dim oFile As EdmLib.IEdmFile7 = Nothing

                  Dim oFolder As EdmLib.IEdmFolder6 = Nothing

                  oFile = oVault.GetObject(EdmLib.EdmObjectType.EdmObject_File, ppoData(nIndex).mlObjectID1)

                  oFolder = oVault.GetObject(EdmLib.EdmObjectType.EdmObject_Folder, ppoData(nIndex).mlObjectID2)

       

                  'Create current file path.

                  Dim sCurrentFilePath As String = ""

                  sCurrentFilePath = oFile.GetLocalPath(oFolder.ID)

                  sCurrentFilePath = sCurrentFilePath.Substring(0, InStrRev(sCurrentFilePath, "\"))

                  sCurrentFilePath = sCurrentFilePath & oFile.Name

       

                  'New file name.

                  Dim sNewFileName As String = ""

                  sNewFileName = "12345.sldprt"

       

                  'Create full path to new file.

                  Dim sFullNewFilePath As String = ""

                  sFullNewFilePath = sCurrentFilePath

                  sFullNewFilePath = sFullNewFilePath.Substring(0, InStrRev(sFullNewFilePath, "\")) & sNewFileName

       

                  'Set focus to the document in SolidWorks.

                  Dim nError As Integer

                  Dim oDocument As SldWorks.ModelDoc2

                  oDocument = oSolidWorksApplication.ActivateDoc2(sCurrentFilePath, True, nError)

       

                  'Close the document.

                  oSolidWorksApplication.CloseDoc(sCurrentFilePath)

       

                  'Rename file.

                  oFile.Rename(0, sNewFileName, True)

       

                  'Open the file having the new name.

                  Dim IfileError As Integer

                  Dim IfileWarning As Integer

                  oSolidWorksApplication.OpenDoc6(sFullNewFilePath, SwConst.swDocumentTypes_e.swDocPART, SwConst.swOpenDocOptions_e.swOpenDocOptions_Silent, "", IfileError, IfileWarning)

       

                  'Clean up.

                  System.Runtime.InteropServices.Marshal.ReleaseComObject(oSolidWorksApplication)

                  oSolidWorksApplication = Nothing

       

          End Select

        

       

      Any thoughts of what might be wrong here???

       

      All suggestions are appreciated.

       

      Thanks in advance.

        • Re: SolidWorks crash when renaming part.
          Nazar Chaykin

          Hi Arne

           

          I want to get full file path in epdm, then to open him in SolidWorks. I'm try to use you code. It dosen't work, but If i commented  string " oFolder = oVault.GetObject(EdmLib.EdmObjectType.EdmObject_Folder, ppoData(nIndex).mlObjectID2)", MsgBox(sCurrentFilePath) show me only File name. With a string "oFolder..." nothing happened. What's the problem?

           

           

          Public Sub OnCmd(ByRef poCmd As EdmCmd, ByRef ppoData As Array) Implements IEdmAddIn5.OnCmd

           

              Select poCmd.mlCmdID

           

                   Case 1

           

                      'Connect to SolidWorks.

                      Dim oSolidWorksApplication As SldWorks

                      oSolidWorksApplication = GetObject("SldWorks.Application")

                      Dim nindex As Long

                      nindex = LBound(ppoData)

           

                      Dim ovault As EdmVault5

                      ovault = poCmd.mpoVault

           

                      Dim oFile As IEdmFile5

                      'Dim oFolder As IEdmFolder7

           

                      oFile = ovault.GetObject(EdmObjectType.EdmObject_File, ppoData(nindex).mlObjectID1)

                      'oFolder = ovault.GetObject(EdmObjectType.EdmObject_Folder, ppoData(nindex).mlObjectID2)

           

                      'Create current file path.

                      Dim sCurrentFilePath As String = ""

                      'sCurrentFilePath = oFile.GetLocalPath(oFolder.ID)

                      sCurrentFilePath = sCurrentFilePath.Substring(0, InStrRev(sCurrentFilePath, "\"))

                      sCurrentFilePath = sCurrentFilePath & oFile.Name

           

                      MsgBox(sCurrentFilePath)

           

              End Select

          End Sub