明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1222|回复: 0

[转帖]VBA-三点划弧

[复制链接]
发表于 2008-5-7 18:09:00 | 显示全部楼层 |阅读模式
研究一下这个帖子,将三点划弧做成模板。
http://www.cnblogs.com/raymond19840709/archive/2007/04/12/710393.html
  1. Private Function GetCenOf3Pt(pt1 As Variant, pt2 As Variant, pt3 As Variant, ByRef radius As Double) As Variant
  2. ''''根据三点计算出圆心和半径
  3. Dim xysm, xyse, xy As Double
  4. Dim ptCen(2) As Double
  5. xy = pt1(0) ^ 2 + pt1(1) ^ 2
  6. xyse = xy - pt3(0) ^ 2 - pt3(1) ^ 2
  7. xysm = xy - pt2(0) ^ 2 - pt2(1) ^ 2
  8. xy = (pt1(0) - pt2(0)) * (pt1(1) - pt3(1)) - (pt1(0) - pt3(0)) * (pt1(1) - pt2(1))
  9. '''判断参数有效性
  10. If Abs(xy) < 0.000001 Then
  11. MsgBox "所输入的参数无法创建圆形!"
  12. Exit Function
  13. End If
  14. '获得圆心和半径
  15. ptCen(0) = (xysm * (pt1(1) - pt3(1)) - xyse * (pt1(1) - pt2(1))) / (2 * xy)
  16. ptCen(1) = (xyse * (pt1(0) - pt2(0)) - xysm * (pt1(0) - pt3(0))) / (2 * xy)
  17. ptCen(2) = 0
  18. radius = Sqr((pt1(0) - ptCen(0)) * (pt1(0) - ptCen(0)) + (pt1(1) - ptCen(1)) * (pt1(1) - ptCen(1)))
  19. If radius < 0.000001 Then
  20. MsgBox "半径过小!"
  21. Exit Function
  22. End If
  23. ''函数返回圆心的位置,而半径则在参数中通过引用方式返回
  24. GetCenOf3Pt = ptCen
  25. End Function
  26. Public Function AddArc3Pt(ByVal ptSt As Variant, ByVal ptSc As Variant, ByVal ptEn As Variant) As AcadArc
  27. ''''三点法创建圆弧
  28. Dim objArc As AcadArc
  29. Dim ptCen As Variant
  30. Dim radius As Double
  31. ptCen = GetCenOf3Pt(ptSt, ptSc, ptEn, radius)
  32. If isClockWise(ptCen, ptSt, ptSc, ptEn) Then
  33. Set objArc = AddArcCSEP(ptCen, ptSt, ptEn)
  34. Else
  35. Set objArc = AddArcCSEP(ptCen, ptEn, ptSt)
  36. End If
  37. objArc.Update
  38. Set AddArc3Pt = objArc
  39. End Function
  40. Private Function AddArcCSEP(ByVal ptCen As Variant, ByVal ptSt As Variant, ByVal ptEn As Variant) As AcadArc
  41. Dim objArc As AcadArc
  42. Dim radius As Double
  43. Dim stAng, enAng As Double
  44. ''计算半径
  45. radius = 100 'GetDistance(ptCen, ptSt)
  46. ''计算起点角度和终点角度
  47. stAng = ThisDrawing.Utility.AngleFromXAxis(ptCen, ptSt)
  48. enAng = ThisDrawing.Utility.AngleFromXAxis(ptCen, ptEn)
  49. Set objArc = ThisDrawing.ModelSpace.AddArc(ptCen, radius, stAng, enAng)
  50. objArc.Update
  51. Set AddArcCSEP = objArc
  52. End Function
  53. '判断三点的方向
  54. Function isClockWise(ptCen, ptSt, ptSc, ptEn) As Boolean
  55. a1 = ThisDrawing.Utility.AngleFromXAxis(ptCen, ptSt)
  56. a2 = ThisDrawing.Utility.AngleFromXAxis(ptCen, ptSc)
  57. a3 = ThisDrawing.Utility.AngleFromXAxis(ptCen, ptEn)
  58. isClockWise = (a1 < a2) Xor (a2 < a3) Xor (a1 < a3)
  59. End Function
  60. Sub ls()
  61.   Dim aa As AcadArc
  62.   Dim pp(0 To 2) As Double, ppp(0 To 2) As Double, pppp(0 To 2) As Double
  63.   pp(0) = 0: pp(1) = 10: pp(2) = 0
  64.   ppp(0) = 10: ppp(1) = 100: ppp(2) = 0
  65.   pppp(0) = -20: pppp(1) = -110: pppp(2) = 0
  66.   Set aa = AddArcCSEP(pp, ppp, pppp)
  67.   
  68. End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 09:00 , Processed in 0.160304 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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