5 Replies Latest reply on Apr 1, 2016 7:07 AM by Robert Chase

    Query list of General Table names in a Drawing

    Robert Chase

      I'm working on updating a macro written a few years ago. The macro takes a General Table (or SW Assembly BOM) on a drawing sheet which is a BOM table and converts it to a CSV file. In the past I had it set so that the user had to pre-select the table before running the macro. I'm rewriting the macro to add some more functionality and while I'm at it I'm trying to make it so you do not have to pre-select the table.


      I would like to be able to have the user have a drawing open, run the macro and it does the following basic routine:

      Find the name of all General Tables in the drawing

      Look at how many columns are in each table that exists

      If it has 6 columns, look at the first cell and see if it has certain text, if it does then that is the Bom table that I want.


      Then I would go through my routine to export the data.

      (We use General Tables for items that are not modeled and do not show up in the SW Assembly BOM)

      I'm using VB.net currently for this macro.


      I know I can select a table with a known name using a command like this:


              Dim swModel As ModelDoc2

              Dim swDrawing As DrawingDoc

              Dim swAssembly As AssemblyDoc

              Dim status As Boolean

              Dim swTableAnnotation As TableAnnotation

              Dim swGeneralTableFeature As GeneralTableFeature

              Dim swSelectionMgr As SelectionMgr

              Dim swModelDocExt As ModelDocExtension

              Dim swFeature As Feature


              'Use currently opened document

              swModel = swApp.ActiveDoc


                'Assume GeneralTable1 is the table we want, and select it

              swModelDocExt = swModel.Extension

              status = swModelDocExt.SelectByID2("General Table1", "GENERALTABLEFEAT", 0, 0, 0, False, 0, Nothing, 0)

              swSelectionMgr = swModel.SelectionManager

              swGeneralTableFeature = swSelectionMgr.GetSelectedObject6(1, -1)

              swFeature = swGeneralTableFeature.GetFeature


      The problem is the table will not always be General Table1... It could be 2, 3, 5... and it may be that 1, 2, and 3 don't exist because someone created one and then deleted it. So I'm trying to avoid simply running through a loop guessing the names, if possible. With genuine BOM features there is a Next feature command that you can go through a list of them, but I cannot seem to figure out how to do the same for General Tables since they don't seem to show up in the feature list.




        • Re: Query list of General Table names in a Drawing
          Kevin Chisholm

          You can use this as a function to get the table name. Then use sBOMName as the name of the BOM to select.


          Dim swApp As Object
          Dim swModel As SldWorks.ModelDoc2
          Dim swFeat As SldWorks.Feature
          Dim sBOMName As String
          Sub mMain()
          Set swApp = Application.SldWorks
          If swApp.GetDocumentCount() = 0 Then Exit Sub
          Set swModel = swApp.ActiveDoc
          If swModel Is Nothing Or swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then
                 MsgBox ("You Must Run This Macro Inside A Drawing")
                 Exit Sub
          End If
          sBOMName = ""
          Set swFeat = swModel.FirstFeature
          Do While Not swFeat Is Nothing
                 If (swFeat.GetTypeName = "BomFeat") Then
                    sBOMName = swFeat.Name
                    Exit Do
                    Set swFeat = swFeat.GetNextFeature
                 End If
          If sBOMName = "" Then
             Debug.Print ("There Was No BOM Found On This Sheet")
             Debug.Print ("The Name Of The BOM Is: " + sBOMName)
          End If
          End Sub


          Hope this helps!

            • Re: Query list of General Table names in a Drawing
              Robert Chase

              I tried code similar to that already but it does not show the General Tables. I ran it and had it print out each 'feature' that it hit in the loop. This works wonderfully for the real BOM Features, but not for the Bom that uses the General Tables on our drawings that is hand typed in. This is the one I'm trying to find a more elegant way to find. I'm not sure if it is a sub-feature of the Sheet and if it is how I would drill down and query the sheet level for features.




            • Re: Query list of General Table names in a Drawing
              Solid Air

              Try this code

              Sub main()

                  Dim swApp As SldWorks.SldWorks
                  Dim swDoc As SldWorks.ModelDoc2
                  Dim swFeat As SldWorks.Feature
                  Dim swSubFeat As SldWorks.Feature
                  Set swApp = Application.SldWorks
                  Set swDoc = swApp.ActiveDoc
                  Set swFeat = swDoc.FirstFeature
                  Do Until swFeat Is Nothing
                      Debug.Print swFeat.Name, swFeat.GetTypeName
                      Set swSubFeat = swFeat.GetFirstSubFeature
                      Do Until swSubFeat Is Nothing
                          Debug.Print swSubFeat.Name, swSubFeat.GetTypeName
                          Set swSubFeat = swSubFeat.GetNextSubFeature
                      Set swFeat = swFeat.GetNextFeature
                  Set swFeat = Nothing
                  Set swSubFeat = Nothing
                  Set swDoc = Nothing
                  Set swApp = Nothing
              End Sub

              • Re: Query list of General Table names in a Drawing
                Robert Chase

                For Posterities sake in case anyone else wants to do something similar here is what the screen grab looks like.