Get and Set Spline Handles Example (VB.NET)
This example shows how to get and set the properties of spline handles in a 2D spline.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Verify that the specified document template exists.
' 2. Open an Immediate window.
'
' Postconditions:
' 1. Creates Sketch1 in the graphics area and FeatureManager design tree.
' 2. Modifies some of the spline handles of Sketch1.
' 3. Inspect the Immediate window.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swModelDocExt As ModelDocExtension
Dim swSpline As SketchSpline
Dim skSegment As SketchSegment
Dim swSplineHandle As Object
Dim boolstatus As Boolean
Dim i As Integer
Sub main()
swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2015\templates\Part.prtdot", 0, 0, 0)
swModel = swApp.ActiveDoc
swSelMgr = swModel.SelectionManager
swModelDocExt = swModel.Extension
Dim pointArray As Object
Dim points() As Double = New Double(17) {}
points(0) = -0.0725658847190971
points(1) = -0.0284590725570979
points(2) = 0
points(3) = -0.0437940929359115
points(4) = 0.0215374317625674
points(5) = 0
points(6) = -0.0245140262770747
points(7) = -0.026920232075895
points(8) = 0
points(9) = 0.0273938454967038
points(10) = -0.0414386885537397
points(11) = 0
points(12) = 0.0413348167730874
points(13) = 0.0386761654855832
points(14) = 0
points(15) = 0.101251331620574
points(16) = 0.0438481259864147
points(17) = 0
pointArray = points
skSegment = swModel.SketchManager.CreateSpline((pointArray))
swModel.SketchManager.InsertSketch(True)
boolstatus = swModelDocExt.SelectByID2("Spline1@Sketch1", "EXTSKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
swSpline = swSelMgr.GetSelectedObject6(1, 0)
' Get the handles on the spline
swSplineHandle = swSpline.GetSplineHandles()
For i = 0 To UBound(swSplineHandle)
Debug.Print("Spline handle " & swSplineHandle(i).SplinePointNumber)
Debug.Print(" X: " & swSplineHandle(i).X * 1000)
Debug.Print(" Y: " & swSplineHandle(i).Y * 1000)
Debug.Print(" Z: " & swSplineHandle(i).Z * 1000)
If (i = 0) Then
swSplineHandle(i).X = -62.33246528 / 1000
swSplineHandle(i).Y = -14.71944444 / 1000
End If
Debug.Print(" Tangent magnitude: " & swSplineHandle(i).TangentMagnitude(swTangentMagnitudeDirection_e.swTangentMagnitudeDirection1))
Debug.Print(" Tangent radial direction: " & swSplineHandle(i).TangentRadialDirection)
If (i = 2) Then
swSplineHandle(i).TangentMagnitude(swTangentMagnitudeDirection_e.swTangentMagnitudeDirection1) = (swSplineHandle(i).TangentMagnitude(swTangentMagnitudeDirection_e.swTangentMagnitudeDirection1)) + 0.02
swSplineHandle(i).TangentRadialDirection = swSplineHandle(i).TangentRadialDirection + 0.5
End If
Debug.Print(" Curvature: " & swSplineHandle(i).Curvature)
Debug.Print(" Radius of curvature: " & swSplineHandle(i).RadiusOfCurvature)
If (i = 3) Then
swSplineHandle(i).RadiusOfCurvature = swSplineHandle(i).RadiusOfCurvature / 2
End If
If (i = 3) Then
Debug.Print(" Tangent driving? " & swSplineHandle(i).TangentDriving)
If (swSplineHandle(i).TangentDriving) Then
swSplineHandle(i).TangentDriving = False
Else
swSplineHandle(i).TangentDriving = True
End If
swSplineHandle(i).Reset()
End If
Next
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class