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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -3
  3. package/cordis.patch.yml +182 -0
  4. package/docs/INSTALL.md +86 -0
  5. package/docs/LESSONS.md +224 -0
  6. package/docs/interfaces.md +183 -0
  7. package/docs/merged-skills/eplan-setup.md +63 -0
  8. package/docs/merged-skills/eplan-troubleshooting.md +149 -0
  9. package/icon.png +0 -0
  10. package/icon.svg +13 -0
  11. package/knowledge/KNOWLEDGE.md +413 -0
  12. package/knowledge/symbols/BP.png +0 -0
  13. package/knowledge/symbols/CDPST.png +0 -0
  14. package/knowledge/symbols/CO.png +0 -0
  15. package/knowledge/symbols/FA3.png +0 -0
  16. package/knowledge/symbols/TLRO.png +0 -0
  17. package/knowledge/symbols/TLRU.png +0 -0
  18. package/knowledge/symbols/TOUL.png +0 -0
  19. package/knowledge/symbols/TOUR.png +0 -0
  20. package/knowledge/symbols/manifest.json +91 -0
  21. package/locale/en.json +6 -0
  22. package/locale/zh.json +6 -0
  23. package/native/bridge/build.ps1 +73 -0
  24. package/native/bridge/src/Json.cs +383 -0
  25. package/native/bridge/src/Program.cs +1133 -0
  26. package/native/host/build.ps1 +74 -0
  27. package/native/host/src/ActionsTool.cs +225 -0
  28. package/native/host/src/BoxTool.cs +196 -0
  29. package/native/host/src/CheckTool.cs +186 -0
  30. package/native/host/src/Context.cs +169 -0
  31. package/native/host/src/DarkScheme.cs +111 -0
  32. package/native/host/src/DiagnoseTool.cs +115 -0
  33. package/native/host/src/Dispatcher.cs +51 -0
  34. package/native/host/src/DrawTool.cs +316 -0
  35. package/native/host/src/Entry.cs +111 -0
  36. package/native/host/src/ExportTool.cs +213 -0
  37. package/native/host/src/InspectTool.cs +367 -0
  38. package/native/host/src/KnowledgeTool.cs +166 -0
  39. package/native/host/src/LookTool.cs +257 -0
  40. package/native/host/src/OpLog.cs +289 -0
  41. package/native/host/src/PageCreateTool.cs +521 -0
  42. package/native/host/src/PageOpenTool.cs +62 -0
  43. package/native/host/src/PagesTool.cs +95 -0
  44. package/native/host/src/Paths.cs +91 -0
  45. package/native/host/src/PlacementEditTool.cs +234 -0
  46. package/native/host/src/ProbePropsTool.cs +152 -0
  47. package/native/host/src/ProjectCreateTool.cs +176 -0
  48. package/native/host/src/ProjectTool.cs +167 -0
  49. package/native/host/src/PropWriter.cs +70 -0
  50. package/native/host/src/PropsTool.cs +257 -0
  51. package/native/host/src/StatusTool.cs +168 -0
  52. package/native/host/src/SymbolInfoTool.cs +144 -0
  53. package/native/host/src/SymbolInsertTool.cs +261 -0
  54. package/native/host/src/SymbolsTool.cs +94 -0
  55. package/native/host/src/TagLocTool.cs +108 -0
  56. package/native/script/EplanMcpBootstrap.cs +221 -0
  57. package/native/script/build-symbol-gallery.mjs +231 -0
  58. package/native/script/prepare-install.ps1 +57 -0
  59. package/native/script/prune.mjs +132 -0
  60. package/native/script/verify-bootstrap.ps1 +62 -0
  61. package/package.json +53 -4
  62. package/preset/bootstrap.js +498 -0
  63. package/preset/experience-nudge.js +47 -0
  64. package/preset/image-budget.js +175 -0
  65. package/preset/knowledge-tool.js +180 -0
  66. package/preset/mcp-link.js +100 -0
  67. package/preset/prompt-surface.js +76 -0
  68. package/preset/restrict-tools.js +141 -0
  69. package/preset/skill-docs.js +181 -0
  70. package/preset/video-digest.js +294 -0
  71. package/preset/video-tool.js +92 -0
  72. package/server/compact.mjs +623 -0
  73. package/server/edit-args.mjs +67 -0
  74. package/server/fuzzy.mjs +57 -0
  75. package/server/launch-decision.mjs +26 -0
  76. package/server/measure.mjs +320 -0
  77. package/server/pdf.mjs +185 -0
  78. package/server/pdf2png.ps1 +99 -0
  79. package/server/server.mjs +3797 -0
  80. package/server/video.mjs +857 -0
  81. package/setup.mjs +223 -0
  82. package/skills/eplan-annotations/SKILL.md +55 -0
  83. package/skills/eplan-drawing/SKILL.md +206 -0
  84. package/skills/eplan-project/SKILL.md +41 -0
  85. package/skills/eplan-review/SKILL.md +80 -0
  86. package/skills/eplan-symbols/SKILL.md +142 -0
  87. package/test/check-docs.mjs +106 -0
  88. package/test/foreground.ps1 +21 -0
  89. package/test/smoke-all.mjs +229 -0
