10 Replies Latest reply on May 2, 2017 11:43 AM by Cad Admin

    Sort BOM VBA Macro issues

    Cad Admin

      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

        • Re: Sort BOM VBA Macro issues
          Peter Brinkhuis

          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.

            • Re: Sort BOM VBA Macro issues
              Cad Admin

              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

                • Re: Sort BOM VBA Macro issues
                  Josh Brady

                  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

              • Re: Sort BOM VBA Macro issues
                Cad Admin

                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"

                • Re: Sort BOM VBA Macro issues
                  Cad Admin

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