SolidWorks generally has two sets of spline functions:
- The spline curve passes through the given points, i.e. spline fitting, (the curve S1 in the below figure)
- The given points are the control points of the spline curve, i.e. spline interpolation (the curve S2 in the below figure)
Accordingly, there are two functions which does the jobs:
The below data are used in the experiments:
|int nOrder = 4;
int nPoints = 5;
|double Pt = new double[3 * nPoints];
Pt = -0.1; Pt = -0.1; Pt = 0;
Pt = 0.1; Pt = -0.1; Pt = 0;
Pt = 0.1; Pt = 0.1; Pt = 0;
Pt = -0.1; Pt = 0.1; Pt = 0;
Pt = -0.15; Pt = 0; Pt = 0;
For the 1st spline fitting problem, another point which is coincident to the 1st point should be appended, to indicate that this curve is closed:
|double Pt2 = new double[Pt.Length + 3];
Pt2[Pt.Length] = -0.1;
Pt2[Pt.Length + 1] = -0.1;
Pt2[Pt.Length + 2] = 0;
For the 2nd interpolation problem, the function call is a bit of cumbersome and the help manual is also less clear. After experimenting for a few times, here comes the solution.
- Prepare a kont vector, it could be uniform ones or whatever valid vector
|double pKnotsArray = new double[nOrder + nPoints];
for (int i = 0; i < pKnotsArray.Length; i++)
- According to the manual, prepare a property array, note that althought the parameters are integers, however
you must use a double array to hold the data, and later, this double array will be merged into one bigger double array:
|#region Property Array
double PropertyArray = new double;
PropertyArray = 3; //Dimension, 2 is not OK!
PropertyArray = nOrder; //Order
PropertyArray = nPoints; //Number of control Points
PropertyArray = 1; //Periodicity=true
- Merge all the double array into a single double :
int n1 = PropertyArray.Length;
double  Parameters=new double[n1+n2+n3];
There are several notes to be mentioned:
- Even though the sketch is on a 2D plane, experiments show that the Z coordinates cannot be omitted;
- Even the spline curve is closed, it is no need to add another point (coincident with the 1st control point) to the control point list!
Otherwise, a sharp corner will result, as shown in the figure below.
- I have also tried to use below codes, however in vein.
List<double > Parameters=new List<double >;
The other function with the same name but different signature does not work in C# COM based programming,
since there is still no way to marshal an "int " to a "ref int".
virtual SketchSegment ICreateSplineByEqnParams(ref int PropArray,
ref double KnotsArray,ref double CntrlPntCoordArray );
By adding another array W into the control point coordiate, the function CreateSplinesByEqnParams
can actually be used to model NURBS curve.