CutLines.bas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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
  43. Dimension_MarkLines cdrAlignTop
  44. End Sub
  45. '// 标注尺寸标记线
  46. Public Function Dimension_MarkLines(Optional ByVal mark As cdrAlignType = cdrAlignTop)
  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. '// 页面边缘对齐
  77. For Each s In sr
  78. If mark = cdrAlignTop Then
  79. s.TopY = py + Line_len + Bleed
  80. Else
  81. s.LeftX = px - Line_len - Bleed
  82. End If
  83. Next s
  84. '// 简单删除重复
  85. RemoveDuplicates sr
  86. '// 设置线宽和颜色,再选择
  87. sr.SetOutlineProperties Outline_Width
  88. sr.SetOutlineProperties Color:=CreateRGBColor(0, 255, 0)
  89. sr.AddToSelection
  90. API.EndOpt
  91. End Function
  92. '// 简单删除重复线算法
  93. Private Function RemoveDuplicates(sr As ShapeRange)
  94. Dim s As Shape, cnt As Integer, rms As New ShapeRange
  95. cnt = 1
  96. #If VBA7 Then
  97. sr.Sort " @shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
  98. #Else
  99. ' X4 不支持 ShapeRange.sort
  100. #End If
  101. For Each s In sr
  102. If cnt > 1 Then
  103. If Check_duplicate(sr(cnt - 1), sr(cnt)) Then rms.Add sr(cnt)
  104. End If
  105. cnt = cnt + 1
  106. Next s
  107. rms.Delete
  108. End Function
  109. '// 检查重复算法
  110. Private Function Check_duplicate(s1 As Shape, s2 As Shape) As Boolean
  111. Check_duplicate = False
  112. Jitter = 0.1
  113. X = Abs(s1.CenterX - s2.CenterX)
  114. Y = Abs(s1.CenterY - s2.CenterY)
  115. w = Abs(s1.SizeWidth - s2.SizeWidth)
  116. h = Abs(s1.SizeHeight - s2.SizeHeight)
  117. If X < Jitter And Y < Jitter And w < Jitter And h < Jitter Then
  118. Check_duplicate = True
  119. End If
  120. End Function
  121. '// 单线条转裁切线 - 放置到页面四边
  122. Public Function SelectLine_to_Cropline()
  123. If 0 = ActiveSelectionRange.Count Then Exit Function
  124. '// 代码运行时关闭窗口刷新
  125. Application.Optimization = True
  126. ActiveDocument.Unit = cdrMillimeter
  127. ActiveDocument.BeginCommandGroup '一步撤消'
  128. '// 获得页面中心点 x,y
  129. px = ActiveDocument.Pages.First.CenterX
  130. py = ActiveDocument.Pages.First.CenterY
  131. Bleed = API.GetSet("Bleed")
  132. Line_len = API.GetSet("Line_len")
  133. Outline_Width = API.GetSet("Outline_Width")
  134. Dim s As Shape
  135. Dim line As Shape
  136. '// 遍历选择的线条
  137. For Each s In ActiveSelection.Shapes
  138. lx = s.LeftX
  139. rx = s.RightX
  140. By = s.BottomY
  141. ty = s.TopY
  142. cx = s.CenterX
  143. cy = s.CenterY
  144. sw = s.SizeWidth
  145. sh = s.SizeHeight
  146. '// 判断横线(高度小于宽度),在页面左边还是右边
  147. If sh <= sw Then
  148. s.Delete
  149. If cx < px Then
  150. Set line = ActiveLayer.CreateLineSegment(0, cy, 0 + Line_len, cy)
  151. Else
  152. Set line = ActiveLayer.CreateLineSegment(px * 2, cy, px * 2 - Line_len, cy)
  153. End If
  154. End If
  155. '// 判断竖线(高度大于宽度),在页面下边还是上边
  156. If sh > sw Then
  157. s.Delete
  158. If cy < py Then
  159. Set line = ActiveLayer.CreateLineSegment(cx, 0, cx, 0 + Line_len)
  160. Else
  161. Set line = ActiveLayer.CreateLineSegment(cx, py * 2, cx, py * 2 - Line_len)
  162. End If
  163. End If
  164. line.Outline.SetProperties Outline_Width
  165. line.Outline.SetProperties Color:=CreateRegistrationColor
  166. Next s
  167. ActiveDocument.EndCommandGroup
  168. '// 代码操作结束恢复窗口刷新
  169. Application.Optimization = False
  170. ActiveWindow.Refresh
  171. Application.Refresh
  172. End Function