BOM Sort place fastners at bottom and sort by Bolt, Washer, Nut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Everyone,
I am trying to write a rule that sorts the BOM by Part Number and then all the fasteners.
I want all the fasteners order from smallest to biggest and in order of Bolt, Washer, Nut. (as show in the image below)
I found a lot of helpful posts and manage to write the following code. First it sorts the BOM by Part Number and then by DIMENSIONS. Then it splits the Bom into two lists and combines the lists in the right order.
(I know the method for finding fasteners is not very pretty but it works for now. Ideally it would search for bolt washer or nut in the description)
How do I sort the list with fasteners correctly?
Also I am very new to programming so any general tips for improvement would be appreciated.
Sub Main
Dim aDoc As Document = ThisApplication.ActiveDocument
Dim drawDoc As DrawingDocument
Dim oDoc As AssemblyDocument
'If part then exit
If aDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Exit Sub
'If Assembly oRefdoc = oAssemdoc
ElseIf aDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
oDoc = aDoc
'If drawing then find ref doc
ElseIf aDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
drawDoc = aDoc
Dim oRefDoc As Document
oRefDoc = drawDoc.ActiveSheet.PartsLists.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
'If ref doc = part the exit
If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Exit Sub
'If ref doc = Assembly then = rdoc
ElseIf oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
oDoc = oRefDoc
'Set sort to Item and turn on autosort
aDoc.ActiveSheet.PartsLists.Item(1).Sort("ITEM",1,AutoSortOnUpdate,True)
End If
End If
Dim oDocBom As BOM
oDocBom = oDoc.ComponentDefinition.BOM
Dim oBom As BOMView = oDocBom.BOMViews(2) 'Structured BOMView must be enabled
oBom.Sort("Part Number",True,"DIMENSIONS",True)
' Create a list to store all parts
Dim partnumList As New List(Of BOMRow)
' Create a list to store parts containing bolts, nuts, and washers
Dim hardwareParts As New List(Of BOMRow)
'Add parts with empty part number to hardwareparts list, add other parts to partnumlist
For Each oRow As BOMRow In oBom.BOMRows
Dim rowDocument As Document = oRow.ComponentDefinitions(1).Document
Dim partNumber As String = rowDocument.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value.ToString()
If partNumber = "" Then
hardwareParts.Add(oRow)
Else
partnumList.Add(oRow)
End If
Next
Dim combinedList As New List(Of BOMRow)(partnumList)
combinedList.AddRange(hardwareParts)
' Update the BOM with the sorted list
Dim i As Integer = 1
For Each oRowcombined As BOMRow In combinedList
' Reorder the BOM rows in the desired sequence
'oDocBom.Item(i).RowIndex = i
oRowcombined.ItemNumber=i
i += 1
Next
oBom.Sort("Item")
oBom.Renumber(1, 1)
aDoc.Update
End Sub
for anyone else working on a similar problem check these out:
https://inventortrenches.blogspot.com/2022/05/ilogic-custom-sort-partslist-with.html