2 Replies Latest reply on Mar 15, 2016 7:43 PM by Sanya Shmidt

    Format BOM on the drawing so text fits the Column width

    Sanya Shmidt

      Hello Guys!

       

      Is it possible to format BOM (via API/VBA calls) that was manually inserted on the drawing so the text is perfectly fits in the column (no double lines)

       

      similar to this. 

       

      'Like in Excel Worksheets("Sheet1").Range("A1:E1").Columns.AutoFit

       

      Any help is appreciated.

       

      Thanks,

        • Re: Format BOM on the drawing so text fits the Column width
          Artem Taturevych

          There seems to be no auto fit option. I could propose you the following.

           

          Find the height of the row with single line via TableAnnotation::GetRowHeight. And change the column width with the step using TableAnnotation::SetColumnWidth until all rows have the single line height.

           

          It is possible to do it better by measuring the text width (considering the font, type etc) but this will be easier to do in .NET (https://msdn.microsoft.com/en-us/library/y4xdbe66(v=vs.110).aspx) rather than in the macro.

            • Re: Format BOM on the drawing so text fits the Column width
              Sanya Shmidt

              Thank you for the suggestion, I can cheat a little bit. Since I know the ~ max field length I can use the following calls:

               

              swTableAnn.SetColumnWidth 0, 0.01, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 1, 0.015, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 2, 0.03, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 3, 0.02, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 4, 0.15, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 5, 0.1, swTableRowColChange_TableSizeCanChange

              swTableAnn.SetColumnWidth 6, 0.02, swTableRowColChange_TableSizeCanChange

               

              Otherwise I`d have to iterate through the rows, there is smth goes wrong with it: seems like it doesn`t update the height when you change width...

              Here is my Macro

               

              ' Alex Shmidt (C)2016

              ' This script sorts the BOM table and adjusts the columns to fit

               

              Public swApp As SldWorks.SldWorks

              Public swModel As SldWorks.ModelDoc2

              Public swDraw As SldWorks.DrawingDoc

              Public swBomTable As SldWorks.BomTableAnnotation

              Public swBomFeat As SldWorks.BomFeature

              Public swSortData As SldWorks.BomTableSortData

              Public swSelMgr As SldWorks.SelectionMgr

              Public swFeat As SldWorks.Feature

              Public sortArray(2) As String

              Public vTables As Variant

              Public boolStatus As Boolean

               

              Public swTableAnn As SldWorks.TableAnnotation

               

              Public Function 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 swFeat.GetTypeName = "BomFeat" 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) = 2

              swSortData.Ascending(0) = True

              swSortData.ColumnIndex(1) = 5

              swSortData.Ascending(1) = True

              swSortData.ColumnIndex(2) = 4

              swSortData.Ascending(1) = True

               

               

              sortArray(0) = swBomTableSortItemGroup_Assemblies

              sortArray(1) = swBomTableSortItemGroup_Parts

              sortArray(2) = swBomTableSortItemGroup_None

               

               

              swSortData.ItemGroups = sortArray

               

              swSortData.DoNotChangeItemNumber = False

               

              boolStatus = swBomTable.Sort(swSortData)

               

               

              ' After sorting, do not re-number the items

              swSortData.DoNotChangeItemNumber = True

               

              ' Save sorting scheme to the BOM table in the drawing

              sortSaved = swSortData.SaveCurrentSortParameters

               

              swModel.ClearSelection2 (True)

               

              'get row information

              nbrTableAnn = swBomFeat.GetTableAnnotationCount

              vTableAnn = swBomFeat.GetTableAnnotations

              For i = 0 To (nbrTableAnn - 1)

                  Set swTableAnn = vTableAnn(i)

                  nbrRows = swTableAnn.RowCount - 1

              Next i

               

              ' here is the COLUMN/ROW ADJUSTMENTS

              Dim k As Double

               

                 For j = 1 To 6

                 For k = 0.005 To 0.1 Step 0.0005

                      swTableAnn.SetColumnWidth j, k, swTableRowColChange_TableSizeCanChange

                      Debug.Print (swTableAnn.GetRowHeight(j))

                      If swTableAnn.GetRowHeight(j) < 0.003125 Then GoTo n1 

                 Next k

                      n1:     swModel.ForceRebuild3 False

                 Next j

                  

                  

              For i = 0 To (nbrRows)

                   swTableAnn.SetRowHeight i, 0.003125, 0

                  ' swTableAnn.SetRowVerticalGap 1, 0.0003

              Next i

                

              End Function