Get Spline's Parameters Example (VB.NET)
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 tree (not in the
' graphics
area).
' (3)
Run the macro.
'
' Postconditions: Examine the results of running the macro
in
' Immediate
window.
'
'------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
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 m_Int1 As Integer
<System.Runtime.InteropServices.FieldOffset(4)>
Private m_Int2 As Integer
<System.Runtime.InteropServices.FieldOffset(0)>
Private m_Double As Double
Private
Sub New(ByVal dValue As Double)
'VB.NET
wants these initialized in the constructor
m_Int1
= 0
m_Int2
= 0
m_Double
= dValue
End
Sub
Private
Sub New(ByVal iValue1 As Integer, ByVal iValue2 As Integer)
'VB.NET
wants these initialized in the constructor
m_Double
= 0.0
m_Int1
= iValue1
m_Int2
= iValue2
End
Sub
'
Use out parameters, so client code can pass in uninitialized variable
Public
Shared Sub Unpack(ByVal dIn As Double, ByRef iOut1 As Integer, ByRef iOut2
As Integer)
Dim
cv As DoubleIntConv
cv
= New DoubleIntConv(dIn)
iOut1
= cv.m_Int1
iOut2
= cv.m_Int2
End
Sub
End
Class
Public
Sub Main()
Dim
swModel As ModelDoc2
Dim
swSelMgr As SelectionMgr
Dim
swFeat As Feature
Dim
swSketch As Sketch
Dim
vSkSeg As Object
Dim
swSkSeg As SketchSegment
Dim
swSkSpline As SketchSpline
Dim
vSplineParam As Object
Dim
vSplinePt As Object
Dim
swSkPt As SketchPoint
Dim
nSplineParam0 As Double
Dim
nSplineParam1 As Double
Dim
arraySize As Integer
Dim
nDim As Integer
Dim
nOrder As Integer
Dim
nCtrlPoints As Integer
Dim
nPeriodic As Integer
Dim
nNumKnots As Integer
Dim
nStyleLayerParam0 As Double
Dim
nStyleLayerParam1 As Double
Dim
nStyleLayerParam2 As Double
Dim
nColor As Integer
Dim
nLineStyle As Integer
Dim
nLineWidth As Integer
Dim
nLayer As Integer
Dim
nLayerOverride As Integer
Dim
nDummy As Integer
Dim
nIndex As Integer
Dim
nOffset As Integer
Dim
i As Integer
Dim
j As Integer
swModel
= swApp.ActiveDoc
swSelMgr
= swModel.SelectionManager
swFeat
= swSelMgr.GetSelectedObject6(1,
-1)
swSketch
= swFeat.GetSpecificFeature2
Debug.Print(swFeat.Name)
Debug.Print("
Number
of splines: " & swSketch.GetSplineParamsCount3(True,
arraySize))
Debug.Print("
Size
of spline parameters array: " & arraySize)
vSplineParam
= swSketch.GetSplineParams3(True)
nIndex
= 0
Do
While nIndex < UBound(vSplineParam)
nOffset
= nIndex
'
Get spline data from first two packed elements
nSplineParam0
= vSplineParam(nOffset + 0) : nIndex = nIndex + 1
nSplineParam1
= vSplineParam(nOffset + 1) : nIndex = nIndex + 1
DoubleIntConv.Unpack(nSplineParam0,
nDim, nOrder)
DoubleIntConv.Unpack(nSplineParam1,
nCtrlPoints, nPeriodic)
If
1 = nPeriodic Then
nNumKnots
= nCtrlPoints + 1
Else
nNumKnots
= nCtrlPoints + nOrder
End
If
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 Or 4 = nDim)
For
i = 2 + nOffset To 2 + nOffset + nCtrlPoints * nDim - 3 Step nDim
Debug.Print("
CtrlPt["
& i & "] = (" & vSplineParam(i + 0) * 1000.0# &
", " & vSplineParam(i + 1) * 1000.0# & ", "
& vSplineParam(i + 2) * 1000.0# & ") mm")
If
4 = nDim Then
Debug.Print("
Weight["
& i & "] = " & vSplineParam(i + 3))
End
If
nIndex
= nIndex + nDim
Next
i
Debug.Print("")
For
i = 2 + nOffset + nCtrlPoints * nDim To 2 + nOffset + nCtrlPoints * nDim
+ nNumKnots - 1
'
Knot weights must be nondescending
Debug.Print("
Knot["
& i & "] = " & vSplineParam(i))
nIndex
= nIndex + 1
Next
i
Debug.Print("")
'
Get style from packed elements - only supported for drawings
nStyleLayerParam0
= vSplineParam(2 + nOffset + nNumKnots + nCtrlPoints * nDim + 0) : nIndex
= nIndex + 0
nStyleLayerParam1
= vSplineParam(2 + nOffset + nNumKnots + nCtrlPoints * nDim + 1) : nIndex
= nIndex + 1
nStyleLayerParam2
= vSplineParam(2 + nOffset + nNumKnots + nCtrlPoints * nDim + 2) : nIndex
= nIndex + 1
DoubleIntConv.Unpack(nStyleLayerParam0,
nColor, nLineStyle)
DoubleIntConv.Unpack(nStyleLayerParam1,
nLineWidth, nLayer)
DoubleIntConv.Unpack(nStyleLayerParam2,
nLayerOverride, 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
Loop
vSkSeg
= swSketch.GetSketchSegments
For
i = 0 To UBound(vSkSeg)
swSkSeg
= vSkSeg(i)
If
swSketchSegments_e.swSketchSPLINE = swSkSeg.GetType
Then
swSkSpline
= swSkSeg
'
Spline passes through these points
'
first, and the last spline points are the same
'
as the first and last control points
vSplinePt
= swSkSpline.GetPoints2
For
j = 0 To UBound(vSplinePt)
swSkPt
= vSplinePt(j)
Debug.Print("
SketchSplinePt["
& j & "] = (" & swSkPt.X
* 1000.0# & ", " & swSkPt.Y
* 1000.0# & ", " & swSkPt.Z
* 1000.0# & ") mm")
Next
j
End
If
Debug.Print("")
Next
i
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As SldWorks
End Class