SmartGroup.bas 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. '// This is free and unencumbered software released into the public domain.
  2. '// For more information, please refer to https://github.com/hongwenjun
  3. '// Attribute VB_Name = "智能群组" SmartGroup 2023.6.30
  4. Public Function Smart_Group(Optional ByVal tr As Double = 0) As ShapeRange
  5. If 0 = ActiveSelectionRange.Count Then Exit Function
  6. On Error GoTo ErrorHandler
  7. API.BeginOpt
  8. Dim OrigSelection As ShapeRange, sr As New ShapeRange
  9. Dim s1 As Shape, sh As Shape, s As Shape
  10. Dim x As Double, Y As Double, w As Double, h As Double
  11. Dim eff1 As Effect
  12. Set OrigSelection = ActiveSelectionRange
  13. '// 遍历物件画矩形
  14. For Each sh In OrigSelection
  15. sh.GetBoundingBox x, Y, w, h
  16. If w * h > 4 Then
  17. Set s = ActiveLayer.CreateRectangle2(x - tr, Y - tr, w + 2 * tr, h + 2 * tr)
  18. sr.Add s
  19. '// 轴线 创建轮廓处理
  20. ElseIf w * h < 0.3 Then
  21. ' Debug.Print w * h
  22. Set eff1 = sh.CreateContour(cdrContourOutside, 0.5, 1, cdrDirectFountainFillBlend, CreateRGBColor(26, 22, 35), _
  23. CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), 0, 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15#)
  24. eff1.Separate
  25. End If
  26. Next sh
  27. '// 查找轴线轮廓
  28. sr.AddRange ActivePage.Shapes.FindShapes(Query:="@Outline.Color=RGB(26, 22, 35)")
  29. sr.AddRange ActivePage.Shapes.FindShapes(Query:="@fill.Color=RGB(26, 22, 35)")
  30. '// 新矩形寻找边界,散开,删除刚才画的新矩形
  31. Dim brk1 As ShapeRange
  32. Set s1 = sr.CustomCommand("Boundary", "CreateBoundary")
  33. Set brk1 = s1.BreakApartEx
  34. sr.Delete
  35. '// 矩形边界智能群组, RetSR 返回群组 和 删除矩形s
  36. Dim RetSR As New ShapeRange
  37. For Each s In brk1
  38. Set sr = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False).Shapes.all
  39. sr.DeleteItem sr.IndexOf(s)
  40. If sr.Count > 0 Then RetSR.Add sr.Group
  41. Next s
  42. '// 智能群组返回和选择
  43. Set Smart_Group = RetSR
  44. RetSR.CreateSelection
  45. ErrorHandler:
  46. API.EndOpt
  47. End Function
  48. '// 智能群组 原理版
  49. Private Function Smart_Group_ABC()
  50. ActiveDocument.Unit = cdrMillimeter
  51. Dim OrigSelection As ShapeRange, brk1 As ShapeRange
  52. Set OrigSelection = ActiveSelectionRange
  53. Dim s1 As Shape, sh As Shape, s As Shape
  54. Set s1 = OrigSelection.CustomCommand("Boundary", "CreateBoundary")
  55. Set brk1 = s1.BreakApartEx
  56. For Each s In brk1
  57. If s.SizeHeight > 10 Then
  58. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  59. sh.Shapes.all.Group
  60. End If
  61. s.Delete
  62. Next
  63. End Function