PDM Searching with API

Programming and macros
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

PDM Searching with API

Unread post by MMartens »

I have a generated list of part numbers as a source. I am trying to get the path name of each part in PDM. Currently the only way I have figured out is to iterate through each item in my list, search it, and get the path. This is very time consuming as running the search multiple times takes approximately 2-3 seconds each. My list is currently 75 to 80 numbers long and will be growing in the future.

I am looking for a way to search multiple filenames. In other words generate a search with OR operators to compile a search result containing the files and then iterate through each search result and get the path. I am able do this in the quick search and search cards...
image.png
image.png (1.86 KiB) Viewed 5485 times
but need a method through API.
by MMartens » Wed Apr 28, 2021 3:27 pm
It is something that will run everytime someone invokes it. After seeing someone else's code I realized why it wasn't working. I had used CreateSearch method instead of CreateSearch2. I had glossed over the instruction in the search syntax section of the API help.
Go to full post
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: PDM Searching with API

Unread post by bnemec »

I'm assuming you have PDM Pro. Do you like C# or VB? You haven't mentioned what you have started with as far as code so This link gives an overview of some PADM API concepts.


https://help.solidworks.com/2019/Englis ... 8dc4969c3b

Since you're searching this link should lead you to examples, they are searching by state, but you just change the search term to be your number.

https://help.solidworks.com/2019/Englis ... de4c3a#Pg0

Does that help?
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

Re: PDM Searching with API

Unread post by MMartens »

I tried using OR operators in a string in the EDMSearch.Filename.

I tried using BeginOR/EndOR with setting the Filename property multiple times in between.

I tried using SetToken with Edmstok_Name argument and OR operators in the search term argument and using BeginOR/EndOR and call SetToken multiple times.

It seems I need to use BeginOR/EndOR and AddVariable2 but I haven't been able to figure out what the variable name argument needs to be to get it to look at the filenames.

edit: I use VB.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: PDM Searching with API

Unread post by bnemec »

Sorry, my links were probably too elementary for you based on what you're saying.

Are you searching for file name or data card variable? I was assuming Part Number variable, but now you're saying Edmstok_Name. If you're just looking for files that match filename then I don't see why you need to mess around with the OR clause.

Here's what I'm thinking

Code: Select all

- get your list of part numbers (file names) into a KeyValue<string,string> partNums.  maybe paste them into a listbox in a Form?  Key can be part number, value empty.
- create a search
- foreach(pn in partNumss) 
  {
   - myPNsearch.SetToken ( Edmstok_Name, pn.key )
   _ while (searchresult != null)
      {
       - do some logic to verify results if you want the set pn.value = path you found
      }
   - myPNsearch.clear
   }
Or am I missing some requirement? I'm still assuming you have a simple list of ~100 part numbers. Or is that list from a PDM search in the vault view and you want to set up that search in API now?
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

Re: PDM Searching with API

Unread post by MMartens »

My list is a CSV report run from our ERP. I was always able to find each file when I searched similar to how you laid out. What I am trying to avoid is running the search for each item in the list. The individual search for each item is causing my code to take over 5 minutes to run with just 80-100 items. My number of items will be growing and will just cause the load time to increase. If I could generate a search result list using OR operators I could drastically reduce the time needed to get past that step of my process.

You are correct in that I am trying to replicate a PDM search and generate a search result list containing the files for each item of my list.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: PDM Searching with API

Unread post by bnemec »

I see.
Help page mentions there's overhead in creating the IEdmSearch5 object, did you create and dispose the search each iteration? Maybe that was causing it to be slow?
User avatar
AlexB
Posts: 508
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 29
x 274
x 462

Re: PDM Searching with API

Unread post by AlexB »

One way that you may be able to get around this is to do the following:

1. Perform a search of the vault with filename "%.sld%" specified.
2. Iterate through each of the results adding them to a dictionary where the key is the filename and the value is the path.
3. For each of the files in your CSV, get the value for the from your dictionary instead of searching the vault for each item

Depending on your vault size, this initial search may be "expensive" however I believe the overall time searching for several files in your vault will eventually save you time.

Disclaimer: I haven't actually tested this method but something similar has worked for me in the past
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

Re: PDM Searching with API

Unread post by MMartens »

I misspoke earlier when I said my list was 80-100 items. It is actually ~240 items.

I tried creating the search object once and doing a clear search after each item like you suggested. This definitely has helped. The load time is down to ~2:15. I would like to be able to get it down further. I would like to see how long it takes if I could get the multiple filename search to work. I think that would be the key to getting a decent load time.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: PDM Searching with API

Unread post by bnemec »

MMartens wrote: Wed Apr 28, 2021 1:30 pm I misspoke earlier when I said my list was 80-100 items. It is actually ~240 items.

