|
用了deepseek寫的VBA代碼用在CAD,挺好用的。有沒有其他的方便CAD使用的deepseek的例子推薦一下? 4 K6 M1 @& a3 Y- J
- Sub AddRectangleAndArrayAndTrim(). n0 U5 W2 x# b
- ' 聲明變量& K- p* x; A1 D# N4 `, s
- Dim lineObj As Object
% T* l- c( X7 x - Dim startPoint As Variant3 ^. h4 t9 q0 c- J5 [
- Dim endPoint As Variant( L1 @5 R' H e; g9 ? w8 F
- Dim rectWidth As Double6 j' \ i& t3 B9 F- T
- Dim rectHeight As Double
7 ^# U2 z1 T+ z; Z& ?8 F3 t - Dim rectStartPoint(0 To 2) As Double
9 u* [8 [$ U/ \4 C - Dim rectEndPoint(0 To 2) As Double* F. k2 q6 d* t
- Dim rotationAngle As Double: z, t: m; i& U: {, e: a( ?4 m4 w
- Dim rectObj As Object
8 Q1 _& w) |0 N - Dim points(0 To 7) As Double ' 用于存儲矩形的四個頂點
: M0 @; g" C- {, Y6 j9 y - Dim centerPoint(0 To 2) As Double ' 直線的中點* ]8 g |- C+ y7 a) I
- Dim newRectObj As Object ' 復制的矩形對象
6 c+ m4 l8 v& c" S5 b3 v' K - Dim rotationAngleRad As Double ' 旋轉角度(弧度)
/ ] U9 x: k5 Z P$ H4 B - Dim intersectPoint As Variant ' 交點* V& ?8 Y4 ?, ~2 S
- Dim trimStartPoint As Variant ' 修剪后的起點
( a" [! c3 e6 z9 t - Dim trimEndPoint As Variant ' 修剪后的終點/ A8 d& c" \# A8 u4 s. L! n" ~$ M
- ' P7 m' X: N) d& G8 y
- ' 定義矩形的尺寸0 w! r0 m1 l1 |4 Y6 P5 e- B
- rectWidth = 0.1 ' 矩形的寬度(短邊)2 x' p% X* s) W/ y' E
- rectHeight = 1 ' 矩形的高度(長邊)
: W& Q( [" S6 d. O, k4 m: g8 M - 3 ?* l! x! f% { j. y* t) I' s4 y
- ' 提示用戶選擇一條直線3 S2 k& y% y6 a$ Z$ u+ i$ T% ?+ p: v6 r
- On Error Resume Next
; a; J' M' N, w8 z - ThisDrawing.Utility.GetEntity lineObj, startPoint, "請選擇一條直線: "
: Q" _" N6 n- Q+ b5 S - On Error GoTo 0# n: q: U5 S, |, o
-
5 j1 `* q/ g* a7 J, I - ' 檢查用戶是否選擇了直線
, O: |4 p. i0 H) m' j* s7 J: `6 | - If lineObj Is Nothing Then
0 [2 {8 U! l: |/ p) J l - MsgBox "未選擇直線或選擇無效。"; Q8 ^) k. G9 i. S
- Exit Sub
% x- g* E* V) B i x& ~- b - End If( r; B) a+ E' B0 }
-
% h: I% c3 Q. g5 d9 c; {) f/ m: o - ' 獲取直線的起點和終點
7 A. I) O) z/ L+ q! F8 T) \ - startPoint = lineObj.StartPoint0 v& `& c$ L+ U# U: B+ n
- endPoint = lineObj.EndPoint9 n, R {$ ^* x$ _- C" B/ {
-
4 N' g' W+ F/ T4 x0 Z2 I - ' 計算直線的中點/ I6 a$ i5 J& w7 G# c& F
- centerPoint(0) = (startPoint(0) + endPoint(0)) / 2
$ W6 v0 V( K8 N3 n" j0 [; _8 Q - centerPoint(1) = (startPoint(1) + endPoint(1)) / 2
, H2 t, T$ k M - centerPoint(2) = (startPoint(2) + endPoint(2)) / 2
( Y6 d3 n* s, C% |; Z6 s5 s1 L - * Y/ a8 i0 @$ R3 e
- ' 計算直線的角度(用于矩形的旋轉) z0 t/ C7 {% \7 g; R+ \4 z
- rotationAngle = Atn((endPoint(1) - startPoint(1)) / (endPoint(0) - startPoint(0)))
6 o2 V* W) y+ h" W; ~7 n - / w, Y5 b* Y& r, W+ ?! N/ h& W* ?
- ' 計算矩形的起點和終點- F2 _0 v1 E7 W% c k4 S
- rectStartPoint(0) = startPoint(0) - (rectWidth / 2) * Cos(rotationAngle + (3.14159 / 2))
* j8 r- x+ I' k6 }/ A - rectStartPoint(1) = startPoint(1) - (rectWidth / 2) * Sin(rotationAngle + (3.14159 / 2))
i1 l4 C/ o' s - rectStartPoint(2) = startPoint(2)( o' [$ g' f* n$ x, w' ^
-
+ p- G7 u% J" m2 \ - rectEndPoint(0) = rectStartPoint(0) + rectHeight * Cos(rotationAngle)& W0 m, I9 C5 H- c+ ~/ r& x
- rectEndPoint(1) = rectStartPoint(1) + rectHeight * Sin(rotationAngle)! h/ l* k( F8 Y! X) u
- rectEndPoint(2) = rectStartPoint(2)
# X' R. J$ ?. w* ^ - * K% g. ^# L+ v, i; c5 P( z
- ' 定義矩形的四個頂點
( C8 z7 v; k/ W: x- _ - points(0) = rectStartPoint(0)
9 y* A# o7 z3 k0 T5 I' n: Y - points(1) = rectStartPoint(1)
. N$ T: K' m0 Z( F% p; U - points(2) = rectEndPoint(0)3 \" U m7 {8 B$ J+ ^
- points(3) = rectEndPoint(1)* ^/ o* N6 j+ G; H, k
- points(4) = rectEndPoint(0) + rectWidth * Cos(rotationAngle + (3.14159 / 2))
# C7 R, F0 o- g5 L! S - points(5) = rectEndPoint(1) + rectWidth * Sin(rotationAngle + (3.14159 / 2))2 ^% N" i8 n( U) \2 ^- a
- points(6) = rectStartPoint(0) + rectWidth * Cos(rotationAngle + (3.14159 / 2))
- ]3 `2 g: p. ?% d2 U+ k3 ^ - points(7) = rectStartPoint(1) + rectWidth * Sin(rotationAngle + (3.14159 / 2))
$ C/ z/ _/ p2 X0 S9 c+ ? I - 4 J0 b6 Q; ?. U$ m
- ' 創建矩形8 s# _! W% O5 c! Q
- Set rectObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)8 {0 }! [ ]2 }+ T& N
- # c7 K9 T2 y& F+ O
- ' 創建圓周陣列(手動復制和旋轉)0 H; c: K+ P- T5 {
- rotationAngleRad = 180 * (3.14159 / 180) ' 將角度轉換為弧度3 o9 v2 [# A: t/ p) `4 G0 Z0 C
- Set newRectObj = rectObj.Copy8 ~+ u' V9 V; O+ W* l
- newRectObj.Rotate centerPoint, rotationAngleRad
0 q6 [1 W$ d4 o! c* X - 7 R- H7 W( G0 b# s* A6 K
- ' 修剪直線
& T0 k. i1 B7 w' K8 h - ' 查找直線與矩形的交點
2 k: S" F6 C& ?# D2 q9 G1 ? - intersectPoint = lineObj.IntersectWith(rectObj, acExtendNone)
' I' ^* h# J8 V& F - If Not IsEmpty(intersectPoint) Then7 y7 q6 k) ?/ N1 Q- N
- ' 修剪直線的起點9 r+ w* i; J( \! V0 \# e' ^
- trimStartPoint = intersectPoint: z9 u0 p; T7 H
- lineObj.StartPoint = trimStartPoint( i+ p) e2 { E
- End If
- J; A* J/ s- P/ {# O6 | - 1 `" h! Y- w! t! g3 Q B8 `/ Y7 M
- intersectPoint = lineObj.IntersectWith(newRectObj, acExtendNone)# \" U9 g3 y/ B4 ^/ ~, ]+ b
- If Not IsEmpty(intersectPoint) Then, V% Y7 E7 B3 S2 N
- ' 修剪直線的終點
! @" x" F3 M6 E% G5 {. T# T/ j - trimEndPoint = intersectPoint
: ?7 N1 m* [; H+ P, N8 J+ g; ^ - lineObj.EndPoint = trimEndPoint7 ?4 O6 U1 y- i$ _$ v
- End If
5 ~* A9 I: O T4 j+ O - # _' ]# j4 B+ w! ]- q+ I/ I
- ' 刷新視圖
3 y8 `; @! j8 z2 r - ThisDrawing.Regen True
! {. ~7 K9 {5 q v+ j! T - ! k6 M' [9 [' P8 K) P
- ' 提示用戶! u0 M! r' ?0 V( |- |( a
- MsgBox "矩形、陣列和修剪操作已完成!"( u8 B, q+ {* q; G
- End Sub
復制代碼
$ k/ y) k7 s! }" ?6 v9 ~: B3 o9 Y! q# L. g5 D" _# O/ I
|
|