7 Replies Latest reply on Feb 16, 2016 3:17 PM by Michael Spens

    Forming tools Count

    Hrituc Alexandru

      Hi,

      How can i do to count all forming tools from a part with api? ...

       

      1.jpg

        • Re: Forming tools Count
          Michael Spens

          Try this as a basis for a more complete tool.  There are so many ways to instance forming tools that it would take some time to implement them all.  Assuming you do one forming tool per feature and only use mirrored features, linear and circular patterns, this would cover it.

           

          '---------------------------------------------------------------------

          Dim swApp As SldWorks.SldWorks

           

           

          Function featureIsFormTool(swFeature As Feature) As Boolean

              If swFeature.GetTypeName2 = "FormToolInstance" Then

                  featureIsFormTool = True

              Else

                  featureIsFormTool = False

              End If

          End Function

           

           

          Function GetMirrorPatternCount(swFeature As Feature) As Integer

              Dim formToolCount As Integer

              Dim swMirrorPatternFeatureData As MirrorPatternFeatureData

              Set swMirrorPatternFeatureData = swFeature.GetDefinition

           

           

              Dim patternArray As Variant

              patternArray = swMirrorPatternFeatureData.PatternFeatureArray

              For i = 0 To UBound(patternArray)

                  Dim f As Feature

                  Set f = patternArray(i)

                  If featureIsFormTool(f) Then

                      Dim patternCount As Integer

                      Dim d1Count As Integer

                      Dim skippedCount As Integer

                      d1Count = swMirrorPatternFeatureData.GetPatternFeatureCount

                      formToolCount = d1Count

                  End If

              Next i

           

           

              GetMirrorPatternCount = formToolCount

          End Function

           

           

          Function GetCircularPatternCount(swFeature As Feature) As Integer

              Dim formToolCount As Integer

              Dim swCircularPatternFeatureData As CircularPatternFeatureData

              Set swCircularPatternFeatureData = swFeature.GetDefinition

              If swCircularPatternFeatureData.BodyPattern Then

                  formToolCount = 0

              Else

                  Dim patternArray As Variant

                  patternArray = swCircularPatternFeatureData.PatternFeatureArray

                  For i = 0 To UBound(patternArray)

                      Dim f As Feature

                      Set f = patternArray(i)

                      If featureIsFormTool(f) Then

                          Dim patternCount As Integer

                          Dim d1Count As Integer

                          Dim skippedCount As Integer

                          d1Count = swCircularPatternFeatureData.TotalInstances

                          skippedCount = swCircularPatternFeatureData.GetSkippedItemCount

                          formToolCount = d1Count - skippedCount - 1 'subtract the seed feature

                      End If

                  Next i

                 

              End If

           

           

              GetCircularPatternCount = formToolCount

          End Function

           

           

          Function GetLinearPatternCount(swFeature As Feature) As Integer

              Dim formToolCount As Integer

              Dim swLinearPatternFeatureData As LinearPatternFeatureData

              Set swLinearPatternFeatureData = swFeature.GetDefinition

              If swLinearPatternFeatureData.BodyPattern Then

                  formToolCount = 0

              Else

                  Dim patternArray As Variant

                  patternArray = swLinearPatternFeatureData.PatternFeatureArray

                  For i = 0 To UBound(patternArray)

                      Dim f As Feature

                      Set f = patternArray(i)

                      If featureIsFormTool(f) Then

                          Dim patternCount As Integer

                          Dim d1Count As Integer

                          Dim d2Count As Integer

                          Dim skippedCount As Integer

                          d1Count = swLinearPatternFeatureData.D1TotalInstances

                          d2Count = swLinearPatternFeatureData.D2TotalInstances

                          skippedCount = swLinearPatternFeatureData.GetSkippedItemCount

                          formToolCount = (d1Count * d2Count) - skippedCount - 1 'subtract the seed feature

                      End If

                  Next i

                 

              End If

           

           

              GetLinearPatternCount = formToolCount

          End Function

          Sub main()

           

           

          Set swApp = Application.SldWorks

          Dim model As ModelDoc2

          Set model = swApp.ActiveDoc

           

           

          Dim fm As FeatureManager

          Set fm = model.FeatureManager

          Dim features As Variant

          features = fm.GetFeatures(True)

          Dim i As Integer

          Dim formToolCount As Integer

           

           

          For i = 0 To UBound(features)

              Dim f As Feature

              Set f = features(i)

              If f.IsSuppressed = False Then

                  Debug.Print f.GetTypeName2

                 

                  'handle feature types

                  Select Case f.GetTypeName2

                      Case "FormToolInstance"

                          formToolCount = formToolCount + 1

                      Case "LPattern"

                          formToolCount = formToolCount + GetLinearPatternCount(f)

                      Case "CirPattern"

                          formToolCount = formToolCount + GetCircularPatternCount(f)

                      Case "MirrorPattern"

                          formToolCount = formToolCount + GetMirrorPatternCount(f)

                             

                  End Select

              End If

          Next i

          MsgBox formToolCount & " form tool features"

           

           

          End Sub

            • Re: Forming tools Count
              Hrituc Alexandru

              Hi Michael,

              I attached a model example. It's possible to get:

              1. TOTAL OF FORMING TOOLS= 9

              2.The number of each part: -counter sink emboss1= 3

                                                           -circular emboss1= 1

                                                           -arc lance1= 1

                                                           -counter sink emboss21= 4

              ???

              Thank you very much

                • Re: Forming tools Count
                  Michael Spens

                  It would take much more development work, but you could begin from the source code I shared.  You would need to add a routine to count sketch pattern points by accessing its referenced sketch. 

                  Getting the number of form features created in one operation using multiple sketch points is a different challenge.  The form tool feature does not have a definition you can access through the API.  You would need to access its child features (two sketches).  The first will contain the sketch points that define the total number created in that feature.

                  Feature names will help you determine the type of forming tool.

                  Hopefully that's enough to help you work through the remaining detail!

                   

                  Mike

                • Re: Forming tools Count
                  Hrituc Alexandru

                  Hi,

                  You count in your code instances from liniar pattern, mirror pattern, circular pattern... How get the instances from Sketch pattern?

                  Thanks

                • Re: Forming tools Count
                  Hrituc Alexandru

                  Hi,

                  On linear pattern:

                  Function GetLinearPatternCount(swFeature As Feature) As Integer

                      Dim formToolCount As Integer

                      Dim swLinearPatternFeatureData As LinearPatternFeatureData

                      Set swLinearPatternFeatureData = swFeature.GetDefinition

                      'If swLinearPatternFeatureData.BodyPattern Then

                          formToolCount = 0

                      'Else

                          Dim patternArray As Variant

                          patternArray = swLinearPatternFeatureData.PatternFeatureArray

                          For i = 0 To UBound(patternArray)

                              Dim f As Feature

                              Set f = patternArray(i)

                              If featureIsFormTool(f) Then

                                  Dim patternCount As Integer

                                  Dim d1Count As Integer

                                  Dim d2Count As Integer

                                  Dim skippedCount As Integer

                                  d1Count = swLinearPatternFeatureData.D1TotalInstances

                                  d2Count = swLinearPatternFeatureData.D2TotalInstances

                                  skippedCount = swLinearPatternFeatureData.GetSkippedItemCount

                                  formToolCount = (d1Count * d2Count) - skippedCount - 1                  'subtract the seed feature

                              End If

                          Next i

                      'End If

                   

                      GetLinearPatternCount = formToolCount

                  End Function

                  If i have a situation like in image... How can check if a feature have in child a pattern??

                  After this macro it takes only 2 forming tools because in pattern is set 2 instances... i want to do something like this:

                  If feature has a child patern then: TOTAL=number of instances from liniar pattern * parent feature skech points

                  1.jpg

                    • Re: Forming tools Count
                      Michael Spens

                      Hello Hrituc,

                      Here are a couple routines you could add to the tool.  These also show how to get parent features and determine what type of feature it is.  For example, "ProfileFeature" is the type name for a sketch.  As seen in the main procedure, "FormToolInstance" is the type name of a forming tool feature.

                       

                      '--------- Gets the number of points in the parent sketch of a forming tool feature ------------------

                      Private Function GetSkPointCount(swFeature As Feature) As Integer

                          Dim ptSketch As Sketch

                          Dim sketches As Variant

                          sketches = swFeature.GetParents

                          Dim ptCount As Integer

                          Dim i As Integer

                          For i = 0 To UBound(sketches)

                              Dim skFeat As Feature

                              Dim sk As Sketch

                              Set skFeat = sketches(i)

                              If skFeat.GetTypeName2 = "ProfileFeature" Then

                                  Set sk = skFeat.GetSpecificFeature2

                                  ptCount = sk.GetSketchPointsCount2 - 2 'sketch always has 2 extra points on a centerline

                              End If

                          Next i

                          GetSkPointCount = ptCount

                      End Function

                       

                      '--------- Gets the number of points in the parent sketch of a sketch pattern -----------------

                      Function GetSkPatternCount(swFeature As Feature) As Integer

                          Dim formToolCount As Integer

                          Dim swSketchPatternFeatureData As SketchPatternFeatureData

                          Set swSketchPatternFeatureData = swFeature.GetDefinition

                       

                          Dim patternArray As Variant

                          patternArray = swSketchPatternFeatureData.PatternFeatureArray

                          For i = 0 To UBound(patternArray)

                              Dim f As Feature

                              Set f = patternArray(i)

                              If featureIsFormTool(f) Then

                                  Dim patternCount As Integer

                                  Dim d1Count As Integer

                                  Dim skippedCount As Integer

                                  Dim sk As Sketch

                                  Dim sketches As Variant

                                  sketches = swFeature.GetParents

                                  Dim j As Integer

                                  For j = 0 To UBound(sketches)

                                      Dim skFeat As Feature

                                      Set skFeat = sketches(j)

                                      If skFeat.GetTypeName2 = "ProfileFeature" Then

                                          Set sk = skFeat.GetSpecificFeature2

                                          d1Count = sk.GetSketchPointsCount2

                                      End If

                                  Next j

                                  formToolCount = d1Count

                              End If

                          Next i

                          GetSkPatternCount = formToolCount

                      End Function