Create Equation-driven Curve (VBA)
This example shows how to create and modify an equation-driven curve.
'---------------------------------------------------------------
' Preconditions: Part
document is open in SolidWorks.
'
' Postconditions:
' 1. A 2D sketch is created, and an equation spline of a
' sine
curve is created.
' 2. After
the break point, the curve is edited
' and a cosine
curve is created.
'----------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swExt As SldWorks.ModelDocExtension
Dim swSelMgr As SldWorks.SelectionMgr
Dim boolstatus As Boolean
Dim y As String
Dim r1 As Double
Dim r2 As Double
Dim rad As Boolean
Dim ang As Double
Dim xOff As Double, yOff As Double
Dim LockStart As Boolean, LockEnd As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swExt = swModel.Extension
Set swSelMgr = swModel.SelectionManager
boolstatus = swExt.SelectByID2("Front
Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
swModel.SketchManager.InsertSketch
True
swModel.ClearSelection2
True
Dim skSegment As SketchSegment
Set skSegment = swModel.SketchManager.CreateEquationSpline2("sin(x)",
"", "", "0", "6.28", false, 0,
0.002, 0.002, false, false)
swModel.ViewZoomtofit2
Stop
Dim skSpline As SketchSpline
Set skSpline = skSegment
Call skSpline.GetEquationParameters(y,
r1, r2, rad, ang, xOff, yOff, LockStart, LockEnd)
Debug.Print "y: " & y
Debug.Print "range start: " & r1
Debug.Print "range end: " & r2
Debug.Print "radian?: " & rad
Debug.Print "ang offset: " & ang
Debug.Print "x offset: " & xOff
Debug.Print "y offset: " & yOff
Debug.Print "Lock start: " & LockStart
Debug.Print "Lock end: " & LockEnd
Debug.Print "-----------------------------"
'Change spline to a cosine curve
y = "cos(x)"
Call skSpline.SetEquationParameters(y,
r1, r2, rad, ang, xOff, yOff, LockStart, LockEnd)
End Sub