Hide Table of Contents

Sort Table Example (VB.NET)

This example shows how to sort bill of materials, hole, general, and weldment cut list tables.

'---------------------------------------------------------------------------
' Preconditions:
' 1. Open a drawing with one of the following tables:
'    * General
'    * Hole chart
'    * Weldment cut list
'    * Bill of materials parts-only table that
'      contains four columns: Item No, Part Number, Description, and Qty.
' 2. Click the move-table icon in the upper-left corner
'    of the table in the view.
'    The PropertyManager opens on the table.
' 3. If the table is a BOM table, ensure that
'    PropertyManager > Item Numbers > Follow assembly order is not selected.
'
' Postconditions:  
' 1. Inspect the Immediate Window.
' 2. The tables are sorted as follows:
'    * If it is a bill of materials table:
'      * Primary sort is on column three (Qty).
'      * Secondary sort is on column one (Part Number).
'      * There is no tertiary sort.
'      * All column sorts are ascending.
'      * Rows are sorted into categories in the following order:
'          1. Parts
'          2. User-defined
'    * If it is a general table, the sort is descending on the first column.
'    * If it is a hole table, the sort is ascending on the Tag column.
'    * If it is a weldment cut list table, the sort is descending
     on the third column.
' --------------------------------------------------------------------------

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Diagnostics

Partial Public Class SolidWorksMacro
    
Private swModDoc As IModelDoc2 = Nothing
    Private swTable As ITableAnnotation = Nothing

    Public Sub Main()
        GetSelTable()
        SortSelTable()
    
End Sub

    Public Sub GetSelTable()
        swModDoc =
DirectCast(swApp.ActiveDoc, IModelDoc2)
        
Dim swSM As ISelectionMgr = Nothing
        swSM = DirectCast(swModDoc.SelectionManager, ISelectionMgr)
        swTable =
DirectCast(swSM.GetSelectedObject6(1, -1), ITableAnnotation)
        swModDoc.ClearSelection2(
True)
    
End Sub

    Public Sub SortSelTable()
        
If swTable IsNot Nothing Then
            Dim tableType As swTableAnnotationType_e = DirectCast(swTable.Type, swTableAnnotationType_e)
            
Dim status As Boolean = False
            Select Case tableType
                
Case swTableAnnotationType_e.swTableAnnotation_General
                    status = SortGeneralTable()
                    
Exit Select
                Case swTableAnnotationType_e.swTableAnnotation_HoleChart
                    status = SortHoleChartTable()
                    
Exit Select
                Case swTableAnnotationType_e.swTableAnnotation_WeldmentCutList
                    status = SortWeldmentCutlistTable()
                    
Exit Select
                Case swTableAnnotationType_e.swTableAnnotation_BillOfMaterials
                    status = SortBOMTable()
                    
Exit Select
                Case Else
                    Debug.Print("Unspecified table type selected.")
                    
Exit Select
            End Select
        End If
    End Sub

    Public Function SortGeneralTable() As Boolean
        Debug.Print("Table type selected : swTableAnnotation_General")
        
Dim swSpecTable As IGeneralTableAnnotation = DirectCast(swTable, IGeneralTableAnnotation)
        
Dim status As Boolean = swSpecTable.Sort(0, False) 'sort descending
        

        Return status
    
End Function

    Public Function SortHoleChartTable() As Boolean
        Debug.Print("Table type selected : swTableAnnotation_HoleChart")
        
Dim swSpecTable As IHoleTableAnnotation = DirectCast(swTable, IHoleTableAnnotation)
        
Dim status As Boolean = swSpecTable.Sort(0, True) 'sort ascending
        

        Return status
    
End Function

    Public Function SortWeldmentCutlistTable() As Boolean
        Debug.Print("Table type selected : swTableAnnotation_WeldmentCutList")
        
Dim swSpecTable As IWeldmentCutListAnnotation = DirectCast(swTable, IWeldmentCutListAnnotation)
        
Dim status As Boolean = swSpecTable.Sort(2, False) 'sort descending
        

        Return status
    
End Function

    Public Function SortBOMTable() As Boolean
        Debug.Print("Table type selected : swTableAnnotation_BillOfMaterials")
        
Dim swSpecTable As IBomTableAnnotation = DirectCast(swTable, IBomTableAnnotation)
        
Dim swSortData As BomTableSortData = swSpecTable.GetBomTableSortData()

        
' Specify the sort order indexes for three columns in the table
        ' Specify the direction of sort for the three sort order indexes
        swSortData.ColumnIndex(0) = 3  ' primary sort
        swSortData.Ascending(0) =
True ' sort ascending
       
        swSortData.ColumnIndex(1) = 1  ' secondary sort
        swSortData.Ascending(1) =
True ' sort ascending
        
        swSortData.ColumnIndex(2) = -1 ' no tertiary sort
        

        Dim pos1 As Integer = swSortData.ColumnIndex(0)
        Debug.Print(
"Column for primary sort is " & pos1) ' should be 3
        

        Dim pos2 As Integer = swSortData.ColumnIndex(1)
        Debug.Print(
"Column for secondary sort is " & pos2) ' should be 1
        

        Dim pos3 As Integer = swSortData.ColumnIndex(2)
        Debug.Print(
"Column for tertiary sort is " & pos3) ' should be -1
        

        Dim listGrpArray As Integer() = Nothing
        Dim grpArray As Object = Nothing
        grpArray = listGrpArray
        
Dim bWantGrp As Boolean = True

        If bWantGrp Then
            ' Sort rows into part and user-defined categories
            listGrpArray = New Integer(2) {}
            listGrpArray(0) =
CInt(swBomTableSortItemGroup_e.swBomTableSortItemGroup_None)
            listGrpArray(1) =
CInt(swBomTableSortItemGroup_e.swBomTableSortItemGroup_Parts)
            listGrpArray(2) =
CInt(swBomTableSortItemGroup_e.swBomTableSortItemGroup_Other)
            grpArray = listGrpArray
        
End If
        swSortData.ItemGroups = grpArray
        
' After sorting, do not re-number the items
        swSortData.DoNotChangeItemNumber = True

        Dim status As Boolean = swSpecTable.Sort(swSortData)
        Debug.Print(
"BOM table sorted: " & status)
        
Return status
    
End Function

    Public swApp As SldWorks
End Class



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Sort Table Example (VB.NET)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2012 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.