Walk the CATIA V6 Product Structure with VBA

Walk the CATIA V6 Product Structure with VBA

The code below recursively walks down a CATIA V6 Product Structure using VBA setting an attribute value as its goes.

Sub Main()

    Dim ioCATIA As Application
    Set ioCATIA = CATIA
    
    Dim ioActiveEditor As Editor
    Set ioActiveEditor = ioCATIA.ActiveEditor
    
    Dim ioActiveObject As VPMRootOccurrence
    Set ioActiveObject = ioActiveEditor.ActiveObject
    
    Dim ioVPMReference As VPMReference
    Set ioVPMReference = ioActiveObject.ReferenceRootOccurrenceOf
    
    ioVPMReference.SetAttributeValue "Manufacturer", "Bob" ' This Works
    
    Dim ioOccurences As VPMOccurrences
    Set ioOccurences = ioActiveObject.Occurrences
    
    Dim ioBoolean As Boolean
    ioBoolean = NavigateProductStructure(ioOccurences)

End Sub

Function NavigateProductStructure(ioOccurences As VPMOccurrences) As Boolean

    Dim ioIndex As Integer
    For ioIndex = 1 To ioOccurences.Count

        Dim ioOccurence As VPMOccurrence
        Set ioOccurence = ioOccurences.Item(ioIndex)
        
        Dim ioVPMInstance As VPMInstance
        Set ioVPMInstance = ioOccurence.PLMEntity
        
        Dim ioVPMReference As VPMReference
        Set ioVPMReference = ioVPMInstance.ReferenceInstanceOf
        ioVPMReference.SetAttributeValue "Manufacturer", "Bob" ' This Works
        
        MsgBox (ioOccurence.Name)
        Dim ioBoolean As Boolean
        ioBoolean = NavigateProductStructure(ioOccurence.Occurrences)
    
    Next

End Function