Tools.bas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. Attribute VB_Name = "Tools"
  2. Public Function 分分合合()
  3. 拼版裁切线.arrange
  4. CQL查找相同.CQLline_CM100
  5. 拼版裁切线.Cut_lines
  6. Dim s As Shape
  7. Set s = ActivePage.SelectShapesFromRectangle(ActivePage.LeftX, ActivePage.TopY, ActivePage.RightX, ActivePage.BottomY, True)
  8. 自动中线色阶条.Auto_ColorMark
  9. End Function
  10. Public Function 傻瓜火车排列()
  11. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  12. Dim ssr As ShapeRange, s As Shape
  13. Dim cnt As Integer
  14. Set ssr = ActiveSelectionRange
  15. cnt = 1
  16. #If VBA7 Then
  17. ' ssr.sort " @shape1.top>@shape2.top"
  18. ssr.Sort " @shape1.left<@shape2.left"
  19. #Else
  20. ' X4 不支持 ShapeRange.sort
  21. #End If
  22. ActiveDocument.ReferencePoint = cdrTopLeft
  23. For Each s In ssr
  24. '' 底对齐 If cnt > 1 Then s.SetPosition ssr(cnt - 1).RightX, ssr(cnt - 1).BottomY
  25. '' 改成顶对齐 2022-08-10
  26. ActiveDocument.ReferencePoint = cdrTopLeft + cdrBottomTop
  27. If cnt > 1 Then s.SetPosition ssr(cnt - 1).RightX, ssr(cnt - 1).TopY
  28. cnt = cnt + 1
  29. Next s
  30. ActiveDocument.EndCommandGroup
  31. Application.Optimization = False
  32. ActiveWindow.Refresh: Application.Refresh
  33. End Function
  34. Public Function 傻瓜阶梯排列()
  35. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  36. Dim ssr As ShapeRange, s As Shape
  37. Dim cnt As Integer
  38. Set ssr = ActiveSelectionRange
  39. cnt = 1
  40. #If VBA7 Then
  41. ssr.Sort " @shape1.top>@shape2.top"
  42. ' ssr.sort " @shape1.left<@shape2.left"
  43. #Else
  44. ' X4 不支持 ShapeRange.sort
  45. #End If
  46. ActiveDocument.ReferencePoint = cdrTopLeft
  47. For Each s In ssr
  48. If cnt > 1 Then s.SetPosition ssr(cnt - 1).LeftX, ssr(cnt - 1).BottomY
  49. cnt = cnt + 1
  50. Next s
  51. ActiveDocument.EndCommandGroup
  52. Application.Optimization = False
  53. ActiveWindow.Refresh: Application.Refresh
  54. End Function
  55. '// 文本转曲线
  56. Public Function TextShape_ConvertToCurves()
  57. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  58. Dim s As Shape, cnt As Long
  59. For Each s In API.FindAllShapes.Shapes.FindShapes(, cdrTextShape)
  60. s.ConvertToCurves
  61. cnt = cnt + 1
  62. Next s
  63. MsgBox "转曲物件统计: " & cnt, , "文本转曲线"
  64. ActiveDocument.EndCommandGroup
  65. Application.Optimization = False
  66. ActiveWindow.Refresh: Application.Refresh
  67. End Function
  68. '' 复制物件
  69. Public Function copy_shape()
  70. Dim OrigSelection As ShapeRange
  71. Set OrigSelection = ActiveSelectionRange
  72. OrigSelection.Copy
  73. End Function
  74. '' 旋转物件角度
  75. Public Function Rotate_Shapes(n As Double)
  76. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  77. ActiveDocument.Unit = cdrMillimeter
  78. Dim sh As Shape, shs As Shapes
  79. Set shs = ActiveSelection.Shapes
  80. Dim s As String, size As String
  81. For Each sh In shs
  82. sh.Rotate n
  83. Next sh
  84. ActiveDocument.EndCommandGroup
  85. Application.Optimization = False
  86. ActiveWindow.Refresh: Application.Refresh
  87. End Function
  88. '' 得到物件尺寸
  89. Public Function get_shape_size(ByRef sx As Double, ByRef sy As Double)
  90. ActiveDocument.Unit = cdrMillimeter
  91. Dim sh As ShapeRange
  92. Set sh = ActiveSelectionRange
  93. sx = sh.SizeWidth
  94. sy = sh.SizeHeight
  95. sx = Int(sx * 100 + 0.5) / 100
  96. sy = Int(sy * 100 + 0.5) / 100
  97. End Function
  98. '' 批量设置物件尺寸
  99. Public Function Set_Shapes_size(ByRef sx As Double, ByRef sy As Double)
  100. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  101. ActiveDocument.Unit = cdrMillimeter
  102. ActiveDocument.ReferencePoint = cdrCenter
  103. Dim sh As Shape, shs As Shapes
  104. Set shs = ActiveSelection.Shapes
  105. Dim s As String, size As String
  106. For Each sh In shs
  107. sh.SizeWidth = sx
  108. sh.SizeHeight = sy
  109. Next sh
  110. ActiveDocument.EndCommandGroup
  111. Application.Optimization = False
  112. ActiveWindow.Refresh: Application.Refresh
  113. End Function
  114. Public Function 尺寸取整()
  115. If 0 = ActiveSelectionRange.Count Then Exit Function
  116. ActiveDocument.Unit = cdrMillimeter
  117. Dim sh As Shape, shs As Shapes
  118. Set shs = ActiveSelection.Shapes
  119. Dim s As String, size As String
  120. For Each sh In shs
  121. size = Int(sh.SizeWidth + 0.5) & "x" & Int(sh.SizeHeight + 0.5) & "mm"
  122. sh.SetSize Int(sh.SizeWidth + 0.5), Int(sh.SizeHeight + 0.5)
  123. s = s & size & vbNewLine
  124. Next sh
  125. MsgBox "物件尺寸信息到剪贴板" & vbNewLine & s & vbNewLine
  126. API.WriteClipBoard s
  127. End Function
  128. Public Function 居中页面()
  129. If 0 = ActiveSelectionRange.Count Then Exit Function
  130. ' 实践应用: 选择物件群组,页面设置物件大小,物件页面居中
  131. ActiveDocument.Unit = cdrMillimeter
  132. Dim OrigSelection As ShapeRange, sh As Shape
  133. Set OrigSelection = ActiveSelectionRange
  134. Set sh = OrigSelection.Group
  135. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  136. #If VBA7 Then
  137. ActiveDocument.ClearSelection
  138. sh.AddToSelection
  139. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  140. #Else
  141. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  142. #End If
  143. End Function
  144. Public Function Python脚本整理尺寸()
  145. mypy = Path & "GMS\262235.xyz\整理尺寸.py"
  146. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  147. Shell cmd_line
  148. End Function
  149. Public Function Python提取条码数字()
  150. mypy = Path & "GMS\262235.xyz\提取条码数字.py"
  151. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  152. Shell cmd_line
  153. End Function
  154. Public Function Python二维码QRCode()
  155. mypy = Path & "GMS\262235.xyz\二维码QRCode.py"
  156. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  157. Shell cmd_line
  158. End Function
  159. '' QRCode二维码制作
  160. Public Function QRCode_replace()
  161. On Error GoTo ErrorHandler
  162. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  163. Dim image_path As String
  164. image_path = API.GetClipBoardString
  165. ActiveDocument.ReferencePoint = cdrCenter
  166. Dim sh As Shape, shs As Shapes, cs As Shape
  167. Dim x As Double, y As Double
  168. Set shs = ActiveSelection.Shapes
  169. cnt = 0
  170. For Each sh In shs
  171. If cnt = 0 Then
  172. ActiveDocument.ClearSelection
  173. ActiveLayer.Import image_path
  174. Set sc = ActiveSelection
  175. cnt = 1
  176. Else
  177. sc.Duplicate 0, 0
  178. End If
  179. sh.GetPosition x, y
  180. sc.SetPosition x, y
  181. sh.GetSize x, y
  182. sc.SetSize x, y
  183. sh.Delete
  184. Next sh
  185. '// 代码操作结束恢复窗口刷新
  186. ActiveDocument.EndCommandGroup
  187. Application.Optimization = False
  188. ActiveWindow.Refresh: Application.Refresh
  189. Exit Function
  190. ErrorHandler:
  191. Application.Optimization = False
  192. On Error Resume Next
  193. End Function
  194. '' QRCode二维码转矢量图
  195. Public Function QRCode_to_Vector()
  196. On Error GoTo ErrorHandler
  197. Set sr = ActiveSelectionRange
  198. With sr(1).Bitmap.Trace(cdrTraceHighQualityImage)
  199. .TraceType = cdrTraceHighQualityImage
  200. .Smoothing = 50 '数值小则平滑,数值大则细节多
  201. .RemoveBackground = False
  202. .DeleteOriginalObject = True
  203. .Finish
  204. End With
  205. Exit Function
  206. ErrorHandler:
  207. On Error Resume Next
  208. End Function
  209. '''//// 选择多物件,组合然后拆分线段,为角线爬虫准备 ////'''
  210. Public Function Split_Segment()
  211. On Error GoTo ErrorHandler
  212. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  213. Dim ssr As ShapeRange
  214. Set ssr = ActiveSelectionRange
  215. Dim s As Shape
  216. Dim nr As NodeRange
  217. Dim nd As Node
  218. Set s = ssr.UngroupAllEx.Combine
  219. Set nr = s.Curve.Nodes.All
  220. nr.BreakApart
  221. s.BreakApartEx
  222. ' For Each nd In nr
  223. ' nd.BreakApart
  224. ' Next nd
  225. ActiveDocument.EndCommandGroup
  226. Application.Optimization = False
  227. ActiveWindow.Refresh: Application.Refresh
  228. Exit Function
  229. ErrorHandler:
  230. Application.Optimization = False
  231. On Error Resume Next
  232. End Function
  233. '''//// 标记画框 支持容差 ////'''
  234. Public Function Mark_CreateRectangle(expand As Boolean)
  235. On Error GoTo ErrorHandler
  236. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  237. ActiveDocument.Unit = cdrMillimeter
  238. ActiveDocument.ReferencePoint = cdrBottomLeft
  239. Dim ssr As ShapeRange
  240. Set ssr = ActiveSelectionRange
  241. Dim sh As Shape
  242. Dim tr As Double
  243. tr = 0
  244. If GlobalUserData.Exists("Tolerance", 1) Then
  245. tr = Val(GlobalUserData("Tolerance", 1))
  246. End If
  247. For Each sh In ssr
  248. If expand = False Then
  249. mark_shape sh
  250. Else
  251. mark_shape_expand sh, tr
  252. End If
  253. Next sh
  254. ActiveDocument.EndCommandGroup
  255. Application.Optimization = False
  256. ActiveWindow.Refresh: Application.Refresh
  257. Exit Function
  258. ErrorHandler:
  259. Application.Optimization = False
  260. On Error Resume Next
  261. End Function
  262. Private Function mark_shape_expand(sh As Shape, tr As Double)
  263. Dim s As Shape
  264. Dim x As Double, y As Double, w As Double, h As Double, r As Double
  265. sh.GetBoundingBox x, y, w, h
  266. x = x - tr: y = y - tr: w = w + 2 * tr: h = h + 2 * tr
  267. r = Max(w, h) / Min(w, h) / 30 * Math.Sqr(w * h)
  268. If w < h Then
  269. Set s = ActiveLayer.CreateRectangle2(x - r, y, w + 2 * r, h)
  270. Else
  271. Set s = ActiveLayer.CreateRectangle2(x, y - r, w, h + 2 * r)
  272. End If
  273. s.Outline.SetProperties Color:=CreateRGBColor(0, 255, 0)
  274. End Function
  275. Public Function Create_Tolerance()
  276. Dim text As String
  277. If GlobalUserData.Exists("Tolerance", 1) Then
  278. text = GlobalUserData("Tolerance", 1)
  279. End If
  280. text = InputBox("请输入容差值 0 --> 99", "容差值(mm)", text)
  281. If text = "" Then Exit Function
  282. GlobalUserData("Tolerance", 1) = text
  283. End Function
  284. Private Function mark_shape(sh As Shape)
  285. Dim s As Shape
  286. Dim x As Double, y As Double, w As Double, h As Double
  287. sh.GetBoundingBox x, y, w, h
  288. Set s = ActiveLayer.CreateRectangle2(x, y, w, h)
  289. s.Outline.SetProperties Color:=CreateRGBColor(0, 255, 0)
  290. End Function
  291. Private Function Max(ByVal a, ByVal b)
  292. If a < b Then
  293. a = b
  294. End If
  295. Max = a
  296. End Function
  297. Private Function Min(ByVal a, ByVal b)
  298. If a > b Then
  299. a = b
  300. End If
  301. Min = a
  302. End Function
  303. '''//// 批量组合合并 ////'''
  304. Public Function Batch_Combine()
  305. On Error GoTo ErrorHandler
  306. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  307. Dim ssr As ShapeRange
  308. Set ssr = ActiveSelectionRange
  309. Dim sh As Shape
  310. For Each sh In ssr
  311. sh.UngroupAllEx.Combine
  312. Next sh
  313. ActiveDocument.EndCommandGroup
  314. Application.Optimization = False
  315. ActiveWindow.Refresh: Application.Refresh
  316. Exit Function
  317. ErrorHandler:
  318. Application.Optimization = False
  319. On Error Resume Next
  320. End Function
  321. '''//// 一键拆开多行组合的文字字符 ////''' ''' 本功能由群友半缘君赞助发行 '''
  322. Public Function Take_Apart_Character()
  323. On Error GoTo ErrorHandler
  324. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  325. ActiveDocument.Unit = cdrMillimeter
  326. ActiveDocument.ReferencePoint = cdrBottomLeft
  327. Dim ssr As ShapeRange
  328. Set ssr = ActiveSelectionRange
  329. Dim s1 As Shape, sh As Shape, s As Shape
  330. Dim tr As Double
  331. ' 记忆选择范围
  332. Dim x As Double, y As Double, w As Double, h As Double
  333. ssr.GetBoundingBox x, y, w, h
  334. Set s1 = ActiveLayer.CreateRectangle2(x, y, w, h)
  335. ' 解散群组,先组合,再散开
  336. Set s = ssr.UngroupAllEx.Combine
  337. Set ssr = s.BreakApartEx
  338. ' 读取容差值
  339. tr = 0
  340. If GlobalUserData.Exists("Tolerance", 1) Then
  341. tr = Val(GlobalUserData("Tolerance", 1))
  342. End If
  343. ' 标记画框,选择标记框
  344. For Each sh In ssr
  345. mark_shape_expand sh, tr
  346. Next sh
  347. Set ssr = ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(0, 255, 0))")
  348. ActiveDocument.ClearSelection
  349. ssr.AddToSelection
  350. ' 调用 智能群组 后删除标记画框
  351. 智能群组和查找.智能群组
  352. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  353. ssr.Delete
  354. Set sh = ActivePage.SelectShapesFromRectangle(s1.LeftX, s1.TopY, s1.RightX, s1.BottomY, False)
  355. ' sh.Shapes.All.Group
  356. s1.Delete
  357. ' 通过s1矩形范围选择群组后合并组合
  358. For Each s In sh.Shapes
  359. s.UngroupAllEx.Combine
  360. Next s
  361. ActiveDocument.EndCommandGroup
  362. Application.Optimization = False
  363. ActiveWindow.Refresh: Application.Refresh
  364. Exit Function
  365. ErrorHandler:
  366. Application.Optimization = False
  367. On Error Resume Next
  368. End Function