找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 614|回复: 2

[每日一码] 一组函数

[复制链接]

已领礼包: 1个

财富等级: 恭喜发财

发表于 2017-5-30 14:05:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. Option Explicit

  2. 'LEAST_OF_DOUBLE + 1# =1#
  3. Public Const LEAST_OF_DOUBLE# = 0.00000001
  4. Public Const PI# = 3.14159265358979
  5. Public Const SECONDS_OF_RAD# = 206264.806247096
  6. Public Const RADS_OF_SECOND# = 4.84813681109537E-06

  7. '添加菜单,如果已存在则返回该菜单对象,如果不存在则创建菜单并返回创建的菜单对象
  8. Public Function csAddMenu(PopupMenuName As String) As AcadPopupMenu
  9. Dim i&
  10. Dim currMenuGroup As AcadMenuGroup
  11. Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0)
  12. For i = 0 To currMenuGroup.Menus.Count - 1
  13. If currMenuGroup.Menus.Item(i).Name = PopupMenuName Then
  14. Set csAddMenu = currMenuGroup.Menus.Item(i)
  15. Exit Function
  16. End If
  17. Next i
  18. Set csAddMenu = currMenuGroup.Menus.Add(PopupMenuName)
  19. currMenuGroup.Menus.InsertMenuInMenuBar PopupMenuName, currMenuGroup.Menus.Count
  20. End Function

  21. '在指定的菜单添加菜单条,如果菜单条已存在则根据 overwrite 参数是否覆盖,返回该菜单条对象,如果不存在则创建菜单条并返回创建的菜单条对象
  22. Public Function csAddMenuItem(PopupMenuName As String, MenuItemName As String, MenuMacro As String, OverWrite As Boolean) As AcadPopupMenuItem
  23. Dim i&
  24. Dim newMenu As AcadPopupMenu
  25. Set newMenu = csAddMenu(PopupMenuName)
  26. For i = 0 To newMenu.Count - 1
  27. If newMenu.Item(i).Label = MenuItemName Then
  28. If OverWrite Then
  29. newMenu.Item(i).Delete
  30. Exit For
  31. Else
  32. Set csAddMenuItem = newMenu.Item(i)
  33. Exit Function
  34. End If
  35. End If
  36. Next i
  37. Set csAddMenuItem = csAddMenu(PopupMenuName).AddMenuItem(10000, MenuItemName, MenuMacro)
  38. End Function



  39. '添加图层,如果已存在则返回该图层对象,如果不存在则创建图层并返回创建的图层对象
  40. Public Function csAddLayer(layerName As String) As AcadLayer
  41. Dim i As Long
  42. For i = 0 To ThisDrawing.Layers.Count - 1
  43. If ThisDrawing.Layers.Item(i).Name = layerName Then
  44. Set csAddLayer = ThisDrawing.Layers.Item(i)
  45. Exit Function
  46. End If
  47. Next i
  48. If ThisDrawing.Layers.Count = i Then
  49. Set csAddLayer = ThisDrawing.Layers.Add(layerName)
  50. End If
  51. End Function

  52. '添加选择集,如果已存在则返回该选择集对象,如果不存在则创建选择集并返回创建的选择集对象
  53. Public Function csAddSelectionSet(SelectionSetName As String) As AcadSelectionSet
  54. Dim i As Long
  55. If ThisDrawing.SelectionSets.Count > 0 Then
  56. For i = 0 To ThisDrawing.SelectionSets.Count - 1
  57. If ThisDrawing.SelectionSets(i).Name = SelectionSetName Then
  58. Set csAddSelectionSet = ThisDrawing.SelectionSets(i)
  59. Exit For
  60. End If
  61. Next i
  62. If i = ThisDrawing.SelectionSets.Count Then
  63. Set csAddSelectionSet = ThisDrawing.SelectionSets.Add(SelectionSetName)
  64. End If
  65. Else
  66. Set csAddSelectionSet = ThisDrawing.SelectionSets.Add(SelectionSetName)
  67. End If
  68. End Function

  69. '根据一个对象(多段线、二维多段线、三维多段线)返回该对象的所有节点平面坐标(x0, y0, 0, x1, y1, 0,……)
  70. Public Function csGetPolygon(ent As AcadEntity) As Double()
  71. Dim vlen&, i&
  72. Dim polygon() As Double
  73. If ent.ObjectName = "AcDbPolyline" Then
  74. Dim lwpl As AcadLWPolyline
  75. Set lwpl = ent
  76. vlen = (UBound(lwpl.Coordinates) - LBound(lwpl.Coordinates) + 1) / 2
  77. ReDim polygon(vlen * 3 - 1)
  78. For i = 0 To vlen - 1
  79. polygon(i * 3) = lwpl.Coordinates(i * 2)
  80. polygon(i * 3 + 1) = lwpl.Coordinates(i * 2 + 1)
  81. polygon(i * 3 + 2) = 0
  82. Next i
  83. '二维多段线
  84. ElseIf ent.ObjectName = "AcDb2dPolyline" Then
  85. Dim pl As AcadPolyline
  86. Set pl = ent
  87. vlen = (UBound(pl.Coordinates) - LBound(pl.Coordinates) + 1) / 3
  88. ReDim polygon(vlen * 3 - 1)
  89. For i = 0 To vlen - 1
  90. polygon(i * 3) = pl.Coordinates(i * 3)
  91. polygon(i * 3 + 1) = pl.Coordinates(i * 3 + 1)
  92. polygon(i * 3 + 2) = 0
  93. Next i
  94. '三维多段线
  95. ElseIf ent.ObjectName = "AcDb3dPolyline" Then
  96. Dim dpl As Acad3DPolyline
  97. Set dpl = ent
  98. vlen = (UBound(dpl.Coordinates) - LBound(dpl.Coordinates) + 1) / 3
  99. ReDim polygon(vlen * 3 - 1)
  100. For i = 0 To vlen - 1
  101. polygon(i * 3) = dpl.Coordinates(i * 3)
  102. polygon(i * 3 + 1) = dpl.Coordinates(i * 3 + 1)
  103. polygon(i * 3 + 2) = 0
  104. Next i
  105. Else
  106. Exit Function
  107. End If
  108. csGetPolygon = polygon
  109. End Function

  110. '检查文件是否存在,存在返回 true, 不存在返回 false
  111. Public Function CheckFileExist(FileFullName As String) As Boolean
  112. Dim fso As Object
  113. Set fso = CreateObject("Scripting.FileSystemObject")
  114. If fso.fileexists(FileFullName) Then
  115. CheckFileExist = True
  116. Else
  117. CheckFileExist = False
  118. End If
  119. End Function

  120. '查看扩展数据
  121. Sub XdataView()
  122. Dim sset As AcadSelectionSet
  123. Set sset = csAddSelectionSet("ss1")
  124. sset.Clear
  125. sset.SelectOnScreen

  126. ' 定义扩展数据变量以保存扩展数据信息
  127. Dim xdataType As Variant
  128. Dim xdata As Variant
  129. Dim xd As Variant

  130. '定义索引计数器
  131. Dim xdi As Integer
  132. xdi = 0

  133. ' 遍历选择集中的对象
  134. ' 并检索对象的扩展数据
  135. Dim msgstr As String
  136. Dim appName As String
  137. Dim ent As AcadEntity
  138. appName = ""
  139. For Each ent In sset
  140. msgstr = ""
  141. xdi = 0

  142. ' 检索 appName 扩展数据类型和值
  143. ent.GetXData appName, xdataType, xdata

  144. ' 如果未初始化 xdataType 变量,
  145. ' 则没有可供该图元检索的 appName 扩展数据
  146. If VarType(xdataType) <> vbEmpty Then
  147. For Each xd In xdata
  148. msgstr = msgstr & vbCrLf & xdataType(xdi) _
  149. & ": " & xd
  150. xdi = xdi + 1
  151. Next xd
  152. End If

  153. ' 如果 msgstr 变量为 NULL,则没有扩展数据
  154. If msgstr = "" Then msgstr = vbCrLf & "NONE"
  155. MsgBox appName & " xdata on " & ent.ObjectName & _
  156. ":" & vbCrLf & msgstr
  157. Next ent
  158. End Sub

  159. '反正切函数,返回 0 ~ 2π
  160. Public Function CSAtn(dx As Double, dy As Double) As Double
  161. If dy <> 0 Then
  162. If dy > 0 And Abs(dx / dy) < 0.0000000001 Then CSAtn = PI * 0.5: Exit Function
  163. If dy < 0 And Abs(dx / dy) < 0.0000000001 Then CSAtn = PI * 1.5: Exit Function
  164. End If
  165. CSAtn = Atn(dy / dx)
  166. If (dx < 0) Then CSAtn = PI + CSAtn
  167. If (dx > 0) And (CSAtn < 0) Then CSAtn = 2 * PI + CSAtn
  168. End Function

  169. '由两组坐标返回距离
  170. Public Function CSDistance(y1 As Double, x1 As Double, y2 As Double, x2 As Double) As Double
  171. CSDistance = Sqr((y1 - y2) ^ 2 + (x1 - x2) ^ 2)
  172. End Function

  173. '将弧度化成 DDD°MM′SS″ 表示的字符串
  174. Public Function Rad2DegreeString(Rad As Double) As String
  175. Dim D&, m&, t&, S&
  176. t = Rad * 206264.806247096
  177. D = t \ 3600
  178. m = (t - D * 3600) \ 60
  179. S = t - D * 3600 - m * 60
  180. Rad2DegreeString = D & "°" & Format(m, "00") & "′" & Format(S, "00") & "″"
  181. End Function


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

发表于 2017-5-30 15:40:32 | 显示全部楼层
看着像 VBA 代码
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 3191个

财富等级: 富可敌国

发表于 2017-5-31 11:10:44 | 显示全部楼层
函数太好,谢谢。好好学习天天向上
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-18 21:25 , Processed in 0.417015 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表