API.bas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. Attribute VB_Name = "API"
  2. '// This is free and unencumbered software released into the public domain.
  3. '// For more information, please refer to https://github.com/hongwenjun
  4. '// Attribute VB_Name = "CorelVBA工具窗口启动" CorelVBA Tool Window Launches 2023.6.11
  5. Public Sub Start()
  6. Toolbar.Show 0
  7. End Sub
  8. '// CorelDRAW 窗口刷新优化和关闭
  9. Public Function BeginOpt(Optional ByVal Name As String = "Undo")
  10. EventsEnabled = False
  11. ActiveDocument.BeginCommandGroup Name
  12. ActiveDocument.SaveSettings
  13. ActiveDocument.Unit = cdrMillimeter
  14. Optimization = True
  15. ' ActiveDocument.PreserveSelection = False
  16. End Function
  17. Public Function EndOpt()
  18. ' ActiveDocument.PreserveSelection = True
  19. ActiveDocument.RestoreSettings
  20. EventsEnabled = True
  21. Optimization = False
  22. EventsEnabled = True
  23. Application.Refresh
  24. ActiveDocument.EndCommandGroup
  25. End Function
  26. Public Function Speak_Msg(message As String)
  27. Speak_Help = Val(GetSetting("262235.xyz", "Settings", "SpeakHelp", "1"))
  28. If Val(Speak_Help) = 1 Then
  29. Dim sapi
  30. Set sapi = CreateObject("sapi.spvoice")
  31. sapi.Speak message
  32. Else
  33. ' 不说话
  34. End If
  35. End Function
  36. Public Function GetSet(s As String)
  37. Bleed = Val(GetSetting("262235.xyz", "Settings", "Bleed", "2.0"))
  38. Line_len = Val(GetSetting("262235.xyz", "Settings", "Line_len", "3.0"))
  39. Outline_Width = Val(GetSetting("262235.xyz", "Settings", "Outline_Width", "0.2"))
  40. ' Debug.Print Bleed, Line_len, Outline_Width
  41. If s = "Bleed" Then
  42. GetSet = Bleed
  43. ElseIf s = "Line_len" Then
  44. GetSet = Line_len
  45. ElseIf s = "Outline_Width" Then
  46. GetSet = Outline_Width
  47. End If
  48. End Function
  49. Public Function Create_Tolerance() As Double
  50. Dim text As String
  51. If GlobalUserData.Exists("Tolerance", 1) Then
  52. text = GlobalUserData("Tolerance", 1)
  53. End If
  54. text = InputBox("请输入容差值 0.1 --> 9.9", "容差值(mm)", text)
  55. If text = "" Then Exit Function
  56. GlobalUserData("Tolerance", 1) = text
  57. Create_Tolerance = Val(text)
  58. End Function
  59. Public Function Set_Space_Width() As Double
  60. Dim text As String
  61. If GlobalUserData.Exists("SpaceWidth", 1) Then
  62. text = GlobalUserData("SpaceWidth", 1)
  63. End If
  64. text = InputBox("请输入间隔宽度值 -99 --> 99", "设置间隔宽度(mm)", text)
  65. If text = "" Then Exit Function
  66. GlobalUserData("SpaceWidth", 1) = text
  67. Set_Space_Width = Val(text)
  68. End Function
  69. '// 获得剪贴板文本字符
  70. Public Function GetClipBoardString() As String
  71. On Error Resume Next
  72. Dim MyData As New DataObject
  73. GetClipBoardString = ""
  74. MyData.GetFromClipboard
  75. GetClipBoardString = MyData.GetText
  76. Set MyData = Nothing
  77. End Function
  78. '// 文本字符复制到剪贴板
  79. Public Function WriteClipBoard(ByVal s As String)
  80. On Error Resume Next
  81. ' VBA_WIN10(vba7) 使用PutInClipboard乱码解决办法
  82. #If VBA7 Then
  83. With CreateObject("Forms.TextBox.1")
  84. .MultiLine = True
  85. .text = s
  86. .SelStart = 0
  87. .SelLength = .TextLength
  88. .Copy
  89. End With
  90. #Else
  91. Dim MyData As New DataObject
  92. MyData.SetText s
  93. MyData.PutInClipboard
  94. #End If
  95. End Function
  96. '// 获得数组元素个数
  97. Public Function arrlen(src As Variant) As Integer
  98. On Error Resume Next '空意味着 0 长度
  99. arrlen = (UBound(src) - LBound(src))
  100. End Function
  101. '// 对数组进行排序[单维]
  102. Public Function ArraySort(src As Variant) As Variant
  103. Dim out As Long, i As Long, tmp As Variant
  104. For out = LBound(src) To UBound(src) - 1
  105. For i = out + 1 To UBound(src)
  106. If src(out) > src(i) Then
  107. tmp = src(i): src(i) = src(out): src(out) = tmp
  108. End If
  109. Next i
  110. Next out
  111. ArraySort = src
  112. End Function
  113. '// 把一个数组倒序
  114. Public Function ArrayReverse(arr)
  115. Dim i As Integer, n As Integer
  116. n = UBound(arr)
  117. Dim p(): ReDim p(n)
  118. For i = 0 To n
  119. p(i) = arr(n - i)
  120. Next
  121. ArrayReverse = p
  122. End Function
  123. '// 测试数组排序
  124. Private Function test_ArraySort()
  125. Dim arr As Variant, i As Integer
  126. arr = Array(5, 4, 3, 2, 1, 9, 999, 33)
  127. For i = 0 To arrlen(arr) - 1
  128. Debug.Print arr(i);
  129. Next i
  130. Debug.Print arrlen(arr)
  131. ArraySort arr
  132. For i = 0 To arrlen(arr) - 1
  133. Debug.Print arr(i);
  134. Next i
  135. End Function
  136. '// 两点连线的角度:返回角度(相对于X轴的角度)
  137. '// p为末点,O为始点
  138. Public Function alfaPP(p, o)
  139. Dim pi As Double: pi = 4 * Atn(1)
  140. Dim beta As Double
  141. If p(0) = o(0) And p(1) = o(1) Then '二点重合
  142. alfaPP = 0
  143. Exit Function
  144. ElseIf p(0) = o(0) And p(1) > o(1) Then
  145. beta = pi / 2
  146. ElseIf p(0) = o(0) And p(1) < o(1) Then
  147. beta = -pi / 2
  148. ElseIf p(1) = o(1) And p(0) < o(0) Then
  149. beta = pi
  150. ElseIf p(1) = o(1) And p(0) > o(0) Then
  151. beta = 0
  152. Else
  153. beta = Atn((p(1) - o(1)) / VBA.Abs(p(0) - o(0)))
  154. If p(1) > o(1) And p(0) < o(0) Then
  155. beta = pi - beta
  156. ElseIf p(1) < o(1) And p(0) < o(0) Then
  157. beta = -(pi + beta)
  158. End If
  159. End If
  160. alfaPP = beta * 180 / pi
  161. End Function
  162. '// 求过P点到线段AB上的垂足点(XY平面内的二维计算)
  163. Public Function pFootInXY(p, a, b)
  164. If a(0) = b(0) Then
  165. pFootInXY = Array(a(0), p(1), 0#): Exit Function
  166. End If
  167. If a(1) = b(1) Then
  168. pFootInXY = Array(p(0), a(1), 0#): Exit Function
  169. End If
  170. Dim aa, bb, c, d, X, Y
  171. aa = (a(1) - b(1)) / (a(0) - b(0))
  172. bb = a(1) - aa * a(0)
  173. c = -(a(0) - b(0)) / (a(1) - b(1))
  174. d = p(1) - c * p(0)
  175. X = (d - bb) / (aa - c)
  176. Y = aa * X + bb
  177. pFootInXY = Array(X, Y, 0#)
  178. End Function
  179. Function FindAllShapes() As ShapeRange
  180. Dim s As Shape
  181. Dim srPowerClipped As New ShapeRange
  182. Dim sr As ShapeRange, srAll As New ShapeRange
  183. If ActiveSelection.Shapes.Count > 0 Then
  184. Set sr = ActiveSelection.Shapes.FindShapes()
  185. Else
  186. Set sr = ActivePage.Shapes.FindShapes()
  187. End If
  188. Do
  189. For Each s In sr.Shapes.FindShapes(Query:="[email protected]")
  190. srPowerClipped.AddRange s.PowerClip.Shapes.FindShapes()
  191. Next s
  192. srAll.AddRange sr
  193. sr.RemoveAll
  194. sr.AddRange srPowerClipped
  195. srPowerClipped.RemoveAll
  196. Loop Until sr.Count = 0
  197. Set FindAllShapes = srAll
  198. End Function
  199. ' ************* 函数模块 ************* '
  200. Function ExistsFile_UseFso(ByVal strPath As String) As Boolean
  201. Dim fso
  202. Set fso = CreateObject("Scripting.FileSystemObject")
  203. ExistsFile_UseFso = fso.FileExists(strPath)
  204. Set fso = Nothing
  205. End Function
  206. Function test()
  207. Dim message, sapi
  208. MsgBox ("Please use the headset and listen to what I have to say...")
  209. message = "This is a simple voice test on your Microsoft Windows."
  210. Set sapi = CreateObject("sapi.spvoice")
  211. sapi.Speak message
  212. End Function