|
|
发表于 2010-1-23 12:34:45
|
显示全部楼层
来自: 中国浙江宁波
本帖最后由 sealive_leafage 于 2010-1-23 12:37 编辑
) n& ]" V5 c8 `# w% p( Y- y7 y. J* ?4 b
问题的难点在于在CAD中如何动态实时获得鼠标点的坐标,网上收集了些资料楼主可以体会一下;获得坐标值后则可以实现楼主要求的效果;详细参考:http://tiancao.net/blogview.asp?logID=214- D8 b% v' Q% \0 r1 y- Y; A
CAD VBA实现橡皮筋直线、圆
# @0 d, p6 W8 Q# [' C- L: ^3 W! J* ~) n$ h5 u' F! U I
首先是计时控件,然后是实时捕捉鼠标的屏幕位置,然后是转换获得CAD当前试图鼠标所在的位置坐标,然后才是实时绘制直线或圆。9 N6 s7 ?2 \3 b6 B: Q }
VBA中不可直接使用vb的timer控件,我们可以调用独立的xTimer控件。( r8 W" n3 E3 \0 }( s0 \, R2 R
控件下载:
; Q) T. w K8 S j0 \8 khttp://www.tiancao.net/flashget.asp?dizhi=http://www.tiancao.net/attachments/month_0707/qnft_XTimers.rar N3 z3 n% k0 R! R5 n C
" S; t9 q* \/ r2 F然后是使用API函数GetCursorPos获得当前鼠标的屏幕坐标。
7 E9 V9 X# A: e3 V然后通过读取CAD系统变量viewSize荷Screensize分别得到当前试图的CAD高度和当前视口的屏幕尺寸(屏幕分辨率的像素).然后得到CAD实际尺寸和屏幕像素的比值。
$ {# \9 l! J, r; j3 G精确度于鼠标的频率快慢有关系8 r4 t$ ?; z, h$ z, s' A
1 z6 G' X1 m$ M" F6 R'获取CAD坐标系统和屏幕像素的比值
- A' Y) v- Q+ L- y8 G; r4 Q, VFunction ViewScreen() As Double9 Y6 w0 q4 j/ b+ }) o4 Q$ I8 h
Dim ScreenSize As Variant$ h+ m$ A- r; J( ]: O* z
ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度$ ? n0 R2 V: m& c6 x
Dim H As Variant
* k1 | B/ j" A& Z9 D H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度
& E3 ?8 k- U4 `" t ViewScreen = Abs(H / ScreenSize(1))
' H8 J: c# i f& [0 C% eEnd Function2 _2 O9 `0 }) v# H) h
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long" O( n' G& q' M& H' j" ?
6 k1 w& v. M/ c
实时获得鼠标的屏幕坐标,然后通过屏幕尺寸和cad实际尺寸的比值,计算出鼠标当前的cad坐标。2 n+ {4 i" D4 i% P( m: q
然后在基点和鼠标坐标之间绘制直线或圆。 E$ }: ?3 S+ T
值得注意的是,屏幕坐标的是以屏幕的左上角为原点的。而cad的世界坐标是以右下角为原点的。
; [8 S; _0 s. H( Y# q- f: u S3 n4 j; u2 q4 I
'得到鼠标屏幕坐标
4 i" P3 g; g/ e4 p1 | a9 O' ^/ j- v, M) b
Private Type POINTAPI& Z4 s) S _7 L8 O( V& ?4 T1 L
x As Long+ D4 v( I, D( S
Y As Long
+ r/ y, F! o6 FEnd Type* M( w! d: |' E% l1 x
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long2 j$ g0 I* V" \- s3 E+ ]" B. A) W
Dim CAD_Point1 As Variant+ I" n" _/ W' S: J& m9 l, i
Dim CAD_Point2 As Variant# V, d! p, T! @
Dim ScreenPoint1 As POINTAPI
" ]0 |, \8 Y1 ~2 K/ m& |Dim ScreenPoint2(1) As Long
% ~5 t) i: d' U$ b7 iDim BiLi As Double* V, p B& I+ |5 F% ^
'获取CAD坐标系统和屏幕像素的比值
/ X/ y1 Q( h( ^% ]( eFunction ViewScreen() As Double
3 _$ T- L& g! u. D2 {0 D- Z3 K Dim ScreenSize As Variant% C% ? K5 x: z1 ?9 E& v
ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度5 U6 t6 e w7 }) \, I; i
Dim H As Variant/ K, u5 z/ |9 q9 T
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度) ]# c4 K1 Y# i4 J% W
ViewScreen = Abs(H / ScreenSize(1))1 Q# {3 ~. ]" l
End Function
5 R! m- |2 S( l* P- a* P2 V. v'通过CAD坐标计算屏幕坐标
7 `8 T' V5 F G- S" t- X, ^1 c: OSub GetScreenPoint()7 ?0 C9 S' ]; G Z* G8 A0 k
BiLi = ViewScreen% l; N$ H, N- ]
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
5 E$ A( `. R9 P1 U0 }" W ThisDrawing.ModelSpace.AddPoint CAD_Point1
% T3 g ^3 D/ v D) ^ GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
3 ~- B2 b+ M" V' P/ ]. {2 C MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
! m4 U) `1 I+ ]: H% p7 J7 Y1 i MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)3 ~ i& z4 A' b& F. i6 U
'以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了
+ k6 d; y$ P' Y. I8 q # L6 l0 d: [$ q- v! r. r+ f
CAD_Point2 = ThisDrawing.Utility.GetPoint(CAD_Point1, "指定下一点,这个点将通过计算得到屏幕坐标:"); P5 T& K2 |# T( U% L4 r
! y5 b7 @; |+ l! M( H; u8 b4 N
ScreenPoint2(0) = Int(ScreenPoint1.x + (CAD_Point2(0) - CAD_Point1(0)) / BiLi)( M! s" H* {0 |& n, a0 v7 V
ScreenPoint2(1) = Int(ScreenPoint1.Y - (CAD_Point2(1) - CAD_Point1(1)) / BiLi); h, E+ p( Q( T6 n
MsgBox "屏幕坐标:" & ScreenPoint2(0) & " / " & ScreenPoint2(1)
/ P( l$ l3 |/ x9 Q '为了验证计算坐标,将CAD窗口在屏幕上移动到该点,看看效果吧。
. f! i/ l, g, f5 t+ k& ] ThisDrawing.Application.WindowState = acNorm
/ X3 F9 C8 k/ k* ^ O ThisDrawing.Application.WindowLeft = ScreenPoint2(0)
$ ~' W0 o0 P) h: L ThisDrawing.Application.WindowTop = ScreenPoint2(1)
" \# H) O* v, \, W! D% a % \2 h! s9 }# @+ n: Z+ C1 m* m
* i5 @# l4 n: B8 UEnd Sub
+ q! L) `* F0 c3 K/ v) d& j' 通过屏幕坐标计算CAD坐标
& _7 L. @" o8 }& w6 c: cSub GetCAD_Point()
3 P. S9 ^. R) z W! c% } BiLi = ViewScreen$ U3 K" t( A% e! T3 K% j
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标' s& O" z7 j8 H1 I9 c i0 q
ThisDrawing.ModelSpace.AddPoint CAD_Point1; U: y3 t( k( i% Y& Q
GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标" T; x! w" v& b! j7 A0 o, ]7 g* }
MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
1 G& h0 v+ T/ {" N; ` MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)7 ?( y9 o; u# T2 R" P
'以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了; J1 a8 a* c4 I* s& z7 |
1 k9 ^ N8 F( U7 P$ f
Dim ScreenPoint3 As POINTAPI
9 m8 S+ k8 p7 C" O, \' G GetCursorPos ScreenPoint30 M" N5 ^& l& t/ u# z) g# T
6 P- |5 N _: X4 d7 r
Dim CAD_Point3(2) As Double( ~/ V, u5 M& ], L
'计算cad坐标
) \; b4 G, z7 y CAD_Point3(0) = CAD_Point1(0) + BiLi * (ScreenPoint3.x - ScreenPoint1.x); K! e3 _7 _! W& I5 H k2 j4 T* W% h8 P2 f
CAD_Point3(1) = CAD_Point1(1) - BiLi * (ScreenPoint3.Y - ScreenPoint1.Y)
/ l$ U0 j/ K8 U& I, e( \ CAD_Point3(2) = 0& O3 J' z( |1 D* a( W
MsgBox "屏幕坐标:" & CAD_Point3(0) & " / " & CAD_Point3(1)/ ^' D; |' V$ o
'为了验证计算坐标,将画一条直线,看看效果吧。
% ~4 i/ a3 U# j2 n- t' ?- T ThisDrawing.ModelSpace.AddLine CAD_Point1, CAD_Point3& N& ^$ \0 I9 w" B6 o
End Sub |
-
评分
-
查看全部评分
|