How to Export BOM Rows in Structured Order Based on Item Number?

How to Export BOM Rows in Structured Order Based on Item Number?

Dai_Tech
Enthusiast Enthusiast
223 Views
3 Replies
Message 1 of 4

How to Export BOM Rows in Structured Order Based on Item Number?

Dai_Tech
Enthusiast
Enthusiast

Hello everyone,

I am trying to export the rows of a Structured BOM from Autodesk Inventor in the order of Item Number that I have already configured in the BOM. However, when I run my code, the rows do not export in the expected order.

 

Here is my code:

 

Sub Main
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
    oBOM.StructuredViewEnabled = True
    Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
	
	Call QueryBOM(oBOMView.BOMRows)
End Sub

Public Sub QueryBOM(oBOMRows As BOMRowsEnumerator)
	Static CurrenrRow As Integer = 3
	Dim i As Integer
	
	For i = 1 To oBOMRows.Count
		Dim oRow As BOMRow = oBOMRows.Item(i)
		Dim oCompDef As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
		
		 MsgBox(oRow.ItemNumber & "_" & System.IO.Path.GetFileName(oCompDef.Document.FullFileName))
		If Not oRow.ChildRows Is Nothing Then
			Call QueryBOM(oRow.ChildRows)
		End If
	Next	
End Sub

 

 

0 Likes
224 Views
3 Replies
Replies (3)
Message 2 of 4

Dai_Tech
Enthusiast
Enthusiast

Can anyone help me, please?

0 Likes
Message 3 of 4

jnowel
Advocate
Advocate

Does your Top Assy BOM always reflect the correct order of the BOM Rows?
As oftentimes the sorting of the ChildRows from Top Assy BOM Rows is not the same with the how it is sorted in the Sub-Assy BOM.

Below code is a modified one where you use the Sub-Assy BOM for the order of the item number.

Sub Main
	
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
	Call QueryBOM(oBOM, "")
	
End Sub

Public Sub QueryBOM(oBOM As BOM, TabSpace As String)

	oBOM.StructuredViewEnabled = True
	oBOM.StructuredViewFirstLevelOnly = False
	Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
	
	Dim oBOMRows As BOMRowsEnumerator = oBOMView.BOMRows
	Dim i As Integer
	
	For i = 1 To oBOMRows.Count
		Dim oRow As BOMRow = oBOMRows.Item(i)
		Dim oCompDef As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
		
		'MsgBox(oRow.ItemNumber & "_" & System.IO.Path.GetFileName(oCompDef.Document.FullFileName))
		Logger.Info(TabSpace & oRow.ItemNumber & "_" & System.IO.Path.GetFileName(oCompDef.Document.FullFileName))

		If Not oRow.ChildRows Is Nothing Then
			Dim oChildAsm As  AssemblyDocument = oRow.ComponentDefinitions(1).Document
			Call QueryBOM(oChildAsm.ComponentDefinition.BOM, TabSpace & "   ")
		End If
	Next	

End Sub

 

Or if you opt to use how the ChildRows's Item Numbers, I have used a SortedDictionary object as intermediary.
Probably not the best way, but a possible workaround.

Sub Main
	
	Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
    oBOM.StructuredViewEnabled = True
    Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")
	Call QueryBOM(oBOMView.BOMRows, "")

End Sub

Public Sub QueryBOM(oBOMRows As BOMRowsEnumerator, TabSpace As String)

	Dim oBOMROW_Dict As New SortedDictionary(Of String, BOMRow)
	
	For Each oBOMRow As BOMRow In oBOMRows
		 If Not oBOMROW_Dict.ContainsKey(oBOMRow.ItemNumber) Then
			 oBOMROW_Dict.Add(oBOMRow.ItemNumber,oBOMRow)
		 End If
	Next
	
	Static CurrenrRow As Integer = 3
	'Dim i As Integer

	For Each key In oBOMROW_Dict	
	'For Each oRow As BOMRow In oBOMRows

		Dim oRow As BOMRow = key.Value
		Dim oCompDef As ComponentDefinition = oRow.ComponentDefinitions.Item(1)
		
		Logger.Info(TabSpace & oRow.ItemNumber & "_" & System.IO.Path.GetFileName(oCompDef.Document.FullFileName))
		
		If Not oRow.ChildRows Is Nothing Then
			Call QueryBOM(oRow.ChildRows, TabSpace & "   ")
		End If
		
	Next
	
End Sub

 

0 Likes
Message 4 of 4

Dai_Tech
Enthusiast
Enthusiast

Thank you, sir, nice idea!!! However, your code has an issue with the "SortedDictionary function" (e.g., 2.1 -> 2.10 results in: 2.1, 2.10, 2.2, 2.3...2.9). But I've fixed it already. Thank you so much!

Dai_Tech_1-1733121505268.png

 

Fixed

Dai_Tech_0-1733121450547.png

 

 

0 Likes