|
发表于 2011-3-25 12:23:08
|
显示全部楼层
我又想了一下,只用加总平均的办法计算中心点未免有点太单一了.如果增加一个选项,由用户在屏幕上指定中心点会更好些- Dim SS As AcadSelectionSet, Ft(0) As Integer, Fd(0) As Variant, V As Variant, P(2) As Double, A() As Double, I As Integer, J As Integer, D As Double
" v. v! {* e% t: L - On Error Resume Next( G9 w b; `, |8 }
- With ThisDrawing
1 A: I6 @9 r- }& A - '创建选择集,用于选择所有文字对象
) F; y9 u9 N; X- U: x: ~4 H6 z% d7 \ - Set SS = .SelectionSets.Add("SS")
6 L, I p4 X4 @2 h - '定义过滤器为选择单行文字对象! K% V0 a4 Y5 V; |1 l$ w
- Ft(0) = 0
o( r2 L/ B6 i6 z! [ - Fd(0) = "TEXT") j W6 T3 s2 P6 m- X, S
- '选择所有单行文字对象( E- v. z3 a4 R: ?2 S' q2 b. h
- SS.Select acSelectionSetAll, , , Ft, Fd1 ]& ?$ v8 z# R2 D4 F, w
- '当存在单行文字对象时排序和替换6 w% E$ t# E6 ?0 A, \
- If SS.Count > 0 Then
/ ~8 c" \/ y _- A& X - '由用户在屏幕上指定中心点
7 o3 q& q L2 P0 x - V = .Utility.GetPoint(, "指定中心点<默认>:")+ W2 j1 s/ t% M' l9 n
- '如果用户没有取消则排序和替换3 v2 e* e( e: t) s; K' ^
- If Err <> -2147352567 Then' J; |1 F2 Y' K, u
- '用户指定了点
9 Z- [$ G# h- I/ i1 } F$ d - If Err = 0 Then9 _5 i# ~/ n0 ^4 |2 y6 _
- P(0) = V(0). h. X) }4 `9 N- o/ F
- P(1) = V(1)5 l$ r4 G/ E2 B2 O: X! p
- '用户选择了默认
+ q H! R( F8 { - Else* o" a3 e+ c% b* ?$ p4 U
- '计算所有单行文字的几何中心
: s0 \2 f. {/ Z o* Z - For I = 0 To SS.Count - 18 Y& ^: L1 l- V3 \- b: a& M8 R
- V = SS(I).InsertionPoint
G7 I% k0 z# s( Y- B* T - P(0) = P(0) + V(0) / SS.Count1 g$ |& e( e2 N9 N% p
- P(1) = P(1) + V(1) / SS.Count
7 _( h$ V/ y6 T; w2 M1 J - Next
9 {+ [$ W8 o$ O4 A - End If# D [3 S' d$ y: g& H
- '重定义动态数组下标: A# C3 ~ |: s9 v
- ReDim A(SS.Count - 1, 1)
6 m9 Q9 r/ f; f9 s0 S6 B3 m( Q& s - For I = 0 To SS.Count - 1
+ V2 N k& Z4 P% B5 Z - '计算所有单行文字对象相对于中心点的角度,并记录其在选择集中的索引号
% L% d8 @; t# _0 z - A(I, 0) = .Utility.AngleFromXAxis(SS(I).InsertionPoint, P)
0 {' O& |5 M1 e; Z, Z5 _& W. v1 ^( l& U - A(I, 1) = I, T; x9 o& {0 L( K6 p' X5 A
- Next# Q9 |. W- W+ u# N& m4 e: C* Q
- '按角度从大到小的顺序排序5 _0 j. ?" n4 H" j0 Q5 [
- For I = 0 To SS.Count - 28 M+ X o8 F; i6 I1 W) b& A8 w
- For J = I + 1 To SS.Count - 1# o% g6 g+ _: S6 Q
- If A(J, 0) > A(I, 0) Then- [# g: L' o- W& y5 h9 p
- D = A(J, 0)
, f! l! g; G7 H, \8 g - A(J, 0) = A(I, 0)
, }' N0 T' I/ v0 o0 E - A(I, 0) = D
1 m$ Q! D7 P. k/ h - D = A(J, 1)2 f8 X* L; u1 O) g
- A(J, 1) = A(I, 1)
6 s. ~4 w1 R! s, x A - A(I, 1) = D, J, K% Z; F6 ]: @8 K4 ^" U5 W
- End If0 A- Q9 b8 M, O# A0 w6 v- o
- Next
! q5 o, `2 ]3 K3 {' X - Next# y+ }! W+ k! B( ]
- '替换% w' c: o' m: p9 n* @
- For I = 0 To SS.Count - 10 w# |' k& j+ u. ^4 Y
- SS(A(I, 1)).TextString = I + 1
/ ?; D2 D( Q% a2 V) h/ O& H - Next
2 y: K8 v4 X" V5 u# d5 i: t: ^+ ]3 i - End If
0 n3 e2 k/ Z+ O3 W) O - End If% M) |( K# c: F- P/ T! Q2 L& g
- '删除用过的选择集, t, i7 Q* ~, v% ~# s
- SS.Delete- l" `& }6 @8 j8 A
- End With
复制代码 |
|