Alright this exports the info to an excel file in the same folder with the same name as the assembly you run it in.
I'm kinda jealous of iLogic since it has simple excel functions for reading and writing unlike VBA. Though there isn't a way to create a new excel with those nifty functions that I saw.
I also added a few additional remarks to help people know what's going on.
Sub Main
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
' Set a reference to the BOM
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
oBOM.StructuredViewFirstLevelOnly = False
oBOM.StructuredViewEnabled = True
'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.item("Structured")
'Create a dictionary to store paint color and area in
Dim Colors As Object
Colors = CreateObject("Scripting.Dictionary")
'Call subroutine to calculate paint areas
Call PaintRecurse(oBOMView.BOMRows, Colors)
'Set output file name
xls = Replace(ThisDoc.Document.fullfilename,".iam",".xlsx")
'Check if output file exists, if not create it
If (Dir(xls) = "") Then
excelApp = CreateObject("Excel.Application")
excelWorkbook = excelApp.Workbooks.Add
excelWorkbook.SaveAs(xls)
excelWorkbook.Close
End If
'Open output file for writing
GoExcel.Open(xls, "Sheet1")
'Add header info
GoExcel.CellValue(xls, "Sheet1", "A1") = "Color"
GoExcel.CellValue(xls, "Sheet1", "B1") = "Area"
num = 2
'Loop through each color and add to the excel file
For Each item In Colors
GoExcel.CellValue(xls, "Sheet1", "A" & num) = item
'Area is returned in cm^3 by default so multiply by factor to get m^3
GoExcel.CellValue(xls, "Sheet1", "B" & num) = Colors(item) * .0001
GoExcel.CellValue(xls, "Sheet1", "C" & num) = "m^3"
num = num + 1
Next
'Save and Close excel file
GoExcel.Save
GoExcel.Close
MsgBox("Paint colors exported to " & xls)
End Sub
Private Sub PaintRecurse(oBOMRows As BOMRowsEnumerator, Colors As Object, Optional SubQty As Integer = 1)
On Error Resume Next
' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.count
' Get the current row.
Dim oRow As BOMRow
oRow = oBOMRows.item(i)
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
oCompDef = oRow.ComponentDefinitions.item(1)
'Checks if current item is a part, skip if not a part
If (oCompDef.Document.DocumentType = kPartDocumentObject) Then
'Set a reference to the faces on the current part
Dim oFaces As Faces
oFaces = oCompDef.SurfaceBodies(1).Faces
'Loop through all faces of part and add the appearance and area to the dictionary
Dim oFace As Face
For Each oFace In oFaces
Colors(oFace.Appearance.DisplayName) = Colors(oFace.Appearance.DisplayName) + oFace.Evaluator.Area * oRow.ItemQuantity * SubQty
Next
End If
'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then Call PaintRecurse(oRow.ChildRows, Colors, oRow.ItemQuantity * SubQty)
Next
End Sub