|
发表于 2010-1-23 12:34:45
|
显示全部楼层
来自: 中国浙江宁波
本帖最后由 sealive_leafage 于 2010-1-23 12:37 编辑 $ J# C, x" O* Q
3 d. y' K0 v( a6 e, T4 `* H \
问题的难点在于在CAD中如何动态实时获得鼠标点的坐标,网上收集了些资料楼主可以体会一下;获得坐标值后则可以实现楼主要求的效果;详细参考:http://tiancao.net/blogview.asp?logID=214, D0 y! |) {) S( x" p3 h/ `5 w* l. b; Z
CAD VBA实现橡皮筋直线、圆4 H8 ^/ l2 `. s3 t, n; Z
6 B( R0 T/ o# ?, ~" j
首先是计时控件,然后是实时捕捉鼠标的屏幕位置,然后是转换获得CAD当前试图鼠标所在的位置坐标,然后才是实时绘制直线或圆。
4 i g9 V5 n' P0 m' A$ x7 }VBA中不可直接使用vb的timer控件,我们可以调用独立的xTimer控件。
/ A& h" p6 S3 K* H; F控件下载: f' Y P& Y8 T/ B" ]% \
http://www.tiancao.net/flashget.asp?dizhi=http://www.tiancao.net/attachments/month_0707/qnft_XTimers.rar q2 |' \" ~. R2 Q* W
, @, ?9 j3 ?$ m7 A# d然后是使用API函数GetCursorPos获得当前鼠标的屏幕坐标。1 c. J9 _& K0 j/ h: @+ V
然后通过读取CAD系统变量viewSize荷Screensize分别得到当前试图的CAD高度和当前视口的屏幕尺寸(屏幕分辨率的像素).然后得到CAD实际尺寸和屏幕像素的比值。
3 m8 D4 I5 p( F, N K3 K精确度于鼠标的频率快慢有关系
( _* }6 [' e. Z. e; u7 ~! Z1 {4 M. m0 l) h, Z' n. I2 E
'获取CAD坐标系统和屏幕像素的比值 F/ V% O4 }3 |% O6 ]: Z
Function ViewScreen() As Double
0 N7 \; @+ O8 \ z- ^! e Dim ScreenSize As Variant
+ e, {) U; k% x& `! f; j ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
) L6 J+ O3 h5 y6 N8 } Dim H As Variant( z' C/ t7 m4 S
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度
5 M( V0 Y* V3 J- C/ H% U0 o ViewScreen = Abs(H / ScreenSize(1))
4 N: Y' a/ w2 \; o6 D, X" }+ B5 MEnd Function
\9 c2 `% `( v- u& b7 ^Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long' K" {3 j6 w8 c) |! j% @
; e, | l$ u/ P3 b* r
实时获得鼠标的屏幕坐标,然后通过屏幕尺寸和cad实际尺寸的比值,计算出鼠标当前的cad坐标。6 U- u+ X4 G) U; \ y
然后在基点和鼠标坐标之间绘制直线或圆。
6 H8 w6 ?( J5 ]1 x% l" u% `值得注意的是,屏幕坐标的是以屏幕的左上角为原点的。而cad的世界坐标是以右下角为原点的。) B: |% \2 h! P
/ ^0 S+ g0 e( k9 R
'得到鼠标屏幕坐标 p7 T1 d/ K% W4 O
a; f" b: \% _' cPrivate Type POINTAPI% o: }& ~, F3 J0 p# Y
x As Long. q0 X2 X! o! E& ?' a5 r. y
Y As Long
& J0 a* G$ M7 V; F! }, F+ w% |! ZEnd Type8 ?9 o( Q0 \( [3 f4 I* N, m
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
/ r: N# r% i* {# p8 K6 nDim CAD_Point1 As Variant" _$ U* M7 H# V% s d
Dim CAD_Point2 As Variant P: Q& A# P& e T& E. P
Dim ScreenPoint1 As POINTAPI$ Y/ o; J: S# f2 o) y) Q
Dim ScreenPoint2(1) As Long8 f1 T; b9 E# v# z8 u" z
Dim BiLi As Double7 @6 z( C' b' Z8 }& X
'获取CAD坐标系统和屏幕像素的比值
# U3 H+ M3 w: z! d; U( W3 GFunction ViewScreen() As Double l4 Y% @7 y8 Y
Dim ScreenSize As Variant
/ j3 {3 M9 R% s ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
* |, D+ r- }- m Dim H As Variant; \: i; \5 x* o* P7 m1 I" K
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度
0 L* i8 j: M1 m8 F: E- N ViewScreen = Abs(H / ScreenSize(1))+ z8 ~5 [$ l, F1 H9 e' L
End Function
0 a: g5 J7 H# L3 b) b/ s% w'通过CAD坐标计算屏幕坐标
* |, N* s! z/ O8 z N: zSub GetScreenPoint()
" n6 F% U, {0 z BiLi = ViewScreen
. R- F7 k& r( c" t CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
7 j: ]$ ?- z+ G$ w/ G# P ThisDrawing.ModelSpace.AddPoint CAD_Point1+ G8 S+ y5 C3 M7 O) J2 r2 r9 ?3 d
GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
3 D0 `6 n) z0 H& x5 s MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
3 y7 H+ T- \# X2 M. X2 w% K! i' v MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
# ~! x# V* G! `" a7 [# v0 n X' B '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了4 g/ }+ ] V* o4 `/ W
7 M. G# ?# M) | CAD_Point2 = ThisDrawing.Utility.GetPoint(CAD_Point1, "指定下一点,这个点将通过计算得到屏幕坐标:")
& D3 }# Q/ K- O3 }" n8 D " k( q) ^& ~: h% P9 a( P+ b& u
ScreenPoint2(0) = Int(ScreenPoint1.x + (CAD_Point2(0) - CAD_Point1(0)) / BiLi)0 U5 M: V0 A- b" k4 w
ScreenPoint2(1) = Int(ScreenPoint1.Y - (CAD_Point2(1) - CAD_Point1(1)) / BiLi)
# G: L p* C8 _0 R+ Z8 s MsgBox "屏幕坐标:" & ScreenPoint2(0) & " / " & ScreenPoint2(1)# p) E1 J/ K8 F: K$ V6 f
'为了验证计算坐标,将CAD窗口在屏幕上移动到该点,看看效果吧。) w2 B' h9 F j5 E' f
ThisDrawing.Application.WindowState = acNorm
& r8 O0 d5 O' h8 u7 y2 g/ N ThisDrawing.Application.WindowLeft = ScreenPoint2(0)) G/ s# T6 \) x4 G
ThisDrawing.Application.WindowTop = ScreenPoint2(1)
7 O/ ]( I1 W- o6 w" _1 q K' x; N F3 [: t W7 h' E- A
, d( t: l+ G3 U7 O5 f% q6 P
End Sub
+ I! ~0 g: W" {* [3 e' 通过屏幕坐标计算CAD坐标5 O% |; `' e+ J5 A% z' G
Sub GetCAD_Point() Z& m# i7 s- i2 V. M
BiLi = ViewScreen; H* Y; `% b. c4 V, I/ B8 B
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
- A# O9 L, E' g1 ` ThisDrawing.ModelSpace.AddPoint CAD_Point1
$ D2 @2 G1 I, N9 _3 G& H GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标* y* B$ ?* S3 R4 K W
MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y S: x# M6 J2 y/ a( A% |
MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)) B* x. C$ x8 c. X
'以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了% @- V; P2 a; N5 Z
! T- W7 B* G' U
Dim ScreenPoint3 As POINTAPI1 w8 d: h8 @% T" u2 F0 j
GetCursorPos ScreenPoint3. u9 Q& L$ }' I& \# \
/ u# {9 b) ^& [; [$ C2 s Dim CAD_Point3(2) As Double& m1 h; K/ X& d2 Q4 Y. p9 }+ o* r% t
'计算cad坐标
' e5 v2 }5 ~/ R CAD_Point3(0) = CAD_Point1(0) + BiLi * (ScreenPoint3.x - ScreenPoint1.x)
7 `* E' l- j1 p% y- U CAD_Point3(1) = CAD_Point1(1) - BiLi * (ScreenPoint3.Y - ScreenPoint1.Y)7 s6 y0 J/ V0 I& D6 p3 a
CAD_Point3(2) = 07 H4 a" a" g4 ^ c1 @
MsgBox "屏幕坐标:" & CAD_Point3(0) & " / " & CAD_Point3(1). ?1 c, k2 t/ s1 ^% g- e
'为了验证计算坐标,将画一条直线,看看效果吧。
& ^3 e' ^' s3 \& J6 P7 ^* G6 y ThisDrawing.ModelSpace.AddLine CAD_Point1, CAD_Point3( x( [3 ^: W/ f8 f1 X7 y
End Sub |
-
评分
-
查看全部评分
|