plcsharp/PLCSharpWpf/Models/AxisRename.cs

107 lines
5.1 KiB
C#

using PLCSharp.VVMs.MotionController;
using System.Text.Json.Nodes;
namespace PLCSharp.Models;
public sealed partial class ProjectDirectory
{
private static readonly Dictionary<string, string[]> AxisNameFields = new()
{
["AxisPoints"] = ["AxisXName", "AxisYName", "AxisZName", "AxisUName"],
["InterpolationGroups"] = ["AxisXName", "AxisYName"]
};
private void CheckAxisName(Guid id, string before, string after)
{
if (string.IsNullOrWhiteSpace(before) || string.IsNullOrWhiteSpace(after) || after != after.Trim() || before == after)
throw new ArgumentException("请输入不同的新轴名,首尾不能有空白。");
if (!Data.Axes.Any(a => a.ID == id && a.Name == before) ||
Data.Axes.Any(a => a.ID != id && (a.Name == before || a.Name == after)))
throw new ProjectConflictException("轴已改变或工程内存在重名。");
}
private static List<PointField> AxisLinks(JsonNode document, string before, string after)
{
var links = new List<PointField>();
foreach (var (table, fields) in AxisNameFields)
foreach (var row in document[table].AsArray())
foreach (var field in fields)
{
var value = (string)row[field];
if (value == after) throw new ProjectConflictException($"点位配置中已有对新轴名的引用:{table}/{row["ID"]}.{field}");
if (value == before) links.Add(new(table, Guid.Parse((string)row["ID"]), field));
}
return links;
}
internal ProjectRename PlanAxisRename(Guid id, string newName)
{
var manifest = JsonNode.Parse(baseline["project.json"]);
var savedRecipes = manifest["recipes"].AsArray().Select(r => Guid.Parse((string)r)).ToHashSet();
if (!savedRecipes.SetEquals(Recipes.Select(r => r.ID)))
throw new ProjectConflictException("请先保存配方列表,再预览工程轴重命名。");
var axis = Data.Axes.Single(a => a.ID == id);
CheckAxisName(id, axis.Name, newName);
var points = baseline[ConfigPath("points")];
return PlanReferences("Axes", id, Guid.Empty, axis.Name, newName) with
{
OriginalPoints = points,
PointReferences = AxisLinks(JsonNode.Parse(points), axis.Name, newName)
};
}
private static void UpdateAxisPointsDocument(ProjectRename plan, Dictionary<string, string> files)
{
var path = ConfigPath("points");
if (files[path] != plan.OriginalPoints) throw new ProjectConflictException("点位配置已改变,请重新预览。");
if (plan.PointReferences.Count == 0) return;
var document = JsonNode.Parse(files[path]);
foreach (var reference in plan.PointReferences)
reference.Row(document)[reference.Field] = plan.NewName;
files[path] = plan.UndoOf.HasValue ? plan.SavedPoints : Serialize(document);
}
internal Action PrepareAxisView(ProjectRename plan, ControllersModel model, Guid recipe)
{
var axis = model.Axes.SingleOrDefault(a => a.ID == plan.ObjectId);
if (axis == null || axis.Name != plan.OldName ||
model.Axes.Any(a => a.ID != axis.ID && (a.Name == plan.OldName || a.Name == plan.NewName)))
throw new ProjectConflictException("编辑器运动轴列表已改变,请先保存。");
var document = JsonNode.Parse(plan.OriginalPoints);
var expected = plan.PointReferences.Where(f => Guid.Parse((string)f.Row(document)["RecipeID"]) == recipe).ToHashSet();
var updates = new List<Action>();
void CheckRows(string table, IEnumerable<object> rows)
{
foreach (var row in rows)
{
var entry = Data.Entry(row);
var id = (Guid)entry.Property("ID").CurrentValue;
foreach (var field in AxisNameFields[table])
{
var property = entry.Property(field);
var value = (string)property.CurrentValue;
if (value == plan.NewName || expected.Remove(new(table, id, field)) != (value == plan.OldName))
throw new ProjectConflictException("编辑器中的轴名引用已改变,请先保存。");
if (value == plan.OldName) updates.Add(() => property.CurrentValue = plan.NewName);
}
}
}
CheckRows("AxisPoints", model.AxisPoints);
CheckRows("InterpolationGroups", model.InterpolationGroups);
if (expected.Count != 0) throw new ProjectConflictException("编辑器中的点位或插补组已改变,请先保存。");
return () =>
{
axis.Name = plan.NewName;
foreach (var update in updates) update();
foreach (var p in model.Matrices.SelectMany(m => m.Points))
{
if (p.AxisX?.ID == axis.ID) p.AxisXName = plan.NewName;
if (p.AxisY?.ID == axis.ID) p.AxisYName = plan.NewName;
if (p.AxisZ?.ID == axis.ID) p.AxisZName = plan.NewName;
if (p.AxisU?.ID == axis.ID) p.AxisUName = plan.NewName;
}
};
}
}