|
|
发表于 2010-1-23 12:34:45
|
显示全部楼层
来自: 中国浙江宁波
本帖最后由 sealive_leafage 于 2010-1-23 12:37 编辑
& M4 X! G0 q6 y' q) ?1 n( t+ s3 j% ]3 B" h1 X, b9 f3 D: N
问题的难点在于在CAD中如何动态实时获得鼠标点的坐标,网上收集了些资料楼主可以体会一下;获得坐标值后则可以实现楼主要求的效果;详细参考:http://tiancao.net/blogview.asp?logID=2146 R' v" Y- s& r. [0 G" o) A% T0 h
CAD VBA实现橡皮筋直线、圆
$ E6 k9 W, {& t, N) T
, V5 r6 M- w( y% y1 e* ]4 ^! X7 S: G首先是计时控件,然后是实时捕捉鼠标的屏幕位置,然后是转换获得CAD当前试图鼠标所在的位置坐标,然后才是实时绘制直线或圆。
9 l+ M. |6 ^2 p, s+ p" FVBA中不可直接使用vb的timer控件,我们可以调用独立的xTimer控件。
) j# b3 E. {8 C Y0 }1 W7 l控件下载: ( j* U0 x5 Z8 I. ^
http://www.tiancao.net/flashget.asp?dizhi=http://www.tiancao.net/attachments/month_0707/qnft_XTimers.rar
4 e8 X, _/ o7 e( r( n* ?2 G- m; `% O& J u9 S+ I0 S4 @
然后是使用API函数GetCursorPos获得当前鼠标的屏幕坐标。
5 `" T7 r/ h5 Q; C* C3 R4 V( \: y" {然后通过读取CAD系统变量viewSize荷Screensize分别得到当前试图的CAD高度和当前视口的屏幕尺寸(屏幕分辨率的像素).然后得到CAD实际尺寸和屏幕像素的比值。) p: L& E! w. X2 `7 j( ~ z
精确度于鼠标的频率快慢有关系
: C! ~0 W8 z1 _1 G
% M+ w- k+ E( d'获取CAD坐标系统和屏幕像素的比值
( D9 B6 U' H3 M- x- A1 F, f' W# JFunction ViewScreen() As Double
5 l F! m+ `, L# K& D8 l& N1 W5 ]" | Dim ScreenSize As Variant
9 J9 Q- T; T3 M: I ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
, ~' [1 G! l* d! a; w Dim H As Variant% ^: |+ V- g7 t6 w- c6 k& _
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度; o6 ~3 s- h# A# G, u1 p
ViewScreen = Abs(H / ScreenSize(1))
r" C/ T0 T# N- I; c5 lEnd Function* G1 f6 G* w$ H; p& W! N9 q/ S
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long1 w: r. h" G4 t5 e2 b
; {3 i% q# \5 g# |
实时获得鼠标的屏幕坐标,然后通过屏幕尺寸和cad实际尺寸的比值,计算出鼠标当前的cad坐标。+ Y6 p" K V' A
然后在基点和鼠标坐标之间绘制直线或圆。
% Z$ h ?* c. Y" o' j值得注意的是,屏幕坐标的是以屏幕的左上角为原点的。而cad的世界坐标是以右下角为原点的。6 ]5 {% r' O( n j
p: F1 m& u! z'得到鼠标屏幕坐标
+ A! X* {; s- \/ t. X9 W! `/ Y% ^; F7 w
Private Type POINTAPI
* \0 Z& }; C; z( @3 @9 @ x As Long# H8 _$ I8 \ p7 ~" [9 f2 D
Y As Long- }& ] Q, P! E7 ~ s) e
End Type* k1 |5 A6 o p: n5 C
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long1 Y2 A) J9 B. p" a; V* R( [/ C
Dim CAD_Point1 As Variant
- b1 K& X0 v. B! _ o- PDim CAD_Point2 As Variant
' u8 m& D6 T8 XDim ScreenPoint1 As POINTAPI
( s/ h& X& N+ B: k) }9 U- l- YDim ScreenPoint2(1) As Long
2 |/ w" G' W6 s# b/ |Dim BiLi As Double+ z% D! R) Z+ X) ]7 n F8 l; `$ ^
'获取CAD坐标系统和屏幕像素的比值* G1 g- M) @9 S4 I `
Function ViewScreen() As Double: g( ~" p/ f5 E/ d$ ~2 z
Dim ScreenSize As Variant* r8 g! O5 ?5 ?: k5 M
ScreenSize = ThisDrawing.GetVariable("screensize") '当前视口的屏幕宽度和高度
& D6 H9 P7 m8 i% ]+ T Dim H As Variant% P, @' u$ v1 c [4 b5 S
H = ThisDrawing.GetVariable("viewsize") '当前视图图形的实际高度1 a4 ]/ K( I9 w6 f9 T
ViewScreen = Abs(H / ScreenSize(1))
8 D% q. q. s, F+ d- s* QEnd Function
+ A! a* M9 Z1 d/ ^$ H: w'通过CAD坐标计算屏幕坐标
; o9 \/ n7 z5 b9 c% u4 O2 RSub GetScreenPoint()$ I R7 }0 c' \( P; g- E
BiLi = ViewScreen& z2 Z5 Y; T$ h, l. d
CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标0 q4 u3 E' e2 A7 C" H, r0 Z
ThisDrawing.ModelSpace.AddPoint CAD_Point1. P- c) Q& d" A7 [# e" \" V
GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
5 P) C2 ]# j1 ~9 z$ i MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
D+ H7 K. q7 r( s; c4 Q, i MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
$ e4 M, Q* _- X! c '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了6 G u7 {8 C, R
- [4 g2 ]2 V. O" C CAD_Point2 = ThisDrawing.Utility.GetPoint(CAD_Point1, "指定下一点,这个点将通过计算得到屏幕坐标:")
* h1 X& _$ O3 J6 {
( R# I6 [3 ^' K+ v" b ScreenPoint2(0) = Int(ScreenPoint1.x + (CAD_Point2(0) - CAD_Point1(0)) / BiLi)# O- D4 q4 `1 n8 H9 J
ScreenPoint2(1) = Int(ScreenPoint1.Y - (CAD_Point2(1) - CAD_Point1(1)) / BiLi)# W; Y$ Y7 K9 W- `' z7 |
MsgBox "屏幕坐标:" & ScreenPoint2(0) & " / " & ScreenPoint2(1)
! Y: a4 I; X0 a/ m- G: C '为了验证计算坐标,将CAD窗口在屏幕上移动到该点,看看效果吧。5 L/ G" N' @/ d8 _, i, o% {
ThisDrawing.Application.WindowState = acNorm
* d7 M7 R9 H5 a; ^ ThisDrawing.Application.WindowLeft = ScreenPoint2(0)
0 I% h! P- e# _& ?" ` ThisDrawing.Application.WindowTop = ScreenPoint2(1)- U5 Q2 X) v5 A- M0 D
0 E n) C+ P$ R ) A% O" w/ c: I4 v7 G* h3 u: O4 H
End Sub
' d% f7 v. d' e3 n' 通过屏幕坐标计算CAD坐标
& W- g1 u3 s) C; @: u1 QSub GetCAD_Point()3 w* ]/ o" m J, T9 p- [& k
BiLi = ViewScreen
$ |4 j7 \/ k& i CAD_Point1 = ThisDrawing.Utility.GetPoint(, "先指定一个点:") '获得鼠标所在位置的CAD坐标
' O% ~$ C9 W/ y' R' T& F. T9 l ThisDrawing.ModelSpace.AddPoint CAD_Point1
4 f0 k% M, m) F ` GetCursorPos ScreenPoint1 '通过api直接获得鼠标所在位置的屏幕坐标
8 o; R* s4 h6 y1 V, _+ D MsgBox "屏幕坐标:" & ScreenPoint1.x & " / " & ScreenPoint1.Y
+ Z* m6 m( C+ w5 _ MsgBox "对应的CAD坐标:" & CAD_Point1(0) & " / " & CAD_Point1(1)
2 C9 W4 G4 G- H. H4 v4 `( A0 k '以上获得了CAD坐标和屏幕坐标的对应关系,下面就可以计算了
- x8 u9 t( X0 r N! P
, F: B3 X5 D/ R( w8 N/ D Dim ScreenPoint3 As POINTAPI
$ n% D6 w) ^: n" i3 k GetCursorPos ScreenPoint3% ~& [/ _/ C$ j( L
" X q; Q; b* n, T! p, t$ d) J7 n4 k Dim CAD_Point3(2) As Double2 t. \+ y' q1 k" Q0 F
'计算cad坐标8 \1 {( ], E) E/ y5 I# G6 w Q
CAD_Point3(0) = CAD_Point1(0) + BiLi * (ScreenPoint3.x - ScreenPoint1.x)
0 [0 A6 O: y+ \7 D) }5 b CAD_Point3(1) = CAD_Point1(1) - BiLi * (ScreenPoint3.Y - ScreenPoint1.Y)+ _1 f0 S, O; K \
CAD_Point3(2) = 0 \; X5 ^( Q* G5 G# Q1 h
MsgBox "屏幕坐标:" & CAD_Point3(0) & " / " & CAD_Point3(1)
; k7 K$ @ g" T) e '为了验证计算坐标,将画一条直线,看看效果吧。3 y. W) v; D8 |1 ?
ThisDrawing.ModelSpace.AddLine CAD_Point1, CAD_Point32 ~9 r3 Q/ |- i$ Z, I" u
End Sub |
-
评分
-
查看全部评分
|