7 Replies Latest reply on Dec 18, 2017 12:29 PM by Matt Schmotzer

    Macro help for connecting specified points with line

    Matt Schmotzer

      Hello Solidworks Community,

       

      Problem Statment:

      I am looking for help adjusting the macro below to draw a line from points that I have created in a specifc order. Can you help me modify?

       

      Overview:

      I have over 150 parts that need to have lines sketched between points. The points are created as reference points in the same order, so the point numbers will be the same, just in a different XYZ location. I use the top centerline to create 8 evenly distributed points on the center line and then simply project the center line points onto each respective surface (seen in Figure 1)

      Track 1.jpg

      From here I go into 3d sketch and start connecting the points. First I connect the profile (seen in Figure 2)

      Track 2.jpg

      Once the profile is complete I then connect the cross bracing members. (Seen in Figure 3)

      Track 3.jpg

      Figure 3 is the desired result I am looking for from the Macro. Once I have the lines I can simply create weldments for the final product as seen in Figure 4. Please note that I have not trimmed any of the members and this is just to give you an idea of what I am trying to create.

      Track 4.jpg

       

      Since only the XYZ position of the point changes but not the point number I would like a macro to connect each point with a line but I want to be able to tell the macro which points to connect. For example if we look at Figure 1 & 2, I would like the following points connected...

      Point 17 to Point 16

      Point 33 to Point 41

      Point 41 to Point 25

       

      Point 18 to Point 15

      Point 34 to Point 42

      Point 42 to Point 26

       

      ...etc....

       

      That is what I am looking to do. I was able to find this previously written macro on the forum for it only connects point 1 to 2 then 3 to 4 then 4 to 5. In addition its only connecting sketch points when I am trying to connect reference points. Will I need to convert entities from reference points to points in a sketch? See the macro below...

       

      Prior Art:

      ' ******************************************************************************

      ' Macro that connects points within a 3dsketch by lines in a new 3Dsketch -By Bert De Spiegelaere

      ' ******************************************************************************

      Dim swApp As Object

      Dim swModel As Object

      Dim skSegment As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

      Dim sketchPointArray As Variant

      Dim pointCount As Integer

       

       

      Sub main()

       

      Set swApp = Application.SldWorks

       

      Set swModel = swApp.ActiveDoc

       

      'Get 3dsketch with points via SelectByID2

      boolstatus = swModel.Extension.SelectByID2("3DSketch with points", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

      swModel.EditSketch

      Set the3DSketch = swModel.GetActiveSketch2

      swModel.SketchManager.InsertSketch True

       

      'Get SketchPoints from within '3DSketch with points' .

      sketchPointArray = the3DSketch.GetSketchPoints2

       

      pointCount = UBound(sketchPointArray) + 1

       

      'create new 3DSketch to connect the points

      swModel.SketchManager.Insert3DSketch True

       

      ' For each SketchPoint in the "3DSketch with points"

       

      For i = 0 To (pointCount - 1)

       

      ' Here I pair up the sketchpoints

      If (i Mod 2) = 0 Then

       

      ' For each 2 Get the coordinates

      'Coords of point 1

      x1Value = sketchPointArray(i).X

       

      y1Value = sketchPointArray(i).Y

       

      z1Value = sketchPointArray(i).Z

      'Coords of point 2

      x2Value = sketchPointArray(i + 1).X

       

      y2Value = sketchPointArray(i + 1).Y

       

      z2Value = sketchPointArray(i + 1).Z

       

      'And create a line between them

       

      Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)

      'between coords we got from point 1 and the coords we got from point 2

       

      End If

       

      Next i

       

      'Close lin 3DSketch

      swModel.SketchManager.InsertSketch True

       

      End Sub

       

      ' ******************************************************************************

      ' ******************************************************************************

       

      Summary:

      Maybe there is a way to alter the above code to directly tell the macro which points to connect? I feel like this is a simple problem but I am a terrible programmer. I tried just hard inputting the point into the "i" or "i+1" field however I am using reference points and not sketch points. Once I convert the reference points into a sketch I have no Idea what order the points are in.

       

      Thank you so much for the help.

        • Re: Macro help for connecting specified points with line
          Deepak Gupta

          Instead can you have the points listed in an excel file and then draw lines specifying the end points value (x, y and z). This would save the time to put all those reference points. And one 3d sketch is fully made, fix all entities.

            • Re: Macro help for connecting specified points with line
              Matt Schmotzer

              Hi Deepak,

              Thank you for the quick response. The reference points were driven off the surfaces created. I do have a macro that could take all my created points and export them into excel. However once the points are in excel in X Y Z format I have no idea which coordinates relate to each point. Is there a macro out there that will take two sets of XYZ data and connect each with a line? I might be able to start there. Thanks again.

                • Re: Macro help for connecting specified points with line
                  Deepak Gupta

                  Matt Schmotzer wrote:

                   

                  Is there a macro out there that will take two sets of XYZ data and connect each with a line? I might be able to start there. Thanks again.

                  Not sure about a full macro but this line in your macro should be able to do that

                   

                  Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)

                   

                  So you should be able to traverse the coordinates in excel and create lines as needed.

                    • Re: Macro help for connecting specified points with line
                      Matt Schmotzer

                      Thanks Deepak,

                      Yea I was thinking about that too, however to use that line I would need to use "convert entities" to convert the reference points to points in a 3d sketch. Once I do that I lose the point numbering. Is there away to tell each point appart other than from their XYZ coordinate once I convert them from a reference point into a point on a 3d sketch? Thank you!

                        • Re: Macro help for connecting specified points with line
                          Deepak Gupta

                          Read the XYZ values from the excel and no need to create the reference points in SOLIDWORKS. For e.g.

                           

                          Set skSegment = swModel.SketchManager.CreateLine(CellA1.value, CellB1.value, CellC1.value, CellA2.value, CellB2.value, CellC2.value)

                            • Re: Macro help for connecting specified points with line
                              Matt Schmotzer

                              Understood, however I am not sure which points to connect. Which point data will row 1 in the excel file hold? I am exporting the point locations and the result is only XYZ. I no longer have control over which points go to which XYZ data.

                               

                              The reason I am using reference points is because I need to create the points in soldiworks based off the surfaces I created. the surfaces were created based off two guide lines. I do not have an excel sheet with all the points I need.

                              • Re: Macro help for connecting specified points with line
                                Matt Schmotzer

                                This is kind of what I am thinking....

                                 

                                 

                                 

                                 

                                Dim swApp As Object

                                 

                                 

                                Dim swModel As Object

                                 

                                 

                                Dim skSegment As Object

                                 

                                 

                                Dim boolstatus As Boolean

                                 

                                 

                                Dim longstatus As Long, longwarnings As Long

                                 

                                 

                                Dim sketchPointArray As Variant

                                 

                                 

                                Dim pointCount As Integer

                                Dim i As Integer

                                Dim j As Integer

                                 

                                 

                                Sub main()

                                 

                                 

                                 

                                 

                                 

                                Set swApp = Application.SldWorks

                                 

                                 

                                 

                                 

                                 

                                Set swModel = swApp.ActiveDoc

                                 

                                 

                                 

                                 

                                 

                                'Get 3dsketch with points via SelectByID2

                                 

                                 

                                boolstatus = swModel.Extension.SelectByID2("3DSketch with points", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

                                 

                                 

                                swModel.EditSketch

                                 

                                 

                                Set the3DSketch = swModel.GetActiveSketch2

                                 

                                 

                                swModel.SketchManager.InsertSketch True

                                 

                                 

                                 

                                 

                                 

                                'Get SketchPoints from within '3DSketch with points' .

                                 

                                 

                                sketchPointArray = the3DSketch.GetSketchPoints2

                                 

                                 

                                 

                                 

                                 

                                pointCount = UBound(sketchPointArray) + 1

                                 

                                 

                                 

                                 

                                 

                                'create new 3DSketch to connect the points

                                 

                                 

                                swModel.SketchManager.Insert3DSketch True

                                 

                                 

                                 

                                 

                                 

                                 

                                ' For each 2 Get the coordinates

                                i=17

                                 

                                 

                                j=16

                                 

                                 

                                x1Value = sketchPointArray(i).X

                                 

                                 

                                y1Value = sketchPointArray(i).Y

                                 

                                 

                                z1Value = sketchPointArray(i).Z

                                 

                                 

                                x2Value = sketchPointArray(j).X

                                 

                                 

                                y2Value = sketchPointArray(j).Y

                                 

                                 

                                z2Value = sketchPointArray(j).Z

                                 

                                 

                                Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)

                                 

                                 

                                 

                                 

                                ' For each 2 Get the coordinates

                                i=33

                                 

                                 

                                j=41

                                 

                                 

                                x1Value = sketchPointArray(i).X

                                 

                                 

                                y1Value = sketchPointArray(i).Y

                                 

                                 

                                z1Value = sketchPointArray(i).Z

                                 

                                 

                                x2Value = sketchPointArray(j).X

                                 

                                 

                                y2Value = sketchPointArray(j).Y

                                 

                                 

                                z2Value = sketchPointArray(j).Z

                                 

                                 

                                Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)

                                 

                                 

                                 

                                 

                                ' For each 2 Get the coordinates

                                i=41

                                 

                                 

                                j=25

                                 

                                 

                                x1Value = sketchPointArray(i).X

                                 

                                 

                                y1Value = sketchPointArray(i).Y

                                 

                                 

                                z1Value = sketchPointArray(i).Z

                                 

                                 

                                x2Value = sketchPointArray(j).X

                                 

                                 

                                y2Value = sketchPointArray(j).Y

                                 

                                 

                                z2Value = sketchPointArray(j).Z

                                 

                                 

                                Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)

                                 

                                 

                                 

                                 

                                 

                                 

                                 

                                'Close lin 3DSketch

                                 

                                 

                                swModel.SketchManager.InsertSketch True

                                 

                                 

                                 

                                 

                                 

                                End Sub