Table of Contents
1. Sketch segment object
2. VBA code analysis
3. python code implementation
Spline curve is a mathematical term. It is a special parametric curve that is generated by curve fitting from a set of control points. The word spline originated from a temporary auxiliary bracket in ship construction. It was later introduced into computer graphics and became a curve fitting method widely used in computer graphics, numerical control programming, engineering modeling and other fields. Depending on the number and type of control points used, spline curves can be divided into third-order spline curves, second-order spline curves, and interpolation spline curves. This example implements Python to read the spline curve of the sketch in the Solidworks part document.
1. Sketch segment object
In Solidworks, the Sketch Segment object represents each part of the sketch, such as lines, arcs, splines, text, etc. These are all subdivisions of sketch segments. For example, a rectangular sketch can be composed of multiple sketch segments, each line segment being an instance of the sketch segment.
Sketch segment objects have specific names inside Solidworks, such as Line8. These names can be used to select and manipulate sketch segments. The SelectByID2 method allows you to select a specific sketch segment using its name.
2. VBA code analysis
Find the following code in the API help and run it through:
'--------------------------------------------- ------- ' Preconditions: ' 1. Verify that the specified part template exists. ' 2. Open the Immediate window. ' ' Postconditions: ' 1. Creates a sketch containing two splines. ' 2. Gets each spline's dimension, order, periodicity, ' control point, and knot point data. ' 3. Examine the Immediate window. '------------------------------------------------ ----- Option Explicit Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swSketchSegment As SldWorks.SketchSegment Dim swFeature As SldWorks.Feature Dim swSketch As SldWorks.Sketch Dim swSplineParamData As SldWorks.SplineParamData Dim swSketchMgr As SldWorks.SketchManager Dim points(11) As Double Dim pointArray As Variant Dim varCtrlPoints As Variant Dim varKnotPoints As Variant Dim status As Boolean Dim i As Integer Dim j As Integer Dim k As Integer Dim splineCount As Long Dim splinePointCount As Long Sub main() Set swApp = Application.SldWorks Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, 0, 0) 'Create a sketch with two splines 'First spline points(0) = -0.185955019567672 points(1) = 4.16208582005027E-02 points(2) = 0 points(3) = -8.62492383138544E-02 points(4) = 4.03922105323034E-02 points(5) = 0 points(6) = -6.72740896322921E-02 points(7) = 5.40598971292923E-02 points(8) = 0 points(9) = -1.41436733240425E-02 points(10) = -5.70437188125084E-03 points(11) = 0 pointArray = points Set swSketchMgr = swModel.SketchManager Set swSketchSegment = swSketchMgr.CreateSpline((pointArray)) swModel.ClearSelection2 True 'Second spline points(0) = -8.38342193907238E-02 points(1) = -3.80341664350112E-02 points(2) = 0 points(3) = -6.55490761158148E-02 points(4) = -1.79490921124739E-02 points(5) = 0 points(6) = -1.79387030603664E-02 points(7) = -6.81344637902441E-02 points(8) = 0 points(9) = 6.34819349185705E-02 points(10) = -3.29692207162395E-02 points(11) = 0 pointArray = points Set swSketchSegment = swSketchMgr.CreateSpline((pointArray)) swModel.ClearSelection2 True 'Sketch swSketchMgr.InsertSketch (True) 'Get each spline's dimension, order, periodicity, control point, and knot data status = swModel.Extension.SelectByID2("Sketch 1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) Set swSelMgr = swModel.SelectionManager Set swFeature = swSelMgr.GetSelectedObject6(1, -1) Set swSketch = swFeature.GetSpecificFeature2 Debug.Print swFeature.Name Debug.Print "" splineCount = swSketch.GetSplineCount(splinePointCount) For i = 1 To splineCount Debug.Print "Spline " & amp; i & amp; ": " Set swSplineParamData = swSketch.GetSplineParams5(True, i - 1) Debug.Print " Dimension: " & swSplineParamData.Dimension Debug.Print " Order: " & swSplineParamData.Order Debug.Print " Periodicity: " & swSplineParamData.Periodic Debug.Print " Number of control points: " & swSplineParamData.ControlPointsCount status = swSplineParamData.GetControlPoints(varCtrlPoints) Debug.Print "Control points:" For j = 0 To UBound(varCtrlPoints) Debug.Print " " & varCtrlPoints(j) Next j Debug.Print " Number of knots: " & swSplineParamData.KnotPointsCount status = swSplineParamData.GetKnotPoints(varKnotPoints) Debug.Print " Knot points:" For k = 0 To UBound(varKnotPoints) Debug.Print " " & varKnotPoints(k) Next k Next i End Sub
Sketch 1
Spline 1:
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
-0.185955019567672
4.16208582005027E-02
0
-0.150380934953332
3.10398728957725E-02
0
-0.10646390756121
1.79774026593307E-02
0
-5.16578490138504E-02
7.31450269896099E-02
0
-3.05079969277205E-02
2.86910814467778E-02
0
-1.41436733240425E-02
-5.70437188125084E-03
0
Number of knots: 10
Knot points:
0
0
0
0
0.491042198542287
0.606202911975324
1
1
1
1
Spline 2:
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
-8.38342193907238E-02
-3.80341664350112E-02
0
-0.077690281088829
-2.89692122866611E-02
0
-5.58988151965229E-02
3.18258179599927E-03
0
-1.75895532053729E-02
-0.10684766264249
0
3.43192698312501E-02
-5.95444361085419E-02
0
6.34819349185705E-02
-3.29692207162395E-02
0
Number of knots: 10
Knot points:
0
0
0
0
0.146797917671912
0.520666331402203
1
1
1
1
Sketch 1
Spline 1:
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
-0.185955019567672
4.16208582005027E-02
0
-0.150380934953332
3.10398728957725E-02
0
-0.10646390756121
1.79774026593307E-02
0
-5.16578490138504E-02
7.31450269896099E-02
0
-3.05079969277205E-02
2.86910814467778E-02
0
-1.41436733240425E-02
-5.70437188125084E-03
0
Number of knots: 10
Knot points:
0
0
0
0
0.491042198542287
0.606202911975324
1
1
1
1
Spline 2:
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
-8.38342193907238E-02
-3.80341664350112E-02
0
-0.077690281088829
-2.89692122866611E-02
0
-5.58988151965229E-02
3.18258179599927E-03
0
-1.75895532053729E-02
-0.10684766264249
0
3.43192698312501E-02
-5.95444361085419E-02
0
6.34819349185705E-02
-3.29692207162395E-02
0
Number of knots: 10
Knot points:
0
0
0
0
0.146797917671912
0.520666331402203
1
1
1
1
Note: The code provided in the help document is likely to have problems, which mainly appear in the following two places:
The template path opened by swApp.NewDocument must be correct
swModel.Extension.SelectByID2 to correctly select sketch 1
3. python code implementation
import win32com.client as win32 import pythoncom import numpy as np def vtPoint(x, y, z): #Convert coordinate points to floating point numbers return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z)) def vtObj(obj): #Convert to object array return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj) def vtFloat(list): #Convert list to floating point number return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list) def vtInt(list): #Convert list to integer return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list) def vtVariant(list): #Convert list to variant return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list) swApp = win32.Dispatch('sldworks.application') swApp.Visible = True Nothing = win32.VARIANT(pythoncom.VT_DISPATCH, None) swModel = swApp.NewDocument(r"C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0,0,0) #Create a sketch with two splines #First spline points1=(-0.185955019567672,4.16208582005027E-02,0,-8.62492383138544E-02,4.03922105323034E-02,0, -6.72740896322921E-02,5.40598971292923E-02,0,-1.41436733240425E-02,-5.70437188125084E-03,0) pointArray = vtFloat(points1) swSketchMgr = swModel.SketchManager swSketchMgr.CreateSpline2(pointArray,1) swModel.ClearSelection2(True) #Second spline points2=(-8.38342193907238E-02,-3.80341664350112E-02,0,-6.55490761158148E-02,-1.79490921124739E-02,0, -1.79387030603664E-02,-6.81344637902441E-02,0,6.34819349185705E-02,-3.29692207162395E-02,0) pointArray = vtFloat(points2) swSketchMgr.CreateSpline2(pointArray,1) swModel.ClearSelection2(True) #Sketch swSketchMgr.InsertSketch(True) #Get each spline's dimension, order, periodicity, control point, and knot data status = swModel.Extension.SelectByID2("Sketch 1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) swSelMgr = swModel.SelectionManager swFeature = swSelMgr.GetSelectedObject6(1, -1) swSketch = swFeature.GetSpecificFeature2 print(swFeature.Name) splinePointCount=win32.VARIANT(pythoncom.VT_BYREF|pythoncom.VT_I4, -1) splineCount = swSketch.GetSplineCount(splinePointCount) for i in range(1,splineCount + 1): print("Spline ",splineCount,i) swSplineParamData = swSketch.GetSplineParams5(True, i - 1) print(" Dimension: ",swSplineParamData.Dimension) print(" Order: ",swSplineParamData.Order) print("Periodicity: ",swSplineParamData.Periodic) print("Number of control points: ",swSplineParamData.ControlPointsCount) varCtrlPoints = win32.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_VARIANT, -1) status = swSplineParamData.GetControlPoints(varCtrlPoints) CtrlPoints=np.array(varCtrlPoints.value).reshape(-1,3) print("Control points:") for j in range(len(CtrlPoints)): print(CtrlPoints[0]) print("Number of knots: ",swSplineParamData.KnotPointsCount) varKnotPoints=win32.VARIANT(pythoncom.VT_BYREF|pythoncom.VT_VARIANT, -1) status = swSplineParamData.GetKnotPoints(varKnotPoints) KnotPoints = np.array(varKnotPoints.value).reshape(-1, 1) for j in range(len(KnotPoints)): print(KnotPoints[j])
Sketch 1
Spline 2 1
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
[-0.18595502 0.04162086 0. ]
[-0.18595502 0.04162086 0. ]
[-0.18595502 0.04162086 0. ]
[-0.18595502 0.04162086 0. ]
[-0.18595502 0.04162086 0. ]
[-0.18595502 0.04162086 0. ]
Number of knots: 10
[0.]
[0.]
[0.]
[0.]
[0.4910422]
[0.60620291]
[1.]
[1.]
[1.]
[1.]
Spline 2 2
Dimension: 3
Order: 4
Periodicity: 0
Number of control points: 6
Control points:
[-0.08383422 -0.03803417 0. ]
[-0.08383422 -0.03803417 0. ]
[-0.08383422 -0.03803417 0. ]
[-0.08383422 -0.03803417 0. ]
[-0.08383422 -0.03803417 0. ]
[-0.08383422 -0.03803417 0. ]
Number of knots: 10
[0.]
[0.]
[0.]
[0.]
[0.14679792]
[0.52066633]
[1.]
[1.]
[1.]
[1.]
The difficulty with this part is:
Spline data type mismatch
status = swSplineParamData.GetControlPoints(varCtrlPoints) data type mismatch
status = swSplineParamData.GetKnotPoints(varKnotPoints) data type mismatch
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeDesktop application developmentTkinter383260 people are learning the system