2 Replies Latest reply on Oct 29, 2015 8:29 AM by John Christensen

    Assign bend radius and bend deduction to multiple parts?

    Alex Rad

      Hello everyone!

       

      Is there a way that I can assign a certain bend radius and bend deduction  to an entire part with multiple bends that I got from a third party company? Currently I am redesigning every part so it matches the bend radius and bend deduction of the press break that we are using at work to bend the part and it takes ages when I have 100 parts to bend. Is there a faster way so I can maybe make all the changes I need all at once. It would make a huge difference! Thanks!

        • Re: Assign bend radius and bend deduction to multiple parts?
          Amen Allah Jlili

          Hello Alex!

           

          The code below does what you are looking for. It has some conditions to function properly though!

           

          Conditions:
          *Your system must be MMGS
          *The code does not traverse all bend radiis, it modifies the bend radius and the bend deduction of the Sheet Metal feature so if there's any bends that has a custom bend radius and bend deduction, this code will not modify them. You need to do that manually.


          *the Sheet Metal feature, in the feature tree manager, must be selected before this code is executed.

           

          You can remove the inputbox from swBenRadius and BA and put your values instead so you don't have to repeat their entry every time.

           

          Sub main()

           

           

              Dim swApp                   As SldWorks.SldWorks

           

           

              Dim swModel                 As SldWorks.ModelDoc2

           

           

              Dim swSelMgr                As SldWorks.SelectionMgr

           

           

              Dim swFeat                  As SldWorks.Feature

           

           

              Dim swSheetMetal            As SldWorks.SheetMetalFeatureData

           

           

              Dim bRet                    As Boolean

           

           

              Set swApp = Application.SldWorks

           

           

              Set swModel = swApp.ActiveDoc

           

           

              Set swSelMgr = swModel.SelectionManager

           

           

              Set swFeat = swSelMgr.GetSelectedObject5(1)

           

               On Error GoTo Handler:

           

              Set swSheetMetal = swFeat.GetDefinition

           

             

              Debug.Print "Feature = " & swFeat.Name

           

           

              Debug.Print "  BendRadius = " & swSheetMetal.BendRadius * 1000# & " mm"

           

           

             

              ' Rollback to change default bend radius

           

           

              bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet

           

           

           

              ' Double the default bend radius value

             

              Dim swBenRadius As Double

           

              Dim BD As Double

           

            ' Change swBendRadius and BD to your values

           

              swBendRadius = CDbl(InputBox("Please Enter Bend Radius", "New Ben Radius"))

           

              BD = CDbl(InputBox("Please Enter BD", "BD"))

             

              swSheetMetal.BendRadius = swBendRadius / 1000

             

              swSheetMetal.BendAllowanceType = 2

             

              swSheetMetal.BendAllowance = BD / 1000

           

           

              ' Apply changes

           

           

              bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet

              Exit Sub

          Handler:

              swApp.SendMsgToUser (Err.Description)

          End Sub

          • Re: Assign bend radius and bend deduction to multiple parts?
            John Christensen

            The following can be used to set all sheetmetal subfeatures to override to either BD or K

            It does not alter part geometry, it does calculate BD based on user input, or use Kfactor based on user input.

             

            '----------------------------------------------------------------------------
            ' Preconditions: Sheet metal part is open.
            '
            ' Postconditions:

            '          1) Allows choice of editing based on BD or Kfactor

            '          2) If BD is chosen, BD is calculated base on user inputs of bend rad and thickness,

            '               currently uses k = 0.3327 for BD calculations (can be edited in macro)

            '               sets subfeature overrides to BD

            '               does not alter part geometry, only flat calculations

            '               issues a report on all bend features, the location of report txt file must be altered 2 places in macro ( currently S:\aaaPROGRAM\misc)

            '          3) If K (Kfactor) is chosen,

            '                    sets sub feature overrides to Kfactor

            '                    user input allows input of new Kfactor

            '                    similar report issued for bend features

            '----------------------------------------------------------------------------
            Dim swApp                       As SldWorks.SldWorks
            Dim swModel                     As SldWorks.ModelDoc2
            Dim swSelMgr                    As SldWorks.SelectionMgr
            Dim swFeat                      As SldWorks.Feature
            Dim swSubFeat                   As SldWorks.Feature
            Dim bRet                        As Boolean
            Dim lRet                        As Long
            Dim gaugeTableFile              As String
            Dim changerad
            Dim newrad

            Option Explicit

            Sub Process_CustomBendAllowance _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swCustBend As SldWorks.CustomBendAllowance _
            )
                 
                If swCustBend.BendDeduction > 0 And swCustBend.Type = 4 Then
               
                    Debug.Print "      BD    = "; Format(swCustBend.BendDeduction * 1000 / 25.4, "##,##0.000"); "in"
                    Print #1, "      BD    = "; Format(swCustBend.BendDeduction * 1000 / 25.4, "##,##0.000"); "in"
                  
                Else
                    Debug.Print "      KFact ="; swCustBend.kFactor
                    Print #1, "      KFact ="; swCustBend.kFactor
                   
                End If

                Debug.Print ""
                Print #1, ""

            End Sub

               
            Sub Process_SheetMetal _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " ["; swFeat.GetTypeName; "]"

                Dim swSheetMetal  As SldWorks.SheetMetalFeatureData
                Set swSheetMetal = swFeat.GetDefinition
                lRet = swSheetMetal.GetOverrideDefaultParameter(bRet)

                Dim swCustBend As SldWorks.CustomBendAllowance
                Set swCustBend = swSheetMetal.GetCustomBendAllowance
                Debug.Print "      Rad   = "; Format(swSheetMetal.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swSheetMetal.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Debug.Print "      Thick = "; Format(swSheetMetal.thickness * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Thick = "; Format(swSheetMetal.thickness * 1000 / 25.4, "##,##0.000"); "in"
               
                Process_CustomBendAllowance swApp, swModel, swCustBend
               

            End Sub

            Sub Process_SMBaseFlange _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name & " ["; swFeat.GetTypeName; "]"
               
                Dim swBaseFlange                As SldWorks.BaseFlangeFeatureData
                Set swBaseFlange = swFeat.GetDefinition

                Debug.Print "      Rad   = "; Format(swBaseFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swBaseFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Debug.Print ""
                Print #1, ""
                'Process_CustomBendAllowance swApp, swModel, swBaseFlange

            End Sub

             

            Sub Process_SM3dBend _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name & " ["; swFeat.GetTypeName; "]"

                Dim swSketchBend As SldWorks.SketchedBendFeatureData
                Dim swCustBend As SldWorks.CustomBendAllowance

                Set swSketchBend = swFeat.GetDefinition
                Set swCustBend = swSketchBend.GetCustomBendAllowance
               
                Debug.Print "      Rad   = "; Format(swSketchBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swSketchBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"

                Process_CustomBendAllowance swApp, swModel, swCustBend

            End Sub

            Sub Process_SMMiteredFlange _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"
               
                Dim swMiterFlange As SldWorks.MiterFlangeFeatureData
                Dim swCustBend    As SldWorks.CustomBendAllowance

                Set swMiterFlange = swFeat.GetDefinition
                Set swCustBend = swMiterFlange.GetCustomBendAllowance

                Debug.Print "      Rad   = "; Format(swMiterFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swMiterFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
               
                Process_CustomBendAllowance swApp, swModel, swCustBend
               
            End Sub

            Sub Process_Bends _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swBends As SldWorks.BendsFeatureData _
            )

                Dim swCustBend  As SldWorks.CustomBendAllowance
                Set swCustBend = swBends.GetCustomBendAllowance

                Debug.Print "      Rad   = "; Format(swBends.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swBends.BendRadius * 1000 / 25.4, "##,##0.000"); "in"

                Process_CustomBendAllowance swApp, swModel, swCustBend

            End Sub

            Sub Process_ProcessBends _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swBends                     As SldWorks.BendsFeatureData
                Set swBends = swFeat.GetDefinition

                Process_Bends swApp, swModel, swBends
               

            End Sub

            Sub Process_FlattenBends _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swBends                     As SldWorks.BendsFeatureData
                Set swBends = swFeat.GetDefinition

                Process_Bends swApp, swModel, swBends

            End Sub

            Sub Process_EdgeFlange _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swEdgeFlange                As SldWorks.EdgeFlangeFeatureData
                Set swEdgeFlange = swFeat.GetDefinition

                Debug.Print "      Rad   = "; Format(swEdgeFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swEdgeFlange.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Debug.Print "      Angle = "; Format(swEdgeFlange.BendAngle * 57.2957795130822, "##,##0")
                Print #1, "      Angle = "; Format(swEdgeFlange.BendAngle * 57.2957795130822, "##,##0")
                Debug.Print ""
                Print #1, ""
               

            End Sub


            Sub Process_FlatPattern _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swFlatPatt     As SldWorks.FlatPatternFeatureData
                Set swFlatPatt = swFeat.GetDefinition
                Dim boolstatus As Boolean
                   
                'force CornerTreatment to false
                swFlatPatt.CornerTreatment = False
                boolstatus = swFeat.ModifyDefinition(swFlatPatt, swModel, Nothing)

                Debug.Print "      Corner Treatment? " & swFlatPatt.CornerTreatment
                Print #1, "      Corner Treatment? " & swFlatPatt.CornerTreatment
               

            End Sub

            Sub Process_Hem _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swHem                       As SldWorks.HemFeatureData
                Dim swCustBend                  As SldWorks.CustomBendAllowance

                Set swHem = swFeat.GetDefinition
                Set swCustBend = swHem.GetCustomBendAllowance

                Debug.Print "      Rad   = "; Format(swHem.Radius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swHem.Radius * 1000 / 25.4, "##,##0.000"); "in"
                Process_CustomBendAllowance swApp, swModel, swCustBend

            End Sub

            Sub Process_Jog _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name & " [" & swFeat.GetTypeName; "]"

                Dim swJog                       As SldWorks.JogFeatureData
                Dim swCustBend                  As SldWorks.CustomBendAllowance

                Set swJog = swFeat.GetDefinition
                Set swCustBend = swJog.GetCustomBendAllowance

                Debug.Print "      Rad   = "; Format(swJog.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                Print #1, "      Rad   = "; Format(swJog.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
               
                Process_CustomBendAllowance swApp, swModel, swCustBend

            End Sub

            Sub Process_LoftedBend _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim swLoftBend                  As SldWorks.LoftedBendsFeatureData
                Set swLoftBend = swFeat.GetDefinition
              

            End Sub

            Sub Process_Rip _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name & " ["; swFeat.GetTypeName; "]"

                Dim swRip                       As SldWorks.RipFeatureData
                Set swRip = swFeat.GetDefinition
              

            End Sub

            Sub Process_CornerFeat _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature _
            )

                Debug.Print "  +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "  +"; swFeat.Name & " ["; swFeat.GetTypeName; "]"

                Dim swCloseCorner               As SldWorks.ClosedCornerFeatureData
                Set swCloseCorner = swFeat.GetDefinition

               
            End Sub


            Sub Process_OneBend _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swFeat As SldWorks.Feature, _
                value, _
                newrad, _
                thickness, _
                kFactor _
            )

                Debug.Print "    +" & swFeat.Name & " [" & swFeat.GetTypeName & "]"
                Print #1, "    +"; swFeat.Name; " [" & swFeat.GetTypeName; "]"

                Dim boolstatus As Boolean
                Dim swOneBend  As SldWorks.OneBendFeatureData
                Dim swCustBend As SldWorks.CustomBendAllowance
               
                Set swOneBend = swFeat.GetDefinition
                Set swCustBend = swOneBend.GetCustomBendAllowance
               
                        '--------value change for bend deduction and kFactor
                            
            Dim ang
            Dim r
            Dim t
            Dim bd
            Dim ba
            Dim pi
            Dim k
            Dim radconst

            ang = swOneBend.BendAngle * 57.2957795130822
            r = newrad
            t = thickness
            pi = 3.141592
            k = 0.3327
            radconst = 0.0174532
            ba = ang * (pi / 180) * (r + k * t)
            bd = 2 * (Tan(ang * radconst / 2)) * (r + t) - ba

                    If ang < 170 And newrad < 100 And (value = "BD" Or value = "bd") Then

                        ' Set the bend allowance type to bend deduction
                        swCustBend.Type = swBendAllowanceDeduction
                        ' Set the value of the bend deduction
                        swCustBend.BendDeduction = bd / 39.370079
                        ' check override button
                        swOneBend.UseDefaultBendAllowance = False
                        'Set the value of the bend deduction
                        Call swOneBend.SetCustomBendAllowance(swCustBend)
                        ' Modify the bend
                        boolstatus = swFeat.ModifyDefinition(swOneBend, swModel, Nothing)
                       
                            Debug.Print "      NewRad for calc = "; Format(newrad, "##,##0.000"); "in"
                            Print #1, "      NewRad for calc = "; Format(newrad, "##,##0.000"); "in"
                            Debug.Print "      Thickness for calc = "; Format(thickness, "##,##0.000")
                            Print #1, "      Thickness for calc = "; Format(thickness, "##,##0.000")
                            Debug.Print "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                            Print #1, "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                            Debug.Print "      Bd = "; Format(bd, "##,##0.0000")
                            Print #1, "      Bd = "; Format(bd, "##,##0.0000")
                            Debug.Print ""
                            Print #1, ""
                           
                    ElseIf value = "k" Or value = "K" Then
                   
                        ' Set the bend allowance type to kFactor
                        swCustBend.Type = swBendAllowanceKFactor
                        ' Set the value of the kFactor
                        swCustBend.kFactor = kFactor
                        ' check override button
                        swOneBend.UseDefaultBendAllowance = False
                        'Set the value
                        Call swOneBend.SetCustomBendAllowance(swCustBend)
                        ' Modify the bend
                        boolstatus = swFeat.ModifyDefinition(swOneBend, swModel, Nothing)
                       
                          Debug.Print "      Default Rad   = "; Format(swOneBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                          Print #1, "      Default Rad   = "; Format(swOneBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                          Debug.Print "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                          Print #1, "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                          Debug.Print "      kFactor = "; Format(kFactor, "##,##0.0000")
                          Print #1, "      kFactor = "; Format(kFactor, "##,##0.0000")
                          Debug.Print ""
                          Print #1, ""
                        
                   
                    Else
                          Debug.Print "      Rad   = "; Format(swOneBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                          Print #1, "      Rad   = "; Format(swOneBend.BendRadius * 1000 / 25.4, "##,##0.000"); "in"
                          Debug.Print "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                          Print #1, "      Angle = "; Format(swOneBend.BendAngle * 57.2957795130822, "##,##0")
                         
                                 
                          Process_CustomBendAllowance swApp, swModel, swCustBend
                         
                    End If
            End Sub


            Sub main()

                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                Set swSelMgr = swModel.SelectionManager
                Set swFeat = swModel.FirstFeature
                Dim value As String
                Dim value2 As Double
                Dim value3 As Double
                Dim value4 As Double
               
                'Bend Deduction or kFactor user change request
                value2 = 100
                value3 = 100
                    value = InputBox("Calculates sub features BD based on user entered R and T, or use existing values and user entered K.  Base features remain unchanged.", "Bend Parameter Update", "BD/bd or K/k, Or Enter")
                    If value = "BD" Or value = "bd" Then
                        value2 = InputBox("Enter New Rad.", "New Rad", "Enter Radius to use in BD Calculation")
                        value3 = InputBox("Enter Thickness.", "Thickness", "Enter Thickness to use in BD Calculation")
                    ElseIf value = "K" Or value = "k" Then
                        value4 = InputBox("Enter new kFactor.  Sub features switch to using kFactor.  Base features remain unchanged.", "kFactor", "Enter kFactor to use.")
                    End If
                           
                'Report file location
                Open "S:\aaaPROGRAM\misc\NewFile.txt" For Output As #1    ' Open file for output.
               
                Debug.Print "File = " & swModel.GetPathName
                Print #1, "File = " & swModel.GetPathName

                Do While Not swFeat Is Nothing
                    ' Process top-level sheet metal features
                    Select Case swFeat.GetTypeName
                        Case "SMBaseFlange"
                            Process_SMBaseFlange swApp, swModel, swFeat

                        Case "SheetMetal"
                            Process_SheetMetal swApp, swModel, swFeat

                        Case "SM3dBend"
                            Process_SM3dBend swApp, swModel, swFeat

                        Case "SMMiteredFlange"
                            Process_SMMiteredFlange swApp, swModel, swFeat

                        Case "ProcessBends"
                            Process_ProcessBends swApp, swModel, swFeat

                        Case "FlattenBends"
                            Process_FlattenBends swApp, swModel, swFeat

                        Case "EdgeFlange"
                            Process_EdgeFlange swApp, swModel, swFeat

                        Case "FlatPattern"
                            Process_FlatPattern swApp, swModel, swFeat

                        Case "Hem"
                            Process_Hem swApp, swModel, swFeat

                        Case "Jog"
                            Process_Jog swApp, swModel, swFeat

                        Case "LoftedBend"
                            Process_LoftedBend swApp, swModel, swFeat

                        Case "Rip"
                            Process_Rip swApp, swModel, swFeat

                        Case "CornerFeat"
                            Process_CornerFeat swApp, swModel, swFeat

                        Case Else
                            ' Probably not a sheet metal feature
                      
                    End Select
                   

                    ' process sheet metal sub-features
                    Set swSubFeat = swFeat.GetFirstSubFeature

                    Do While Not swSubFeat Is Nothing
                        Select Case swSubFeat.GetTypeName
                            Case "OneBend"
                                Process_OneBend swApp, swModel, swSubFeat, value, value2, value3, value4
                            Case "SketchBend"
                                Process_OneBend swApp, swModel, swSubFeat, value, value2, value3, value4
                            Case Else
                                ' Probably not a sheet metal feature
                        End Select

                        Set swSubFeat = swSubFeat.GetNextSubFeature()
                    Loop

                    Set swFeat = swFeat.GetNextFeature
                
                   
                Loop
                Close #1    ' Close file.
               
                'open report file
                Shell "C:\Windows\Notepad.exe S:\aaaPROGRAM\misc\NewFile.txt", 1
               
               
            End Sub