set/get cell to equation in Swtable

Equation in BOM | SOLIDWORKS Forums  https://forum.solidworks.com/message/641655#comment-641655

This code don't use

swTable.set_Text(1, 4, "=\\"TOTAL: \\" SUM(G2:G11) \\"kg\\"");

Search key word is " set_Text " in SOLIDWORKS API Help??

help ,in Solidworks 2012 API , result is not found?

in Swolidworks 2012 API,follow code ,don't set/get cell Equation.

Private Sub ll()

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

        Set SwApp = Application.SldWorks

        Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

        Set SwSelMgr = SwModel.SelectionManager

    Dim SwTabAnn As TableAnnotation, Str

    Dim SwBomTab As BomTableAnnotation

        Set SwBomTab = SwSelMgr.GetSelectedObject5(1)

        Set SwTabAnn = SwBomTab

            With SwTabAnn

                For jj = 0 To .ColumnCount - 1

                       Debug.Print .GetColumnTitle(jj),

                       Debug.Print .Text(.RowCount - 1, jj)

                Next jj

            End With

End Sub

''''

Private Sub ll()

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

         Set SwApp = Application.SldWorks

         Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

         Set SwSelMgr = SwModel.SelectionManager

    Dim SwView As View

         Set SwView = SwSelMgr.GetSelectedObject5(1)

         Debug.Print SwView.Name

    Dim swBOMTab As BomTableAnnotation, BomPath

         BomPath = "E:\\SldDrt\\PlateMaterial.sldbomtbt"

         Set swBOMTab = SwView.InsertBomTable2(False, 0.2, 0.2, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_Indented, "BEM1800-9/25", BomPath)

       

End Sub

'''

 

Private Sub ll()
    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2
         Set SwApp = Application.SldWorks
         Set SwModel = SwApp.ActiveDoc
    Dim SwSelMgr As SelectionMgr
         Set SwSelMgr = SwModel.SelectionManager
    Dim SwView As View
         Set SwView = SwSelMgr.GetSelectedObject5(1)
         Debug.Print SwView.Name
    Dim swBOMTab As BomTableAnnotation, BomPath
         BomPath = "E:\\SldDrt\\PlateMaterial.sldbomtbt"
         Set swBOMTab = SwView.InsertBomTable2(False, 0.415, 0.03, _
            swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomRight, _
            swBomType_e.swBomType_TopLevelOnly, _
            SwView.ReferencedConfiguration, _
            BomPath)
Debug.Print SwView.ReferencedConfiguration
            '"BEM1800-9/25", _
            BomPath)
       
End Sub
SolidworksApi macros
Answers 5
AT
AT 2016-06-26 Best Answer

The VBA syntax:

<span style="color: #333333; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; font-size: 14px;">swTable.Text(1, 4) = "=""TOTAL: "" SUM(G2:G11) ""kg"""</span>

C# syntax:

<span style="color: #333333; font-family: Verdana, 'Verdana Ref', Geneva, Tahoma, sans-serif; font-size: 14px;">swTable.set_Text(1, 4, "=\"TOTAL: \" SUM(G2:G11) \"kg\"")</span>

Thanks,

Artem

YN
YN 2016-06-27

Thank you fo your reply.

Red arrow indicate,the following code.

.Text(0, 2) = Equ→ Result is   "='QTY.'"

