For now, let’s start with some API fundamentals for SolidWorks Enterprise PDM! I had someone ask about how to compare files that are outside of EPDM with files already in the system. For example, if a contract design group sends you a batch of files for a design, how do you know what to put back in EPDM? The file set may contain common parts as well as new parts, assemblies and drawings. Trying to sift through the file set to figure out what you should add to EPDM could be extremely tedious. I can think of four or five activities involving sticks, rocks and dirt that would be more invigorating. Yet another chance to automate!
Connecting to EPDM
As a starting point, fire up a new SolidWorks VSTA VB.NET macro. Alternatively, you could begin with a new Visual Studio project. The sample code below assumes using a macro. Once you’ve started the project, add a reference to the SolidWorks Enterprise PDM library. Select Project, Add Reference. You should find it under the COM tab named PDMWorks Enterprise 20XX Type Library.
Open the SolidWorksMacro.vb code file if it isn’t open already and add an Imports statement to add EdmLib (the EPDM library). Add the following code under the main procedure.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconstImports System.Runtime.InteropServices
Imports System
Imports EdmLib
Partial Class SolidWorksMacro
Public Sub main()
Dim vaultName As String = "EPDMVault"
Dim CompareFolder As String = "C:\EPDM Test"
'create a vault connection and log in
Dim v As New EdmVault5
v.LoginAuto(vaultName, 0)
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class
Logging into EPDM
There’s not much to creating a connection to a local vault view and logging in. The EdmVault5 interface just needs to be declared with the New keyword to connect. The LoginAuto method is identical to a user opening the local view folder. If they’re not currently logged in, the system will attempt to log them in using their Windows login. If that fails, the login prompt is displayed. The first argument for LoginAuto is the name of the vault. The second argument is to help EPDM know what the calling application is (it’s window handle as an integer). Since I’ve used a SolidWorks macro, I’m simply passing 0 to essentially ignore the argument. It’s really a shortcut for something less than critical.
Creating a Search Interface
To compare files between the vault and an outside folder, we’ll use the EPDM search capability. Using search through the API isn’t much different than using it through the client software. Add the following code to the procedure to include the search definition. We’ll fill out the details and run the search next.
Public Sub main()
Dim vaultName As String = "EPDMVault"
Dim CompareFolder As String = "C:\EPDM Test"
'create a vault connection and log in
Dim v As New EdmVault5
v.LoginAuto(vaultName, 0)
'create the search interface and initial settings
Dim search As IEdmSearch5
search = v.CreateSearch
search.FindFiles = True
search.FindFolders = False
End Sub
The search interface type is IEdmSearch5. You create a search interface by calling EdmVault5.CreateSearch. It has a handful of different settings. Since we’re interested in finding files, we’ve set the FindFiles property to True and the FindFolders property to false.
Get the List of Files to Compare
Before we start utilizing the file and directory methods from VB.NET, it’s easiest to add a reference to the System.IO namespace. Add the following code to import System.IO by the rest of the Imports statements.
...
Imports EdmLibImports System.IO
Once that’s been added, you can use FileInfo and DirectoryInfo classes from System.IO to get all files in a given folder path. The code is finally using the CompareFolder variable to get the compare folder.
Public Sub main()
Dim vaultName As String = "EPDMVault"
Dim CompareFolder As String = "C:\Test Folder"
'create a vault connection and log in
Dim v As New EdmVault5
v.LoginAuto(vaultName, 0)
'create the search interface and initial settings
Dim search As IEdmSearch5
search = v.CreateSearch
search.FindFiles = True
search.FindFolders = False
'get the file names from the folder to compare
Dim searchDir As New DirectoryInfo(CompareFolder)
Dim files As FileInfo()
files = searchDir.GetFiles("*.*", _
SearchOption.AllDirectories)
Stop
End Sub
If you run the macro to this point, you can review the array named files once you hit the Stop point. It should have an array of FileInfo objects that include the name of every file in the test folder.
Now that we have an array of file names, we can run an EPDM search on each one. If we find a match in EPDM, we’ll add that into a collection to later report back to the user.
Running a Search
Depending on how you have your vault configured, a search for a file name in EPDM might possibly return multiple files. Since we’re interested in simply knowing if the file exists at all in the vault, we’ll only look for the first result. If there isn’t a first result, we can confidently assume the file doesn’t exist in the vault.
Add the rest of the macro shown below. If you’re looking for a shortcut, this is the entire macro.
Public Sub main()
Dim vaultName As String = "EPDMVault"
Dim CompareFolder As String = "C:\Test Folder"
'create a vault connection and log in
Dim v As New EdmVault5
v.LoginAuto(vaultName, 0)
'create the search interface and initial settings
Dim search As IEdmSearch5
search = v.CreateSearch
search.FindFiles = True
search.FindFolders = False
'get the file names from the folder to compare
Dim searchDir As New DirectoryInfo(CompareFolder)
Dim files As FileInfo()
files = searchDir.GetFiles("*.*", _
SearchOption.AllDirectories)
'collect any duplicates found in EPDM
Dim duplicates As New Collection
For Each f As FileInfo In files
'run the search for this file name
search.FileName = f.Name
Dim sr As IEdmSearchResult5
sr = search.GetFirstResult
If Not sr Is Nothing Then
'found a file in the vault with the same name
duplicates.Add(f.Name)
'optionally, get a local copy of the file
Dim eFile As IEdmFile5
eFile = sr 'get the file from the search result
eFile.GetFileCopy(0) 'get the latest file copy
Next
'report back the files found
Dim myMessage As String = "Duplicate file list:" & vbCrLf
For Each dup As String In duplicates
myMessage = myMessage & dup & vbCrLf
Next
MsgBox(myMessage & vbCrLf & duplicates.Count & _
" duplicate files", MsgBoxStyle.Information)
'reset the collection
duplicates = Nothing
End Sub
I’ve used a Collection to keep track of the files found during the search. It was easier than an array since I could simply add new elements and didn’t have to worry about resizing.
The EPDM search is run by setting the FileName property and then calling the GetFirstResult method. The rest is simply formatting a message to return the list to the user.
I've also added a few additional lines of code to get a local file copy in the vault. You need to first get the IEdmFile5 interface (directly from the IEdmSearchResult5 interface). There are several optional arguments to the GetFileCopy method, but if you're just getting the latest version, you can use the exact syntax posted.
I've also added a few additional lines of code to get a local file copy in the vault. You need to first get the IEdmFile5 interface (directly from the IEdmSearchResult5 interface). There are several optional arguments to the GetFileCopy method, but if you're just getting the latest version, you can use the exact syntax posted.