@atlisp/mcp 1.8.15 → 1.8.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/handlers/3d-handlers.js +128 -0
  2. package/dist/handlers/batch-handlers.js +260 -0
  3. package/dist/handlers/block-handlers.js +214 -0
  4. package/dist/handlers/cad-handlers.js +180 -0
  5. package/dist/handlers/constraint-handlers.js +130 -0
  6. package/dist/handlers/dim-hatch-handlers.js +340 -0
  7. package/dist/handlers/draw-handlers.js +175 -0
  8. package/dist/handlers/edit-handlers.js +179 -0
  9. package/dist/handlers/entity-handlers.js +196 -0
  10. package/dist/handlers/external-handlers.js +218 -0
  11. package/dist/handlers/file-handlers.js +172 -0
  12. package/dist/handlers/function-handlers.js +158 -0
  13. package/dist/handlers/layer-handlers.js +181 -0
  14. package/dist/handlers/package-handlers.js +89 -0
  15. package/dist/handlers/parametric-handlers.js +272 -0
  16. package/dist/handlers/pdf-annotation-handlers.js +117 -0
  17. package/dist/handlers/prompt-handlers.js +9 -0
  18. package/dist/handlers/purge-handlers.js +137 -0
  19. package/dist/handlers/query-print-handlers.js +662 -0
  20. package/dist/handlers/resource-cache.js +63 -0
  21. package/dist/handlers/resource-defs.js +79 -0
  22. package/dist/handlers/resource-handlers.js +131 -0
  23. package/dist/handlers/resource-readers.js +1045 -0
  24. package/dist/handlers/sheetset-handlers.js +430 -0
  25. package/dist/handlers/spatial-handlers.js +174 -0
  26. package/dist/handlers/style-handlers.js +162 -0
  27. package/dist/handlers/text-handlers.js +198 -0
  28. package/dist/handlers/ucs-layout-handlers.js +271 -0
  29. package/dist/handlers/viewport-handlers.js +150 -0
  30. package/dist/handlers/xdata-handlers.js +148 -0
  31. package/dist/handlers/xref-handlers.js +334 -0
  32. package/dist/lisp-security.js +7 -2
  33. package/package.json +2 -2
