API.bas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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("LYVBA", "Settings", "SpeakHelp", "0")) '// 关停语音功能
  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("LYVBA", "Settings", "Bleed", "2.0"))
  38. Line_len = Val(GetSetting("LYVBA", "Settings", "Line_len", "3.0"))
  39. Outline_Width = Val(GetSetting("LYVBA", "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(Optional ByVal OnlyRead As Boolean = False) As Double
  60. Dim text As String
  61. If GlobalUserData.Exists("SpaceWidth", 1) Then
  62. text = GlobalUserData("SpaceWidth", 1)
  63. If OnlyRead Then
  64. Set_Space_Width = Val(text)
  65. Exit Function
  66. End If
  67. End If
  68. text = InputBox("请输入间隔宽度值 -99 --> 99", "设置间隔宽度(mm)", text)
  69. If text = "" Then Exit Function
  70. GlobalUserData("SpaceWidth", 1) = text
  71. Set_Space_Width = Val(text)
  72. End Function
  73. '// 获得剪贴板文本字符
  74. Public Function GetClipBoardString() As String
  75. On Error Resume Next
  76. Dim MyData As New DataObject
  77. GetClipBoardString = ""
  78. MyData.GetFromClipboard
  79. GetClipBoardString = MyData.GetText
  80. Set MyData = Nothing
  81. End Function
  82. '// 文本字符复制到剪贴板
  83. Public Function WriteClipBoard(ByVal s As String)
  84. On Error Resume Next
  85. ' VBA_WIN10(vba7) 使用PutInClipboard乱码解决办法
  86. #If VBA7 Then
  87. With CreateObject("Forms.TextBox.1")
  88. .MultiLine = True
  89. .text = s
  90. .SelStart = 0
  91. .SelLength = .TextLength
  92. .Copy
  93. End With
  94. #Else
  95. Dim MyData As New DataObject
  96. MyData.SetText s
  97. MyData.PutInClipboard
  98. #End If
  99. End Function
  100. '// 换行转空格 多个空格换成一个空格
  101. Public Function Newline_to_Space(ByVal Str As String) As String
  102. Str = VBA.Replace(Str, Chr(13), " ")
  103. Str = VBA.Replace(Str, Chr(9), " ")
  104. Do While InStr(Str, " ")
  105. Str = VBA.Replace(Str, " ", " ")
  106. Loop
  107. Newline_to_Space = Str
  108. End Function
  109. '// 获得数组元素个数
  110. Public Function arrlen(src As Variant) As Integer
  111. On Error Resume Next '空意味着 0 长度
  112. arrlen = (UBound(src) - LBound(src))
  113. End Function
  114. '// 对数组进行排序[单维]
  115. Public Function ArraySort(src As Variant) As Variant
  116. Dim out As Long, i As Long, tmp As Variant
  117. For out = LBound(src) To UBound(src) - 1
  118. For i = out + 1 To UBound(src)
  119. If src(out) > src(i) Then
  120. tmp = src(i): src(i) = src(out): src(out) = tmp
  121. End If
  122. Next i
  123. Next out
  124. ArraySort = src
  125. End Function
  126. '// 把一个数组倒序
  127. Public Function ArrayReverse(arr)
  128. Dim i As Integer, n As Integer
  129. n = UBound(arr)
  130. Dim P(): ReDim P(n)
  131. For i = 0 To n
  132. P(i) = arr(n - i)
  133. Next
  134. ArrayReverse = P
  135. End Function
  136. '// 测试数组排序
  137. Private Function test_ArraySort()
  138. Dim arr As Variant, i As Integer
  139. arr = Array(5, 4, 3, 2, 1, 9, 999, 33)
  140. For i = 0 To arrlen(arr) - 1
  141. Debug.Print arr(i);
  142. Next i
  143. Debug.Print arrlen(arr)
  144. ArraySort arr
  145. For i = 0 To arrlen(arr) - 1
  146. Debug.Print arr(i);
  147. Next i
  148. End Function
  149. '// 两点连线的角度:返回角度(相对于X轴的角度)
  150. '// p为末点,O为始点
  151. Public Function alfaPP(P, o)
  152. Dim pi As Double: pi = 4 * Atn(1)
  153. Dim beta As Double
  154. If P(0) = o(0) And P(1) = o(1) Then '二点重合
  155. alfaPP = 0
  156. Exit Function
  157. ElseIf P(0) = o(0) And P(1) > o(1) Then
  158. beta = pi / 2
  159. ElseIf P(0) = o(0) And P(1) < o(1) Then
  160. beta = -pi / 2
  161. ElseIf P(1) = o(1) And P(0) < o(0) Then
  162. beta = pi
  163. ElseIf P(1) = o(1) And P(0) > o(0) Then
  164. beta = 0
  165. Else
  166. beta = Atn((P(1) - o(1)) / VBA.Abs(P(0) - o(0)))
  167. If P(1) > o(1) And P(0) < o(0) Then
  168. beta = pi - beta
  169. ElseIf P(1) < o(1) And P(0) < o(0) Then
  170. beta = -(pi + beta)
  171. End If
  172. End If
  173. alfaPP = beta * 180 / pi
  174. End Function
  175. '// 求过P点到线段AB上的垂足点(XY平面内的二维计算)
  176. Public Function pFootInXY(P, a, b)
  177. If a(0) = b(0) Then
  178. pFootInXY = Array(a(0), P(1), 0#): Exit Function
  179. End If
  180. If a(1) = b(1) Then
  181. pFootInXY = Array(P(0), a(1), 0#): Exit Function
  182. End If
  183. Dim aa, bb, c, d, X, Y
  184. aa = (a(1) - b(1)) / (a(0) - b(0))
  185. bb = a(1) - aa * a(0)
  186. c = -(a(0) - b(0)) / (a(1) - b(1))
  187. d = P(1) - c * P(0)
  188. X = (d - bb) / (aa - c)
  189. Y = aa * X + bb
  190. pFootInXY = Array(X, Y, 0#)
  191. End Function
  192. Public Function FindAllShapes() As ShapeRange
  193. Dim s As Shape
  194. Dim srPowerClipped As New ShapeRange
  195. Dim sr As ShapeRange, srAll As New ShapeRange
  196. If ActiveSelection.Shapes.Count > 0 Then
  197. Set sr = ActiveSelection.Shapes.FindShapes()
  198. Else
  199. Set sr = ActivePage.Shapes.FindShapes()
  200. End If
  201. Do
  202. For Each s In sr.Shapes.FindShapes(Query:="[email protected]")
  203. srPowerClipped.AddRange s.PowerClip.Shapes.FindShapes()
  204. Next s
  205. srAll.AddRange sr
  206. sr.RemoveAll
  207. sr.AddRange srPowerClipped
  208. srPowerClipped.RemoveAll
  209. Loop Until sr.Count = 0
  210. Set FindAllShapes = srAll
  211. End Function
  212. ' ************* 函数模块 ************* '
  213. Public Function ExistsFile_UseFso(ByVal strPath As String) As Boolean
  214. Dim fso
  215. Set fso = CreateObject("Scripting.FileSystemObject")
  216. ExistsFile_UseFso = fso.FileExists(strPath)
  217. Set fso = Nothing
  218. End Function
  219. Public Function WebHelp(url As String)
  220. Dim h As Long, r As Long
  221. h = FindWindow(vbNullString, "Toolbar")
  222. r = ShellExecute(h, "", url, "", "", 1)
  223. End Function
  224. Public Function test_sapi()
  225. Dim message, sapi
  226. MsgBox ("Please use the headset and listen to what I have to say...")
  227. message = "This is a simple voice test on your Microsoft Windows."
  228. Set sapi = CreateObject("sapi.spvoice")
  229. sapi.Speak message
  230. End Function