dsh-eplan 0.0.0-stage → 0.14.0
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,521 @@
|
|
|
1
|
+
// page_create:新建一页。
|
|
2
|
+
//
|
|
3
|
+
// EPLAN 的页名不是自由字符串,而是结构标识 + 计数器:
|
|
4
|
+
// "=EB3+ET1/1"
|
|
5
|
+
// └ =高层功能 └ +安装位置 └ /页号
|
|
6
|
+
// 所以要拆开写进 PagePropertyList,而不是直接赋 Name。
|
|
7
|
+
using System;
|
|
8
|
+
using System.Collections.Generic;
|
|
9
|
+
using System.Globalization;
|
|
10
|
+
using System.Text.RegularExpressions;
|
|
11
|
+
using Eplan.EplApi.DataModel;
|
|
12
|
+
using EplanMcp;
|
|
13
|
+
|
|
14
|
+
namespace EplanMcpHost
|
|
15
|
+
{
|
|
16
|
+
internal static class PageCreateTool
|
|
17
|
+
{
|
|
18
|
+
// =EB3+ET1/1 ; 各段都可省略
|
|
19
|
+
private static readonly Regex PageNamePattern = new Regex(
|
|
20
|
+
@"^\s*(?:=(?<plant>[^+/]*))?(?:\+(?<location>[^+/]*))?/(?<counter>[0-9]+)?\s*$",
|
|
21
|
+
RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
|
22
|
+
|
|
23
|
+
public static JsonValue Run(JsonValue args)
|
|
24
|
+
{
|
|
25
|
+
string name = args["name"].AsString(args["page"].AsString(null));
|
|
26
|
+
string typeName = args["type"].AsString("Schematic");
|
|
27
|
+
bool dryRun = args["dryRun"].AsBool(false);
|
|
28
|
+
|
|
29
|
+
if (string.IsNullOrEmpty(name))
|
|
30
|
+
throw new HostException("BAD_ARGUMENT", "缺少页名(name),例如 \"=EB3+ET1/1\"。");
|
|
31
|
+
|
|
32
|
+
Match match = PageNamePattern.Match(name);
|
|
33
|
+
if (!match.Success)
|
|
34
|
+
throw new HostException("BAD_PAGE_NAME",
|
|
35
|
+
"页名格式不对:「" + name + "」。\n" +
|
|
36
|
+
"正确的写法是 =高层功能+安装位置/页号,例如 \"=EB3+ET1/1\"、" +
|
|
37
|
+
"\"=EB3/5\" 或 \"+ET1/3\"。");
|
|
38
|
+
|
|
39
|
+
DocumentTypeManager.DocumentType docType = ParseDocumentType(typeName, name);
|
|
40
|
+
Project project = Context.Resolve(args["project"].AsString(null));
|
|
41
|
+
|
|
42
|
+
// 已经存在就报错,不要悄悄覆盖;append=true 时接着用已有的页。
|
|
43
|
+
bool append = args["append"].AsBool(false);
|
|
44
|
+
Page existingPage = null;
|
|
45
|
+
foreach (Page existing in project.Pages)
|
|
46
|
+
{
|
|
47
|
+
if (string.Equals(existing.Name, name, StringComparison.OrdinalIgnoreCase))
|
|
48
|
+
{
|
|
49
|
+
existingPage = existing;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (existingPage != null && !append)
|
|
54
|
+
throw new HostException("PAGE_EXISTS", "页「" + name + "」已经存在,换个页号,或传 append=true 接着往上画。");
|
|
55
|
+
|
|
56
|
+
PagePropertyList parts = new PagePropertyList();
|
|
57
|
+
string plant = match.Groups["plant"].Success ? match.Groups["plant"].Value : null;
|
|
58
|
+
string location = match.Groups["location"].Success ? match.Groups["location"].Value : null;
|
|
59
|
+
string counterText = match.Groups["counter"].Success ? match.Groups["counter"].Value : null;
|
|
60
|
+
int counter = 1;
|
|
61
|
+
if (counterText != null) int.TryParse(counterText, NumberStyles.Integer, CultureInfo.InvariantCulture, out counter);
|
|
62
|
+
|
|
63
|
+
if (!string.IsNullOrEmpty(plant)) parts.DESIGNATION_PRODUCT = plant;
|
|
64
|
+
if (!string.IsNullOrEmpty(location)) parts.DESIGNATION_PLACEOFINSTALLATION = location;
|
|
65
|
+
parts.PAGE_COUNTER = counter;
|
|
66
|
+
|
|
67
|
+
JsonValue result = JsonValue.NewObject();
|
|
68
|
+
result.Set("requestedName", name);
|
|
69
|
+
result.Set("plant", plant);
|
|
70
|
+
result.Set("location", location);
|
|
71
|
+
result.Set("counter", counter);
|
|
72
|
+
result.Set("type", docType.ToString());
|
|
73
|
+
result.Set("project", project.ProjectName);
|
|
74
|
+
result.Set("dryRun", dryRun);
|
|
75
|
+
|
|
76
|
+
// dryRun 也要把「打算摆哪些东西」一并报出来,否则用户没法审。
|
|
77
|
+
JsonValue nodesArg = args["nodes"];
|
|
78
|
+
JsonValue textsArg = args["texts"];
|
|
79
|
+
int nodeCount = (nodesArg != null && nodesArg.IsArray) ? nodesArg.Count : 0;
|
|
80
|
+
int textCount = (textsArg != null && textsArg.IsArray) ? textsArg.Count : 0;
|
|
81
|
+
|
|
82
|
+
if (dryRun)
|
|
83
|
+
{
|
|
84
|
+
result.Set("created", false);
|
|
85
|
+
result.Set("wouldCreate", true);
|
|
86
|
+
result.Set("wouldPlace", JsonValue.NewNumber(nodeCount));
|
|
87
|
+
result.Set("wouldWrite", JsonValue.NewNumber(textCount));
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
Page page;
|
|
93
|
+
bool created = existingPage == null;
|
|
94
|
+
if (created)
|
|
95
|
+
{
|
|
96
|
+
page = new Page();
|
|
97
|
+
page.Create(project, docType, parts);
|
|
98
|
+
|
|
99
|
+
// 实测:只把结构属性写进 PagePropertyList,EPLAN 不一定会拿来拼页名
|
|
100
|
+
// (创建出来的页可能是 "/1" 而不是 "=MCP/1")。Page.Name 是可写的,
|
|
101
|
+
// 创建后按用户给的完整名字改一次,保证落地的名字就是他要的。
|
|
102
|
+
if (!string.Equals(page.Name, name, StringComparison.Ordinal))
|
|
103
|
+
{
|
|
104
|
+
try
|
|
105
|
+
{
|
|
106
|
+
page.Name = name;
|
|
107
|
+
}
|
|
108
|
+
catch (Exception ex)
|
|
109
|
+
{
|
|
110
|
+
result.Set("renameWarning",
|
|
111
|
+
"页已创建,但改成「" + name + "」失败:" + ex.Message +
|
|
112
|
+
"。当前页名是「" + page.Name + "」。");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
{
|
|
118
|
+
page = existingPage;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
result.Set("created", created);
|
|
122
|
+
|
|
123
|
+
// ---------------------------------------------------------------
|
|
124
|
+
// 一次性把整页摆完。
|
|
125
|
+
//
|
|
126
|
+
// 为什么要这样:单个 symbol_insert 一次只放一个符号,画一页要点十几次工具,
|
|
127
|
+
// 每一次都是一轮模型往返(实测一次调用 EPLAN 侧只要 130 ms,而模型往返要几秒)。
|
|
128
|
+
// 真正的成本不是机器,是往返次数。所以这里允许一次调用把节点和文字全给齐。
|
|
129
|
+
//
|
|
130
|
+
// 一个节点失败不影响其它节点:失败项收集到 nodesFailed 里统一报出来。
|
|
131
|
+
// 这样一次调用能尽量多出活,而不是第一个坏符号就把整页废掉。
|
|
132
|
+
// ---------------------------------------------------------------
|
|
133
|
+
// 栅格吸附:EPLAN 靠栅格对齐引脚,坐标差 0.5 mm 红线就连不出来。
|
|
134
|
+
// 与其让调用方每次自己算 4 的倍数,不如这里统一吸,并把改过的坐标报回去 ——
|
|
135
|
+
// 报回去很重要,否则调用方以为自己给的就是最终位置。
|
|
136
|
+
double grid = args["grid"].AsDouble(4.0);
|
|
137
|
+
if (grid <= 0) grid = 4.0;
|
|
138
|
+
JsonValue layout = args["layout"];
|
|
139
|
+
JsonValue snapped = JsonValue.NewArray();
|
|
140
|
+
|
|
141
|
+
if (nodeCount > 0)
|
|
142
|
+
{
|
|
143
|
+
JsonValue placedNodes = JsonValue.NewArray();
|
|
144
|
+
JsonValue failedNodes = JsonValue.NewArray();
|
|
145
|
+
for (int i = 0; i < nodeCount; i++)
|
|
146
|
+
{
|
|
147
|
+
JsonValue node = nodesArg[i];
|
|
148
|
+
JsonValue nodeArgs = JsonValue.NewObject();
|
|
149
|
+
if (node != null && node.IsObject)
|
|
150
|
+
{
|
|
151
|
+
foreach (string key in node.Keys) nodeArgs.Set(key, node[key]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 先把 "列/行" 引用解析成坐标,后面的吸附逻辑就能统一处理。
|
|
155
|
+
JsonValue rawAt = nodeArgs["at"];
|
|
156
|
+
if (rawAt != null && rawAt.Kind == JsonKind.String)
|
|
157
|
+
{
|
|
158
|
+
double lx, ly;
|
|
159
|
+
string layoutError;
|
|
160
|
+
if (TryResolveLayoutPoint(rawAt, layout, out lx, out ly, out layoutError))
|
|
161
|
+
{
|
|
162
|
+
JsonValue resolved = JsonValue.NewArray();
|
|
163
|
+
resolved.Add(JsonValue.NewNumber(lx));
|
|
164
|
+
resolved.Add(JsonValue.NewNumber(ly));
|
|
165
|
+
nodeArgs.Set("at", resolved);
|
|
166
|
+
rawAt = resolved;
|
|
167
|
+
}
|
|
168
|
+
else
|
|
169
|
+
{
|
|
170
|
+
throw new HostException("BAD_LAYOUT", layoutError, "node #" + i);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (rawAt != null && rawAt.IsArray && rawAt.Count >= 2)
|
|
175
|
+
{
|
|
176
|
+
double rx = rawAt[0].AsDouble(double.NaN);
|
|
177
|
+
double ry = rawAt[1].AsDouble(double.NaN);
|
|
178
|
+
if (!double.IsNaN(rx) && !double.IsNaN(ry))
|
|
179
|
+
{
|
|
180
|
+
double sx = Math.Round(rx / grid) * grid;
|
|
181
|
+
double sy = Math.Round(ry / grid) * grid;
|
|
182
|
+
if (Math.Abs(sx - rx) > 1e-9 || Math.Abs(sy - ry) > 1e-9)
|
|
183
|
+
{
|
|
184
|
+
JsonValue fixedPoint = JsonValue.NewArray();
|
|
185
|
+
fixedPoint.Add(JsonValue.NewNumber(sx));
|
|
186
|
+
fixedPoint.Add(JsonValue.NewNumber(sy));
|
|
187
|
+
nodeArgs.Set("at", fixedPoint);
|
|
188
|
+
|
|
189
|
+
JsonValue entry = JsonValue.NewObject();
|
|
190
|
+
entry.Set("index", JsonValue.NewNumber(i));
|
|
191
|
+
entry.Set("symbol", nodeArgs["symbol"]);
|
|
192
|
+
entry.Set("from", rawAt);
|
|
193
|
+
entry.Set("to", fixedPoint);
|
|
194
|
+
snapped.Add(entry);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
nodeArgs.Set("page", page.Name);
|
|
199
|
+
nodeArgs.Set("project", project.ProjectName);
|
|
200
|
+
if (nodeArgs["library"].AsString(null) == null)
|
|
201
|
+
nodeArgs.Set("library", "GB_symbol");
|
|
202
|
+
try
|
|
203
|
+
{
|
|
204
|
+
JsonValue one = SymbolInsertTool.Run(nodeArgs);
|
|
205
|
+
JsonValue brief = JsonValue.NewObject();
|
|
206
|
+
brief.Set("index", JsonValue.NewNumber(i));
|
|
207
|
+
brief.Set("symbol", one["symbol"]);
|
|
208
|
+
brief.Set("at", one["at"]);
|
|
209
|
+
brief.Set("deviceTag", one["deviceTag"]);
|
|
210
|
+
placedNodes.Add(brief);
|
|
211
|
+
}
|
|
212
|
+
catch (Exception ex)
|
|
213
|
+
{
|
|
214
|
+
JsonValue failure = JsonValue.NewObject();
|
|
215
|
+
failure.Set("index", JsonValue.NewNumber(i));
|
|
216
|
+
failure.Set("symbol", node == null ? null : node["symbol"]);
|
|
217
|
+
failure.Set("at", node == null ? null : node["at"]);
|
|
218
|
+
failure.Set("error", DescribeError(ex));
|
|
219
|
+
failedNodes.Add(failure);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
result.Set("nodesPlaced", placedNodes);
|
|
223
|
+
result.Set("nodesFailed", failedNodes);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (snapped.Count > 0) result.Set("snapped", snapped);
|
|
227
|
+
|
|
228
|
+
if (textCount > 0)
|
|
229
|
+
{
|
|
230
|
+
JsonValue drawArgs = JsonValue.NewObject();
|
|
231
|
+
drawArgs.Set("page", page.Name);
|
|
232
|
+
drawArgs.Set("project", project.ProjectName);
|
|
233
|
+
JsonValue items = JsonValue.NewArray();
|
|
234
|
+
for (int i = 0; i < textCount; i++)
|
|
235
|
+
{
|
|
236
|
+
JsonValue text = textsArg[i];
|
|
237
|
+
JsonValue item = JsonValue.NewObject();
|
|
238
|
+
item.Set("kind", "text");
|
|
239
|
+
if (text != null && text.IsObject)
|
|
240
|
+
{
|
|
241
|
+
foreach (string key in text.Keys) item.Set(key, text[key]);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
JsonValue textAt = item["at"];
|
|
245
|
+
if (textAt != null && textAt.Kind == JsonKind.String)
|
|
246
|
+
{
|
|
247
|
+
double lx, ly;
|
|
248
|
+
string layoutError;
|
|
249
|
+
if (!TryResolveLayoutPoint(textAt, layout, out lx, out ly, out layoutError))
|
|
250
|
+
throw new HostException("BAD_LAYOUT", layoutError, "text #" + i);
|
|
251
|
+
JsonValue resolved = JsonValue.NewArray();
|
|
252
|
+
resolved.Add(JsonValue.NewNumber(lx));
|
|
253
|
+
resolved.Add(JsonValue.NewNumber(ly));
|
|
254
|
+
item.Set("at", resolved);
|
|
255
|
+
}
|
|
256
|
+
items.Add(item);
|
|
257
|
+
}
|
|
258
|
+
drawArgs.Set("items", items);
|
|
259
|
+
try
|
|
260
|
+
{
|
|
261
|
+
JsonValue drawn = DrawTool.Run(drawArgs);
|
|
262
|
+
result.Set("textsWritten", drawn["created"]);
|
|
263
|
+
}
|
|
264
|
+
catch (Exception ex)
|
|
265
|
+
{
|
|
266
|
+
result.Set("textsFailed", DescribeError(ex));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
result.Set("page", Describe(page));
|
|
271
|
+
|
|
272
|
+
// 记一笔,好在事后撤回。往已有页追加(append=true)时不能记「删掉这页」——
|
|
273
|
+
// 那会把用户本来就有的页连同内容一起删掉,比不改还糟。
|
|
274
|
+
if (args["append"].AsBool(false))
|
|
275
|
+
{
|
|
276
|
+
OpJournal.Append("page_create", page.Project.ProjectName, page.Name, null, false,
|
|
277
|
+
"这是往已有页上追加内容,回退会把用户原有的页一起删掉,所以不记录回退方式。");
|
|
278
|
+
}
|
|
279
|
+
else
|
|
280
|
+
{
|
|
281
|
+
JsonValue undo = JsonValue.NewObject();
|
|
282
|
+
undo.Set("kind", "delete_page");
|
|
283
|
+
undo.Set("page", page.Name);
|
|
284
|
+
OpJournal.Append("page_create", page.Project.ProjectName, page.Name, undo, true, null);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return result;
|
|
288
|
+
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
// ---------------------------------------------------------------
|
|
294
|
+
// 声明式布局:把「算坐标」从调用方手里拿走。
|
|
295
|
+
//
|
|
296
|
+
// 手写绝对坐标是最容易出错的一步:改一个器件的 y,整列都要跟着改;
|
|
297
|
+
// 算错 4 mm 红线就连不出来,而且错了很难看出来。
|
|
298
|
+
// 所以允许 at 写成 "列/行" 的引用:
|
|
299
|
+
//
|
|
300
|
+
// "layout": { "columns": [60, 100, 140], "rows": { "in": 264, "bus": 180 } }
|
|
301
|
+
// "at": "0/bus" → (columns[0], rows["bus"]) = (60, 180)
|
|
302
|
+
// "at": "2/1" → (columns[2], rows[1]) = (140, rows[1])
|
|
303
|
+
//
|
|
304
|
+
// 列只能按下标取(列本身没有名字),行既可以是对象(按名)也可以是数组(按下标)。
|
|
305
|
+
// 写错了不静默兜底,抛出来 —— 布局写错却画出个东西,比报错难查得多。
|
|
306
|
+
// ---------------------------------------------------------------
|
|
307
|
+
private static bool TryResolveLayoutPoint(JsonValue expr, JsonValue layout, out double x, out double y, out string error)
|
|
308
|
+
{
|
|
309
|
+
x = 0; y = 0; error = null;
|
|
310
|
+
if (expr == null || !(expr.Kind == JsonKind.String)) return false;
|
|
311
|
+
if (layout == null || !layout.IsObject)
|
|
312
|
+
{
|
|
313
|
+
error = "用了 \"" + expr.AsString("") + "\" 这种布局引用,但没给 layout。";
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
string text = expr.AsString("").Trim();
|
|
318
|
+
string[] parts = text.Split('/');
|
|
319
|
+
if (parts.Length != 2)
|
|
320
|
+
{
|
|
321
|
+
error = "布局引用要写成 \"列/行\",例如 \"0/bus\";收到的是 \"" + text + "\"。";
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
JsonValue columns = layout["columns"];
|
|
326
|
+
JsonValue rows = layout["rows"];
|
|
327
|
+
|
|
328
|
+
double colValue;
|
|
329
|
+
if (!ResolveAxis(columns, parts[0], true, out colValue, out error))
|
|
330
|
+
{
|
|
331
|
+
error = "列 \"" + parts[0] + "\":" + error;
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
double rowValue;
|
|
336
|
+
if (!ResolveAxis(rows, parts[1], false, out rowValue, out error))
|
|
337
|
+
{
|
|
338
|
+
error = "行 \"" + parts[1] + "\":" + error;
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
x = colValue;
|
|
343
|
+
y = rowValue;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/// <summary>取一个轴上的值:数组按下标,对象按名字。</summary>
|
|
348
|
+
private static bool ResolveAxis(JsonValue axis, string key, bool indexOnly, out double value, out string error)
|
|
349
|
+
{
|
|
350
|
+
value = 0; error = null;
|
|
351
|
+
if (axis == null || axis.IsNull)
|
|
352
|
+
{
|
|
353
|
+
error = "layout 里没有 " + (indexOnly ? "columns" : "rows") + "。";
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (axis.IsObject)
|
|
358
|
+
{
|
|
359
|
+
if (indexOnly)
|
|
360
|
+
{
|
|
361
|
+
error = "列只能用下标(columns 是数组),不能按名字取。";
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
JsonValue named = axis[key];
|
|
365
|
+
if (named == null || named.IsNull)
|
|
366
|
+
{
|
|
367
|
+
// 把现有名字报出来。只写「没有这个名称」等于让调用方盲猜 ——
|
|
368
|
+
// 而它下一句就得再猜一次,多一次往返。
|
|
369
|
+
System.Text.StringBuilder available = new System.Text.StringBuilder();
|
|
370
|
+
foreach (string name in axis.Keys)
|
|
371
|
+
{
|
|
372
|
+
if (available.Length > 0) available.Append(" / ");
|
|
373
|
+
available.Append(name);
|
|
374
|
+
}
|
|
375
|
+
error = "没有这个名称。可用的是:" + (available.Length == 0 ? "(空)" : available.ToString()) +
|
|
376
|
+
"。也可以把 rows 写成数组来用下标。";
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
value = named.AsDouble(double.NaN);
|
|
380
|
+
if (double.IsNaN(value))
|
|
381
|
+
{
|
|
382
|
+
error = "这个名称对应的不是数字。";
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (axis.IsArray)
|
|
389
|
+
{
|
|
390
|
+
int index;
|
|
391
|
+
if (!int.TryParse(key, out index))
|
|
392
|
+
{
|
|
393
|
+
error = "要按下标取(数组),给的是名字。";
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
if (index < 0 || index >= axis.Count)
|
|
397
|
+
{
|
|
398
|
+
error = "下标越界,只有 " + axis.Count + " 个。";
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
value = axis[index].AsDouble(double.NaN);
|
|
402
|
+
if (double.IsNaN(value))
|
|
403
|
+
{
|
|
404
|
+
error = "这个下标对应的不是数字。";
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
error = "既不是数组也不是对象。";
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/// <summary>把异常整理成给人看的一小段,宿主异常带错误码。</summary>
|
|
415
|
+
private static JsonValue DescribeError(Exception ex)
|
|
416
|
+
{
|
|
417
|
+
JsonValue item = JsonValue.NewObject();
|
|
418
|
+
HostException host = ex as HostException;
|
|
419
|
+
item.Set("code", host != null ? host.Code : ex.GetType().Name);
|
|
420
|
+
item.Set("message", ex.Message);
|
|
421
|
+
return item;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
private static JsonValue Describe(Page page)
|
|
425
|
+
{
|
|
426
|
+
JsonValue item = JsonValue.NewObject();
|
|
427
|
+
item.Set("name", page.Name);
|
|
428
|
+
item.Set("identifyingName", page.IdentifyingName);
|
|
429
|
+
item.Set("type", page.PageTypeName);
|
|
430
|
+
try
|
|
431
|
+
{
|
|
432
|
+
JsonValue size = JsonValue.NewArray();
|
|
433
|
+
size.Add(JsonValue.NewNumber(page.Size.X));
|
|
434
|
+
size.Add(JsonValue.NewNumber(page.Size.Y));
|
|
435
|
+
item.Set("size", size);
|
|
436
|
+
}
|
|
437
|
+
catch { }
|
|
438
|
+
return item;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private static DocumentTypeManager.DocumentType ParseDocumentType(string text, string originalName)
|
|
442
|
+
{
|
|
443
|
+
if (string.IsNullOrEmpty(text)) return DocumentTypeManager.DocumentType.Circuit;
|
|
444
|
+
switch (text.Trim().ToLowerInvariant())
|
|
445
|
+
{
|
|
446
|
+
case "schematic":
|
|
447
|
+
case "circuit":
|
|
448
|
+
case "原理图":
|
|
449
|
+
return DocumentTypeManager.DocumentType.Circuit;
|
|
450
|
+
case "overview":
|
|
451
|
+
case "总览":
|
|
452
|
+
return DocumentTypeManager.DocumentType.Overview;
|
|
453
|
+
case "panel":
|
|
454
|
+
case "panellayout":
|
|
455
|
+
case "布局图":
|
|
456
|
+
return DocumentTypeManager.DocumentType.PanelLayout;
|
|
457
|
+
case "singleline":
|
|
458
|
+
case "circuitsingleline":
|
|
459
|
+
case "单线图":
|
|
460
|
+
return DocumentTypeManager.DocumentType.CircuitSingleLine;
|
|
461
|
+
case "fluid":
|
|
462
|
+
case "circuitfluid":
|
|
463
|
+
case "流体图":
|
|
464
|
+
return DocumentTypeManager.DocumentType.CircuitFluid;
|
|
465
|
+
case "graphics":
|
|
466
|
+
case "图形":
|
|
467
|
+
return DocumentTypeManager.DocumentType.Graphics;
|
|
468
|
+
case "title":
|
|
469
|
+
case "titlepage":
|
|
470
|
+
case "封面":
|
|
471
|
+
return DocumentTypeManager.DocumentType.TitlePage;
|
|
472
|
+
default:
|
|
473
|
+
throw new HostException("BAD_ARGUMENT",
|
|
474
|
+
"不认识的页类型「" + text + "」。可用:schematic / overview / panel / singleline / fluid / graphics / title。");
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/// <summary>
|
|
479
|
+
/// 删一页。用途:清理试画留下的过程页,别让用户的页树里堆一堆垃圾。
|
|
480
|
+
///
|
|
481
|
+
/// 只删整页,不动页里的单个对象;页不存在时直接报 PAGE_NOT_FOUND,不静默成功。
|
|
482
|
+
/// </summary>
|
|
483
|
+
public static JsonValue Delete(JsonValue args)
|
|
484
|
+
{
|
|
485
|
+
string name = args["name"].AsString(args["page"].AsString(null));
|
|
486
|
+
if (string.IsNullOrEmpty(name))
|
|
487
|
+
throw new HostException("BAD_ARGUMENT", "缺少页名(name)。");
|
|
488
|
+
|
|
489
|
+
Page page = Context.FindPage(name, args["project"].AsString(null));
|
|
490
|
+
string projectName = page.Project.ProjectName;
|
|
491
|
+
string pageName = page.Name;
|
|
492
|
+
int functions = page.Functions == null ? 0 : page.Functions.Length;
|
|
493
|
+
|
|
494
|
+
if (args["dryRun"].AsBool(false))
|
|
495
|
+
{
|
|
496
|
+
JsonValue plan = JsonValue.NewObject();
|
|
497
|
+
plan.Set("page", pageName);
|
|
498
|
+
plan.Set("project", projectName);
|
|
499
|
+
plan.Set("wouldRemoveFunctions", JsonValue.NewNumber(functions));
|
|
500
|
+
plan.Set("dryRun", true);
|
|
501
|
+
return plan;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
page.Remove();
|
|
505
|
+
|
|
506
|
+
JsonValue result = JsonValue.NewObject();
|
|
507
|
+
result.Set("page", pageName);
|
|
508
|
+
result.Set("project", projectName);
|
|
509
|
+
result.Set("removedFunctions", JsonValue.NewNumber(functions));
|
|
510
|
+
result.Set("removed", true);
|
|
511
|
+
|
|
512
|
+
// 删页不可回退:删之前没留快照,事后没有任何办法把那页复原。
|
|
513
|
+
// 与其假装能撤,不如在历史里写清楚,让调用方知道这次操作是单向的。
|
|
514
|
+
OpJournal.Append("page_delete", projectName, pageName, null, false,
|
|
515
|
+
"删除页面没有留快照,撤回不了。要保留内容请事先用 export 导出该页。");
|
|
516
|
+
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
}
|
|
521
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// page_open:把某一页在 EPLAN 界面里打开。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要:
|
|
4
|
+
// 工具在后台改图、渲染,用户却看不到——EPLAN 里显示的还停在别的页。
|
|
5
|
+
// "画好了" 和 "看到了" 是两件事,这一步把后者补上。
|
|
6
|
+
//
|
|
7
|
+
// 动作名是试出来的,值得记下来:
|
|
8
|
+
// * XSeGotoGraphicOpenPageAction —— 调用不报错、什么也不做。
|
|
9
|
+
// 它的 ModuleName 是 SearchAndReplaceGui,是「搜索结果跳转」专用,
|
|
10
|
+
// 不是通用的开页动作。别被名字骗了。
|
|
11
|
+
// * XGedOpenPageName —— 也确实存在,但参数名不对时会把页切到 /1,行为诡异。
|
|
12
|
+
// * **OpenPageAction + 参数 PageName** —— 唯一正确的一对,实测能把标题
|
|
13
|
+
// 从 "=MCP/2" 切到 "=MCP/8"。
|
|
14
|
+
//
|
|
15
|
+
// 窗口标题刷新的等待放在桥接器里做(见 bridge/src/Program.cs 的 WaitWindowTitle),
|
|
16
|
+
// 因为本方法跑在 EPLAN 主线程上,在这里 Sleep 会把界面锁死,标题永远更新不了。
|
|
17
|
+
using System;
|
|
18
|
+
using Eplan.EplApi.ApplicationFramework;
|
|
19
|
+
using Eplan.EplApi.DataModel;
|
|
20
|
+
using EplanMcp;
|
|
21
|
+
|
|
22
|
+
namespace EplanMcpHost
|
|
23
|
+
{
|
|
24
|
+
internal static class PageOpenTool
|
|
25
|
+
{
|
|
26
|
+
private const string ActionName = "OpenPageAction";
|
|
27
|
+
private const string ParameterName = "PageName";
|
|
28
|
+
|
|
29
|
+
public static JsonValue Run(JsonValue args)
|
|
30
|
+
{
|
|
31
|
+
string pageName = args["page"].AsString(null);
|
|
32
|
+
Page page = Context.FindPage(pageName, args["project"].AsString(null));
|
|
33
|
+
|
|
34
|
+
JsonValue result = JsonValue.NewObject();
|
|
35
|
+
result.Set("page", page.Name);
|
|
36
|
+
result.Set("project", page.Project.ProjectName);
|
|
37
|
+
|
|
38
|
+
ActionManager manager = new ActionManager();
|
|
39
|
+
Eplan.EplApi.ApplicationFramework.Action action = manager.FindAction(ActionName, true);
|
|
40
|
+
if (action == null)
|
|
41
|
+
throw new HostException("ACTION_MISSING",
|
|
42
|
+
"EPLAN 里没有 " + ActionName + " 这个动作,无法跳页。");
|
|
43
|
+
|
|
44
|
+
try
|
|
45
|
+
{
|
|
46
|
+
ActionCallingContext context = new ActionCallingContext();
|
|
47
|
+
context.AddParameter(ParameterName, page.Name);
|
|
48
|
+
action.Execute(context);
|
|
49
|
+
}
|
|
50
|
+
catch (Exception ex)
|
|
51
|
+
{
|
|
52
|
+
throw new HostException("PAGE_OPEN_FAILED",
|
|
53
|
+
"跳页动作执行失败:" + ex.GetType().Name + ": " + ex.Message);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
result.Set("action", ActionName);
|
|
57
|
+
result.Set("parameter", ParameterName);
|
|
58
|
+
result.Set("requested", true);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// pages:列出页面,确认目标页存在、名字没写错。
|
|
2
|
+
using System;
|
|
3
|
+
using System.Collections.Generic;
|
|
4
|
+
using System.Globalization;
|
|
5
|
+
using Eplan.EplApi.DataModel;
|
|
6
|
+
using EplanMcp;
|
|
7
|
+
|
|
8
|
+
namespace EplanMcpHost
|
|
9
|
+
{
|
|
10
|
+
internal static class PagesTool
|
|
11
|
+
{
|
|
12
|
+
public static JsonValue Run(JsonValue args)
|
|
13
|
+
{
|
|
14
|
+
string filter = args["filter"].AsString(null);
|
|
15
|
+
int limit = args["limit"].AsInt(100);
|
|
16
|
+
if (limit <= 0) limit = 100;
|
|
17
|
+
bool includePlacements = args["includePlacements"].AsBool(false);
|
|
18
|
+
|
|
19
|
+
Project project = Context.Resolve(args["project"].AsString(null));
|
|
20
|
+
Page[] pages = project.Pages;
|
|
21
|
+
if (pages == null) pages = new Page[0];
|
|
22
|
+
|
|
23
|
+
List<Page> matched = new List<Page>();
|
|
24
|
+
int total = 0;
|
|
25
|
+
foreach (Page page in pages)
|
|
26
|
+
{
|
|
27
|
+
total++;
|
|
28
|
+
if (filter != null && filter.Length > 0)
|
|
29
|
+
{
|
|
30
|
+
string name = page.Name ?? string.Empty;
|
|
31
|
+
string ident = page.IdentifyingName ?? string.Empty;
|
|
32
|
+
if (name.IndexOf(filter, StringComparison.OrdinalIgnoreCase) < 0
|
|
33
|
+
&& ident.IndexOf(filter, StringComparison.OrdinalIgnoreCase) < 0)
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
matched.Add(page);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 按页名排序,结果稳定、便于模型比对。
|
|
40
|
+
matched.Sort(delegate (Page a, Page b)
|
|
41
|
+
{
|
|
42
|
+
return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
JsonValue result = JsonValue.NewObject();
|
|
46
|
+
result.Set("project", project.ProjectName);
|
|
47
|
+
result.Set("totalPages", total);
|
|
48
|
+
result.Set("matchedCount", matched.Count);
|
|
49
|
+
|
|
50
|
+
JsonValue list = JsonValue.NewArray();
|
|
51
|
+
int emitted = 0;
|
|
52
|
+
foreach (Page page in matched)
|
|
53
|
+
{
|
|
54
|
+
if (emitted >= limit) break;
|
|
55
|
+
emitted++;
|
|
56
|
+
|
|
57
|
+
JsonValue item = JsonValue.NewObject();
|
|
58
|
+
item.Set("name", page.Name);
|
|
59
|
+
item.Set("identifyingName", page.IdentifyingName);
|
|
60
|
+
item.Set("type", page.PageTypeName);
|
|
61
|
+
try { item.Set("size", PageSizeJson(page)); }
|
|
62
|
+
catch { }
|
|
63
|
+
|
|
64
|
+
if (includePlacements)
|
|
65
|
+
{
|
|
66
|
+
try
|
|
67
|
+
{
|
|
68
|
+
Placement[] placements = page.AllPlacements;
|
|
69
|
+
item.Set("placementCount", placements == null ? 0 : placements.Length);
|
|
70
|
+
int functions = 0;
|
|
71
|
+
if (page.Functions != null) functions = page.Functions.Length;
|
|
72
|
+
item.Set("functionCount", functions);
|
|
73
|
+
}
|
|
74
|
+
catch (Exception ex)
|
|
75
|
+
{
|
|
76
|
+
item.Set("placementError", ex.Message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
list.Add(item);
|
|
80
|
+
}
|
|
81
|
+
result.Set("pages", list);
|
|
82
|
+
if (matched.Count > emitted) result.Set("truncated", true);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private static JsonValue PageSizeJson(Page page)
|
|
87
|
+
{
|
|
88
|
+
Eplan.EplApi.Base.PointD size = page.Size;
|
|
89
|
+
JsonValue array = JsonValue.NewArray();
|
|
90
|
+
array.Add(JsonValue.NewNumber(size.X));
|
|
91
|
+
array.Add(JsonValue.NewNumber(size.Y));
|
|
92
|
+
return array;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|