3 Replies Latest reply on Oct 25, 2017 7:37 AM by Mr Omkar Deshpande

    Macro to insert BOM into sheet and link to configuration in that sheet

    Grant Kirkland

      I have a macro which will insert a BOM into a sheet, and another (thanks Deepak) which renames the sheet to match the configuration name.

      I would like to combine these macros.

       

      Goals:

      Rename sheet to configuration name

      Insert BOM (top level only)

      Rename BOM to <Configuration name> BOM

      Link BOM to the first (or any?) drawing view on each sheet (or not linked at all? I'm not sure...)

       

      I don't want the drawing views linked to a BOM, I just want the BOM on the sheet because all views on a sheet will always be linked to the BOM inserted into that sheet.

      I hope some of that makes sense.

       

       

      This is what I have so far:

       

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSheet As SldWorks.Sheet

      Dim swModelview2 As Configuration

      Dim ConfigName As String

      Dim vSheets As Variant

      Dim swView As SldWorks.View

      Dim i As Integer

       

       

      Sub main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

       

      vSheets = swModel.GetSheetNames

      For i = 1 To swModel.GetSheetCount

      swModel.ActivateSheet vSheets(i - 1)

      Set swSheet = swModel.GetCurrentSheet

      Count = swModel.GetModelViewCount

       

       

      Set swView = swModel.GetFirstView

       

       

      Do While Not swView Is Nothing

      ConfigName = swView.ReferencedConfiguration

      Set swView = swView.GetNextView

      Loop

      swSheet.SetName ConfigName

       

       

      '-----------------------------------------------------

          ' Select Drawing View1

          swModel.ClearSelection2 True

       

       

          boolstatus = swModel.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert boolstatus

          Set swView = swModel.SelectionManager.GetSelectedObject6(1, -1)

          'Set swView = swModel.GetFirstView

         

          ' Get selected drawing view

          AnchorType = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft

          BomType = swBomType_e.swBomType_TopLevelOnly

          Configuration = ""

          TableTemplate = "C:\Vault\Templates\Standard Bom - PDM.sldbomtbt"

         

          ' Insert BOM table

          Set swBomAnn = swView.InsertBomTable2(False, 0.45, 0.28, AnchorType, swBomType_e.swBomType_TopLevelOnly, Configuration, TableTemplate)

          swModel.ClearSelection2 True

      '-----------------------------------------------------

       

       

      Next i

       

       

      ' Rebuild

      swModel.EditRebuild3

      ' Zoom To Fit

      swModel.ViewZoomtofit2

      ' Save

      swModel.Save2 False

         

      End Sub

          • Re: Macro to insert BOM into sheet and link to configuration in that sheet
            Grant Kirkland

            This doesn't seem to work?

             

            ViewCongNme = swview.ReferencedConfiguration

             

            Is grabbing the correct configuration but the BOM is not changing.

              • Re: Macro to insert BOM into sheet and link to configuration in that sheet
                Mr Omkar Deshpande

                Ya, I had not tried it.

                Maybe this will work

                 

                Replace your macro completely with this (Dont forget to make a backup though) :

                 

                Dim swApp As SldWorks.SldWorks

                 

                Dim swModel As SldWorks.ModelDoc2

                 

                Dim swSheet As SldWorks.Sheet

                 

                Dim swModelview2 As Configuration

                 

                Dim ConfigName As String

                 

                Dim vSheets As Variant

                 

                Dim swView As SldWorks.View

                 

                Dim i As Integer

                 

                Dim swBomAnn As BomTableAnnotation

                 

                Dim swBomFeat As BomFeature

                 

                Dim Configuration As String

                 

                Dim sConfName As String

                 

                Sub main()

                 

                Set swApp = Application.SldWorks

                 

                Set swModel = swApp.ActiveDoc

                 

                 

                 

                 

                 

                vSheets = swModel.GetSheetNames

                 

                For i = 1 To swModel.GetSheetCount

                 

                swModel.ActivateSheet vSheets(i - 1)

                 

                Set swSheet = swModel.GetCurrentSheet

                 

                Count = swModel.GetModelViewCount

                 

                 

                 

                 

                 

                Set swView = swModel.GetFirstView

                 

                 

                 

                 

                 

                Do While Not swView Is Nothing

                 

                ConfigName = swView.ReferencedConfiguration

                 

                Set swView = swView.GetNextView

                 

                Loop

                 

                swSheet.SetName ConfigName

                 

                 

                 

                 

                 

                '-----------------------------------------------------

                 

                    ' Select Drawing View1

                 

                    swModel.ClearSelection2 True

                 

                 

                 

                 

                 

                    boolstatus = swModel.Extension.SelectByID2("Drawing View2", "DRAWINGVIEW", 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert boolstatus

                 

                    Set swView = swModel.SelectionManager.GetSelectedObject6(1, -1)

                 

                    'Set swView = swModel.GetFirstView

                 

                  

                 

                    ' Get selected drawing view

                 

                    AnchorType = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft

                 

                    'BomType = swBomType_e.swBomType_TopLevelOnly

                   

                    bomtype = swBomType_e.swBomType_PartsOnly

                   

                    Configuration = ""

                 

                    'TableTemplate = "C:\Vault\Templates\Standard Bom - PDM.sldbomtbt"

                 

                    sConfName = swView.ReferencedConfiguration

                 

                    ' Insert BOM table

                 

                    Set swBomAnn = swView.InsertBomTable2(False, 0.45, 0.28, AnchorType, swBomType_e.swBomType_PartsOnly, Configuration, TableTemplate)

                 

                    Set swBomFeat = swBomAnn.BomFeature

                   

                    swBomFeat.Name = sConfName

                   

                    swBomFeat.Configuration = sConfName

                   

                    swBomFeat.TableType = swBomType_e.swBomType_TopLevelOnly

                 

                    swModel.ClearSelection2 True

                 

                '-----------------------------------------------------

                 

                 

                 

                 

                 

                Next i

                 

                 

                 

                 

                 

                ' Rebuild

                 

                swModel.EditRebuild3

                 

                ' Zoom To Fit

                 

                swModel.ViewZoomtofit2

                 

                ' Save

                 

                swModel.Save2 False

                 

                  

                 

                End Sub