7 Replies Latest reply on Dec 27, 2014 6:33 AM by Ben D

    Count Bends - Macro

    Denten McDougall
      Is there a way that i can capture the number of bends in a sheetmetal part and save them to a configuration specific property?

      Thanks for any assistance you can provide.
        • Count Bends - Macro
          Mike Spens
          Try this. The main code comes from a sample in the API help. Change the name of the property in the last line as desired.

          'Traverse SubFeatures Example
          'This example shows how to Traverse the Sub-Features
          'of each feature in a Part .

          Dim swApp As SldWorks.SldWorks
          Dim Part As SldWorks.ModelDoc2

          Sub main()
          Set swApp = CreateObject("SldWorks.Application")
          Set Part = swApp.ActiveDoc
          ' Get the 1st feature in part
          Dim Feature As SldWorks.Feature
          Set Feature = Part.FirstFeature
          Dim FeatureCount As Integer
          FeatureCount = 0
          ' While we have a valid feature
          While Not Feature Is Nothing
          ' Get the name of the feature
          Dim FeatureName As String
          Dim FeatureType As String
          Dim SubFeat As SldWorks.Feature
          Set Feature = Feature.GetNextFeature
          FeatureType = Feature.GetTypeName2
          If FeatureType = "FlatPattern" Then
          Set SubFeat = Feature.GetFirstSubFeature
          ' While we have a valid Sub-feature
          While Not SubFeat Is Nothing
          ' Get the type of the Sub-feature
          If SubFeat.GetTypeName2 = "UiBend" Then
          FeatureCount = FeatureCount + 1
          End If

          Set SubFeat = SubFeat.GetNextSubFeature
          ' Continue until the last Sub-feature is done
          Wend

          ' Get the next feature
          Set Feature = Feature.GetNextFeature()

          ' Continue until the last feature is done

          End If

          Wend
          MsgBox FeatureCount & " bends in this part"
          'add a custom property
          Part.AddCustomInfo3 "", "Number of Bends", swCustomInfoText, CStr(FeatureCount)
          End Sub
            • Count Bends - Macro
              Denten McDougall
              Thanks Mike! I am not sure how i didn't see that in the API samples, but it is exactly what i needed.

              Denten
              • Re: Count Bends - Macro
                Ben D

                Just a short input to this old thread as I used this code just recently. Mike's code works fine as long as the last feature is flat pattern. If I had other features below the flat pattern, I received an error. So I changed the code a little, and now it worked for me in all cases. This is it:

                 

                'Traverse SubFeatures Example

                'This example shows how to Traverse the Sub-Features

                'of each feature in a Part .

                 

                 

                Dim swApp As SldWorks.SldWorks

                Dim swPart As SldWorks.ModelDoc2

                 

                 

                Sub countBends()

                    Set swApp = CreateObject("SldWorks.Application")

                  

                    Set Part = swApp.ActiveDoc

                    'Get the 1st feature in part

                    Dim Feature As SldWorks.Feature

                    Set Feature = Part.FirstFeature

                    Dim FeatureCount As Integer

                    FeatureCount = 0

                  

                    ' While we have a valid feature

                    While Not Feature Is Nothing

                        ' Get the name of the feature

                        Dim FeatureName As String

                        Dim FeatureType As String

                        Dim SubFeat As SldWorks.Feature

                        FeatureType = Feature.GetTypeName2

                      

                        If FeatureType = "FlatPattern" Then

                            Set SubFeat = Feature.GetFirstSubFeature

                            ' While we have a valid Sub-feature

                            While Not SubFeat Is Nothing

                                ' Get the type of the Sub-feature

                                If SubFeat.GetTypeName2 = "UiBend" Then

                                    FeatureCount = FeatureCount + 1

                                End If

                                Set SubFeat = SubFeat.GetNextSubFeature

                                ' Continue until the last Sub-feature is done

                            Wend

                        End If

                        ' Get the next feature

                        Set Feature = Feature.GetNextFeature

                    Wend

                  

                    MsgBox FeatureCount & " bends in this part"

                    'add a custom property

                    'Part.AddCustomInfo3 "", "Number of Bends", swCustomInfoText, CStr(FeatureCount)

                End Sub