BOM Sort place fastners at bottom and sort by Bolt, Washer, Nut

BOM Sort place fastners at bottom and sort by Bolt, Washer, Nut

b_berghuis
Explorer Explorer
158 Views
1 Reply
Message 1 of 2

BOM Sort place fastners at bottom and sort by Bolt, Washer, Nut

b_berghuis
Explorer
Explorer

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)

 

Schermafbeelding 2024-11-14 152737.png

 

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://forums.autodesk.com/t5/inventor-programming-ilogic/ilogic-sort-and-renumber-bom-from-asm/td-... 

https://forums.autodesk.com/t5/inventor-programming-ilogic/order-bom-based-on-part-number-not-alphab... 

https://inventortrenches.blogspot.com/2022/05/ilogic-custom-sort-partslist-with.html 

https://forums.autodesk.com/t5/inventor-programming-ilogic/sort-bom-and-renumber-sort-parts-list/td-...

 

0 Likes
159 Views
1 Reply
Reply (1)
Message 2 of 2

daltonNYAW9
Advocate
Advocate

Sorting the "DIMENSIONS" value is fairly difficult since it is a "String" value not a "Double".
String sort: 100, 20, 3

Double Sort: 3, 20, 100

So you will first need to convert it to a Double

 

I made a rule that will hopefully do this

It sorts the list you create, then renumbers the "ItemNumber" value
Before:

daltonNYAW9_0-1732131588631.png

After:

daltonNYAW9_1-1732131604950.png

Sub Main
	Dim oDoc As AssemblyDocument = ThisDoc.Document

	Dim oDocBom As BOM
	oDocBom = oDoc.ComponentDefinition.BOM
	oDocBom.StructuredViewEnabled = True
	Dim oBom As BOMView = oDocBom.BOMViews(2) 'Structured BOMView must be enabled

	' 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

	hardwareParts.Sort(Function(x, y) GetDimensionsValue(x).CompareTo(GetDimensionsValue(y)))
	
	For i = 0 To hardwareParts.Count - 1
		hardwareParts(i).ItemNumber = i + 1
	Next

	oBom.Sort("Item")
End Sub

Function GetDimensionsValue(oRow As BOMRow) As Double
	Dim dimensions As String = oRow.ComponentDefinitions(1).Document.PropertySets(4)("DIMENSIONS").Value

	Return _
	CDbl(dimensions.Substring(1, String.Concat(dimensions, "x").IndexOf("x") -1))

End Function
0 Likes