'Saving the product tree of a CATpart into a txt file

I need to extract the product tree of the CATpart from CATIA. I want to do this using macro's and I have code that works for CATproducts:

Sub CATMain()
Dim productDocument1
Set productDocument1 = CATIA.ActiveDocument
'Input box to select txt or xls
Dim exportFormat
exportFormat = "txt" 
'Input box to enter name of file
Dim partName
partName = Inputbox ("Please enter the file name.")
'Input box to enter file location
Dim oLocation
oLocation = "C:\Users\xvreeswijk\Documents\Programs\Input\"
productDocument1.ExportData oLocation & partName & "." & _
exportFormat,"txt"
End Sub

But when I want to use this for a CATpart I get the error: The method of ExportData failed. Is this possible using vba macro's or is another way easier?



Solution 1:[1]

Here a example to get the names of bodies and hybridbodies of a part (only top level entries)

Sub CATMain()

    Dim oPartDocument as PartDocument
    Dim sListofBodies as String
    Dim oBody as Body
    dim oHybridBody as Hybridbody

    Set oPartDocument = CATIA.ActiveDocument
    Set oPart = oPartDocument.Part
    
    for each oBody in oPart.Bodies
        if Not oBody.InBooleanOperation then
            sListofBodies = sListofBodies & oBody.Name & Chr(10)
        end if  
    next
    
    MsgBox sListofBodies

    sListofBodies = ""
    
    for each oHybridBody in oPart.HybridBodies
        sListofBodies = sListofBodies & oHybridBody.Name & Chr(10)  
    next

    MsgBox sListofBodies

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shrotter