'''

Private Sub ll1()

    Dim Equ: Equ = "='QTY.'"

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

         Set SwApp = Application.SldWorks

         Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

         Set SwSelMgr = SwModel.SelectionManager

    Dim SwTabAnn As TableAnnotation

         Set SwTabAnn = SwSelMgr.GetSelectedObject5(1)

         With SwTabAnn

              .Text(0, 2) = Equ

              .Text(1, 2) = Equ

              .Text(2, 2) = Equ

         End With

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''

Help me, I need to further optimize the code.

How to  add formula of the column

My Question ,

BOM no code of column  formula  with  API???

comment imagecomment imagecomment imagecomment image
YN
YN 2016-06-27

My Question ,

BOM no formula code  in  columns  with  API???

2016 SOLIDWORKS API Help - swTableColumnTypes_e Enumeration  http://help.solidworks.com/2016/english/api/swconst/solidworks.interop.swconst~solidworks.interop.swconst.swtablecolumntypes_e.html

swBomTableColumnType_ItemNumber202
swBomTableColumnType_PartNumber201
swBomTableColumnType_Quantity203

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

Private Sub ll2()

    Dim Equ, Arr

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

         Set SwApp = Application.SldWorks

         Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

         Set SwSelMgr = SwModel.SelectionManager

    Dim SwBomTab As BomTableAnnotation, SwTabAnn As TableAnnotation

         Set SwBomTab = SwSelMgr.GetSelectedObject5(1)

         With SwBomTab

             Arr = .GetAllCustomProperties

             For jj = 0 To 3

                Debug.Print jj, .GetColumnCustomProperty(0)

             Next jj

         End With

         ''

         Set SwTabAnn = SwBomTab

         With SwTabAnn

              For jj = 0 To .ColumnCount - 1

                  Debug.Print jj, .GetColumnTitle(jj), .GetColumnType(jj)

              Next jj

              SwTabAnn.SetColumnType 2, 0 'swBomTableColumnType_PartNumber 'swBomTableColumnType_Quantity

         End With

End Sub

YN
YN 2016-07-06

''

Private Sub TraverChangeBOMconfiguration()

    Dim Str, tmp

       Str = "Bill of Materials1"

       Str = "材料明细表"

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2, SwDraw As DrawingDoc

       Set SwApp = Application.SldWorks

       Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

       Set SwSelMgr = SwModel.SelectionManager

    Dim sConfNames(0) As String, bVisible(0) As Boolean

     

    Dim ConfArr, swConf As Configuration

       ConfArr = SwModel.GetConfigurationNames

    Dim SwBomFeat As BomFeature, SwTabAnn As TableAnnotation

       ''

       For ii = 0 To UBound(ConfArr)

           SwModel.ShowConfiguration2 ConfArr(ii)

           Set swConf = SwModel.GetActiveConfiguration

           tmp = SwModel.Extension.SelectByID2(Str, "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0)

           Set SwBomFeat = SwSelMgr.GetSelectedObject5(1)

           SwBomFeat.Configuration = swConf.Name

           ''

           sConfNames(0) = ConfArr(ii) 'ConfName

           bVisible(0) = True

           ConfNames = sConfNames

           SwBomFeat.SetConfigurations False, bVisible, ConfNames

           Debug.Print ii, SwBomFeat.Configuration, swConf.Name, SwBomFeat.Configuration = swConf.Name

           Set SwTabAnn = SwBomFeat.GetTableAnnotations(0)

           With SwTabAnn

               .SetColumnWidth 3, 0.4, 0

               '.Text(0, .ColumnCount - 1) = "num"

               .Text(0, 3) = "数量"

           End With

           DigitsSwAnn SwTabAnn

           ''

           'SwModel.EditRebuild3

       Next ii

    'Stop

End Sub

Function Digits(Num)

    Select Case Num

        Case 0 To 9.9999

           Digits = Format(Num, "0.00")

        Case 10 To 99.999

           Digits = Format(Num, "0.0")

        Case Is > 100

           Digits = Format(Num, "0")

    End Select

End Function

Function DigitsSwAnn(SwTabAnn As TableAnnotation)

        With SwTabAnn

             For ii = 1 To .RowCount - 1

                  .Text(ii, 5) = ""

                  .Text(ii, 8) = ""

             Next ii

             ''

             For ii = 1 To .RowCount - 1

                  If .Text(ii, 3) = 1 Then

                              .Text(ii, 6) = Digits(.Text(ii, 5))

                              .Text(ii, 5) = " "

                              .Text(ii, 9) = .Text(ii, 8)

                              .Text(ii, 8) = " "

                

                  Else

                              .Text(ii, 6) = .Text(ii, 5) * .Text(ii, 3)

                              .Text(ii, 9) = .Text(ii, 8) * .Text(ii, 3)

                  End If

             Next ii

        End With

End Function

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

Private Sub ll()

  Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

     Set SwApp = Application.SldWorks

     Set SwModel = SwApp.ActiveDoc

  Dim ConfArr, Str

     ConfArr = SwModel.GetConfigurationNames

   

     'For ii = 0 To UBound(ConfArr)

     For ii = UBound(ConfArr) - 5 To UBound(ConfArr)

       If Not UCase(ConfArr(ii)) Like UCase("*Default*") And Not ConfArr(ii) Like "*配置*" Then

          Str = Trim(ConfArr(ii)) & Chr(10) & Str

       End If

     Next ii

     Debug.Print Str

     Dim sFileName As String, nErrors As Long, nWarnings As Long

     ''

     Dim fileConfig As String, fileDispName As String, fileOptions As Long

     ''

    sFileName = Left(SwModel.GetPathName, Len(SwModel.GetPathName) - 6)

    If SwModel.GetType = swDocPART Then

       sFileName = sFileName & "Eprt"

    ElseIf SwModel.GetType = swDocASSEMBLY Then

       sFileName = sFileName & "Easm"

    ElseIf SwModel.GetType = swDocDRAWING Then

       sFileName = sFileName & "Edrw"

    Else

       Exit Sub

    End If

    ''

    SwApp.SetUserPreferenceIntegerValue swEdrawingsSaveAsSelectionOption, swEdrawingSaveSelected

    SwApp.SetUserPreferenceStringListValue swUserPreferenceStringListValue_e.swEmodelSelectionList, Str

    SwModel.Extension.SaveAs sFileName, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings

     

End Sub

YN
YN 2017-08-08

Sub del()

    Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

        Set SwApp = Application.SldWorks

        Set SwModel = SwApp.ActiveDoc

    Dim SwSelMgr As SelectionMgr

    Dim SwTabAnn As TableAnnotation

    Dim TextFormat As TextFormat

    Dim wArr: wArr = Array(8, 21, 41, 8, 18, 10, 10, 35, 10, 10, 9)

        Set SwSelMgr = SwModel.SelectionManager

        Set SwTabAnn = SwSelMgr.GetSelectedObject5(1)

        With SwTabAnn

             .Text(.RowCount - 1, 3) = "数量"

            Set TextFormat = .GetTextFormat

            With TextFormat

                .CharHeight = 3 / 1000

                .TypeFaceName = "黑体"

                .WidthFactor = 0.7

            End With

            .SetTextFormat False, TextFormat

            ''

            For jj = 0 To UBound(wArr)

                .SetColumnWidth jj, wArr(jj) / 1000, 0

                Debug.Print jj, .RowCount, .Text(.RowCount - 1, jj)

                Select Case jj

                   Case 5, 6, 8, 9

                     Set TextFormat = .GetCellTextFormat(.RowCount - 1, jj)

                     With TextFormat

                       .CharHeight = 2 / 1000

                       .WidthFactor = 0.6

                     End With

                     .SetCellTextFormat .RowCount - 1, jj, False, TextFormat

                End Select

            Next jj

            ''

            For ii = 0 To .RowCount

                .SetRowHeight ii, 5 / 1000, 0

            Next ii

            Debug.Print .Text(.RowCount - 1, 3)

            Stop

      

        End With

      

      

      

End Sub