Arrange.bas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Attribute VB_Name = "Arrange"
  2. '// This is free and unencumbered software released into the public domain.
  3. '// For more information, please refer to https://github.com/hongwenjun
  4. '// Attribute VB_Name = "拼版裁切线" Arrange 2023.6.11
  5. Type Coordinate
  6. X As Double
  7. Y As Double
  8. End Type
  9. Sub Cut_lines()
  10. If 0 = ActiveSelectionRange.Count Then Exit Sub
  11. '// 代码运行时关闭窗口刷新
  12. Application.Optimization = True
  13. ActiveDocument.BeginCommandGroup '一步撤消'
  14. ActiveDocument.Unit = cdrMillimeter
  15. Dim OrigSelection As ShapeRange
  16. Set OrigSelection = ActiveSelectionRange
  17. Dim s1 As Shape, sbd As Shape
  18. Dim dot As Coordinate
  19. Dim arr As Variant, border As Variant
  20. ' 当前选择物件的范围边界
  21. set_lx = OrigSelection.LeftX: set_rx = OrigSelection.RightX
  22. set_by = OrigSelection.BottomY: set_ty = OrigSelection.TopY
  23. set_cx = OrigSelection.CenterX: set_cy = OrigSelection.CenterY
  24. radius = 8
  25. Bleed = API.GetSet("Bleed")
  26. Line_len = API.GetSet("Line_len")
  27. Outline_Width = API.GetSet("Outline_Width")
  28. border = Array(set_lx, set_rx, set_by, set_ty, set_cx, set_cy, radius, Bleed, Line_len)
  29. ' 创建边界矩形,用来添加角线
  30. Set sbd = ActiveLayer.CreateRectangle(set_lx, set_by, set_rx, set_ty)
  31. OrigSelection.Add sbd
  32. For Each Target In OrigSelection
  33. Set s1 = Target
  34. lx = s1.LeftX: rx = s1.RightX
  35. By = s1.BottomY: ty = s1.TopY
  36. cx = s1.CenterX: cy = s1.CenterY
  37. '// 范围边界物件判断
  38. If Abs(set_lx - lx) < radius Or Abs(set_rx - rx) < radius Or Abs(set_by - By) _
  39. < radius Or Abs(set_ty - ty) < radius Then
  40. arr = Array(lx, By, rx, By, lx, ty, rx, ty) '// 物件左下-右下-左上-右上 四个顶点坐标数组
  41. For i = 0 To 3
  42. dot.X = arr(2 * i)
  43. dot.Y = arr(2 * i + 1)
  44. '// 范围边界坐标点判断
  45. If Abs(set_lx - dot.X) < radius Or Abs(set_rx - dot.X) < radius _
  46. Or Abs(set_by - dot.Y) < radius Or Abs(set_ty - dot.Y) < radius Then
  47. draw_line dot, border '// 以坐标点和范围边界画裁切线
  48. End If
  49. Next i
  50. End If
  51. Next Target
  52. sbd.Delete '删除边界矩形
  53. '// 使用CQL 颜色标志查找,然后群组统一设置线宽和注册色
  54. ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(26, 22, 35))").CreateSelection
  55. ActiveSelection.group
  56. ActiveSelection.Outline.SetProperties Outline_Width, Color:=CreateRegistrationColor
  57. ActiveDocument.EndCommandGroup
  58. '// 代码操作结束恢复窗口刷新
  59. Application.Optimization = False
  60. ActiveWindow.Refresh
  61. Application.Refresh
  62. End Sub
  63. '范围边界 border = Array(set_lx, set_rx, set_by, set_ty, set_cx, set_cy, radius, Bleed, Line_len)
  64. Private Function draw_line(dot As Coordinate, border As Variant)
  65. radius = border(6): Bleed = border(7): Line_len = border(8)
  66. Dim line As Shape
  67. If Abs(dot.Y - border(3)) < radius Then
  68. Set line = ActiveLayer.CreateLineSegment(dot.X, border(3) + Bleed, dot.X, border(3) + (Line_len + Bleed))
  69. set_line_color line
  70. ElseIf Abs(dot.Y - border(2)) < radius Then
  71. Set line = ActiveLayer.CreateLineSegment(dot.X, border(2) - Bleed, dot.X, border(2) - (Line_len + Bleed))
  72. set_line_color line
  73. End If
  74. If Abs(dot.X - border(1)) < radius Then
  75. Set line = ActiveLayer.CreateLineSegment(border(1) + Bleed, dot.Y, border(1) + (Line_len + Bleed), dot.Y)
  76. set_line_color line
  77. ElseIf Abs(dot.X - border(0)) < radius Then
  78. Set line = ActiveLayer.CreateLineSegment(border(0) - Bleed, dot.Y, border(0) - (Line_len + Bleed), dot.Y)
  79. set_line_color line
  80. End If
  81. End Function
  82. '// 旧版本
  83. Private Function draw_line_按点基准(dot As Coordinate, border As Variant)
  84. Bleed = 2: Line_len = 3: radius = border(6)
  85. Dim line As Shape
  86. If Abs(dot.Y - border(3)) < radius Then
  87. Set line = ActiveLayer.CreateLineSegment(dot.X, dot.Y + Bleed, dot.X, dot.Y + (Line_len + Bleed))
  88. set_line_color line
  89. ElseIf Abs(dot.Y - border(2)) < radius Then
  90. Set line = ActiveLayer.CreateLineSegment(dot.X, dot.Y - Bleed, dot.X, dot.Y - (Line_len + Bleed))
  91. set_line_color line
  92. End If
  93. If Abs(dot.X - border(1)) < radius Then
  94. Set line = ActiveLayer.CreateLineSegment(dot.X + Bleed, dot.Y, dot.X + (Line_len + Bleed), dot.Y)
  95. set_line_color line
  96. ElseIf Abs(dot.X - border(0)) < radius Then
  97. Set line = ActiveLayer.CreateLineSegment(dot.X - Bleed, dot.Y, dot.X - (Line_len + Bleed), dot.Y)
  98. set_line_color line
  99. End If
  100. End Function
  101. Private Function set_line_color(line As Shape)
  102. '// 设置轮廓线注册色
  103. line.Outline.SetProperties Color:=CreateRGBColor(26, 22, 35)
  104. End Function
  105. '// CorelDRAW 物件排列拼版简单代码
  106. Sub Arrange()
  107. On Error GoTo ErrorHandler
  108. ActiveDocument.Unit = cdrMillimeter
  109. row = 3 ' 拼版 3 x 4
  110. List = 4
  111. sp = 0 '间隔 0mm
  112. Dim Str, arr, n
  113. Str = API.GetClipBoardString
  114. ' 替换 mm x * 换行 TAB 为空格
  115. Str = VBA.Replace(Str, "mm", " ")
  116. Str = VBA.Replace(Str, "x", " ")
  117. Str = VBA.Replace(Str, "X", " ")
  118. Str = VBA.Replace(Str, "*", " ")
  119. Str = VBA.Replace(Str, Chr(13), " ")
  120. Str = VBA.Replace(Str, Chr(9), " ")
  121. Do While InStr(Str, " ") '多个空格换成一个空格
  122. Str = VBA.Replace(Str, " ", " ")
  123. Loop
  124. arr = Split(Str)
  125. Dim s1 As Shape
  126. Dim X As Double, Y As Double
  127. If 0 = ActiveSelectionRange.Count Then
  128. X = Val(arr(0)): Y = Val(arr(1))
  129. row = Int(ActiveDocument.Pages.First.SizeWidth / X)
  130. List = Int(ActiveDocument.Pages.First.SizeHeight / Y)
  131. If UBound(arr) > 2 Then
  132. row = Val(arr(2)): List = Val(arr(3))
  133. If row * List > 800 Then
  134. GoTo ErrorHandler
  135. ElseIf UBound(arr) > 3 Then
  136. sp = Val(arr(4)) '间隔
  137. End If
  138. End If
  139. '// 建立矩形 Width x Height 单位 mm
  140. Set s1 = ActiveLayer.CreateRectangle(0, 0, X, Y)
  141. '// 填充颜色无,轮廓颜色 K100,线条粗细0.3mm
  142. s1.Fill.ApplyNoFill
  143. s1.Outline.SetProperties 0.3, OutlineStyles(0), CreateCMYKColor(0, 100, 0, 0), ArrowHeads(0), _
  144. ArrowHeads(0), cdrFalse, cdrFalse, cdrOutlineButtLineCaps, cdrOutlineMiterLineJoin, 0#, 100, MiterLimit:=5#
  145. '// 如果当前选择物件,按当前物件拼版
  146. ElseIf 1 = ActiveSelectionRange.Count Then
  147. Set s1 = ActiveSelection
  148. X = s1.SizeWidth: Y = s1.SizeHeight
  149. row = Int(ActiveDocument.Pages.First.SizeWidth / X)
  150. List = Int(ActiveDocument.Pages.First.SizeHeight / Y)
  151. End If
  152. sw = X: sh = Y
  153. '// StepAndRepeat 方法在范围内创建多个形状副本
  154. Dim dup1 As ShapeRange
  155. Set dup1 = s1.StepAndRepeat(row - 1, sw + sp, 0#)
  156. Dim dup2 As ShapeRange
  157. Set dup2 = ActiveDocument.CreateShapeRangeFromArray(dup1, s1).StepAndRepeat(List - 1, 0#, (sh + sp))
  158. Exit Sub
  159. ErrorHandler:
  160. Speak_Msg "记事本输入数字,示例: 50x50 4x3 ,复制到剪贴板再运行工具!"
  161. MsgBox "记事本输入数字,示例: 50x50 4x3 ,复制到剪贴板再运行工具!"
  162. On Error Resume Next
  163. End Sub