Container.bas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. Attribute VB_Name = "Container"
  2. ' ① 标记容器盒子
  3. Public Function SetBoxName()
  4. Dim box As ShapeRange, s As Shape
  5. Set box = ActiveSelectionRange
  6. Application.Optimization = True
  7. ' 设置物件名字,以供CQL查询
  8. For Each s In box
  9. s.Name = "Container"
  10. Next s
  11. Application.Optimization = False
  12. ActiveWindow.Refresh: Application.Refresh
  13. MsgBox "标记容器盒子" & vbNewLine & "名字: Container"
  14. End Function
  15. ' ② 删除容器盒子边界外面的物件 ③④
  16. Public Function Remove_OutsideBox()
  17. Dim s As Shape
  18. Dim ssr As ShapeRange, box As ShapeRange
  19. Dim rmsr As New ShapeRange
  20. Dim x As Double, y As Double
  21. Set ssr = ActiveSelectionRange
  22. ' CQL查找容器盒物件
  23. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  24. ssr.RemoveRange box
  25. If box.Count = 0 Then Exit Function
  26. ActiveDocument.Unit = cdrMillimeter
  27. For Each s In ssr
  28. x = s.CenterX: y = s.CenterY
  29. If box(1).IsOnShape(x, y) = cdrOutsideShape Then rmsr.Add s
  30. Next s
  31. rmsr.Delete
  32. End Function
  33. Public Function Remove_OnMargin()
  34. Dim s As Shape
  35. Dim ssr As ShapeRange, box As ShapeRange
  36. Dim rmsr As New ShapeRange
  37. Dim x As Double, y As Double
  38. Set ssr = ActiveSelectionRange
  39. ' CQL查找容器盒物件
  40. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  41. ssr.RemoveRange box
  42. If box.Count = 0 Then Exit Function
  43. ActiveDocument.Unit = cdrMillimeter
  44. For Each s In ssr
  45. x = s.CenterX: y = s.CenterY
  46. If box(1).IsOnShape(x, y) = cdrOnMarginOfShape Then rmsr.Add s
  47. Next s
  48. rmsr.Delete
  49. End Function
  50. Public Function Select_OutsideBox()
  51. Dim s As Shape
  52. Dim ssr As ShapeRange, box As ShapeRange
  53. Dim SelSr As New ShapeRange
  54. Dim x As Double, y As Double, radius
  55. Set ssr = ActiveSelectionRange
  56. ' CQL查找容器盒物件
  57. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  58. ssr.RemoveRange box
  59. If box.Count = 0 Then Exit Function
  60. ActiveDocument.Unit = cdrMillimeter
  61. For Each s In ssr
  62. x = s.CenterX: y = s.CenterY
  63. radius = s.SizeWidth / 2
  64. If box(1).IsOnShape(x, y, radius) = cdrOutsideShape Then SelSr.Add s
  65. Next s
  66. ActiveDocument.ClearSelection
  67. SelSr.AddToSelection
  68. End Function
  69. Public Function Select_OnMargin()
  70. Dim s As Shape
  71. Dim ssr As ShapeRange, box As ShapeRange
  72. Dim SelSr As New ShapeRange
  73. Dim x As Double, y As Double, radius
  74. Set ssr = ActiveSelectionRange
  75. ' CQL查找容器盒物件
  76. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  77. ssr.RemoveRange box
  78. If box.Count = 0 Then Exit Function
  79. ActiveDocument.Unit = cdrMillimeter
  80. For Each s In ssr
  81. x = s.CenterX: y = s.CenterY
  82. radius = s.SizeWidth / 2
  83. If box(1).IsOnShape(x, y, radius) = cdrOnMarginOfShape Then SelSr.Add s
  84. Next s
  85. ActiveDocument.ClearSelection
  86. SelSr.AddToSelection
  87. End Function
  88. ' 图片批量置入容器
  89. Public Sub Batch_ToPowerClip()
  90. ActiveDocument.BeginCommandGroup ' 一键撤销返回
  91. Dim s As Shape, ssr As ShapeRange, box As ShapeRange
  92. ' 标记容器,请酌情取消注释
  93. ' Set box = ActiveSelectionRange
  94. ' For Each s In box
  95. ' If s.Type <> cdrBitmapShape Then s.Name = "Container"
  96. ' Next s
  97. Set ssr = Smart_Group(0.5) ' 智能群组容差 0.5mm
  98. Application.Optimization = True
  99. For Each s In ssr
  100. Image_ToPowerClip s
  101. Next s
  102. ActiveDocument.EndCommandGroup
  103. Application.Optimization = False
  104. ActiveWindow.Refresh: Application.Refresh
  105. End Sub
  106. ' 图片置入容器,基本函数
  107. Public Function Image_ToPowerClip(arg As Shape)
  108. Dim box As ShapeRange
  109. Dim ssr As New ShapeRange, rmsr As New ShapeRange
  110. Set ssr = arg.UngroupEx
  111. ' CQL查找容器盒物件
  112. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  113. ssr.RemoveRange box
  114. If box.Count = 0 Then Exit Function
  115. ssr.AddToPowerClip box(1), 0
  116. End Function
  117. Private Function Smart_Group(Optional ByVal tr As Double = 0) As ShapeRange
  118. If 0 = ActiveSelectionRange.Count Then Exit Function
  119. On Error GoTo ErrorHandler
  120. Application.Optimization = True
  121. ActiveDocument.ReferencePoint = cdrBottomLeft
  122. ActiveDocument.Unit = cdrMillimeter
  123. Dim OrigSelection As ShapeRange, sr As New ShapeRange
  124. Dim s1 As Shape, sh As Shape, s As Shape
  125. Dim x As Double, y As Double, w As Double, h As Double
  126. Dim eff1 As Effect
  127. Set OrigSelection = ActiveSelectionRange
  128. '// 遍历物件画矩形
  129. For Each sh In OrigSelection
  130. sh.GetBoundingBox x, y, w, h
  131. If w * h > 4 Then
  132. Set s = ActiveLayer.CreateRectangle2(x - tr, y - tr, w + 2 * tr, h + 2 * tr)
  133. sr.Add s
  134. '// 轴线 创建轮廓处理
  135. ElseIf w * h < 0.3 Then
  136. ' Debug.Print w * h
  137. Set eff1 = sh.CreateContour(cdrContourOutside, 0.5, 1, cdrDirectFountainFillBlend, CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), CreateRGBColor(26, 22, 35), 0, 0, cdrContourSquareCap, cdrContourCornerMiteredOffsetBevel, 15#)
  138. eff1.Separate
  139. End If
  140. Next sh
  141. '// 查找轴线轮廓
  142. ActivePage.Shapes.FindShapes(Query:="@Outline.Color=RGB(26, 22, 35)").CreateSelection
  143. ActivePage.Shapes.FindShapes(Query:="@fill.Color=RGB(26, 22, 35)").AddToSelection
  144. For Each sh In ActiveSelection.Shapes
  145. sr.Add sh
  146. Next sh
  147. '// 新矩形寻找边界,散开,删除刚才画的新矩形
  148. Set s1 = sr.CustomCommand("Boundary", "CreateBoundary")
  149. Set brk1 = s1.BreakApartEx
  150. sr.Delete
  151. '// 矩形边界智能群组, retsr 返回群组 和 删除矩形s
  152. Dim retsr As New ShapeRange, rmsr As New ShapeRange
  153. For Each s In brk1
  154. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, False)
  155. s.Delete
  156. retsr.Add sh.Shapes.All.group
  157. Next
  158. Set Smart_Group = retsr
  159. Application.Optimization = False
  160. ActiveWindow.Refresh: Application.Refresh
  161. Exit Function
  162. ErrorHandler:
  163. Application.Optimization = False
  164. MsgBox "请先选择一些物件来确定群组范围!"
  165. On Error Resume Next
  166. End Function