2 Replies Latest reply on Aug 12, 2014 11:18 AM by Trevor Wunn

    Issue with BOM Macro

    Trevor Wunn

      Good Afternoon Everyone,

       

      I'm having an issue with my code that I could use some advice on. The macro is supposed to run on a drawing, load the model if it isn't already, find a BOM, if it finds one, select it, find items in the bom and combine them.

      It is getting stuck right after it selects it. I know I'm missing something but I'm not sure what it is.

       

      Thanks for your help!

       

      -Trevor

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swTable As SldWorks.TableAnnotation

      Dim swBomTable As SldWorks.BomTableAnnotation

      Dim swBomFeat As SldWorks.BomFeature

      Dim swFeat As SldWorks.Feature

      Dim swView As SldWorks.View

      Dim swAnn As SldWorks.Annotation

      Dim swSelData As SldWorks.SelectData

      Dim numCol As Long

      Dim numRow As Long

      Dim i As Long

      Dim j As Long

      Dim boolStatus As Boolean

      Dim pipeTotal As String

      Dim pipeLength As String

      Dim pipeQuantity As String

      Dim flatBarTotal As String

      Dim flatBarLength As String

      Dim flatBarQuantity As String

      Dim longStatus As Long

       

      Sub main()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swDraw = swModel

      Set swSelMgr = swModel.SelectionManager

       

      'Load model

      Set swView = swDraw.GetFirstView

      Set swView = swView.GetNextView

      boolStatus = swView.LoadModel()

       

      'If boolStatus = True Then

      '    MsgBox "Model Loaded. Please insert new" & Chr(34) & "Top-Level Only" & Chr(34) & "BOM and select it before proceeding", vbCritical, "Error, Son!!"

      '    Exit Sub

      'End If

       

      'Rename first BOM found

      Set swFeat = swModel.FirstFeature

      Do While Not swFeat Is Nothing

          If "BomFeat" = swFeat.GetTypeName Then

              boolStatus = True

              swFeat.Name = "Bill of Materials1"

              swFeat.Select (False)

          End If

          If (boolStatus) Then Exit Do

          Set swFeat = swFeat.GetNextFeature

      Loop

       

      'Select BOM

      Set swView = swDraw.GetFirstView

      While Not swView Is Nothing

          Set swAnn = swView.GetFirstAnnotation3

              While Not swAnn Is Nothing

                  Debug.Print swAnn.GetName

                  If swTableAnnotation = swAnn.GetType Then

                      boolStatus = swAnn.Select3(True, swSelData)

                  End If

                  Set swAnn = swAnn.GetNext3

              Wend

          Set swView = swView.GetNextView

      Wend

                

      XXXXXXXXXXXX This is where I am having issues XXXXXXXXXXX

       

      Set swTable = swSelMgr.GetSelectedObject5(1)

      Set swBomTable = swTable

      Set swTable = swBomTable

      Set swBomFeat = swBomTable.BomFeature

       

       

      pipeTotal = 0

      pipeLength = 0

      pipeQuantity = 0

       

      flatBarTotal = 0

      flatBarLength = 0

      flatBarQuantity = 0

       

       

      numCol = swTable.ColumnCount

      numRow = swTable.RowCount

       

      For i = 0 To numRow - 1

          For j = 0 To numCol - 1

              If swTable.Text(i, j) = "STL PIPE 1.25, SCH 40" Then

                  pipeLength = swTable.Text(i, j + 1)

                  pipeQuantity = swTable.Text(i, j + 3)

                  pipeTotal = pipeTotal + (pipeLength * pipeQuantity)

                  Debug.Print "Current Pipe Quantity: " & pipeTotal

              ElseIf swTable.Text(i, j) = "STL FLAT BAR .25 X 5" Then

                  flatBarLength = swTable.Text(i, j + 1)

                  flatBarQuantity = swTable.Text(i, j + 3)

                  flatBarTotal = flatBarTotal + (flatBarLength * flatBarQuantity)

                  Debug.Print "Current FlatBar Quantity: " & flatBarTotal

              End If

          Next j

      Next i

       

       

      pipeTotal = (Int((((pipeTotal * 1.1) / 12) / 21)) + 1) * 12 * 21

      Debug.Print "Final Pipe Total: " & pipeTotal

       

       

      flatBarTotal = (Int((((flatBarTotal * 1.1) / 12) / 20)) + 1) * 12 * 20

      Debug.Print "Final FlatBar Total: " & flatBarTotal

       

       

      swBomFeat.PartConfigurationGrouping = swDisplay_ConfigurationWithSameName_AsOneItem

       

       

      For i = 1 To numRow

      If swTable.Text(i, 2) = "STL PIPE 1.25, SCH 40" Then

          swTable.Text(i, 3) = pipeTotal

          swTable.Text(i, 4) = "-"

          swTable.Text(i, 5) = "1"

          swTable.Text(i, 6) = "FIELD FIT"

      ElseIf swTable.Text(i, 2) = "STL FLAT BAR .25 X 5" Then

          swTable.Text(i, 3) = flatBarTotal

          swTable.Text(i, 4) = "-"

          swTable.Text(i, 5) = "1"

          swTable.Text(i, 6) = "FIELD FIT"

      End If

      Next

       

       

      End Sub

        • Re: Issue with BOM Macro
          Patrick O'Hern

          You are already selecting the BOM during your "Rename first BOM found" loop, so rather than selecting it again later, I added a few lines to get the BomTableAnnotation from the selected feature.  I also commented out the code that is no longer necessary.  I left it in so that you can see what changed, but you should remove it to avoid confusion later.

           

           

          Option Explicit

              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swDraw As SldWorks.DrawingDoc
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swTable As SldWorks.TableAnnotation
              Dim swBomTable As SldWorks.BomTableAnnotation
              Dim swBomFeat As SldWorks.BomFeature
              Dim swFeat As SldWorks.Feature
              Dim swView As SldWorks.View
              Dim swAnn As SldWorks.Annotation
              Dim swSelData As SldWorks.SelectData
              Dim numCol As Long
              Dim numRow As Long
              Dim i As Long
              Dim j As Long
              Dim boolStatus As Boolean
              Dim pipeTotal As String
              Dim pipeLength As String
              Dim pipeQuantity As String
              Dim flatBarTotal As String
              Dim flatBarLength As String
              Dim flatBarQuantity As String
              Dim longStatus As Long

          Sub main()

              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swDraw = swModel
              Set swSelMgr = swModel.SelectionManager
              
              'Load model
              Set swView = swDraw.GetFirstView
              Set swView = swView.GetNextView
              boolStatus = swView.LoadModel()
              
              'If boolStatus = True Then
              '    MsgBox "Model Loaded. Please insert new" & Chr(34) & "Top-Level Only" & Chr(34) & "BOM and select it before proceeding", vbCritical, "Error, Son!!"
              '    Exit Sub
              'End If
              
              'Rename first BOM found
              Set swFeat = swModel.FirstFeature
              Do While Not swFeat Is Nothing
                  If "BomFeat" = swFeat.GetTypeName Then
                      boolStatus = True
                      swFeat.Name = "Bill of Materials1"
                      swFeat.Select (False)
          '---------------------------------------------------------------------
          'New Code
                      Dim vTables As Variant
                      Set swBomFeat = swFeat.GetSpecificFeature2
                      vTables = swBomFeat.GetTableAnnotations
                      Set swBomTable = vTables(0)
          '---------------------------------------------------------------------
                  End If
                  If (boolStatus) Then Exit Do
                  Set swFeat = swFeat.GetNextFeature
              Loop
             
             
          '    'Select BOM
          '    Set swView = swDraw.GetFirstView
          '    While Not swView Is Nothing
          '        Set swAnn = swView.GetFirstAnnotation3
          '            While Not swAnn Is Nothing
          '                Debug.Print swAnn.GetName
          '                If swTableAnnotation = swAnn.GetType Then
          '                    boolStatus = swAnn.Select3(False, swSelData)
          '                End If
          '                Set swAnn = swAnn.GetNext3
          '            Wend
          '        Set swView = swView.GetNextView
          '    Wend
                        
          '    XXXXXXXXXXXX This is where I am having issues XXXXXXXXXXX
             
              
          '    Set swTable = swSelMgr.GetSelectedObject5(1)
          '    Set swBomTable = swTable
              Set swTable = swBomTable
              Set swBomFeat = swBomTable.BomFeature
              
              
              pipeTotal = 0
              pipeLength = 0
              pipeQuantity = 0
              
              flatBarTotal = 0
              flatBarLength = 0
              flatBarQuantity = 0
              
              
              numCol = swTable.ColumnCount
              numRow = swTable.RowCount
              
              For i = 0 To numRow - 1
                  For j = 0 To numCol - 1
                      If swTable.Text(i, j) = "STL PIPE 1.25, SCH 40" Then
                          pipeLength = swTable.Text(i, j + 1)
                          pipeQuantity = swTable.Text(i, j + 3)
                          pipeTotal = pipeTotal + (pipeLength * pipeQuantity)
                          Debug.Print "Current Pipe Quantity: " & pipeTotal
                      ElseIf swTable.Text(i, j) = "STL FLAT BAR .25 X 5" Then
                          flatBarLength = swTable.Text(i, j + 1)
                          flatBarQuantity = swTable.Text(i, j + 3)
                          flatBarTotal = flatBarTotal + (flatBarLength * flatBarQuantity)
                          Debug.Print "Current FlatBar Quantity: " & flatBarTotal
                      End If
                  Next j
              Next i
              
              
              pipeTotal = (Int((((pipeTotal * 1.1) / 12) / 21)) + 1) * 12 * 21
              Debug.Print "Final Pipe Total: " & pipeTotal
              
              
              flatBarTotal = (Int((((flatBarTotal * 1.1) / 12) / 20)) + 1) * 12 * 20
              Debug.Print "Final FlatBar Total: " & flatBarTotal
              
              
              swBomFeat.PartConfigurationGrouping = swDisplay_ConfigurationWithSameName_AsOneItem
              
              
              For i = 1 To numRow
              If swTable.Text(i, 2) = "STL PIPE 1.25, SCH 40" Then
                  swTable.Text(i, 3) = pipeTotal
                  swTable.Text(i, 4) = "-"
                  swTable.Text(i, 5) = "1"
                  swTable.Text(i, 6) = "FIELD FIT"
              ElseIf swTable.Text(i, 2) = "STL FLAT BAR .25 X 5" Then
                  swTable.Text(i, 3) = flatBarTotal
                  swTable.Text(i, 4) = "-"
                  swTable.Text(i, 5) = "1"
                  swTable.Text(i, 6) = "FIELD FIT"
              End If
              Next


          End Sub