CutLines.bas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Attribute VB_Name = "CutLines"
  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 = "裁切线" CutLines 2023.6.9
  5. '// 选中多个物件批量制作四角裁切线
  6. Public Function Batch_CutLines()
  7. If 0 = ActiveSelectionRange.Count Then Exit Function
  8. API.BeginOpt
  9. Bleed = API.GetSet("Bleed")
  10. Line_len = API.GetSet("Line_len")
  11. Outline_Width = API.GetSet("Outline_Width")
  12. '// 定义当前选择物件 分别获得 左右下上中心坐标(x,y)和尺寸信息
  13. Dim s1 As Shape, OrigSelection As ShapeRange, sr As New ShapeRange
  14. Set OrigSelection = ActiveSelectionRange
  15. For Each s1 In OrigSelection
  16. lx = s1.LeftX: rx = s1.RightX
  17. By = s1.BottomY: ty = s1.TopY
  18. cx = s1.CenterX: cy = s1.CenterY
  19. sw = s1.SizeWidth: sh = s1.SizeHeight
  20. '// 添加裁切线,分别左下-右下-左上-右上
  21. Dim s2, s3, s4, s5, s6, s7, s8, s9 As Shape
  22. Set s2 = ActiveLayer.CreateLineSegment(lx - Bleed, By, lx - (Bleed + Line_len), By)
  23. Set s3 = ActiveLayer.CreateLineSegment(lx, By - Bleed, lx, By - (Bleed + Line_len))
  24. Set s4 = ActiveLayer.CreateLineSegment(rx + Bleed, By, rx + (Bleed + Line_len), By)
  25. Set s5 = ActiveLayer.CreateLineSegment(rx, By - Bleed, rx, By - (Bleed + Line_len))
  26. Set s6 = ActiveLayer.CreateLineSegment(lx - Bleed, ty, lx - (Bleed + Line_len), ty)
  27. Set s7 = ActiveLayer.CreateLineSegment(lx, ty + Bleed, lx, ty + (Bleed + Line_len))
  28. Set s8 = ActiveLayer.CreateLineSegment(rx + Bleed, ty, rx + (Bleed + Line_len), ty)
  29. Set s9 = ActiveLayer.CreateLineSegment(rx, ty + Bleed, rx, ty + (Bleed + Line_len))
  30. '// 选中裁切线 群组 设置线宽和注册色
  31. ActiveDocument.AddToSelection s2, s3, s4, s5, s6, s7, s8, s9
  32. ActiveSelection.group
  33. sr.Add ActiveSelection
  34. Next s1
  35. '// 设置线宽和颜色,再选择
  36. sr.SetOutlineProperties Outline_Width
  37. sr.SetOutlineProperties Color:=CreateRegistrationColor
  38. sr.AddToSelection
  39. API.EndOpt
  40. End Function
  41. Sub test_MarkLines()
  42. Dimension_MarkLines cdrAlignLeft, True
  43. ' Dimension_MarkLines cdrAlignTop, True
  44. End Sub
  45. '// 标注尺寸标记线
  46. Public Function Dimension_MarkLines(Optional ByVal mark As cdrAlignType = cdrAlignTop, Optional ByVal mirror As Boolean = False)
  47. If 0 = ActiveSelectionRange.Count Then Exit Function
  48. API.BeginOpt
  49. Bleed = API.GetSet("Bleed")
  50. Line_len = API.GetSet("Line_len")
  51. Outline_Width = API.GetSet("Outline_Width")
  52. '// 定义当前选择物件 分别获得 左右下上中心坐标(x,y)和尺寸信息
  53. Dim s As Shape, s1 As Shape, OrigSelection As ShapeRange, sr As New ShapeRange
  54. Set OrigSelection = ActiveSelectionRange
  55. For Each s1 In OrigSelection
  56. lx = s1.LeftX: rx = s1.RightX
  57. By = s1.BottomY: ty = s1.TopY
  58. '// 添加使用 左-上 标注尺寸标记线
  59. Dim s2, s6, s7, s8, s9 As Shape
  60. If mark = cdrAlignTop Then
  61. Set s7 = ActiveLayer.CreateLineSegment(lx, ty + Bleed, lx, ty + (Bleed + Line_len))
  62. Set s9 = ActiveLayer.CreateLineSegment(rx, ty + Bleed, rx, ty + (Bleed + Line_len))
  63. sr.Add s7: sr.Add s9
  64. Else
  65. Set s2 = ActiveLayer.CreateLineSegment(lx - Bleed, By, lx - (Bleed + Line_len), By)
  66. Set s6 = ActiveLayer.CreateLineSegment(lx - Bleed, ty, lx - (Bleed + Line_len), ty)
  67. sr.Add s2: sr.Add s6
  68. End If
  69. Next s1
  70. '// 获得页面中心点 x,y
  71. ' px = ActiveDocument.Pages.First.CenterX
  72. ' py = ActiveDocument.Pages.First.CenterY
  73. '// 物件范围边界
  74. px = OrigSelection.LeftX
  75. py = OrigSelection.TopY
  76. mpx = OrigSelection.RightX
  77. mpy = OrigSelection.BottomY
  78. '// 页面边缘对齐
  79. For Each s In sr
  80. If mark = cdrAlignTop Then
  81. s.TopY = py + Line_len + Bleed
  82. Else
  83. s.LeftX = px - Line_len - Bleed
  84. End If
  85. Next s
  86. '// 简单删除重复
  87. RemoveDuplicates sr
  88. '// 设置线宽和颜色,再选择
  89. sr.SetOutlineProperties Outline_Width
  90. sr.SetOutlineProperties Color:=CreateCMYKColor(80, 40, 0, 20)
  91. sr.AddToSelection
  92. If mirror Then
  93. If mark = cdrAlignTop Then
  94. sr.BottomY = mpy - Line_len - Bleed
  95. Else
  96. sr.RightX = mpx + Line_len + Bleed
  97. End If
  98. End If
  99. API.EndOpt
  100. End Function
  101. '// 简单删除重复线算法
  102. Private Function RemoveDuplicates(sr As ShapeRange)
  103. Dim s As Shape, cnt As Integer, rms As New ShapeRange
  104. cnt = 1
  105. #If VBA7 Then
  106. sr.Sort " @shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
  107. #Else
  108. ' X4 不支持 ShapeRange.sort
  109. #End If
  110. For Each s In sr
  111. If cnt > 1 Then
  112. If Check_duplicate(sr(cnt - 1), sr(cnt)) Then rms.Add sr(cnt)
  113. End If
  114. s.Name = "DMKLine"
  115. cnt = cnt + 1
  116. Next s
  117. rms.Delete
  118. End Function
  119. '// 检查重复算法
  120. Private Function Check_duplicate(s1 As Shape, s2 As Shape) As Boolean
  121. Check_duplicate = False
  122. Jitter = 0.1
  123. X = Abs(s1.CenterX - s2.CenterX)
  124. Y = Abs(s1.CenterY - s2.CenterY)
  125. w = Abs(s1.SizeWidth - s2.SizeWidth)
  126. h = Abs(s1.SizeHeight - s2.SizeHeight)
  127. If X < Jitter And Y < Jitter And w < Jitter And h < Jitter Then
  128. Check_duplicate = True
  129. End If
  130. End Function
  131. '// 单线条转裁切线 - 放置到页面四边
  132. Public Function SelectLine_to_Cropline()
  133. If 0 = ActiveSelectionRange.Count Then Exit Function
  134. '// 代码运行时关闭窗口刷新
  135. Application.Optimization = True
  136. ActiveDocument.Unit = cdrMillimeter
  137. ActiveDocument.BeginCommandGroup '一步撤消'
  138. '// 获得页面中心点 x,y
  139. px = ActiveDocument.Pages.First.CenterX
  140. py = ActiveDocument.Pages.First.CenterY
  141. Bleed = API.GetSet("Bleed")
  142. Line_len = API.GetSet("Line_len")
  143. Outline_Width = API.GetSet("Outline_Width")
  144. Dim s As Shape
  145. Dim line As Shape
  146. '// 遍历选择的线条
  147. For Each s In ActiveSelection.Shapes
  148. lx = s.LeftX
  149. rx = s.RightX
  150. By = s.BottomY
  151. ty = s.TopY
  152. cx = s.CenterX
  153. cy = s.CenterY
  154. sw = s.SizeWidth
  155. sh = s.SizeHeight
  156. '// 判断横线(高度小于宽度),在页面左边还是右边
  157. If sh <= sw Then
  158. s.Delete
  159. If cx < px Then
  160. Set line = ActiveLayer.CreateLineSegment(0, cy, 0 + Line_len, cy)
  161. Else
  162. Set line = ActiveLayer.CreateLineSegment(px * 2, cy, px * 2 - Line_len, cy)
  163. End If
  164. End If
  165. '// 判断竖线(高度大于宽度),在页面下边还是上边
  166. If sh > sw Then
  167. s.Delete
  168. If cy < py Then
  169. Set line = ActiveLayer.CreateLineSegment(cx, 0, cx, 0 + Line_len)
  170. Else
  171. Set line = ActiveLayer.CreateLineSegment(cx, py * 2, cx, py * 2 - Line_len)
  172. End If
  173. End If
  174. line.Outline.SetProperties Outline_Width
  175. line.Outline.SetProperties Color:=CreateRegistrationColor
  176. Next s
  177. ActiveDocument.EndCommandGroup
  178. '// 代码操作结束恢复窗口刷新
  179. Application.Optimization = False
  180. ActiveWindow.Refresh
  181. Application.Refresh
  182. End Function