Tools.bas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. Attribute VB_Name = "Tools"
  2. Public Sub 填入居中文字(Str)
  3. Dim s As Shape
  4. Set s = ActiveSelection
  5. x = s.CenterX
  6. Y = s.CenterY
  7. Set s = ActiveLayer.CreateArtisticText(0, 0, Str)
  8. s.CenterX = x
  9. s.CenterY = Y
  10. End Sub
  11. Public Sub 尺寸标注()
  12. ActiveDocument.Unit = cdrMillimeter
  13. Set s = ActiveSelection
  14. x = s.CenterX: Y = s.TopY
  15. sw = s.SizeWidth: sh = s.SizeHeight
  16. Text = Int(sw) & "x" & Int(sh) & "mm"
  17. Set s = ActiveLayer.CreateArtisticText(0, 0, Text)
  18. s.CenterX = x: s.BottomY = Y + 5
  19. End Sub
  20. Public Sub 批量居中文字(Str)
  21. Dim s As Shape, sr As ShapeRange
  22. Set sr = ActiveSelectionRange
  23. For Each s In sr.Shapes
  24. x = s.CenterX: Y = s.CenterY
  25. Set s = ActiveLayer.CreateArtisticText(0, 0, Str)
  26. s.CenterX = x: s.CenterY = Y
  27. Next
  28. End Sub
  29. Public Sub 批量标注()
  30. ActiveDocument.Unit = cdrMillimeter
  31. Set sr = ActiveSelectionRange
  32. For Each s In sr.Shapes
  33. x = s.CenterX: Y = s.TopY
  34. sw = s.SizeWidth: sh = s.SizeHeight
  35. Text = Int(sw + 0.5) & "x" & Int(sh + 0.5) & "mm"
  36. Set s = ActiveLayer.CreateArtisticText(0, 0, Text)
  37. s.CenterX = x: s.BottomY = Y + 5
  38. Next
  39. End Sub
  40. Public Sub 智能群组()
  41. Set s1 = ActiveSelectionRange.CustomCommand("Boundary", "CreateBoundary")
  42. Set brk1 = s1.BreakApartEx
  43. For Each s In brk1
  44. Set sh = ActivePage.SelectShapesFromRectangle(s.LeftX, s.TopY, s.RightX, s.BottomY, True)
  45. sh.Shapes.All.Group
  46. s.Delete
  47. Next
  48. End Sub
  49. Private Function 对角线角度(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double
  50. pi = 4 * VBA.Atn(1) ' 计算圆周率'
  51. 对角线角度 = VBA.Atn((y2 - y1) / (x2 - x1)) / pi * 180
  52. End Function
  53. Public Sub 角度转平()
  54. ActiveDocument.ReferencePoint = cdrCenter
  55. Dim sr As ShapeRange '定义物件范围
  56. Set sr = ActiveSelectionRange
  57. Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double
  58. Dim Shift As Long
  59. Dim b As Boolean
  60. b = ActiveDocument.GetUserArea(x1, y1, x2, y2, Shift, 10, False, 306)
  61. If Not b Then
  62. a = 对角线角度(x1, y1, x2, y2)
  63. sr.Rotate -a
  64. End If
  65. End Sub
  66. ' 实践应用: 选择物件群组,页面设置物件大小,物件页面居中
  67. Public Function 群组居中页面()
  68. ActiveDocument.Unit = cdrMillimeter
  69. Dim OrigSelection As ShapeRange, sh As Shape
  70. Set OrigSelection = ActiveSelectionRange
  71. Set sh = OrigSelection.Group
  72. ' MsgBox "选择物件尺寸: " & sh.SizeWidth & "x" & sh.SizeHeight
  73. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  74. #If VBA7 Then
  75. ActiveDocument.ClearSelection
  76. sh.AddToSelection
  77. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  78. #Else
  79. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  80. #End If
  81. End Function
  82. Public Function 批量多页居中()
  83. If 0 = ActiveSelectionRange.Count Then Exit Function
  84. On Error GoTo ErrorHandler
  85. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  86. ActiveDocument.Unit = cdrMillimeter
  87. Set sr = ActiveSelectionRange
  88. total = sr.Count
  89. '// 建立多页面
  90. Set doc = ActiveDocument
  91. doc.AddPages (total - 1)
  92. Dim sh As Shape
  93. '// 遍历批量物件,放置物件到页面
  94. For i = 1 To sr.Count
  95. doc.Pages(i).Activate
  96. Set sh = sr.Shapes(i)
  97. ActivePage.SetSize Int(sh.SizeWidth + 0.9), Int(sh.SizeHeight + 0.9)
  98. '// 物件居中页面
  99. #If VBA7 Then
  100. ActiveDocument.ClearSelection
  101. sh.AddToSelection
  102. ActiveSelection.AlignAndDistribute 3, 3, 2, 0, False, 2
  103. #Else
  104. sh.AlignToPageCenter cdrAlignHCenter + cdrAlignVCenter
  105. #End If
  106. Next i
  107. ActiveDocument.EndCommandGroup: Application.Optimization = False
  108. ActiveWindow.Refresh: Application.Refresh
  109. Exit Function
  110. ErrorHandler:
  111. Application.Optimization = False
  112. MsgBox "请先选择一些物件"
  113. On Error Resume Next
  114. End Function
  115. '// 安全线: 点击一次建立辅助线,再调用清除参考线
  116. Public Function guideangle(actnumber As ShapeRange, cardblood As Integer)
  117. Dim sr As ShapeRange
  118. Set sr = ActiveDocument.MasterPage.GuidesLayer.FindShapes(Type:=cdrGuidelineShape)
  119. If sr.Count <> 0 Then
  120. sr.Delete
  121. Exit Function
  122. End If
  123. If 0 = ActiveSelectionRange.Count Then Exit Function
  124. ActiveDocument.Unit = cdrMillimeter
  125. With actnumber
  126. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .TopY - cardblood, 0#)
  127. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(0, .BottomY + cardblood, 0#)
  128. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.LeftX + cardblood, 0, 90#)
  129. Set s1 = ActiveDocument.MasterPage.GuidesLayer.CreateGuideAngle(.RightX - cardblood, 0, 90#)
  130. End With
  131. End Function
  132. Public Function 按面积排列(space_width As Double)
  133. If 0 = ActiveSelectionRange.Count Then Exit Function
  134. ActiveDocument.Unit = cdrMillimeter
  135. ActiveDocument.ReferencePoint = cdrCenter
  136. Set ssr = ActiveSelectionRange
  137. cnt = 1
  138. #If VBA7 Then
  139. ssr.Sort "@shape1.width * @shape1.height < @shape2.width * @shape2.height"
  140. #Else
  141. ' X4 不支持 ShapeRange.sort
  142. #End If
  143. Dim Str As String, size As String
  144. For Each sh In ssr
  145. size = Int(sh.SizeWidth + 0.5) & "x" & Int(sh.SizeHeight + 0.5) & "mm"
  146. sh.SetSize Int(sh.SizeWidth + 0.5), Int(sh.SizeHeight + 0.5)
  147. Str = Str & size & vbNewLine
  148. Next sh
  149. ActiveDocument.ReferencePoint = cdrTopLeft
  150. For Each s In ssr
  151. If cnt > 1 Then s.SetPosition ssr(cnt - 1).LeftX, ssr(cnt - 1).BottomY - space_width
  152. cnt = cnt + 1
  153. Next s
  154. ' 写文件,可以EXCEL里统计
  155. ' Set fs = CreateObject("Scripting.FileSystemObject")
  156. ' Set f = fs.CreateTextFile("D:\size.txt", True)
  157. ' f.WriteLine str: f.Close
  158. Str = 分类汇总(Str)
  159. Debug.Print Str
  160. Dim s1 As Shape
  161. Set s1 = ActiveLayer.CreateParagraphText(0, 0, 100, 150, Str, Font:="华文中宋")
  162. End Function
  163. '// 实现Excel里分类汇总功能
  164. Private Function 分类汇总(Str As String) As String
  165. Dim a, b, d, arr
  166. Str = VBA.Replace(Str, vbNewLine, " ")
  167. Do While InStr(Str, " ")
  168. Str = VBA.Replace(Str, " ", " ")
  169. Loop
  170. arr = Split(Str)
  171. Set d = CreateObject("Scripting.dictionary")
  172. For i = 0 To UBound(arr) - 1
  173. If d.Exists(arr(i)) = True Then
  174. d.Item(arr(i)) = d.Item(arr(i)) + 1
  175. Else
  176. d.Add arr(i), 1
  177. End If
  178. Next
  179. Str = " 规 格" & vbTab & vbTab & vbTab & "数量" & vbNewLine
  180. a = d.keys: b = d.items
  181. For i = 0 To d.Count - 1
  182. ' Debug.Print a(i), b(i)
  183. Str = Str & a(i) & vbTab & vbTab & b(i) & "条" & vbNewLine
  184. Next
  185. 分类汇总 = Str & "合计总量:" & vbTab & vbTab & vbTab & UBound(arr) & "条" & vbNewLine
  186. End Function