Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Improve my macro for sorting a SW Bill of material so Sub-assemblies come first.

joshF

Industrial
Nov 17, 2021
3
Hello,

I have this macro to sort the bill of material by part number. And it works perfectly. However, team want to start to sort the bill of material so that sub-assemblies are listed first. I have not successfully been able to add that feature in the code. can you help modify the code to work? (see attachment PDF)

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swBomTable As SldWorks.BomTableAnnotation
Dim swBomFeat As SldWorks.BomFeature
Dim swSortData As SldWorks.BomTableSortData
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.Feature
Dim vTables As Variant
Dim boolstatus As Boolean
Dim sortArray(2) As Integer
Dim bWantGrp As Boolean

Sub main()

' Initialize SolidWorks application and active document
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swSelMgr = swModel.SelectionManager
Set swFeat = swModel.FirstFeature

' Toggle document property to set Top-level BOMS to 1 configuration
boolstatus = swModel.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swOneConfigOnlyTopLevelBom, 0, True)

' Loop through features to find BOM feature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName = "BomFeat" Then
swFeat.Select False
Set swBomFeat = swFeat.GetSpecificFeature2
vTables = swBomFeat.GetTableAnnotations
Set swBomTable = vTables(0) ' Get the first BOM table annotation
Exit Do ' Exit once the BOM is found
End If
Set swFeat = swFeat.GetNextFeature
Loop

' Get BOM sorting data
Set swSortData = swBomTable.GetBomTableSortData

' Specify the literal sort method
swSortData.SortMethod = swBomTableSortMethod_Literal
swSortData.ColumnIndex(0) = 1 ' Sort by the second column (index 1)
swSortData.Ascending(0) = True ' Sort in ascending order

' Group rows by assemblies and parts
bWantGrp = True
If bWantGrp Then
sortArray(0) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Assemblies
sortArray(1) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Parts
sortArray(2) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_None ' No further grouping
swSortData.ItemGroups = sortArray
End If

' Allow re-numbering of items after sorting
swSortData.DoNotChangeItemNumber = False

' Apply the sorting to the BOM
boolstatus = swBomTable.Sort(swSortData)

' Clear selections
swModel.ClearSelection2 True

End Sub

 
 https://files.engineering.com/getfile.aspx?folder=7c8d3ad7-9fe6-4da1-94de-fc0b56fbbc90&file=ENG-TIP_QUESTION_SORTING.pdf
Replies continue below

Recommended for you

Part and Inventory Search

Sponsor