ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
AMArne Morten Fredriksen17/01/2014

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.