SmartGroup.bas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Attribute VB_Name = "SmartGroup"
  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 = "智能群组" SmartGroup 2023.6.11
  5. Sub 剪贴板物件替换()
  6. Replace_UI.Show 0
  7. End Sub
  8. Public Sub 智能群组(Optional ByVal tr As Double = 0)
  9. If 0 = ActiveSelectionRange.Count Then Exit Sub
  10. On Error GoTo ErrorHandler
  11. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  12. ActiveDocument.ReferencePoint = cdrBottomLeft
  13. ActiveDocument.Unit = cdrMillimeter
  14. Dim OrigSelection As ShapeRange, sr As New ShapeRange
  15. Dim s1 As Shape, sh As Shape, s As Shape
  16. Dim X As Double, Y As Double, w As Double, h As Double
  17. Dim eff1 As Effect
  18. Set OrigSelection = ActiveSelectionRange
  19. '// 遍历物件画矩形
  20. For Each sh In OrigSelection
  21. sh.GetBoundingBox X, Y, w, h
  22. If w * h > 4 Then
  23. Set s = ActiveLayer.CreateRectangle2(X - tr, Y - tr, w + 2 * tr, h + 2 * tr)
  24. sr.Add s
  25. '// 轴线 创建轮廓处理
  26. ElseIf w * h < 0.3 Then
  27. ' Debug.Print w * h
  28. Set eff1 = sh.CreateContour(cdrContourOutside, 0.5, 1, cdrDirectFountainFillBlend, _
  29. CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), 0, 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15#)
  30. eff1.Separate
  31. End If
  32. Next sh
  33. '// 查找轴线轮廓
  34. ActivePage.Shapes.FindShapes(Query:="@Outline.Color=RGB(26, 22, 35)").CreateSelection
  35. ActivePage.Shapes.FindShapes(Query:="@fill.Color=RGB(26, 22, 35)").AddToSelection
  36. For Each sh In ActiveSelection.Shapes
  37. sr.Add sh
  38. Next sh
  39. '// 新矩形寻找边界,散开,删除刚才画的新矩形
  40. Set s1 = sr.CustomCommand("Boundary", "CreateBoundary")
  41. Set brk1 = s1.BreakApartEx
  42. sr.Delete
  43. '// 矩形边界智能群组,删除矩形
  44. For Each s In brk1
  45. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  46. sh.Shapes.all.group
  47. s.Delete
  48. Next
  49. ActiveDocument.EndCommandGroup
  50. Application.Optimization = False
  51. ActiveWindow.Refresh: Application.Refresh
  52. Exit Sub
  53. ErrorHandler:
  54. Application.Optimization = False
  55. MsgBox "请先选择一些物件来确定群组范围!"
  56. On Error Resume Next
  57. End Sub
  58. ' 智能群组_V1 第一版,储备示例代码
  59. Function 智能群组_V1()
  60. On Error GoTo ErrorHandler
  61. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  62. ActiveDocument.Unit = cdrMillimeter
  63. Dim OrigSelection As ShapeRange, brk1 As ShapeRange
  64. Set OrigSelection = ActiveSelectionRange
  65. Dim s1 As Shape, sh As Shape, s As Shape
  66. Set s1 = OrigSelection.CustomCommand("Boundary", "CreateBoundary")
  67. ' s1.Outline.SetProperties Color:=CreateRGBColor(26, 22, 35)
  68. Set brk1 = s1.BreakApartEx
  69. For Each s In brk1
  70. If s.SizeHeight > 10 Then
  71. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  72. sh.Shapes.all.group
  73. End If
  74. s.Delete
  75. Next
  76. ' ActiveDocument.ClearSelection
  77. ' ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(26, 22, 35))").CreateSelections
  78. '// 代码操作结束恢复窗口刷新
  79. ActiveDocument.EndCommandGroup
  80. Application.Optimization = False
  81. ActiveWindow.Refresh: Application.Refresh
  82. Exit Function
  83. ErrorHandler:
  84. Application.Optimization = False
  85. MsgBox "请先选择一些物件来确定群组范围!"
  86. On Error Resume Next
  87. End Function