|
发表于 2010-1-23 12:34:45
|
显示全部楼层
本帖最后由 sealive_leafage 于 2010-1-23 12:37 编辑 1 c6 n1 E: E: Y) f6 e1 c/ Q) `
8 }9 y; j; w! }9 a问题的难点在于在CAD中如何动态实时获得鼠标点的坐标,网上收集了些资料楼主可以体会一下;获得坐标值后则可以实现楼主要求的效果;详细参考:http://tiancao.net/blogview.asp?logID=2147 f8 h- Y) B* Y
CAD VBA实现橡皮筋直线、圆
5 h! o& P% S" [8 N+ n3 t
0 ]1 m3 ]$ A2 W5 j首先是计时控件,然后是实时捕捉鼠标的屏幕位置,然后是转换获得CAD当前试图鼠标所在的位置坐标,然后才是实时绘制直线或圆。
7 ?# r* x* L8 }/ O. R/ R5 D) L. FVBA中不可直接使用vb的timer控件,我们可以调用独立的xTimer控件。
0 S) A' ^! c3 n控件下载: ) u/ T9 x; y2 ?
http://www.tiancao.net/flashget.asp?dizhi=http://www.tiancao.net/attachments/month_0707/qnft_XTimers.rar4 l7 x4 R! J1 Z6 E5 U: d, I
- C5 o7 y! i6 V& m, m+ H
然后是使用API函数GetCursorPos获得当前鼠标的屏幕坐标。
, I& ]; \ I) ~% k0 `然后通过读取CAD系统变量viewSize荷Screensize分别得到当前试图的CAD高度和当前视口的屏幕尺寸(屏幕分辨率的像素).然后得到CAD实际尺寸和屏幕像素的比值。
2 v8 s5 F) }6 h- j4 p( ?$ o1 S精确度于鼠标的频率快慢有关系
1 M# @% l. g) [- n. v
0 A1 X1 r" t2 v K% b'获取CAD坐标系统和屏幕像素的比值% d( K' \2 x6 G# m/ S( `
Function ViewScreen() As Double" C @7 U3 c1 \( U
Dim ScreenSize As Variant
$ u1 k' O8 m/ ^- V- o/ j- } ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度: ^4 a/ [" f# [5 @5 W6 W$ l
Dim H As Variant3 @( J1 K! u6 @1 }
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度- w4 P! U7 K: T) c3 Q
ViewScreen = Abs(H / ScreenSize(1)): M8 m# T" C: q- k% S
End Function; h& J& N% i3 S. Y
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
( x+ R. ]( {' j& s- K. Z
$ t3 x4 [% o" ]) e- G1 w实时获得鼠标的屏幕坐标,然后通过屏幕尺寸和cad实际尺寸的比值,计算出鼠标当前的cad坐标。
* o6 S+ ?6 p" N' o2 n g然后在基点和鼠标坐标之间绘制直线或圆。
$ v- y' @+ L3 q Y' b% w3 q, r值得注意的是,屏幕坐标的是以屏幕的左上角为原点的。而cad的世界坐标是以右下角为原点的。
: t& ~6 i9 F( x) }9 S E
" ]& p& B: U5 F5 x$ f# S'得到鼠标屏幕坐标( ~" t( [5 }* t! J3 W# r
" u8 i7 A6 N* I+ i" e$ {
Private Type POINTAPI* u: I: c' I( ^8 m4 I ?
x As Long
' f7 |9 K& @7 x$ r Y As Long2 U: G+ g8 N5 p0 m* j, |
End Type( ~ Y% c. t4 y" V- y
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
# L2 e1 H/ n- SDim CAD_Point1 As Variant& {0 Q/ H/ r. |
Dim CAD_Point2 As Variant# t ^8 E3 G' n8 k$ B
Dim ScreenPoint1 As POINTAPI
8 s9 B' p. A9 u/ n& r8 ^Dim ScreenPoint2(1) As Long
0 a" Z5 s- X0 {4 g! vDim BiLi As Double
& {$ j' T6 V6 h+ Z9 q- Z'获取CAD坐标系统和屏幕像素的比值
g7 W) l7 y4 j# g# zFunction ViewScreen() As Double
7 {# k2 r6 p/ h. T' @ Dim ScreenSize As Variant
* T$ Q, W0 ?, S7 N0 \% k6 ] ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
j% d8 L9 U% E; r Dim H As Variant
+ Y: P* F4 d" H( F- v5 d$ y$ T H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度
! P' [8 ^0 a7 W4 F/ h) L3 f ViewScreen = Abs(H / ScreenSize(1))% ~; L W& [% r8 p
End Function J# q$ o' Q8 i" v: A
'通过CAD坐标计算屏幕坐标# g% s6 A- M, m& r! c& D6 d
Sub GetScreenPoint()
" I- \" o. b. |2 M( W8 g2 B BiLi = ViewScreen3 T4 D4 G1 \0 B' {; x; W
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标2 e6 i* \! a) v& N+ u3 W& a1 F4 f
ThisDrawing.ModelSpace.AddPoint CAD_Point1
. \2 J- v/ T) v, z6 ^3 Q GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
+ Y& r; s+ L. l. I- [6 x MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
2 D" Z8 E4 l6 [/ S( J) V MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
5 O ~% y# y/ j/ X+ j/ s '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了
5 F7 E' D: x4 h
( u& j' s5 [9 q5 b" L2 T8 w! `* H CAD_Point2 = ThisDrawing.Utility.GetPoint(CAD_Point1, "指定下一点,这个点将通过计算得到屏幕坐标:")
# `1 \2 h. E# f4 J) A
q# g2 h* b4 C8 e( }1 Z1 g ScreenPoint2(0) = Int(ScreenPoint1.x + (CAD_Point2(0) - CAD_Point1(0)) / BiLi)) k# G" P. I6 j5 H+ n: e9 U7 ~
ScreenPoint2(1) = Int(ScreenPoint1.Y - (CAD_Point2(1) - CAD_Point1(1)) / BiLi)
4 g" Y6 _' o& ]' J% C; I MsgBox "屏幕坐标:" & ScreenPoint2(0) & " / " & ScreenPoint2(1). K2 U+ c f4 T* v
'为了验证计算坐标,将CAD窗口在屏幕上移动到该点,看看效果吧。
O( B6 K p9 u ThisDrawing.Application.WindowState = acNorm
1 L$ ~2 k& x# r" y ] ThisDrawing.Application.WindowLeft = ScreenPoint2(0). K! ?9 K9 ^( A- r5 y
ThisDrawing.Application.WindowTop = ScreenPoint2(1)0 i# m5 ^' j& N! G$ K8 k
- g% H& t* y/ H$ G0 U$ R# H 1 B R; ~2 b/ L, i; Y
End Sub
+ ~# T" k, Y- e% K! y' 通过屏幕坐标计算CAD坐标
1 _- U' K! A: g0 q1 O5 g- z$ `( dSub GetCAD_Point()7 b4 S5 t" s: J' I9 O
BiLi = ViewScreen; X9 I4 b; N7 `
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
" ?; V6 S) I# o# m3 | ThisDrawing.ModelSpace.AddPoint CAD_Point1
- ~$ `0 e8 D8 {2 V! f0 j GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标0 q( X, u Z' U
MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
( x# ?. U% b. t/ f' a MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
) m% H7 K% J( Y8 g5 J' t' t3 C; a '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了
# u9 I$ Y x. W7 F
r7 G& h$ X& J9 T& L3 t Dim ScreenPoint3 As POINTAPI, ~0 u/ l) B8 Q0 G
GetCursorPos ScreenPoint3# K* \4 x9 h9 p6 E6 Y
+ T' Z& n! [; r0 A9 {' y% y i
Dim CAD_Point3(2) As Double
$ {+ m9 i7 k7 |1 ^ '计算cad坐标, Y& X6 ~' E- R
CAD_Point3(0) = CAD_Point1(0) + BiLi * (ScreenPoint3.x - ScreenPoint1.x)
1 s! U) v% T z( w7 a6 i CAD_Point3(1) = CAD_Point1(1) - BiLi * (ScreenPoint3.Y - ScreenPoint1.Y)3 U! ], l3 f# a
CAD_Point3(2) = 0 J: s9 K4 E+ u1 D3 {
MsgBox "屏幕坐标:" & CAD_Point3(0) & " / " & CAD_Point3(1)
. N/ R4 T3 j( ?' \ '为了验证计算坐标,将画一条直线,看看效果吧。
" ?& y2 j5 K2 \" V ThisDrawing.ModelSpace.AddLine CAD_Point1, CAD_Point38 t* _8 Q2 u, z. G7 _+ W( j( Q
End Sub |
-
评分
-
查看全部评分
|