目录
1、草图段对象
2、VBA代码分析
3、python代码实现
样条曲线(spline curve)是数学术语,是一种特殊的参数曲线,由一组控制点通过曲线拟合的方式生成。样条一词源于船舶建造中的一种临时性辅助支架,后来被引入计算机图形学中,成为一种广泛应用于计算机图形学、数控编程、工程建模等领域的曲线拟合方法。根据所使用的控制点数量和类型不同,样条曲线可以分为三阶样条曲线、二阶样条曲线和插值样条曲线等。本例实现python读取Solidworks的part文档中的草图的样条曲线。
1、草图段对象
在Solidworks中,草图段(SketchSegment)对象代表草图中的各个部分,如直线、圆弧、样条曲线和文字等。这些都属于草图段的细分分类。例如,一个矩形草图可以由多个草图段构成,每一条线段都是草图段的一个实例。
草图段对象在Solidworks内部具有特定的名称,例如Line8。这些名称可用于选择和操作草图段。通过SelectByID2方法,可以使用草图段的名称选择特定草图段。
2、VBA代码分析
在API help帮助中找到以下以下代码,并跑通:
'----------------------------------------------------
' 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("草图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 " & i & ": "
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
草图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
草图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
注意:帮助文档提供的代码大概率有问题,主要出现在以下两个地方,
swApp.NewDocument打开的模板路径要正确
swModel.Extension.SelectByID2要正确选中草图1
3、python代码实现
import win32com.client as win32
import pythoncom
import numpy as np
def vtPoint(x, y, z):
# 坐标点转化为浮点数
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))
def vtObj(obj):
# 转化为对象数组
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)
def vtFloat(list):
# 列表转化为浮点数
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)
def vtInt(list):
# 列表转化为整数
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)
def vtVariant(list):
# 列表转化为变体
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("草图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])
草图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.]
这部分的难点在于:
样条曲线数据类型不匹配
status = swSplineParamData.GetControlPoints(varCtrlPoints)数据类型不匹配
status = swSplineParamData.GetKnotPoints(varKnotPoints)数据类型不匹配