Tools.bas 13 KB

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