SmartGroup.bas 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Public Sub Smart_Group(Optional ByVal tr As Double = 0)
  6. If 0 = ActiveSelectionRange.Count Then Exit Sub
  7. On Error GoTo ErrorHandler
  8. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  9. ActiveDocument.ReferencePoint = cdrBottomLeft
  10. ActiveDocument.Unit = cdrMillimeter
  11. Dim OrigSelection As ShapeRange, sr As New ShapeRange
  12. Dim s1 As Shape, sh As Shape, s As Shape
  13. Dim X As Double, Y As Double, w As Double, h As Double
  14. Dim eff1 As Effect
  15. Set OrigSelection = ActiveSelectionRange
  16. '// 遍历物件画矩形
  17. For Each sh In OrigSelection
  18. sh.GetBoundingBox X, Y, w, h
  19. If w * h > 4 Then
  20. Set s = ActiveLayer.CreateRectangle2(X - tr, Y - tr, w + 2 * tr, h + 2 * tr)
  21. sr.Add s
  22. '// 轴线 创建轮廓处理
  23. ElseIf w * h < 0.3 Then
  24. ' Debug.Print w * h
  25. Set eff1 = sh.CreateContour(cdrContourOutside, 0.5, 1, cdrDirectFountainFillBlend, _
  26. CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), 0, 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15#)
  27. eff1.Separate
  28. End If
  29. Next sh
  30. '// 查找轴线轮廓
  31. ActivePage.Shapes.FindShapes(Query:="@Outline.Color=RGB(26, 22, 35)").CreateSelection
  32. ActivePage.Shapes.FindShapes(Query:="@fill.Color=RGB(26, 22, 35)").AddToSelection
  33. For Each sh In ActiveSelection.Shapes
  34. sr.Add sh
  35. Next sh
  36. '// 新矩形寻找边界,散开,删除刚才画的新矩形
  37. Set s1 = sr.CustomCommand("Boundary", "CreateBoundary")
  38. Set brk1 = s1.BreakApartEx
  39. sr.Delete
  40. '// 矩形边界智能群组,删除矩形
  41. For Each s In brk1
  42. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  43. sh.Shapes.all.Group
  44. s.Delete
  45. Next
  46. ActiveDocument.EndCommandGroup
  47. Application.Optimization = False
  48. ActiveWindow.Refresh: Application.Refresh
  49. Exit Sub
  50. ErrorHandler:
  51. Application.Optimization = False
  52. MsgBox "请先选择一些物件来确定群组范围!"
  53. On Error Resume Next
  54. End Sub
  55. '// 智能群组 原理版
  56. Function Smart_Group_ABC()
  57. ActiveDocument.Unit = cdrMillimeter
  58. Dim OrigSelection As ShapeRange, brk1 As ShapeRange
  59. Set OrigSelection = ActiveSelectionRange
  60. Dim s1 As Shape, sh As Shape, s As Shape
  61. Set s1 = OrigSelection.CustomCommand("Boundary", "CreateBoundary")
  62. Set brk1 = s1.BreakApartEx
  63. For Each s In brk1
  64. If s.SizeHeight > 10 Then
  65. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  66. sh.Shapes.all.Group
  67. End If
  68. s.Delete
  69. Next
  70. End Function