Solidworks EPDM 'Get latest' from VBA

5.5k Views Asked by At

Due to a company policy on how the PDM system operates, when the user checks in a file, the local copy is deleted from the users cache. My macro checks files out, edits them and checks back in again. If I try and edit a file that has just been edited I get a 'file not found' error (because it's been deleted from cache). I have tried to get around this by writing a sub to get the latest copy of a file immediatly before editing it to ensure there is always a file present but the code doesnt seem to retrieve the file. The sub is as below.

Sub GetLatest(fName As String)

Dim vaultName As String
Dim eVault As IEdmVault13
Dim eFile As IEdmFile8
Dim BG As IEdmBatchGet
Dim files(1) As EdmSelItem

'log into the vault
vaultName = Config.ReadXMLElement(pathConfig, "vaultname")
Set eVault = New EdmVault5
If Not eVault.IsLoggedIn Then
    Call eVault.LoginAuto(vaultName, 0)
End If

'get the file to get lastest
Set eFile = eVault.GetFileFromPath(fName)
'put the file in an array
files(0).mlDocID = 0
files(0).mlProjID = eFile.ID

Set BG = eVault.CreateUtility(EdmUtil_BatchGet)

Call BG.AddSelection(eVault, files())
Call BG.CreateTree(0, EdmGetCmdFlags.Egcf_SkipExisting)
Call BG.GetFiles(0, Nothing)

End Sub

If I manually 'get latest' in the EPDM browser before editing the file, the macro reads it fine. The code is slightly modified from that posted by Michael Dekoning at https://forum.solidworks.com/thread/51105

1

There are 1 best solutions below

0
On

From first glance, it looks like you are populating the EdmSelItem properties incorrectly. The docID property is the database ID of the document. The ProjID property is the ID of the containing folder. For getting the latest version, you can use any containing folder, as it will be checked out in all folders. With EPDM, when a file is "shared" it can have multiple parent folder IDs it belongs to, and we can enumerate over then using the methods from iEdmFile5 GetFirstFolderPosition and GetNextFolder.

You can refer to the documentation for further information and examples.

If you want to get a single file, try the following adjustment and see if that does it:

Set eFile = eVault.GetFileFromPath(fName)
Dim eFolder as iEdmFolder5
Dim Pos as iEdmPos5
Set Pos=eFile.GetFirstFolderPosition
Set eFolder=eFile.GetNextFolder(Pos)
'Get the file from the folder
files(0).mlDocID = eFile.ID
files(0).mlProjID = eFolder.ID

When you provide DocID = 0 it tells EPDM to get all the files in the folder specified. Like so:

'Get all files from the folder
files(0).mlDocID = 0
files(0).mlProjID = eFolder.ID