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,167 @@
|
|
|
1
|
+
// project_open / project_close:开、关项目。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要:
|
|
4
|
+
// MCP 会话常常在 EPLAN 刚启动、还没有打开任何项目的时候接入。
|
|
5
|
+
// 之前只能靠人先手工打开项目,否则所有工具都报 PROJECT_NOT_OPEN。
|
|
6
|
+
// 把这一步做进工具里,"链接实时性"才完整:连不上就自己把项目拉起来。
|
|
7
|
+
//
|
|
8
|
+
// 实现细节:ProjectManager.OpenProject 的第二个参数是个嵌套枚举
|
|
9
|
+
// (ProjectManager+OpenMode),这里用反射取值,避免写死枚举成员名——
|
|
10
|
+
// 不同版本 EPLAN 的枚举成员不完全一样。
|
|
11
|
+
using System;
|
|
12
|
+
using System.Reflection;
|
|
13
|
+
using Eplan.EplApi.DataModel;
|
|
14
|
+
using EplanMcp;
|
|
15
|
+
|
|
16
|
+
namespace EplanMcpHost
|
|
17
|
+
{
|
|
18
|
+
internal static class ProjectTool
|
|
19
|
+
{
|
|
20
|
+
private const string DefaultProjectDir = @"C:\Users\Public\EPLAN\Data\项目\Company name";
|
|
21
|
+
|
|
22
|
+
public static JsonValue Open(JsonValue args)
|
|
23
|
+
{
|
|
24
|
+
string path = args["path"].AsString(null);
|
|
25
|
+
string name = args["name"].AsString(null);
|
|
26
|
+
|
|
27
|
+
if (string.IsNullOrEmpty(path))
|
|
28
|
+
{
|
|
29
|
+
if (string.IsNullOrEmpty(name))
|
|
30
|
+
throw new HostException("BAD_ARGUMENT",
|
|
31
|
+
"project_open 需要 path(.elk 完整路径)或 name(项目名)之一。");
|
|
32
|
+
string dir = args["dir"].AsString(DefaultProjectDir);
|
|
33
|
+
path = System.IO.Path.Combine(dir, name + ".elk");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!System.IO.File.Exists(path))
|
|
37
|
+
throw new HostException("PROJECT_FILE_MISSING",
|
|
38
|
+
"找不到项目文件:" + path + "。检查项目名或目录是否正确。");
|
|
39
|
+
|
|
40
|
+
ProjectManager manager = new ProjectManager();
|
|
41
|
+
JsonValue result = JsonValue.NewObject();
|
|
42
|
+
result.Set("path", path);
|
|
43
|
+
|
|
44
|
+
// 已经打开就直接返回,避免重复打开触发 EPLAN 的"项目已被占用"。
|
|
45
|
+
Project already = FindOpen(manager, path);
|
|
46
|
+
if (already != null)
|
|
47
|
+
{
|
|
48
|
+
result.Set("alreadyOpen", true);
|
|
49
|
+
result.Set("project", Describe(already));
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
InvokeOpen(manager, path);
|
|
54
|
+
|
|
55
|
+
// 开完之后不一定能立刻读 CurrentProject:在 LockingStep 之外访问它
|
|
56
|
+
// 会抛 S063110。所以按「CurrentProject → 按路径找 → 取最后一个」三级回退。
|
|
57
|
+
Project opened = null;
|
|
58
|
+
try { opened = manager.CurrentProject; } catch { }
|
|
59
|
+
if (opened == null) opened = FindOpen(manager, path);
|
|
60
|
+
if (opened == null)
|
|
61
|
+
{
|
|
62
|
+
try
|
|
63
|
+
{
|
|
64
|
+
Project[] all = manager.OpenProjects;
|
|
65
|
+
if (all != null && all.Length > 0) opened = all[all.Length - 1];
|
|
66
|
+
}
|
|
67
|
+
catch { }
|
|
68
|
+
}
|
|
69
|
+
if (opened == null)
|
|
70
|
+
throw new HostException("PROJECT_OPEN_FAILED",
|
|
71
|
+
"OpenProject 调用返回了,但拿不到打开后的项目对象。检查该项目是否被别的 EPLAN 实例占用。");
|
|
72
|
+
|
|
73
|
+
result.Set("alreadyOpen", false);
|
|
74
|
+
result.Set("project", Describe(opened));
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public static JsonValue Close(JsonValue args)
|
|
79
|
+
{
|
|
80
|
+
ProjectManager manager = new ProjectManager();
|
|
81
|
+
Project project = Context.Resolve(args["project"].AsString(null));
|
|
82
|
+
string name = project.ProjectName;
|
|
83
|
+
project.Close();
|
|
84
|
+
JsonValue result = JsonValue.NewObject();
|
|
85
|
+
result.Set("closed", true);
|
|
86
|
+
result.Set("project", name);
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private static Project FindOpen(ProjectManager manager, string path)
|
|
91
|
+
{
|
|
92
|
+
Project[] open = manager.OpenProjects;
|
|
93
|
+
if (open == null) return null;
|
|
94
|
+
foreach (Project project in open)
|
|
95
|
+
{
|
|
96
|
+
try
|
|
97
|
+
{
|
|
98
|
+
if (string.Equals(project.ProjectLinkFilePath, path, StringComparison.OrdinalIgnoreCase))
|
|
99
|
+
return project;
|
|
100
|
+
}
|
|
101
|
+
catch { }
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// <summary>
|
|
107
|
+
/// 反射调用 OpenProject。优先用 (string, OpenMode) 重载,
|
|
108
|
+
/// OpenMode 取枚举里第一个值或名为 Standard 的那个。
|
|
109
|
+
/// </summary>
|
|
110
|
+
private static void InvokeOpen(ProjectManager manager, string path)
|
|
111
|
+
{
|
|
112
|
+
MethodInfo withMode = null;
|
|
113
|
+
MethodInfo single = null;
|
|
114
|
+
|
|
115
|
+
foreach (MethodInfo method in manager.GetType().GetMethods(
|
|
116
|
+
BindingFlags.Public | BindingFlags.Instance))
|
|
117
|
+
{
|
|
118
|
+
if (method.Name != "OpenProject") continue;
|
|
119
|
+
ParameterInfo[] parameters = method.GetParameters();
|
|
120
|
+
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(string))
|
|
121
|
+
single = method;
|
|
122
|
+
if (parameters.Length == 2
|
|
123
|
+
&& parameters[0].ParameterType == typeof(string)
|
|
124
|
+
&& !parameters[1].ParameterType.IsByRef)
|
|
125
|
+
withMode = method;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (withMode != null)
|
|
129
|
+
{
|
|
130
|
+
Type enumType = withMode.GetParameters()[1].ParameterType;
|
|
131
|
+
object mode = null;
|
|
132
|
+
try { mode = Enum.Parse(enumType, "Standard"); }
|
|
133
|
+
catch { }
|
|
134
|
+
if (mode == null)
|
|
135
|
+
{
|
|
136
|
+
Array values = Enum.GetValues(enumType);
|
|
137
|
+
if (values.Length > 0) mode = values.GetValue(0);
|
|
138
|
+
}
|
|
139
|
+
if (mode == null)
|
|
140
|
+
throw new HostException("PROJECT_OPEN_FAILED",
|
|
141
|
+
"无法构造 OpenMode 值(枚举 " + enumType.FullName + " 没有任何成员)。");
|
|
142
|
+
withMode.Invoke(manager, new object[] { path, mode });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (single != null)
|
|
147
|
+
{
|
|
148
|
+
single.Invoke(manager, new object[] { path });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
throw new HostException("PROJECT_OPEN_UNSUPPORTED",
|
|
153
|
+
"ProjectManager 上没有 OpenProject(string) 或 OpenProject(string, OpenMode) 重载。");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private static JsonValue Describe(Project project)
|
|
157
|
+
{
|
|
158
|
+
JsonValue item = JsonValue.NewObject();
|
|
159
|
+
try { item.Set("name", project.ProjectName); } catch { }
|
|
160
|
+
try { item.Set("path", project.ProjectLinkFilePath); } catch { }
|
|
161
|
+
try { item.Set("directory", project.ProjectDirectoryPath); } catch { }
|
|
162
|
+
try { item.Set("isReadOnly", project.IsReadOnly); } catch { }
|
|
163
|
+
try { item.Set("pageCount", JsonValue.NewNumber(project.Pages.Length)); } catch { }
|
|
164
|
+
return item;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// 写 EPLAN 属性的统一入口。
|
|
2
|
+
//
|
|
3
|
+
// 踩出来的两个坑,记在这里免得再踩:
|
|
4
|
+
//
|
|
5
|
+
// 1. `project.Properties["PROJ_CREATORLONGNAME"]` —— 字符串索引器对**所有** PROJ_ 标识名
|
|
6
|
+
// 都抛 `PropertyNotFoundException: S063109属性无效`。别用索引器。
|
|
7
|
+
//
|
|
8
|
+
// 2. `type.GetProperty("PROJ_CREATORLONGNAME", ...)` 抛 `AmbiguousMatchException`,
|
|
9
|
+
// 因为每个属性名都有重载(一个是单值,一个是带 Int32 index 的多值版本)。
|
|
10
|
+
// 必须遍历 `GetProperties()` 挑 `GetIndexParameters().Length == 0` 的那个。
|
|
11
|
+
using System;
|
|
12
|
+
using System.Reflection;
|
|
13
|
+
using Eplan.EplApi.DataModel;
|
|
14
|
+
|
|
15
|
+
namespace EplanMcpHost
|
|
16
|
+
{
|
|
17
|
+
internal static class PropWriter
|
|
18
|
+
{
|
|
19
|
+
/// <summary>把一个属性的值读出来;读不到返回 null。</summary>
|
|
20
|
+
public static string Read(Project project, string ident)
|
|
21
|
+
{
|
|
22
|
+
PropertyValue value = Find(project, ident);
|
|
23
|
+
if (value == null) return null;
|
|
24
|
+
try { return value.ToString(); }
|
|
25
|
+
catch { return null; }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// <summary>写一个属性。成功返回 true,失败时 ex 带回原因。</summary>
|
|
29
|
+
public static bool Write(Project project, string ident, string text, out Exception error)
|
|
30
|
+
{
|
|
31
|
+
error = null;
|
|
32
|
+
PropertyValue value = Find(project, ident);
|
|
33
|
+
if (value == null)
|
|
34
|
+
{
|
|
35
|
+
error = new InvalidOperationException("EPLAN 里没有名为 " + ident + " 的项目属性。");
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
try
|
|
39
|
+
{
|
|
40
|
+
value.Set(text ?? string.Empty);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch (Exception ex)
|
|
44
|
+
{
|
|
45
|
+
error = ex;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private static PropertyValue Find(Project project, string ident)
|
|
51
|
+
{
|
|
52
|
+
if (project == null || string.IsNullOrEmpty(ident)) return null;
|
|
53
|
+
|
|
54
|
+
PropertyInfo member = null;
|
|
55
|
+
foreach (PropertyInfo candidate in project.Properties.GetType().GetProperties(
|
|
56
|
+
BindingFlags.Public | BindingFlags.Instance))
|
|
57
|
+
{
|
|
58
|
+
if (candidate.Name != ident) continue;
|
|
59
|
+
if (candidate.GetIndexParameters().Length != 0) continue;
|
|
60
|
+
if (candidate.PropertyType != typeof(PropertyValue)) continue;
|
|
61
|
+
member = candidate;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
if (member == null) return null;
|
|
65
|
+
|
|
66
|
+
try { return member.GetValue(project.Properties, null) as PropertyValue; }
|
|
67
|
+
catch { return null; }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// props:读写对象属性。
|
|
2
|
+
//
|
|
3
|
+
// 属性访问有两条路:
|
|
4
|
+
// 1. Properties.Function.FUNC_XXX 这类静态字段 → 强类型、IDE 可补全,但名字写死;
|
|
5
|
+
// 2. UniversalPropertyList 的字符串索引器 → 可以传 EPLAN 的标识名(如 "FUNC_COMMENT")。
|
|
6
|
+
// 这里优先用 1(可靠),失败后退到 2。
|
|
7
|
+
using System;
|
|
8
|
+
using System.Collections.Generic;
|
|
9
|
+
using System.Reflection;
|
|
10
|
+
using Eplan.EplApi.DataModel;
|
|
11
|
+
using EplanMcp;
|
|
12
|
+
|
|
13
|
+
namespace EplanMcpHost
|
|
14
|
+
{
|
|
15
|
+
internal static class PropsTool
|
|
16
|
+
{
|
|
17
|
+
private static readonly string[] KnownLists = new string[]
|
|
18
|
+
{
|
|
19
|
+
"Eplan.EplApi.DataModel.Properties+Function",
|
|
20
|
+
"Eplan.EplApi.DataModel.Properties+FunctionBase",
|
|
21
|
+
"Eplan.EplApi.DataModel.Properties+Placement",
|
|
22
|
+
"Eplan.EplApi.DataModel.Properties+StorableObject",
|
|
23
|
+
"Eplan.EplApi.DataModel.Properties+Page",
|
|
24
|
+
"Eplan.EplApi.DataModel.Properties+Project"
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
public static JsonValue Run(JsonValue args)
|
|
28
|
+
{
|
|
29
|
+
JsonValue selector = args["selector"];
|
|
30
|
+
if (selector == null || !selector.IsObject)
|
|
31
|
+
throw new HostException("BAD_ARGUMENT", "缺少 selector(至少给 page 或 deviceTag)。");
|
|
32
|
+
|
|
33
|
+
bool dryRun = args["dryRun"].AsBool(false);
|
|
34
|
+
JsonValue get = args["get"];
|
|
35
|
+
JsonValue set = args["set"];
|
|
36
|
+
|
|
37
|
+
List<ObjectInfo> targets = Resolve(selector);
|
|
38
|
+
if (targets.Count == 0)
|
|
39
|
+
throw new HostException("OBJECT_NOT_FOUND",
|
|
40
|
+
"没有找到匹配的对象:selector = " + Json.Write(selector));
|
|
41
|
+
|
|
42
|
+
JsonValue result = JsonValue.NewObject();
|
|
43
|
+
result.Set("matched", targets.Count);
|
|
44
|
+
result.Set("dryRun", dryRun);
|
|
45
|
+
|
|
46
|
+
JsonValue rows = JsonValue.NewArray();
|
|
47
|
+
foreach (ObjectInfo info in targets)
|
|
48
|
+
{
|
|
49
|
+
JsonValue row = JsonValue.NewObject();
|
|
50
|
+
row.Set("kind", info.Kind);
|
|
51
|
+
row.Set("type", info.TypeName);
|
|
52
|
+
if (info.Name != null) row.Set("name", info.Name);
|
|
53
|
+
if (info.Page != null) row.Set("page", info.Page);
|
|
54
|
+
|
|
55
|
+
if (get != null && get.IsArray && get.Count > 0)
|
|
56
|
+
{
|
|
57
|
+
JsonValue values = JsonValue.NewObject();
|
|
58
|
+
for (int i = 0; i < get.Count; i++)
|
|
59
|
+
{
|
|
60
|
+
string key = get[i].AsString(null);
|
|
61
|
+
if (string.IsNullOrEmpty(key)) continue;
|
|
62
|
+
values.Set(key, ReadProperty(info, key));
|
|
63
|
+
}
|
|
64
|
+
row.Set("values", values);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (set != null && set.IsObject)
|
|
68
|
+
{
|
|
69
|
+
JsonValue applied = JsonValue.NewArray();
|
|
70
|
+
JsonValue failed = JsonValue.NewArray();
|
|
71
|
+
foreach (string key in set.Keys)
|
|
72
|
+
{
|
|
73
|
+
string value = set[key].AsString(null);
|
|
74
|
+
if (dryRun)
|
|
75
|
+
{
|
|
76
|
+
applied.Add(JsonValue.NewString(key + " = " + value + "(未写入,dryRun)"));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
try
|
|
80
|
+
{
|
|
81
|
+
WriteProperty(info, key, value);
|
|
82
|
+
applied.Add(JsonValue.NewString(key));
|
|
83
|
+
}
|
|
84
|
+
catch (Exception ex)
|
|
85
|
+
{
|
|
86
|
+
failed.Add(JsonValue.NewString(key + ":" + ex.Message));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
row.Set("applied", applied);
|
|
90
|
+
if (failed.Count > 0) row.Set("failed", failed);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
rows.Add(row);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
result.Set("objects", rows);
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------
|
|
101
|
+
// 定位对象
|
|
102
|
+
// ---------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
private class ObjectInfo
|
|
105
|
+
{
|
|
106
|
+
public string Kind; // page / function / project
|
|
107
|
+
public string TypeName;
|
|
108
|
+
public string Name;
|
|
109
|
+
public string Page;
|
|
110
|
+
public object Target;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private static List<ObjectInfo> Resolve(JsonValue selector)
|
|
114
|
+
{
|
|
115
|
+
List<ObjectInfo> found = new List<ObjectInfo>();
|
|
116
|
+
|
|
117
|
+
// selector 里可以带 project,和顶层参数同义;写操作要能指定目标项目。
|
|
118
|
+
string projectName = selector["project"].AsString(null);
|
|
119
|
+
|
|
120
|
+
string pageName = selector["page"].AsString(null);
|
|
121
|
+
string deviceTag = selector["deviceTag"].AsString(null);
|
|
122
|
+
|
|
123
|
+
if (pageName != null)
|
|
124
|
+
{
|
|
125
|
+
Page page = Context.FindPage(pageName, projectName);
|
|
126
|
+
ObjectInfo info = new ObjectInfo();
|
|
127
|
+
info.Kind = "page";
|
|
128
|
+
info.TypeName = page.GetType().Name;
|
|
129
|
+
info.Name = page.Name;
|
|
130
|
+
info.Page = page.Name;
|
|
131
|
+
info.Target = page;
|
|
132
|
+
found.Add(info);
|
|
133
|
+
|
|
134
|
+
if (deviceTag != null)
|
|
135
|
+
{
|
|
136
|
+
// 在该页里按设备标识找 Function。
|
|
137
|
+
foreach (Function function in page.Functions)
|
|
138
|
+
{
|
|
139
|
+
if (string.Equals(function.Name, deviceTag, StringComparison.OrdinalIgnoreCase))
|
|
140
|
+
{
|
|
141
|
+
found.Add(FunctionInfo(function, page.Name));
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return found;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (deviceTag != null)
|
|
150
|
+
{
|
|
151
|
+
Project project = Context.Resolve(projectName);
|
|
152
|
+
foreach (Page page in project.Pages)
|
|
153
|
+
{
|
|
154
|
+
Function[] functions = page.Functions;
|
|
155
|
+
if (functions == null) continue;
|
|
156
|
+
foreach (Function function in functions)
|
|
157
|
+
{
|
|
158
|
+
if (string.Equals(function.Name, deviceTag, StringComparison.OrdinalIgnoreCase))
|
|
159
|
+
found.Add(FunctionInfo(function, page.Name));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return found;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 只给了 objectId 或什么都没给:退到当前项目。
|
|
166
|
+
Project current = Context.Resolve(projectName);
|
|
167
|
+
ObjectInfo projectInfo = new ObjectInfo();
|
|
168
|
+
projectInfo.Kind = "project";
|
|
169
|
+
projectInfo.TypeName = current.GetType().Name;
|
|
170
|
+
projectInfo.Name = current.ProjectName;
|
|
171
|
+
projectInfo.Target = current;
|
|
172
|
+
found.Add(projectInfo);
|
|
173
|
+
return found;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private static ObjectInfo FunctionInfo(Function function, string pageName)
|
|
177
|
+
{
|
|
178
|
+
ObjectInfo info = new ObjectInfo();
|
|
179
|
+
info.Kind = "function";
|
|
180
|
+
info.TypeName = function.GetType().Name;
|
|
181
|
+
info.Name = function.Name;
|
|
182
|
+
info.Page = pageName;
|
|
183
|
+
info.Target = function;
|
|
184
|
+
return info;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ---------------------------------------------------------------
|
|
188
|
+
// 属性读写
|
|
189
|
+
// ---------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
private static JsonValue ReadProperty(ObjectInfo info, string key)
|
|
192
|
+
{
|
|
193
|
+
UniversalPropertyList list = PropertyListOf(info);
|
|
194
|
+
if (list == null) return JsonValue.NewString("(该对象没有属性表)");
|
|
195
|
+
|
|
196
|
+
try
|
|
197
|
+
{
|
|
198
|
+
object anyId = ResolvePropertyId(info, key);
|
|
199
|
+
if (anyId != null)
|
|
200
|
+
{
|
|
201
|
+
PropertyValue value = list[(AnyPropertyId)anyId];
|
|
202
|
+
if (value == null || value.IsEmpty) return JsonValue.NewNull();
|
|
203
|
+
return JsonValue.NewString(value.ToString());
|
|
204
|
+
}
|
|
205
|
+
PropertyValue byName = list[key];
|
|
206
|
+
if (byName == null || byName.IsEmpty) return JsonValue.NewNull();
|
|
207
|
+
return JsonValue.NewString(byName.ToString());
|
|
208
|
+
}
|
|
209
|
+
catch (Exception ex)
|
|
210
|
+
{
|
|
211
|
+
return JsonValue.NewString("(读取失败: " + ex.Message + ")");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private static void WriteProperty(ObjectInfo info, string key, string value)
|
|
216
|
+
{
|
|
217
|
+
UniversalPropertyList list = PropertyListOf(info);
|
|
218
|
+
if (list == null) throw new HostException("NOT_WRITABLE", "该对象没有属性表。");
|
|
219
|
+
|
|
220
|
+
object anyId = ResolvePropertyId(info, key);
|
|
221
|
+
if (anyId != null)
|
|
222
|
+
{
|
|
223
|
+
list[(AnyPropertyId)anyId] = value;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
list[key] = value;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private static UniversalPropertyList PropertyListOf(ObjectInfo info)
|
|
230
|
+
{
|
|
231
|
+
if (info.Kind == "page") return ((Page)info.Target).Properties;
|
|
232
|
+
if (info.Kind == "function") return ((FunctionBase)info.Target).Properties;
|
|
233
|
+
if (info.Kind == "project") return ((Project)info.Target).Properties;
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// <summary>
|
|
238
|
+
/// 把 "FUNC_COMMENT" 这样的名字解析成 AnyPropertyId。
|
|
239
|
+
/// EPLAN 的静态属性(Properties.Function.FUNC_COMMENT)本身就是 AnyPropertyId 的子类,
|
|
240
|
+
/// 反射取它们的值即可。
|
|
241
|
+
/// </summary>
|
|
242
|
+
private static object ResolvePropertyId(ObjectInfo info, string key)
|
|
243
|
+
{
|
|
244
|
+
Assembly assembly = typeof(Properties).Assembly;
|
|
245
|
+
foreach (string typeName in KnownLists)
|
|
246
|
+
{
|
|
247
|
+
Type type = assembly.GetType(typeName);
|
|
248
|
+
if (type == null) continue;
|
|
249
|
+
PropertyInfo property = type.GetProperty(key, BindingFlags.Public | BindingFlags.Static);
|
|
250
|
+
if (property == null) continue;
|
|
251
|
+
object value = property.GetValue(null, null);
|
|
252
|
+
if (value is AnyPropertyId) return value;
|
|
253
|
+
}
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// status:任何绘图之前的第一件事。
|
|
2
|
+
// 只读,不修改项目。
|
|
3
|
+
using System;
|
|
4
|
+
using System.IO;
|
|
5
|
+
using Eplan.EplApi.Base;
|
|
6
|
+
using Eplan.EplApi.DataModel;
|
|
7
|
+
using Eplan.EplApi.DataModel.MasterData;
|
|
8
|
+
using EplanMcp;
|
|
9
|
+
|
|
10
|
+
namespace EplanMcpHost
|
|
11
|
+
{
|
|
12
|
+
internal static class StatusTool
|
|
13
|
+
{
|
|
14
|
+
public static JsonValue Run(JsonValue args)
|
|
15
|
+
{
|
|
16
|
+
JsonValue result = JsonValue.NewObject();
|
|
17
|
+
result.Set("unit", "mm");
|
|
18
|
+
result.Set(
|
|
19
|
+
"coordinateSystem",
|
|
20
|
+
"原点在页面左下角,X 向右、Y 向上,单位 mm");
|
|
21
|
+
|
|
22
|
+
// 让模型知道「用户现在到底看得见哪一页」。
|
|
23
|
+
// 数据改对了但界面停在别的页,对用户来说等于没做,这一点必须可查询。
|
|
24
|
+
try
|
|
25
|
+
{
|
|
26
|
+
string title = System.Diagnostics.Process.GetCurrentProcess().MainWindowTitle;
|
|
27
|
+
result.Set("windowTitle", title);
|
|
28
|
+
result.Set("visiblePage", PageFromTitle(title));
|
|
29
|
+
}
|
|
30
|
+
catch { }
|
|
31
|
+
|
|
32
|
+
ProjectManager manager = new ProjectManager();
|
|
33
|
+
Project project = manager.CurrentProject;
|
|
34
|
+
|
|
35
|
+
JsonValue open = JsonValue.NewArray();
|
|
36
|
+
Project[] projects = manager.OpenProjects;
|
|
37
|
+
if (projects != null)
|
|
38
|
+
{
|
|
39
|
+
foreach (Project p in projects)
|
|
40
|
+
{
|
|
41
|
+
JsonValue item = JsonValue.NewObject();
|
|
42
|
+
item.Set("name", p.ProjectName);
|
|
43
|
+
item.Set("path", p.ProjectLinkFilePath);
|
|
44
|
+
item.Set("isCurrent", project != null && ProjectNameEquals(p, project));
|
|
45
|
+
item.Set("isReadOnly", p.IsReadOnly);
|
|
46
|
+
open.Add(item);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
result.Set("openProjects", open);
|
|
50
|
+
|
|
51
|
+
if (project == null)
|
|
52
|
+
{
|
|
53
|
+
result.Set("ready", false);
|
|
54
|
+
result.Set("currentProject", (string)null);
|
|
55
|
+
result.Set("hint", "EPLAN 里还没有打开的项目,先打开一个再绘图。");
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
JsonValue current = JsonValue.NewObject();
|
|
60
|
+
current.Set("name", project.ProjectName);
|
|
61
|
+
current.Set("path", project.ProjectLinkFilePath);
|
|
62
|
+
current.Set("directory", project.ProjectDirectoryPath);
|
|
63
|
+
current.Set("isReadOnly", project.IsReadOnly);
|
|
64
|
+
current.Set("isLocked", project.IsLocked);
|
|
65
|
+
current.Set("pageCount", project.Pages == null ? 0 : project.Pages.Length);
|
|
66
|
+
|
|
67
|
+
JsonValue libraries = JsonValue.NewArray();
|
|
68
|
+
if (project.SymbolLibraries != null)
|
|
69
|
+
{
|
|
70
|
+
foreach (SymbolLibrary library in project.SymbolLibraries)
|
|
71
|
+
{
|
|
72
|
+
JsonValue item = JsonValue.NewObject();
|
|
73
|
+
item.Set("name", library.Name);
|
|
74
|
+
libraries.Add(item);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
current.Set("symbolLibraries", libraries);
|
|
78
|
+
result.Set("currentProject", current);
|
|
79
|
+
result.Set("ready", true);
|
|
80
|
+
AppendLibraries(result);
|
|
81
|
+
AppendSkills(result);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
/// <summary>从 EPLAN 主窗口标题里取当前显示的那一页;取不到返回 null。</summary>
|
|
87
|
+
private static string PageFromTitle(string title)
|
|
88
|
+
{
|
|
89
|
+
if (string.IsNullOrEmpty(title)) return null;
|
|
90
|
+
int index = title.LastIndexOf(" - ", StringComparison.Ordinal);
|
|
91
|
+
if (index < 0 || index + 3 >= title.Length) return null;
|
|
92
|
+
string page = title.Substring(index + 3).Trim();
|
|
93
|
+
if (page.Length == 0) return null;
|
|
94
|
+
|
|
95
|
+
// 项目刚打开、还没切到具体页时,标题后半段是**项目路径**而不是页名。
|
|
96
|
+
// 不认出来的话 visiblePage 会报一条 "C:\...\test",模型会以为用户正看着那一页。
|
|
97
|
+
// 页名的结构标识一定带 "/",而路径带反斜杠 —— 用这个区分,不猜。
|
|
98
|
+
if (page.IndexOf('\\') >= 0 || page.IndexOf('/') < 0) return null;
|
|
99
|
+
|
|
100
|
+
return page;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private static bool ProjectNameEquals(Project a, Project b)
|
|
104
|
+
{
|
|
105
|
+
return string.Equals(a.ProjectName, b.ProjectName, StringComparison.OrdinalIgnoreCase)
|
|
106
|
+
&& string.Equals(a.ProjectLinkFilePath, b.ProjectLinkFilePath, StringComparison.OrdinalIgnoreCase);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
// ---------------------------------------------------------------
|
|
111
|
+
// 有哪些符号库,让宿主自己说。
|
|
112
|
+
//
|
|
113
|
+
// 库名写错是新手最常犯的错:GB_symbol 里没有 T 节点,SPECIAL 里没有熔断器,
|
|
114
|
+
// 拿错库只会得到一句「找不到符号」。把这个列表给出去,模型就不用猜了。
|
|
115
|
+
// ---------------------------------------------------------------
|
|
116
|
+
private static void AppendLibraries(JsonValue result)
|
|
117
|
+
{
|
|
118
|
+
try
|
|
119
|
+
{
|
|
120
|
+
ProjectManager manager = new ProjectManager();
|
|
121
|
+
Project project = manager.CurrentProject;
|
|
122
|
+
if (project == null) return;
|
|
123
|
+
|
|
124
|
+
JsonValue names = JsonValue.NewArray();
|
|
125
|
+
SymbolLibrary[] libraries = project.SymbolLibraries;
|
|
126
|
+
if (libraries != null)
|
|
127
|
+
{
|
|
128
|
+
foreach (SymbolLibrary library in libraries)
|
|
129
|
+
{
|
|
130
|
+
if (library == null) continue;
|
|
131
|
+
names.Add(JsonValue.NewString(library.Name));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
result.Set("symbolLibraries", names);
|
|
135
|
+
}
|
|
136
|
+
catch { }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------
|
|
140
|
+
// 知识文件在哪,要由宿主自己报,不能让模型猜。
|
|
141
|
+
//
|
|
142
|
+
// 人设里写的是「去读 skills/xxx/SKILL.md」,但安装目录每台机器不一样;
|
|
143
|
+
// 写死路径换个地方就失效,让模型去找又纯属浪费往返。
|
|
144
|
+
// 宿主的 DLL 就在 <安装目录>\native\host\bin\ 下,往上三层就是 bundle 根。
|
|
145
|
+
// ---------------------------------------------------------------
|
|
146
|
+
private static void AppendSkills(JsonValue result)
|
|
147
|
+
{
|
|
148
|
+
try
|
|
149
|
+
{
|
|
150
|
+
string hostDir = Path.GetDirectoryName(typeof(StatusTool).Assembly.Location);
|
|
151
|
+
if (string.IsNullOrEmpty(hostDir)) return;
|
|
152
|
+
|
|
153
|
+
string root = Path.GetFullPath(Path.Combine(hostDir, "..", "..", ".."));
|
|
154
|
+
string skillsDir = Path.Combine(root, "skills");
|
|
155
|
+
result.Set("skillsDir", skillsDir);
|
|
156
|
+
if (!Directory.Exists(skillsDir)) return;
|
|
157
|
+
|
|
158
|
+
JsonValue found = JsonValue.NewArray();
|
|
159
|
+
string[] files = Directory.GetFiles(skillsDir, "SKILL.md", SearchOption.AllDirectories);
|
|
160
|
+
Array.Sort(files);
|
|
161
|
+
foreach (string file in files) found.Add(JsonValue.NewString(file));
|
|
162
|
+
result.Set("skills", found);
|
|
163
|
+
}
|
|
164
|
+
catch { }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
}
|