Hi @nick5MWHR. That earlier code example simply iterated down through all levels of assembly components, capturing component name, referenced document file name, and what primary level the component was on in the overall structure of the assembly. However, since you seem to prefer how the structured BOM with all levels included does its Item Number values, I decided that we could do this process by iterating through the structured view of the BOM directly, then just use the Item Number property of the BOMRow for the 'level'. So, below is a very similar code example that is recursively iterating through the rows in a BOM view, instead of recursively iterating through assembly components.
Just remember, down around Lines 19 & 20 is where the Excel file and sheet name are being specified, so you will most likely need to change those, as needed. This code does not create a new Excel file...just opens one, then writes this data into it. So, if that Excel file does not already exist, this will not work. And if the Excel file does exist, make sure the sheet name is correct. And, if the file and sheet names are OK, but the file already contained other, older data, this code will not clear that old data before it writes the new data into it. If old data exists, it will be overwritten. However if the old data had 400 lines of data, and the new data being written to it only has 200 lines of data, the result will be the first 200 lines being new data, but the other 200 below that still being old data. Just something to keep in mind. Using the iLogic GoExcel tools is a trade-off of (simplicity & ease of use) vs (more dynamic control).
Sub Main
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
If oADoc Is Nothing Then Logger.Debug(iLogicVb.RuleName & " exited (no AssemblyDocument obtained)") : Return
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oBOM As Inventor.BOM = oADef.BOM
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
Dim oStrBOMView As BOMView = Nothing
For Each oBOMView As BOMView In oBOM.BOMViews
If oBOMView.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
oStrBOMView = oBOMView
Exit For
End If
Next oBOMView
If oStrBOMView Is Nothing Then Return
oData = New List(Of List(Of String)) 'to initialize it
RecurseBOMRows(oStrBOMView.BOMRows, AddressOf ProcessBOMRow)
If oData.Count > 0 Then
Dim sExcelFile As String = "C:\Temp\Assembly file & component names with levels.xlsx"
Dim sSheet As String = "Sheet1"
GoExcel.Open(sExcelFile, sSheet)
GoExcel.DisplayAlerts = True
Dim iFirstDataRow As Integer = 2
Dim iRow As Integer = iFirstDataRow
GoExcel.CellValue("A1") = "File Name"
GoExcel.CellValue("B1") = "Component Name"
GoExcel.CellValue("C1") = "Level"
For i As Integer = 0 To oData.Count - 1
Dim oDataEntry As List(Of String) = oData.Item(i)
'Dim sFileName As String = oDataEntry.Item(0)
'Dim sCompName As String = oDataEntry.Item(1)
'Dim sLevel As String = oDataEntry.Item(2)
GoExcel.CellValues("A" & iRow.ToString, "C" & iRow.ToString) = oDataEntry
iRow = iRow + 1
Next i
GoExcel.Save
End If
'MsgBox("This rule's work has finished.",,"Job Is Done!")
End Sub
Dim oData As List(Of List(Of String))
Sub RecurseBOMRows(oBOMRows As BOMRowsEnumerator, BOMRowProcess As Action(Of Inventor.BOMRow))
If oBOMRows Is Nothing OrElse oBOMRows.Count = 0 Then Return
For Each oBOMRow As Inventor.BOMRow In oBOMRows
BOMRowProcess(oBOMRow)
If oBOMRow.ChildRows IsNot Nothing AndAlso oBOMRow.ChildRows.Count > 0 Then
RecurseBOMRows(oBOMRow.ChildRows, BOMRowProcess)
End If
Next oBOMRow
End Sub
Sub ProcessBOMRow(oBOMRow As Inventor.BOMRow)
If oBOMRow Is Nothing Then Return
Dim oCD As Inventor.ComponentDefinition = Nothing
Dim oRowDoc As Inventor.Document = Nothing
Dim oOcc As ComponentOccurrence = Nothing
Try : oCD = oBOMRow.ComponentDefinitions.Item(1) : Catch : End Try
Try : oRowDoc = oCD.Document : Catch : End Try
Try : oOcc = oBOMRow.ComponentOccurrences.Item(1) : Catch : End Try
If (oCD Is Nothing) OrElse (oRowDoc Is Nothing) OrElse (oOcc Is Nothing) Then Return
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(oRowDoc.FullFileName)
Dim sOccName As String = oOcc.Name
Dim sLevel As String = oBOMRow.ItemNumber
Dim oDataEntry As New List(Of String) From {sFileName, sOccName, sLevel}
oData.Add(oDataEntry)
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)