1

Sorry but I'm a total newbie in CATScript.
But I'm looking for a solution that will provide me to check every node in my Product structure recursively. I try to adopt the Fibonacci procedure:

Function Fib(n As Long) As Long    
    Dim first As Long
    Dim second As Long
    Dim sum As Long
    Dim i As Long
    
    first = 0
    second = 1
    sum = 0
    
    If n = 0 Then
        Fib = first
    ElseIf n = 1 Then
        Fib = second
    Else
        For i = 2 To n
            sum = first + second
            first = second
            second = sum
        Next i
        Fib = sum
    End If
    
End Function

with this:

Private Sub TestMain    
    Dim searchName As String
    searchName = "SearchName"

    ' Start with the selected object
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    Dim prod As Product
    Set prod = doc.Product

    Dim foundItem As Object
    foundItem = TestMainChildren(doc.Selection.Item(1).Value, searchName)

    MsgBox "Found: " & foundItem.Name
End Sub

Private Function TestMainChildren(ByRef catiaObject As Object, ByVal searchName As String) As Object
    Dim item As Object
    For Each item In catiaObject.Items
        If item.Name = "SearchName" then
            Set TestMainChildren = item
            Exit For
        End if

        Dim catiaType As String
        catiaType = TypeName(item)
        If catiaType = "Product" Then
            TestMainChildren item, searchName
        End If
    Next
End Sub

but I have no idea how to do this. Can anybody help here?

1 Answers1

2

It depends on what you want, but it is often very useless to check all the instances whith a recursive loop. what is your end goal?

i suggest you to check every instance opened :

 Sub main()  
      Dim d As Document
        For Each d In CATIA.Documents
          Dim p As Product
          Set p = d.Product
          MsgBox (p.Name)
        Next
    End Sub

If you insist and really want a recursive loop :

    Sub main()
      Dim d As Document
      Set d = CATIA.ActiveDocument

      Dim p As Product
      Set p = d.Product
      
      Call RecursiveAllProducts(p) 'here your recursive starts     
    End Sub

Sub RecursiveAllProducts(p As Product) 'your recursive

    MsgBox (p.PartNumber)
    If p.Products.Count > 0 Then
        For i = 1 To p.Products.Count
            Dim p_ As Product
            Set p_ = p.Products.Item(i)
          Call RecursiveAllProducts(p_) 'you call your recursive again
        Next i
    End If

End Sub
Disvoys
  • 61
  • 1
  • 7