Macro to check-in/out files via Solidworks application

If the macro tries to check-out (IEdmFile5 :: LockFile) the document is opened in the Solidworks I get an error message that tells me the current file is in use by another application and does not check it out.

The lock and unlock file methods only works for non opened documents? If yes, is there another method to get it?

If a part or assembly file is open in Solidworks, you will have to use the Solidworks API to call ForceReleaseLocks to free the file in order to work with it using PDM functions. Now, it’s very important to read the “Remarks” section closely as it indicates how to re-attach the file when your processing is complete. Failure to do so can result in file corruption or other errors.
https://help.solidworks.com/2016/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.imodeldoc2~forcereleaselocks.html

With drawings, the same can be done using CloseAndReopen.
https://help.solidworks.com/2016/english/api/sldworksapi/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.ISldWorks~CloseAndReopen.html

1 Like

It worked!

Is it possible to call an update for PDM task pane? The model got locked but the task pane only updates after a few seconds or forced by the user.

Here’s a code that it’s working for testing purposes:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim pdmVault As EdmVault5
Dim handle As Long

Const vaultName As String = "Your vault name"

Sub main()

Dim swFrame As SldWorks.Frame
Dim swModel As SldWorks.ModelDoc2

Set swApp = Application.SldWorks

Set swFrame = swApp.Frame

handle = swFrame.GetHWnd

Set pdmVault = New EdmVault5

pdmVault.LoginAuto vaultName, handle

Set swModel = swApp.ActiveDoc

Call checkOutFile(pdmVault, swModel.GetType)

Call checkInFile(pdmVault, swModel.GetType)
End Sub

Private Sub checkOutFile(ByVal pdmVault, ByVal docType As Integer)

Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim ret As Integer

Set swModel = swApp.ActiveDoc

Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)

If pdmFile.IsLocked Then Exit Sub

If swModel.ForceReleaseLocks Then

    pdmFile.LockFile pdmFolder.ID, handle
End If

If docType = swDocDRAWING Then
    
    ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
Else
    
    ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
End If
End Sub

Private Sub checkInFile(ByVal pdmVault, ByVal docType As Integer)

Dim swModel As SldWorks.ModelDoc2
Dim pdmFile As IEdmFile5
Dim pdmFolder As IEdmFolder5
Dim ret As Integer

Set swModel = swApp.ActiveDoc

Set pdmFile = pdmVault.GetFileFromPath(swModel.GetPathName, pdmFolder)

If Not pdmFile.IsLocked Then Exit Sub

If swModel.ForceReleaseLocks Then
    
    pdmFile.UnlockFile pdmFolder.ID, handle, EdmUnlock_OverwriteLatestVersion
End If

If docType = swDocDRAWING Then
    
    ret = swApp.CloseAndReopen(swModel, swCloseReopenOption_DiscardChanges, swModel)
Else
    
    ret = swModel.ReloadOrReplace(False, swModel.GetPathName, True)
End If
End Sub