Exercise point rename and undo on the portable WPF sample

This commit is contained in:
wanglei 2026-12-05 21:50:37 +08:00
parent c4f406e975
commit 534dee0427
2 changed files with 29 additions and 7 deletions

View File

@ -53,6 +53,8 @@
调用 `preview_workflow_rename(id, newName)` 可重命名配方中的工作流,并更新同一配方脚本中能确定的 `GetTask` 常量参数和直接查询 `WorkflowsModel.Workflows` 时的名称相等比较。请逐项检查 `ManualReferences` 中的运行时表达式。工作流 ID 和脚本文件路径保持稳定。
调用 `preview_point_rename(kind, id, newName)` 可重命名轴点位(`AxisPoints`)或机器人点位(`RobotPoints`)。预览会列出同一配方的脚本差异,以及矩阵的起点、X 端点、Y 端点引用(`MatrixReferences`)。轴点位支持直接查询 `ControllersModel.AxisPoints` 时的名称相等比较。机器人点位按所属机器人匹配,支持通过常量名称直接选择唯一机器人后的 `RunPoint`、`Jog` 和 `Points` 查询。运行时名称和无法判定所属机器人的调用会列入 `ManualReferences`,供人工核对。
核对后将草案 ID 交给 `apply_changes`,在 WPF 弹窗确认后一起保存名称和受影响的脚本。要撤销,把**已保存的重命名草案 ID** 传给 `preview_undo`,再对返回的**撤销草案 ID** 调用 `apply_changes` 并确认。变量 ID 始终保留。撤销记录保存在处理本次请求的编辑器或离线 MCP 服务会话中,沿用最近 256 个草案的缓存;关闭会话后记录失效。名称所在的 JSON 文件或该配方的脚本发生冲突时会停止写入。
## 界面展示

View File

@ -42,7 +42,18 @@ def prepare_workflow(sample, directory):
target_script.with_name(caller["id"] + ".cs").write_bytes(source.encode("utf-8"))
def verify(exe, directory, workflow=False):
def prepare_point(sample, directory):
shutil.copytree(sample, directory)
caller, = (p for p in directory.rglob("*.cs") if 'GetTask("搬运")' in p.read_text(encoding="utf-8"))
source = caller.read_text(encoding="utf-8").replace("using PLCSharp.Models;", "using PLCSharp.Models; using System.Linq;")
source = source.replace('_ = model.GetTask("搬运");',
'_ = model.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == "定位");')
source = source.replace('_ = model.GetTask(flow.Step.ToString());',
'_ = model.ControllersModel.AxisPoints.FirstOrDefault(p => p.Name == flow.Step.ToString());')
caller.write_bytes(source.encode("utf-8"))
def verify(exe, directory, workflow=False, point=False):
before = {p: p.read_bytes() for p in directory.rglob("*") if p.is_file()}
client = Client(exe, directory, "--allow-write")
try:
@ -52,23 +63,32 @@ def verify(exe, directory, workflow=False):
kind, old, new, tool, lookup = (
("Workflows", "搬运", "工件搬运", "preview_workflow_rename", "GetTask") if workflow else
("Variables", "计数", "完成计数", "preview_variable_rename", "GetVariable"))
if point:
kind, old, new, tool = "AxisPoints", "定位", "装配定位", "preview_point_rename"
items = client.call("list_objects", kind=kind, recipeId=recipe)["items"]
target, = (item for item in items if item["name"] == old)
draft = client.call(tool, id=target["id"], newName=new)
draft = client.call(tool, id=target["id"], newName=new, **({"kind": kind} if point else {}))
plan = draft["Rename"]
script, = plan["Scripts"]
manual, = plan["ManualReferences"]
assert draft["Status"] == "pending" and script["Review"]["Compiled"]
assert manual["confidence"] == "dynamic" and manual["line"] == 6
assert f'{lookup}("{new}")' in script["After"] and manual["expression"] in script["After"]
replacement = f'p.Name == "{new}"' if point else f'{lookup}("{new}")'
if point:
link, = plan["MatrixReferences"]
assert link["Table"] == "Matrices" and link["Field"] == "StartName"
assert replacement in script["After"] and manual["expression"] in script["After"]
print(json.dumps({"source": summary["source"], "dynamic": manual, "compiled": True}, ensure_ascii=False), flush=True)
assert all(p.read_bytes() == old for p, old in before.items())
saved = client.call("apply_changes", draftId=draft["DraftId"])
assert saved["Status"] == "saved"
assert client.call("get_object", kind=kind, id=target["id"])["Name"] == new
assert client.call("get_object", kind="Workflows", id=script["Id"])["Code"] == script["After"]
if point:
assert client.call("get_object", kind="Matrices", id=link["Id"])["StartName"] == new
metadata = draft["Path"]
assert {p for p, old in before.items() if p.read_bytes() != old} == {
directory / (f"recipes/{recipe}/recipe.json" if workflow else "configuration/variables.json"), directory / script["Path"]}
directory / metadata, directory / script["Path"]}
undo = client.call("preview_undo", draftId=draft["DraftId"])
assert undo["Rename"]["Compiled"] and undo["Rename"]["UndoOf"] == draft["DraftId"]
assert client.call("apply_changes", draftId=undo["DraftId"])["Status"] == "saved"
@ -83,12 +103,12 @@ def verify(exe, directory, workflow=False):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("mode", choices=["prepare", "verify", "prepare-workflow", "verify-workflow"])
parser.add_argument("mode", choices=["prepare", "verify", "prepare-workflow", "verify-workflow", "prepare-point", "verify-point"])
parser.add_argument("source", type=Path, help="Sample directory for prepare, MCP executable for verify")
parser.add_argument("project", type=Path)
args = parser.parse_args()
if args.mode.startswith("prepare"):
(prepare_workflow if args.mode == "prepare-workflow" else prepare)(args.source, args.project)
{"prepare": prepare, "prepare-workflow": prepare_workflow, "prepare-point": prepare_point}[args.mode](args.source, args.project)
else:
verify(args.source, args.project, workflow=args.mode == "verify-workflow")
verify(args.source, args.project, workflow=args.mode == "verify-workflow", point=args.mode == "verify-point")