Search code examples
vbacatia

CATIA VBA Measure a user selected line(s)/spline


I am trying to get the length of user selected lines/splines

This is the code I'm using to have users select their lines:

Dim USel As Selection
Dim USelLB
Dim InputObject(0)

InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel

USel.Clear
USelLB.Clear

Linestomeasure = USelLB.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)

Linestomeasure is a public variable, in the mainsub i've been trying to measure Linestomeasure using the following code:

Dim pd1 As PartDocument
Dim a As Object
Dim c As Reference

a = TrimLines.Item(1)
c = pd1.Part.CreateReferenceFromObject(a)

Dim Mea1 As Measurable
Dim TheSPAWorkbench As SPAWorkbench
Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")
Set Mea1 = TheSPAWorkbench.GetMeasurable(c)

But when I run the code a = trimLines.Item(1) gets highlighted in the debugger with the error message "Object Required".

Does anyone have an idea on how I can change my code so that I can get the length of the line as a variable that I can work with ? Or just a different way to go about what I'm trying to do?


Solution

  • Edited answer to reflect comment bellow

    Looks like you are assigning the wrong type of variable to the USelLB.SelectElement3 and also missunderstanding how it actually works.

    The Selection.SelectElement3 returns a String that reflects whether the selection was sucessfull or not.

    The Object retrieved from the Selection is inside the Selection.Item(Index)

    Your code should be something like this:

    Dim PD1 as PartDocument
    Dim Sel 'as Selection 'Sometimes it is needed to comment the selection to use the .SelectElement3 method
    Dim InputObjType(0)
    Dim SelectionResult as string    
    Dim LineToMeasure as AnyObject    
    Dim I as Integer    
    Dim SpaWorkbench as SPAWorkbench
    Dim Measurable as Measurable
    
    InputObjType(0) = "AnyObject"
    
    'set PD1 = Catia.ActiveDocument
    set Sel = PD1.Selection
    Set TheSPAWorkbench = pd1.GetWorkbench("SPAWorkbench")    
    
    Sel.Clear
    SelectionResult= Sel.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
    
    If SelectionResult = "Ok" or SelectionResult = "Normal" then 'Check if user did not cancel the Selection
        For i = 1 to Selection.Count
            Set LineToMeasure = Sel.Item(i).Value
            set Measurable = SpaWorkbench.GetMeasurable(LineToMeasure)
    
            'Measure whatever you need here.
        Next
    End If
    

    Keep in mind that using the AnyObject type filter may cause the user to select unwanted objects. You shoudl use a more specific filter.