Get Spline's Parameters Example (C#)
This example shows how to get a spline's parameters.
//-----------------------------------------
//
// Preconditions:
// (1) Part, assembly or drawing is open.
// (2) Sketch containing at least one spline is
// selected
in the FeatureManager design
// (not
in the graphics area).
// (3) Run the macro.
//
// Postconditions: Examine the results of running
// the
macro in the Immediate window.
//
//------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace GetSplineParams3SketchCSharp.csproj
{
partial
class SolidWorksMacro
{
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
public
class DoubleIntConv
{
//
An 8-byte double contains two 4-byte integers
[System.Runtime.InteropServices.FieldOffset(0)]
private
int m_Int1;
[System.Runtime.InteropServices.FieldOffset(4)]
private
int m_Int2;
[System.Runtime.InteropServices.FieldOffset(0)]
private
double m_Double;
private
DoubleIntConv(double dValue)
{
//C#
wants these initialized in the constructor
m_Int1
= 0;
m_Int2
= 0;
m_Double
= dValue;
}
private
DoubleIntConv(int iValue1, int iValue2)
{
//C#
wants these
initialized in the constructor
m_Double
= 0.0;
m_Int1
= iValue1;
m_Int2
= iValue2;
}
//
Use out parameters, so client code can pass in uninitialized variable
public
static void Unpack(double dIn, ref int iOut1, ref int iOut2)
{
DoubleIntConv
cv = default(DoubleIntConv);
cv
= new DoubleIntConv(dIn);
iOut1
= cv.m_Int1;
iOut2
= cv.m_Int2;
}
}
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
SelectionMgr
swSelMgr = default(SelectionMgr);
Feature
swFeat = default(Feature);
Sketch
swSketch = default(Sketch);
object[]
vSkSeg = null;
SketchSegment
swSkSeg = default(SketchSegment);
SketchSpline
swSkSpline = default(SketchSpline);
double[]
vSplineParam = null;
object[]
vSplinePt = null;
SketchPoint
swSkPt = default(SketchPoint);
double
nSplineParam0 = 0;
double
nSplineParam1 = 0;
int
arraySize = 0;
int
nDim = 0;
int
nOrder = 0;
int
nCtrlPoints = 0;
int
nPeriodic = 0;
int
nNumKnots = 0;
double
nStyleLayerParam0 = 0;
double
nStyleLayerParam1 = 0;
double
nStyleLayerParam2 = 0;
int
nColor = 0;
int
nLineStyle = 0;
int
nLineWidth = 0;
int
nLayer = 0;
int
nLayerOverride = 0;
int
nDummy = 0;
int
nIndex = 0;
int
nOffset = 0;
int
i = 0;
int
j = 0;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
swFeat
= (Feature)swSelMgr.GetSelectedObject6(1,
-1);
swSketch
= (Sketch)swFeat.GetSpecificFeature2();
Debug.Print(swFeat.Name);
Debug.Print("
Number
of splines: " + swSketch.GetSplineParamsCount3(true,
out arraySize));
Debug.Print("
Size
of spline parameters array: " + arraySize);
vSplineParam
= (double[])swSketch.GetSplineParams3(true);
nIndex
= 0;
while
(nIndex < vSplineParam.Length - 1)
{
nOffset
= nIndex;
//
Get spline data from first two packed elements
nSplineParam0
= (double)vSplineParam[nOffset + 0];
nIndex
= nIndex + 1;
nSplineParam1
= (double)vSplineParam[nOffset + 1];
nIndex
= nIndex + 1;
DoubleIntConv.Unpack(nSplineParam0,
ref nDim, ref nOrder);
DoubleIntConv.Unpack(nSplineParam1,
ref nCtrlPoints, ref nPeriodic);
if
(1 == nPeriodic)
{
nNumKnots
= nCtrlPoints + 1;
}
else
{
nNumKnots
= nCtrlPoints + nOrder;
}
Debug.Print("
Dim = " + nDim);
Debug.Print("
Order = " + nOrder);
Debug.Print("
CtrlPoints = " + nCtrlPoints);
Debug.Print("
Periodic = " + nPeriodic);
Debug.Print("
NumKnots = " + nNumKnots);
Debug.Print("");
//
Get control point data
//
Dimension is always 3 or 4
Debug.Assert(3
== nDim | 4 == nDim);
for
(i = 2 + nOffset; i <= 2 + nOffset + nCtrlPoints * nDim - 3; i += nDim)
{
Debug.Print("
CtrlPt[" + i + "] = (" + (double)vSplineParam[i + 0] *
1000.0 + ", " + (double)vSplineParam[i + 1] * 1000.0 + ",
" + (double)vSplineParam[i + 2] * 1000.0 + ") mm");
if
(4 == nDim)
{
Debug.Print("
Weight[" + i + "] = " + vSplineParam[i + 3]);
}
nIndex
= nIndex + nDim;
}
Debug.Print("");
for
(i = 2 + nOffset + nCtrlPoints * nDim; i <= 2 + nOffset + nCtrlPoints
* nDim + nNumKnots - 1; i++)
{
//
Knot weights must be nondescending
Debug.Print("
Knot[" + i + "] = " + vSplineParam[i]);
nIndex
= nIndex + 1;
}
Debug.Print("");
//
Get style from packed elements - only supported for drawings
nStyleLayerParam0
= (double)vSplineParam[2 + nOffset + nNumKnots + nCtrlPoints * nDim +
0];
nIndex
= nIndex + 0;
nStyleLayerParam1
= (double)vSplineParam[2 + nOffset + nNumKnots + nCtrlPoints * nDim +
1];
nIndex
= nIndex + 1;
nStyleLayerParam2
= (double)vSplineParam[2 + nOffset + nNumKnots + nCtrlPoints * nDim +
2];
nIndex
= nIndex + 1;
DoubleIntConv.Unpack(nStyleLayerParam0,
ref nColor, ref nLineStyle);
DoubleIntConv.Unpack(nStyleLayerParam1,
ref nLineWidth, ref nLayer);
DoubleIntConv.Unpack(nStyleLayerParam2,
ref nLayerOverride, ref nDummy);
Debug.Print("
Color = " + nColor);
Debug.Print("
LineStyle = " + nLineStyle);
Debug.Print("
LineWidth = " + nLineWidth);
Debug.Print("
Layer = " + nLayer);
Debug.Print("
LayerOverride = " + nLayerOverride);
Debug.Print("
---------------------------------");
nIndex
= nIndex + 1;
}
vSkSeg
= (object[])swSketch.GetSketchSegments();
for
(i = 0; i <= vSkSeg.Length - 1; i++)
{
swSkSeg
= (SketchSegment)vSkSeg[i];
if
((int)swSketchSegments_e.swSketchSPLINE == swSkSeg.GetType())
{
swSkSpline
= (SketchSpline)swSkSeg;
//
Spline passes through these points
//
first, and the last spline points are the same
//
as the first and last control points
vSplinePt
= (object[])swSkSpline.GetPoints2();
for
(j = 0; j <= vSplinePt.Length - 1; j++)
{
swSkPt
= (SketchPoint)vSplinePt[j];
Debug.Print("
SketchSplinePt[" + j + "] = (" + swSkPt.X
* 1000.0 + ", " + swSkPt.Y
* 1000.0 + ", " + swSkPt.Z
* 1000.0 + ") mm");
}
}
Debug.Print("");
}
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}