7 Replies Latest reply on May 19, 2017 12:43 PM by Alex Hartung

    How to split BOM table and cut and paste sections into different sheets with API

    Alex Hartung

      Hello all,

       

      I am currently writing a BOM Macro that will take a model and auto-generate an entire BOM based on our specified template. The end results include:

           1. Cover Page with Isometric model View of the part placed in the center of the page and scaled appropriately.

           2. BOM sheet with Same model view inserted in the bottom right section and scaled appropriately.

           3. Bill of Materials Table inserted into the BOM page using the model view previously inserted in the bottom right.    

        

      Additional features:

           Often BOM are very long and for the specific sheet size and format I am using, 14 items is often the cut-off I want for the BOM table per sheet. With that being said, I am using the horizontal auto-split method to split the BOM table into an array of Tables that each contain 14 items until the last member which will have the remainder. The ideal format will thus be an extra BOM sheet for each index in the BOM table array.

       

       

      The problem is... I cannot figure out how to manipulate and select the specific sections of the BOM that were cut up by the auto-split. I have tried using all kinds of different objects like ModelDoc to use the .EditCut and .Paste features to Cut and Paste the specific sections I am trying to move into each new sheet. I have done this successfully with making enough copies of the BOM Sheet that match the (# of items in BOM Table / 14). The reason I don't want to insert the BOM table into the BOM Sheet and then copy and paste as many times as I want and then delete the specific rows outside my range is because then I end up with a bunch of Bill of Materials Tables rather than just 1. With more than 1, they will not auto-update properly and thus does not give me the functionality I am looking for.

       

      If anyone could please shed some light on this, I would greatly appreciate it!

       

      P.S. I have attempted to record a macro as I cut and paste the sections I am looking for and ultimately the results give me something like Part.Extension.SelectByID2("DetailItem456@BOM", "TABLEANNOTATIONS", blah blah). But this does not make sense where they would get these "DetailItem456" and when I do multiple selections one after another for each new section of the BOM Table, it always gives me "DetailItem456" as the selection name. This simply does not seem correct. When I implement in a greater schema, it ends up copying and pasting the sheets and the BOM table entirely.

       

      Thanks in advance! I have provided the brief section I have written so far that seems to be functional with the section I am trying to work on commented out.

       

      Dim swApp           As SldWorks.SldWorks

      Dim swModelDoc      As ModelDoc2

      Dim swDrawingDoc    As DrawingDoc

      Dim swView          As SldWorks.View

      Dim sheetNames      As Variant

      Dim documentTitle   As String

      Dim drawingTemplate As String

      Dim sizes           As Variant

      Dim scaleratio      As Integer

      Dim count           As Integer

      Dim Part As Object

      Dim swModel As ModelDoc2

      Dim boolstatus As Boolean

       

      Sub main()

          '***Get the SldWorks app, Model, and Doc

          Set swApp = Application.SldWorks

          Set swModelDoc = swApp.ActiveDoc

          documentTitle = swModelDoc.GetTitle

         

          drawingTemplate = swApp.GetUserPreferenceStringValue(swFileLocationsDocumentTemplates)

          drawingTemplate = "F:\Dropbox (OceanAero)\MECHE\SOLIDWORKS_FILES\SOLIDWORKS_TEMPLATES\DRAWINGS\TEMPLATE_DRAWING_BOM_DEFAULT_SOLIDWORKS_V01R08.drwdot"

         

          sizes = swApp.GetTemplateSizes(drawingTemplate)

         

          If IsEmpty(sizes) Then

              MsgBox "Failed to get valid template sizes."

              Exit Sub

          End If

         

          ' Create a new Drawing

          Set swDrawingDoc = swApp.NewDocument(drawingTemplate, sizes(0), sizes(1), sizes(2))

          sheetNames = swDrawingDoc.GetSheetNames

          Dim status As Boolean

          Dim Position As Variant

          Dim retVal As String

          Set swView = swDrawingDoc.CreateDrawViewFromModelView3(swModelDoc.GetPathName(), "*Isometric", 0, 0, 0)

          swView.SetDisplayMode (swViewDisplayMode_ShadedWithEdges) ' Not working for some reason

          Position = swView.Position

          Position(0) = Position(0) + 0.12

          Position(1) = Position(1) + 0.16

          swView.Position = Position

          swView.ScaleDecimal = 0.1

          swDrawingDoc.ActivateView ("Drawing View1")

          swView.GetDisplayEdgesInShadedMode

          swDrawingDoc.ViewDisplayShaded

          swDrawingDoc.ViewModelEdges

          swDrawingDoc.EditRebuild

          swDrawingDoc.ActivateSheet sheetNames(1)

          Set swView = swDrawingDoc.CreateDrawViewFromModelView3(swModelDoc.GetPathName(), "*Isometric", 0, 0, 0)

          Position = swView.Position

          Position(0) = Position(0) + 0.3

          Position(1) = Position(1) + 0.034

          swView.Position = Position

          swView.ScaleDecimal = 0.02

          swDrawingDoc.ViewModelEdges

          swDrawingDoc.ViewDisplayShaded

          swDrawingDoc.EditRebuild

          swDrawingDoc.ActivateSheet ("BOM")

          Set swModelDoc = swDrawingDoc

          status = swModelDoc.Extension.SelectByID2("BOM", "SHEET", 0.09205356547875, 0.10872368523, 0, False, 0, Nothing, 0)

          Dim currentsheet As Sheet

          Set currentsheet = swModelDoc.GetCurrentSheet

          swModelDoc.EditCopy

         

          For i = 0 To 3 ' This will be adapted to the # of splits that are done for the BOM table

              swModelDoc.ActivateSheet (currentsheet.GetName)

              status = swModelDoc.PasteSheet(swInsertOption_MoveToEnd, swRenameOption_Yes)

          Next i

       

          '***Get the first View

          '***Set the full path to the desired BOM template

          '***alternatively an empty string will use the default template

          Dim TemplatePath As String

          TemplatePath = swApp.GetUserPreferenceStringValue(swFileLocationsBOMTemplates)

          TemplatePath = "F:\Dropbox (OceanAero)\MECHE\SOLIDWORKS_FILES\SOLIDWORKS_TEMPLATES\BOM\TEMPLATE_BOM_DEFAULT_SOLIDWORKS_V01R08.sldbomtbt"

         

          '***Set the arguments for InsertBomTable2

          Dim UseAnchorPoint As Boolean

          UseAnchorPoint = True

          Dim X As Double

          X = 0#

          Dim Y As Double

          Y = 0#

          Dim AnchorType As Long

          AnchorType = swBOMConfigurationAnchor_TopLeft

          Dim BomType As Long

          BomType = swBomType_PartsOnly

          Dim Configuration As String

          Configuration = ""  'Empty string uses the configuration referenced in the view

          Dim TableTemplate As String

          TableTemplate = TemplatePath    'Full path to the template required

         

          '***Insert the BomTableAnnotation

          Dim BomTableAnnotationObj As SldWorks.BomTableAnnotation

          Dim swTable              As SldWorks.TableAnnotation

          Dim swTableArray()          As SldWorks.TableAnnotation

          swDrawingDoc.ActivateSheet ("BOM")

          Set swView = swDrawingDoc.ActivateView("DrawingView2")

          Set BomTableAnnotationObj = swView.InsertBomTable2(UseAnchorPoint, X, Y, AnchorType, BomType, Configuration, TableTemplate)

          Set swTable = BomTableAnnotationObj

          count = swTable.TotalRowCount / 14

          swTableArray = swTable.HorizontalAutoSplit(14, swHorizontalAutoSplitApply_Continuousely, swHorizontalAutoSplitPlacementOfSplitTable_BelowLastSplit)

          Debug.Print count

         

          Set Part = swApp.ActiveDoc

          For i = 1 To UBound(swTableArray)

              status = Part.Extension.SelectByID2("DetailItem456@BOM", "ANNOTATIONTABLES", 1.98546152455048E-03, -8.21423383205782E-02, 0, False, 0, Nothing, 0)

              Part.EditCut

              swDrawingDoc.ActivateSheet ("BOM(" & i & ")")

              Part.ViewZoomtofit2

              Part.Paste

              swDrawingDoc.ActivateSheet ("BOM")

          Next i

          swDrawingDoc.EditRebuild

          '***Update to show the BOM feature in the Feature Manager Tree

          swDrawingDoc.FeatureManager.UpdateFeatureTree

      End Sub