5 Replies Latest reply on Jun 28, 2017 4:35 PM by Trevor Holloway

    Macro to associate BOM with Sheet Number

    Trevor Holloway

      Hi,

      Does anyone know of a way to find what sheet of a multi-sheet drawing a BOM resides on?  This seems like it should be easy, but after searching forums and the API help, it doesn't appear to be as simple as I thought. 

       

      I have a macro that cycles through each sheet in a multi-sheet drawing, saves the BOM out of each sheet to Excel, and saves the drawing sheet to PDF.  It would help simplify my code if I could determine which sheet the BOM resides on, since I could then just cycle through the drawing sheet by sheet.  Plus, I want to know if there is a way to do this just because I'm curious if it can be done.

       

      BOMs seem to be treated as "features" that can be cycled through in the feature tree using getnextfeature.  My code uses getfirstfeature...getnextfeature to extract the BOMs for now.  My problem is that I don't care about where they are in the feature tree; I care about what sheet they're on, and there seems to be no property in the BOM feature that contains this information.

       

      The code is pretty messy, but I attached it for reference.  My job is to go through and clean it up, hence this question.  Any help is appreciated.

        • Re: Macro to associate BOM with Sheet Number
          Cad Admin

          Trevor,

          The code below will return all Names of all BOMs in a drawing using the feature tree...Expand as needed

           

          Sub main()

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swFeat As SldWorks.Feature

             

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swFeat = swModel.FirstFeature

              swModel.ClearSelection2 True

             

              While Not swFeat Is Nothing

                  If "BomFeat" = swFeat.GetTypeName Then

                      Debug.Print swFeat.Name

                      swFeat.Select2 True, 0

                  End If

                  Set swFeat = swFeat.GetNextFeature

              Wend

          End Sub

            • Re: Macro to associate BOM with Sheet Number
              Trevor Holloway

              Cad Admin,

              Thanks for the quick reply.  What I'm actually looking for is a way to associate the name of each BOM with the name of the drawing sheet that contains it.  However, there doesn't appear to be a property such as "swFeat.sheetname" that gives me this information.  Any help is appreciated.

                  • Re: Macro to associate BOM with Sheet Number
                    Trevor Holloway

                    Thanks for your help; I have no idea how I missed that in all my searching.  I haven't used it yet, but it looks like exactly what I need.  I'll mark this correct for now in case I forget to check back once I use it.

                    • Re: Macro to associate BOM with Sheet Number
                      Trevor Holloway

                      For future reference, here is the code I ended up using.  I cut this out of the main procedure and simplified it a bit, so the loops may not be what you want, but it should work with a bit of tweaking.  Thanks to CADAdmin and Artem Taturevych for the help in getting this to work.

                      Sub Main()
                      Dim swApp As Object
                      Dim swModel As ModelDoc2
                      Dim swFeat As SldWorks.Feature
                      Dim swBOMFeat As SldWorks.BomFeature
                      Dim swBOMAnn As Variant
                      Dim swBOMAnnParent As Variant
                      Dim swBOMAnnitem As SldWorks.TableAnnotation
                      Dim swAnn As SldWorks.Annotation
                      Dim swViewTest As SldWorks.view
                      Dim swSheetTest As SldWorks.Sheet
                      
                      
                      'Macro finds BOMS in drawing file and prints their names and the sheet name containing them to the immediate window
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      If swModel.GetType = swDocDRAWING Then
                      
                      
                          'Get array of BOMs' corresponding sheet names (this should be run in a loop that iterates through each sheet)
                          Set swFeat = swModel.FirstFeature
                          Do While Not swFeat Is Nothing
                              If "BomFeat" = swFeat.GetTypeName Then
                                  Set swBOMFeat = swFeat.GetSpecificFeature2
                                  swBOMAnn = swBOMFeat.GetTableAnnotations
                                  For Each swBOMAnnParent In swBOMAnn
                                      Set swBOMAnnitem = swBOMAnnParent
                                  Next swBOMAnnParent
                                  'just use one of the returned items since they're all on the same sheet
                                  Set swAnn = swBOMAnnitem.GetAnnotation()
                                  If swAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingSheet Then
                                      Set swSheetTest = swAnn.Owner
                                  ElseIf swAnn.OwnerType = swAnnotationOwner_e.swAnnotationOwner_DrawingView Then
                                      Set swViewTest = swAnn.Owner
                                      Set swSheetTest = swViewTest.Sheet
                                  End If
                                  Debug.Print (swSheetTest.GetName & " contains " & swBOMFeat.name) 'Prints sheet name containing BOM
                              End If
                              Set swFeat = swFeat.GetNextFeature
                          Loop
                      End If
                      End Sub