8 Replies Latest reply on Jun 1, 2017 7:31 AM by Cad Admin

    VBA - Delete BOM Macro - Question

    Cad Admin

      I use a simple macro i found here in the forum. https://forum.solidworks.com/message/706196#comment-706196 from Artem Taturevych . I have a Drawing with 7 different BOMS in the tree, i needed them to all be deleted.  I believed this used to work, but for some reason it is only deleting one BOM now.  From what i know it should continue till the swFeat = nothing, aka end of the feature tree.

       

      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

         

          While Not swFeat Is Nothing

         

              If "BomFeat" = swFeat.GetTypeName Then

                  swFeat.Select2 False, -1

                  swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed

              End If

       

              Set swFeat = swFeat.GetNextFeature

       

          Wend

         

      End Sub

       

        • Re: VBA - Delete BOM Macro - Question
          Peter Brinkhuis
          • Does it traverse the whole feature tree?
          • What is the return value for the feature selection?
          • What is the return value for the deletion?
          • Re: VBA - Delete BOM Macro - Question
            Cad Admin

            Deletes the first BOM then stops...if I rerun deletes the next one and so on.  It doesn't seem to be traversing the tree correctly....

              • Re: VBA - Delete BOM Macro - Question
                Peter Brinkhuis

                The idea that I tried to convey with my last post is that you need to do some more digging. Use Debug.Print on every feature name to print find out if it really does traverse the whole tree or not. Add a break point here and there, check the variable status when that breakpoint is hit and see what happens on every iteration. Without doing that, you're just guessing.

                 

                Maybe it traverses the tree, but the selection fails. Maybe the deletion fails. You say the macro stops, but does it reach the end or not?

                  • Re: VBA - Delete BOM Macro - Question
                    Cad Admin

                    Peter,

                    I had went through added some breakpoints, added some Debug.Print, ran stepping through the code. The macro ends(complete) after a single deletion.  I can step through the macro fine, single deletion. If I run it, it deletes a single BOM then ends.  I can run as may times i want, but that is not what it is meant to do.... Ill keep looking/trying.

                     

                    thanks for your help/input

                      • Re: VBA - Delete BOM Macro - Question
                        Rajat Jain

                        Hi,

                        The problem with the code is that you delete the feature where your pointer is currently placed. When you find the first BoM Feature, you delete it. The while loop tries to get the next feature from the feature which is already deleted. Thus, it ends there.

                        Kindly use the following code: -

                        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

                           

                            While Not swFeat Is Nothing

                           

                                If "BomFeat" = swFeat.GetTypeName Then

                                    swFeat.Select2 true, -1

                                End If

                                Set swFeat = swFeat.GetNextFeature

                            Wend

                            swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed

                        End Sub

                         

                        Hope it helps!!!

                        Rajat Jain

                  • Re: VBA - Delete BOM Macro - Question
                    Deepak Gupta

                    Try these codes

                     

                    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

                        swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed  

                    End Sub