Hello everybody!
I am writing quite simple macro, which is looking for the file in the PDM and opens it.
What I have at the beginning, is the name of the file without extension, so I don`t know, is it Part or Assembly. Also I don`t know precisely, in which folder it is. Because if I know it, then I can simply use swApp.OpenDoc6, which also requires the extension. So, before opening, I have to get the extension of it.
I tried every piece of code from here: https://forum.solidworks.com/thread/223999#comment-888233 , but looke like "EdmVault5" always returns me an "user-defined type is not defined".
FYI, I am using SW 2019.
Do you have any examples of code, solving this problem?
UPDATE:
Great, now this Macro works perfectly. Thanks everybody!
If anybody else will need something similar - here is the code.
Dim swApp As Object<br>Dim Part As Object<br>Dim myModelView As Object<br><br>Dim message As String<br>Dim myVault As New EdmVault5<br>Dim search As IEdmSearch7<br>Dim result As IEdmSearchResult5<br>Dim epdmfile As IEdmFile5<br>Dim epdmFolder As IEdmFolder5<br><br>Dim boolstatus As Boolean<br>Dim DesiredFile As String<br>Dim ModelName As String<br>Dim ModelPath As String<br><br>Dim longstatus As Long, longwarnings As Long, longerrors As Long<br><br>Sub main()<br>Set swApp = Application.SldWorks<br><br>myVault.LoginAuto "YOURVAULTNAME", 0 '<br><br>Set search = myVault.CreateUtility(EdmUtility.EdmUtil_Search)<br>search.Clear<br><br>DesiredFile = "45052-03" '------HERE ADJUST THE MODEL NAME -----------<br><br>search.FindFolders = False<br>search.FindFiles = True<br>search.FileName = DesiredFile & ".%"<br>search.Recursive = True<br>search.SetToken Edmstok_Recursive, True<br><br>Set result = search.GetFirstResult<br><br>Set epdmfile = Nothing<br>Set epdmFolder = Nothing<br><br> While Not result Is Nothing<br><br> Set epdmFolder = myVault.GetObject(EdmObjectType.EdmObject_Folder, result.ParentFolderID)<br> Set epdmfile = myVault.GetObject(EdmObjectType.EdmObject_File, result.ID)<br> Set result = search.GetNextResult()<br> ModelName = epdmfile.Name<br><br> If Right(ModelName, 3) = "PRT" Then<br><br> ModelPath = epdmFolder.LocalPath & "\\" & epdmfile.Name<br> Set Part = swApp.OpenDoc6(ModelPath, 1, 0, "", longstatus, longwarnings) 'Opening the .SLDPRT model<br><br> ElseIf Right(ModelName, 3) = "ASM" Then<br><br> ModelPath = epdmFolder.LocalPath & "\\" & epdmfile.Name<br> Set Part = swApp.OpenDoc6(ModelPath, 2, 0, "", longstatus, longwarnings) 'Opening the .SLDASM model<br> End If<br> 'message = message & vbCrLf & epdmfile.Name & " at " & epdmFolder.LocalPath<br><br> Wend<br>Set Part = swApp.ActiveDoc<br><br>' HERE IS YOUR CODE<br><br>' Close the file<br>swApp.CloseDoc ModelName<br><br>End Sub<span class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span>SolidworksApi/macros
Hi Grigorii
Did you add a reference to the PDMWorks Enterprise 20xx Type Library?
BiI SAP-PDM
Check this article: Using SOLIDWORKS PDM API In VBA And VSTA Macros
Yep, thanks, completely forgot it.
I am trying to get the location of a file in the PDM, but looks like it gives nothing, even it successfully logged into PDM. Sorry, I don`t have a lot of experience with VBA.
This has to be changed.
Everything else should work. Are you sure the file is in the vault and the name of the vault is right?
You will get the local path in this way.
BiI SAP-PDM
But looks like it searches only in root folder, which looks like C:\VAULTNAME, so how to set the path to search like C:\VAULTNAME\Automation\Parts? The needed part located somewhere at C:\VAULTNAME\Automation\Parts\Cylinders\47000, so I want to search in every folder at C:\VAULTNAME\Automation\Parts.
Hi Grigori,
please try IEdmSearch7 and the "CreateUtility(EdmUtility.EdmUtil_Search)" methode from EdmVault5.
Within this method you can use the following properties:
For example
.FindFiles = True
.FindFolders = False
.StartFolderID = 253147 ((int) e.g. your rootfolder C:\vaultname\)
.Recursive = True
.SetToken Edmstok_AllVersions, False
.FileName = sFileName (var for yor searchname)
Cheers
Mario
I think it is close already to the working solution. But it still returns me an empty message. How to get an ID of a folder like C:\VAULTNAME\Automation\Parts? The needed part (47075-1.SLDPRT) is located somewhere at C:\VAULTNAME\Automation\Parts\Cylinders\47000, so I want to search in every folder at C:\VAULTNAME\Automation\Parts.
The token Edmstok_Recursive is set to true by default. So the file should be shown in the search if it is in the vault.
Have a look here for all tokens. 2014 SOLIDWORKS API Help - EdmSearchToken Enumeration
If you want to search in another folder, you need the folder id in search.StartFolderID. To get the id you can do another search for the folder,
or do it via SQL.
BiI SAP-PDM
Closer and closer now. Something doesn`t make it recursive, so after running this code I get the following message:
1
VAULTNAME
You can take the ID from the Explorer with the corresponding column configuration.
1 = rootfolerID
VAULTNAME must be your Vaultname
GREAT!
@GB Thanks for posting the code! I've changed to early binding to make adding code easier and also added the ability to open a drawing.
' |PRE-CONDITIONS|:
' Early Binding
'Add the SOLIDWORKS type library under the Tools->References window in VBA Editor
'Search for PDMWorks Enterprise [Year] Type Library. If this reference is not available in the list use the Browse button to lookup the file in the installation directory
Const sVAULT_NAME As String = "ABG_Vault"
Const sDESIRED_FILE As String = "303-0066"
'
'Type EdmBomInfo
' mlBomID As Integer
' mbsBomName As String
'End Type
Sub mainEarlyBinding()
Dim pdmVault As EdmVault5
Dim search As IEdmSearch7
Dim result As IEdmSearchResult5
Dim epdmfile As IEdmFile7 'had to update IEdmFile5 to 7 for BOM functionality
Dim epdmFolder As IEdmFolder5
Dim sModelName As String
Dim sModelPath As String
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
Dim swPart As SldWorks.ModelDoc2
Dim swDrawing As SldWorks.DrawingDoc
Dim longstatus As Long, longwarnings As Long, longerrors As Long
'Global Const Error Handling
If Len(sVAULT_NAME) = 0 Then
'Relay error
MsgBox "UPDATE CODE - Vault Name is Empty ", vbCritical, "Error"
Exit Sub
End If
If Len(sDESIRED_FILE) = 0 Then
'Relay error
MsgBox "UPDATE CODE - There is no file to Search ", vbCritical, "Error"
Exit Sub
End If
'Vault Login
Set pdmVault = New EdmVault5
pdmVault.LoginAuto sVAULT_NAME, 0 'Will display the standard PDM Login Window if login is required.
If pdmVault.IsLoggedIn Then
'TODO: Implement the routine
Debug.Print "Logged into Vault!"
Else
Err.Raise vbError, "User is not logged in to the vault"
End If
'Vault Search Setup
Set search = pdmVault.CreateUtility(EdmUtility.EdmUtil_Search)
search.Clear
search.FindFolders = False
search.FindFiles = True
search.FileName = sDESIRED_FILE & ".%"
search.Recursive = True
search.SetToken Edmstok_Recursive, True
Set result = search.GetFirstResult
Set epdmfile = Nothing
Set epdmFolder = Nothing
While Not result Is Nothing
Set epdmFolder = pdmVault.GetObject(EdmObjectType.EdmObject_Folder, result.ParentFolderID)
Set epdmfile = pdmVault.GetObject(EdmObjectType.EdmObject_File, result.id)
Set result = search.GetNextResult()
sModelName = epdmfile.Name
If Right(sModelName, 3) = "PRT" Then
sModelName = epdmFolder.LocalPath & "\" & epdmfile.Name
Set swPart = swApp.OpenDoc6(sModelName, 1, 0, "", longstatus, longwarnings) 'Opening the .SLDPRT model
ElseIf Right(sModelName, 3) = "ASM" Then
sModelName = epdmFolder.LocalPath & "\" & epdmfile.Name
Set swPart = swApp.OpenDoc6(sModelName, 2, 0, "", longstatus, longwarnings) 'Opening the .SLDASM model
ElseIf Right(sModelName, 3) = "DRW" Then
sModelName = epdmFolder.LocalPath & "\" & epdmfile.Name
Set swPart = swApp.OpenDoc6(sModelName, 3, 2, "", longstatus, longwarnings) 'Opening the .SLDDRW model
End If
'message = message & vbCrLf & epdmfile.Name & " at " & epdmFolder.LocalPath
Wend
Set swPart = swApp.ActiveDoc
' HERE IS YOUR CODE
Stop
' Close the file
swApp.CloseDoc sModelName
End Sub
any way to get it to search at a lower level instead of the entire vault? Would be much faster.
Is the Logic correct for the search results? Think search.GetNextResult() should happen at the end of the while loop. Possibly skipping the first result?
Set result = search.GetFirstResult
Set epdmfile = Nothing
Set epdmFolder = Nothing
While Not result Is Nothing
Set epdmFolder = pdmVault.GetObject(EdmObjectType.EdmObject_Folder, result.ParentFolderID)
Set epdmfile = pdmVault.GetObject(EdmObjectType.EdmObject_File, result.id)
Set result = search.GetNextResult()
sModelName = epdmfile.Name