Searching objects in the database and opening them

#vba #3dx

We have created objects and propagated the changes to the database.

Now is the time to see how to open them in the Catia editor.

In Catia V5, we simply had to select the Open method and enter the location of the document.

Dim PartDoc As Document
Set PartDoc = CATIA.Documents.Open(FullDocPath & "\Document-name.CATPart")

In the 3DEXPERIENCE platform, we must search for an object in the database before opening it. In this post, we will use the 'Easy Search' option.

These are the steps we followed:

  1. Create search criteria: create a dictionary with the key attributes to be used for the search. We use a dictionary because the attributes can differ each time and we want to reuse the function.

  2. Search for entities: together with the criteria dictionary and the base type — the type of object to be searched for; in this example, a 'VPMReference' — this function performs the actual search in the database and returns a list of results as 'PLMEntities'.

  3. Process search results: the results of the search are handled, and if only one object is found, it will be loaded in a new editor.

Option Explicit

Sub CatMain()
    Dim SearchCriteria As Object
    Set SearchCriteria = CreateObject("Scripting.Dictionary")
    Call SearchCriteria.Add("PLM_ExternalID", "prd-xxxxxxxx-00000001")
    Call SearchCriteria.Add("revision", "A")

    Dim Entities As PLMEntities
    Set Entities = EasySearchDbObject("VPMReference", SearchCriteria)

    Call ProcessAndOpenSearchResult(Entities)
End Sub

' Searches the database for PLM entities matching the given criteria
Public Function EasySearchDbObject(ByVal BaseType As String, ByVal Criteria As Object) As PLMEntities

    On Error GoTo ErrorHandler
    Dim SearchService As SearchService
    Set SearchService = CATIA.GetSessionService("Search")

    Dim DbSearch As DatabaseSearch
    Set DbSearch = SearchService.DatabaseSearch

    DbSearch.Mode = SearchMode_Easy
    DbSearch.BaseType = BaseType

    Call AddCriteriaToDbSearch(DbSearch, Criteria)

    Call SearchService.Search

    Set EasySearchDbObject = DbSearch.Results

    Exit Function

ErrorHandler:
    Set EasySearchDbObject = Nothing
End Function

' Helper method to add criteria to DbSearch
Private Sub AddCriteriaToDbSearch(ByVal DbSearch As DatabaseSearch, ByVal Criteria As Object)

    Dim Key As Variant
    For Each Key In Criteria.Keys()
        ' If a key is not valid, just jump to the next one
        ' Valid keys are searchable attributes of the PLM entity
        On Error Resume Next
        Call DbSearch.AddEasyCriteria(CStr(Key), Criteria(Key))
        On Error GoTo 0
    Next
End Sub

' Handles the result of a PLM entity search and opens it if found
Private Sub ProcessAndOpenSearchResult(ByVal Entities As PLMEntities)

    If (Entities Is Nothing) Or (Entities.Count = 0) Then
        Call MsgBox("Object not found in the database", vbCritical, "Object not found.")
    ElseIf Entities.Count = 1 Then
        Dim NewEditor As Editor
        Set NewEditor = OpenPlmEntity(Entities.Item(1))
        If NewEditor Is Nothing Then
            Call MsgBox("Failed to open the PLM entity.", vbCritical, "Open Error")
        End If
    Else
        Call MsgBox("More than one object has been found with those criteria. Refine the search to find just one object", _
                      vbCritical, "Multiple object found.")
    End If
End Sub

' Opens a PLM entity and returns the editor
Public Function OpenPlmEntity(ByVal Entity As PLMEntity) As Editor

    On Error GoTo ErrorHandler

    Dim OpenService As PLMOpenService
    Set OpenService = CATIA.GetSessionService("PLMOpenService")

    Dim NewEditor As Editor
    Call OpenService.PLMOpen(Entity, NewEditor)

    Dim ErrorCode As Long
    Dim ErrorMessage As String
    Call OpenService.getLastError(ErrorMessage, ErrorCode)

    If ErrorCode <> 0 Then
        Call MsgBox(ErrorMessage, vbCritical, "Open Error.")
    End If

ErrorHandler:
    Set OpenPlmEntity = NewEditor
End Function