10 Replies Latest reply on Dec 7, 2015 4:46 PM by Arthur McRae

    macro replace chain of splines

    Arthur McRae

      So I work in the sign industry and get sent dxf files from our graphic designers. Often they contain splines. My problem is I've set up parametric parts and assemblies based on a copied dxf sketch, when the graphic designers do a revision, scale etc I have to basically start over selecting every imported spline and replacing with the new.  I've used excel macros a bit and know how powerful they can be but I don't even have a clue where to start with this. Help.

           Is there anyone out there able to help me get started? I'd love to be able to select chain I want to replace with then select chain on the original.  The spline counts will be the same, the same placement in relation to one another but often scaled.  Let me know if you need any more information on my dilemma.

       

      Thanks so very much for any time you spend on this;

      Arthur

        • Re: macro replace chain of splines
          Feroz Mahomed

          Arthur,

          Can you post a sample dxf file with splines? I want to test the fit spline function.

            • Re: macro replace chain of splines
              Arthur McRae

              Here you are, this is how I receive the file from the graphic designers.  I haven't removed the leader lines that have become extra line segments like I normally do before importing to SolidWorks.

                • Re: macro replace chain of splines
                  Feroz Mahomed

                  Arthur,

                   

                  Attached is a macro I use to select all splines.

                  I sometimes get splines in my sketches when I use the convert entity function. I replace the spline manually by creating a tangent arc. I then use the macro to select all splines and then delete them. Please note that the macro only selects all splines in the active sketch.Deleting would have to be done manually.

                    • Re: macro replace chain of splines
                      Arthur McRae

                      The approach I was trying to take (not having any luck with) was have active sketch, prompt select chain, recording splines in array, select second chain, user check message that chain was selected from same place, use replace entity with each point in the array.  I can't get any of it to debug properly. I'm not sure if I'm taking the correct approach or if what I'm attempting is impossible. I'm still new at learning the SolidWorks API library.

                      Also checked the file I uploaded, just realized that particular file is all polylines not splines

                  • Re: macro replace chain of splines
                    Arthur McRae

                    So after discovering that I uploaded an inaccurate example I was given this. A literal example. I was give these files made a bent aluminium sheet pan with a cut out of the letters, I then make a plastic letter with a flange and place it in the cut out. I was then given the second file with resizing, right now my only option is having to go  back through the entire process of doing my offsets on each letter again offset the push thru .125  and the flange .625 from the original again. I discovered that by selecting and using the replace entity tool none of my previous work is messed up. The problem with that is on the more complicated letters that requires allot of clicking each separate piece.

                  • Re: macro replace chain of splines
                    Arthur McRae

                    So my thought process (nothing is working yet) for the macro is this:

                    check that sketch is open

                    prompt to select chain to be replaced (can't figure this out at all)

                    save selection in order as array

                    prompt to select chain to keep

                    save to next column in array

                    run replace entities through array

                    ask if want to run macro on another chain (not sure how to code this either)

                     

                    I have checked that in scaled chains that the chain selection order is always the same.

                    Deepak Gupta I know you have created some beautiful macros, can you help?

                    • Re: macro replace chain of splines
                      Arthur McRae

                      here is what I've gotten so far.

                      I can't get the replace sketch function to properly work

                       

                      Dim swApp As SldWorks.SldWorks

                      Dim swDoc As SldWorks.ModelDoc2

                      Dim swSketch As SldWorks.Sketch

                      Dim SelMgr As SldWorks.SelectionMgr

                      Dim sketchSegment As SldWorks.sketchSegment

                      Dim SelData As SldWorks.SelectData

                      Dim SelectCount As Long

                      Dim boolstatus As Boolean

                      Dim check As Boolean

                      Dim iReply As String

                      Dim vSegs As Variant

                      Dim vSeg As Variant

                      Dim swSeg As SldWorks.sketchSegment

                      Sub main()

                       

                      Set swApp = Application.SldWorks

                      Set swDoc = swApp.ActiveDoc

                      Set swSketch = swDoc.SketchManager.ActiveSketch

                       

                       

                      iReply = "WARNING! Macro underdevelopment"

                      MsgBox iReply

                       

                       

                      'check that sketch active

                      If swSketch Is Nothing Then

                          MsgBox "No sketch active!"

                          Exit Sub

                      End If

                       

                       

                      'select chains

                      Dim I As Integer

                      Dim N As Integer

                      Dim CountCheck As Variant

                      ReDim CountCheck(1)

                       

                       

                      check = False

                          While check = False

                                 

                              For I = 0 To 1

                             

                              vSeg = ChainSelector(I)

                             

                              CountCheck(I) = SelectCount

                              If I = 0 Then

                              ReDim vSegs(UBound(vSeg), 1)

                              Else

                                  ' error message

                                  If Not CountCheck(0) = CountCheck(1) Then

                                  iReply = "Selected chains do not contain same number of entities " & _

                                  CountCheck(0) & " V.S. " & CountCheck(1) & vbCrLf

                                  MsgBox iReply

                                 

                                  Else

                                  check = True

                                  End If

                              End If

                              If I = 0 Or check = True Then

                              For N = 0 To SelectCount

                              Set vSegs(N, I) = vSeg(N)

                              Next N

                              End If

                              Next I

                             

                         

                          Wend

                       

                       

                      ' replace first array with second

                      For I = 0 To SelectCount

                      boolstatus = seg_replace(vSegs(I, 0), vSegs(I, 1))

                      If boolstatus = False Then

                      iReply = "ERROR" & vbCrLf & "Replace failed." & vbCrLf

                      MsgBox iReply

                      End

                      End If

                       

                       

                      Next I

                       

                       

                      End Sub

                       

                       

                       

                       

                      Function ChainSelector(I As Integer) As Variant

                       

                       

                      Dim swFrame As SldWorks.Frame

                      Dim C As Integer

                       

                       

                      Set SelMgr = swDoc.SelectionManager

                      Set SelData = SelMgr.CreateSelectData

                      Set swFrame = swApp.Frame ' set status bar variable

                       

                       

                      Segment_Check: 'return point if not sketch segment

                      'clear selection to start

                      swDoc.ClearSelection2 True

                      'boolstatus = swApp.SetSelectionFilter("SKETCHSEGMENT", True)

                              ' loop count message

                              If I = 0 Then

                              iReply = "Select member from chain to be replaced.    " & vbCrLf

                              MsgBox iReply

                              Else

                                  If I = 1 Then

                                  iReply = "Select member from static chain.    " & vbCrLf

                                  MsgBox iReply

                              Else

                              iReply = "Incorrect loop count." & vbCrLf & "ERROR" & vbCrLf

                              MsgBox iReply

                              Return

                              End If

                              End If

                             

                       

                       

                      ' Wait While/Wend

                          While SelMgr.GetSelectedObjectCount2(-1) < 1

                            DoEvents

                            swFrame.SetStatusBarText "Please select sketch segment." ' feed status message to status bar

                          Wend

                         

                              ' object type check

                       

                       

                              Dim selObject As Object

                              Dim selectByString As String

                              Dim objectTypeStr As String

                              Dim objectTypeInt As Long

                       

                       

                              ' Get the selection specification of the first selected object

                              Set selObject = SelMgr.GetSelectedObject6(1, -1)

                              SelMgr.GetSelectByIdSpecification selObject, selectByString, objectTypeStr, objectTypeInt

                              'Debug.Print "Name of selected feature: " & selectByString

                              'Debug.Print "Type of object: " & objectTypeStr

                              'Debug.Print "Type of object as defined in swSelectType_e: " & objectTypeInt

                              If Not objectTypeStr = "SKETCHSEGMENT" Then

                              GoTo Segment_Check

                              End If

                      Set swSeg = SelMgr.GetSelectedObject6(1, -1)

                       

                       

                       

                       

                      ' select chain

                      boolstatus = swSeg.SelectChain(False, Nothing)

                       

                       

                      ' Response to selection

                          iReply = SelMgr.GetSelectedObjectCount2(-1) & " item(s) selected.   " & vbCrLf

                          MsgBox iReply

                       

                       

                      ' Save selection to array

                      SelectCount = 0 ' zero counter

                      SelectCount = SelMgr.GetSelectedObjectCount2(-1) 'select regardless to mark

                      SelectCount = SelectCount - 1

                      ReDim vSeg(SelectCount)

                       

                       

                          For C = 0 To SelectCount

                              Set vSeg(C) = SelMgr.GetSelectedObject6(C + 1, -1)

                          Next

                         

                       

                       

                          ChainSelector = vSeg

                       

                       

                       

                       

                          swFrame.SetStatusBarText "Done" ' feed done message to status bar

                       

                       

                      End Function

                       

                       

                      Function seg_replace(seg1 As Variant, seg2 As Variant) As Boolean

                       

                       

                      swDoc.ClearSelection2 True

                              Dim selectByString As String

                              Dim objectTypeStr As String

                              Dim objectTypeInt As Long

                      SelMgr.GetSelectByIdSpecification seg1, selectByString, objectTypeStr, objectTypeInt

                              Debug.Print "Name of selected feature: " & selectByString

                              'Debug.Print "Type of object: " & objectTypeStr

                              'Debug.Print "Type of object as defined in swSelectType_e: " & objectTypeInt

                      SelMgr.GetSelectByIdSpecification seg2, selectByString, objectTypeStr, objectTypeInt

                              Debug.Print "Name of selected feature: " & selectByString

                              'Debug.Print "Type of object: " & objectTypeStr

                              'Debug.Print "Type of object as defined in swSelectType_e: " & objectTypeInt

                      boolstatus = swDoc.SketchManager.SketchReplace(False)

                      seg_replace = boolstatus

                      End Function

                      • Re: macro replace chain of splines
                        Arthur McRae

                        Finally got my Macro working, it is attached.  Figured out where I went wrong in a couple places.  Let me know if you end up using it or parts of it's code I'd be interested in seeing what you've done too.  I wanted to do some better forms but seeing as this is literally my second vba project I'm pleased with what I've accomplished.