I've been trying to figure out this problem but can't seem to find where the error is. What this code does thus far is draw an array of vertical lines and trims them to the spline. I will then extract the endpoints of the vertical lines where they touch the spline for a future calculation. I'm using the same bit of code for when I found the endpoints of the spline, except that I'm now using it in a loop to step through each vertical line. When I print the intPoint variable, for some reason 10,000 is added to intPoint. Why is this? I'm using SolidWorks 2018. I've attached the test file and the macro.
The whole code as it stands now. The comment "Get end points of vertical lines only" is the loop where I am having issues. As mentioned before, I have checked to make sure my swSketchSeg is not an arc or spline. I changed the loop code a little bit but now I am getting and error on swEndPoint = swLine.GetEndPoint2 saying Object variable or With Block variable not set.
Sub main()
Debug.Print String(255, vbNewLine)
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSketchMgr As SldWorks.SketchManager
Dim swSketchSeg As SldWorks.SketchSegment
Dim swHorzSeg As SldWorks.SketchSegment
Dim swVertSeg As SldWorks.SketchSegment
Dim swSpline As SldWorks.SketchSegment
Dim swSketch As SldWorks.Sketch
Dim swLine As SketchLine
Dim swCurve As SldWorks.Curve
Dim swEndpoint As SketchPoint
Dim nStartParam As Double
Dim nEndParam As Double
Dim intPoint(7) As Double
Dim xStart As Double
Dim xEnd As Double
Dim n As Integer
Dim numlines As Long
Dim bIsClosed As Boolean
Dim bIsPeriodic As Boolean
Dim splineStart As Variant
Dim splineEnd As Variant
Dim vLines As Variant
Dim vSegs As Variant
Dim Seg As Variant
Dim seldata As SelectData
Dim nDummy As Long
Dim nStartSuccess As Long
Dim nEndSuccess As Long
Dim bRet As Boolean
Dim minEl As Double
Dim maxEl As Double
Dim divisions As Integer
Dim spacing As Double
Dim myFeature As SldWorks.Feature
Dim boolstatus As Boolean
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swSpline = swSelMgr.GetSelectedObject5(1)
Set swCurve = swSpline.GetCurve
Set swSketchMgr = swModel.SketchManager
Set swSketch = swModel.SketchManager.ActiveSketch
bRet = swCurve.GetEndParams(nStartParam, nEndParam, bIsClosed, bIsPeriodic)
Debug.Assert bRet
minEl = 10 / 1000
maxEl = 110 / 1000
divisions = 7
splineStart = swCurve.Evaluate(nStartParam)
splineEnd = swCurve.Evaluate(nEndParam)
spacing = (Abs(splineStart(0) - splineEnd(0))) / divisions
xStart = splineStart(0) + spacing / 2
xEnd = splineEnd(0)
'Create first centerline and pattern it
Set swVertSeg = swSketchMgr.CreateLine(xStart, minEl, 0#, xStart, maxEl, 0#)
swVertSeg.Color = vbRed
boolstatus = swSketchMgr.CreateLinearSketchStepAndRepeat(divisions, 1, spacing, 0.01, 0, 1.5707963267949, "", False, False, False, False, False)
Set swHorzSeg = swSketchMgr.CreateLine(xStart - spacing / 2, minEl, 0#, xEnd, minEl, 0#)
'Select boundary lines for trim inside operation
vSegs = swSketch.GetSketchSegments
Set swSketchSeg = vSegs(UBound(vSegs))
swSketchSeg.Select4 True, seldata
'Select all sketch segments for trim inside operation
vSegs = swSketch.GetSketchSegments
For Each Seg In vSegs
Set swSketchSeg = Seg
swSketchSeg.Select True
Next Seg
boolstatus = swSketchMgr.SketchTrim(swSketchTrimChoice_e.swSketchTrimInside, splineStart(0), minEl + 1, 0)
n = 0
vSegs = swSketch.GetSketchSegments
'Get end points of vertical lines only
For n = 0 to 6
Set swSketchSeg = vSegs(n)
If swSketchSeg.GetType <> swSketchSPLINE Then
'Check if current sketch segment is the horizontal line
If swApp.IsSame(swSketchSeg, swHorzSeg) = 0 Then
Set swLine = swSketchSeg
swEndpoint = swLine.GetEndPoint2
intPoint(n) = swEndpoint
n = n + 1
Else
'current sketch segment is the horizontal line
End If
End If
Next Seg
'Debug.Print intPoint(0) & " ," & intPoint(1) & " ," & intPoint(2) & " ," & intPoint(3) & " ," & intPoint(4) & " ," & intPoint(5) & " ," & intPoint(6)
Debug.Print ".........."
'Debug.Print String(255, vbNewLine)
'Debug.Print splineStart(0) & " ," & splineStart(1)
'Debug.Print splineEnd(0) & " ," & splineEnd(1)
End Sub
EDIT: I fixed the loop as
For n = 0 To UBound(vSegs)
Set swSketchSeg = vSegs(n)
If swSketchSeg.GetType <> swSketchSPLINE Then
'Check if current sketch segment is the horizontal line
If swApp.IsSame(swSketchSeg, swHorzSeg) = 0 Then 'current sketch segment is a vertical line
Set swLine = swSketchSeg
Set swEndpoint = swLine.GetEndPoint2
intPoint(n) = swEndpoint.Y
Debug.Print n & " ," & intPoint(n)
Else
'current sketch segment is the horizontal line
End If
End If
Next n