@@ -0,0 +1,175 @@
1
+ import { cad } from '../cad.js';
2
+ import { escapeLispString } from '../handler-utils.js';
3
+
4
+ export async function drawLine(params) {
5
+ if (!cad.connected) { await cad.connect(); }
6
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
7
+ try {
8
+ const code = `(command "_.LINE" "${escapeLispString(params.p1)}" "${escapeLispString(params.p2)}" "")`;
9
+ await cad.sendCommand(code);
10
+ return { content: [{ type: 'text', text: `已绘制直线: ${params.p1} → ${params.p2}` }] };
11
+ } catch (e) {
12
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
13
+ }
14
+ }
15
+
16
+ export async function drawCircle(params) {
17
+ if (!cad.connected) { await cad.connect(); }
18
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
19
+ try {
20
+ const code = `(command "_.CIRCLE" "${escapeLispString(params.center)}" ${params.radius})`;
21
+ await cad.sendCommand(code);
22
+ return { content: [{ type: 'text', text: `已绘制圆: 圆心 ${params.center},半径 ${params.radius}` }] };
23
+ } catch (e) {
24
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
25
+ }
26
+ }
27
+
28
+ export async function drawArc(params) {
29
+ if (!cad.connected) { await cad.connect(); }
30
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
31
+ try {
32
+ const code = `(progn
33
+ (setq cen (list ${params.center}))
34
+ (setq r ${params.radius})
35
+ (setq ang1 (* pi (/ ${params.startAngle} 180.0)))
36
+ (setq ang2 (* pi (/ ${params.endAngle} 180.0)))
37
+ (command "_.ARC" "C" cen (polar cen ang1 r) (polar cen ang2 r))
38
+ )`;
39
+ await cad.sendCommand(code);
40
+ return { content: [{ type: 'text', text: `已绘制圆弧: 圆心 ${params.center},半径 ${params.radius},角度 ${params.startAngle}°→${params.endAngle}°` }] };
41
+ } catch (e) {
42
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
43
+ }
44
+ }
45
+
46
+ export async function drawRectangle(params) {
47
+ if (!cad.connected) { await cad.connect(); }
48
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
49
+ try {
50
+ const code = `(command "_.RECTANG" "${escapeLispString(params.corner1)}" "${escapeLispString(params.corner2)}")`;
51
+ await cad.sendCommand(code);
52
+ return { content: [{ type: 'text', text: `已绘制矩形: ${params.corner1} — ${params.corner2}` }] };
53
+ } catch (e) {
54
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
55
+ }
56
+ }
57
+
58
+ export async function drawPolygon(params) {
59
+ if (!cad.connected) { await cad.connect(); }
60
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
61
+ try {
62
+ const code = `(command "_.POLYGON" ${params.sides} "${escapeLispString(params.center)}" "I" ${params.radius})`;
63
+ await cad.sendCommand(code);
64
+ return { content: [{ type: 'text', text: `已绘制正${params.sides}边形: 中心 ${params.center},半径 ${params.radius}` }] };
65
+ } catch (e) {
66
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
67
+ }
68
+ }
69
+
70
+ export async function drawEllipse(params) {
71
+ if (!cad.connected) { await cad.connect(); }
72
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
73
+ try {
74
+ const code = `(command "_.ELLIPSE" "C" "${escapeLispString(params.center)}" "${escapeLispString(params.majorAxis)}" "${escapeLispString(params.minorAxis)}")`;
75
+ await cad.sendCommand(code);
76
+ return { content: [{ type: 'text', text: `已绘制椭圆: 中心 ${params.center}` }] };
77
+ } catch (e) {
78
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
79
+ }
80
+ }
81
+
82
+ export async function drawText(params) {
83
+ if (!cad.connected) { await cad.connect(); }
84
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
85
+ try {
86
+ const h = params.height || 2.5;
87
+ const code = `(command "_.TEXT" "${escapeLispString(params.point)}" ${h} "0" "${escapeLispString(params.text)}")`;
88
+ await cad.sendCommand(code);
89
+ return { content: [{ type: 'text', text: `已创建单行文字: "${params.text}"` }] };
90
+ } catch (e) {
91
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
92
+ }
93
+ }
94
+
95
+ export async function drawMtext(params) {
96
+ if (!cad.connected) { await cad.connect(); }
97
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
98
+ try {
99
+ const w = params.width || 0;
100
+ const code = `(command "_.MTEXT" "${escapeLispString(params.point)}" "W" ${w} "${escapeLispString(params.text)}" "")`;
101
+ await cad.sendCommand(code);
102
+ return { content: [{ type: 'text', text: `已创建多行文字: "${params.text}"` }] };
103
+ } catch (e) {
104
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
105
+ }
106
+ }
107
+
108
+ export async function drawPolyline(params) {
109
+ if (!cad.connected) { await cad.connect(); }
110
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
111
+ try {
112
+ const pts = params.points.split(/\s+/).filter(Boolean);
113
+ const pointArgs = pts.map(p => `"${escapeLispString(p)}"`).join(' ');
114
+ const closeArg = params.closed ? ' "C"' : ' ""';
115
+ const code = `(command "_.PLINE" ${pointArgs}${closeArg})`;
116
+ await cad.sendCommand(code);
117
+ return { content: [{ type: 'text', text: `已创建多段线: ${pts.length} 个顶点` }] };
118
+ } catch (e) {
119
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
120
+ }
121
+ }
122
+
123
+ export async function drawSpline(params) {
124
+ if (!cad.connected) { await cad.connect(); }
125
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
126
+ try {
127
+ const pts = params.points.split(/\s+/).filter(Boolean);
128
+ const pointArgs = pts.map(p => `"${escapeLispString(p)}"`).join(' ');
129
+ const code = `(command "_.SPLINE" ${pointArgs} "" "" "")`;
130
+ await cad.sendCommand(code);
131
+ return { content: [{ type: 'text', text: `已创建样条曲线: ${pts.length} 个控制点` }] };
132
+ } catch (e) {
133
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
134
+ }
135
+ }
136
+
137
+ export async function drawPoint(params) {
138
+ if (!cad.connected) { await cad.connect(); }
139
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
140
+ try {
141
+ const code = `(command "_.POINT" "${escapeLispString(params.point)}")`;
142
+ await cad.sendCommand(code);
143
+ return { content: [{ type: 'text', text: `已创建点: ${params.point}` }] };
144
+ } catch (e) {
145
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
146
+ }
147
+ }
148
+
149
+ export async function drawHatch(params) {
150
+ if (!cad.connected) { await cad.connect(); }
151
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
152
+ try {
153
+ const pattern = params.pattern || 'ANSI31';
154
+ const pts = params.points.split(/\s+/).filter(Boolean);
155
+ const ptList = pts.map(p => { const [x, y] = p.split(','); return `${x} ${y}`; }).join(' ');
156
+ const lispPts = `(list ${pts.map(p => `(list ${p})`).join(' ')})`;
157
+ const code = `(progn
158
+ (setq pts ${lispPts})
159
+ (entmake (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline")
160
+ (cons 90 ${pts.length}) '(70 . 1)
161
+ (cons 10 (nth 0 pts)) (cons 10 (nth 1 pts))
162
+ ))
163
+ )`;
164
+ const code2 = `(command "_.HATCH" "${escapeLispString(pattern)}" "" "")`;
165
+ try {
166
+ await cad.sendCommand(code);
167
+ await cad.sendCommand(code2);
168
+ } catch(hErr) {
169
+ return { content: [{ type: 'text', text: `错误: ${hErr.message}` }], isError: true };
170
+ }
171
+ return { content: [{ type: 'text', text: `已创建图案填充: ${pattern}` }] };
172
+ } catch (e) {
173
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
174
+ }
175
+ }
@@ -0,0 +1,179 @@
1
+ import { cad } from '../cad.js';
2
+ import { escapeLispString } from '../handler-utils.js';
3
+
4
+ export async function trimEntity(params) {
5
+ if (!cad.connected) { await cad.connect(); }
6
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
7
+ try {
8
+ const code = `(progn
9
+ (vl-load-com)
10
+ (if (setq ent1 (handent "${escapeLispString(params.handle)}"))
11
+ (if (setq ent2 (handent "${escapeLispString(params.cuttingEdgeHandle)}"))
12
+ (progn (command "_.TRIM" ent2 "" ent1 "" "") "OK")
13
+ "CUT_EDGE_NOT_FOUND"
14
+ )
15
+ "NOTFOUND"
16
+ )
17
+ )`;
18
+ const result = await cad.sendCommandWithResult(code);
19
+ if (result === 'OK' || result === '"OK"') {
20
+ return { content: [{ type: 'text', text: `已修剪实体: ${params.handle}` }] };
21
+ }
22
+ return { content: [{ type: 'text', text: `修剪失败: ${result}` }], isError: true };
23
+ } catch (e) {
24
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
25
+ }
26
+ }
27
+
28
+ export async function extendEntity(params) {
29
+ if (!cad.connected) { await cad.connect(); }
30
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
31
+ try {
32
+ const code = `(progn
33
+ (vl-load-com)
34
+ (if (setq ent1 (handent "${escapeLispString(params.handle)}"))
35
+ (if (setq ent2 (handent "${escapeLispString(params.boundaryHandle)}"))
36
+ (progn (command "_.EXTEND" ent2 "" ent1 "") "OK")
37
+ "BOUNDARY_NOT_FOUND"
38
+ )
39
+ "NOTFOUND"
40
+ )
41
+ )`;
42
+ const result = await cad.sendCommandWithResult(code);
43
+ if (result === 'OK' || result === '"OK"') {
44
+ return { content: [{ type: 'text', text: `已延伸实体: ${params.handle}` }] };
45
+ }
46
+ return { content: [{ type: 'text', text: `延伸失败: ${result}` }], isError: true };
47
+ } catch (e) {
48
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
49
+ }
50
+ }
51
+
52
+ export async function fillet(params) {
53
+ if (!cad.connected) { await cad.connect(); }
54
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
55
+ try {
56
+ const code = `(progn
57
+ (vl-load-com)
58
+ (if (and (setq ent1 (handent "${escapeLispString(params.handle1)}"))
59
+ (setq ent2 (handent "${escapeLispString(params.handle2)}")))
60
+ (progn
61
+ (command "_.FILLET" "R" ${params.radius} "_.FILLET" ent1 ent2)
62
+ "OK"
63
+ )
64
+ "NOTFOUND"
65
+ )
66
+ )`;
67
+ const result = await cad.sendCommandWithResult(code);
68
+ if (result === 'OK' || result === '"OK"') {
69
+ return { content: [{ type: 'text', text: `已圆角连接实体,半径=${params.radius}` }] };
70
+ }
71
+ return { content: [{ type: 'text', text: `圆角失败: ${result}` }], isError: true };
72
+ } catch (e) {
73
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
74
+ }
75
+ }
76
+
77
+ export async function chamfer(params) {
78
+ if (!cad.connected) { await cad.connect(); }
79
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
80
+ try {
81
+ const code = `(progn
82
+ (vl-load-com)
83
+ (if (and (setq ent1 (handent "${escapeLispString(params.handle1)}"))
84
+ (setq ent2 (handent "${escapeLispString(params.handle2)}")))
85
+ (progn
86
+ (command "_.CHAMFER" "D" ${params.distance1} ${params.distance2} "_.CHAMFER" ent1 ent2)
87
+ "OK"
88
+ )
89
+ "NOTFOUND"
90
+ )
91
+ )`;
92
+ const result = await cad.sendCommandWithResult(code);
93
+ if (result === 'OK' || result === '"OK"') {
94
+ return { content: [{ type: 'text', text: `已倒角连接实体,距离=${params.distance1},${params.distance2}` }] };
95
+ }
96
+ return { content: [{ type: 'text', text: `倒角失败: ${result}` }], isError: true };
97
+ } catch (e) {
98
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
99
+ }
100
+ }
101
+
102
+ export async function breakEntity(params) {
103
+ if (!cad.connected) { await cad.connect(); }
104
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
105
+ try {
106
+ const code = `(progn
107
+ (vl-load-com)
108
+ (if (setq ent (handent "${escapeLispString(params.handle)}"))
109
+ (progn
110
+ (command "_.BREAK" ent "${escapeLispString(params.point1)}" "${escapeLispString(params.point2)}")
111
+ "OK"
112
+ )
113
+ "NOTFOUND"
114
+ )
115
+ )`;
116
+ const result = await cad.sendCommandWithResult(code);
117
+ if (result === 'OK' || result === '"OK"') {
118
+ return { content: [{ type: 'text', text: `已打断实体: ${params.handle}` }] };
119
+ }
120
+ return { content: [{ type: 'text', text: `打断失败: ${result}` }], isError: true };
121
+ } catch (e) {
122
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
123
+ }
124
+ }
125
+
126
+ export async function joinEntities(params) {
127
+ if (!cad.connected) { await cad.connect(); }
128
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
129
+ try {
130
+ const handleList = params.handles
131
+ .map(h => `(setq ss (ssadd (handent "${escapeLispString(h)}") ss))`)
132
+ .join('\n');
133
+ const code = `(progn
134
+ (vl-load-com)
135
+ (setq ss (ssadd))
136
+ ${handleList}
137
+ (if (> (sslength ss) 1)
138
+ (progn (command "_.JOIN" ss "") "OK")
139
+ "TOO_FEW"
140
+ )
141
+ )`;
142
+ const result = await cad.sendCommandWithResult(code);
143
+ if (result === 'OK' || result === '"OK"') {
144
+ return { content: [{ type: 'text', text: `已合并 ${params.handles.length} 个实体` }] };
145
+ }
146
+ return { content: [{ type: 'text', text: `合并失败: ${result}` }], isError: true };
147
+ } catch (e) {
148
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
149
+ }
150
+ }
151
+
152
+ export async function stretch(params) {
153
+ if (!cad.connected) { await cad.connect(); }
154
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
155
+ try {
156
+ const handleList = params.handles
157
+ .map(h => `(setq ss (ssadd (handent "${escapeLispString(h)}") ss))`)
158
+ .join('\n');
159
+ const code = `(progn
160
+ (vl-load-com)
161
+ (setq ss (ssadd))
162
+ ${handleList}
163
+ (if (> (sslength ss) 0)
164
+ (progn
165
+ (command "_.STRETCH" ss "" "${escapeLispString(params.basePoint)}" "${escapeLispString(params.displacement)}")
166
+ "OK"
167
+ )
168
+ "NO_ENTITIES"
169
+ )
170
+ )`;
171
+ const result = await cad.sendCommandWithResult(code);
172
+ if (result === 'OK' || result === '"OK"') {
173
+ return { content: [{ type: 'text', text: `已拉伸 ${params.handles.length} 个实体` }] };
174
+ }
175
+ return { content: [{ type: 'text', text: `拉伸失败: ${result}` }], isError: true };
176
+ } catch (e) {
177
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
178
+ }
179
+ }
@@ -0,0 +1,196 @@
1
+ import { cad } from '../cad.js';
2
+ import { log } from '../logger.js';
3
+ import { escapeLispString } from '../handler-utils.js';
4
+
5
+ export async function getEntity(handle) {
6
+ if (!cad.connected) { await cad.connect(); }
7
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
8
+
9
+ const code = `(progn
10
+ (vl-load-com)
11
+ (if (setq ent (handent "${escapeLispString(handle)}"))
12
+ (progn
13
+ (setq ed (entget ent))
14
+ (list
15
+ "type" (cdr (assoc 0 ed))
16
+ "handle" (cdr (assoc 5 ed))
17
+ "layer" (cdr (assoc 8 ed))
18
+ "color" (if (assoc 62 ed) (cdr (assoc 62 ed)) 256)
19
+ "linetype" (if (assoc 6 ed) (cdr (assoc 6 ed)) "ByLayer")
20
+ "elevation" (if (assoc 38 ed) (cdr (assoc 38 ed)) 0.0)
21
+ )
22
+ )
23
+ nil
24
+ )
25
+ )`;
26
+ try {
27
+ const result = await cad.sendCommandWithResult(code);
28
+ if (!result || result === 'nil') {
29
+ return { content: [{ type: 'text', text: `未找到实体: ${handle}` }], isError: true };
30
+ }
31
+ return { content: [{ type: 'text', text: result }] };
32
+ } catch (e) {
33
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
34
+ }
35
+ }
36
+
37
+ export async function setEntityProperty(handle, property, value) {
38
+ if (!cad.connected) { await cad.connect(); }
39
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
40
+
41
+ let propCode = '';
42
+ switch (property) {
43
+ case 'layer':
44
+ propCode = `(cons 8 "${escapeLispString(value)}")`;
45
+ break;
46
+ case 'color':
47
+ propCode = `(cons 62 ${isNaN(Number(value)) ? 256 : Number(value)})`;
48
+ break;
49
+ case 'linetype':
50
+ propCode = `(cons 6 "${escapeLispString(value)}")`;
51
+ break;
52
+ case 'elevation':
53
+ propCode = `(cons 38 ${isNaN(Number(value)) ? 0 : Number(value)})`;
54
+ break;
55
+ default:
56
+ return { content: [{ type: 'text', text: `不支持的属性: ${property}` }], isError: true };
57
+ }
58
+
59
+ const code = `(progn
60
+ (vl-load-com)
61
+ (if (setq ent (handent "${escapeLispString(handle)}"))
62
+ (progn
63
+ (setq ed (entget ent))
64
+ (entmod (append ed (list ${propCode})))
65
+ "OK"
66
+ )
67
+ "NOTFOUND"
68
+ )
69
+ )`;
70
+ try {
71
+ const result = await cad.sendCommandWithResult(code);
72
+ if (result === 'OK' || result === '"OK"') {
73
+ return { content: [{ type: 'text', text: `已设置实体 ${handle} 的 ${property} 为 ${value}` }] };
74
+ }
75
+ return { content: [{ type: 'text', text: `操作失败: ${result}` }], isError: true };
76
+ } catch (e) {
77
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
78
+ }
79
+ }
80
+
81
+ export async function deleteEntity(handle) {
82
+ if (!cad.connected) { await cad.connect(); }
83
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
84
+
85
+ const code = `(progn
86
+ (vl-load-com)
87
+ (if (setq ent (handent "${escapeLispString(handle)}"))
88
+ (progn
89
+ (entdel ent)
90
+ "DELETED"
91
+ )
92
+ "NOTFOUND"
93
+ )
94
+ )`;
95
+ try {
96
+ const result = await cad.sendCommandWithResult(code);
97
+ if (result === 'DELETED' || result === '"DELETED"') {
98
+ return { content: [{ type: 'text', text: `已删除实体: ${handle}` }] };
99
+ }
100
+ return { content: [{ type: 'text', text: `删除失败: ${result}` }], isError: true };
101
+ } catch (e) {
102
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
103
+ }
104
+ }
105
+
106
+ export async function selectEntities(filter) {
107
+ if (!cad.connected) { await cad.connect(); }
108
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
109
+
110
+ let filterCode = 'nil';
111
+ if (filter && (filter.type || filter.layer)) {
112
+ const items = [];
113
+ if (filter.type) items.push(`'(0 . "${escapeLispString(filter.type)}")`);
114
+ if (filter.layer) items.push(`'(8 . "${escapeLispString(filter.layer)}")`);
115
+ filterCode = `(list ${items.join(' ')})`;
116
+ }
117
+
118
+ const code = `(progn
119
+ (vl-load-com)
120
+ (if (setq ss (ssget "X" ${filterCode}))
121
+ (progn
122
+ (setq cnt (sslength ss) handles nil i 0)
123
+ (while (< i cnt)
124
+ (setq handles (cons (cdr (assoc 5 (entget (ssname ss i)))) handles))
125
+ (setq i (1+ i)))
126
+ (reverse handles)
127
+ )
128
+ nil
129
+ )
130
+ )`;
131
+ try {
132
+ const result = await cad.sendCommandWithResult(code);
133
+ if (!result || result === 'nil') {
134
+ return { content: [{ type: 'text', text: '未找到符合条件的实体' }] };
135
+ }
136
+ return { content: [{ type: 'text', text: result }] };
137
+ } catch (e) {
138
+ return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true };
139
+ }
140
+ }
141
+ export async function bringToFront(handle) {
142
+ if (!cad.connected) { await cad.connect(); }
143
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
144
+ try {
145
+ await cad.sendCommand(`(command "_.DRAWORDER" (handent "${escapeLispString(handle)}") "" "_F")\n`);
146
+ return { content: [{ type: 'text', text: `已置于最前: ${handle}` }] };
147
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
148
+ }
149
+ export async function sendToBack(handle) {
150
+ if (!cad.connected) { await cad.connect(); }
151
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
152
+ try {
153
+ await cad.sendCommand(`(command "_.DRAWORDER" (handent "${escapeLispString(handle)}") "" "_B")\n`);
154
+ return { content: [{ type: 'text', text: `已置于最后: ${handle}` }] };
155
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
156
+ }
157
+ export async function bringAbove(handle, referenceHandle) {
158
+ if (!cad.connected) { await cad.connect(); }
159
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
160
+ try {
161
+ await cad.sendCommand(`(command "_.DRAWORDER" (handent "${escapeLispString(handle)}") "" "_A" (handent "${escapeLispString(referenceHandle)}"))\n`);
162
+ return { content: [{ type: 'text', text: `已置于 ${referenceHandle} 之上` }] };
163
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
164
+ }
165
+ export async function sendBelow(handle, referenceHandle) {
166
+ if (!cad.connected) { await cad.connect(); }
167
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
168
+ try {
169
+ await cad.sendCommand(`(command "_.DRAWORDER" (handent "${escapeLispString(handle)}") "" "_U" (handent "${escapeLispString(referenceHandle)}"))\n`);
170
+ return { content: [{ type: 'text', text: `已置于 ${referenceHandle} 之下` }] };
171
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
172
+ }
173
+ export async function selectAll() {
174
+ if (!cad.connected) { await cad.connect(); }
175
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
176
+ try {
177
+ const result = await cad.sendCommandWithResult('(progn (setq ss (ssget "_X")) (setq handles nil) (setq i 0) (if ss (repeat (sslength ss) (setq handles (cons (cdr (assoc 5 (entget (ssname ss i)))) handles)) (setq i (1+ i)))) (vl-prin1-to-string (reverse handles)))');
178
+ return { content: [{ type: 'text', text: result || '[]' }] };
179
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
180
+ }
181
+ export async function selectLast() {
182
+ if (!cad.connected) { await cad.connect(); }
183
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
184
+ try {
185
+ const result = await cad.sendCommandWithResult('(vl-princ-to-string (list (cdr (assoc 5 (entget (entlast))))))');
186
+ return { content: [{ type: 'text', text: result || 'nil' }] };
187
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
188
+ }
189
+ export async function selectByColor(colorIndex) {
190
+ if (!cad.connected) { await cad.connect(); }
191
+ if (!cad.connected) return { content: [{ type: 'text', text: '未连接 CAD' }], isError: true };
192
+ try {
193
+ const result = await cad.sendCommandWithResult(`(progn (setq ss (ssget "_X" (list (cons 62 ${colorIndex})))) (setq handles nil) (setq i 0) (if ss (repeat (sslength ss) (setq handles (cons (cdr (assoc 5 (entget (ssname ss i)))) handles)) (setq i (1+ i)))) (vl-prin1-to-string (reverse handles)))`);
194
+ return { content: [{ type: 'text', text: result || '[]' }] };
195
+ } catch (e) { return { content: [{ type: 'text', text: `错误: ${e.message}` }], isError: true }; }
196
+ }