@@ -0,0 +1,91 @@
1
+ // 数据目录:知识库与经验文件放哪、以及从旧位置搬过来。
2
+ //
3
+ // 为什么不用 %TEMP%:
4
+ // 原先知识库和经验文件都在 Path.GetTempPath() 下。临时目录会被磁盘清理、存储感知、
5
+ // 或者机器重装清掉 —— 而这两份数据的全部价值就在于「跨会话留下来」。
6
+ // 放在 %LOCALAPPDATA% 才是它们该待的地方。
7
+ //
8
+ // 为什么还要留退回路径:
9
+ // 宿主 DLL 跑在 EPLAN 的 AppDomain 里。取不到 LocalApplicationData 时宁可退回
10
+ // 临时目录(本次会话仍可用),也不要抛异常把 status 之类的基础工具带崩。
11
+ //
12
+ // 迁移规则(只做一次、绝不覆盖):
13
+ // 新位置已有同名文件 —— 说明已经迁移过或者新位置有更新的数据,一律不动;
14
+ // 旧文件复制成功之后**不删除**,留着当兜底,用户想回退随时能找回来。
15
+ using System;
16
+ using System.IO;
17
+
18
+ namespace EplanMcpHost
19
+ {
20
+ internal static class Paths
21
+ {
22
+ private static string _knowledgeDir;
23
+
24
+ // 知识库(symbols.json)与经验文件(notes.jsonl)共用的目录。
25
+ internal static string KnowledgeDir
26
+ {
27
+ get
28
+ {
29
+ if (_knowledgeDir == null) _knowledgeDir = Resolve();
30
+ return _knowledgeDir;
31
+ }
32
+ }
33
+
34
+ // 旧位置。只在迁移时读,不作为写入目标。
35
+ internal static string LegacyKnowledgeDir
36
+ {
37
+ get { return Path.Combine(Path.GetTempPath(), "eplan-mcp", "knowledge"); }
38
+ }
39
+
40
+ private static string Resolve()
41
+ {
42
+ string legacy = LegacyKnowledgeDir;
43
+ string durable = null;
44
+
45
+ try
46
+ {
47
+ string root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
48
+ if (!string.IsNullOrEmpty(root)) durable = Path.Combine(root, "eplan-mcp", "knowledge");
49
+ }
50
+ catch
51
+ {
52
+ durable = null;
53
+ }
54
+
55
+ if (string.IsNullOrEmpty(durable)) return legacy;
56
+
57
+ try
58
+ {
59
+ if (!Directory.Exists(durable)) Directory.CreateDirectory(durable);
60
+ }
61
+ catch
62
+ {
63
+ // 建不出来(权限、只读盘)就退回临时目录,本次会话照常能用。
64
+ return legacy;
65
+ }
66
+
67
+ Migrate(legacy, durable);
68
+ return durable;
69
+ }
70
+
71
+ private static void Migrate(string fromDir, string toDir)
72
+ {
73
+ try
74
+ {
75
+ if (!Directory.Exists(fromDir)) return;
76
+ string[] files = Directory.GetFiles(fromDir, "*", SearchOption.TopDirectoryOnly);
77
+ foreach (string from in files)
78
+ {
79
+ string to = Path.Combine(toDir, Path.GetFileName(from));
80
+ if (File.Exists(to)) continue; // 新位置已有,绝不覆盖
81
+ try { File.Copy(from, to, false); }
82
+ catch { } // 单个文件搬不动不影响其余的
83
+ }
84
+ }
85
+ catch
86
+ {
87
+ // 迁移是尽力而为,失败了也只是用不上旧数据,不该让工具调用失败。
88
+ }
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,234 @@
1
+ // 增量改页:移动 / 改名 / 删除单个对象。
2
+ //
3
+ // 为什么需要它:以前改一个标签或挪一个元件,只能删页重建整页(审计里同一页
4
+ // 被重建 6 次、其中三次输出字节级相同)。这三个动作在 API 里都只是一行赋值,
5
+ // 但 Location 和 Name 都不是「属性」,所以 props 工具改不了,必须单独做 op。
6
+ //
7
+ // 选对象:objectId(page_inspect 会返回)> deviceTag(Function.Name)> index
8
+ // (AllPlacements 下标,用于没有名字的文字/图形)。三选一,且必须唯一命中。
9
+ using System;
10
+ using System.Collections.Generic;
11
+ using Eplan.EplApi.Base;
12
+ using Eplan.EplApi.DataModel;
13
+ using EplanMcp;
14
+
15
+ namespace EplanMcpHost
16
+ {
17
+ internal static class PlacementEditTool
18
+ {
19
+ public static JsonValue Move(JsonValue args)
20
+ {
21
+ Page page;
22
+ Placement target;
23
+ Resolve(args, out page, out target);
24
+
25
+ JsonValue result = JsonValue.NewObject();
26
+ result.Set("page", page.Name);
27
+ result.Set("type", target.GetType().Name);
28
+ result.Set("from", Context.PointJson(target.Location));
29
+ try { result.Set("objectId", target.ObjectIdentifier.ToString()); } catch { }
30
+
31
+ JsonValue at = args["at"];
32
+ JsonValue offset = args["offset"];
33
+ PointD to;
34
+ if (at != null && at.IsArray)
35
+ {
36
+ to = Context.Point(at, "at");
37
+ }
38
+ else if (offset != null && offset.IsArray)
39
+ {
40
+ PointD delta = Context.Point(offset, "offset");
41
+ to = new PointD(target.Location.X + delta.X, target.Location.Y + delta.Y);
42
+ }
43
+ else
44
+ {
45
+ throw new HostException("BAD_ARGUMENT",
46
+ "要给 at [x, y](绝对坐标)或 offset [dx, dy](相对位移)。");
47
+ }
48
+ result.Set("to", Context.PointJson(to));
49
+
50
+ bool dryRun = args["dryRun"].AsBool(false);
51
+ result.Set("dryRun", dryRun);
52
+ if (dryRun)
53
+ {
54
+ result.Set("moved", false);
55
+ return result;
56
+ }
57
+
58
+ target.Location = to;
59
+ result.Set("moved", true);
60
+ return result;
61
+ }
62
+
63
+ public static JsonValue Relabel(JsonValue args)
64
+ {
65
+ Page page;
66
+ Function function;
67
+ ResolveFunction(args, out page, out function);
68
+
69
+ string to = args["to"].AsString(null);
70
+ if (string.IsNullOrEmpty(to))
71
+ throw new HostException("BAD_ARGUMENT", "要给 to(新的设备标识),例如 \"-K2\"。");
72
+
73
+ JsonValue result = JsonValue.NewObject();
74
+ result.Set("page", page.Name);
75
+ result.Set("from", function.Name);
76
+ result.Set("to", to);
77
+ try { result.Set("objectId", function.ObjectIdentifier.ToString()); } catch { }
78
+
79
+ bool dryRun = args["dryRun"].AsBool(false);
80
+ result.Set("dryRun", dryRun);
81
+ if (dryRun)
82
+ {
83
+ result.Set("renamed", false);
84
+ return result;
85
+ }
86
+
87
+ function.Name = to;
88
+ result.Set("renamed", true);
89
+ // 设备标识的文字是单独的 PropertyPlacement:有些项目里它跟着 Name 变,
90
+ // 有些不会。所以改完必须重新出图确认,这一句写进结果里,别让人猜。
91
+ result.Set("note", "改完请重新出图确认图上的标识文字也跟着变了;没变就用 page_edit action=\"draw_graphics\" 补。");
92
+ return result;
93
+ }
94
+
95
+ public static JsonValue Delete(JsonValue args)
96
+ {
97
+ Page page;
98
+ Placement target;
99
+ Resolve(args, out page, out target);
100
+
101
+ JsonValue result = JsonValue.NewObject();
102
+ result.Set("page", page.Name);
103
+ result.Set("type", target.GetType().Name);
104
+ result.Set("from", Context.PointJson(target.Location));
105
+ try { result.Set("objectId", target.ObjectIdentifier.ToString()); } catch { }
106
+
107
+ bool dryRun = args["dryRun"].AsBool(false);
108
+ result.Set("dryRun", dryRun);
109
+ if (dryRun)
110
+ {
111
+ result.Set("deleted", false);
112
+ return result;
113
+ }
114
+
115
+ target.Remove();
116
+ result.Set("deleted", true);
117
+ return result;
118
+ }
119
+
120
+ // ---------------------------------------------------------------
121
+ // 定位
122
+ // ---------------------------------------------------------------
123
+
124
+ private static void Resolve(JsonValue args, out Page page, out Placement target)
125
+ {
126
+ JsonValue selector = args["selector"];
127
+ if (selector == null || !selector.IsObject)
128
+ throw new HostException("BAD_ARGUMENT", "缺少 selector(给 objectId / deviceTag / index 之一)。");
129
+
130
+ page = Context.FindPage(
131
+ selector["page"].AsString(args["page"].AsString(null)),
132
+ selector["project"].AsString(args["project"].AsString(null)));
133
+
134
+ List<Placement> matches = Match(page, selector);
135
+ if (matches.Count == 0)
136
+ throw new HostException("OBJECT_NOT_FOUND",
137
+ "页「" + page.Name + "」上没找到匹配的对象:" + Json.Write(selector) +
138
+ "。先用 page_inspect view=\"list\" 看 objectId / deviceTag。");
139
+ if (matches.Count > 1)
140
+ throw new HostException("OBJECT_AMBIGUOUS",
141
+ "匹配到 " + matches.Count + " 个对象,这次只改一个。请给更具体的 objectId。");
142
+ target = matches[0];
143
+ }
144
+
145
+ private static void ResolveFunction(JsonValue args, out Page page, out Function function)
146
+ {
147
+ JsonValue selector = args["selector"];
148
+ if (selector == null || !selector.IsObject)
149
+ throw new HostException("BAD_ARGUMENT", "缺少 selector(给 deviceTag 或 objectId)。");
150
+
151
+ page = Context.FindPage(
152
+ selector["page"].AsString(args["page"].AsString(null)),
153
+ selector["project"].AsString(args["project"].AsString(null)));
154
+
155
+ string objectId = selector["objectId"].AsString(null);
156
+ string deviceTag = selector["deviceTag"].AsString(null);
157
+
158
+ Function found = null;
159
+ Function[] functions = page.Functions;
160
+ if (functions != null)
161
+ {
162
+ foreach (Function candidate in functions)
163
+ {
164
+ if (!string.IsNullOrEmpty(objectId))
165
+ {
166
+ try
167
+ {
168
+ if (string.Equals(candidate.ObjectIdentifier.ToString(), objectId, StringComparison.OrdinalIgnoreCase))
169
+ {
170
+ found = candidate;
171
+ break;
172
+ }
173
+ }
174
+ catch { }
175
+ }
176
+ else if (!string.IsNullOrEmpty(deviceTag) &&
177
+ string.Equals(candidate.Name, deviceTag, StringComparison.OrdinalIgnoreCase))
178
+ {
179
+ found = candidate;
180
+ break;
181
+ }
182
+ }
183
+ }
184
+
185
+ if (found == null)
186
+ throw new HostException("OBJECT_NOT_FOUND",
187
+ "页「" + page.Name + "」上没有设备标识为「" + (deviceTag ?? objectId) + "」的 Function。");
188
+ function = found;
189
+ }
190
+
191
+ private static List<Placement> Match(Page page, JsonValue selector)
192
+ {
193
+ string objectId = selector["objectId"].AsString(null);
194
+ string deviceTag = selector["deviceTag"].AsString(null);
195
+ int index = selector["index"].AsInt(-1);
196
+
197
+ Placement[] all = page.AllPlacements;
198
+ List<Placement> matches = new List<Placement>();
199
+
200
+ if (index >= 0)
201
+ {
202
+ if (all == null || index >= all.Length)
203
+ throw new HostException("OBJECT_NOT_FOUND",
204
+ "index " + index + " 越界:这一页只有 " + (all == null ? 0 : all.Length) + " 个放置对象。");
205
+ matches.Add(all[index]);
206
+ return matches;
207
+ }
208
+
209
+ if (all == null) return matches;
210
+
211
+ foreach (Placement placement in all)
212
+ {
213
+ if (!string.IsNullOrEmpty(objectId))
214
+ {
215
+ try
216
+ {
217
+ if (string.Equals(placement.ObjectIdentifier.ToString(), objectId, StringComparison.OrdinalIgnoreCase))
218
+ matches.Add(placement);
219
+ }
220
+ catch { }
221
+ continue;
222
+ }
223
+
224
+ Function function = placement as Function;
225
+ if (function != null && !string.IsNullOrEmpty(deviceTag) &&
226
+ string.Equals(function.Name, deviceTag, StringComparison.OrdinalIgnoreCase))
227
+ {
228
+ matches.Add(placement);
229
+ }
230
+ }
231
+ return matches;
232
+ }
233
+ }
234
+ }
@@ -0,0 +1,152 @@
1
+ // probe_props:试出 EPLAN 到底认哪些属性标识名,以及该怎么写进去。
2
+ //
3
+ // 背景:`project.Properties["PROJ_XXX"]` 这种字符串索引器对所有 PROJ_ 标识名
4
+ // 都抛 `S063109属性无效`(实测 14 个全挂),但这些属性明明在
5
+ // `ProjectPropertyList` 上以强类型成员的形式存在。所以这里除了字符串索引器,
6
+ // 还走一遍反射取成员 —— 两种路径都试,把「哪条通、怎么写成功」如实报出来。
7
+ using System;
8
+ using System.Reflection;
9
+ using Eplan.EplApi.DataModel;
10
+ using EplanMcp;
11
+
12
+ namespace EplanMcpHost
13
+ {
14
+ internal static class ProbePropsTool
15
+ {
16
+ public static JsonValue Run(JsonValue args)
17
+ {
18
+ JsonValue identsArg = args["idents"];
19
+ string patternEarly = args["pattern"].AsString(null);
20
+ // 只列成员名(不给 idents)也是合法用法,所以这里两个都放过。
21
+ bool hasIdents = identsArg != null && identsArg.IsArray && identsArg.Count > 0;
22
+ if (!hasIdents && string.IsNullOrEmpty(patternEarly))
23
+ throw new HostException("BAD_ARGUMENT", "给一组要试的属性标识名(idents),或者用 pattern 列出成员名。");
24
+
25
+ Project project = Context.Resolve(args["project"].AsString(null));
26
+ bool write = args["write"].AsBool(false);
27
+ string writeValue = args["writeValue"].AsString("__mcp_probe__");
28
+
29
+ // 先看运行时类型上到底有哪些成员名:dump 出来的可能是别的类,
30
+ // 只有 project.Properties 的真实类型才算数。
31
+ string pattern = patternEarly;
32
+ if (!string.IsNullOrEmpty(pattern))
33
+ {
34
+ JsonValue names = JsonValue.NewArray();
35
+ foreach (PropertyInfo p in project.Properties.GetType().GetProperties(
36
+ BindingFlags.Public | BindingFlags.Instance))
37
+ {
38
+ if (p.PropertyType != typeof(PropertyValue)) continue;
39
+ if (pattern != "*" && p.Name.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) < 0) continue;
40
+ names.Add(JsonValue.NewString(p.Name));
41
+ }
42
+ JsonValue listed = JsonValue.NewObject();
43
+ listed.Set("pattern", pattern);
44
+ listed.Set("count", names.Count);
45
+ listed.Set("names", names);
46
+ return listed;
47
+ }
48
+
49
+ JsonValue found = JsonValue.NewArray();
50
+ JsonValue missing = JsonValue.NewArray();
51
+
52
+ for (int i = 0; i < identsArg.Count; i++)
53
+ {
54
+ string ident = identsArg[i].AsString(null);
55
+ if (string.IsNullOrEmpty(ident)) continue;
56
+
57
+ JsonValue item = JsonValue.NewObject();
58
+ item.Set("ident", ident);
59
+
60
+ PropertyValue value = null;
61
+
62
+ // 路径一:字符串索引器。对 PROJ_* 基本都会失败,但不能先入为主。
63
+ try
64
+ {
65
+ value = project.Properties[ident];
66
+ item.Set("viaIndexer", true);
67
+ }
68
+ catch (Exception ex)
69
+ {
70
+ item.Set("viaIndexer", false);
71
+ item.Set("indexerError", ex.GetType().Name + ": " + ex.Message);
72
+ }
73
+
74
+ // 路径二:反射取强类型成员。
75
+ if (value == null)
76
+ {
77
+ try
78
+ {
79
+ // 每个属性名都有重载(带 Int32 index 的多值版本),
80
+ // 直接 GetProperty 会抛 AmbiguousMatchException,
81
+ // 必须挑「不带索引参数」的那个。
82
+ PropertyInfo member = null;
83
+ foreach (PropertyInfo candidate in project.Properties.GetType().GetProperties(
84
+ BindingFlags.Public | BindingFlags.Instance))
85
+ {
86
+ if (candidate.Name != ident) continue;
87
+ if (candidate.GetIndexParameters().Length != 0) continue;
88
+ member = candidate;
89
+ break;
90
+ }
91
+ if (member != null)
92
+ {
93
+ value = member.GetValue(project.Properties, null) as PropertyValue;
94
+ item.Set("viaMember", true);
95
+ }
96
+ }
97
+ catch (Exception ex)
98
+ {
99
+ item.Set("memberError", ex.GetType().Name + ": " + ex.Message);
100
+ }
101
+ }
102
+
103
+ if (value == null)
104
+ {
105
+ item.Set("found", false);
106
+ missing.Add(item);
107
+ continue;
108
+ }
109
+
110
+ item.Set("found", true);
111
+
112
+ string before = null;
113
+ try { before = value.ToString(); } catch { }
114
+ item.Set("current", before);
115
+
116
+ if (write)
117
+ {
118
+ try
119
+ {
120
+ value.Set(writeValue);
121
+ string after = null;
122
+ try { after = value.ToString(); } catch { }
123
+ item.Set("writable", true);
124
+ item.Set("afterWrite", after);
125
+ if (before != null) { try { value.Set(before); } catch { } }
126
+ }
127
+ catch (Exception ex)
128
+ {
129
+ item.Set("writable", false);
130
+ item.Set("writeError", ex.GetType().Name + ": " + ex.Message);
131
+ }
132
+ }
133
+
134
+ found.Add(item);
135
+ }
136
+
137
+ JsonValue result = JsonValue.NewObject();
138
+ result.Set("project", project.ProjectName);
139
+ // 把运行时类型名报出来:反射到哪个类上,决定了成员名长什么样。
140
+ Type propsType = project.Properties.GetType();
141
+ result.Set("propertiesType", propsType.FullName);
142
+ result.Set("propertiesAssembly", propsType.Assembly.GetName().Name);
143
+ int memberCount = 0;
144
+ foreach (PropertyInfo p in propsType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
145
+ if (p.PropertyType == typeof(PropertyValue)) memberCount++;
146
+ result.Set("propertyMemberCount", memberCount);
147
+ result.Set("found", found);
148
+ result.Set("missing", missing);
149
+ return result;
150
+ }
151
+ }
152
+ }
@@ -0,0 +1,176 @@
1
+ // project_create:新建一个项目,并按需写入项目属性(例如作者)。
2
+ //
3
+ // EPLAN 新建项目要两样东西:
4
+ // linkFilePath 项目文件(.elk)要落在哪里
5
+ // templateFilePath 用哪个模板(.zw9 基础项目 / .ept 项目模板)
6
+ // 模板决定图框、标题栏、符号库、层设置 —— 不传模板建出来的项目是空壳,
7
+ // 画出来的图没有图框,用户一眼就看出不对。
8
+ using System;
9
+ using System.Collections.Generic;
10
+ using System.IO;
11
+ using Eplan.EplApi.Base;
12
+ using Eplan.EplApi.DataModel;
13
+ using EplanMcp;
14
+
15
+ namespace EplanMcpHost
16
+ {
17
+ internal static class ProjectCreateTool
18
+ {
19
+ public static JsonValue Run(JsonValue args)
20
+ {
21
+ string name = args["name"].AsString(null);
22
+ if (string.IsNullOrEmpty(name))
23
+ throw new HostException("BAD_ARGUMENT", "缺少项目名(name),例如「三菱变频器」。");
24
+
25
+ string directory = args["directory"].AsString(
26
+ @"C:\Users\Public\EPLAN\Data\项目\Company name");
27
+ string template = args["template"].AsString(null);
28
+ JsonValue properties = args["properties"];
29
+ bool dryRun = args["dryRun"].AsBool(false);
30
+
31
+ string elkPath = Path.Combine(directory, name + ".elk");
32
+
33
+ if (File.Exists(elkPath) || Directory.Exists(Path.Combine(directory, name + ".edb")))
34
+ throw new HostException("PROJECT_EXISTS",
35
+ "项目「" + name + "」已经存在:" + elkPath + "。换个名字,或者用 project_open 打开它。");
36
+
37
+ if (!Directory.Exists(directory))
38
+ throw new HostException("BAD_ARGUMENT", "目录不存在:" + directory);
39
+
40
+ if (string.IsNullOrEmpty(template))
41
+ template = PickTemplate(args["templateName"].AsString(null));
42
+
43
+ if (string.IsNullOrEmpty(template) || !File.Exists(template))
44
+ throw new HostException("TEMPLATE_NOT_FOUND",
45
+ "找不到项目模板:" + (template ?? "(未指定)") +
46
+ "。模板决定图框和标题栏,必须给一个;常见位置是 C:\\Users\\Public\\EPLAN\\Data\\模板\\Company name\\ 下的 .zw9 或 .ept。");
47
+
48
+ JsonValue result = JsonValue.NewObject();
49
+ result.Set("name", name);
50
+ result.Set("elkPath", elkPath);
51
+ result.Set("template", template);
52
+ result.Set("dryRun", dryRun);
53
+
54
+ if (dryRun)
55
+ {
56
+ result.Set("created", false);
57
+ result.Set("wouldCreate", true);
58
+ return result;
59
+ }
60
+
61
+ ProjectManager manager = new ProjectManager();
62
+
63
+ Project project;
64
+ try
65
+ {
66
+ project = manager.CreateProject(elkPath, template);
67
+ }
68
+ catch (Exception ex)
69
+ {
70
+ throw new HostException("PROJECT_CREATE_FAILED",
71
+ "建项目失败:" + ex.GetType().Name + ": " + ex.Message, ex.ToString());
72
+ }
73
+
74
+ if (project == null)
75
+ throw new HostException("PROJECT_CREATE_FAILED", "EPLAN 没有返回项目对象,创建可能没成功。");
76
+
77
+ result.Set("created", true);
78
+
79
+ JsonValue info = JsonValue.NewObject();
80
+ info.Set("name", project.ProjectName);
81
+ info.Set("path", project.ProjectLinkFilePath);
82
+ info.Set("directory", project.ProjectDirectoryPath);
83
+ info.Set("pageCount", project.Pages == null ? 0 : project.Pages.Length);
84
+ result.Set("project", info);
85
+
86
+ // 项目属性:EPLAN 的标识名没法从反射里枚举出来(Properties.Project 这个类
87
+ // 在程序集里查不到),所以按调用方给的键逐个试,把每个键的成败如实报出来,
88
+ // 而不是整批失败或整批假装成功。
89
+ if (properties != null && properties.IsObject)
90
+ {
91
+ JsonValue applied = JsonValue.NewObject();
92
+ JsonValue failed = JsonValue.NewObject();
93
+ foreach (string key in properties.Keys)
94
+ {
95
+ string ident = MapFriendly(key);
96
+ string text = properties[key].AsString("");
97
+ Exception error;
98
+ if (PropWriter.Write(project, ident, text, out error))
99
+ {
100
+ applied.Set(ident, text);
101
+ }
102
+ else
103
+ {
104
+ failed.Set(ident, error.GetType().Name + ": " + error.Message);
105
+ }
106
+ }
107
+ result.Set("propertiesApplied", applied);
108
+ result.Set("propertiesFailed", failed);
109
+ }
110
+
111
+ // 作者这类信息如果用户在 EPLAN 里看得见,多半是通过「项目属性」对话框。
112
+ // 这里额外报告一份实际读回来的值,方便核对有没有真的写进去。
113
+ JsonValue probe = ProbeAuthor(project);
114
+ if (probe != null) result.Set("authorProbe", probe);
115
+
116
+ return result;
117
+ }
118
+
119
+ /// <summary>没指定模板时,从模板目录里挑一个基础项目(优先 GB)。</summary>
120
+ private static string PickTemplate(string preferred)
121
+ {
122
+ string root = @"C:\Users\Public\EPLAN\Data\模板\Company name";
123
+ if (!Directory.Exists(root)) return null;
124
+
125
+ string[] order = preferred != null
126
+ ? new string[] { preferred }
127
+ : new string[] { "GB_tpl001.ept", "GB_bas001.zw9", "IEC_tpl001.ept", "IEC_bas001.zw9" };
128
+
129
+ foreach (string candidate in order)
130
+ {
131
+ string full = Path.Combine(root, candidate);
132
+ if (File.Exists(full)) return full;
133
+ }
134
+
135
+ string[] any = Directory.GetFiles(root, "*.zw9");
136
+ if (any.Length > 0) return any[0];
137
+ any = Directory.GetFiles(root, "*.ept");
138
+ return any.Length > 0 ? any[0] : null;
139
+ }
140
+
141
+ /// <summary>把项目身份相关的属性读回来,确认真的写进去了。</summary>
142
+ private static JsonValue ProbeAuthor(Project project)
143
+ {
144
+ string[] idents = new string[]
145
+ {
146
+ "PROJ_CREATORLONGNAME", // 创建者(长名)—— 标题栏里的「作者」就是它
147
+ "PROJ_RESPONSIBLEFORPROJECT",
148
+ "PROJ_CREATORNAME",
149
+ };
150
+
151
+ JsonValue found = JsonValue.NewObject();
152
+ bool any = false;
153
+ foreach (string ident in idents)
154
+ {
155
+ string text = PropWriter.Read(project, ident);
156
+ if (text == null) continue;
157
+ found.Set(ident, text);
158
+ any = true;
159
+ }
160
+ return any ? found : null;
161
+ }
162
+
163
+ /// <summary>把用户好写的短名字映射到 EPLAN 的标识名。</summary>
164
+ private static string MapFriendly(string key)
165
+ {
166
+ switch (key.ToLowerInvariant())
167
+ {
168
+ case "author": return "PROJ_CREATORLONGNAME";
169
+ case "creator": return "PROJ_CREATORLONGNAME";
170
+ case "responsible": return "PROJ_RESPONSIBLEFORPROJECT";
171
+ case "note": return "PROJ_CREATORNOTE";
172
+ default: return key; // 已经是标识名就原样用
173
+ }
174
+ }
175
+ }
176
+ }