4 Replies Latest reply on Dec 21, 2015 9:00 AM by Dennis Hvam

    Adding a row at the end of a BOM table - API

    Dennis Hvam

      Hello,

       

      I'm looking for a way to simply add a row at the end of a BOM table in a Drawing, and then get the SUM() of all the rows above. A macro would be the best solution for me since every boom is run through DriveWorks.

       

      The BOM tables can vary in rows, depending on the number of parts of cause.

       

      Example: The BOM always consists of 6 Columns, and its always the last cell in the 6'th column which needs the total sum of the above cells.

       

      I have worked with macro's before but it was very limited.

        • Re: Adding a row at the end of a BOM table - API
          Amen Allah Jlili

          Try this short snippet. It should work, I've tested it.
          Pre-condition: BOM is selected

           

           

           

          Sub main()
          Dim swapp As SldWorks.SldWorks
          Dim swmodel As ModelDoc2
          Dim swselectionmanager As SelectionMgr
          Dim swannotationtable As TableAnnotation
          Dim result As Boolean
          Set swapp = Application.SldWorks
          Set swmodel = swapp.ActiveDoc
          Set swselectionmanager = swmodel.SelectionManager
          Set swannotationtable = swselectionmanager.GetSelectedObject6(1, -1)
          result = swannotationtable.InsertRow(4, swannotationtable.RowCount)
          Dim rowletter As String
          rowletter = ChrW(64 + swannotationtable.ColumnCount)
          swannotationtable.Text(swannotationtable.RowCount - 1, swannotationtable.ColumnCount - 1) = _
          "=SUM(" & rowletter & "1:" & rowletter & swannotationtable.RowCount - 1 & ")"
          End Sub
          
            • Re: Adding a row at the end of a BOM table - API
              Dennis Hvam

              Hello Amen,

               

              This works like a charm!

               

              Is there any way to get it to work without preselecting the BOM, or have a code that selects the BOM, the reason is that all the Drawings is run through DriveWorks, and I don't actually interact with the drawings.

               

              The BOM's can have a specific name if that is required

                • Re: Adding a row at the end of a BOM table - API
                  Amen Allah Jlili

                  Alright big boy, here's what you asked for.

                   

                  Notes: Instead of using the selection manager, the snippet below uses the feature manager to get the bom feature and add the sum row. Make sure to change the BomFeatureName string variable to the BOM feature name in your feature manager. Please note that it's case-sensitive. The snippet will yield an error if a bom with the specified name is not found.

                   

                   

                  Sub main()
                  On Error GoTo handler:
                  Dim BomFeatureName As String: BomFeatureName = "Bill of Materials1"
                  Dim swapp As SldWorks.SldWorks
                  Dim swmodel As ModelDoc2
                  Dim swannotationtable As TableAnnotation
                  Dim result As Boolean
                  Dim featuremanager As featuremanager
                  Dim feature As feature
                  Dim vfeature As Variant
                  Set swapp = Application.SldWorks
                  Set swmodel = swapp.ActiveDoc
                  Set featuremanager = swmodel.featuremanager
                  vfeatures = featuremanager.GetFeatures(True)
                  For i = 0 To UBound(vfeatures)
                  Set feature = vfeatures(i)
                  If feature.Name = BomFeatureName Then
                  Dim bomfeature As bomfeature
                  Set bomfeature = feature.GetSpecificFeature2
                  Dim bomtableannotation As bomtableannotation
                  Set bomtableannotation = bomfeature.GetTableAnnotations(0)
                  Set swannotationtable = bomtableannotation
                  Exit For
                  End If
                  Next i
                  result = swannotationtable.InsertRow(4, swannotationtable.RowCount)
                  Dim rowletter As String
                  rowletter = ChrW(64 + swannotationtable.ColumnCount)
                  swannotationtable.Text(swannotationtable.RowCount - 1, swannotationtable.ColumnCount - 1) = _
                  "=SUM(" & rowletter & "1:" & rowletter & swannotationtable.RowCount - 1 & ")"
                  Exit Sub
                  handler:
                  swapp.SendMsgToUser ("Cannot find BOM with the name: " + BomFeatureName)
                  End Sub