3 Replies Latest reply on Nov 2, 2012 4:22 PM by Keith Rice

    How to delete a BOM Table from a Part Doc using API?

    Steve Fye

      I would like a macro that can do the following:


      1.  Inserts a BOM table into a PART document

      2.  Exports it as text

      3.  Deletes the table created in step 1


      I have done everything successfully except for step 3.  I am pretty sure that I'm close to the correct answer, but I haven't quite got it.  Here's my code (removed some items for clarity).


      How the heck does one *get* the table so it can be deleted?  I don't want the user to be required to select it.


      Private Sub MyMacro()


                Dim swModel As Object

                Dim swBOMTable As Object


                Set swApp = CreateObject("SldWorks.Application")

                Set swModel = swApp.ActiveDoc


           'Set swBOMTable = swModel.Extension.InsertBomTable(<deleted this for forum post - it works>)


           'retval = swBOMTable.SaveAsText(<deleted this for forum post - it works>)


      'Here is where I think it's going wrong.

      'The following code executes, but retval returns FALSE

      'How do I select the table automatically so that I can delete it?


                retval = swModel.Extension.SelectByID2("", "BOMFEATURE", 0, 0, 0, False, 0, Nothing, 0)

      'The above line always returns FALSE




      End Sub

        • Re: How to delete a BOM Table from a Part Doc using API?
          Keith Rice

          Hi Steve,


          Try this:


          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swFeat As SldWorks.Feature

          Dim swSubFeat As SldWorks.Feature

          Dim swBomFeat As SldWorks.BomFeature

          Dim vBOMTableAnns As Variant

          Dim swBomTableAnn As SldWorks.BomTableAnnotation

          Dim swTableAnn As SldWorks.TableAnnotation

          Dim swAnn As SldWorks.Annotation


          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swFeat = swModel.FirstFeature

              Do While Not swFeat Is Nothing

                  If swFeat.GetTypeName2 = "TableFolder" Then

                      Set swSubFeat = swFeat.GetFirstSubFeature

                      Set swBomFeat = swSubFeat.GetSpecificFeature2

                      vBOMTableAnns = swBomFeat.GetTableAnnotations

                      Set swBomTableAnn = vBOMTableAnns(0)    'assume only one BOM table

                      Set swTableAnn = swBomTableAnn

                      Set swAnn = swTableAnn.GetAnnotation

                      swAnn.Select3 False, Nothing


                      Exit Do

                  End If

                  Set swFeat = swFeat.GetNextFeature


          End Sub


          Also, some macro writing advice:


          1. Use "Set swApp = Application.SldWorks" instead of "Set swApp = CreateObject("SldWorks.Application")". The latter is archaic and I have seen issues with macros not running until it is changed to the former.


          2. Instead of declaring SolidWorks objects as Object, declare them according to their specific object interface. For example, "Dim swModel As SldWorks.ModelDoc2". This allows for you to use Microsoft Intellisense when writing code and will make your life a lot easier. If you want to see a video tutorial that demonstrates early binding and much more, check out Lesson 2.2 here.


          I hope this is helpful!



          Video Tutorials for the SolidWorks API