TSP.bas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. Attribute VB_Name = "TSP"
  2. Public Function CDR_TO_TSP()
  3. Set fs = CreateObject("Scripting.FileSystemObject")
  4. Set f = fs.CreateTextFile("C:\TSP\CDR_TO_TSP", True)
  5. ActiveDocument.Unit = cdrMillimeter
  6. Dim sh As Shape, shs As Shapes, cs As Shape
  7. Dim x As Double, y As Double
  8. Set shs = ActiveSelection.Shapes
  9. Dim TSP As String
  10. TSP = shs.Count & " " & 0 & vbNewLine
  11. For Each sh In shs
  12. x = sh.CenterX
  13. y = sh.CenterY
  14. TSP = TSP & x & " " & y & vbNewLine
  15. Next sh
  16. f.WriteLine TSP
  17. f.Close
  18. MsgBox "小圆点导出节点信息到数据文件!" & vbNewLine
  19. End Function
  20. Public Function PATH_TO_TSP()
  21. Set fs = CreateObject("Scripting.FileSystemObject")
  22. Set f = fs.CreateTextFile("C:\TSP\CDR_TO_TSP", True)
  23. ActiveDocument.Unit = cdrMillimeter
  24. Dim sh As Shape, shs As Shapes, cs As Shape
  25. Dim x As Double, y As Double
  26. Set shs = ActiveSelection.Shapes
  27. Dim TSP As String
  28. TSP = shs.Count & " " & 0 & vbNewLine
  29. For Each sh In shs
  30. x = sh.CenterX
  31. y = sh.CenterY
  32. TSP = TSP & x & " " & y & vbNewLine
  33. Next sh
  34. f.WriteLine TSP
  35. f.Close
  36. MsgBox "选择曲线导出节点信息到数据文件!" & vbNewLine
  37. End Function
  38. Public Function START_TSP()
  39. cmd_line = "C:\TSP\CDR2TSP.exe C:\TSP\CDR_TO_TSP"
  40. Shell cmd_line
  41. End Function
  42. Public Function TSP_TO_DRAW_LINE()
  43. ' On Error GoTo ErrorHandler
  44. ActiveDocument.Unit = cdrMillimeter
  45. Set fs = CreateObject("Scripting.FileSystemObject")
  46. Set f = fs.OpenTextFile("C:\TSP\TSP.txt", 1, False)
  47. Dim Str, arr, n
  48. Str = f.ReadAll()
  49. Str = VBA.replace(Str, vbNewLine, " ")
  50. Do While InStr(Str, " ")
  51. Str = VBA.replace(Str, " ", " ")
  52. Loop
  53. arr = Split(Str)
  54. total = Val(arr(0))
  55. ReDim ce(total) As CurveElement
  56. Dim crv As Curve
  57. ce(0).ElementType = cdrElementStart
  58. ce(0).PositionX = 0
  59. ce(0).PositionY = 0
  60. Dim x As Double
  61. Dim y As Double
  62. For n = 2 To UBound(arr) - 1 Step 2
  63. x = Val(arr(n))
  64. y = Val(arr(n + 1))
  65. ce(n / 2).ElementType = cdrElementLine
  66. ce(n / 2).PositionX = x
  67. ce(n / 2).PositionY = y
  68. Next
  69. Set crv = CreateCurve(ActiveDocument)
  70. crv.CreateSubPathFromArray ce
  71. ActiveLayer.CreateCurve crv
  72. ErrorHandler:
  73. On Error Resume Next
  74. End Function
  75. Public Function TSP_TO_DRAW_LINE_BAK()
  76. On Error GoTo ErrorHandler
  77. ActiveDocument.Unit = cdrMillimeter
  78. Dim Str, arr, n
  79. Str = API.GetClipBoardString
  80. Str = VBA.replace(Str, vbNewLine, " ")
  81. Do While InStr(Str, " ")
  82. Str = VBA.replace(Str, " ", " ")
  83. Loop
  84. arr = Split(Str)
  85. total = Val(arr(0))
  86. ReDim ce(total) As CurveElement
  87. Dim crv As Curve
  88. ce(0).ElementType = cdrElementStart
  89. ce(0).PositionX = 0
  90. ce(0).PositionY = 0
  91. Dim x As Double
  92. Dim y As Double
  93. For n = 2 To UBound(arr) - 1 Step 2
  94. x = Val(arr(n))
  95. y = Val(arr(n + 1))
  96. ce(n / 2).ElementType = cdrElementLine
  97. ce(n / 2).PositionX = x
  98. ce(n / 2).PositionY = y
  99. Next
  100. Set crv = CreateCurve(ActiveDocument)
  101. crv.CreateSubPathFromArray ce
  102. ActiveLayer.CreateCurve crv
  103. ErrorHandler:
  104. On Error Resume Next
  105. End Function
  106. Public Function MAKE_TSP()
  107. cmd_line = "C:\TSP\TSP.exe"
  108. Shell cmd_line
  109. End Function
  110. ' 位图制作小圆点
  111. Public Function BITMAP_MAKE_DOTS()
  112. ' On Error GoTo ErrorHandler
  113. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  114. ActiveDocument.Unit = cdrMillimeter
  115. Dim line, art, n, h, w
  116. Dim x As Double
  117. Dim y As Double
  118. Dim s As Shape
  119. flag = 0
  120. Set fs = CreateObject("Scripting.FileSystemObject")
  121. Set f = fs.OpenTextFile("C:\TSP\BITMAP", 1, False)
  122. line = f.ReadLine()
  123. Debug.Print line
  124. ' 读取第一行,位图 h高度 和 w宽度
  125. arr = Split(line)
  126. h = Val(arr(0)): w = Val(arr(1))
  127. If h * w > 40000 Then
  128. MsgBox "位图转换后的小圆点数量比较多:" & vbNewLine & h & " x " & w & " = " & h * w
  129. flag = 1
  130. End If
  131. For i = 1 To h
  132. line = f.ReadLine()
  133. arr = Split(line)
  134. For n = LBound(arr) To UBound(arr)
  135. If arr(n) > 0 Then
  136. x = n: y = -i
  137. If flag = 1 Then
  138. Set s = ActiveLayer.CreateRectangle2(x, y, 0.6, 0.6)
  139. Else
  140. make_dots x, y
  141. End If
  142. End If
  143. Next n
  144. Next i
  145. ActiveDocument.EndCommandGroup: Application.Optimization = False
  146. ActiveWindow.Refresh: Application.Refresh
  147. Exit Function
  148. ErrorHandler:
  149. Application.Optimization = False
  150. On Error Resume Next
  151. End Function
  152. Private Function make_dots(x As Double, y As Double)
  153. Dim s As Shape
  154. Dim c As Variant
  155. c = Array(0, 255, 0)
  156. Set s = ActiveLayer.CreateEllipse2(x, y, 0.5, 0.5)
  157. s.Fill.UniformColor.RGBAssign c(Int(Rnd() * 2)), c(Int(Rnd() * 2)), c(Int(Rnd() * 2))
  158. s.Outline.Width = 0#
  159. End Function