I'm trying to generate a script that sorts and renumbers an assembly BOM, based on the iProperty for part number with each row.
The part numbers for "Make" items are structured differently than those for "Buy" items. "Make Items" part numbers start with 3 numbers, then have "-D-M" and then three or sometimes four numbers after that. All other part numbers are "buy" items.
I'd like to run the script and have both "Make" and "Buy" items sorted by part number within their groups, but then re-numbered so the "Make" items are 1-99 and the "Buy" items are 100-[end]. There will never be 99 "make" items in an assembly, so I think 100 is enough.
Then I think I'm supposed to save the changed Structured BOM item numbers back to the BOM, but I'm not sure.
The goal is to have a numerical separation in the parts list numbers, so you see a balloon value less than 100, you know it's a "make" item, without having to ask anyone to manually take care of numbering.
Is this a challenging script to write?
Solved! Go to Solution.
Solved by Michael.Navara. Go to Solution.
You can start from code mentioned in this post if you want to sort BOM rows. Or you can use two counters for two groups of components.
Sub Main()
Dim asm As AssemblyDocument = ThisDoc.Document
Dim bomView As BOMView = asm.ComponentDefinition.BOM.BOMViews(2) 'Structured BOMView must be enabled
SortBomView(bomView)
End Sub
Private Sub SortBomView(bomView As BOMView)
Dim makeCounter = 1
Dim buyCounter = 100
For Each bomRow As BOMRow In bomView.BOMRows
Dim rowDocument As Document = bomRow.ComponentDefinitions(1).Document
Dim partNumber As String = rowDocument.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value.ToString()
If (IsMake(partNumber)) Then
bomRow.ItemNumber = makeCounter
makeCounter += 1
Else
bomRow.ItemNumber = buyCounter
buyCounter += 1
End If
Next
End Sub
Private Function IsMake(partNumber As String) As Boolean
'Implement your detection of Make part here
Return partNumber.Substring(3).StartsWith("-D-M")
End Function
This works, but I don't understand it. What part of it sorts by part number? Please help me understand the sorcery!
This is not a sorcery 😀
Function IsMake splits BOMRows to two groups. Each of them has its own counter (makeCounter and buyCounter)
When you use last value from counter (lines 17 and 20), its value are increased by 1 (lines 18 and 21).
Order of BOMRows in each group stay unchanged.
Is it possible to make this separation like it does now, and then sort both lists, so that the make and buy items are sorted by their part numbers after they're separated? So it'll be the "Make" numbers in order of part number starting with 1, 2, 3; and then the "Buy" numbers in order of part number, starting with "100, 101, 102, etc."
You can see my mentioned post, where it is shown.
Before renumbering you can sort BOMRows by PartNumber
After renumbering you can sort BOMRows by Item number
'Finally sort the rows by ItemNumber
'bomView.Sort("Položka") 'Czech
bomView.Sort("Item") 'English
Can't find what you're looking for? Ask the community or share your knowledge.