Create Equation-driven Curve (C#)
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.
//----------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace ExampleCS.csproj
{
partial
class SolidWorksMacro
{
#region
Macro Variables
ModelDoc2
swModel;
ModelDocExtension
swExt;
SelectionMgr
swSelMgr;
bool
boolstatus;
string
y;
double
r1;
double
r2;
bool
rad;
double
ang;
double
xOff;
double
yOff;
bool
LockStart;
bool
LockEnd;
#endregion
public
void Main()
{
swModel
= swApp.ActiveDoc as ModelDoc2;
swExt
= swModel.Extension;
swSelMgr
= swModel.SelectionManager as
SelectionMgr;
boolstatus
= swExt.SelectByID2("Front
Plane", "PLANE", 0, 0, 0, false, 0, null, 0);
swModel.SketchManager.InsertSketch(true);
swModel.ClearSelection2(true);
SketchSegment
skSegment;
skSegment
= swModel.SketchManager.CreateEquationSpline2("sin(x)",
"", "", "0", "6.28", false, 0,
0.002, 0.002, false, false) as SketchSegment;
swModel.ViewZoomtofit2();
System.Diagnostics.Debugger.Break();
SketchSpline
skSpline;
skSpline
= skSegment as SketchSpline;
skSpline.GetEquationParameters(out y, out r1,
out r2, out rad, out ang, out xOff, out yOff, out LockStart, out 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)";
skSpline.SetEquationParameters(y, r1, r2, rad,
ang, xOff, yOff, LockStart,LockEnd);
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}