4 Replies Latest reply on Apr 25, 2018 12:54 PM by Aaron Muizelaar

    Insert and sort a BOM

    Charles Winter

      This code inserts a BOM.  The template, if I insert it manually sorts and pins the BOM, when called in the macro it doesn't, how do I achieve this?  The template is attached.

      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swDrawing As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swFeatMgr As SldWorks.FeatureManager

      Dim swView As SldWorks.View

      Dim swBomAnn As BomTableAnnotation

      Dim swBomFeat As SldWorks.BomFeature

      Dim AnchorType As Long

      Dim BomType As Long

      Dim Configuration As String

      Dim TableTemplate As String

      Dim Names As Variant

      Dim Visible As Variant

      Dim boolstatus As Boolean

      Dim strViewConfig As String

      Dim Filename As String

      Dim filetitle As String

      Dim checksave As Boolean

      Dim lErrors As Long

      Dim lWarnings As Long

       

       

      Sub main()

      Set swApp = Application.SldWorks

      Set swDrawing = swApp.ActiveDoc

       

      'Check to see if a document is open
      If swDrawing Is Nothing Then
              MsgBox "You must have a SolidWorks document open to use this macro.", vbOKOnly, "Nothing is open!"
              Exit Sub
      End If

      'Check to see if a drawing is open
      If swDrawing.GetType <> swDocDRAWING Then
              MsgBox "This macro only works on drawings.", , "Invalid file"
              Exit Sub
      End If

      'Check to see if the document has been saved
      Filename = swDrawing.GetPathName
      filetitle = swDrawing.GetTitle

      checksave = swDrawing.GetSaveFlag
      'MsgBox checksave
      boolstatus = swDrawing.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)

      If Filename = "" Then

      'BOM Tools - Created 4/21/2015 by Chris Manger
      '
      '2015.12.08 -  Removed code to check if user is in LMACH, all users are in LM-Test now.
      '2015.12.13 -  Updated vault paths to LMEPDM vault
      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swDrawing As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swFeatMgr As SldWorks.FeatureManager

      Dim swView As SldWorks.View

      Dim swBomAnn As BomTableAnnotation

      Dim swBomFeat As SldWorks.BomFeature

      Dim AnchorType As Long

      Dim BomType As Long

      Dim Configuration As String

      Dim TableTemplate As String

      Dim Names As Variant

      Dim Visible As Variant

      Dim boolstatus As Boolean

      Dim strViewConfig As String

      Dim Filename As String

      Dim filetitle As String

      Dim checksave As Boolean

      Dim lErrors As Long

      Dim lWarnings As Long

       

       

      Sub main()

      Set swApp = Application.SldWorks

      Set swDrawing = swApp.ActiveDoc

       

      'Check to see if a document is open
      If swDrawing Is Nothing Then
              MsgBox "You must have a SolidWorks document open to use this macro.", vbOKOnly, "Nothing is open!"
              Exit Sub
      End If

      'Check to see if a drawing is open
      If swDrawing.GetType <> swDocDRAWING Then
              MsgBox "This macro only works on drawings.", , "Invalid file"
              Exit Sub
      End If

      'Check to see if the document has been saved
      Filename = swDrawing.GetPathName
      filetitle = swDrawing.GetTitle

      checksave = swDrawing.GetSaveFlag
      'MsgBox checksave
      boolstatus = swDrawing.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)

      If Filename = "" Then
              MsgBox "You must save your drawing first", , "Save Error"
              Exit Sub
      End If

       

      Load frmBomMenu
      frmBomMenu.Show

      Select Case frmBomMenu.Tag
          Case 0
              TableTemplate = "C:\LMEPDM\Engineering\zSysEngLibrary\LM BOM Templates\LM-ASM-WLD-BOM.sldbomtbt"
              BomType = SwConst.swBomType_e.swBomType_TopLevelOnly
          Case 1
              TableTemplate = "C:\LMEPDM\Engineering\zSysEngLibrary\LM BOM Templates\LM-PARTSLIST-BOM.sldbomtbt"
              BomType = SwConst.swBomType_e.swBomType_Indented
          Case 2
              modExportBOM.main
              Unload frmBomMenu
              Set frmBomMenu = Nothing
              Exit Sub
      End Select
      Unload frmBomMenu
      Set frmBomMenu = Nothing

      Set swSelMgr = swDrawing.SelectionManager

      Set swFeatMgr = swDrawing.FeatureManager

      ' Select view
      Set swSelMgr = swDrawing.SelectionManager
      On Error Resume Next
      Set swView = swSelMgr.GetSelectedObject6(1, 0)
      If swView Is Nothing Then
          ' Tell the user that nothing is selected
          MsgBox "Please select a drawing view.", vbCritical, "No View is Selected!"
          End
      End If

      strViewConfig = swView.ReferencedConfiguration

      'MsgBox strViewConfig

      AnchorType = SwConst.swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft

      ' Insert BOM table