3 Replies Latest reply on Jun 23, 2017 7:39 AM by Tom Curtis

    API VBA: How do you get total length weld, despite different bead types?

    Tom Curtis

      Hi,

       

      I am attempting to write a macro to pick out various features for costing an assembly due to the way our company puts models together.

       

      Where I am stuck is with the GetWeldBeadFolder() function, in particular accessing all the weld types at once and getting a total length. For example, a 3 mm butt weld, a 3 mm fillet weld, and a 4 mm fillet weld will be in separate weld folders within the assembly. The above function allows me to use TotalLength, but only within a single sub weld folder, not the overall weld folder.

       

      Is there a way to access the total length of all welds at the assembly level, regardless of type?

       

      I think my only alternative is to loop through the cosmetic weld beads in an assembly and pick out each length, to add together later.

       

      Any help with this would be greatly appreciated. I finally got the function to work and it doesn't do what I want in this instance!

        • Re: API VBA: How do you get total length weld, despite different bead types?
          Peter Brinkhuis

          I must say the code looks a little scary Instead of those very deeply nested if statements that limit the amount of items you can handle, you can create a while loop:

           

          dim i as integer

          i=1

          status = swModelDocExt.SelectByID2("Weld Bead" & i, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)

          While status 'or While status = true

               'do something

               i=i+1

               status = swModelDocExt.SelectByID2("Weld Bead" & i, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)

          Wend

           

          This would work if every item has a name that is perfectly defined. This is generally not the case, so it would be better to traverse the feature tree for welds, then get that weld and add the length to an overal length variable. You probably won't find a function that gives you the total weld length anyway, so I suggest you traverse the feature tree.

          • Re: API VBA: How do you get total length weld, despite different bead types?
            Tom Curtis

            I found the answer! Many thanks to Peter Brinkhuis Thank You   for getting me to use a loop, it was the key that unlocked navigating subweld folders. Below is the macro text, I hope it can save someone else the pain one day. This will work even if there are no welds. Works nicely for welds at part level or assembly level.

             

             

             

            'Precondiotions:

            ' 1. The assembly must be open

            ' 2. There must be a custom property called finish

            ' 3. The cosmetic welds must be numbered sequentially starting with "Weld Bead1"

             

             

            'Explicit dimension

            Dim swApp As SldWorks.SldWorks

            Dim swModel As SldWorks.ModelDoc2

            Dim swModelDocExt As ModelDocExtension

            Dim swCustPropMgr As CustomPropertyManager

             

             

            Sub main()

                'Predimension for the whole routine:

                    '- For getting finish

                    Dim val As String

                    Dim valout As String

                    Dim swAssyFinish As String

                   

                    'For getting Assembly Weld data

                    Dim swAssyWeldName As String

                    Dim bStatus As Boolean

                    Dim swSelectionMgr As SldWorks.SelectionMgr

                    Dim swAssyWeld As Feature

                    Dim swAssyWeldData As SldWorks.CosmeticWeldBeadFeatureData

                    Dim swAssyWeldFolder As SldWorks.CosmeticWeldBeadFolder

                    Dim swAssySubWeldLengthTotal As Variant

                    Dim swAssySubWeldCount As Integer

                    Dim swAssyWeldLengthTotal As Variant

                    Dim swAssyWeldCountTotal As Integer

                    Dim swAssyWeldTypeCount As Integer

                          

                'connect to the application

                Set swApp = Application.SldWorks

               

                'Connect to the open document

               

                Set swModel = swApp.ActiveDoc

               

                'Find the cutom property called "Finish"

                Set swModelDocExt = swModel.Extension

                ' Get the custom property data

                Set swCustPropMgr = swModelDocExt.CustomPropertyManager("")

                swCustPropMgr.Get4 "Finish", False, val, valout

                swAssyFinish = valout

               

                Debug.Print "Assembly Finish:          " & swAssyFinish

               

                'Find weld information in this assembly:

                                                 

                    'Prepare values to loop through the weld features

                   

                    Dim i As Integer

                    i = 1

                    swAssyWeldTypeCount = 0

                    bStatus = swModelDocExt.SelectByID2("Weld Bead" & i, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)

                    'Enter a loop to go through every COSMETIC_WELD in the model

                   

                    While bStatus

                   

                   

                        'Select "Weld Bead1"

                        Set swSelectionMgr = swModel.SelectionManager

                        Set swAssyWeld = swSelectionMgr.GetSelectedObject6(1, 0)

                       

                   

                        'Access the data related to welds

                        Set swAssyWeldData = swAssyWeld.GetDefinition

                        'Access the folder for welds

                        Set swAssyWeldFolder = swAssyWeldData.GetWeldBeadFolder()

                        'Assign the values of total length and quantity to their own values

                        swAssySubWeldLengthTotal = swAssyWeldFolder.TotalLength

                        swAssySubWeldCount = swAssyWeldFolder.TotalNumber

                        'Accumulate values for overall length, quantity and types of welds

                        swAssyWeldCountTotal = swAssyWeldCountTotal + swAssySubWeldCount

                        swAssyWeldTypeCount = swAssyWeldTypeCount + 1

                       

                        'Select the weld bead in the next sub folder by adding 1 to number in current sub folder

                        i = i + swAssySubWeldCount

                       

                        bStatus = swModelDocExt.SelectByID2("Weld Bead" & i, "COSMETIC_WELD", 0, 0, 0, False, 0, Nothing, 0)

                        swAssyWeldLengthTotal = swAssyWeldLengthTotal + swAssySubWeldLengthTotal

                    Wend

             

             

              

                'print to see assembly weld results

                Debug.Print "Total weld length:        " & swAssyWeldLengthTotal

                Debug.Print "Total number of welds:    " & swAssyWeldCountTotal

                Debug.Print "Number of weld Types used:" & swAssyWeldTypeCount

             

             

            End Sub