Code:
Sub CATMain()
Dim CATIAApp As Application
Set CATIAApp = CATIA Dim productDoc As ProductDocument
Set productDoc = CATIAApp.ActiveDocument
Dim rootProduct As Product
Set rootProduct = productDoc.Product
' Start Excel
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Dim xlWorkbook As Object
Set xlWorkbook = xlApp.Workbooks.Add
Dim xlSheet As Object
Set xlSheet = xlWorkbook.Sheets(1)
' Headers
xlSheet.Cells(1, 1).Value = "PartNumber"
xlSheet.Cells(1, 2).Value = "Type"
xlSheet.Cells(1, 3).Value = "Description"
xlSheet.Cells(1, 4).Value = "Mass (kg)"
xlSheet.Cells(1, 5).Value = "CoG X (mm)"
xlSheet.Cells(1, 6).Value = "CoG Y (mm)"
xlSheet.Cells(1, 7).Value = "CoG Z (mm)"
xlSheet.Cells(1, 8).Value = "Density"
xlSheet.Cells(1, 9).Value = "Material"
Dim rowIndex As Integer
rowIndex = 2
Call TraverseProduct(rootProduct, xlSheet, rowIndex)
MsgBox ("Gotowe!")
End Sub
Sub TraverseProduct(ByVal prod As Product, ByRef xlSheet As Object, ByRef rowIndex As Integer)
Dim i As Integer
For i = 1 To prod.Products.Count
Dim subProd As Product
Set subProd = prod.Products.Item(i)
If Not subProd.Definition = "MASTER" Then
Dim partType As String
partType = IIf(subProd.Products.Count > 0, "Assembly", "Part")
Dim mass As Double, cogX As Double, cogY As Double, cogZ As Double
Dim oCoords(2)
If TypeName(subProd.ReferenceProduct.Parent) = "PartDocument" Then
Dim subPart As Part
Set subPart = subProd.ReferenceProduct.Parent.Part
Dim density As String
Dim material As String
density = subPart.density
Dim oMatMan As MaterialManager
Set oMatMan = subPart.GetItem("CATMatManagerVBExt")
Dim oMat As material
oMatMan.GetMaterialOnPart subPart, oMat
material = oMat.Name
Else
density = ""
material = "{Mixed}"
End If
On Error Resume Next
Dim inertia 'As inertia
Set inertia = subProd.GetTechnologicalObject("Inertia")
inertia.GetCOGPosition oCoords
If Not inertia Is Nothing Then
mass = inertia.mass
cogX = oCoords(0)
cogY = oCoords(1)
cogZ = oCoords(2)
End If
On Error GoTo 0
xlSheet.Cells(rowIndex, 1).Value = subProd.PartNumber
xlSheet.Cells(rowIndex, 2).Value = subProd.Definition
xlSheet.Cells(rowIndex, 3).Value = subProd.DescriptionRef
xlSheet.Cells(rowIndex, 4).Value = mass
xlSheet.Cells(rowIndex, 5).Value = cogX * 1000
xlSheet.Cells(rowIndex, 6).Value = cogY * 1000
xlSheet.Cells(rowIndex, 7).Value = cogZ * 1000
xlSheet.Cells(rowIndex, 8).Value = density
xlSheet.Cells(rowIndex, 9).Value = material
rowIndex = rowIndex + 1
' Recurse into sub-assemblies
If subProd.Products.Count > 0 Then
Call TraverseProduct(subProd, xlSheet, rowIndex)
End If
End If
Next i
End Sub