Tools.bas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. On Error GoTo ErrorHandler
  61. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  62. Dim s As Shape, cnt As Long
  63. For Each s In API.FindAllShapes.Shapes.FindShapes(, cdrTextShape)
  64. s.ConvertToCurves
  65. cnt = cnt + 1
  66. Next s
  67. MsgBox "转曲物件统计: " & cnt, , "文本转曲线"
  68. ActiveDocument.EndCommandGroup
  69. Application.Optimization = False
  70. ActiveWindow.Refresh: Application.Refresh
  71. Exit Function
  72. ErrorHandler:
  73. Application.Optimization = False
  74. On Error Resume Next
  75. End Function
  76. '' 复制物件
  77. Public Function copy_shape()
  78. Dim OrigSelection As ShapeRange
  79. Set OrigSelection = ActiveSelectionRange
  80. OrigSelection.Copy
  81. End Function
  82. '' 旋转物件角度
  83. Public Function Rotate_Shapes(n As Double)
  84. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  85. ActiveDocument.Unit = cdrMillimeter
  86. Dim sh As Shape, shs As Shapes
  87. Set shs = ActiveSelection.Shapes
  88. Dim s As String, size As String
  89. For Each sh In shs
  90. sh.Rotate n
  91. Next sh
  92. ActiveDocument.EndCommandGroup
  93. Application.Optimization = False
  94. ActiveWindow.Refresh: Application.Refresh
  95. End Function
  96. '' 得到物件尺寸
  97. Public Function get_shape_size(ByRef sx As Double, ByRef sy As Double)
  98. ActiveDocument.Unit = cdrMillimeter
  99. Dim sh As ShapeRange
  100. Set sh = ActiveSelectionRange
  101. sx = sh.SizeWidth
  102. sy = sh.SizeHeight
  103. sx = Int(sx * 100 + 0.5) / 100
  104. sy = Int(sy * 100 + 0.5) / 100
  105. End Function
  106. '' 批量设置物件尺寸
  107. Public Function Set_Shapes_size(ByRef sx As Double, ByRef sy As Double)
  108. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  109. ActiveDocument.Unit = cdrMillimeter
  110. ActiveDocument.ReferencePoint = cdrCenter
  111. Dim sh As Shape, shs As Shapes
  112. Set shs = ActiveSelection.Shapes
  113. Dim s As String, size As String
  114. For Each sh In shs
  115. sh.SizeWidth = sx
  116. sh.SizeHeight = sy
  117. Next sh
  118. ActiveDocument.EndCommandGroup
  119. Application.Optimization = False
  120. ActiveWindow.Refresh: Application.Refresh
  121. End Function
  122. Public Function 尺寸取整()
  123. If 0 = ActiveSelectionRange.Count Then Exit Function
  124. ActiveDocument.Unit = cdrMillimeter
  125. ' 修改变形尺寸基准
  126. ActiveDocument.ReferencePoint = cdrCenter
  127. Dim sh As Shape, shs As Shapes
  128. Set shs = ActiveSelection.Shapes
  129. Dim s As String, size As String
  130. For Each sh In shs
  131. size = Int(sh.SizeWidth + 0.5) & "x" & Int(sh.SizeHeight + 0.5) & "mm"
  132. sh.SetSize Int(sh.SizeWidth + 0.5), Int(sh.SizeHeight + 0.5)
  133. s = s & size & vbNewLine
  134. Next sh
  135. MsgBox "物件尺寸信息到剪贴板" & vbNewLine & s & vbNewLine
  136. API.WriteClipBoard s
  137. End Function
  138. Public Function 居中页面()
  139. If 0 = ActiveSelectionRange.Count Then Exit Function
  140. ' 实践应用: 选择物件群组,页面设置物件大小,物件页面居中
  141. ActiveDocument.Unit = cdrMillimeter
  142. Dim OrigSelection As ShapeRange, sh As Shape
  143. Set OrigSelection = ActiveSelectionRange
  144. Set sh = OrigSelection.Group
  145. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  146. #If VBA7 Then
  147. ActiveDocument.ClearSelection
  148. sh.AddToSelection
  149. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  150. #Else
  151. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  152. #End If
  153. End Function
  154. '''/// 使用Python脚本 整理尺寸 提取条码数字 建立二维码 位图转文本 ///'''
  155. Public Function Python_Organize_Size()
  156. mypy = Path & "GMS\262235.xyz\Organize_Size.py"
  157. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  158. Shell cmd_line
  159. End Function
  160. Public Function Python_Get_Barcode_Number()
  161. mypy = Path & "GMS\262235.xyz\Get_Barcode_Number.py"
  162. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  163. Shell cmd_line
  164. End Function
  165. Public Function Python_BITMAP()
  166. mypy = Path & "GMS\262235.xyz\BITMAP.py"
  167. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  168. Shell cmd_line
  169. End Function
  170. Public Function Python_BITMAP2()
  171. Bitmap = "C:\TSP\BITMAP.exe"
  172. Shell Bitmap
  173. End Function
  174. Public Function Python_Make_QRCode()
  175. mypy = Path & "GMS\262235.xyz\Make_QRCode.py"
  176. cmd_line = "pythonw " & Chr(34) & mypy & Chr(34)
  177. Shell cmd_line
  178. End Function
  179. '' QRCode二维码制作
  180. Public Function QRCode_replace()
  181. On Error GoTo ErrorHandler
  182. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  183. Dim image_path As String
  184. image_path = API.GetClipBoardString
  185. ActiveDocument.ReferencePoint = cdrCenter
  186. Dim sh As Shape, shs As Shapes, cs As Shape
  187. Dim x As Double, Y As Double
  188. Set shs = ActiveSelection.Shapes
  189. cnt = 0
  190. For Each sh In shs
  191. If cnt = 0 Then
  192. ActiveDocument.ClearSelection
  193. ActiveLayer.Import image_path
  194. Set sc = ActiveSelection
  195. cnt = 1
  196. Else
  197. sc.Duplicate 0, 0
  198. End If
  199. sh.GetPosition x, Y
  200. sc.SetPosition x, Y
  201. sh.GetSize x, Y
  202. sc.SetSize x, Y
  203. sh.Delete
  204. Next sh
  205. '// 代码操作结束恢复窗口刷新
  206. ActiveDocument.EndCommandGroup
  207. Application.Optimization = False
  208. ActiveWindow.Refresh: Application.Refresh
  209. Exit Function
  210. ErrorHandler:
  211. Application.Optimization = False
  212. On Error Resume Next
  213. End Function
  214. '' QRCode二维码转矢量图
  215. Public Function QRCode_to_Vector()
  216. On Error GoTo ErrorHandler
  217. Set sr = ActiveSelectionRange
  218. With sr(1).Bitmap.Trace(cdrTraceHighQualityImage)
  219. .TraceType = cdrTraceHighQualityImage
  220. .Smoothing = 50 '数值小则平滑,数值大则细节多
  221. .RemoveBackground = False
  222. .DeleteOriginalObject = True
  223. .Finish
  224. End With
  225. Exit Function
  226. ErrorHandler:
  227. On Error Resume Next
  228. End Function
  229. '''//// 选择多物件,组合然后拆分线段,为角线爬虫准备 ////'''
  230. Public Function Split_Segment()
  231. On Error GoTo ErrorHandler
  232. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  233. Dim ssr As ShapeRange
  234. Set ssr = ActiveSelectionRange
  235. Dim s As Shape
  236. Dim nr As NodeRange
  237. Dim nd As Node
  238. Set s = ssr.UngroupAllEx.Combine
  239. Set nr = s.Curve.Nodes.All
  240. nr.BreakApart
  241. s.BreakApartEx
  242. ' For Each nd In nr
  243. ' nd.BreakApart
  244. ' Next nd
  245. ActiveDocument.EndCommandGroup
  246. Application.Optimization = False
  247. ActiveWindow.Refresh: Application.Refresh
  248. Exit Function
  249. ErrorHandler:
  250. Application.Optimization = False
  251. On Error Resume Next
  252. End Function
  253. '''//// 标记画框 支持容差 ////'''
  254. Public Function Mark_CreateRectangle(expand As Boolean)
  255. On Error GoTo ErrorHandler
  256. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  257. ActiveDocument.Unit = cdrMillimeter
  258. ActiveDocument.ReferencePoint = cdrBottomLeft
  259. Dim ssr As ShapeRange
  260. Set ssr = ActiveSelectionRange
  261. Dim sh As Shape
  262. Dim tr As Double
  263. tr = 0
  264. If GlobalUserData.Exists("Tolerance", 1) Then
  265. tr = Val(GlobalUserData("Tolerance", 1))
  266. End If
  267. For Each sh In ssr
  268. If expand = False Then
  269. mark_shape sh
  270. Else
  271. mark_shape_expand sh, tr
  272. End If
  273. Next sh
  274. ActiveDocument.EndCommandGroup
  275. Application.Optimization = False
  276. ActiveWindow.Refresh: Application.Refresh
  277. Exit Function
  278. ErrorHandler:
  279. Application.Optimization = False
  280. On Error Resume Next
  281. End Function
  282. Private Function mark_shape_expand(sh As Shape, tr As Double)
  283. Dim s As Shape
  284. Dim x As Double, Y As Double, w As Double, h As Double, r As Double
  285. sh.GetBoundingBox x, Y, w, h
  286. x = x - tr: Y = Y - tr: w = w + 2 * tr: h = h + 2 * tr
  287. r = Max(w, h) / Min(w, h) / 30 * Math.Sqr(w * h)
  288. If w < h Then
  289. Set s = ActiveLayer.CreateRectangle2(x - r, Y, w + 2 * r, h)
  290. Else
  291. Set s = ActiveLayer.CreateRectangle2(x, Y - r, w, h + 2 * r)
  292. End If
  293. s.Outline.SetProperties Color:=CreateRGBColor(0, 255, 0)
  294. End Function
  295. Private Function mark_shape(sh As Shape)
  296. Dim s As Shape
  297. Dim x As Double, Y As Double, w As Double, h As Double
  298. sh.GetBoundingBox x, Y, w, h, True
  299. Set s = ActiveLayer.CreateRectangle2(x, Y, w, h)
  300. s.Outline.SetProperties Color:=CreateRGBColor(0, 255, 0)
  301. End Function
  302. Private Function Max(ByVal a, ByVal B)
  303. If a < B Then
  304. a = B
  305. End If
  306. Max = a
  307. End Function
  308. Private Function Min(ByVal a, ByVal B)
  309. If a > B Then
  310. a = B
  311. End If
  312. Min = a
  313. End Function
  314. '''//// 批量组合合并 ////'''
  315. Public Function Batch_Combine()
  316. On Error GoTo ErrorHandler
  317. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  318. Dim ssr As ShapeRange
  319. Set ssr = ActiveSelectionRange
  320. Dim sh As Shape
  321. For Each sh In ssr
  322. sh.UngroupAllEx.Combine
  323. Next sh
  324. ActiveDocument.EndCommandGroup
  325. Application.Optimization = False
  326. ActiveWindow.Refresh: Application.Refresh
  327. Exit Function
  328. ErrorHandler:
  329. Application.Optimization = False
  330. On Error Resume Next
  331. End Function
  332. '''//// 一键拆开多行组合的文字字符 ////''' ''' 本功能由群友半缘君赞助发行 '''
  333. Public Function Take_Apart_Character()
  334. On Error GoTo ErrorHandler
  335. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  336. ActiveDocument.Unit = cdrMillimeter
  337. ActiveDocument.ReferencePoint = cdrBottomLeft
  338. Dim ssr As ShapeRange
  339. Set ssr = ActiveSelectionRange
  340. Dim s1 As Shape, sh As Shape, s As Shape
  341. Dim tr As Double
  342. ' 记忆选择范围
  343. Dim x As Double, Y As Double, w As Double, h As Double
  344. ssr.GetBoundingBox x, Y, w, h
  345. Set s1 = ActiveLayer.CreateRectangle2(x, Y, w, h)
  346. ' 解散群组,先组合,再散开
  347. Set s = ssr.UngroupAllEx.Combine
  348. Set ssr = s.BreakApartEx
  349. ' 读取容差值
  350. tr = 0
  351. If GlobalUserData.Exists("Tolerance", 1) Then
  352. tr = Val(GlobalUserData("Tolerance", 1))
  353. End If
  354. ' 标记画框,选择标记框
  355. For Each sh In ssr
  356. mark_shape_expand sh, tr
  357. Next sh
  358. Set ssr = ActivePage.Shapes.FindShapes(Query:="@colors.find(RGB(0, 255, 0))")
  359. ActiveDocument.ClearSelection
  360. ssr.AddToSelection
  361. ' 调用 智能群组 后删除标记画框
  362. 智能群组和查找.智能群组
  363. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  364. ssr.Delete
  365. Set sh = ActivePage.SelectShapesFromRectangle(s1.LeftX, s1.TopY, s1.RightX, s1.BottomY, False)
  366. ' sh.Shapes.All.Group
  367. s1.Delete
  368. ' 通过s1矩形范围选择群组后合并组合
  369. For Each s In sh.Shapes
  370. s.UngroupAllEx.Combine
  371. Next s
  372. ActiveDocument.EndCommandGroup
  373. Application.Optimization = False
  374. ActiveWindow.Refresh: Application.Refresh
  375. Exit Function
  376. ErrorHandler:
  377. Application.Optimization = False
  378. On Error Resume Next
  379. End Function
  380. '''//// 简单一刀切 识别群组 ////''' ''' 本功能由群友宏瑞广告赞助发行 '''
  381. Public Function Single_Line()
  382. If 0 = ActiveSelectionRange.Count Then Exit Function
  383. On Error GoTo ErrorHandler
  384. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  385. ActiveDocument.Unit = cdrMillimeter
  386. Dim cm(2) As Color
  387. Set cm(0) = CreateRGBColor(0, 255, 0) ' RGB 绿
  388. Set cm(1) = CreateRGBColor(255, 0, 0) ' RGB 红
  389. Dim ssr As ShapeRange
  390. Dim SrNew As New ShapeRange
  391. Dim s As Shape, s1 As Shape, line As Shape, line2 As Shape
  392. Dim cnt As Integer
  393. cnt = 1
  394. If 1 = ActiveSelectionRange.Count Then
  395. Set ssr = ActiveSelectionRange(1).UngroupAllEx
  396. Else
  397. Set ssr = ActiveSelectionRange
  398. End If
  399. ' 记忆选择范围
  400. Dim x As Double, Y As Double, w As Double, h As Double
  401. ssr.GetBoundingBox x, Y, w, h
  402. Set s1 = ActiveLayer.CreateRectangle2(x, Y, w, h)
  403. s1.Outline.SetProperties Color:=cm(0)
  404. SrNew.Add s1
  405. #If VBA7 Then
  406. ' ssr.sort " @shape1.top>@shape2.top"
  407. ssr.Sort " @shape1.left<@shape2.left"
  408. #Else
  409. ' X4 不支持 ShapeRange.sort
  410. #End If
  411. ''' 相交 Set line2 = line.Intersect(s, True, True)
  412. ''' 判断相交 line.Curve.IntersectsWith(s.Curve)
  413. For Each s In ssr
  414. If cnt > 1 Then
  415. s.ConvertToCurves
  416. Set line = ActiveLayer.CreateLineSegment(s.LeftX, s.TopY, s.LeftX, s.TopY - s.SizeHeight)
  417. line.Outline.SetProperties Color:=cm(1)
  418. SrNew.Add line
  419. End If
  420. cnt = cnt + 1
  421. Next s
  422. SrNew.Group
  423. ActiveDocument.EndCommandGroup
  424. Application.Optimization = False
  425. ActiveWindow.Refresh: Application.Refresh
  426. Exit Function
  427. ErrorHandler:
  428. Application.Optimization = False
  429. On Error Resume Next
  430. End Function
  431. Public Function Single_Line_Vertical()
  432. If 0 = ActiveSelectionRange.Count Then Exit Function
  433. On Error GoTo ErrorHandler
  434. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  435. ActiveDocument.Unit = cdrMillimeter
  436. Dim cm(2) As Color
  437. Set cm(0) = CreateRGBColor(0, 255, 0) ' RGB 绿
  438. Set cm(1) = CreateRGBColor(255, 0, 0) ' RGB 红
  439. Dim ssr As ShapeRange
  440. Dim SrNew As New ShapeRange
  441. Dim s As Shape, s1 As Shape, line As Shape, line2 As Shape
  442. Dim cnt As Integer
  443. cnt = 1
  444. If 1 = ActiveSelectionRange.Count Then
  445. Set ssr = ActiveSelectionRange(1).UngroupAllEx
  446. Else
  447. Set ssr = ActiveSelectionRange
  448. End If
  449. ' 记忆选择范围
  450. Dim x As Double, Y As Double, w As Double, h As Double
  451. ssr.GetBoundingBox x, Y, w, h
  452. Set s1 = ActiveLayer.CreateRectangle2(x, Y, w, h)
  453. s1.Outline.SetProperties Color:=cm(0)
  454. SrNew.Add s1
  455. #If VBA7 Then
  456. ssr.Sort " @shape1.top>@shape2.top"
  457. #Else
  458. ' X4 不支持 ShapeRange.sort
  459. #End If
  460. For Each s In ssr
  461. If cnt > 1 Then
  462. s.ConvertToCurves
  463. Set line = ActiveLayer.CreateLineSegment(s.LeftX, s.TopY, s.RightX, s.TopY)
  464. line.Outline.SetProperties Color:=cm(1)
  465. SrNew.Add line
  466. End If
  467. cnt = cnt + 1
  468. Next s
  469. SrNew.Group
  470. ActiveDocument.EndCommandGroup
  471. Application.Optimization = False
  472. ActiveWindow.Refresh: Application.Refresh
  473. Exit Function
  474. ErrorHandler:
  475. Application.Optimization = False
  476. On Error Resume Next
  477. End Function
  478. Public Function Single_Line_LastNode()
  479. If 0 = ActiveSelectionRange.Count Then Exit Function
  480. On Error GoTo ErrorHandler
  481. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  482. ActiveDocument.Unit = cdrMillimeter
  483. Dim cm(2) As Color
  484. Set cm(0) = CreateRGBColor(0, 255, 0) ' RGB 绿
  485. Set cm(1) = CreateRGBColor(255, 0, 0) ' RGB 红
  486. Dim ssr As ShapeRange
  487. Dim SrNew As New ShapeRange
  488. Dim s As Shape, s1 As Shape, line As Shape, line2 As Shape
  489. Dim cnt As Integer
  490. cnt = 1
  491. If 1 = ActiveSelectionRange.Count Then
  492. Set ssr = ActiveSelectionRange(1).UngroupAllEx
  493. Else
  494. Set ssr = ActiveSelectionRange
  495. End If
  496. ' 记忆选择范围
  497. Dim x As Double, Y As Double, w As Double, h As Double
  498. ssr.GetBoundingBox x, Y, w, h
  499. Set s1 = ActiveLayer.CreateRectangle2(x, Y, w, h)
  500. s1.Outline.SetProperties Color:=cm(0)
  501. SrNew.Add s1
  502. #If VBA7 Then
  503. ssr.Sort " @shape1.left<@shape2.left"
  504. #Else
  505. ' X4 不支持 ShapeRange.sort
  506. #End If
  507. Dim nr As NodeRange
  508. For Each s In ssr
  509. If cnt > 1 Then
  510. Set nr = s.DisplayCurve.Nodes.All
  511. Set line = ActiveLayer.CreateLineSegment(nr.FirstNode.PositionX, nr.FirstNode.PositionY, nr.LastNode.PositionX, nr.LastNode.PositionY)
  512. line.Outline.SetProperties Color:=cm(1)
  513. SrNew.Add line
  514. End If
  515. cnt = cnt + 1
  516. Next s
  517. SrNew.Group
  518. ActiveDocument.EndCommandGroup
  519. Application.Optimization = False
  520. ActiveWindow.Refresh: Application.Refresh
  521. Exit Function
  522. ErrorHandler:
  523. Application.Optimization = False
  524. On Error Resume Next
  525. End Function
  526. '''//// 选择范围画框 ////'''
  527. Public Function Mark_Range_Box()
  528. If 0 = ActiveSelectionRange.Count Then Exit Function
  529. ActiveDocument.Unit = cdrMillimeter
  530. Dim s1 As Shape, ssr As ShapeRange
  531. Set ssr = ActiveSelectionRange
  532. Dim x As Double, Y As Double, w As Double, h As Double
  533. ssr.GetBoundingBox x, Y, w, h
  534. Set s1 = ActiveLayer.CreateRectangle2(x, Y, w, h)
  535. s1.Outline.SetProperties Color:=CreateRGBColor(0, 255, 0) ' RGB 绿
  536. End Function
  537. '''//// 快速颜色选择 ////'''
  538. Function quickColorSelect()
  539. Dim x As Double, Y As Double
  540. Dim s As Shape, s1 As Shape
  541. Dim sr As ShapeRange, sr2 As ShapeRange
  542. Dim Shift As Long, bClick As Boolean
  543. Dim c As New Color, c2 As New Color
  544. EventsEnabled = False
  545. Set sr = ActivePage.Shapes.FindShapes(Query:="@fill.type = 'uniform'")
  546. ActiveDocument.ClearSelection
  547. bClick = False
  548. While Not bClick
  549. On Error Resume Next
  550. bClick = ActiveDocument.GetUserClick(x, Y, Shift, 10, False, cdrCursorPickNone)
  551. If Not bClick Then
  552. Set s = ActivePage.SelectShapesAtPoint(x, Y, False)
  553. Set s = s.Shapes.Last
  554. c2.CopyAssign s.Fill.UniformColor
  555. Set sr2 = New ShapeRange
  556. For Each s1 In sr.Shapes
  557. c.CopyAssign s1.Fill.UniformColor
  558. If c.IsSame(c2) Then
  559. sr2.Add s1
  560. End If
  561. Next s1
  562. sr2.CreateSelection
  563. ActiveWindow.Refresh
  564. End If
  565. Wend
  566. EventsEnabled = True
  567. End Function
  568. '''//// 切割图形-垂直分割-水平分割 ////'''
  569. Function divideVertically()
  570. If 0 = ActiveSelectionRange.Count Then Exit Function
  571. On Error GoTo ErrorHandler
  572. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  573. cutInHalf 1
  574. ActiveDocument.EndCommandGroup
  575. Application.Optimization = False
  576. ActiveWindow.Refresh: Application.Refresh
  577. Exit Function
  578. ErrorHandler:
  579. Application.Optimization = False
  580. On Error Resume Next
  581. End Function
  582. Function divideHorizontally()
  583. If 0 = ActiveSelectionRange.Count Then Exit Function
  584. On Error GoTo ErrorHandler
  585. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  586. cutInHalf 2
  587. ActiveDocument.EndCommandGroup
  588. Application.Optimization = False
  589. ActiveWindow.Refresh: Application.Refresh
  590. Exit Function
  591. ErrorHandler:
  592. Application.Optimization = False
  593. On Error Resume Next
  594. End Function
  595. Private Function cutInHalf(Optional method As Integer)
  596. Dim s As Shape, rect As Shape, rect2 As Shape
  597. Dim trimmed1 As Shape, trimmed2 As Shape
  598. Dim x As Double, Y As Double, w As Double, h As Double
  599. Dim vBool As Boolean
  600. Dim leeway As Double
  601. Dim sr As ShapeRange, sr2 As New ShapeRange
  602. vBool = True
  603. If method = 2 Then
  604. vBool = False
  605. End If
  606. leeway = 0.1
  607. Set sr = ActiveSelectionRange
  608. ActiveDocument.BeginCommandGroup "Cut in half"
  609. For Each s In sr
  610. s.GetBoundingBox x, Y, w, h
  611. If (vBool) Then
  612. 'vertical slice
  613. Set rect = ActiveLayer.CreateRectangle2(x - leeway, Y - leeway, (w / 2) + leeway, h + (leeway * 2))
  614. Set rect2 = ActiveLayer.CreateRectangle2(x + (w / 2), Y - leeway, (w / 2) + leeway, h + (leeway * 2))
  615. Else
  616. Set rect = ActiveLayer.CreateRectangle2(x - leeway, Y - leeway, w + (leeway * 2), (h / 2) + leeway)
  617. Set rect2 = ActiveLayer.CreateRectangle2(x - leeway, Y + (h / 2), w + (leeway * 2), (h / 2) + leeway)
  618. End If
  619. Set trimmed1 = rect.Intersect(s, True, True)
  620. rect.Delete
  621. Set trimmed2 = rect2.Intersect(s, True, True)
  622. s.Delete
  623. rect2.Delete
  624. sr2.Add trimmed1
  625. sr2.Add trimmed2
  626. Next s
  627. ActiveDocument.EndCommandGroup
  628. sr2.CreateSelection
  629. End Function
  630. '// 批量多页居中-遍历批量物件,放置物件到页面
  631. Public Function 批量多页居中()
  632. If 0 = ActiveSelectionRange.Count Then Exit Function
  633. On Error GoTo ErrorHandler
  634. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  635. ActiveDocument.Unit = cdrMillimeter
  636. Set sr = ActiveSelectionRange
  637. total = sr.Count
  638. '// 建立多页面
  639. Set doc = ActiveDocument
  640. doc.AddPages (total - 1)
  641. #If VBA7 Then
  642. sr.Sort " @shape1.Top * 100 - @shape1.Left > @shape2.Top * 100 - @shape2.Left"
  643. #Else
  644. ' X4 不支持 ShapeRange.sort
  645. #End If
  646. Dim sh As Shape
  647. '// 遍历批量物件,放置物件到页面
  648. For I = 1 To sr.Count
  649. doc.Pages(I).Activate
  650. Set sh = sr.Shapes(I)
  651. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  652. '// 物件居中页面
  653. #If VBA7 Then
  654. ActiveDocument.ClearSelection
  655. sh.AddToSelection
  656. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  657. #Else
  658. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  659. #End If
  660. Next I
  661. ActiveDocument.EndCommandGroup: Application.Optimization = False
  662. ActiveWindow.Refresh: Application.Refresh
  663. Exit Function
  664. ErrorHandler:
  665. Application.Optimization = False
  666. MsgBox "请先选择一些物件"
  667. On Error Resume Next
  668. End Function
  669. '// 安全线: 点击一次建立辅助线,再调用清除参考线
  670. Public Function guideangle(actnumber As ShapeRange, cardblood As Integer)
  671. Dim sr As ShapeRange
  672. Set sr = ActiveDocument.MasterPage.GuidesLayer.FindShapes(Type:=cdrGuidelineShape)
  673. If sr.Count <> 0 Then
  674. sr.Delete
  675. Exit Function
  676. End If
  677. If 0 = ActiveSelectionRange.Count Then Exit Function
  678. ActiveDocument.Unit = cdrMillimeter
  679. With actnumber
  680. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .TopY - cardblood, 0#)
  681. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .BottomY + cardblood, 0#)
  682. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.LeftX + cardblood, 0, 90#)
  683. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.RightX - cardblood, 0, 90#)
  684. End With
  685. End Function