Public f, fso

Function ListePrd(prd1 As Product)

Dim products1 As Products
Set products1 = prd1.Products
Dim Val
Val = 0
Val = products1.Count
nbrligne = 0

For iNoeud = 1 To Val
If products1.Item(iNoeud).Products.Count = 0 Then
'Current element is a CATPart
Dim Chaine As String

'Write PartNumber, Name & Mass value for current CATPart.
Chaine = products1.Item(iNoeud).PartNumber & ";" & products1.Item(iNoeud).Name & ";" & products1.Item(iNoeud).Analyze.Mass
f.WriteLine Chaine
nbrligne = nbrligne + 1

Else
'Current element is a sub-Product

Dim Prd2 As Product
Set Prd2 = products1.Item(iNoeud)

'Write PartNumber, Name & Mass value for current sub-Product.
Dim Chaine1 As String
Chaine1 = Prd2.PartNumber & ";" & Prd2.Name & ";" & Prd2.Analyze.Mass
f.WriteLine Chaine1
nbrligne = nbrligne + 1

'Iterate over current sub-Product to collect inertia results for its children and write them in output file.
Call ListePrd(Prd2)
End If
Next

End Function


Sub CATMain()
'Get hold of currently loaded Product document in session.
Dim productDocument1 As ProductDocument
Set productDocument1 = CATIA.ActiveDocument

Dim Product1 As Product
Set Product1 = productDocument1.Product

'Set the path for output file with results.
fichier = "c:\tmp\EXPORT_INERTIA.txt"

'Create the output file for inertia results.
Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
Set f = fso.OpenTextFile(fichier, ForWriting)
If Err.Number <> 0 Then
Set f = fso.CreateTextFile(fichier, True)
End If
On Error GoTo 0

'Write PartNumber, Name & Mass value for Root Product.
Dim Chaine2 As String
Chaine2 = Product1.PartNumber & ";" & Product1.Name & ";" & Product1.Analyze.Mass
f.WriteLine Chaine2
nbrligne = nbrligne + 1

'Iterate over assembly to collect inertia results and write them in output file.
Call ListePrd(Product1)
f.Close

End Sub

