9 Replies Latest reply on Jun 27, 2016 4:02 PM by Eric Schneider

    Changes to Sheet Metal Properties Not Being Applied

    Eric Schneider

      The following was basically copy/pasted from here. When I run the following script, I get no errors, debug printout shows changes, second run of script shows changes were applied but no changes appear in the tree. What am I doing wrong?

       

      Dim swApp                       As SldWorks.SldWorks
      Dim swModel                     As SldWorks.ModelDoc2
      Dim swSelMgr                    As SldWorks.SelectionMgr
      Dim swFeat                      As SldWorks.Feature
      Dim swFeatMgr                   As SldWorks.FeatureManager
      Dim swFeatStat                  As SldWorks.FeatureStatistics
      Dim swSubFeat                   As SldWorks.Feature
      Dim swSheetMetal                As SldWorks.SheetMetalFeatureData
      Dim featCount                   As Long
      Dim counter                     As Long
      Dim bRet                        As Boolean
      
      Sub main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swSelMgr = swModel.SelectionManager
          Set swFeat = swModel.FirstFeature
          Set swFeatMgr = swModel.FeatureManager
          Set swFeatStat = swFeatMgr.FeatureStatistics
          Do While Not swFeat Is Nothing
              ' Process top-level sheet metal features
              ' Debug.Print swFeat.GetTypeName
          Select Case swFeat.GetTypeName
              Case "SheetMetal"
                  Set swSheetMetal = swFeat.GetDefinition
                  ' Rollback to change default bend radius
                  bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet
                  Debug.Print MToInch(swSheetMetal.BendAllowance)
                  Debug.Print swSheetMetal.BendAllowanceType
                  swSheetMetal.BendAllowanceType = -1
                  swSheetMetal.BendAllowance = swSheetMetal.BendRadius * 2#
                  Debug.Print MToInch(swSheetMetal.BendAllowance)
                  Debug.Print swSheetMetal.BendAllowanceType
                  ' Apply changes
                  bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet
                  Debug.Print MToInch(swSheetMetal.BendAllowance)
                  Debug.Print swSheetMetal.BendAllowanceType
                  'Rebuild sheet
                  swModel.ForceRebuild3 (False)
                  Exit Sub
              Case Else
              'not a sheetmetal feature
          End Select
          Set swFeat = swFeat.GetNextFeature
          Loop
      End Sub
      
      Function MToInch(inputVal As Double) As Double
          MToInch = (inputVal * 1000#) / 25.4
      End Function
      
        • Re: Changes to Sheet Metal Properties Not Being Applied
          Viktor Bovzdarenko

          Hi Eric

          The macro works perfectly

          It seems that it modifies bend allowance on lower level than you expect.

          See pictures:

          If modifying on higher level is needed then you should get parent of your "SheetMetal" feature and do changes there: 

          OwnerFeat = swFeat.GetParents

           

          Regards

          Viktor

            • Re: Changes to Sheet Metal Properties Not Being Applied
              Eric Schneider

              Thanks for the reply. You are correct. How do I set it to "Bend Deduction" though? If the property is set to Deduction, debug says it's -1 but when the script is run it sets to -1 but returns 3 (swBendAllowanceDirect).

              API Help swBendAllowanceTypes_e Enumeration

              swBendAllowanceBendCalculationTable 5

              swBendAllowanceBendTable 1

              swBendAllowanceDeduction 4

              swBendAllowanceDirect 3

              swBendAllowanceKFactor 2

                • Re: Changes to Sheet Metal Properties Not Being Applied
                  Viktor Bovzdarenko

                  Hi Eric

                  For  "Bend Deduction" is required to use:

                  Dim swCustBend As SldWorks.CustomBendAllowance: Set swCustBend = swSheetMetal.GetCustomBendAllowance           

                  swCustBend.Type = swBendAllowanceDeduction           

                  swCustBend.BendDeduction = swSheetMetal.BendRadius * 2

                  swSheetMetal.SetCustomBendAllowance swCustBend

                   

                  See the example of the macro, which modifies  "Bend Deduction"  here: #CODE|Modify Bend Deduction

                   

                  Hope this helps

                  Regards

                  Viktor

                    • Re: Changes to Sheet Metal Properties Not Being Applied
                      Eric Schneider

                      Thank you so much for taking the time!

                      • Re: Changes to Sheet Metal Properties Not Being Applied
                        Eric Schneider

                        I still need to set the default values in the parent folder.

                         

                        Dim OwnerFeat As SldWorks.FeatureFolder: OwnerFeat = swFeat.GetParents
                        

                        Errors out: Object variable or With block not set

                         

                        Dim OwnerFeat: OwnerFeat = swFeat.GetParents
                        Debug.Print "TypeName = " & OwnerFeat.GetTypeName
                        

                        Errors out: Object required

                         

                        Thanks again for your expertise.

                          • Re: Changes to Sheet Metal Properties Not Being Applied
                            Viktor Bovzdarenko

                            Hi Eric

                            Try to replace:

                            1. Dim OwnerFeat As SldWorks.FeatureFolder: OwnerFeat = swFeat.GetParents  to

                             

                            1. Dim vOwnerFeat As Variant: Set vOwnerFeat = swFeat.GetParents
                            2. Dim swFeat As Feature: Set  swFeat  = OwnerFeat(0)
                            3. Debug.Print "TypeName = " & swFeat.GetTypeName
                              • Re: Changes to Sheet Metal Properties Not Being Applied
                                Eric Schneider

                                Thanks again for your time. I tried what is in the post above but it still errors with type mismatch. This is strange because Variant is what the API help says to use here. API help also says to use System.Object here but the compiler complains about user defined type not defined.

                                 

                                Sub tmp()
                                
                                    Dim swApp       As SldWorks.SldWorks
                                    Dim swModel     As SldWorks.ModelDoc2
                                    Dim bRet        As Boolean
                                    Dim swFeat      As SldWorks.Feature
                                    Dim vOwnerFeat  As Variant
                                
                                    Set swApp = Application.SldWorks
                                    Set swModel = swApp.ActiveDoc
                                    Set swFeat = swModel.FirstFeature
                                    Do While Not swFeat Is Nothing
                                Debug.Print "TypeName = " & swFeat.GetTypeName
                                        If swFeat.GetTypeName = "SheetMetal" Then
                                            Set vOwnerFeat = swFeat.GetParents
                                            swFeat = vOwnerFeat(0)
                                Debug.Print "TypeName = " & swFeat.GetTypeName
                                            Dim swSheetMetal As SldWorks.SheetMetalFeatureData: Set swSheetMetal = swFeat.GetDefinition
                                            Dim swCustBend As SldWorks.CustomBendAllowance: Set swCustBend = swSheetMetal.GetCustomBendAllowance
                                            bRet = swSheetMetal.AccessSelections(swModel, Nothing): Debug.Assert bRet
                                            Debug.Print "Before: BendDeduction= " & swCustBend.BendDeduction
                                            Debug.Print "Before:CustomBendAllowance Type: " & swCustBend.Type ' 4 - swBendAllowanceDeduction
                                            swCustBend.Type = swBendAllowanceDeduction
                                            Debug.Print "After:CustomBendAllowance Type: " & swCustBend.Type ' 4 - swBendAllowanceDeduction
                                            swCustBend.BendDeduction = 0.44 / 39.3700787401575
                                            swSheetMetal.SetCustomBendAllowance swCustBend
                                            bRet = swFeat.ModifyDefinition(swSheetMetal, swModel, Nothing): Debug.Assert bRet
                                            swModel.ForceRebuild3 (False)
                                            Debug.Print "After rebuild: BendDeduction = " & swCustBend.BendDeduction
                                '            Exit Do
                                        End If
                                        Set swFeat = swFeat.GetNextFeature
                                    Loop
                                End Sub
                                
                                  • Re: Changes to Sheet Metal Properties Not Being Applied
                                    Viktor Bovzdarenko

                                    Hi Eric

                                    There was only one minor error (moved "SET" to another line):

                                    1. If swFeat.GetTypeName = "SheetMetal" Then 
                                    2.             Set vOwnerFeat = swFeat.GetParents 
                                    3.             Set swFeat = vOwnerFeat(0

                                    Here is the whole macro which worked perfectly for me:

                                    Sub Main()

                                        Dim swApp       As SldWorks.SldWorks

                                        Dim swModel     As SldWorks.ModelDoc2

                                        Dim bRet        As Boolean

                                        Dim swFeat      As SldWorks.Feature

                                        Dim vOwnerFeat  As Variant

                                     

                                        Set swApp = Application.SldWorks

                                        Set swModel = swApp.ActiveDoc

                                        Set swFeat = swModel.FirstFeature

                                        Do While Not swFeat Is Nothing

                                            Debug.Print "TypeName = " & swFeat.GetTypeName

                                            If swFeat.GetTypeName = "SheetMetal" Then

                                                vOwnerFeat = swFeat.GetParents

                                                Set swFeat = vOwnerFeat(0)

                                                Debug.Print "TypeName = " & swFeat.GetTypeName

                                                Dim swSheetMetal As SldWorks.SheetMetalFeatureData: Set swSheetMetal = swFeat.GetDefinition

                                                Dim swCustBend As SldWorks.CustomBendAllowance: Set swCustBend = swSheetMetal.GetCustomBendAllowance

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

                                                Debug.Print "Before: BendDeduction= " & swCustBend.BendDeduction

                                                Debug.Print "Before:CustomBendAllowance Type: " & swCustBend.Type ' 4 - swBendAllowanceDeduction

                                                swCustBend.Type = swBendAllowanceDeduction

                                                Debug.Print "After:CustomBendAllowance Type: " & swCustBend.Type ' 4 - swBendAllowanceDeduction

                                                swCustBend.BendDeduction = 0.44 / 39.3700787401575

                                                swSheetMetal.SetCustomBendAllowance swCustBend

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

                                                swModel.ForceRebuild3 (False)

                                                Debug.Print "After rebuild: BendDeduction = " & swCustBend.BendDeduction

                                    '            Exit Do

                                            End If

                                            Set swFeat = swFeat.GetNextFeature

                                        Loop

                                    End Sub