1
1

Container.bas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Attribute VB_Name = "Container"
  2. ' ① 标记容器盒子
  3. Public Function SetBoxName()
  4. API.BeginOpt "标记容器盒子"
  5. Dim box As ShapeRange, S As Shape
  6. Set box = ActiveSelectionRange
  7. ' 设置物件名字,以供CQL查询
  8. For Each S In box
  9. S.Name = "Container"
  10. Next S
  11. API.EndOpt
  12. MsgBox "标记容器盒子" & vbNewLine & "名字: Container"
  13. End Function
  14. ' 图片批量置入容器
  15. Public Sub Batch_ToPowerClip()
  16. API.BeginOpt "批量置入容器"
  17. Dim S As Shape, ssr As ShapeRange, box As ShapeRange
  18. Set ssr = Smart_Group(0.5) ' 智能群组容差 0.5mm
  19. For Each S In ssr
  20. Image_ToPowerClip S
  21. Next S
  22. API.EndOpt
  23. End Sub
  24. ' 图片置入容器,基本函数
  25. Public Function Image_ToPowerClip(arg As Shape)
  26. Dim box As ShapeRange
  27. Dim ssr As New ShapeRange, rmsr As New ShapeRange
  28. Set ssr = arg.UngroupEx
  29. ' CQL查找容器盒物件
  30. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  31. ssr.RemoveRange box
  32. If box.Count = 0 Then Exit Function
  33. box.SetOutlineProperties Width:=0, Color:=Nothing
  34. ssr.AddToPowerClip box(1), 0
  35. box(1).Name = "powerclip_ok"
  36. End Function
  37. ' 图片OneKey置入容器
  38. Public Sub OneKey_ToPowerClip()
  39. API.BeginOpt "图片OneKey置入容器"
  40. Dim S As Shape, ssr As ShapeRange, box As ShapeRange
  41. ' 标记容器,设置透明
  42. Set box = ActiveSelectionRange
  43. For Each S In box
  44. If S.Type <> cdrBitmapShape Then S.Name = "Container"
  45. Next S
  46. Set ssr = Smart_Group(0.5) ' 智能群组容差 0.5mm
  47. Application.Optimization = True
  48. For Each S In ssr
  49. Image_ToPowerClip S
  50. Next S
  51. API.EndOpt
  52. End Sub
  53. ' ② 删除容器盒子边界外面的物件 ③④
  54. Public Function Remove_OutsideBox(radius As Double)
  55. API.BeginOpt "删除容器盒子边界外面的物"
  56. On Error GoTo ErrorHandler
  57. Dim S As Shape, bc As Shape
  58. Dim ssr As ShapeRange, box As ShapeRange
  59. Dim rmsr As New ShapeRange
  60. Dim x As Double, Y As Double
  61. Set ssr = ActiveSelectionRange
  62. ' CQL查找容器盒物件
  63. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  64. ssr.RemoveRange box
  65. If box.Count = 0 Then GoTo ErrorHandler
  66. Set bc = box(1).Duplicate(0, 0)
  67. If bc.Type = cdrTextShape Then bc.ConvertToCurves
  68. For Each S In ssr
  69. x = S.CenterX: Y = S.CenterY
  70. If bc.IsOnShape(x, Y, radius) = cdrOutsideShape Then rmsr.Add S
  71. Next S
  72. rmsr.Add bc: rmsr.Delete: API.EndOpt
  73. Exit Function
  74. ErrorHandler:
  75. Application.Optimization = False
  76. On Error Resume Next
  77. End Function
  78. Public Function Select_OutsideBox(radius As Double)
  79. On Error GoTo ErrorHandler
  80. API.BeginOpt "选择容器外面对象"
  81. Dim S As Shape, bc As Shape
  82. Dim ssr As ShapeRange, box As ShapeRange
  83. Dim SelSr As New ShapeRange
  84. Dim x As Double, Y As Double
  85. Set ssr = ActiveSelectionRange
  86. ' CQL查找容器盒物件
  87. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  88. ssr.RemoveRange box
  89. If box.Count = 0 Then GoTo ErrorHandler
  90. Set bc = box(1).Duplicate(0, 0)
  91. If bc.Type = cdrTextShape Then bc.ConvertToCurves
  92. ActiveDocument.unit = cdrMillimeter
  93. For Each S In ssr
  94. x = S.CenterX: Y = S.CenterY
  95. If bc.IsOnShape(x, Y, S.SizeWidth / 2 * radius) = cdrOutsideShape Then SelSr.Add S
  96. Next S
  97. ActiveDocument.ClearSelection
  98. bc.Delete: SelSr.AddToSelection: API.EndOpt
  99. Exit Function
  100. ErrorHandler:
  101. Application.Optimization = False
  102. End Function
  103. Public Function Select_by_BlendGroup(radius As Double)
  104. On Error GoTo ErrorHandler
  105. API.BeginOpt "使用调和群组选择"
  106. Dim S As Shape, bc As Shape
  107. Dim ssr As ShapeRange, box As ShapeRange, gp As ShapeRange
  108. Dim SelSr As New ShapeRange
  109. Dim x As Double, Y As Double
  110. Set ssr = ActiveSelectionRange
  111. ' CQL查找容器盒物件
  112. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  113. ssr.RemoveRange box
  114. If box.Count = 0 Then GoTo ErrorHandler
  115. Set gp = box.Duplicate(0, 0).UngroupAllEx
  116. Set bc = gp.BreakApartEx.UngroupAllEx.Combine
  117. ActiveDocument.unit = cdrMillimeter
  118. For Each S In ssr
  119. x = S.CenterX: Y = S.CenterY
  120. If bc.IsOnShape(x, Y, S.SizeWidth / 2 * radius) = cdrOnMarginOfShape Then SelSr.Add S
  121. Next S
  122. ActiveDocument.ClearSelection
  123. bc.Delete: SelSr.AddToSelection: API.EndOpt
  124. Exit Function
  125. ErrorHandler:
  126. Application.Optimization = False
  127. On Error Resume Next
  128. End Function
  129. Public Function Select_OnMargin(radius As Double)
  130. On Error GoTo ErrorHandler
  131. API.BeginOpt "选择容器边界对象"
  132. Dim S As Shape, bc As Shape
  133. Dim ssr As ShapeRange, box As ShapeRange
  134. Dim SelSr As New ShapeRange
  135. Dim x As Double, Y As Double
  136. Set ssr = ActiveSelectionRange
  137. ' CQL查找容器盒物件
  138. Set box = ssr.Shapes.FindShapes(Query:="@name ='Container'")
  139. ssr.RemoveRange box
  140. If box.Count = 0 Then GoTo ErrorHandler
  141. Set bc = box(1).Duplicate(0, 0)
  142. If bc.Type = cdrTextShape Then bc.ConvertToCurves ' 如果是文本转曲
  143. ActiveDocument.unit = cdrMillimeter
  144. For Each S In ssr
  145. x = S.CenterX: Y = S.CenterY
  146. If bc.IsOnShape(x, Y, S.SizeWidth / 2 * radius) = cdrOnMarginOfShape Then SelSr.Add S
  147. Next S
  148. ActiveDocument.ClearSelection
  149. bc.Delete: SelSr.AddToSelection: API.EndOpt
  150. Exit Function
  151. ErrorHandler:
  152. Application.Optimization = False
  153. On Error Resume Next
  154. End Function
  155. Private Function Smart_Group(Optional ByVal tr As Double = 0) As ShapeRange
  156. If 0 = ActiveSelectionRange.Count Then Exit Function
  157. On Error GoTo ErrorHandler
  158. Application.Optimization = True
  159. ActiveDocument.ReferencePoint = cdrBottomLeft
  160. ActiveDocument.unit = cdrMillimeter
  161. Dim OrigSelection As ShapeRange, sr As New ShapeRange
  162. Dim s1 As Shape, sh As Shape, S As Shape
  163. Dim x As Double, Y As Double, w As Double, h As Double
  164. Dim eff1 As Effect
  165. Set OrigSelection = ActiveSelectionRange
  166. '// 遍历物件画矩形
  167. For Each sh In OrigSelection
  168. sh.GetBoundingBox x, Y, w, h
  169. If w * h > 4 Then
  170. Set S = ActiveLayer.CreateRectangle2(x - tr, Y - tr, w + 2 * tr, h + 2 * tr)
  171. sr.Add S
  172. '// 轴线 创建轮廓处理
  173. ElseIf w * h < 0.3 Then
  174. ' Debug.Print w * h
  175. 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#)
  176. eff1.Separate
  177. End If
  178. Next sh
  179. '// 查找轴线轮廓
  180. ActivePage.Shapes.FindShapes(Query:="@Outline.Color=RGB(26, 22, 35)").CreateSelection
  181. ActivePage.Shapes.FindShapes(Query:="@fill.Color=RGB(26, 22, 35)").AddToSelection
  182. For Each sh In ActiveSelection.Shapes
  183. sr.Add sh
  184. Next sh
  185. '// 新矩形寻找边界,散开,删除刚才画的新矩形
  186. Set s1 = sr.CustomCommand("Boundary", "CreateBoundary")
  187. Set brk1 = s1.BreakApartEx
  188. sr.Delete
  189. '// 矩形边界智能群组, retsr 返回群组 和 删除矩形s
  190. Dim retsr As New ShapeRange, rmsr As New ShapeRange
  191. For Each S In brk1
  192. Set sh = ActivePage.SelectShapesFromRectangle(S.LeftX, S.TopY, S.RightX, S.BottomY, False)
  193. S.Delete
  194. retsr.Add sh.Shapes.All.group
  195. Next
  196. Set Smart_Group = retsr
  197. Application.Optimization = False
  198. ActiveWindow.Refresh: Application.Refresh
  199. Exit Function
  200. ErrorHandler:
  201. Application.Optimization = False
  202. MsgBox "请先选择一些物件来确定群组范围!"
  203. On Error Resume Next
  204. End Function