I'm trying to iterate what (I thought) was a fairly simply process that needs to be repeated 100+ times.
The general goal is to first, sketch lines between a set of points and the midpoint. Then I need to create a reference plane, coincident to each point and perpendicular to each line.
Here is the code so far: For the record, before starting the code, I import points using an excel to point cloud macro that I someone else created.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
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 strSketchName As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Get 3dsketch with points via SelectByID2
boolstatus = swModel.Extension.SelectByID2("3DSketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Set the3Dsketch = swModel.GetActiveSketch2
'Get SketchPoints from within '3DSketch with points'
sketchPointArray = the3Dsketch.GetSketchPoints2
pointCount = UBound(sketchPointArray)
'create new 3DSketch to connect the points
' For each SketchPoint in the "3DSketch with points"
'Coords of Midpoint (pt 1)
x1Value = sketchPointArray(0).X
y1Value = sketchPointArray(0).Y
z1Value = sketchPointArray(0).Z
For i = 1 To pointCount
'Coords of Point on Head Cap (pt 2)
x2Value = sketchPointArray(i).X
y2Value = sketchPointArray(i).Y
z2Value = sketchPointArray(i).Z
'And create a line between them
Set skSegment = swModel.SketchManager.CreateLine(x1Value, y1Value, z1Value, x2Value, y2Value, z2Value)
'Make planes in the same code
For i = 1 To 2
strSketchName = "Sketch" & i + 1
boolstatus = swModel.Extension.SelectByID2("", "EXTSKETCHPOINT", sketchPointArray(i).X, sketchPointArray(i).Y, sketchPointArray(i).Z, False, 0, Nothing, 0)
boolstatus = swModel.Extension.SelectByID2(strSketchName, "SKETCH", 0, 0, 0, True, 1, Nothing, 1)
Dim myRefPlane As Object
Set myRefPlane = swModel.FeatureManager.InsertRefPlane(4, 0, 2, 0, 0, 0)
The code works perfectly until the part I bolded - in which the program doesn't give me any error messages nor does it even attempt to make a reference plane, it simply crashes all of solidworks entirely.
I cut the program down to only act between a few points, and it still crashes.
Does anyone know what I can do to fix this problem? Is there any other way I can iterate through different points/lines and create several reference planes?