8 Replies Latest reply on May 22, 2018 4:47 PM by Hakan Bi̇rcan

    Looping through drawing BOM to get all assemblies

    Tommy Alatalo

      I need to loop through all levels of a drawing BOM from top level down and find all assembly drawings associated.

      The goal of this is to find all assembly drawings (and possibly export them as pdfs) associated to the product in the top drawing.

       

      So what I'm looking at is a regular BOM/Assembly structure:

       

      Top level drawing

      --> Part

      --> Part

      --> Part

      --> Assembly (Drawing level 2)

           --> Part

           --> Subassembly1 (Drawing level 3)

                --> Part

                --> Subassembly 2 (Drawing level 4)

                     --> Part

       

      One way to find all the assemblies I guess would be to add an indented BOM into the drawing, getting the assemblies from it and then deleting it, but I would prefer to use the existing BOM table, and loop through it if possible so that I don't run a risk of getting the wrong BOM data in case the original top level BOM has been edited manually.

       

      I have limited experience with VBA with most of it is coming from Excel, so any pointers or code examples as to approach this is much appreciated.

        • Re: Looping through drawing BOM to get all assemblies
          Raghvendra Bhargava

          Hi Tommy,

          Why do you need to get through the BOM to get assembly?

          You can select the view and get all assembly and component.

          try this:

           

          Dim swApp As SldWorks.SldWorks

          Dim Part As ModelDoc2

          Dim swDrw As DrawingDoc

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

           

          Sub main()

          Set swApp = Application.SldWorks

           

          Set Part = swApp.ActiveDoc

          Set swDrw = Part

          boolstatus = Part.ActivateView("Drawing View1")

          boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0.54146, 0.54953, 0, False, 0, Nothing, 0)

           

          Dim swView As View

          Set swView = swDrw.ActiveDrawingView

           

          Dim Model As ModelDoc2

          Set Model = swView.ReferencedDocument

           

          If Model.GetType = 2 Then

              Dim swAsm As AssemblyDoc

              Set swAsm = Model

             

              Dim varModel As Variant

              varModel = swAsm.GetComponents(False)

             

              Dim Compval As Variant

              Dim swcomp As Component2

              For Each Compval In varModel

                  Set swcomp = Compval

                  Debug.Print swcomp.Name2 ' Watch Immediate window to check the name

              Next

             

          End If

           

          End Sub

            • Re: Looping through drawing BOM to get all assemblies
              Tommy Alatalo

              Well the reason why to use the BOM instead of the assembly was as I said if the case is that some manual editing has been done on the BOM to either add or remove rows that are not represented by the assembly. I will give your suggestion a try though, however looking at your code I don't see it traversing down into subassemblies beyond the first assembly level, or am I mistaken?

                • Re: Looping through drawing BOM to get all assemblies
                  Raghvendra Bhargava

                  It will get you all subassembly and components as I am passing GetComponents as 'False'.

                  You can read your BOM cell : check this out >2012 SOLIDWORKS API Help - Get Table Annotation and Contents Example (VBA)

                    • Re: Looping through drawing BOM to get all assemblies
                      Tommy Alatalo

                      Thank you, I tested the macro and it's true that it loops through all levels.

                      I've changed my purpose for this macro a bit, so I would like to export all the drawings associated to the assemblies and components found by using your loop. Do I need to open the drawings via the macro in order to export them, or is it possible to do it in the background without opening the drawings as active documents?

                       

                      I already have a macro for exporting an open drawing from SW, but now I'm looking at opening drawings referenced by the parts/assemblies.

                       

                      Since you are more seasoned with SolidWorks and VBA, I would like to ask a general question about what would be a good way to learn VBA and the solidworks API more properly? Are there any good online resources to recommend?

                        • Re: Looping through drawing BOM to get all assemblies
                          Deepak Gupta

                          Tommy Alatalo wrote:

                           

                          Since you are more seasoned with SolidWorks and VBA, I would like to ask a general question about what would be a good way to learn VBA and the solidworks API more properly? Are there any good online resources to recommend?

                          I would be happy to recommend CadSharp- SolidWorks API

                          • Re: Looping through drawing BOM to get all assemblies
                            Raghvendra Bhargava

                            Hi Tommy,

                            You need to open and active Drawing to save as DXF.

                            As per your workflow please check GetDocumentDependencies2 Method (ISldWorks). It may help you in the case when you don't want to open drawing just to get the Part and Assembly dependencies.

                             

                            And check out API Beginner tutorial for your beginning.

                            Cheers!!

                              • Re: Looping through drawing BOM to get all assemblies
                                Tommy Alatalo

                                I found a possible issue with solving this, I use the SolidWorks Workgroup PDM, and when getting a local copy of the top drawing, the subassemly and lower level part drawings are not downloaded to my local machine it seems. That would mean I can't traverse down through the assembly hierarchy and open the lower drawings since they're not present. Is there a way to get all referenced parts/assemblies/drawings from the SW PDM without having to expand every level down (clicking the little "+" in the tree) from the top to be able to select every single file to be downloaded? Maybe this has to do with some PDMWorkgroup option I need to set up.

                                 

                                I would appreciate if someone could explain the difference in traversing down the assembly hierarchy and getting referenced drawings that way, compared to inserting an indented BOM in the top drawing, traversing that and getting the parts/assembly paths and referenced drawings that way? Is one better than the other and if so - why?

                                 

                                Thanks for the help so far!

                                 

                                By the way the GetDocumentDependencies2 method looks like a good tool to use, but it returns suppressed components too, is there a way around that?

                      • Re: Looping through drawing BOM to get all assemblies
                        Hakan Bi̇rcan

                        Hi Tommy,

                        Did you find the way to iterate in document tree as you described?...

                        Regards,

                        Mucip:)