6 Replies Latest reply on Oct 3, 2014 9:16 AM by Unspecified Unspecified

    Copy pattern if more than one feature is being used.

    Unspecified Unspecified

      I'm new to SW api programming, however, I use vba in my CAM package regularly. The issue I'm having is my CAM package does not respond well to multiple hole features being patterned together. So what I want to do is loop through all the patterns in a part, if a pattern has more than 1 feature make a new pattern/patterns so that only one feature remains in each pattern.

      Since I am new the the SW api, I just want to make sure I'm not starting down a road with no end...does this sound like a basic macro or something that would be excessively complicated?

       

      Thanks,

      Mike

        • Re: Copy pattern if more than one feature is being used.
          Jesse Seger

          It's looks like it will be possible.  I don't think it's excessively complicated, but it might take some time.

           

          You'll want to look at the ILinearPatternFeatureData interface.

           

          2014 SolidWorks API Help - ILinearPatternFeatureData Interface Members

           

          PatternFeatureArray will get or set the features.

            • Re: Copy pattern if more than one feature is being used.
              Unspecified Unspecified

              Having a little trouble with getting the array from the pattern. I could be way off so correct me if I'm wrong here but what I think I need to do is get the pattern with multiple 'things' being patterned and create a new pattern for each (based off the original pattern), then delete the original.

              I've tried a bunch of ways to get the array and split it, but I cant even get the array...done the obvious google searches and if I am looking at the right stuff its well beyond me! haha

              Heres my code so far...just looking at sketchpattern right now

               

               

              Option Explicit

               

               

              Sub main()

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim swSelMgr                As SldWorks.SelectionMgr

                  Dim swFeat                  As SldWorks.Feature

                  Dim swBody                  As SldWorks.Body2

                  Dim swSketchPatt            As SldWorks.SketchPatternFeatureData

                  Dim swSketch                As SldWorks.Sketch

                  Dim swSketchFeat            As SldWorks.Feature

                  Dim vBasePt                 As Variant

                  Dim j                       As Long

                  Dim Feature                 As Feature

                 

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                 

                  Set Feature = swModel.FirstFeature

                      While Not Feature Is Nothing

                          If Feature.GetTypeName2 = "SketchPattern" Then

                              Set swSketchPatt = Feature.GetDefinition

                              If countPatterns(swSketchPatt) > 1 Then

                                  For j = LBound(swSketchPatt.PatternFeatureArray) To UBound(swSketchPatt.PatternFeatureArray)

                                      Debug.Print j

                                  Next

                    

                              vBasePt = swSketchPatt.GetBasePoint

                              Debug.Print Feature.Name

                              Debug.Print "  GeometryPattern      = " & swSketchPatt.GeometryPattern

                              Debug.Print "  BasePt               = (" & vBasePt(0) * 1000# & ", " & vBasePt(1) * 1000# & ", " & vBasePt(2) * 1000# & ") mm"

                              Debug.Print "  BodyCount            = " & swSketchPatt.GetPatternBodyCount

                              Debug.Print "  FaceCount            = " & swSketchPatt.GetPatternFaceCount

                              Debug.Print "  FeatureCount         = " & swSketchPatt.GetPatternFeatureCount

                              Debug.Print "  RefPtType            = " & swSketchPatt.GetReferencePointType

                              Debug.Print "  UseCentroid          = " & swSketchPatt.UseCentroid

                             

                             

                              End If

                          End If

                          Set Feature = Feature.GetNextFeature

                      Wend

               

              End Sub

               

              Function countPatterns(swSketchPatt As SketchPatternFeatureData) As Long

               

              Dim i As Long

               

              i = swSketchPatt.GetPatternBodyCount

              i = i + swSketchPatt.GetPatternFaceCount

              i = i + swSketchPatt.GetPatternFeatureCount

               

              countPatterns = i

               

              End Function

                • Re: Copy pattern if more than one feature is being used.
                  Jesse Seger

                  Looks like you made some good progress.  Can you attach a sample model and maybe some screen shots showing what you're trying to accomplish exactly?  I think I understand, but I want to make sure we're on the same page.

                    • Re: Copy pattern if more than one feature is being used.
                      Unspecified Unspecified

                      Here are two parts. 'SKPattern-Before' is the original and has patterns with multiple features, 'SKPattern -After' shows the result I want, each feature has its own pattern.

                        • Re: Copy pattern if more than one feature is being used.
                          Jesse Seger

                          Here's a little update.  It's not totally complete.  See the comments in the code.

                           

                           

                           

                          Sub main()

                              Dim swApp                   As SldWorks.SldWorks

                              Dim swModel                 As SldWorks.ModelDoc2

                              Dim swSelMgr                As SldWorks.SelectionMgr

                              Dim swFeat                  As SldWorks.Feature

                              Dim swBody                  As SldWorks.Body2

                              Dim swSketchPatt            As SldWorks.SketchPatternFeatureData

                              Dim swSketch                As SldWorks.Sketch

                              Dim swSketchFeat            As SldWorks.Feature

                              Dim swSketchPoint           As SketchPoint

                              Dim swVertex                As Vertex

                              Dim swEdge                  As Edge

                              Dim swFeatMgr               As FeatureManager

                              Dim vBasePt                 As Variant

                              Dim j                       As Long

                              Dim Feature                 As Feature

                            

                              Set swApp = Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                              Set swSelMgr = swModel.SelectionManager

                              Set swFeatMgr = swModel.FeatureManager

                             

                              Set Feature = swModel.FirstFeature

                                  While Not Feature Is Nothing

                                      If Feature.GetTypeName2 = "SketchPattern" Then

                                          Set swSketchPatt = Feature.GetDefinition

                                          If countPatterns(swSketchPatt) > 1 Then

                                             

                                              For j = LBound(swSketchPatt.PatternFeatureArray) To UBound(swSketchPatt.PatternFeatureArray)

                                                 

                                                  ' for each pattern in this feature, create a new sketch driven pattern

                                                  ' this may not be complete for all scenarios

                                                  ' for example, this really only accomodates sketch driven patterns

                                                  ' where the Reference Point is set to Selected Point

                                                 

                                                  swModel.ClearSelection

                                                  Dim swPatFeat As Feature

                                                  Set swPatFeat = swSketchPatt.PatternFeatureArray(j)

                                                 

                                                  ' need to call AccessSelections to get the sketch and reference points

                                                  swSketchPatt.AccessSelections swModel, Nothing

                                                 

                                                  ' get the sketch used to create the pattern

                                                  Set swSketchFeat = swSketchPatt.Sketch

                                                 

                                                  ' get the reference point used

                                                  ' need to ReleaseSelectionAccess before selecting entities

                                                  If swSketchPatt.GetReferencePointType() = 0 Then

                                                      ' curve

                                                      Set swEdge = swSketchPatt.ReferencePoint

                                                      swSketchPatt.ReleaseSelectionAccess

                                                      ' need to select edge

                                                  ElseIf swSketchPatt.GetReferencePointType() = 1 Then

                                                      ' sketch point

                                                      Set swSketchPoint = swSketchPatt.ReferencePoint

                                                      swSketchPatt.ReleaseSelectionAccess

                                                      swSketchPoint.Select2 True, 32

                                                  ElseIf swSketchPatt.GetReferencePointType() = 2 Then

                                                      ' vertex

                                                      Set swVertex = swSketchPatt.ReferencePoint

                                                      swSketchPatt.ReleaseSelectionAccess

                                                      ' need to select vertex

                                                  End If

                                                 

                                                 

                                                  swSketchFeat.Select2 True, 64

                                                  swPatFeat.Select2 True, 4

                                                 

                                                  ' create the sketch driven pattern

                                                  ' this requires you to pre-select the features, sketches, points, etc

                                                  swFeatMgr.FeatureSketchDrivenPattern False, False

                                                 

                                              Next

                               

                                          vBasePt = swSketchPatt.GetBasePoint

                                          Debug.Print Feature.Name

                                          Debug.Print "  GeometryPattern      = " & swSketchPatt.GeometryPattern

                                          Debug.Print "  BasePt               = (" & vBasePt(0) * 1000# & ", " & vBasePt(1) * 1000# & ", " & vBasePt(2) * 1000# & ") mm"

                                          Debug.Print "  BodyCount            = " & swSketchPatt.GetPatternBodyCount

                                          Debug.Print "  FaceCount            = " & swSketchPatt.GetPatternFaceCount

                                          Debug.Print "  FeatureCount         = " & swSketchPatt.GetPatternFeatureCount

                                          Debug.Print "  RefPtType            = " & swSketchPatt.GetReferencePointType

                                          Debug.Print "  UseCentroid          = " & swSketchPatt.UseCentroid

                                        

                                        

                                          End If

                                      End If

                                      Set Feature = Feature.GetNextFeature

                                  Wend

                           

                          End Sub

                           

                          Function countPatterns(swSketchPatt As SketchPatternFeatureData) As Long

                           

                          Dim i As Long

                           

                          i = swSketchPatt.GetPatternBodyCount

                          i = i + swSketchPatt.GetPatternFaceCount

                          i = i + swSketchPatt.GetPatternFeatureCount

                           

                          countPatterns = i

                           

                          End Function

                            • Re: Copy pattern if more than one feature is being used.
                              Unspecified Unspecified

                              Thanks Jesse, this is very helpful.

                               

                              I haven't had a lot of time to play with the code yet, but so far its getting through the first skpattern and making 3 new, but it doesn't like the second skpattern...SW crashes when it hits the swFeatMgr.FeatureSketchDrivenPattern False, False line. Im assuming it has to do with the refernce point type...but I will experiment more.

                              I also added a couple lines to delete the original pattern so only the newly created ones exist and eventually I may look at sorting these new patterns to sit beside the feature they are patterning.

                              I will post my results when I have more.

                               

                              Thanks again Jesse!

                               

                              Mike