Quantcast
Channel: Dotnet/C# – Xinyustudio
Viewing all articles
Browse latest Browse all 204

Create Splines using SolidWorks

$
0
0

SolidWorks generally has two sets of spline functions:

  1. The spline curve passes through the given points, i.e. spline fitting, (the curve S1 in the below figure)
  2. 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:

  1. IModelDoc2.CreateSpline
  2. SketchManager.CreateSplinesByEqnParams

The below data are used in the experiments:

int nOrder = 4;
int nPoints = 5;
double[] Pt = new double[3 * nPoints];
Pt[0] = -0.1; Pt[1] = -0.1; Pt[2] = 0;
Pt[3] = 0.1; Pt[4] = -0.1; Pt[5] = 0;
Pt[6] = 0.1; Pt[7] = 0.1; Pt[8] = 0;
Pt[9] = -0.1; Pt[10] = 0.1; Pt[11] = 0;
Pt[12] = -0.15; Pt[13] = 0; Pt[14] = 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];
Pt.CopyTo(Pt2, 0);
Pt2[Pt.Length] = -0.1;
Pt2[Pt.Length + 1] = -0.1;
Pt2[Pt.Length + 2] = 0;

modDoc.CreateSpline(Pt2);

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++)
pKnotsArray[i]=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[4];
PropertyArray[0] = 3; //Dimension, 2 is not OK!
PropertyArray[1] = nOrder; //Order
PropertyArray[2] = nPoints; //Number of control Points
PropertyArray[3] = 1; //Periodicity=true
#endregion
  • Merge all the double array into a single double []:

int n1 = PropertyArray.Length;
int n2 = pKnotsArray.Length;
int n3 = Pt.Length;

double [] Parameters=new double[n1+n2+n3];
PropertyArray.CopyTo(Parameters,0);
pKnotsArray.CopyTo(Parameters, n1);
Pt.CopyTo(Parameters, n1+n2);

modDoc.SketchManager.CreateSplinesByEqnParams(Parameters);

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 []>;
    Parameter.Add(PropertyArray);
    Parameter.Add(pKnotArray);
    Parameter.Add(Pt);
    modDoc.SketchManager.CreateSplinesByEqnParams(Parameters.ToArray());
  • 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.

Spline2.png Spline3.png


Posted in CAD, Dotnet/C#

Viewing all articles
Browse latest Browse all 204

Trending Articles