当前位置:   article > 正文

python二次开发Solidworks:读取样条曲线数据_solidworks python

solidworks python

目录

1、草图段对象

2、VBA代码分析

3、python代码实现


样条曲线(spline curve)是数学术语,是一种特殊的参数曲线,由一组控制点通过曲线拟合的方式生成。样条一词源于船舶建造中的一种临时性辅助支架,后来被引入计算机图形学中,成为一种广泛应用于计算机图形学、数控编程、工程建模等领域的曲线拟合方法。根据所使用的控制点数量和类型不同,样条曲线可以分为三阶样条曲线、二阶样条曲线和插值样条曲线等。本例实现python读取Solidworks的part文档中的草图的样条曲线。

1、草图段对象

在Solidworks中,草图段(SketchSegment)对象代表草图中的各个部分,如直线、圆弧、样条曲线和文字等。这些都属于草图段的细分分类。例如,一个矩形草图可以由多个草图段构成,每一条线段都是草图段的一个实例。

草图段对象在Solidworks内部具有特定的名称,例如Line8。这些名称可用于选择和操作草图段。通过SelectByID2方法,可以使用草图段的名称选择特定草图段。

2、VBA代码分析

在API help帮助中找到以下以下代码,并跑通:

  1. '----------------------------------------------------
  2. ' Preconditions:
  3. ' 1. Verify that the specified part template exists.
  4. ' 2. Open the Immediate window.
  5. '
  6. ' Postconditions:
  7. ' 1. Creates a sketch containing two splines.
  8. ' 2. Gets each spline's dimension, order, periodicity,
  9. ' control point, and knot point data.
  10. ' 3. Examine the Immediate window.
  11. '-----------------------------------------------------
  12. Option Explicit
  13. Dim swApp As SldWorks.SldWorks
  14. Dim swModel As SldWorks.ModelDoc2
  15. Dim swSelMgr As SldWorks.SelectionMgr
  16. Dim swSketchSegment As SldWorks.SketchSegment
  17. Dim swFeature As SldWorks.Feature
  18. Dim swSketch As SldWorks.Sketch
  19. Dim swSplineParamData As SldWorks.SplineParamData
  20. Dim swSketchMgr As SldWorks.SketchManager
  21. Dim points(11) As Double
  22. Dim pointArray As Variant
  23. Dim varCtrlPoints As Variant
  24. Dim varKnotPoints As Variant
  25. Dim status As Boolean
  26. Dim i As Integer
  27. Dim j As Integer
  28. Dim k As Integer
  29. Dim splineCount As Long
  30. Dim splinePointCount As Long
  31. Sub main()
  32. Set swApp = Application.SldWorks
  33. Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, 0, 0)
  34. 'Create a sketch with two splines
  35. 'First spline
  36. points(0) = -0.185955019567672
  37. points(1) = 4.16208582005027E-02
  38. points(2) = 0
  39. points(3) = -8.62492383138544E-02
  40. points(4) = 4.03922105323034E-02
  41. points(5) = 0
  42. points(6) = -6.72740896322921E-02
  43. points(7) = 5.40598971292923E-02
  44. points(8) = 0
  45. points(9) = -1.41436733240425E-02
  46. points(10) = -5.70437188125084E-03
  47. points(11) = 0
  48. pointArray = points
  49. Set swSketchMgr = swModel.SketchManager
  50. Set swSketchSegment = swSketchMgr.CreateSpline((pointArray))
  51. swModel.ClearSelection2 True
  52. 'Second spline
  53. points(0) = -8.38342193907238E-02
  54. points(1) = -3.80341664350112E-02
  55. points(2) = 0
  56. points(3) = -6.55490761158148E-02
  57. points(4) = -1.79490921124739E-02
  58. points(5) = 0
  59. points(6) = -1.79387030603664E-02
  60. points(7) = -6.81344637902441E-02
  61. points(8) = 0
  62. points(9) = 6.34819349185705E-02
  63. points(10) = -3.29692207162395E-02
  64. points(11) = 0
  65. pointArray = points
  66. Set swSketchSegment = swSketchMgr.CreateSpline((pointArray))
  67. swModel.ClearSelection2 True
  68. 'Sketch
  69. swSketchMgr.InsertSketch (True)
  70. 'Get each spline's dimension, order, periodicity, control point, and knot data
  71. status = swModel.Extension.SelectByID2("草图1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
  72. Set swSelMgr = swModel.SelectionManager
  73. Set swFeature = swSelMgr.GetSelectedObject6(1, -1)
  74. Set swSketch = swFeature.GetSpecificFeature2
  75. Debug.Print swFeature.Name
  76. Debug.Print ""
  77. splineCount = swSketch.GetSplineCount(splinePointCount)
  78. For i = 1 To splineCount
  79. Debug.Print "Spline " & i & ": "
  80. Set swSplineParamData = swSketch.GetSplineParams5(True, i - 1)
  81. Debug.Print " Dimension: " & swSplineParamData.Dimension
  82. Debug.Print " Order: " & swSplineParamData.Order
  83. Debug.Print " Periodicity: " & swSplineParamData.Periodic
  84. Debug.Print " Number of control points: " & swSplineParamData.ControlPointsCount
  85. status = swSplineParamData.GetControlPoints(varCtrlPoints)
  86. Debug.Print " Control points:"
  87. For j = 0 To UBound(varCtrlPoints)
  88. Debug.Print " " & varCtrlPoints(j)
  89. Next j
  90. Debug.Print " Number of knots: " & swSplineParamData.KnotPointsCount
  91. status = swSplineParamData.GetKnotPoints(varKnotPoints)
  92. Debug.Print " Knot points:"
  93. For k = 0 To UBound(varKnotPoints)
  94. Debug.Print " " & varKnotPoints(k)
  95. Next k
  96. Next i
  97. 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代码实现

  1. import win32com.client as win32
  2. import pythoncom
  3. import numpy as np
  4. def vtPoint(x, y, z):
  5. # 坐标点转化为浮点数
  6. return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))
  7. def vtObj(obj):
  8. # 转化为对象数组
  9. return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)
  10. def vtFloat(list):
  11. # 列表转化为浮点数
  12. return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)
  13. def vtInt(list):
  14. # 列表转化为整数
  15. return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)
  16. def vtVariant(list):
  17. # 列表转化为变体
  18. return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)
  19. swApp = win32.Dispatch('sldworks.application')
  20. swApp.Visible = True
  21. Nothing = win32.VARIANT(pythoncom.VT_DISPATCH, None)
  22. swModel = swApp.NewDocument(r"C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0,0,0)
  23. #Create a sketch with two splines
  24. #First spline
  25. points1=(-0.185955019567672,4.16208582005027E-02,0,-8.62492383138544E-02,4.03922105323034E-02,0,
  26. -6.72740896322921E-02,5.40598971292923E-02,0,-1.41436733240425E-02,-5.70437188125084E-03,0)
  27. pointArray = vtFloat(points1)
  28. swSketchMgr = swModel.SketchManager
  29. swSketchMgr.CreateSpline2(pointArray,1)
  30. swModel.ClearSelection2(True)
  31. #Second spline
  32. points2=(-8.38342193907238E-02,-3.80341664350112E-02,0,-6.55490761158148E-02,-1.79490921124739E-02,0,
  33. -1.79387030603664E-02,-6.81344637902441E-02,0,6.34819349185705E-02,-3.29692207162395E-02,0)
  34. pointArray = vtFloat(points2)
  35. swSketchMgr.CreateSpline2(pointArray,1)
  36. swModel.ClearSelection2(True)
  37. #Sketch
  38. swSketchMgr.InsertSketch(True)
  39. #Get each spline's dimension, order, periodicity, control point, and knot data
  40. status = swModel.Extension.SelectByID2("草图1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
  41. swSelMgr = swModel.SelectionManager
  42. swFeature = swSelMgr.GetSelectedObject6(1, -1)
  43. swSketch = swFeature.GetSpecificFeature2
  44. print(swFeature.Name)
  45. splinePointCount=win32.VARIANT(pythoncom.VT_BYREF|pythoncom.VT_I4, -1)
  46. splineCount = swSketch.GetSplineCount(splinePointCount)
  47. for i in range(1,splineCount+1):
  48. print("Spline ",splineCount,i)
  49. swSplineParamData = swSketch.GetSplineParams5(True, i - 1)
  50. print(" Dimension: ",swSplineParamData.Dimension)
  51. print(" Order: ",swSplineParamData.Order)
  52. print(" Periodicity: ",swSplineParamData.Periodic)
  53. print(" Number of control points: ",swSplineParamData.ControlPointsCount)
  54. varCtrlPoints = win32.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_VARIANT, -1)
  55. status = swSplineParamData.GetControlPoints(varCtrlPoints)
  56. CtrlPoints=np.array(varCtrlPoints.value).reshape(-1,3)
  57. print(" Control points:")
  58. for j in range(len(CtrlPoints)):
  59. print(CtrlPoints[0])
  60. print(" Number of knots: ",swSplineParamData.KnotPointsCount)
  61. varKnotPoints=win32.VARIANT(pythoncom.VT_BYREF|pythoncom.VT_VARIANT, -1)
  62. status = swSplineParamData.GetKnotPoints(varKnotPoints)
  63. KnotPoints = np.array(varKnotPoints.value).reshape(-1, 1)
  64. for j in range(len(KnotPoints)):
  65. 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)数据类型不匹配

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/980210
推荐阅读
相关标签
  

闽ICP备14008679号