dsh-eplan 0.0.0-stage → 0.14.1
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.
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/cordis.patch.yml +182 -0
- package/docs/INSTALL.md +86 -0
- package/docs/LESSONS.md +224 -0
- package/docs/interfaces.md +183 -0
- package/docs/merged-skills/eplan-setup.md +63 -0
- package/docs/merged-skills/eplan-troubleshooting.md +149 -0
- package/icon.png +0 -0
- package/icon.svg +13 -0
- package/knowledge/KNOWLEDGE.md +413 -0
- package/knowledge/symbols/BP.png +0 -0
- package/knowledge/symbols/CDPST.png +0 -0
- package/knowledge/symbols/CO.png +0 -0
- package/knowledge/symbols/FA3.png +0 -0
- package/knowledge/symbols/TLRO.png +0 -0
- package/knowledge/symbols/TLRU.png +0 -0
- package/knowledge/symbols/TOUL.png +0 -0
- package/knowledge/symbols/TOUR.png +0 -0
- package/knowledge/symbols/manifest.json +91 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/native/bridge/build.ps1 +73 -0
- package/native/bridge/src/Json.cs +383 -0
- package/native/bridge/src/Program.cs +1133 -0
- package/native/host/build.ps1 +74 -0
- package/native/host/src/ActionsTool.cs +225 -0
- package/native/host/src/BoxTool.cs +196 -0
- package/native/host/src/CheckTool.cs +186 -0
- package/native/host/src/Context.cs +169 -0
- package/native/host/src/DarkScheme.cs +111 -0
- package/native/host/src/DiagnoseTool.cs +115 -0
- package/native/host/src/Dispatcher.cs +51 -0
- package/native/host/src/DrawTool.cs +316 -0
- package/native/host/src/Entry.cs +111 -0
- package/native/host/src/ExportTool.cs +213 -0
- package/native/host/src/InspectTool.cs +367 -0
- package/native/host/src/KnowledgeTool.cs +166 -0
- package/native/host/src/LookTool.cs +257 -0
- package/native/host/src/OpLog.cs +289 -0
- package/native/host/src/PageCreateTool.cs +521 -0
- package/native/host/src/PageOpenTool.cs +62 -0
- package/native/host/src/PagesTool.cs +95 -0
- package/native/host/src/Paths.cs +91 -0
- package/native/host/src/PlacementEditTool.cs +234 -0
- package/native/host/src/ProbePropsTool.cs +152 -0
- package/native/host/src/ProjectCreateTool.cs +176 -0
- package/native/host/src/ProjectTool.cs +167 -0
- package/native/host/src/PropWriter.cs +70 -0
- package/native/host/src/PropsTool.cs +257 -0
- package/native/host/src/StatusTool.cs +168 -0
- package/native/host/src/SymbolInfoTool.cs +144 -0
- package/native/host/src/SymbolInsertTool.cs +261 -0
- package/native/host/src/SymbolsTool.cs +94 -0
- package/native/host/src/TagLocTool.cs +108 -0
- package/native/script/EplanMcpBootstrap.cs +221 -0
- package/native/script/build-symbol-gallery.mjs +231 -0
- package/native/script/prepare-install.ps1 +57 -0
- package/native/script/prune.mjs +132 -0
- package/native/script/verify-bootstrap.ps1 +62 -0
- package/package.json +53 -4
- package/preset/bootstrap.js +498 -0
- package/preset/experience-nudge.js +47 -0
- package/preset/image-budget.js +175 -0
- package/preset/knowledge-tool.js +180 -0
- package/preset/mcp-link.js +100 -0
- package/preset/prompt-surface.js +76 -0
- package/preset/restrict-tools.js +141 -0
- package/preset/skill-docs.js +181 -0
- package/preset/video-digest.js +294 -0
- package/preset/video-tool.js +92 -0
- package/server/compact.mjs +623 -0
- package/server/edit-args.mjs +67 -0
- package/server/fuzzy.mjs +57 -0
- package/server/launch-decision.mjs +26 -0
- package/server/measure.mjs +320 -0
- package/server/pdf.mjs +185 -0
- package/server/pdf2png.ps1 +99 -0
- package/server/server.mjs +3797 -0
- package/server/video.mjs +857 -0
- package/setup.mjs +223 -0
- package/skills/eplan-annotations/SKILL.md +55 -0
- package/skills/eplan-drawing/SKILL.md +206 -0
- package/skills/eplan-project/SKILL.md +41 -0
- package/skills/eplan-review/SKILL.md +80 -0
- package/skills/eplan-symbols/SKILL.md +142 -0
- package/test/check-docs.mjs +106 -0
- package/test/foreground.ps1 +21 -0
- package/test/smoke-all.mjs +229 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// draw:在一页上批量画线、矩形、文本、折线。
|
|
2
|
+
// 支持 dryRun:只回方案不落图。失败时把本次已创建的图元删掉,不留半成品。
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections.Generic;
|
|
5
|
+
using Eplan.EplApi.Base;
|
|
6
|
+
using Eplan.EplApi.DataModel;
|
|
7
|
+
using Eplan.EplApi.DataModel.Graphics;
|
|
8
|
+
using EplanMcp;
|
|
9
|
+
|
|
10
|
+
namespace EplanMcpHost
|
|
11
|
+
{
|
|
12
|
+
internal static class DrawTool
|
|
13
|
+
{
|
|
14
|
+
public static JsonValue Run(JsonValue args)
|
|
15
|
+
{
|
|
16
|
+
string pageName = args["page"].AsString(null);
|
|
17
|
+
JsonValue items = args["items"];
|
|
18
|
+
bool dryRun = args["dryRun"].AsBool(false);
|
|
19
|
+
|
|
20
|
+
if (items == null || !items.IsArray || items.Count == 0)
|
|
21
|
+
throw new HostException("BAD_ARGUMENT", "items 必须是非空数组。");
|
|
22
|
+
|
|
23
|
+
Page page = Context.FindPage(pageName, args["project"].AsString(null));
|
|
24
|
+
|
|
25
|
+
JsonValue result = JsonValue.NewObject();
|
|
26
|
+
result.Set("page", page.Name);
|
|
27
|
+
result.Set("dryRun", dryRun);
|
|
28
|
+
result.Set("requestedCount", items.Count);
|
|
29
|
+
|
|
30
|
+
List<JsonValue> planned = new List<JsonValue>();
|
|
31
|
+
List<Placement> created = new List<Placement>();
|
|
32
|
+
|
|
33
|
+
for (int i = 0; i < items.Count; i++)
|
|
34
|
+
{
|
|
35
|
+
JsonValue item = items[i];
|
|
36
|
+
string kind = item["kind"].AsString("");
|
|
37
|
+
JsonValue description = DescribeItem(kind, item, i);
|
|
38
|
+
planned.Add(description);
|
|
39
|
+
if (!description["valid"].AsBool(false)) continue;
|
|
40
|
+
if (dryRun) continue;
|
|
41
|
+
|
|
42
|
+
try
|
|
43
|
+
{
|
|
44
|
+
created.Add(CreateOne(page, kind, item));
|
|
45
|
+
}
|
|
46
|
+
catch (Exception ex)
|
|
47
|
+
{
|
|
48
|
+
// 回滚已创建的部分,避免留下半成品。
|
|
49
|
+
Rollback(created);
|
|
50
|
+
throw new HostException("DRAW_FAILED",
|
|
51
|
+
"第 " + (i + 1) + " 个图元(" + kind + ")绘制失败:" + ex.Message +
|
|
52
|
+
"。已回滚本次画出的 " + created.Count + " 个图元。",
|
|
53
|
+
ex.ToString());
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 校验:有没有非法项。
|
|
58
|
+
foreach (JsonValue d in planned)
|
|
59
|
+
{
|
|
60
|
+
if (!d["valid"].AsBool(false))
|
|
61
|
+
{
|
|
62
|
+
if (!dryRun) Rollback(created);
|
|
63
|
+
result.Set("items", ToArray(planned));
|
|
64
|
+
result.Set("created", 0);
|
|
65
|
+
throw new HostException("BAD_ARGUMENT",
|
|
66
|
+
"有坐标或参数不合法的图元:" + Json.Write(d));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
result.Set("items", ToArray(planned));
|
|
71
|
+
result.Set("created", created.Count);
|
|
72
|
+
result.Set("boundingBox", BoundingBox(planned));
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private static JsonValue ToArray(List<JsonValue> list)
|
|
77
|
+
{
|
|
78
|
+
JsonValue array = JsonValue.NewArray();
|
|
79
|
+
foreach (JsonValue v in list) array.Add(v);
|
|
80
|
+
return array;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private static JsonValue DescribeItem(string kind, JsonValue item, int index)
|
|
84
|
+
{
|
|
85
|
+
JsonValue d = JsonValue.NewObject();
|
|
86
|
+
d.Set("index", index);
|
|
87
|
+
d.Set("kind", kind);
|
|
88
|
+
try
|
|
89
|
+
{
|
|
90
|
+
switch (kind)
|
|
91
|
+
{
|
|
92
|
+
case "line":
|
|
93
|
+
case "rect":
|
|
94
|
+
{
|
|
95
|
+
PointD from = Context.Point(item["from"], "from");
|
|
96
|
+
PointD to = Context.Point(item["to"], "to");
|
|
97
|
+
d.Set("from", Context.PointJson(from));
|
|
98
|
+
d.Set("to", Context.PointJson(to));
|
|
99
|
+
if (Math.Abs(from.X - to.X) < 1e-9 && Math.Abs(from.Y - to.Y) < 1e-9)
|
|
100
|
+
{
|
|
101
|
+
d.Set("valid", false);
|
|
102
|
+
d.Set("problem", "起点和终点重合");
|
|
103
|
+
return d;
|
|
104
|
+
}
|
|
105
|
+
d.Set("width", item["width"].AsDouble(0.2));
|
|
106
|
+
string color = item["color"].AsString(null);
|
|
107
|
+
if (!string.IsNullOrEmpty(color))
|
|
108
|
+
{
|
|
109
|
+
Int16 colorId;
|
|
110
|
+
if (!TryColor(color, out colorId))
|
|
111
|
+
{
|
|
112
|
+
d.Set("valid", false);
|
|
113
|
+
d.Set("problem", "不认识的颜色「" + color + "」,可用 yellow / red / green / cyan / blue / magenta / black / gray,或 1-12 的数字");
|
|
114
|
+
return d;
|
|
115
|
+
}
|
|
116
|
+
d.Set("color", color);
|
|
117
|
+
d.Set("colorId", JsonValue.NewNumber(colorId));
|
|
118
|
+
}
|
|
119
|
+
d.Set("valid", true);
|
|
120
|
+
return d;
|
|
121
|
+
}
|
|
122
|
+
case "text":
|
|
123
|
+
{
|
|
124
|
+
PointD at = Context.Point(item["at"], "at");
|
|
125
|
+
string text = item["text"].AsString(null);
|
|
126
|
+
if (string.IsNullOrEmpty(text))
|
|
127
|
+
{
|
|
128
|
+
d.Set("valid", false);
|
|
129
|
+
d.Set("problem", "文本内容为空");
|
|
130
|
+
return d;
|
|
131
|
+
}
|
|
132
|
+
d.Set("at", Context.PointJson(at));
|
|
133
|
+
d.Set("text", text);
|
|
134
|
+
d.Set("height", item["height"].AsDouble(2.5));
|
|
135
|
+
d.Set("valid", true);
|
|
136
|
+
return d;
|
|
137
|
+
}
|
|
138
|
+
case "polyline":
|
|
139
|
+
{
|
|
140
|
+
JsonValue points = item["points"];
|
|
141
|
+
if (points == null || !points.IsArray || points.Count < 2)
|
|
142
|
+
{
|
|
143
|
+
d.Set("valid", false);
|
|
144
|
+
d.Set("problem", "polyline 至少需要 2 个点");
|
|
145
|
+
return d;
|
|
146
|
+
}
|
|
147
|
+
JsonValue list = JsonValue.NewArray();
|
|
148
|
+
for (int i = 0; i < points.Count; i++)
|
|
149
|
+
list.Add(Context.PointJson(Context.Point(points[i], "points[" + i + "]")));
|
|
150
|
+
d.Set("points", list);
|
|
151
|
+
d.Set("width", item["width"].AsDouble(0.2));
|
|
152
|
+
d.Set("valid", true);
|
|
153
|
+
return d;
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
d.Set("valid", false);
|
|
157
|
+
d.Set("problem", "不认识的图元类型「" + kind + "」,可用:line / rect / text / polyline");
|
|
158
|
+
return d;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (Exception ex)
|
|
162
|
+
{
|
|
163
|
+
d.Set("valid", false);
|
|
164
|
+
d.Set("problem", ex.Message);
|
|
165
|
+
return d;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private static Placement CreateOne(Page page, string kind, JsonValue item)
|
|
170
|
+
{
|
|
171
|
+
switch (kind)
|
|
172
|
+
{
|
|
173
|
+
case "line":
|
|
174
|
+
{
|
|
175
|
+
Line line = new Line();
|
|
176
|
+
line.Create(page, Context.Point(item["from"], "from"), Context.Point(item["to"], "to"));
|
|
177
|
+
double width = item["width"].AsDouble(0.2);
|
|
178
|
+
if (width > 0) line.Width = width;
|
|
179
|
+
ApplyColor(item, line);
|
|
180
|
+
return line;
|
|
181
|
+
}
|
|
182
|
+
case "rect":
|
|
183
|
+
{
|
|
184
|
+
Rectangle rect = new Rectangle();
|
|
185
|
+
rect.Create(page);
|
|
186
|
+
rect.SetArea(Context.Point(item["from"], "from"), Context.Point(item["to"], "to"));
|
|
187
|
+
ApplyColor(item, rect);
|
|
188
|
+
return rect;
|
|
189
|
+
}
|
|
190
|
+
case "text":
|
|
191
|
+
{
|
|
192
|
+
Text text = new Text();
|
|
193
|
+
text.Create(page, item["text"].AsString(""), item["height"].AsDouble(2.5));
|
|
194
|
+
text.Location = Context.Point(item["at"], "at");
|
|
195
|
+
return text;
|
|
196
|
+
}
|
|
197
|
+
case "polyline":
|
|
198
|
+
{
|
|
199
|
+
JsonValue points = item["points"];
|
|
200
|
+
PolyLine poly = new PolyLine();
|
|
201
|
+
poly.Create(page);
|
|
202
|
+
for (int i = 0; i < points.Count; i++)
|
|
203
|
+
{
|
|
204
|
+
PointD p = Context.Point(points[i], "points[" + i + "]");
|
|
205
|
+
poly.SetPointAt(i, ref p);
|
|
206
|
+
}
|
|
207
|
+
return poly;
|
|
208
|
+
}
|
|
209
|
+
default:
|
|
210
|
+
throw new HostException("BAD_ARGUMENT", "不认识的图元类型: " + kind);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/// <summary>
|
|
215
|
+
/// 给图形图元上色。EPLAN 的调色板是固定编号(1 红、2 黄、3 绿、4 青、5 蓝、6 品红、7 黑…),
|
|
216
|
+
/// Pen.ColorId 就是这个编号的索引。不传 color 时保持默认(跟图层走)。
|
|
217
|
+
/// </summary>
|
|
218
|
+
private static void ApplyColor(JsonValue item, Placement graphic)
|
|
219
|
+
{
|
|
220
|
+
string color = item["color"].AsString(null);
|
|
221
|
+
if (string.IsNullOrEmpty(color)) return;
|
|
222
|
+
Int16 colorId;
|
|
223
|
+
if (!TryColor(color, out colorId)) return;
|
|
224
|
+
double width = item["width"].AsDouble(0.2);
|
|
225
|
+
Pen pen = new Pen();
|
|
226
|
+
pen.ColorId = colorId;
|
|
227
|
+
if (width > 0) pen.Width = width;
|
|
228
|
+
Line line = graphic as Line;
|
|
229
|
+
if (line != null) { line.Pen = pen; return; }
|
|
230
|
+
Rectangle rect = graphic as Rectangle;
|
|
231
|
+
if (rect != null) { rect.Pen = pen; return; }
|
|
232
|
+
PolyLine poly = graphic as PolyLine;
|
|
233
|
+
if (poly != null) poly.Pen = pen;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
internal static bool TryColor(string text, out Int16 id)
|
|
237
|
+
{
|
|
238
|
+
id = 0;
|
|
239
|
+
string key = (text ?? string.Empty).Trim().ToLowerInvariant();
|
|
240
|
+
if (key == "red") id = 1;
|
|
241
|
+
else if (key == "yellow") id = 2;
|
|
242
|
+
else if (key == "green") id = 3;
|
|
243
|
+
else if (key == "cyan") id = 4;
|
|
244
|
+
else if (key == "blue") id = 5;
|
|
245
|
+
else if (key == "magenta") id = 6;
|
|
246
|
+
else if (key == "black") id = 7;
|
|
247
|
+
else if (key == "gray" || key == "grey") id = 9;
|
|
248
|
+
else
|
|
249
|
+
{
|
|
250
|
+
Int16 parsed;
|
|
251
|
+
if (Int16.TryParse(key, out parsed) && parsed >= 1 && parsed <= 12) id = parsed;
|
|
252
|
+
else return false;
|
|
253
|
+
}
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private static void Rollback(List<Placement> created)
|
|
258
|
+
{
|
|
259
|
+
for (int i = created.Count - 1; i >= 0; i--)
|
|
260
|
+
{
|
|
261
|
+
try { created[i].Remove(); }
|
|
262
|
+
catch { }
|
|
263
|
+
}
|
|
264
|
+
created.Clear();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private static JsonValue BoundingBox(List<JsonValue> planned)
|
|
268
|
+
{
|
|
269
|
+
double minX = double.MaxValue, minY = double.MaxValue;
|
|
270
|
+
double maxX = double.MinValue, maxY = double.MinValue;
|
|
271
|
+
bool any = false;
|
|
272
|
+
|
|
273
|
+
foreach (JsonValue d in planned)
|
|
274
|
+
{
|
|
275
|
+
if (!d["valid"].AsBool(false)) continue;
|
|
276
|
+
JsonValue from = d["from"];
|
|
277
|
+
JsonValue to = d["to"];
|
|
278
|
+
JsonValue at = d["at"];
|
|
279
|
+
JsonValue points = d["points"];
|
|
280
|
+
if (from != null) { Absorb(from, ref minX, ref minY, ref maxX, ref maxY, ref any); }
|
|
281
|
+
if (to != null) { Absorb(to, ref minX, ref minY, ref maxX, ref maxY, ref any); }
|
|
282
|
+
if (at != null) { Absorb(at, ref minX, ref minY, ref maxX, ref maxY, ref any); }
|
|
283
|
+
if (points != null && points.IsArray)
|
|
284
|
+
{
|
|
285
|
+
for (int i = 0; i < points.Count; i++)
|
|
286
|
+
Absorb(points[i], ref minX, ref minY, ref maxX, ref maxY, ref any);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (!any) return JsonValue.NewNull();
|
|
291
|
+
JsonValue box = JsonValue.NewObject();
|
|
292
|
+
JsonValue min = JsonValue.NewArray();
|
|
293
|
+
min.Add(JsonValue.NewNumber(minX));
|
|
294
|
+
min.Add(JsonValue.NewNumber(minY));
|
|
295
|
+
JsonValue max = JsonValue.NewArray();
|
|
296
|
+
max.Add(JsonValue.NewNumber(maxX));
|
|
297
|
+
max.Add(JsonValue.NewNumber(maxY));
|
|
298
|
+
box.Set("min", min);
|
|
299
|
+
box.Set("max", max);
|
|
300
|
+
return box;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private static void Absorb(JsonValue point, ref double minX, ref double minY, ref double maxX, ref double maxY, ref bool any)
|
|
304
|
+
{
|
|
305
|
+
if (point == null || !point.IsArray || point.Count < 2) return;
|
|
306
|
+
double x = point[0].AsDouble(double.NaN);
|
|
307
|
+
double y = point[1].AsDouble(double.NaN);
|
|
308
|
+
if (double.IsNaN(x) || double.IsNaN(y)) return;
|
|
309
|
+
if (x < minX) minX = x;
|
|
310
|
+
if (y < minY) minY = y;
|
|
311
|
+
if (x > maxX) maxX = x;
|
|
312
|
+
if (y > maxY) maxY = y;
|
|
313
|
+
any = true;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// EPLAN 进程内的宿主:所有真正的绘图逻辑都在这里。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:EPLAN 的脚本编译器(EplanEplApiScriptLogu.erx)把引用集写死了,
|
|
4
|
+
// 里面没有 Eplan.EplApi.DataModelu.dll / HEServicesu.dll,所以 .cs 脚本
|
|
5
|
+
// 连 "DataModel" 这个命名空间都看不到,画不了图。
|
|
6
|
+
// 这段代码是我们自己用 csc.exe 编译成独立 DLL、再由引导脚本用反射加载进来的,
|
|
7
|
+
// 因此可以自由引用 DataModel / HEServices。
|
|
8
|
+
//
|
|
9
|
+
// 生命周期:宿主实例在 EPLAN 进程内缓存,跨次调用复用(缓存 SymbolLibrary 等)。
|
|
10
|
+
using System;
|
|
11
|
+
using System.Collections.Generic;
|
|
12
|
+
using System.Globalization;
|
|
13
|
+
using System.IO;
|
|
14
|
+
using System.Text;
|
|
15
|
+
using Eplan.EplApi.Base;
|
|
16
|
+
using Eplan.EplApi.DataModel;
|
|
17
|
+
using Eplan.EplApi.DataModel.Graphics;
|
|
18
|
+
using Eplan.EplApi.DataModel.MasterData;
|
|
19
|
+
using Eplan.EplApi.HEServices;
|
|
20
|
+
using EplanMcp;
|
|
21
|
+
|
|
22
|
+
namespace EplanMcpHost
|
|
23
|
+
{
|
|
24
|
+
/// <summary>
|
|
25
|
+
/// 由引导脚本反射调用的唯一入口。
|
|
26
|
+
/// 参数 payloadJson 形如 {"op":"status","args":{...}},
|
|
27
|
+
/// 返回 {"ok":true,"result":{...}} 或 {"ok":false,"error":{"code","message","detail"}}。
|
|
28
|
+
/// </summary>
|
|
29
|
+
public class Entry
|
|
30
|
+
{
|
|
31
|
+
private static readonly object Gate = new object();
|
|
32
|
+
|
|
33
|
+
public string Run(string payloadJson)
|
|
34
|
+
{
|
|
35
|
+
// EPLAN 的动作可能并发进来,统一串行化,避免两个线程同时改同一个项目。
|
|
36
|
+
lock (Gate)
|
|
37
|
+
{
|
|
38
|
+
JsonValue response = JsonValue.NewObject();
|
|
39
|
+
try
|
|
40
|
+
{
|
|
41
|
+
JsonValue request = Json.Parse(payloadJson);
|
|
42
|
+
string op = request["op"].AsString("");
|
|
43
|
+
JsonValue args = request["args"];
|
|
44
|
+
if (args == null || !args.IsObject) args = JsonValue.NewObject();
|
|
45
|
+
|
|
46
|
+
// 所有 DataModel 访问都必须在一个 LockingStep 里,否则
|
|
47
|
+
// ProjectManager.CurrentProject 直接抛 S063110「不能生成 LockingStep 类别的对象」。
|
|
48
|
+
// 实测 ExecuteAction 跑在 EPLAN 主线程(threadId=1, STA,
|
|
49
|
+
// DispatcherSynchronizationContext),所以这里新建 LockingStep 是合法的。
|
|
50
|
+
//
|
|
51
|
+
// LockingStep 只有 ctor 和 Dispose(),没有 Rollback/Commit:
|
|
52
|
+
// Dispose 即提交,退出 using 就落盘。需要撤销的操作由各工具自己做
|
|
53
|
+
// (例如 DrawTool 失败时会 SetUndoStep/删除刚建的图元)。
|
|
54
|
+
// 所有 DataModel 访问都必须在一个 LockingStep 里。
|
|
55
|
+
// 连 project_open 也不例外:ProjectManager.OpenProject 内部会调
|
|
56
|
+
// lockHandleIfNeccesary,没有 LockingStep 直接抛
|
|
57
|
+
// S063110「不能生成 LockingStep 类别的对象」(实测踩过)。
|
|
58
|
+
JsonValue result;
|
|
59
|
+
using (LockingStep step = new LockingStep())
|
|
60
|
+
{
|
|
61
|
+
result = Dispatcher.Execute(op, args);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
response.Set("ok", true);
|
|
65
|
+
response.Set("result", result ?? JsonValue.NewObject());
|
|
66
|
+
}
|
|
67
|
+
catch (HostException ex)
|
|
68
|
+
{
|
|
69
|
+
response.Set("ok", false);
|
|
70
|
+
JsonValue err = JsonValue.NewObject();
|
|
71
|
+
err.Set("code", ex.Code);
|
|
72
|
+
err.Set("message", ex.Message);
|
|
73
|
+
if (ex.Detail != null) err.Set("detail", ex.Detail);
|
|
74
|
+
response.Set("error", err);
|
|
75
|
+
}
|
|
76
|
+
catch (Exception ex)
|
|
77
|
+
{
|
|
78
|
+
response.Set("ok", false);
|
|
79
|
+
JsonValue err = JsonValue.NewObject();
|
|
80
|
+
err.Set("code", "EPLAN_HOST_ERROR");
|
|
81
|
+
err.Set("message", Describe(ex));
|
|
82
|
+
err.Set("detail", ex.ToString());
|
|
83
|
+
response.Set("error", err);
|
|
84
|
+
}
|
|
85
|
+
return Json.Write(response);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private static string Describe(Exception ex)
|
|
90
|
+
{
|
|
91
|
+
Exception inner = ex;
|
|
92
|
+
List<string> parts = new List<string>();
|
|
93
|
+
while (inner != null)
|
|
94
|
+
{
|
|
95
|
+
string text = inner.GetType().Name + ": " + inner.Message;
|
|
96
|
+
if (!parts.Contains(text)) parts.Add(text);
|
|
97
|
+
inner = inner.InnerException;
|
|
98
|
+
}
|
|
99
|
+
return string.Join(" <- ", parts.ToArray());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
internal class HostException : Exception
|
|
104
|
+
{
|
|
105
|
+
public string Code { get; private set; }
|
|
106
|
+
public string Detail { get; private set; }
|
|
107
|
+
|
|
108
|
+
public HostException(string code, string message) : base(message) { Code = code; }
|
|
109
|
+
public HostException(string code, string message, string detail) : base(message) { Code = code; Detail = detail; }
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// export:把项目或指定页导出成 PNG / PDF / DXF。
|
|
2
|
+
// PNG 用于模型自己 read_image 检查画得对不对,PDF/DXF 用于交付。
|
|
3
|
+
using System;
|
|
4
|
+
using System.Collections;
|
|
5
|
+
using System.Collections.Generic;
|
|
6
|
+
using System.IO;
|
|
7
|
+
using Eplan.EplApi.DataModel;
|
|
8
|
+
using Eplan.EplApi.HEServices;
|
|
9
|
+
using EplanMcp;
|
|
10
|
+
|
|
11
|
+
namespace EplanMcpHost
|
|
12
|
+
{
|
|
13
|
+
internal static class ExportTool
|
|
14
|
+
{
|
|
15
|
+
public static JsonValue Run(JsonValue args)
|
|
16
|
+
{
|
|
17
|
+
string kind = args["kind"].AsString("png").ToLowerInvariant();
|
|
18
|
+
JsonValue pageNames = args["pages"];
|
|
19
|
+
string outDir = args["outDir"].AsString(null);
|
|
20
|
+
string fileName = args["fileName"].AsString(null);
|
|
21
|
+
|
|
22
|
+
string projectName = args["project"].AsString(null);
|
|
23
|
+
Project project = Context.Resolve(projectName);
|
|
24
|
+
if (outDir == null)
|
|
25
|
+
outDir = Path.Combine(project.ProjectDirectoryPath, "_eplan_export");
|
|
26
|
+
if (!Directory.Exists(outDir)) Directory.CreateDirectory(outDir);
|
|
27
|
+
|
|
28
|
+
string stamp = DateTime.Now.ToString("yyyyMMdd_HHmmss", System.Globalization.CultureInfo.InvariantCulture);
|
|
29
|
+
if (string.IsNullOrEmpty(fileName)) fileName = project.ProjectName + "_" + stamp;
|
|
30
|
+
|
|
31
|
+
List<Page> pages = new List<Page>();
|
|
32
|
+
if (pageNames != null && pageNames.IsArray && pageNames.Count > 0)
|
|
33
|
+
{
|
|
34
|
+
for (int i = 0; i < pageNames.Count; i++)
|
|
35
|
+
pages.Add(Context.FindPage(pageNames[i].AsString(null), projectName));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
JsonValue result = JsonValue.NewObject();
|
|
39
|
+
result.Set("kind", kind);
|
|
40
|
+
result.Set("outDir", outDir);
|
|
41
|
+
result.Set("project", project.ProjectName);
|
|
42
|
+
|
|
43
|
+
JsonValue files = JsonValue.NewArray();
|
|
44
|
+
Export service = new Export();
|
|
45
|
+
try
|
|
46
|
+
{
|
|
47
|
+
switch (kind)
|
|
48
|
+
{
|
|
49
|
+
case "png":
|
|
50
|
+
files = ExportPng(service, project, pages, outDir, fileName,
|
|
51
|
+
args["theme"].AsString("dark").ToLowerInvariant());
|
|
52
|
+
break;
|
|
53
|
+
case "pdf":
|
|
54
|
+
files = ExportPdf(service, project, pages, outDir, fileName);
|
|
55
|
+
break;
|
|
56
|
+
case "dxf":
|
|
57
|
+
files = ExportDxf(service, project, pages, outDir, fileName);
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
throw new HostException("BAD_ARGUMENT",
|
|
61
|
+
"不支持的导出格式「" + kind + "」。可用:png / pdf / dxf。");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
finally
|
|
65
|
+
{
|
|
66
|
+
try { service.Dispose(); } catch { }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
result.Set("files", files);
|
|
70
|
+
result.Set("count", files.Count);
|
|
71
|
+
if (files.Count == 0)
|
|
72
|
+
throw new HostException("EXPORT_EMPTY",
|
|
73
|
+
"导出没有产出任何文件(格式 " + kind + ")。检查页名是否正确、项目是否已保存。");
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// <summary>按 theme 重新上色;失败不影响导出本身。</summary>
|
|
78
|
+
private static void ApplyTheme(string path, string theme)
|
|
79
|
+
{
|
|
80
|
+
if (theme != "dark" || string.IsNullOrEmpty(path) || !File.Exists(path)) return;
|
|
81
|
+
try { DarkScheme.Apply(path); } catch { }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private static JsonValue ExportPng(Export service, Project project, List<Page> pages, string outDir, string fileName, string theme)
|
|
85
|
+
{
|
|
86
|
+
JsonValue files = JsonValue.NewArray();
|
|
87
|
+
|
|
88
|
+
if (pages.Count == 0)
|
|
89
|
+
{
|
|
90
|
+
// 整个项目:GraphicProjectEx 每页一张,返回文件列表。
|
|
91
|
+
string[] produced = service.GraphicProjectEx(
|
|
92
|
+
project, outDir, Export.Format.PNG, 24, 1600, Export.Compression.None, false);
|
|
93
|
+
if (produced != null)
|
|
94
|
+
{
|
|
95
|
+
foreach (string path in produced)
|
|
96
|
+
{
|
|
97
|
+
if (string.IsNullOrEmpty(path)) continue;
|
|
98
|
+
ApplyTheme(path, theme);
|
|
99
|
+
files.Add(Describe(path));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return files;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 坑:GraphicPageEx 的第二个参数是**输出目录**,不是文件路径。
|
|
106
|
+
// 传 "xxx.png" 时 EPLAN 会把 xxx.png 建成一个目录,再把 <页名>.png 塞进去。
|
|
107
|
+
// 所以这里给目录,然后拿返回值 —— 它才是真正的产物路径。
|
|
108
|
+
Directory.CreateDirectory(outDir);
|
|
109
|
+
for (int i = 0; i < pages.Count; i++)
|
|
110
|
+
{
|
|
111
|
+
string produced = service.GraphicPageEx(
|
|
112
|
+
pages[i], outDir, Export.Format.PNG, 24, 1600, Export.Compression.None, false);
|
|
113
|
+
|
|
114
|
+
if (!string.IsNullOrEmpty(produced) && File.Exists(produced))
|
|
115
|
+
{
|
|
116
|
+
ApplyTheme(produced, theme);
|
|
117
|
+
files.Add(Describe(produced));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 返回值不可用时按 EPLAN 的命名约定回查一遍:页名里的 = + / 会变成下划线。
|
|
122
|
+
string expected = Path.Combine(outDir, Sanitize(pages[i].Name) + ".png");
|
|
123
|
+
if (File.Exists(expected))
|
|
124
|
+
{
|
|
125
|
+
ApplyTheme(expected, theme);
|
|
126
|
+
files.Add(Describe(expected));
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
{
|
|
130
|
+
files.Add(Describe(produced ?? expected));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return files;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private static JsonValue ExportPdf(Export service, Project project, List<Page> pages, string outDir, string fileName)
|
|
137
|
+
{
|
|
138
|
+
JsonValue files = JsonValue.NewArray();
|
|
139
|
+
string target = Path.Combine(outDir, fileName + ".pdf");
|
|
140
|
+
|
|
141
|
+
if (pages.Count == 0)
|
|
142
|
+
{
|
|
143
|
+
service.PdfProject(project, string.Empty, target,
|
|
144
|
+
Export.DegreeOfColor.Color, false, string.Empty, true);
|
|
145
|
+
}
|
|
146
|
+
else
|
|
147
|
+
{
|
|
148
|
+
// PdfPages 要 ArrayList,里面是 Page 对象。
|
|
149
|
+
ArrayList list = new ArrayList();
|
|
150
|
+
foreach (Page page in pages) list.Add(page);
|
|
151
|
+
service.PdfPages(list, string.Empty, target,
|
|
152
|
+
Export.DegreeOfColor.Color, false, string.Empty, true);
|
|
153
|
+
}
|
|
154
|
+
files.Add(Describe(target));
|
|
155
|
+
return files;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private static JsonValue ExportDxf(Export service, Project project, List<Page> pages, string outDir, string fileName)
|
|
159
|
+
{
|
|
160
|
+
JsonValue files = JsonValue.NewArray();
|
|
161
|
+
|
|
162
|
+
if (pages.Count == 0)
|
|
163
|
+
{
|
|
164
|
+
service.DxfDwgProjectToDisk(project, string.Empty, outDir, string.Empty);
|
|
165
|
+
// DXF 导出没有返回值,扫描目录里最新的 dxf 文件。
|
|
166
|
+
string[] produced = Directory.GetFiles(outDir, "*.dxf");
|
|
167
|
+
foreach (string path in produced) files.Add(Describe(path));
|
|
168
|
+
return files;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for (int i = 0; i < pages.Count; i++)
|
|
172
|
+
{
|
|
173
|
+
service.DxfDwgPageToDisk(
|
|
174
|
+
pages[i], string.Empty, outDir, fileName + "_" + Sanitize(pages[i].Name), string.Empty);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 同样按命名约定回查。
|
|
178
|
+
for (int i = 0; i < pages.Count; i++)
|
|
179
|
+
{
|
|
180
|
+
string expected = Path.Combine(outDir, fileName + "_" + Sanitize(pages[i].Name) + ".dxf");
|
|
181
|
+
if (File.Exists(expected)) files.Add(Describe(expected));
|
|
182
|
+
}
|
|
183
|
+
return files;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private static string Sanitize(string name)
|
|
187
|
+
{
|
|
188
|
+
if (string.IsNullOrEmpty(name)) return "page";
|
|
189
|
+
char[] bad = Path.GetInvalidFileNameChars();
|
|
190
|
+
System.Text.StringBuilder sb = new System.Text.StringBuilder(name.Length);
|
|
191
|
+
foreach (char c in name)
|
|
192
|
+
{
|
|
193
|
+
if (Array.IndexOf(bad, c) >= 0 || c == '=' || c == '+' || c == '/' || c == '\\') sb.Append('_');
|
|
194
|
+
else sb.Append(c);
|
|
195
|
+
}
|
|
196
|
+
return sb.ToString().Trim('_');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private static JsonValue Describe(string path)
|
|
200
|
+
{
|
|
201
|
+
JsonValue item = JsonValue.NewObject();
|
|
202
|
+
item.Set("path", path);
|
|
203
|
+
try
|
|
204
|
+
{
|
|
205
|
+
FileInfo info = new FileInfo(path);
|
|
206
|
+
item.Set("exists", info.Exists);
|
|
207
|
+
if (info.Exists) item.Set("bytes", info.Length);
|
|
208
|
+
}
|
|
209
|
+
catch { }
|
|
210
|
+
return item;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|