Tools.bas 13 KB

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