iCAx开思网

标题: 圓周分布鉆孔-宏 (已解決) [打印本页]

作者: ryouss    时间: 2018-5-24 17:36
标题: 圓周分布鉆孔-宏 (已解決)
本帖最后由 ryouss 于 2018-5-25 09:18 编辑

如圖所示,鉆孔直徑8時原點有孔,但改為鉆孔直徑5時原點就沒有孔,
有興趣者幫看一下,可否有解決方法!
宏是在2012版編程的.

附SWP文件  [attach]1257903[/attach]


原點沒孔
[attach]1257901[/attach]
原點有孔
[attach]1257902[/attach]

  1. ' *************************************************************
  2. ' macro recorded on 05/20/18 by scliang
  3. ' 功能:圓周分佈鉆孔,本範例因是用除料拉伸,所以鉆孔是平底.
  4. ' 操作: 1.在零件先選取要鉆孔之平面.
  5. '       2.執行 "main" .
  6. '       3.X座標取正數,若是負數可能會出錯.
  7. '       4.首圈半徑近似於相鄰兩孔之中心(弧長)距離.
  8. '
  9. ' *************************************************************

  10. Dim X1 As Double 'TextBox1
  11. Dim Y1 As Double 'TextBox2
  12. Dim Drill_Diameter As Double 'TextBox3
  13. Dim Start_Circle_radius As Double 'TextBox4
  14. Dim Drill_depth As Double 'TextBox5
  15. Dim Circle_number  As Integer 'TextBox6
  16. Dim X2 As Double
  17. Dim BX1 As Double
  18. Dim BX2 As Double
  19. Dim pi As Double
  20. Dim Circle_radius As Double

  21. Sub main()
  22. UserForm1.Show
  23. End Sub

  24. Sub Draw_()
  25. With UserForm1
  26. '判定資料是否沒打入
  27. If .TextBox1.Value = "" Or .TextBox2.Value = "" Or .TextBox3.Value = "" Or .TextBox4.Value = "" Or .TextBox5.Value = "" Or .TextBox6.Value = "" Then
  28.       MsgBox ("Enter empty")
  29.       Exit Sub
  30. End If
  31. '判定資料是否是輸入錯誤(起始圓半徑限制不能小於等於鉆孔直徑)
  32. Drill_Diameter = .TextBox3.Value / 1000
  33. Start_Circle_radius = .TextBox4.Value / 1000
  34. If Drill_Diameter >= Start_Circle_radius Then
  35.       MsgBox ("Data error")
  36.       Exit Sub
  37. End If
  38. Set swApp = Application.SldWorks
  39. Set Part = swApp.ActiveDoc
  40. Set swModel = swApp.ActiveDoc
  41. Set swSketchMgr = swModel.SketchManager
  42. Part.SketchManager.InsertSketch True '依據選取面插入草圖
  43. '中心圓之座標及作圖
  44. X1 = .TextBox1.Value / 1000
  45. Y1 = .TextBox2.Value / 1000
  46. X2 = X1 + Drill_Diameter / 2
  47. Set swSketchSegment = swSketchMgr.CreateCircle(X1, Y1, 0#, X2, Y1, 0#)
  48. '圓周分佈之鉆孔
  49. pi = Atn(1) * 4
  50. Circle_number = .TextBox6.Value
  51. Drill_depth = .TextBox5.Value / 1000 '鉆孔深
  52. For i = 1 To Circle_number
  53.       Circle_radius = i * Start_Circle_radius '分佈圓周之半徑
  54.       Copy_Number = Int(2 * Circle_radius * pi / Start_Circle_radius + 0.5) '分佈圓周之鉆孔數
  55. '分佈圓之基圓作圖
  56.       BX1 = X1 + Circle_radius
  57.       BX2 = BX1 + Drill_Diameter / 2
  58.       Set swSketchSegment = swSketchMgr.CreateCircle(BX1, Y1, 0#, BX2, Y1, 0#)
  59. '分佈圓之複製孔數,圓周複製參數:圓弧半徑、圓弧角、花紋數、花紋間距(間隔弧度)、圖案旋轉、刪除實例
  60.       boolstatus = swSketchMgr.CreateCircularSketchStepAndRepeat(Circle_radius, pi, Copy_Number, 2 * pi, True, "", True, True, True)
  61. Next
  62. End With
  63. '除料拉伸
  64. Dim myFeature As Object
  65. Set myFeature = Part.FeatureManager.FeatureCut3(True, False, False, 0, 0, Drill_depth, 0, False, False, False, False, 1.74532925199433E-02, _
  66. 1.74532925199433E-02, False, False, False, False, False, True, True, True, True, False, 0, 0, False)
  67. End Sub
复制代码






作者: pyczt    时间: 2018-5-25 08:18
ISketchManager::AddToDBalso avoids some of the peculiarities involved with creating entities via theuser interface, such as inferencing, automatic relations, and snapping to thegrid. Adding entities directly to the database also significantly increases theperformance of this method. When you are done creating entities, it isimportant to ISketchManager::AddToDB(False), to restore SolidWorks to itsnormal operating mode.
ISketchManagerAddToDB还避免了通过用户界面创建实体所涉及的一些特性,例如推理、自动关系和与网格的切换。将实体直接添加到数据库中也显著提高了该方法的性能。创建实体后,对于ISketchManagerAddToDB(False)来说,将SolidWorks恢复到其正常运行模式非常重要。
One of the benefitsof adding sketch entities directly to the database is that you can avoid gridand entity snapping. For example, if you create a sketch line whose endpoint isnear another entity or near a grid point, the new line endpoint snaps to theother item or grid point. Setting ISketchManager::AddToDB to true avoids thisbehavior during sketch entity creation.

将草图实体直接添加到数据库的好处之一是可以避免网格和实体抓取。例如,如果您创建了一个草图线,其端点位于另一个实体附近或网格点附近,则新的线端点将切换到另一个项或网格点。将ISketchManagerAddToDB设置为true,可以避免在创建草图实体时发生这种行为。
  1. Sub Draw_()
  2. With UserForm1
  3. '
  4. If .TextBox1.Value = "" Or .TextBox2.Value = "" Or .TextBox3.Value = "" Or .TextBox4.Value = "" Or .TextBox5.Value = "" Or .TextBox6.Value = "" Then
  5.       MsgBox ("Enter empty")
  6.       Exit Sub
  7. End If
  8. '
  9. Drill_Diameter = .TextBox3.Value / 1000
  10. Start_Circle_radius = .TextBox4.Value / 1000
  11. If Drill_Diameter >= Start_Circle_radius Then
  12.       MsgBox ("Data error")
  13.       Exit Sub
  14. End If
  15. Set swApp = Application.SldWorks
  16. Set Part = swApp.ActiveDoc
  17. Set swModel = swApp.ActiveDoc
  18. Set swSketchMgr = swModel.SketchManager
  19. Part.SketchManager.InsertSketch True '

  20. swModel.SketchManager.AddToDB = True
  21. X1 = .TextBox1.Value / 1000
  22. Y1 = .TextBox2.Value / 1000
  23. X2 = X1 + Drill_Diameter / 2
  24. Set swSketchSegment = swSketchMgr.CreateCircle(X1, Y1, 0#, X2, Y1, 0#)

  25. pi = Atn(1) * 4
  26. Circle_number = .TextBox6.Value
  27. Drill_depth = .TextBox5.Value / 1000
  28. For i = 1 To Circle_number
  29.       Circle_radius = i * Start_Circle_radius
  30.       Copy_Number = Int(2 * Circle_radius * pi / Start_Circle_radius + 0.5)

  31.       BX1 = X1 + Circle_radius
  32.       BX2 = BX1 + Drill_Diameter / 2
  33.       Set swSketchSegment = swSketchMgr.CreateCircle(BX1, Y1, 0#, BX2, Y1, 0#)

  34.       boolstatus = swSketchMgr.CreateCircularSketchStepAndRepeat(Circle_radius, pi, Copy_Number, 2 * pi, True, "", True, True, True)
  35. Next
  36. End With
  37. swModel.SketchManager.AddToDB = False

  38. Dim myFeature As Object
  39. Set myFeature = Part.FeatureManager.FeatureCut3(True, False, False, 0, 0, Drill_depth, 0, False, False, False, False, 1.74532925199433E-02, _
  40. 1.74532925199433E-02, False, False, False, False, False, True, True, True, True, False, 0, 0, False)
  41. End Sub
复制代码




作者: ryouss    时间: 2018-5-25 09:13
本帖最后由 ryouss 于 2018-5-25 11:23 编辑
pyczt 发表于 2018-5-25 08:18
ISketchManager::AddToDBalso avoids some of the peculiarities involved with creating entities via the ...

如上原程式補加 swModel.SketchManager.AddToDB = True  後,

不但解決了 x=0,y=0 的問題,也一併解決了 X 為負值的問題,再次謝謝 pyczt 大大的幫忙.

[attach]1257910[/attach]
作者: qxzch    时间: 2018-5-26 15:22
很神奇的宏!
作者: qxzch    时间: 2018-5-27 18:11
提点建议:1. 中心的孔做个选择,可以钻也可不钻;2. 首圈孔的个数可以自定义,现在是固定的6个。这样更人性化
作者: ryouss    时间: 2018-5-27 20:35
qxzch 发表于 2018-5-27 18:11
提点建议:1. 中心的孔做个选择,可以钻也可不钻;2. 首圈孔的个数可以自定义,现在是固定的6个。这样更人 ...

首圈半徑近似於相鄰兩孔之中心(弧長)距離.


為了滿足如上條件才6个.

作者: Trouble12138    时间: 2018-6-16 14:22
点了就要下载啊,本来就却积分
作者: makejon    时间: 2018-10-3 09:30
怎么提是empty




欢迎光临 iCAx开思网 (https://www.icax.org/) Powered by Discuz! X3.3