Selection generic function

#vba #v5 #3dx

If you have experience programming in V5 and 3dx, you are likely aware that the selection object is accessed through the Document or the Editor, respectively.

In this post, we have explored how to share code between both applications. One potential solution could be:

Dim ActiveSelection As Selection

If CATIA.System.Configuration.Version = 5 Then
    Set ActiveSelection = CATIA.ActiveDocument.Selection
    
ElseIf CATIA.System.Configuration.Version = 6 Then
    Set ActiveSelection = CATIA.ActiveEditor.Selection
    
End If

This solution is designed for the active document or editor. To enhance its reusability and allow selection from other documents or editors without needing to activate them, a more generic function can be created:

Dim ActiveSelection As Selection
Set ActiveSelection = GetSelection(CATIA.ActiveDocument)  ' or CATIA.ActiveEditor

' The following function can be created in a separated Module or Class
Public Function GetSelection(ByVal WorkingContext As Variant) As Selection
    On Error GoTo ErrorHandler
    Set GetSelection = WorkingContext.Selection
    ExitFunction
    
ErrorHandler:
    On Error GoTo 0
    Debug.Print "Selection error " & Err.Description
    ExitFunction
End Function