AutoCutLines.bas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Attribute VB_Name = "AutoCutLines"
  2. #If VBA7 Then
  3. Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
  4. #Else
  5. Private Declare Sub Sleep Lib "kernel32" (ByValdwMilliseconds As Long)
  6. #End If
  7. Public Sub AutoCutLines()
  8. Nodes_TO_TSP
  9. START_Cut_Line_Algorithm 3#
  10. '延时500毫秒,如果电脑够快,可以调整到100ms
  11. Sleep 500
  12. TSP_TO_DRAW_LINES
  13. End Sub
  14. Private Function Nodes_TO_TSP()
  15. On Error GoTo ErrorHandler
  16. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  17. ActiveDocument.Unit = cdrMillimeter
  18. Set fs = CreateObject("Scripting.FileSystemObject")
  19. Set f = fs.CreateTextFile("C:\TSP\CDR_TO_TSP", True)
  20. Dim s As Shape, ssr As ShapeRange
  21. Set ssr = ActiveSelectionRange
  22. Dim TSP As String
  23. TSP = (ssr.Count * 4) & " " & 0 & vbNewLine
  24. For Each s In ssr
  25. lx = s.LeftX: rx = s.RightX
  26. By = s.BottomY: ty = s.TopY
  27. TSP = TSP & lx & " " & By & vbNewLine
  28. TSP = TSP & lx & " " & ty & vbNewLine
  29. TSP = TSP & rx & " " & By & vbNewLine
  30. TSP = TSP & rx & " " & ty & vbNewLine
  31. Next s
  32. f.WriteLine TSP
  33. f.Close
  34. '// 刷新一下文件流,延时的效果
  35. Set f = fs.OpenTextFile("C:\TSP\CDR_TO_TSP", 1, False)
  36. Dim str
  37. str = f.ReadAll()
  38. f.Close
  39. ActiveDocument.EndCommandGroup: Application.Optimization = False
  40. ActiveWindow.Refresh: Application.Refresh
  41. Exit Function
  42. ErrorHandler:
  43. Application.Optimization = False
  44. On Error Resume Next
  45. End Function
  46. '// TSP功能画线-多线段
  47. Private Function TSP_TO_DRAW_LINES()
  48. On Error GoTo ErrorHandler
  49. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  50. ActiveDocument.Unit = cdrMillimeter
  51. Set fs = CreateObject("Scripting.FileSystemObject")
  52. Set f = fs.OpenTextFile("C:\TSP\TSP2.txt", 1, False)
  53. Dim str, arr, n
  54. Dim line As Shape
  55. str = f.ReadAll()
  56. f.Close
  57. Set f = fs.OpenTextFile("C:\TSP\TSP2.txt", 1, False)
  58. str = f.ReadAll()
  59. str = VBA.Replace(str, vbNewLine, " ")
  60. Do While InStr(str, " ")
  61. str = VBA.Replace(str, " ", " ")
  62. Loop
  63. arr = Split(str)
  64. For n = 2 To UBound(arr) - 1 Step 4
  65. x = Val(arr(n))
  66. y = Val(arr(n + 1))
  67. x1 = Val(arr(n + 2))
  68. y1 = Val(arr(n + 3))
  69. Set line = ActiveLayer.CreateLineSegment(x, y, x1, y1)
  70. set_line_color line
  71. ' 调试线条顺序
  72. puts x, y, (n + 2) / 4
  73. Next
  74. ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(26, 22, 35))").CreateSelection
  75. ActiveSelection.group
  76. ActiveSelection.Outline.SetProperties 0.2, Color:=CreateCMYKColor(0, 100, 100, 0)
  77. ActiveDocument.EndCommandGroup: Application.Optimization = False
  78. ActiveWindow.Refresh: Application.Refresh
  79. Exit Function
  80. ErrorHandler:
  81. Application.Optimization = False
  82. On Error Resume Next
  83. End Function
  84. '// 运行裁切线算法 Cut_Line_Algorithm.py
  85. Private Function START_Cut_Line_Algorithm(Optional ext As Double = 3)
  86. cmd_line = "python C:\TSP\Cut_Line_Algorithm.py" & " " & ext
  87. Shell cmd_line
  88. End Function
  89. '// 设置线条标记(颜色)
  90. Private Function set_line_color(line As Shape)
  91. line.Outline.SetProperties Color:=CreateRGBColor(26, 22, 35)
  92. End Function
  93. Public Sub puts(x, y, n)
  94. Dim st As String
  95. st = str(n)
  96. Set s = ActiveLayer.CreateArtisticText(x, y, st)
  97. End Sub