明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2306|回复: 4

将多边形各边边长注记在各边中点上源代码(VBA)

[复制链接]
发表于 2003-12-17 15:55:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2003-12-17 17:43:48 编辑

不足之处,请完善
  1. Const pi = 3.1415926
  2. Sub add_dis_to_midp()
  3. Dim entry As Object
  4. Dim pickedp As Variant
  5. Dim pickedp1 As Variant
  6. Dim coors As Variant
  7. Dim vexn As Integer
  8. Dim i As Integer
  9. Dim midtext As AcadText
  10. Dim textstr As String
  11. Dim ang As Double
  12. Dim Ispoly As Boolean
  13. Ispoly = False
  14. On Error Resume Next
  15. Dim textheight1 As Double

  16. textheight1 = 1#  '定义字高
  17. Dim offsetvalue As Double
  18. offsetvalue = 0.5 '定义文字偏移直线的距离

  19. Dim inserttext(0 To 2) As Double
  20. ThisDrawing.Utility.GetEntity entry, pickedp, "选择一个多边形:"
  21. '获得顶点数
  22. If TypeName(entry) = "IAcadLWPolyline" Then
  23.     vexn = (UBound(entry.Coordinates) + 1) / 2
  24.     Ispoly = True
  25. End If
  26. If TypeName(entry) = "IAcadPolyline" Then
  27.     vexn = (UBound(entry.Coordinates) + 1) / 3
  28.     Ispoly = True
  29. End If

  30. If Ispoly Then
  31.     For i = 0 To vexn - 1 Step 1
  32.         pickedp = entry.Coordinate(i)
  33.         If i = vexn - 1 And entry.Closed = True Then
  34.         pickedp1 = entry.Coordinate(0)
  35.         Else
  36.         pickedp1 = entry.Coordinate(i + 1)
  37.         End If
  38.         '线段中点
  39.         inserttext(0) = (pickedp(0) + pickedp1(0)) / 2
  40.         inserttext(1) = (pickedp(1) + pickedp1(1)) / 2
  41.         inserttext(2) = 0
  42.         
  43.         '线段距离
  44.         textstr = CStr(dist(pickedp, pickedp1))
  45.         '文字角度
  46.         'ang = ThisDrawing.Utility.AngleFromXAxis(pickedp, pickedp1)
  47.         ang = getang(pickedp, pickedp1) '+ pi   加pi表示文字在前进反向的右边,不加是左边
  48.         '偏移处理
  49.         pickedp = ThisDrawing.Utility.PolarPoint(inserttext, ang + pi / 2, offsetvalue)
  50.         
  51.         inserttext(0) = pickedp(0)
  52.         inserttext(1) = pickedp(1)
  53.         inserttext(2) = 0
  54.         '写文字
  55.         If textstr <> "0" Then '0距离不处理
  56.         Set midtext = ThisDrawing.ModelSpace.AddText(textstr, inserttext, textheight1)
  57.         
  58.         midtext.Alignment = acAlignmentCenter
  59.         midtext.TextAlignmentPoint = inserttext
  60.         midtext.Rotation = ang
  61.         End If
  62.     Next
  63. End If
  64. End Sub

  65. Function dist(p1 As Variant, p2 As Variant) As Double '距离
  66. On Error Resume Next
  67. dist = Sqr(((p1(0) - p2(0)) ^ 2 + (p1(1) - p2(1)) ^ 2))
  68. If Err Then
  69. Err.Clear
  70. dist = 0
  71. End If
  72. End Function


  73. Function getang(p1 As Variant, p2 As Variant) '角度
  74. On Error Resume Next
  75. Dim pl As AcadLine
  76. Dim startp(0 To 2) As Double
  77. Dim endp(0 To 2) As Double
  78. startp(0) = p1(0)
  79. startp(1) = p1(1)
  80. startp(2) = 0
  81. endp(0) = p2(0)
  82. endp(1) = p2(1)
  83. endp(2) = 0
  84. Set pl = ThisDrawing.ModelSpace.AddLine(startp, endp)
  85. getang = pl.Angle
  86. pl.Delete
  87. End Function

发表于 2003-12-17 16:15:00 | 显示全部楼层
计算角度可以用AngleFromXAxis 方法
标注的文字最好按照当前标注样式的大小和小数点位数来标注,并注意文字的旋转角度。
 楼主| 发表于 2003-12-17 17:43:00 | 显示全部楼层
ang = ThisDrawing.Utility.AngleFromXAxis(pickedp, pickedp1) + pi
跟ang = getang(pickedp, pickedp1) + pi

运行的结果不同,何解?
发表于 2003-12-17 19:35:00 | 显示全部楼层
谢谢各位老大帮了我大忙!
发表于 2003-12-18 22:38:00 | 显示全部楼层
可能是因为AngleFromXAxis方法需要三维点,而二维多段线的顶点是二维点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-28 10:33 , Processed in 0.175796 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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