We use cookies to operate this website, improve its usability and track visits. Please note that by using this site you are consenting to the use of cookies.
Learn More |
Sort BOM VBA Macro issues
SOLIDWORKS Forums
5 months ago

I have a macro, that i thought was correct... I want to sort my top level BOMs by "Part number", "ascending" in order of (1) assemblies then (2)parts...

The normal "Sort" GUI works, must be something i cant see...

 

 

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 sortArray(2) As String
Dim vTables      As Variant
Dim boolstatus   As Boolean

 

Sub Sort_BOM_Data()

 

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swDraw = swModel

Set swSelMgr = swModel.SelectionManager

Set swFeat = swModel.FirstFeature

 

Do While Not swFeat Is Nothing

If "BomFeat" = swFeat.GetTypeName Then
    swFeat.Select (False)
    Set swBomFeat = swFeat.GetSpecificFeature2
    vTables = swBomFeat.GetTableAnnotations
    Set swBomTable = vTables(0)
End If
Set swFeat = swFeat.GetNextFeature

Loop

 

Set swSortData = swBomTable.GetBomTableSortData

 

swSortData.ColumnIndex(0) = 3

swSortData.Ascending(0) = True

 

sortArray(0) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Assemblies

sortArray(1) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Parts

sortArray(2) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_None

 

swSortData.ItemGroups = sortArray

swSortData.DoNotChangeItemNumber = False

 

boolstatus = swBomTable.Sort(swSortData)

 

swModel.ClearSelection2 (True)

 

End Sub


Categories: API∕Macros

Comments
Last comment By: Cad Admin   Fri, 28 Apr 2017 11:25:12 GMT
Re: Sort BOM VBA Macro issues

I did a few tests and it seems that the ItemGroups property expects an object when setting. The object should be an array of integers. There is no VBA example available and I don't know how to set an object to an array of integers though.

By: Peter Brinkhuis  Sat, 22 Apr 2017 14:08:12 GMT
Re: Sort BOM VBA Macro issues

I was looking at 2016 SOLIDWORKS API Help - Sort Table Example (VBA) & modified my code but still no luck getting the result i need...

 

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 sortArray(2) As String

Dim vTables As Variant

Dim boolstatus As Boolean

 

Sub Sort_BOM_Data()

 

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, ie remove config name from QTY column

boolstatus = swModel.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swOneConfigOnlyTopLevelBom, 0, True)

 

Do While Not swFeat Is Nothing

    If "BomFeat" = swFeat.GetTypeName Then

        swFeat.Select (False)

        Set swBomFeat = swFeat.GetSpecificFeature2

        vTables = swBomFeat.GetTableAnnotations

        Set swBomTable = vTables(0)

    End If

    Set swFeat = swFeat.GetNextFeature

Loop

 

Set swSortData = swBomTable.GetBomTableSortData

 

' Specify the literal sort method

swSortData.SortMethod = swBomTableSortMethod_Literal

 

swSortData.ColumnIndex(0) = 3

swSortData.Ascending(0) = True

swSortData.ColumnIndex(1) = 3

swSortData.Ascending(1) = True

 

Dim sortArray(2) As Integer

Dim bWantGrp As Boolean

bWantGrp = True

 

If bWantGrp Then

' Sort rows into part and user-defined categories

sortArray(0) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Assemblies

sortArray(1) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_Parts

sortArray(2) = swBomTableSortItemGroup_e.swBomTableSortItemGroup_None

End If

 

swSortData.ItemGroups = sortArray

 

' After sorting, do not re-number the items

swSortData.DoNotChangeItemNumber = False

 

boolstatus = swBomTable.Sort(swSortData)

 

swModel.ClearSelection2 (True)

 

End Sub

By: Cad Admin  Wed, 26 Apr 2017 10:46:17 GMT
Re: Sort BOM VBA Macro issues

It may help others to answer your question if you provide more info about what does happen when you run your code... Sorts wrong? Error of some sort?  If so, which line? Nothing at all happens?

 

Just looking at the API help, it states in Remarks for the ColumnIndex property, "Call this property three times to set sort order indexes of all three columns".  You have to use -1 to specify secondary or tertiary sort are not used.  This is shown in the example you referenced. Based on your image in the first post, I'm guessing you actually want:

 

swSortData.ColumnIndex(0) = 3

swSortData.Ascending(0) = True

swSortData.ColumnIndex(1) = -1

swSortData.Ascending(1) = True

swSortData.ColumnIndex(2) = -1

swSortData.Ascending(2) = True

By: Josh Brady  Wed, 26 Apr 2017 12:34:25 GMT
Re: Sort BOM VBA Macro issues

Sorry, about explaining what it is not doing....or the issue.  The biggest thing it is not grouping "assembly" first then "parts".

By: Cad Admin  Wed, 26 Apr 2017 13:09:02 GMT
Re: Sort BOM VBA Macro issues

Yeah the same occurred here. There's no error and no change in the drawing. When I added a break point and checked out the value of swSortData.ItemGroups, it remained "nothing" after just setting it.

By: Peter Brinkhuis  Wed, 26 Apr 2017 13:17:03 GMT
Re: Sort BOM VBA Macro issues

Also for some reason columnindex(0) should be 2 not 3...solidworks must not count the driven Items column as 1 or it may us it a 0....

Using the Example above.  The macro "saves the sort" in the drawing.  After running a macro with columnindex set to 3, and looking at "edit saved sort"  the sort was saved to "REV" column. also the checkboxes for assembly then parts were unchecked.

 

By: Cad Admin  Wed, 26 Apr 2017 13:28:00 GMT
Re: Sort BOM VBA Macro issues

Ok,  Going backwards & using the "Example API" at 2016 SOLIDWORKS API Help - Sort Table Example (VBA) .

 

Changing the following ....

swSortData.ColumnIndex(0) = 2  ' primary sort on column 1

swSortData.Ascending(0) = True ' sort ascending

swSortData.ColumnIndex(1) = -1 ' secondary sort on column 3

swSortData.Ascending(1) = True ' sort ascending

swSortData.ColumnIndex(2) = -1 ' no tertiary sort

 

swSortData.DoNotChangeItemNumber = False

 

& commenting out ' swTable.InsertRow 2, 2...don't need it..

 

After doing that, the macro sorts fine sets everything correctly (except grouping)...but it seams to ignore the Array

 

 

swSortData.ItemGroups = listGrpArray is returning "empty"

By: Cad Admin  Wed, 26 Apr 2017 14:16:56 GMT
Re: Sort BOM VBA Macro issues

Most likely it just starts counting at zero, that pretty usual in programming There are some inconsistencies in the SW API though with that regard.

By: Peter Brinkhuis  Wed, 26 Apr 2017 14:17:59 GMT
Re: Sort BOM VBA Macro issues

Inconsistencies? surly you jest...LOL. Ya I've seen a few.  But a little boggled with it not setting the groupings and returning a "empty array"

By: Cad Admin  Wed, 26 Apr 2017 14:24:43 GMT
Re: Sort BOM VBA Macro issues

This is perplexing, i have tried almost everything, every example...all show the same.  I believe it is in the array.

By: Cad Admin  Fri, 28 Apr 2017 11:25:12 GMT
You are not authorized to view this page No results found! Suggestions: Check spelling, try a different search, or browse topics below.