AutoCutLines.bas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. TSP_TO_DRAW_LINE
  14. End Sub
  15. Private Function Nodes_TO_TSP()
  16. On Error GoTo ErrorHandler
  17. API.BeginOpt "Nodes_TO_TSP"
  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. API.EndOpt
  40. Exit Function
  41. ErrorHandler:
  42. Application.Optimization = False
  43. On Error Resume Next
  44. End Function
  45. '// TSP功能画线-多线段
  46. Private Function TSP_TO_DRAW_LINES()
  47. On Error GoTo ErrorHandler
  48. API.BeginOpt "TSP_TO_DRAW_LINES"
  49. Set fs = CreateObject("Scripting.FileSystemObject")
  50. Set f = fs.OpenTextFile("C:\TSP\TSP2.txt", 1, False)
  51. Dim str, arr, n
  52. Dim line As Shape
  53. str = f.ReadAll()
  54. f.Close
  55. Set f = fs.OpenTextFile("C:\TSP\TSP2.txt", 1, False)
  56. str = f.ReadAll()
  57. str = VBA.Replace(str, vbNewLine, " ")
  58. Do While InStr(str, " ")
  59. str = VBA.Replace(str, " ", " ")
  60. Loop
  61. arr = Split(str)
  62. For n = 2 To UBound(arr) - 1 Step 4
  63. x = Val(arr(n))
  64. Y = Val(arr(n + 1))
  65. x1 = Val(arr(n + 2))
  66. y1 = Val(arr(n + 3))
  67. Set line = ActiveLayer.CreateLineSegment(x, Y, x1, y1)
  68. set_line_color line
  69. ' 调试线条顺序
  70. puts x, Y, (n + 2) / 4
  71. Next
  72. ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(26, 22, 35))").CreateSelection
  73. ActiveSelection.Group
  74. ActiveSelection.Outline.SetProperties 0.2, Color:=CreateCMYKColor(0, 100, 100, 0)
  75. API.EndOpt
  76. Exit Function
  77. ErrorHandler:
  78. Application.Optimization = False
  79. On Error Resume Next
  80. End Function
  81. '// 运行裁切线算法 Cut_Line_Algorithm.py
  82. Private Function START_Cut_Line_Algorithm(Optional ext As Double = 3)
  83. cmd_line = "python C:\TSP\Cut_Line_Algorithm.py" & " " & ext
  84. Shell cmd_line
  85. End Function
  86. '// 设置线条标记(颜色)
  87. Private Function set_line_color(line As Shape)
  88. line.Outline.SetProperties Color:=CreateRGBColor(26, 22, 35)
  89. End Function
  90. Public Sub puts(x, Y, n)
  91. Dim st As String
  92. st = str(n)
  93. Set s = ActiveLayer.CreateArtisticText(x, Y, st)
  94. End Sub
  95. '// TSP功能画线-弓形线
  96. Public Function TSP_TO_DRAW_LINE()
  97. On Error GoTo ErrorHandler
  98. API.BeginOpt
  99. Set fs = CreateObject("Scripting.FileSystemObject")
  100. Set f = fs.OpenTextFile("C:\TSP\TSP2.txt", 1, False)
  101. Dim str, arr, n
  102. str = f.ReadAll()
  103. str = API.Newline_to_Space(str)
  104. arr = Split(str)
  105. total = Val(arr(0)) * 2
  106. ReDim ce(total) As CurveElement
  107. Dim crv As Curve
  108. ce(0).ElementType = cdrElementStart
  109. ce(0).PositionX = Val(arr(2)) ' - 3
  110. ce(0).PositionY = Val(arr(3)) ' - 3
  111. Dim x As Double
  112. Dim Y As Double
  113. For n = 2 To UBound(arr) - 1 Step 2
  114. x = Val(arr(n))
  115. Y = Val(arr(n + 1))
  116. ce(n / 2).ElementType = cdrElementLine
  117. ce(n / 2).PositionX = x
  118. ce(n / 2).PositionY = Y
  119. Next
  120. Set crv = CreateCurve(ActiveDocument)
  121. crv.CreateSubPathFromArray ce
  122. ActiveLayer.CreateCurve crv
  123. ErrorHandler:
  124. API.EndOpt
  125. End Function