Hi, try the next rule driven from the master assembly.
Sub Main
doc = ThisDoc.Document
Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition
Dim oBOM As BOM = oAssyDef.BOM
oBOM.StructuredViewFirstLevelOnly = False
oBOM.StructuredViewEnabled = True
oBOM.PartsOnlyViewEnabled = True
Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item(3) ' Parts Only
Dim oBOMRowPO As BOMRow
For Each oBOMRowPO In oBOMViewPO.BOMRows
On Error Resume Next
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As ComponentDefinition
oCompDef = oBOMRowPO.ComponentDefinitions.Item(1)
Dim CompFileNameOnly As String
CompFileNameOnly = oCompDef.Document.Displayname
'MessageBox.Show(CompFileNameOnly)
Dim Qty As String
Qty = oBOMRowPO.TotalQuantity
iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = Qty
Next
Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item(2) 'Structured
Dim oBOMRowStruc As BOMRow
Dim arrSubAssemblyList As New ArrayList
Call QueryBOMRowProperties(oBOMViewStruc.BOMRows, arrSubAssemblyList, 1)
End Sub
Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, arrSubAssembly As ArrayList, oParentQty As Integer)
Dim i As Long
For i = 1 To oBOMRows.Count
Dim oBOMRowStruc As BOMRow = oBOMRows.Item(i)
Dim oCompDef As ComponentDefinition = oBOMRowStruc.ComponentDefinitions.Item(1)
Dim oQty As Integer
If TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
Dim CompFileNameOnly As String
CompFileNameOnly = oCompDef.Document.Displayname
'MessageBox.Show(CompFileNameOnly)
oQty = oBOMRowStruc.ItemQuantity * oParentQty
Dim additionalQty As Integer
If arrSubAssembly.Count <> 0 Then
Dim counter As Integer = 0
For Each CompData As String In arrSubAssembly
Dim commaindex As Integer = CompData.IndexOf(":")
Dim CompName As String = CompData.Substring(0,commaindex)
If CompName = CompFileNameOnly Then
additionalQty = CompData.Substring(commaindex+1)
Else
counter += 1
End If
Next
If additionalQty = 0 Then
arrSubAssembly.Add(CompFileNameOnly & ":" & oQty)
Else
arrSubAssembly(counter) = CompFileNameOnly & ":" & oQty + additionalQty
End If
Else
arrSubAssembly.Add(CompFileNameOnly & ":" & oQty)
End If
iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = oQty + additionalQty
'Recursively iterate child rows if present.
If Not oBOMRowStruc.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oBOMRowStruc.ChildRows, arrSubAssembly, oQty)
End If
End If
Next
End Sub
I hope this helps with your problem. Grettings!
Please accept as solution and give likes if applicable.
I am attaching my Upwork profile for specific queries.
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn