Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
jnowel
in reply to: Dai_Tech

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