Tools.bas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. Attribute VB_Name = "Tools"
  2. #If VBA7 Then
  3. Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
  4. #Else
  5. Private Declare Sub Sleep Lib "kernel32" (ByValdwMilliseconds As Long)
  6. #End If
  7. Public Function wait()
  8. Sleep 3000
  9. End Function
  10. Public Sub 填入居中文字(str)
  11. Dim s As Shape
  12. <<<<<<< HEAD
  13. Dim x As Double, y As Double, Shift As Long
  14. Dim b As Boolean
  15. b = ActiveDocument.GetUserClick(x, y, Shift, 10, False, cdrCursorIntersectSingle)
  16. str = VBA.Replace(str, vbNewLine, Chr(10))
  17. str = VBA.Replace(str, Chr(10), vbNewLine)
  18. Set s = ActiveLayer.CreateArtisticText(0, 0, str)
  19. s.CenterX = x
  20. s.CenterY = y
  21. =======
  22. Set s = ActiveSelection
  23. X = s.CenterX
  24. Y = s.CenterY
  25. Set s = ActiveLayer.CreateArtisticText(0, 0, Str)
  26. s.CenterX = X
  27. s.CenterY = Y
  28. >>>>>>> 556e97d494ce938408287776a3528f332486766c
  29. End Sub
  30. Public Sub 尺寸标注()
  31. ActiveDocument.Unit = cdrMillimeter
  32. Set s = ActiveSelection
  33. <<<<<<< HEAD
  34. x = s.CenterX: y = s.TopY
  35. sw = s.SizeWidth: sh = s.SizeHeight
  36. text = Int(sw) & "x" & Int(sh) & "mm"
  37. Set s = ActiveLayer.CreateArtisticText(0, 0, text)
  38. s.CenterX = x: s.BottomY = y + 5
  39. =======
  40. X = s.CenterX: Y = s.TopY
  41. sw = s.SizeWidth: sh = s.SizeHeight
  42. Text = Int(sw) & "x" & Int(sh) & "mm"
  43. Set s = ActiveLayer.CreateArtisticText(0, 0, Text)
  44. s.CenterX = X: s.BottomY = Y + 5
  45. >>>>>>> 556e97d494ce938408287776a3528f332486766c
  46. End Sub
  47. Public Sub 批量居中文字(str)
  48. Dim s As Shape, sr As ShapeRange
  49. Set sr = ActiveSelectionRange
  50. For Each s In sr.Shapes
  51. <<<<<<< HEAD
  52. x = s.CenterX: y = s.CenterY
  53. Set s = ActiveLayer.CreateArtisticText(0, 0, str)
  54. s.CenterX = x: s.CenterY = y
  55. =======
  56. X = s.CenterX: Y = s.CenterY
  57. Set s = ActiveLayer.CreateArtisticText(0, 0, Str)
  58. s.CenterX = X: s.CenterY = Y
  59. >>>>>>> 556e97d494ce938408287776a3528f332486766c
  60. Next
  61. End Sub
  62. Public Sub 批量标注()
  63. ActiveDocument.Unit = cdrMillimeter
  64. Set sr = ActiveSelectionRange
  65. For Each s In sr.Shapes
  66. <<<<<<< HEAD
  67. x = s.CenterX: y = s.TopY
  68. sw = s.SizeWidth: sh = s.SizeHeight
  69. text = Int(sw + 0.5) & "x" & Int(sh + 0.5) & "mm"
  70. Set s = ActiveLayer.CreateArtisticText(0, 0, text)
  71. s.CenterX = x: s.BottomY = y + 5
  72. =======
  73. X = s.CenterX: Y = s.TopY
  74. sw = s.SizeWidth: sh = s.SizeHeight
  75. Text = Int(sw + 0.5) & "x" & Int(sh + 0.5) & "mm"
  76. Set s = ActiveLayer.CreateArtisticText(0, 0, Text)
  77. s.CenterX = X: s.BottomY = Y + 5
  78. >>>>>>> 556e97d494ce938408287776a3528f332486766c
  79. Next
  80. End Sub
  81. Public Sub 智能群组()
  82. Set s1 = ActiveSelectionRange.CustomCommand("Boundary", "CreateBoundary")
  83. Set brk1 = s1.BreakApartEx
  84. For Each s In brk1
  85. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, True)
  86. sh.Shapes.All.group
  87. s.Delete
  88. Next
  89. End Sub
  90. ' 实践应用: 选择物件群组,页面设置物件大小,物件页面居中
  91. Public Function 群组居中页面()
  92. ActiveDocument.Unit = cdrMillimeter
  93. Dim OrigSelection As ShapeRange, sh As Shape
  94. Set OrigSelection = ActiveSelectionRange
  95. Set sh = OrigSelection.group
  96. ' MsgBox "选择物件尺寸: " & sh.SizeWidth & "x" & sh.SizeHeight
  97. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  98. #If VBA7 Then
  99. ActiveDocument.ClearSelection
  100. sh.AddToSelection
  101. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  102. #Else
  103. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  104. #End If
  105. End Function
  106. Public Function 批量多页居中()
  107. If 0 = ActiveSelectionRange.Count Then Exit Function
  108. On Error GoTo ErrorHandler
  109. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  110. ActiveDocument.Unit = cdrMillimeter
  111. Set sr = ActiveSelectionRange
  112. total = sr.Count
  113. '// 建立多页面
  114. Set doc = ActiveDocument
  115. doc.AddPages (total - 1)
  116. Dim sh As Shape
  117. '// 遍历批量物件,放置物件到页面
  118. For i = 1 To sr.Count
  119. doc.Pages(i).Activate
  120. Set sh = sr.Shapes(i)
  121. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  122. '// 物件居中页面
  123. #If VBA7 Then
  124. ActiveDocument.ClearSelection
  125. sh.AddToSelection
  126. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  127. #Else
  128. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  129. #End If
  130. Next i
  131. ActiveDocument.EndCommandGroup: Application.Optimization = False
  132. ActiveWindow.Refresh: Application.Refresh
  133. Exit Function
  134. ErrorHandler:
  135. Application.Optimization = False
  136. MsgBox "请先选择一些物件"
  137. On Error Resume Next
  138. End Function
  139. '// 安全线: 点击一次建立辅助线,再调用清除参考线
  140. Public Function guideangle(actnumber As ShapeRange, cardblood As Integer)
  141. Dim sr As ShapeRange
  142. Set sr = ActiveDocument.MasterPage.GuidesLayer.FindShapes(Type:=cdrGuidelineShape)
  143. If sr.Count <> 0 Then
  144. sr.Delete
  145. Exit Function
  146. End If
  147. If 0 = ActiveSelectionRange.Count Then Exit Function
  148. ActiveDocument.Unit = cdrMillimeter
  149. With actnumber
  150. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .TopY - cardblood, 0#)
  151. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .BottomY + cardblood, 0#)
  152. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.LeftX + cardblood, 0, 90#)
  153. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.RightX - cardblood, 0, 90#)
  154. End With
  155. End Function
  156. Public Function splash_cnt()
  157. splash.show 0
  158. splash.text1 = splash.text1 & ">"
  159. Sleep 100
  160. End Function
  161. Public Function vba_cnt()
  162. VBA_FORM.text1 = VBA_FORM.text1 & ">"
  163. Sleep 100
  164. End Function
  165. Public Function 按面积排列(space_width As Double)
  166. If 0 = ActiveSelectionRange.Count Then Exit Function
  167. ActiveDocument.Unit = cdrMillimeter
  168. ActiveDocument.ReferencePoint = cdrCenter
  169. Set ssr = ActiveSelectionRange
  170. cnt = 1
  171. #If VBA7 Then
  172. ssr.Sort "@shape1.width * @shape1.height < @shape2.width * @shape2.height"
  173. #Else
  174. ' X4 不支持 ShapeRange.sort
  175. #End If
  176. Dim str As String, size As String
  177. For Each sh In ssr
  178. size = Int(sh.SizeWidth + 0.5) & "x" & Int(sh.SizeHeight + 0.5) & "mm"
  179. sh.SetSize Int(sh.SizeWidth + 0.5), Int(sh.SizeHeight + 0.5)
  180. str = str & size & vbNewLine
  181. Next sh
  182. ActiveDocument.ReferencePoint = cdrTopLeft
  183. For Each s In ssr
  184. If cnt > 1 Then s.SetPosition ssr(cnt - 1).LeftX, ssr(cnt - 1).BottomY - space_width
  185. cnt = cnt + 1
  186. vba_cnt
  187. Next s
  188. ' 写文件,可以EXCEL里统计
  189. ' Set fs = CreateObject("Scripting.FileSystemObject")
  190. ' Set f = fs.CreateTextFile("D:\size.txt", True)
  191. ' f.WriteLine str: f.Close
  192. str = 分类汇总(str)
  193. Debug.Print str
  194. Dim s1 As Shape
  195. ' Set s1 = ActiveLayer.CreateParagraphText(0, 0, 100, 150, Str, Font:="华文中宋")
  196. <<<<<<< HEAD
  197. x = ssr.FirstShape.LeftX - 100
  198. y = ssr.FirstShape.TopY
  199. Set s1 = ActiveLayer.CreateParagraphText(x, y, x + 90, y - 150, str, Font:="华文中宋")
  200. =======
  201. X = ssr.FirstShape.LeftX - 100
  202. Y = ssr.FirstShape.TopY
  203. Set s1 = ActiveLayer.CreateParagraphText(X, Y, X + 90, Y - 150, Str, Font:="华文中宋")
  204. >>>>>>> 556e97d494ce938408287776a3528f332486766c
  205. End Function
  206. '// 实现Excel里分类汇总功能
  207. Private Function 分类汇总(str As String) As String
  208. Dim a, b, d, arr
  209. str = VBA.Replace(str, vbNewLine, " ")
  210. Do While InStr(str, " ")
  211. str = VBA.Replace(str, " ", " ")
  212. Loop
  213. arr = Split(str)
  214. Set d = CreateObject("Scripting.dictionary")
  215. For i = 0 To UBound(arr) - 1
  216. If d.Exists(arr(i)) = True Then
  217. d.Item(arr(i)) = d.Item(arr(i)) + 1
  218. Else
  219. d.Add arr(i), 1
  220. End If
  221. Next
  222. str = " 规 格" & vbTab & vbTab & vbTab & "数量" & vbNewLine
  223. a = d.keys: b = d.items
  224. For i = 0 To d.Count - 1
  225. ' Debug.Print a(i), b(i)
  226. str = str & a(i) & vbTab & vbTab & b(i) & "条" & vbNewLine
  227. Next
  228. 分类汇总 = str & "合计总量:" & vbTab & vbTab & vbTab & UBound(arr) & "条" & vbNewLine
  229. End Function
  230. ' 两个端点的坐标,为(x1,y1)和(x2,y2) 那么其角度a的tan值: tana=(y2-y1)/(x2-x1)
  231. ' 所以计算arctan(y2-y1)/(x2-x1), 得到其角度值a
  232. ' VB中用atn(), 返回值是弧度,需要 乘以 PI /180
  233. Private Function lineangle(x1, y1, x2, y2) As Double
  234. pi = 4 * VBA.Atn(1) ' 计算圆周率
  235. If x2 = x1 Then
  236. lineangle = 90: Exit Function
  237. End If
  238. lineangle = VBA.Atn((y2 - y1) / (x2 - x1)) / pi * 180
  239. End Function
  240. Public Function 角度转平()
  241. On Error GoTo ErrorHandler
  242. ' ActiveDocument.ReferencePoint = cdrCenter
  243. Set sr = ActiveSelectionRange
  244. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  245. If nr.Count = 2 Then
  246. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  247. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  248. a = lineangle(x1, y1, x2, y2): sr.Rotate -a
  249. ' sr.LastShape.Delete '// 删除参考线
  250. End If
  251. ErrorHandler:
  252. End Function
  253. Public Function 自动旋转角度()
  254. On Error GoTo ErrorHandler
  255. ' ActiveDocument.ReferencePoint = cdrCenter
  256. Set sr = ActiveSelectionRange
  257. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  258. If nr.Count = 2 Then
  259. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  260. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  261. a = lineangle(x1, y1, x2, y2): sr.Rotate 90 + a
  262. sr.LastShape.Delete '// 删除参考线
  263. End If
  264. ErrorHandler:
  265. End Function
  266. Public Function 交换对象()
  267. Set sr = ActiveSelectionRange
  268. If sr.Count = 2 Then
  269. x = sr.LastShape.CenterX: y = sr.LastShape.CenterY
  270. sr.LastShape.CenterX = sr.FirstShape.CenterX: sr.LastShape.CenterY = sr.FirstShape.CenterY
  271. sr.FirstShape.CenterX = x: sr.FirstShape.CenterY = y
  272. End If
  273. End Function
  274. Public Function 参考线镜像()
  275. On Error GoTo ErrorHandler
  276. Set sr = ActiveSelectionRange
  277. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  278. If nr.Count = 2 Then
  279. ActiveDocument.BeginCommandGroup "Mirror"
  280. byshape = False
  281. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  282. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  283. a = lineangle(x1, y1, x2, y2) '// 参考线和水平的夹角 a
  284. sr.Remove sr.Count
  285. ang = 90 - a ' 镜像的旋转角度
  286. For Each s In sr
  287. With s
  288. .Duplicate ' // 复制物件保留,然后按 x1,y1 点 旋转
  289. .RotationCenterX = x1
  290. .RotationCenterY = y1
  291. .Rotate ang
  292. If Not byshape Then
  293. lx = .LeftX
  294. .Stretch -1#, 1# ' // 通过拉伸完成镜像
  295. .LeftX = lx
  296. .Move (x1 - .LeftX) * 2 - .SizeWidth, 0
  297. .RotationCenterX = x1 '// 之前因为镜像,旋转中心点反了,重置回来
  298. .RotationCenterY = y1
  299. .Rotate -ang
  300. End If
  301. .RotationCenterX = .CenterX '// 重置回旋转中心点为物件中心
  302. .RotationCenterY = .CenterY
  303. End With
  304. Next s
  305. ActiveDocument.EndCommandGroup
  306. End If
  307. ErrorHandler:
  308. End Function
  309. Public Function autogroup(Optional group As String = "group", Optional shft = 0, Optional sss As Shapes = Nothing, Optional undogroup = True) As ShapeRange
  310. Dim sr As ShapeRange, sr_all As ShapeRange, os As ShapeRange
  311. Dim sp As SubPaths
  312. Dim arr()
  313. Dim s As Shape
  314. If sss Is Nothing Then Set os = ActiveSelectionRange Else Set os = sss.All
  315. On Error GoTo errn
  316. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  317. If ActiveSelection.Shapes.Count > 0 Then
  318. gcnt = os.Shapes.Count
  319. ReDim arr(1 To gcnt, 1 To gcnt)
  320. Set sr_all = ActiveSelectionRange
  321. sr_all.RemoveAll
  322. ReDim arr(1 To gcnt, 1 To gcnt)
  323. ActiveDocument.Unit = cdrTenthMicron
  324. sgap = 10
  325. If shft = 2 Or shft = 3 Or shft = 6 Or shft = 7 Then
  326. os.RemoveAll
  327. For Each s In ActiveSelectionRange.Shapes
  328. os.Add ActivePage.SelectShapesFromRectangle(s.LeftX - sgap, s.BottomY - sgap, s.RightX + sgap, s.TopY + sgap, True)
  329. Next s
  330. End If
  331. For i = 1 To os.Shapes.Count
  332. Set s1 = os.Shapes(i)
  333. arr(i, i) = i
  334. For j = 1 To os.Shapes.Count
  335. Set s2 = os.Shapes(j)
  336. If s2.LeftX < s1.RightX + sgap And s2.RightX > s1.LeftX - sgap And s2.BottomY < s1.TopY + sgap And s2.TopY > s1.BottomY - sgap Then
  337. If shft = 1 Or shft = 3 Or shft = 5 Or shft = 7 Then
  338. Set isec = s1.Intersect(s2)
  339. If Not isec Is Nothing Then
  340. arr(i, j) = j
  341. isec.CreateSelection
  342. isec.Delete
  343. End If
  344. Else
  345. arr(i, j) = j
  346. End If
  347. End If
  348. Next j
  349. Next i
  350. For i = 1 To gcnt
  351. arr = collect_arr(arr, i, i)
  352. Next i
  353. Set sr = ActiveSelectionRange
  354. For i = 1 To gcnt
  355. sr.RemoveAll
  356. inar = 0
  357. For j = 1 To gcnt
  358. If arr(i, j) > 0 Then
  359. sr.Add os.Shapes(j)
  360. inar = inar + 1
  361. End If
  362. Next j
  363. If inar > 1 Then
  364. If group = "group" Then
  365. If shft < 4 Then sr_all.Add sr.group
  366. End If
  367. Else
  368. If sr.Shapes.Count > 0 Then sr_all.AddRange sr
  369. End If
  370. Next i
  371. Set autogroup = sr_all
  372. End If
  373. ActiveDocument.EndCommandGroup
  374. Application.Optimization = False
  375. ActiveWindow.Refresh: Application.Refresh
  376. Exit Function
  377. errn:
  378. Application.Optimization = False
  379. End Function
  380. Public Function collect_arr(arr, ci, ki)
  381. lim = UBound(arr)
  382. For k = 1 To lim
  383. If arr(ki, k) > 0 Then
  384. arr(ci, k) = k
  385. If ki <> ci Then arr(ki, k) = Empty
  386. If ci <> k And ki <> k Then arr = collect_arr(arr, ci, k)
  387. End If
  388. Next k
  389. 'If ki <> ci Then arr(ki, ki) = Empty
  390. collect_arr = arr
  391. End Function
  392. ' 两个端点的坐标,为(x1,y1)和(x2,y2) 那么其角度a的tan值: tana=(y2-y1)/(x2-x1)
  393. ' 所以计算arctan(y2-y1)/(x2-x1), 得到其角度值a
  394. ' VB中用atn(), 返回值是弧度,需要 乘以 PI /180
  395. Private Function lineangle(x1, y1, x2, y2) As Double
  396. pi = 4 * VBA.Atn(1) ' 计算圆周率
  397. If x2 = x1 Then
  398. lineangle = 90: Exit Function
  399. End If
  400. lineangle = VBA.Atn((y2 - y1) / (x2 - x1)) / pi * 180
  401. End Function
  402. <<<<<<< HEAD
  403. Sub Make_Sizes()
  404. ActiveDocument.Unit = cdrMillimeter
  405. Set os = ActiveSelectionRange
  406. If os.Count > 0 Then
  407. For Each s In os.Shapes
  408. Set pts = os.FirstShape.SnapPoints.BBox(cdrTopLeft)
  409. Set pte = os.LastShape.SnapPoints.BBox(cdrTopRight)
  410. ActiveLayer.CreateLinearDimension cdrDimensionHorizontal, pts, pte, True, os.LeftX + os.SizeWidth / 10, os.TopY + os.SizeHeight / 10, cdrDimensionStyleEngineering
  411. Set pte = os.LastShape.SnapPoints.BBox(cdrBottomLeft)
  412. ActiveLayer.CreateLinearDimension cdrDimensionVertical, pts, pte, True, os.LeftX - os.SizeWidth / 10, os.BottomY + os.SizeHeight / 10, cdrDimensionStyleEngineering
  413. Next s
  414. End If
  415. End Sub
  416. '''//// 选择多物件,组合然后拆分线段,为角线爬虫准备 ////'''
  417. Public Function Split_Segment()
  418. On Error GoTo ErrorHandler
  419. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  420. Dim ssr As ShapeRange
  421. Set ssr = ActiveSelectionRange
  422. Dim s As Shape
  423. Dim nr As NodeRange
  424. Dim nd As Node
  425. Set s = ssr.UngroupAllEx.Combine
  426. Set nr = s.Curve.Nodes.All
  427. nr.BreakApart
  428. s.BreakApartEx
  429. ' For Each nd In nr
  430. ' nd.BreakApart
  431. ' Next nd
  432. ActiveDocument.EndCommandGroup
  433. Application.Optimization = False
  434. ActiveWindow.Refresh: Application.Refresh
  435. Exit Function
  436. ErrorHandler:
  437. Application.Optimization = False
  438. On Error Resume Next
  439. End Function
  440. '// 修复圆角缺角到直角
  441. Public Sub corner_off()
  442. Dim os As ShapeRange
  443. Dim s As Shape, fir As Shape, ci As Shape
  444. Dim nd As Node, nds As Node, nde As Node
  445. Set os = ActiveSelectionRange
  446. ud = ActiveDocument.Unit
  447. ActiveDocument.Unit = cdrMillimeter
  448. On Error GoTo errn
  449. ActiveDocument.BeginCommandGroup "corners off"
  450. 'Application.Optimization = True
  451. selec = False
  452. If os.Shapes.Count = 1 Then
  453. Set s = os.FirstShape
  454. If Not s.Curve Is Nothing Then
  455. For Each nd In s.Curve.Nodes
  456. If nd.Selected Then
  457. selec = True
  458. Exit For
  459. End If
  460. Next nd
  461. End If
  462. End If
  463. If os.Shapes.Count > 1 Or Not selec Then
  464. os.ConvertToCurves
  465. For Each s In os.Shapes
  466. Set nds = Nothing
  467. Set nde = Nothing
  468. For k = 1 To 3
  469. For i = 1 To s.Curve.Nodes.Count
  470. If i <= s.Curve.Nodes.Count Then
  471. Set nd = s.Curve.Nodes(i)
  472. If Not nd.NextSegment Is Nothing And Not nd.PrevSegment Is Nothing Then
  473. If Abs(nd.PrevSegment.Length - nd.NextSegment.Length) < (nd.PrevSegment.Length + nd.NextSegment.Length) / 30 And nd.PrevSegment.Type = cdrCurveSegment And nd.NextSegment.Type = cdrCurveSegment Then
  474. corner_off_make s, nd.Previous, nd.Next
  475. ElseIf Not nd.Next.NextSegment Is Nothing Then
  476. If (nd.PrevSegment.Type = cdrLineSegment Or Abs(Abs(nd.PrevSegment.StartingControlPointAngle - nd.PrevSegment.EndingControlPointAngle) - 180) < 1) _
  477. And (nd.Next.NextSegment.Type = cdrLineSegment Or Abs(Abs(nd.Next.NextSegment.StartingControlPointAngle - nd.Next.NextSegment.EndingControlPointAngle) - 180) < 1) _
  478. And nd.NextSegment.Type = cdrCurveSegment Then
  479. corner_off_make s, nd, nd.Next
  480. End If
  481. End If
  482. End If
  483. End If
  484. Next i
  485. Next k
  486. Next s
  487. ElseIf os.Shapes.Count = 1 And selec Then
  488. Set nds = Nothing
  489. Set nde = Nothing
  490. For Each nd In s.Curve.Nodes
  491. If Not nd.Selected And Not nd.Next.Selected Then Exit For
  492. Next nd
  493. If Not nd Is s.Curve.Nodes.Last Then
  494. For i = 1 To s.Curve.Nodes.Count
  495. Set nd = nd.Next
  496. If Not nde Is Nothing And Not nds Is Nothing And Not nd.Selected Then Exit For
  497. If Not nds Is Nothing And nd.Selected Then Set nde = nd
  498. If nde Is Nothing And nds Is Nothing And nd.Selected Then Set nds = nd
  499. Next i
  500. If Not nds Is Nothing And Not nde Is Nothing Then
  501. 'ActiveLayer.CreateEllipse2 nds.PositionX, nds.PositionY, nde.PrevSegment.Length / 4
  502. 'ActiveLayer.CreateEllipse2 nde.PositionX, nde.PositionY, nde.PrevSegment.Length / 4
  503. corner_off_make s, nds, nde
  504. End If
  505. End If
  506. End If
  507. errn:
  508. Application.Optimization = False
  509. ActiveDocument.EndCommandGroup
  510. Application.Refresh
  511. ActiveDocument.Unit = ud
  512. End Sub
  513. Private Sub corner_off_make(s As Shape, nds As Node, nde As Node)
  514. Dim l1 As Shape, l2 As Shape
  515. Dim os As ShapeRange
  516. Dim ss As Shape
  517. ud = ActiveDocument.Unit
  518. ActiveDocument.Unit = cdrMillimeter
  519. Set l1 = ActiveLayer.CreateLineSegment(nds.PositionX, nds.PositionY, nds.PositionX + s.SizeWidth * 3, nds.PositionY)
  520. l1.RotationCenterX = nds.PositionX
  521. l1.RotationAngle = nds.PrevSegment.EndingControlPointAngle + 180
  522. Set l2 = ActiveLayer.CreateLineSegment(nde.PositionX, nde.PositionY, nde.PositionX + s.SizeWidth * 3, nde.PositionY)
  523. l2.RotationCenterX = nde.PositionX
  524. l2.RotationAngle = nde.NextSegment.StartingControlPointAngle + 180
  525. Set lcross = l2.Curve.Segments.First.GetIntersections(l1.Curve.Segments.First)
  526. If lcross.Count > 0 Then
  527. cx = lcross(1).PositionX
  528. cy = lcross(1).PositionY
  529. sx = nds.PositionX
  530. sy = nds.PositionY
  531. ex = nde.PositionX
  532. ey = nde.PositionY
  533. l1.Curve.Nodes.Last.PositionX = cx
  534. l1.Curve.Nodes.Last.PositionY = cy
  535. l2.Curve.Nodes.Last.PositionX = cx
  536. l2.Curve.Nodes.Last.PositionY = cy
  537. s.Curve.Nodes.Range(Array(nds.AbsoluteIndex, nde.AbsoluteIndex)).BreakApart
  538. Set os = s.BreakApartEx
  539. oscnt = os.Shapes.Count
  540. For Each ss In os.Shapes
  541. If ss.Curve.Nodes.First.PositionX = ex And ss.Curve.Nodes.First.PositionY = ey Then Set s2 = ss
  542. If ss.Curve.Nodes.Last.PositionX = sx And ss.Curve.Nodes.Last.PositionY = sy Then Set s1 = ss
  543. If ss.Curve.Nodes.First.PositionX = sx And ss.Curve.Nodes.First.PositionY = sy Then ss.Delete
  544. Next ss
  545. If s1.Curve.Segments.Last.Type = cdrLineSegment Or Abs(Abs(s1.Curve.Segments.Last.StartingControlPointAngle - s1.Curve.Segments.Last.EndingControlPointAngle) - 180) < 1 Then
  546. s1.Curve.Nodes.Last.PositionX = lcross(1).PositionX
  547. s1.Curve.Nodes.Last.PositionY = lcross(1).PositionY
  548. l1.Delete
  549. Else
  550. Set s1 = l1.Weld(s1)
  551. End If
  552. If oscnt = 2 Then Set s2 = s1
  553. If s2.Curve.Segments.First.Type = cdrLineSegment Or Abs(Abs(s2.Curve.Segments.First.StartingControlPointAngle - s2.Curve.Segments.First.EndingControlPointAngle) - 180) < 1 Then
  554. s2.Curve.Nodes.First.PositionX = lcross(1).PositionX
  555. s2.Curve.Nodes.First.PositionY = lcross(1).PositionY
  556. l2.Delete
  557. Else
  558. Set s2 = l2.Weld(s2)
  559. End If
  560. If oscnt > 2 Then Set s2 = s1.Weld(s2)
  561. s2.CustomCommand "ConvertTo", "JoinCurves", 0.1
  562. Set s = s2
  563. Else
  564. l1.Delete
  565. l2.Delete
  566. End If
  567. ActiveDocument.Unit = ud
  568. End Sub
  569. Sub ExportNodePositions()
  570. Dim s As Shape, n As Node
  571. Dim srActiveLayer As ShapeRange
  572. Dim x As Double, y As Double
  573. Dim strNodePositions As String
  574. ActiveDocument.Unit = cdrMillimeter
  575. 'Get all the curve shapes on the Active Layer
  576. '获取Active Layer上的所有曲线形状
  577. Set srActiveLayer = ActiveLayer.Shapes.FindShapes(Query:="@type='curve'")
  578. 'This is another way you can get only the curve shapes
  579. '这是另一种你只能得到曲线形状的方法
  580. 'Set srActiveLayer = ActiveLayer.Shapes.FindShapes.FindAnyOfType(cdrCurveShape)
  581. 'Loop through each curve
  582. '遍历每条曲线
  583. For Each s In srActiveLayer.Shapes
  584. 'Loop though each node in the curve and get the position
  585. '遍历曲线中的每个节点并获取位置
  586. For Each n In s.Curve.Nodes
  587. n.GetPosition x, y
  588. strNodePositions = strNodePositions & "x: " & x & " y: " & y & vbCrLf
  589. Next n
  590. Next s
  591. 'Save the node positions to a file
  592. '将节点位置保存到文件
  593. Open "C:\Temp\NodePositions.txt" For Output As #1
  594. Print #1, strNodePositions
  595. Close #1
  596. End Sub
  597. Sub 服务器T()
  598. Dim mark As Shape
  599. Dim sr As ShapeRange
  600. Set sr = ActiveSelectionRange
  601. If (Shift And 1) <> 0 Then ActivePage.Shapes.FindShapes(Query:="@type ='rectangle'or @type ='curve'or @type ='Ellipse'or @type ='Polygon'").CreateSelection
  602. sr.Shapes.FindShapes(Query:="@type ='rectangle'or @type ='curve'or @type ='Ellipse'or @type ='Polygon'").ConvertToCurves
  603. If sr.Count = 0 Then Exit Sub
  604. ' CorelDRAW设置原点标记导出DXF使用
  605. ' 更新原点标记,现在能设置任意坐标点
  606. Dim MarkPos_Array() As Double
  607. MarkPos_Array = Get_MarkPosition
  608. AtOrigin MarkPos_Array(0), MarkPos_Array(1)
  609. sr.Add ActiveDocument.ActiveShape
  610. Set mark = ActiveDocument.ActiveShape
  611. ActiveDocument.ClearSelection
  612. sr.CreateSelection
  613. ' Set mark = ActiveDocument.ActiveShape
  614. ' If FileExists("d:\mytempdxf.dxf") Then
  615. ' DeleteFile "d:\mytempdxf.dxf"
  616. ' End If
  617. SaveDXF "d:\mytempdxf.dxf"
  618. ' Do While FileExists("d:\mytempdxf.dxf") = False
  619. ' DoEvents
  620. ' Delay 1
  621. ' Loop
  622. Shell Application.GMSManager.GMSPath & "tuznr.exe d:/mytempdxf.dxf", 1
  623. mark.Delete
  624. End Sub
  625. Sub SaveDXF(FileName As String)
  626. Dim expopt As StructExportOptions
  627. Set expopt = CreateStructExportOptions
  628. expopt.UseColorProfile = False
  629. Dim expflt As ExportFilter
  630. Set expflt = ActiveDocument.ExportEx(FileName, cdrDXF, cdrSelection, expopt)
  631. With expflt
  632. .BitmapType = 0 ' FilterDXFLib.dxfBitmapJPEG
  633. .TextAsCurves = True
  634. .Version = 3 ' FilterDXFLib.dxfVersion13
  635. .Units = 3 ' FilterDXFLib.dxfMillimeters
  636. .FillUnmapped = True
  637. .Finish
  638. End With
  639. End Sub
  640. ' 更新原点标记函数,现在能设置任意坐标点
  641. Sub AtOrigin(Optional px As Double = 0#, Optional py As Double = 0#)
  642. Dim doc As Document: Set doc = ActiveDocument
  643. doc.Unit = cdrMillimeter
  644. '// 导入原点标记标记文件 OriginMark.cdr 解散群组
  645. doc.ActiveLayer.Import path & "GMS\OriginMark.cdr"
  646. doc.ReferencePoint = cdrCenter
  647. doc.Selection.Ungroup
  648. Dim sh As Shape, shs As Shapes
  649. Set shs = ActiveSelection.Shapes
  650. '// 按 MarkName 名称查找 标记物件
  651. For Each sh In shs
  652. If "AtOrigin" = sh.ObjectData("MarkName").Value Then
  653. sh.SetPosition px, py
  654. Else
  655. sh.Delete ' 不需要的标记删除
  656. End If
  657. Next sh
  658. End Sub
  659. ' 使用 GlobalUserData 对象保存 Mark标记坐标文本,调用函数能设置文本
  660. Public Function Mark_SetPosition() As String
  661. Dim text As String
  662. If GlobalUserData.Exists("MarkPosition", 1) Then
  663. text = GlobalUserData("MarkPosition", 1)
  664. End If
  665. text = InputBox("请输入Mark标记坐标(x,y),空格或逗号间隔", "设置Mark标记坐标(x,y),单位(mm)", text)
  666. If text = "" Then Exit Function
  667. GlobalUserData("MarkPosition", 1) = text
  668. Mark_SetPosition = text
  669. End Function
  670. ' 调用设置Mark标记坐标功能,返回 数组(x,y)
  671. Public Function Get_MarkPosition() As Double()
  672. Dim MarkPos_Array(0 To 1) As Double
  673. Dim str, arr
  674. str = Mark_SetPosition
  675. ' 替换 逗号 为空格
  676. str = VBA.Replace(str, ",", " ")
  677. Do While InStr(str, " ") '多个空格换成一个空格
  678. str = VBA.Replace(str, " ", " ")
  679. Loop
  680. arr = Split(str)
  681. MarkPos_Array(0) = Val(arr(0))
  682. MarkPos_Array(1) = Val(arr(1))
  683. Debug.Print MarkPos_Array(0), MarkPos_Array(1) ' 视图->立即窗口,调试显示
  684. Get_MarkPosition = MarkPos_Array
  685. End Function
  686. Public Function SetNames()
  687. Dim ssr As ShapeRange
  688. Set ssr = ActiveSelectionRange
  689. #If VBA7 Then
  690. ssr.Sort " @shape1.left<@shape2.left"
  691. #Else
  692. ' X4 不支持 ShapeRange.sort
  693. #End If
  694. Dim text As String
  695. Dim lines() As String
  696. ' 提取文本信息,切割文本
  697. If ssr(1).Type = cdrTextShape Then
  698. If ssr(1).text.Type = cdrArtistic Then
  699. text = ssr(1).text.Story.text
  700. lines = Split(text, vbCr)
  701. ssr.Remove 1
  702. #If VBA7 Then
  703. ssr.Sort " @shape1.top>@shape2.top"
  704. #Else
  705. ' X4 不支持 ShapeRange.sort
  706. #End If
  707. End If
  708. Else
  709. MsgBox "请把多行文本放最左边"
  710. Exit Function
  711. End If
  712. ' Debug.Print ssr.Count, UBound(lines), LBound(lines)
  713. ' 给物件设置名称,用处:批量导出可以有一个名称
  714. i = 0
  715. If ssr.Count <= UBound(lines) + 1 Then
  716. For Each s In ssr
  717. s.Name = lines(i)
  718. i = i + 1
  719. Next s
  720. End If
  721. If ssr.Count <> UBound(lines) + 1 Then MsgBox "文本行:" & (UBound(lines) + 1) & vbNewLine & "右边物件:" & ssr.Count
  722. End Function
  723. Sub Nodes_TO_TSP()
  724. Set fs = CreateObject("Scripting.FileSystemObject")
  725. Set f = fs.CreateTextFile("C:\TSP\CDR_TO_TSP", True)
  726. ActiveDocument.Unit = cdrMillimeter
  727. Dim s As Shape, ssr As ShapeRange
  728. Set ssr = ActiveSelectionRange
  729. Dim TSP As String
  730. TSP = (ssr.Count * 4) & " " & 0 & vbNewLine
  731. For Each s In ssr
  732. lx = s.LeftX: rx = s.RightX
  733. By = s.BottomY: ty = s.TopY
  734. TSP = TSP & lx & " " & By & vbNewLine
  735. TSP = TSP & lx & " " & ty & vbNewLine
  736. TSP = TSP & rx & " " & By & vbNewLine
  737. TSP = TSP & rx & " " & ty & vbNewLine
  738. Next s
  739. f.WriteLine TSP
  740. f.Close
  741. End Sub
  742. '// 获得剪贴板文本字符
  743. Public Function GetClipBoardString() As String
  744. On Error Resume Next
  745. Dim MyData As New DataObject
  746. GetClipBoardString = ""
  747. MyData.GetFromClipboard
  748. GetClipBoardString = MyData.GetText
  749. Set MyData = Nothing
  750. End Function
  751. =======
  752. Public Function 角度转平()
  753. On Error GoTo ErrorHandler
  754. ' ActiveDocument.ReferencePoint = cdrCenter
  755. Set sr = ActiveSelectionRange
  756. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  757. If nr.Count = 2 Then
  758. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  759. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  760. a = lineangle(x1, y1, x2, y2): sr.Rotate -a
  761. ' sr.LastShape.Delete '// 删除参考线
  762. End If
  763. ErrorHandler:
  764. End Function
  765. Public Function 自动旋转角度()
  766. On Error GoTo ErrorHandler
  767. ' ActiveDocument.ReferencePoint = cdrCenter
  768. Set sr = ActiveSelectionRange
  769. Set nr = sr.LastShape.DisplayCurve.Nodes.All
  770. If nr.Count = 2 Then
  771. x1 = nr.FirstNode.PositionX: y1 = nr.FirstNode.PositionY
  772. x2 = nr.LastNode.PositionX: y2 = nr.LastNode.PositionY
  773. a = lineangle(x1, y1, x2, y2): sr.Rotate 90 + a
  774. sr.LastShape.Delete '// 删除参考线
  775. End If
  776. ErrorHandler:
  777. End Function
  778. Public Function 交换对象()
  779. Set sr = ActiveSelectionRange
  780. If sr.Count = 2 Then
  781. X = sr.LastShape.CenterX: Y = sr.LastShape.CenterY
  782. sr.LastShape.CenterX = sr.FirstShape.CenterX: sr.LastShape.CenterY = sr.FirstShape.CenterY
  783. sr.FirstShape.CenterX = X: sr.FirstShape.CenterY = Y
  784. End If
  785. End Function
  786. '// ===================================================
  787. Private Sub btn_autoalign_byrow_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  788. If get_events("btn_autoalign_byrow", Shift, Button) = "exit" Then Exit Sub
  789. autogroup("group_lines", 16 + Shift).CreateSelection
  790. End Sub
  791. Private Sub btn_autoalign_bycolumn_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  792. If get_events("btn_autoalign_bycolumn", Shift, Button) = "exit" Then Exit Sub
  793. autogroup("group_lines", 13 + Shift).CreateSelection
  794. End Sub
  795. Private Sub btn_autogroup_byrow_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  796. If get_events("btn_autogroup_byrow", Shift, Button) = "exit" Then Exit Sub
  797. autogroup("group_lines", 6).CreateSelection
  798. End Sub
  799. Private Sub btn_autogroup_bycolumn_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  800. If get_events("btn_autogroup_bycolumn", Shift, Button) = "exit" Then Exit Sub
  801. autogroup("group_lines", 3).CreateSelection
  802. End Sub
  803. Private Sub btn_autogroup_bysquare_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  804. If get_events("btn_autogroup_bysquare", Shift, Button) = "exit" Then Exit Sub
  805. autogroup("group").CreateSelection
  806. End Sub
  807. Private Sub btn_autogroup_byshape_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  808. If get_events("btn_autogroup_byshape", Shift, Button) = "exit" Then Exit Sub
  809. autogroup("group", 1).CreateSelection
  810. End Sub
  811. Public Sub begin_func(Optional undoname = "nul", Optional units = cdrMillimeter, Optional undogroup = True, Optional optimize = True, Optional sett = "before")
  812. ActiveDocument.SaveSettings sett
  813. ActiveDocument.Unit = units
  814. If undogroup Then ActiveDocument.BeginCommandGroup undoname
  815. Application.Optimization = optimize
  816. EventsEnabled = Not optimize
  817. End Sub
  818. Public Sub end_func(Optional undogroup = True, Optional sett = "before")
  819. cure_app undogroup
  820. ActiveDocument.RestoreSettings sett
  821. End Sub
  822. Sub cure_app(Optional undogroup = True)
  823. EventsEnabled = True
  824. Application.Optimization = False
  825. Application.Refresh
  826. DoEvents
  827. If undogroup Then ActiveDocument.EndCommandGroup
  828. End Sub
  829. Public Function collect_arr(arr, ci, ki)
  830. lim = UBound(arr)
  831. For k = 1 To lim
  832. If arr(ki, k) > 0 Then
  833. arr(ci, k) = k
  834. If ki <> ci Then arr(ki, k) = Empty
  835. If ci <> k And ki <> k Then arr = collect_arr(arr, ci, k)
  836. End If
  837. Next k
  838. 'If ki <> ci Then arr(ki, ki) = Empty
  839. collect_arr = arr
  840. End Function
  841. Public Function autogroup(Optional group As String = "group", Optional shft = 0, Optional sss As Shapes = Nothing, Optional undogroup = True) As ShapeRange
  842. Dim sr As ShapeRange, sr_all As ShapeRange, os As ShapeRange
  843. Dim sp As SubPaths
  844. Dim arr()
  845. Dim s As Shape
  846. If sss Is Nothing Then Set os = ActiveSelectionRange Else Set os = sss.All
  847. 'On Error GoTo errn
  848. If ActiveSelection.Shapes.Count > 0 Then
  849. begin_func "autogroup" & group, cdrMillimeter, undogroup
  850. gcnt = os.Shapes.Count
  851. ReDim arr(1 To gcnt, 1 To gcnt)
  852. Set sr_all = ActiveSelectionRange
  853. sr_all.RemoveAll
  854. If group = "group_lines" Then
  855. For i = 1 To gcnt
  856. If shft = 3 Or shft = 13 Or shft = 14 Then
  857. coord = Int(os.Shapes(i).CenterX)
  858. Else
  859. coord = Int(os.Shapes(i).CenterY)
  860. End If
  861. fnd = False
  862. For k = 1 To gcnt
  863. If arr(k, 1) > 0 Then
  864. If arr(k, 2) = coord Then
  865. arr(k, 1) = arr(k, 1) + 1
  866. arr(k, 2 + arr(k, 1)) = i
  867. fnd = True
  868. Exit For
  869. End If
  870. Else
  871. Exit For
  872. End If
  873. Next k
  874. If Not fnd Then
  875. arr(k, 1) = 1
  876. arr(k, 2) = coord
  877. arr(k, 3) = i
  878. End If
  879. Next i
  880. Set sr = ActiveSelectionRange
  881. For i = 1 To gcnt
  882. If arr(i, 1) > 0 Then
  883. sr.RemoveAll
  884. For k = 3 To gcnt
  885. If arr(i, k) > 0 Then sr.Add os.Shapes(arr(i, k))
  886. Next k
  887. If sr.Shapes.Count > 0 Then
  888. sr.CreateSelection
  889. If shft = 13 Then
  890. sr.AlignAndDistribute cdrAlignDistributeHNone, cdrAlignDistributeVDistributeSpacing
  891. ElseIf shft = 14 Then
  892. sr.AlignAndDistribute cdrAlignDistributeHNone, cdrAlignDistributeVDistributeCenter
  893. ElseIf shft = 16 Then
  894. sr.AlignAndDistribute cdrAlignDistributeHDistributeSpacing, cdrAlignDistributeVNone
  895. ElseIf shft = 17 Then
  896. sr.AlignAndDistribute cdrAlignDistributeHDistributeCenter, cdrAlignDistributeVNone
  897. Else
  898. sr.group
  899. End If
  900. sr_all.AddRange sr
  901. End If
  902. End If
  903. Next i
  904. Else
  905. ReDim arr(1 To gcnt, 1 To gcnt)
  906. ActiveDocument.Unit = cdrTenthMicron
  907. sgap = 10
  908. If shft = 2 Or shft = 3 Or shft = 6 Or shft = 7 Then
  909. os.RemoveAll
  910. For Each s In ActiveSelectionRange.Shapes
  911. os.Add ActivePage.SelectShapesFromRectangle(s.LeftX - sgap, s.BottomY - sgap, s.RightX + sgap, s.TopY + sgap, True)
  912. Next s
  913. End If
  914. For i = 1 To os.Shapes.Count
  915. Set s1 = os.Shapes(i)
  916. arr(i, i) = i
  917. For j = 1 To os.Shapes.Count
  918. Set s2 = os.Shapes(j)
  919. If s2.LeftX < s1.RightX + sgap And s2.RightX > s1.LeftX - sgap And s2.BottomY < s1.TopY + sgap And s2.TopY > s1.BottomY - sgap Then
  920. If shft = 1 Or shft = 3 Or shft = 5 Or shft = 7 Then
  921. Set isec = s1.Intersect(s2)
  922. If Not isec Is Nothing Then
  923. arr(i, j) = j
  924. isec.CreateSelection
  925. isec.Delete
  926. End If
  927. Else
  928. arr(i, j) = j
  929. End If
  930. End If
  931. Next j
  932. Next i
  933. For i = 1 To gcnt
  934. arr = collect_arr(arr, i, i)
  935. Next i
  936. Set sr = ActiveSelectionRange
  937. For i = 1 To gcnt
  938. sr.RemoveAll
  939. inar = 0
  940. For j = 1 To gcnt
  941. If arr(i, j) > 0 Then
  942. sr.Add os.Shapes(j)
  943. inar = inar + 1
  944. End If
  945. Next j
  946. If inar > 1 Then
  947. If group = "group" Then
  948. If shft < 4 Then sr_all.Add sr.group
  949. Else
  950. If group = "front" Then
  951. sr.Sort "@shape1.com.zOrder > @shape2.com.zOrder"
  952. ElseIf group = "back" Then
  953. sr.Sort "@shape1.com.zOrder < @shape2.com.zOrder"
  954. Else
  955. sr.Sort "@shape1.width*@shape1.height < @shape2.width*@shape2.height"
  956. End If
  957. Set fs = sr.FirstShape
  958. Set ls = sr.LastShape
  959. For Each s In sr.Shapes
  960. If Not s Is ls And Not s Is fs Then
  961. If group = "autocut" Then
  962. Set isec = ls.Intersect(s)
  963. If Not isec Is Nothing Then
  964. If isec.Curve.Area = s.Curve.Area Then
  965. Set ls = fs.Trim(ls, False)
  966. Else
  967. Set ls = fs.Weld(ls, False)
  968. End If
  969. isec.Delete
  970. End If
  971. Else
  972. Set fs = s.Weld(fs, False, False)
  973. End If
  974. End If
  975. Next s
  976. If group = "weld" Then
  977. Set ls = fs.Weld(ls, False)
  978. Else
  979. Set ls = fs.Trim(ls, False)
  980. End If
  981. sr_all.Add ls
  982. End If
  983. Else
  984. If sr.Shapes.Count > 0 Then sr_all.AddRange sr
  985. End If
  986. Next i
  987. End If
  988. Set autogroup = sr_all
  989. End If
  990. errn:
  991. end_func undogroup
  992. End Function
  993. Sub auto_cut()
  994. autogroup("autocut").CreateSelection
  995. End Sub
  996. Sub auto_big_small()
  997. autogroup("big").CreateSelection
  998. End Sub
  999. Sub auto_group()
  1000. autogroup.CreateSelection
  1001. End Sub
  1002. Sub auto_weld()
  1003. autogroup("weld").CreateSelection
  1004. End Sub
  1005. Sub auto_group_lines()
  1006. autogroup("group_lines", 6).CreateSelection
  1007. End Sub
  1008. Sub auto_group_columns()
  1009. autogroup("group_lines", 3).CreateSelection
  1010. End Sub
  1011. >>>>>>> 556e97d494ce938408287776a3528f332486766c