SolidWorks
Get BCurve Spline Points Example (VB.NET)
'---------------------------------------------------------------------------------------------------------
'
This macro demonstrates how to get knots, control points, and other parameters
for a selected spline
'
(or any selected curve).
'
'
1. Open
a part document that contains a sketch with a spline curve
'
(e.g.,
<install_dir>\samples\tutorial\molds\telephone.sldprt).
'
2. Select
the spline curve (select Shape Feature1 in the FeatureManager design tree
of telephone.sldprt and then select the top blue spline curve of telephone.sldprt).
'
3. Open
an immediate window to view runtime messages.
'
4. Run
this macro.
'
5. View
the following information about the selected spline in the immediate window:
'
*
dimension
'
*
order or degree
'
*
number of control points
'
*
periodicity
'
*
number of knots:
'
if
periodicity = 1,
'
number
of control points + 1
'
else
'
number
of control points + order
'
*
each knot's double value between 0 and 1, inclusive
'
*
coordinates of each control point for the given dimension:
'
1-D:
X
'
2-D:
X, Y
'
3-D:
X, Y, Z
'
4-D:
X, Y, Z,
W (where W is the weight of the control point)
'---------------------------------------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
'
Extract a long value out of a packed double at a specific index
Function
ExtractFields(ByVal dValue As Double, ByVal index As Integer) As Integer
Dim
byteArray() As Byte = BitConverter.GetBytes(dValue)
Dim
integerValue As Long = BitConverter.ToInt32(byteArray, index)
Return
integerValue
End
Function
Sub
Main()
Dim
i As Integer
Dim
j As Integer
Dim
swApp As Object
Dim
Part As Object
swApp
= CreateObject("SldWorks.Application")
Part
= swApp.ActiveDoc
Dim
swSelectMgr As Object
swSelectMgr
= Part.SelectionManager
Dim
swSketchSeg As Object
swSketchSeg
= swSelectMgr.GetSelectedObject5(1)
Dim
swCurveIn As Object
swCurveIn
= swSketchSeg.GetCurve
Dim
varSplineParams As Object
'
Get non-cubic, rational, nonperiodic, closed spline parameters
varSplineParams
= swCurveIn.GetBCurveParams4(False, False,
True, True)
Dim
iNumKnots As Integer
Dim
iNumCtrlPts As Integer
Dim
iDimension As Integer
Dim
iOrder As Integer
Dim
iPeriodicity As Integer
Dim
iSplineArraySize As Integer
Dim
dTmpValue1 As Double, dTmpValue2 As Double
Dim
iSplineIndex As Integer
Dim
iVarIndex As Integer
'
Obtain the curve's dimension and its order (degree) from the first packed
double
dTmpValue1
= varSplineParams(0)
iDimension
= ExtractFields(dTmpValue1, 0)
iOrder
= ExtractFields(dTmpValue1, 4)
'
Obtain the curve's number of control points and its periodicity from the
second packed double
dTmpValue2
= varSplineParams(1)
iNumCtrlPts
= ExtractFields(dTmpValue2, 0)
iPeriodicity
= ExtractFields(dTmpValue2, 4)
'
Calculate the number of knots according to periodicity and order (degree)
of the spline
If
(iPeriodicity = 1) Then
iNumKnots
= iNumCtrlPts + 1
Else
iNumKnots
= iNumCtrlPts + iOrder
End
If
'Calculate
the size of the spline parameters array
iSplineArraySize
= 2 + iNumKnots + (iDimension * iNumCtrlPts)
Dim
dSplineParams() As Double
ReDim
dSplineParams(iSplineArraySize - 1)
'
Populate spline parameters double array using data returned from GetBCurveParams4
'
First packed double contains the dimension and order
dSplineParams(0)
= dTmpValue1
'
Second packed double contains the number of control points and the periodicity
dSplineParams(1)
= dTmpValue2
'
Process remaining double values in varSplineParams (knots and control
points)
iSplineIndex
= 2
iVarIndex
= 2
Debug.Print("*******************************")
Debug.Print("GetCurveParam4
")
Debug.Print("*******************************")
Debug.Print("Dimension:
" & iDimension)
Debug.Print("Order
or degree: " & iOrder)
Debug.Print("Number
of control points: " & iNumCtrlPts)
Debug.Print("Periodicity:
" & iPeriodicity)
Debug.Print("Number
of knots: " & iNumKnots)
'
Knots
Debug.Print("*******************************")
Debug.Print("Knots")
Debug.Print("--------------")
For
i = 0 To (iNumKnots - 1)
'
Populate spline parameters array with knot values
dSplineParams(iSplineIndex)
= varSplineParams(iVarIndex)
'
Print double value between 0 and 1, inclusive, for each knot in the curve
Debug.Print(varSplineParams(iVarIndex))
iSplineIndex
= iSplineIndex + 1
iVarIndex
= iVarIndex + 1
Next
i
'
Control points
Debug.Print("*******************************")
Debug.Print("Control
points")
For
i = 0 To (iNumCtrlPts - 1)
Debug.Print("--------------")
For
j = 1 To iDimension
'
Populate spline parameters array with control points
dSplineParams(iSplineIndex)
= varSplineParams(iVarIndex)
'
Print coordinates of each control point
If
(j = 1) Then
Debug.Print("X:"
+ Str(varSplineParams(iVarIndex)))
ElseIf
(j = 2) Then
Debug.Print("Y:"
+ Str(varSplineParams(iVarIndex)))
ElseIf
(j = 3) Then
Debug.Print("Z:"
+ Str(varSplineParams(iVarIndex)))
ElseIf
(j = 4) Then
Debug.Print("W:"
+ Str(varSplineParams(iVarIndex)))
End
If
iSplineIndex
= iSplineIndex + 1
iVarIndex
= iVarIndex + 1
Next
j
Next
i
Dim
varSplinePtParams As Object
varSplinePtParams
= dSplineParams
Dim
varSplinePts As Object
'
Pass spline parameters as variant array to GetSplinePts(),
'
which returns a variant array of doubles for coordinates of each spline
point:
'
[ x1, y1, z1, x2, y2, z2,....]
varSplinePts
= swCurveIn.GetSplinePts((varSplinePtParams))
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class