|
|
发表于 2010-1-23 12:34:45
|
显示全部楼层
来自: 中国浙江宁波
本帖最后由 sealive_leafage 于 2010-1-23 12:37 编辑
5 H0 A' @. t, ?4 y! [' i) ]/ B" [; e+ H$ i
问题的难点在于在CAD中如何动态实时获得鼠标点的坐标,网上收集了些资料楼主可以体会一下;获得坐标值后则可以实现楼主要求的效果;详细参考:http://tiancao.net/blogview.asp?logID=214
0 h1 L% G! M9 HCAD VBA实现橡皮筋直线、圆' Q4 ]- P2 ]* n: j
; s, J3 C3 \; \0 G2 M2 q9 m
首先是计时控件,然后是实时捕捉鼠标的屏幕位置,然后是转换获得CAD当前试图鼠标所在的位置坐标,然后才是实时绘制直线或圆。. b5 I% }" u/ p" r, @
VBA中不可直接使用vb的timer控件,我们可以调用独立的xTimer控件。8 l4 n9 c( \' t% p% B" J
控件下载: : z5 G4 w: F7 z& i8 w7 m( {" m
http://www.tiancao.net/flashget.asp?dizhi=http://www.tiancao.net/attachments/month_0707/qnft_XTimers.rar4 p* ]1 B9 x3 u; U! ?
% j) j" A: F9 T$ P6 A% j2 w% |然后是使用API函数GetCursorPos获得当前鼠标的屏幕坐标。
4 {0 h: @+ t+ N e然后通过读取CAD系统变量viewSize荷Screensize分别得到当前试图的CAD高度和当前视口的屏幕尺寸(屏幕分辨率的像素).然后得到CAD实际尺寸和屏幕像素的比值。
( ^' Z. h$ Z X) H& }6 _1 Z精确度于鼠标的频率快慢有关系
4 \+ _& o: p% ?4 ]0 v0 q8 [! U( }+ H! C) f1 P
'获取CAD坐标系统和屏幕像素的比值
% z6 \' p" l% Q8 nFunction ViewScreen() As Double
+ W( S2 M% O9 Q' ? Dim ScreenSize As Variant3 ^6 }+ u! i1 H d. B0 B/ W+ l5 z
ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度* p5 y+ { p1 ^! z
Dim H As Variant' I! H5 x2 B( f% h* \
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度6 Q8 D/ o8 W# U
ViewScreen = Abs(H / ScreenSize(1))3 [# T! F) ]2 E+ g+ _2 Z3 d, d0 ?
End Function
: S4 X* s2 t; `' \Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
/ S7 q/ y9 f$ H# [* y
1 L( c c3 a/ `& K实时获得鼠标的屏幕坐标,然后通过屏幕尺寸和cad实际尺寸的比值,计算出鼠标当前的cad坐标。
. a7 y% \+ [- F/ J9 F( @3 M然后在基点和鼠标坐标之间绘制直线或圆。
' v) H) \' ^/ i% ^ v5 O值得注意的是,屏幕坐标的是以屏幕的左上角为原点的。而cad的世界坐标是以右下角为原点的。& [! u9 O9 u7 }. W' W
! X8 C0 o' n$ p* Z2 u'得到鼠标屏幕坐标( T' m0 m$ m' b6 m
0 j/ E8 ?5 q1 vPrivate Type POINTAPI3 ^9 g. I; U/ g; K) J, t1 F+ v6 z8 W
x As Long" m$ @, p' j5 v- P8 ]
Y As Long( ~2 Y" F4 y \0 f! R2 A
End Type
2 p) \3 I) ~: U3 e7 i8 N2 f# cPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
" J# h; k5 P; c) P- IDim CAD_Point1 As Variant
$ F2 x" g5 a9 R# Q5 z% n& {5 LDim CAD_Point2 As Variant& q* q$ [1 F6 k
Dim ScreenPoint1 As POINTAPI
7 K$ |6 g% {; r9 o: \# ]1 O+ gDim ScreenPoint2(1) As Long% ]: R$ v4 ~- R' N+ j
Dim BiLi As Double. o# r$ V n3 R/ [' L
'获取CAD坐标系统和屏幕像素的比值
1 c# _+ o6 Y0 r9 _Function ViewScreen() As Double+ r7 g0 z! h) t/ ?% N8 |
Dim ScreenSize As Variant
5 h4 s, P$ f6 v" X& ? ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
0 g; c8 y& X! o4 x V Dim H As Variant/ {2 M# r8 \$ X1 f6 J; x- M
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度
# s7 Y! }* P1 j& M$ _ ViewScreen = Abs(H / ScreenSize(1))4 P& f, w) L$ g- ~) \$ [2 j
End Function
0 Z& y3 Y2 Q6 ]+ v3 Q* s0 f'通过CAD坐标计算屏幕坐标/ h3 ?% `0 B- Z) O
Sub GetScreenPoint(); r$ L2 ], i8 n( J( b! m3 ^9 k1 g
BiLi = ViewScreen, u! r: [8 b8 V- S
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标1 p$ t4 y5 W0 h+ d) {8 I" `. G5 V# a
ThisDrawing.ModelSpace.AddPoint CAD_Point1
2 M6 V3 ?. H1 e- ~0 D GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
% L) V! @: _ P! M MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
: Z+ X! O* G. K% d; p+ q: K MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)- C2 K+ N4 j( c
'以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了/ [/ f8 [9 B2 s
7 _7 c9 U0 X1 M0 g CAD_Point2 = ThisDrawing.Utility.GetPoint(CAD_Point1, "指定下一点,这个点将通过计算得到屏幕坐标:")
D9 ~! t5 ]1 R0 {2 c 2 m5 {' p7 P& e3 G0 @) i; }
ScreenPoint2(0) = Int(ScreenPoint1.x + (CAD_Point2(0) - CAD_Point1(0)) / BiLi)5 Z" f, g r5 C; q0 ^; x# A
ScreenPoint2(1) = Int(ScreenPoint1.Y - (CAD_Point2(1) - CAD_Point1(1)) / BiLi)& ]5 }& R9 a2 Z7 a, G5 O
MsgBox "屏幕坐标:" & ScreenPoint2(0) & " / " & ScreenPoint2(1)! m B, w2 \9 E7 C
'为了验证计算坐标,将CAD窗口在屏幕上移动到该点,看看效果吧。
+ \7 Y. G( \$ n; Z ThisDrawing.Application.WindowState = acNorm
8 P1 u" s! c4 D# X* d) ~ ThisDrawing.Application.WindowLeft = ScreenPoint2(0)! w: k5 k6 P1 \
ThisDrawing.Application.WindowTop = ScreenPoint2(1), m! d* ^/ E& d% L, F
" }( w+ g6 ]; ~
; W* R/ n3 b8 z* R' Y) r
End Sub
& K9 \! s3 ~$ n# H R$ Y# Q9 o1 P' 通过屏幕坐标计算CAD坐标
! [3 ]. I$ k5 s; YSub GetCAD_Point()
8 V1 M* P6 n4 m0 P; v2 G. H BiLi = ViewScreen
8 m- [. m1 l# J1 d5 x9 r CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
) ?& d+ r/ d4 s4 z" b ThisDrawing.ModelSpace.AddPoint CAD_Point1, @+ [' ~: j5 h) S6 B3 _+ n
GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
) w' E& B) `* _! A& P/ `9 k& T MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
d3 C7 j8 X8 ^5 ` MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
$ _# v) |+ u5 ?. X8 K '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了9 w+ E. n' Y& S& m2 v6 p) x
3 T T x- O1 G7 b- B ` Dim ScreenPoint3 As POINTAPI
5 f# Q* o6 y6 M/ M& L. z" c, B& ] GetCursorPos ScreenPoint30 l' v8 F: b+ b C) ]) W
3 j1 J: f7 ?5 s: d3 |1 s Dim CAD_Point3(2) As Double
* @! d# U/ K& h4 y: A+ g '计算cad坐标, m! U+ S- A3 }4 S
CAD_Point3(0) = CAD_Point1(0) + BiLi * (ScreenPoint3.x - ScreenPoint1.x). R7 {6 i" ~1 r+ R! t& S% q
CAD_Point3(1) = CAD_Point1(1) - BiLi * (ScreenPoint3.Y - ScreenPoint1.Y)9 r" G5 @- d) R4 G
CAD_Point3(2) = 0" g* ~0 M, @' u O% h
MsgBox "屏幕坐标:" & CAD_Point3(0) & " / " & CAD_Point3(1)
. A( Q3 U. D# ?9 U8 R4 C0 S, j( M4 ~ '为了验证计算坐标,将画一条直线,看看效果吧。$ A. r7 F" ]; b9 r
ThisDrawing.ModelSpace.AddLine CAD_Point1, CAD_Point3
. Y% M; D9 k1 h d. S: P3 }+ T: Q) z! JEnd Sub |
-
评分
-
查看全部评分
|