I tried creating the search object once and doing a clear search after each item like you suggested. This definitely has helped. The load time is down to ~2:15. I would like to be able to get it down further. I would like to see how long it takes if I could get the multiple filename search to work. I think that would be the key to getting a decent load time.
80, 10, 200, shouldn't matter that much. Is this a once a day report type thing or something that users are triggering regularly? If once a day then I'd say a few minutes is fine. If it's something users are doing regularly and you expect that number to grow, I'd consider querying the Database directly. How comfortable are you with SQL in VB?

Edit: another thought, you probably already know this, but the API search is almost no different then the GUI search. So the more terms that it's searching the longer it's going to take. I assume you're not searching all versions, and you've set it to only search files, not folders
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

Re: PDM Searching with API

Unread post by MMartens »

It is something that will run everytime someone invokes it. After seeing someone else's code I realized why it wasn't working. I had used CreateSearch method instead of CreateSearch2. I had glossed over the instruction in the search syntax section of the API help.
User avatar
bnemec
Posts: 1954
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2562
x 1411

Re: PDM Searching with API

Unread post by bnemec »

MMartens wrote: Wed Apr 28, 2021 3:27 pm It is something that will run everytime someone invokes it. After seeing someone else's code I realized why it wasn't working. I had used CreateSearch method instead of CreateSearch2. I had glossed over the instruction in the search syntax section of the API help.
Good to hear you got it working. That sounds like my kind of thing to do. Can you mark your solution post as best answer?
Bananaman
Posts: 1
Joined: Tue Jun 28, 2022 12:12 am
Answers: 0

Re: PDM Searching with API

Unread post by Bananaman »

Hey @MMartens. I was planning on attempting something very similar to what you've done. Was there any chance you'd be able to share what you've made with me to give me a bit of a head start?
MMartens
Posts: 36
Joined: Wed Apr 28, 2021 10:01 am
Answers: 6
Location: Shawano, WI USA
x 6

Re: PDM Searching with API

Unread post by MMartens »

Here is my sub I use...

Code: Select all

Function getFileLocations(names As String)  'use PDM search to find files
    Dim pdmVault As EdmLib.EdmVault5
    Dim pdmBatchGetUtil As EdmLib.IEdmBatchGet
    Dim pdmSearch As EdmLib.IEdmSearch9
    Dim searchResult As EdmLib.IEdmSearchResult5

    Set pdmVault = CreateObject("ConisioLib.EdmVault")
    pdmVault.LoginAuto "EmtRoto", 0
    Set pdmBatchGetUtil = pdmVault.CreateUtility(EdmUtil_BatchGet)
    
    If Not pdmVault.IsLoggedIn Then
      Err.Raise vbError, "User is not logged in to the vault"
      Exit Function
    End If
    
    Dim pdmFolder As EdmLib.IEdmFolder12
    Set pdmSearch = pdmVault.CreateSearch2
    Set pdmFolder = pdmVault.GetFolderFromPath([i]YOUR ROOT FOLDER HERE[/i])
    With pdmSearch
      .SetToken Edmstok_AllVersions, 0
      .StartFolderID = pdmFolder.ID
      .Filename = names
      .FindFiles = True
      .FindFolders = False
      .FindHistoricStates = False
    End With
    Set searchResult = pdmSearch.GetFirstResult
    If searchResult Is Nothing Then
      Exit Function
    End If
    
    Dim pdmFile As EdmLib.IEdmFile5
    Dim pdmSelItems() As EdmLib.EdmSelItem
    Dim i, n As Integer
    
    i = 0
    ReDim pdmSelItems(0 To 2)
    'get the latest version of file
    Do While Not searchResult Is Nothing
      Set pdmFile = pdmVault.GetFileFromPath(searchResult.Path, pdmFolder)
      If pdmFile.GetLocalVersionNo(pdmFolder) <> pdmFile.CurrentVersion Then
        ReDim Preserve pdmSelItems(0 To i)
        pdmSelItems(i).mlDocID = pdmFile.ID
        pdmSelItems(i).mlProjID = pdmFolder.ID
        i = i + 1
      End If
      Set searchResult = pdmSearch.GetNextResult
    Loop
        
    pdmBatchGetUtil.AddSelection pdmVault, pdmSelItems
    pdmBatchGetUtil.CreateTree 0, EdmLib.EdmGetCmdFlags.Egcf_RefreshFileListing
    pdmBatchGetUtil.GetFiles 0
        
End Function
The "names" variable can be a single name or several names separated by a space.
Ziggins
Posts: 1
Joined: Wed Nov 02, 2022 5:58 pm
Answers: 0

Re: PDM Searching with API

Unread post by Ziggins »

Another solution is to get access to the SQL database itself. I just ran a query to cross reference between Dynamics AX 2012 items, and PDM files. 500k items came back with file locations based on 2 different search criteria's (Card variables, and file names) in about 27 seconds total. Searching for <500 files/items would be sub second.
Post Reply