2 Replies Latest reply on Jun 20, 2014 3:06 AM by Yong Ning

    How to change fontsize of general table position with API?

    Yong Ning

      Hope ,change fontsize of general table

       

      0.jpg

      ****************************

      change this tips code ,the error result is

       

      1.jpg

       

      SolidWorks Forums: How to move the general table position with API?

      https://forum.solidworks.com/message/420885#420885

       

       

      Sub ll1()

         Dim swApp As SldWorks.SldWorks

         Dim swModel As SldWorks.ModelDoc2

         Dim swSelMgr As SldWorks.SelectionMgr

         Dim swTable As SldWorks.TableAnnotation

         Dim swAnn As SldWorks.Annotation, swTextFormat As TextFormat  

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          Set swTable = swSelMgr.GetSelectedObject6(1, -1)    ''

          ''

          Set swAnn = swTable.GetAnnotation

          'swAnn.SetPosition 0.15, 0.2, 0

          With swAnn

             .Layer = "aa"

          End With

          Debug.Print swAnn.GetTextFormatCount

        

          For ii = 0 To swAnn.GetTextFormatCount

            Set swTextFormat = swAnn.GetTextFormat(ii)

            With swTextFormat

               Debug.Print .CharHeight  '→ result is Error

            End With

          Next ii

      End Sub

      ************************

        • Re: How to change fontsize of general table position with API?
          Keith Rice

          Yong,

           

          There probably is no ITextFormat object being retrieved by IAnnotation::GetTextFormat. You can change the character height in each cell like this:

           

          =======

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swTable As SldWorks.TableAnnotation

          Dim swTextFormat As SldWorks.TextFormat

          Dim dblCharHeight As Double

          Dim i As Integer, j As Integer

           

          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager

              Set swTable = swSelMgr.GetSelectedObject6(1, -1)

             

              For i = 0 To swTable.ColumnCount - 1

                  For j = 0 To swTable.RowCount - 1

                      Set swTextFormat = swTable.GetCellTextFormat(j, i)

                      Debug.Print "Old: " & swTextFormat.CharHeight

                      swTextFormat.CharHeight = 0.01

                      Debug.Print "New: " & swTextFormat.CharHeight

                      swTable.SetCellTextFormat j, i, False, swTextFormat

                  Next j

              Next i

          End Sub

           

          =======

           

          Keith

          SolidWorks API Training

            • Re: How to change fontsize of general table position with API?
              Yong Ning

              Thanks Keith Rice,

              Change your tips code is

              *************************

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swSelMgr As SldWorks.SelectionMgr

              Dim swTable As SldWorks.TableAnnotation

              Dim swTextFormat As SldWorks.TextFormat

              Dim dblCharHeight As Double

              Dim i As Integer, j As Integer

               

              Sub main()

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                  Set swTable = swSelMgr.GetSelectedObject6(1, -1)

               

                  For i = 0 To swTable.ColumnCount - 1

                      For j = 0 To swTable.RowCount - 1

                          Set swTextFormat = swTable.GetCellTextFormat(j, i)

                          Debug.Print "Old: " & swTextFormat.CharHeight

                          With swTextFormat

                             .CharHeight = 0.003

                             .WidthFactor = 0.6

                          End With

               

                          Debug.Print "New: " & swTextFormat.CharHeight

                          swTable.SetCellTextFormat j, i, False, swTextFormat

                      Next j

                      Debug.Print swTable.GetRowHeight(i)

                      swTable.SetRowHeight i, 0.01, 0

                      'Stop

                      Debug.Print swTable.GetRowHeight(i)

                  Next i

              End Sub

               

              *****************************

              SolidWorks Forums: Select and Edit a general table

              https://forum.solidworks.com/message/34785#34785

               

               

              ***********************************

              Function WidthFactorTextFormat(TextFormat As TextFormat, Rng As Range, wFactor, LineSpec) As TextFormat

                With TextFormat

                       .Italic = Rng.Font.Italic

                       .Bold = Rng.Font.Bold

                       .LineSpacing = LineSpec / 1000

                       .CharHeight = (Rng.Font.Size - 6) / 1000

                       .TypeFaceName = Rng.Font.Name

                       .WidthFactor = wFactor

                End With

                'Debug.Print Rng.Address, Rng.Font.Size

                Set WidthFactorTextFormat = TextFormat 

              End Function