明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: 水镜影

用VBA编程,画一条曲线,如用三个点,怎么画?

  [复制链接]
发表于 2004-5-16 16:45:00 | 显示全部楼层

三点法(起点、第二点和终点)创建圆弧,首先计算出圆弧的圆心和半径,然后根据圆心、起点和终点,创建圆弧对象,其实现代码为:

Public Function AddArc3Pt(ByVal ptSt As Variant, ByVal ptSc As Variant, ByVal ptEn As Variant) As AcadArc

Dim objArc As AcadArc

Dim ptCen As Variant

Dim radius As Double

ptCen = GetCenOf3Pt(ptSt, ptSc, ptEn, radius)

Set objArc = AddArcCSEP(ptCen, ptSt, ptEn)

objArc.color = acGreen

objArc.Update

Set AddArc3Pt = objArc

End Function

GetCenOf3Pt是自定义的函数,能够根据三点计算出圆心和半径,其实现代码为:

Public Function GetCenOf3Pt(pt1 As Variant, pt2 As Variant, pt3 As Variant, _

ByRef radius As Double) As Variant

Dim xysm, xyse, xy As Double

Dim ptCen(2) As Double

xy = pt1(0) ^ 2 + pt1(1) ^ 2

xyse = xy - pt3(0) ^ 2 - pt3(1) ^ 2

xysm = xy - pt2(0) ^ 2 - pt2(1) ^ 2

xy = (pt1(0) - pt2(0)) * (pt1(1) - pt3(1)) - (pt1(0) - pt3(0)) * (pt1(1) - pt2(1))

'判断参数有效性

If Abs(xy) < 0.000001 Then

MsgBox "所输入的参数无法创建圆形!"

Exit Function

End If

'获得圆心和半径

ptCen(0) = (xysm * (pt1(1) - pt3(1)) - xyse * (pt1(1) - pt2(1))) / (2 * xy)

ptCen(1) = (xyse * (pt1(0) - pt2(0)) - xysm * (pt1(0) - pt3(0))) / (2 * xy)

ptCen(2) = 0

radius = Sqr((pt1(0) - ptCen(0)) * (pt1(0) - ptCen(0)) + (pt1(1) - ptCen(1)) * (pt1(1) - ptCen(1)))

If radius < 0.000001 Then

MsgBox "半径过小!"

Exit Function

End If

'函数返回圆心的位置,而半径则在参数中通过引用方式返回

GetCenOf3Pt = ptCen

End Function

需要注意的是,radius参数是按地址传递的,因此也是函数的返回值。

摘自《AutoCAD VBA精彩实例教程》第2章相关内容。

发表于 2004-5-17 09:46:00 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-28 05:33 , Processed in 0.136176 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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