cut_lines.bas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Type Coordinate
  2. x As Double
  3. y As Double
  4. End Type
  5. Sub cut_lines()
  6. '// 代码运行时关闭窗口刷新
  7. Application.Optimization = True
  8. ActiveDocument.Unit = cdrMillimeter
  9. Dim OrigSelection As ShapeRange
  10. Set OrigSelection = ActiveSelectionRange
  11. Dim s1 As Shape
  12. Dim dot As Coordinate
  13. Dim arr As Variant, border As Variant
  14. ' 当前选择物件的范围边界
  15. set_lx = OrigSelection.LeftX: set_rx = OrigSelection.RightX
  16. set_by = OrigSelection.BottomY: set_ty = OrigSelection.TopY
  17. set_cx = OrigSelection.CenterX: set_cy = OrigSelection.CenterY
  18. radius = 8: border = Array(set_lx, set_rx, set_by, set_ty, set_cx, set_cy, radius)
  19. For Each Target In OrigSelection
  20. Set s1 = Target
  21. lx = s1.LeftX: rx = s1.RightX
  22. by = s1.BottomY: ty = s1.TopY
  23. cx = s1.CenterX: cy = s1.CenterY
  24. '// 范围边界物件判断
  25. If Abs(set_lx - lx) < radius Or Abs(set_rx - rx) < radius Or Abs(set_by - by) _
  26. < radius Or Abs(set_ty - ty) < radius Then
  27. arr = Array(lx, by, rx, by, lx, ty, rx, ty) '// 物件左下-右下-左上-右上 四个顶点坐标数组
  28. For i = 0 To 3
  29. dot.x = arr(2 * i)
  30. dot.y = arr(2 * i + 1)
  31. '// 范围边界坐标点判断
  32. If Abs(set_lx - dot.x) < radius Or Abs(set_rx - dot.x) < radius _
  33. Or Abs(set_by - dot.y) < radius Or Abs(set_ty - dot.y) < radius Then
  34. draw_line dot, border '// 以坐标点和范围边界画裁切线
  35. End If
  36. Next i
  37. End If
  38. Next Target
  39. Dim s As Shape '// 使用 ObjectData 搜索裁切线,群组裁切线
  40. For Each s In ActivePage.Shapes
  41. If "cut_line" = s.ObjectData("name").Value Then
  42. ActiveDocument.AddToSelection s
  43. End If
  44. Next s
  45. ActiveSelection.Group
  46. '// 代码操作结束恢复窗口刷新
  47. Application.Optimization = False
  48. ActiveWindow.Refresh
  49. Application.Refresh
  50. End Sub
  51. '范围边界 border = Array(set_lx, set_rx, set_by, set_ty, set_cx, set_cy, radius)
  52. Private Function draw_line(dot As Coordinate, border As Variant)
  53. Bleed = 2: line_len = 3: radius = border(6)
  54. Dim line As Shape
  55. If Abs(dot.y - border(3)) < radius Then
  56. Set line = ActiveLayer.CreateLineSegment(dot.x, dot.y + Bleed, dot.x, dot.y + (line_len + Bleed))
  57. set_line_color line
  58. ElseIf Abs(dot.y - border(2)) < radius Then
  59. Set line = ActiveLayer.CreateLineSegment(dot.x, dot.y - Bleed, dot.x, dot.y - (line_len + Bleed))
  60. set_line_color line
  61. End If
  62. If Abs(dot.x - border(1)) < radius Then
  63. Set line = ActiveLayer.CreateLineSegment(dot.x + Bleed, dot.y, dot.x + (line_len + Bleed), dot.y)
  64. set_line_color line
  65. ElseIf Abs(dot.x - border(0)) < radius Then
  66. Set line = ActiveLayer.CreateLineSegment(dot.x - Bleed, dot.y, dot.x - (line_len + Bleed), dot.y)
  67. set_line_color line
  68. End If
  69. End Function
  70. Private Function set_line_color(line As Shape)
  71. '// 设置线宽和注册色,添加物件名为最后群组使用
  72. line.Outline.SetProperties 0.1
  73. line.Outline.SetProperties Color:=CreateRegistrationColor
  74. line.ObjectData("Name").Value = "cut_line"
  75. End Function