SolidWorks
Get BCurve Spline Points Example (VBA)
'---------------------------------------------------------------------------------------------------------
' 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).
' 3. Open
an immediate window to view 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)
'---------------------------------------------------------------------------------------------------------
' Define a data type for a packed double
Type DoubleRec
dValue
As Double
End Type
' Define a data type that consists of two long values
Type Int2Rec
iLower
As Long
iUpper
As Long
End Type
' Extract two long values out of a packed double value
Function ExtractFields(dValue As Double, iLower As Integer,
iUpper As Integer)
Dim
dr As DoubleRec
Dim
i2r As Int2Rec
'
Assign the double value to a DoubleRec data type
dr.dValue
= dValue
'
Convert the DoubleRec to an Int2Rec data type
LSet
i2r = dr
'
Extract two long values from the Int2Rec data type
iLower
= i2r.iLower
iUpper
= i2r.iUpper
End Function
Sub main()
Dim
swApp As Object
Dim
Part As Object
Set
swApp = CreateObject("SldWorks.Application")
Set
Part = swApp.ActiveDoc
Dim
swSelectMgr As Object
Set
swSelectMgr = Part.SelectionManager
Dim
swSketchSeg As Object
Set
swSketchSeg = swSelectMgr.GetSelectedObject5(1)
Dim
swCurveIn As Object
Set
swCurveIn = swSketchSeg.GetCurve
Dim
varSplineParams As Variant
'
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
dTmpValue1
= varSplineParams(0)
'
Obtain the curve's dimension and its order (degree) from the first packed
double
ExtractFields
dTmpValue1, iDimension, iOrder
dTmpValue2
= varSplineParams(1)
'
Obtain the curve's number of control points and its periodicity from the
second packed double
ExtractFields
dTmpValue2, iNumCtrlPts, iPeriodicity
'
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 Variant
varSplinePtParams
= dSplineParams
Dim
varSplinePts As Variant
'
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