Synchronize remaining reviewed object changes with the WPF editor
This commit is contained in:
parent
bcd8c02b85
commit
c32188fddf
@ -7,7 +7,7 @@ namespace PLCSharp.Mcp;
|
||||
[McpServerToolType]
|
||||
internal sealed class ProjectTools(ProjectGateway gateway)
|
||||
{
|
||||
[McpServerTool(Name = "preview_object_edit"), Description("Preview create, update or delete for Variables, AxisPoints, RobotPoints or hardware (Controllers, Axes, DI, DQ, Cylinders, Robots, Connects, Cameras). Use persisted JSON column names. Create generates an ID; pass RecipeID for scoped objects and RobotID for robot points. Update/delete require id. Objects in properties merge recursively, arrays replace. Review workflow/config references, dynamic references and compile results before apply_changes. Use preview_undo to reverse.")]
|
||||
[McpServerTool(Name = "preview_object_edit"), Description("Preview create, update or delete for Recipes, Workflows, Variables, SystemVariables, AxisPoints, RobotPoints, Matrices, RobotMatrices, InterpolationGroups, VisionFunctions, ImageDatas, LocalImageDatas, CanvasConfigs, CustomControls or hardware (Controllers, Axes, DI, DQ, Cylinders, Robots, Connects, Cameras). Use persisted JSON column names. Create generates an ID; pass RecipeID for scoped objects and RobotID for robot points/matrices and FuncID for local images. Workflow Code uses __WORKFLOW_ID__ for the generated class name suffix. Deletion previews list owned records and affected files. Update/delete require id. Objects in properties merge recursively, arrays replace. Review workflow/config references, dynamic references and compile results before apply_changes. Use preview_undo to reverse.")]
|
||||
public string PreviewObjectEdit(string action, string kind, string id = null, JsonObject properties = null) =>
|
||||
gateway.Call("previewObjectEdit", new() { ["action"] = action, ["kind"] = kind, ["id"] = id, ["properties"] = properties?.DeepClone() });
|
||||
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
using System.Collections;
|
||||
using PLCSharp.Core.Prism;
|
||||
using PLCSharp.VVMs.Vision;
|
||||
using PLCSharp.VVMs.Homepage;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PLCSharp.VVMs.Cylinders;
|
||||
@ -17,18 +20,37 @@ public partial class GlobalModel
|
||||
{
|
||||
var row = edit.After ?? edit.Before;
|
||||
if (row["RecipeID"] != null && Guid.Parse((string)row["RecipeID"]) != CurrentRecipe.ID) return null;
|
||||
Robot robot = edit.Kind == "RobotPoints" ? RobotModel.Robots.SingleOrDefault(r => r.ID == Guid.Parse((string)row["RobotID"])) : null;
|
||||
if (edit.Kind == "Recipes") return () =>
|
||||
{
|
||||
if (edit.After == null && RecipeSelected?.ID == edit.Id) RecipeSelected = null;
|
||||
if (edit.After == null && CurrentRecipe.ID == edit.Id)
|
||||
{
|
||||
CurrentRecipe = Recipes.Single(r => r.ID == Project.DefaultRecipeId);
|
||||
foreach (var r in Recipes) r.Current = r.ID == CurrentRecipe.ID;
|
||||
LoadRecipe();
|
||||
}
|
||||
};
|
||||
if (edit.Kind == "Workflows") return () =>
|
||||
{
|
||||
if (edit.Action == "create") Project.Flows(CurrentRecipe.ID).Single(f => f.ID == edit.Id).Start(this);
|
||||
if (edit.Action == "delete" && WorkflowsModel.SelectedTask?.ID == edit.Id) WorkflowsModel.SelectedTask = null;
|
||||
};
|
||||
Robot robot = edit.Kind is "RobotPoints" or "RobotMatrices" ? RobotModel.Robots.SingleOrDefault(r => r.ID == Guid.Parse((string)row["RobotID"])) : null;
|
||||
var controller = ControllersModel.Controller;
|
||||
var entity = ProjectConfiguration.Entity(Project.Data, edit.Kind);
|
||||
var key = entity.FindPrimaryKey().Properties.Single().PropertyInfo;
|
||||
IList list = edit.Kind switch
|
||||
{
|
||||
"Variables" => VariablesModel.Variables, "AxisPoints" => ControllersModel.AxisPoints,
|
||||
"SystemVariables" => VariablesModel.SystemVariables, "Matrices" => ControllersModel.Matrices,
|
||||
"RobotMatrices" => robot?.RobotMatrices, "InterpolationGroups" => ControllersModel.InterpolationGroups,
|
||||
"ImageDatas" => VisionsModel.ImageDatas, "VisionFunctions" => VisionsModel.VisionFunctions,
|
||||
"LocalImageDatas" => VisionsModel.VisionFunctions.SingleOrDefault(f => f.ID == Guid.Parse((string)row["FuncID"]))?.LocalImageDatas,
|
||||
"CustomControls" => CustomControls, "Variables" => VariablesModel.Variables, "AxisPoints" => ControllersModel.AxisPoints,
|
||||
"RobotPoints" => robot?.Points, "Axes" => ControllersModel.Axes, "DI" => ControllersModel.DI,
|
||||
"DQ" => ControllersModel.DQ, "Robots" => RobotModel.Robots, "Cylinders" => CylindersModel.Cylinders,
|
||||
"Cameras" => VisionsModel.Cameras, "Connects" => Connects.Connects, _ => null
|
||||
};
|
||||
object live = edit.Kind == "Controllers" ? controller : list?.Cast<object>().SingleOrDefault(o => (Guid)key.GetValue(o) == edit.Id);
|
||||
object live = edit.Kind == "Controllers" ? controller : edit.Kind == "CanvasConfigs" && CurrentCanvasConfig?.ID == edit.Id ? CurrentCanvasConfig : list?.Cast<object>().SingleOrDefault(o => (Guid)key.GetValue(o) == edit.Id);
|
||||
var properties = entity.GetProperties().Where(p => edit.Before == null || edit.After == null ||
|
||||
!JsonNode.DeepEquals(edit.Before[p.GetColumnName()], edit.After[p.GetColumnName()])).ToArray();
|
||||
if (edit.Before != null && live != null)
|
||||
@ -66,6 +88,12 @@ public partial class GlobalModel
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (edit.Kind == "CanvasConfigs")
|
||||
{
|
||||
CurrentCanvasConfig = next == null ? new CanvasConfig { RecipeID = CurrentRecipe.ID } : Project.Data.CanvasConfigs.Find(edit.Id);
|
||||
_EventAggregator.GetEvent<MessageEvent>().Publish(new() { Target = "UIReLoad" });
|
||||
return;
|
||||
}
|
||||
if (list == null) return; // A saved robot point may belong to a robot without a configured driver.
|
||||
if (edit.Kind == "Connects")
|
||||
{
|
||||
@ -88,10 +116,11 @@ public partial class GlobalModel
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (edit.Kind == "Variables" || edit.Kind == "Cylinders")
|
||||
if (edit.Kind is "Variables" or "Cylinders" or "SystemVariables" or "LocalImageDatas")
|
||||
{
|
||||
var tracked = next == null ? null : Project.Data.Find(entity.ClrType, edit.Id);
|
||||
if (tracked is Variable variable) variable.RestoreValue(Project.Data);
|
||||
if (tracked is SystemVariable system) system.RestoreValue(Project.Data);
|
||||
if (tracked == null) Remove();
|
||||
else if (live == null) list.Add(tracked);
|
||||
if (edit.Kind == "Variables" && next == null && VariablesModel.SelectedVariable == live) VariablesModel.SelectedVariable = null;
|
||||
@ -100,6 +129,8 @@ public partial class GlobalModel
|
||||
IList channels = edit.Kind switch { "Axes" => controller.Axes, "DI" => controller.DI, "DQ" => controller.DQ, _ => null };
|
||||
if (next == null)
|
||||
{
|
||||
if (live != null && VisionsModel.SelectedVisionFunction == live) VisionsModel.SelectedVisionFunction = null;
|
||||
if (live != null && SelectedCustomControl == live) SelectedCustomControl = null;
|
||||
Remove();
|
||||
if (live != null) channels?.Remove(live);
|
||||
if (live != null && ControllersModel.SelectedAxis == live) ControllersModel.SelectedAxis = null;
|
||||
@ -110,10 +141,25 @@ public partial class GlobalModel
|
||||
else if (live == null)
|
||||
{
|
||||
live = next is CameraBase camera ? VisionsModel.ReviewedCamera(camera) : next;
|
||||
if (live is VisionFunction function)
|
||||
{
|
||||
function.VisionsModel = VisionsModel;
|
||||
function.GlobalModel = this;
|
||||
function.ImageDatas = VisionsModel.ImageDatas;
|
||||
foreach (var local in Project.Data.LocalImageDatas.Where(l => l.FuncID == function.ID)) function.LocalImageDatas.Add(local);
|
||||
}
|
||||
list.Add(live);
|
||||
channels?.Add(live);
|
||||
}
|
||||
else ApplyFields(live);
|
||||
if (next != null && live is Matrix matrixItem) ControllersModel.Create(matrixItem);
|
||||
if (next != null && live is RobotMatrix robotMatrix) robot?.Create(robotMatrix);
|
||||
if (next != null && live is InterpolationGroup group)
|
||||
{
|
||||
group.AxisX = ControllersModel.Axes.FirstOrDefault(axis => axis.Name == group.AxisXName);
|
||||
group.AxisY = ControllersModel.Axes.FirstOrDefault(axis => axis.Name == group.AxisYName);
|
||||
}
|
||||
if (edit.Kind == "CustomControls") _EventAggregator.GetEvent<MessageEvent>().Publish(new() { Target = "UIReLoad" });
|
||||
if (edit.Kind is "AxisPoints" or "Axes") ControllersModel.RefreshReviewedAxes(edit.Kind, edit.Id, (string)row["Name"]);
|
||||
if (edit.Kind == "RobotPoints")
|
||||
foreach (var matrix in robot.RobotMatrices.Where(m => m.StartName == (string)row["Name"] || m.XEndName == (string)row["Name"] || m.YEndName == (string)row["Name"]))
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using MiniExcelLibs;
|
||||
using MiniExcelLibs;
|
||||
using PLCSharp.Core.Prism;
|
||||
using PLCSharp.Core.Tools;
|
||||
using PLCSharp.VVMs.GlobalVariables;
|
||||
@ -788,7 +788,6 @@ namespace PLCSharp.Models
|
||||
if (CurrentCanvasConfig == null)
|
||||
{
|
||||
CurrentCanvasConfig = new CanvasConfig() { RecipeID = CurrentRecipe.ID };
|
||||
_DatasContext.Add(CurrentCanvasConfig);
|
||||
}
|
||||
|
||||
var currRecipeCustomControls = _DatasContext.CustomControls.Where(c => c.RecipeID == CurrentRecipe.ID);
|
||||
|
||||
@ -21,6 +21,8 @@ public partial class ScriptReviewWindow : Window
|
||||
foreach (var link in edit.ConfigReferences) lines.Add($"配置 {link["path"]} · {link["table"]}/{link["id"]} · {link["field"]}");
|
||||
foreach (var hit in edit.ManualReferences) lines.Add($"待核对 [{hit["confidence"]}] {hit["path"]}:{hit["line"]} {hit["expression"]}");
|
||||
if (edit.Action == "delete" && lines.Count > 0) lines.Add("确认删除将保留上列引用,请核对删除后的影响。");
|
||||
foreach (var owned in edit.OwnedRows) lines.Add($"{(owned.Restore ? "恢复" : "随对象删除")} {owned.Kind} · {owned.Name} · {owned.Id}");
|
||||
foreach (var path in edit.ChangedFiles) lines.Add($"文件 {path}");
|
||||
foreach (var key in (edit.Before ?? edit.After).Select(k => k.Key))
|
||||
if (!System.Text.Json.Nodes.JsonNode.DeepEquals(edit.Before?[key], edit.After?[key]))
|
||||
lines.Add($"{key}:{edit.Before?[key]} → {edit.After?[key]}");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user