71 lines
3.8 KiB
C#
71 lines
3.8 KiB
C#
using PLCSharp.VVMs.Cylinders;
|
|
using PLCSharp.VVMs.MotionController;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace PLCSharp.Models;
|
|
|
|
public sealed partial class ProjectDirectory
|
|
{
|
|
private static string[] CylinderFields(string kind) => kind switch
|
|
{
|
|
"DI" => ["InA", "InB"], "DQ" => ["OutA", "OutB"],
|
|
_ => throw new ArgumentException("请选择 DI 或 DQ。")
|
|
};
|
|
|
|
private void CheckIoName(string kind, Guid id, string before, string after, bool inverse)
|
|
{
|
|
if ((!inverse && string.IsNullOrWhiteSpace(after)) || after != after?.Trim() || before == after)
|
|
throw new ArgumentException("请输入不同的新名称,首尾不能有空白。");
|
|
var rows = kind == "DI" ? Data.DI.Select(p => new { p.ID, p.Name }) : Data.DQ.Select(p => new { p.ID, p.Name });
|
|
if (!rows.Any(p => p.ID == id && p.Name == before) || rows.Any(p => p.ID != id &&
|
|
((before != null && before != "" && p.Name == before) || (after != null && after != "" && p.Name == after))))
|
|
throw new ProjectConflictException("通道已更改或同类通道存在重名。");
|
|
}
|
|
|
|
internal ProjectRename PlanIoRename(string kind, Guid id, string newName)
|
|
{
|
|
var fields = CylinderFields(kind);
|
|
CheckSavedRecipes();
|
|
var hardware = JsonNode.Parse(baseline[ConfigPath("hardware")]);
|
|
var old = (string)new PointField(kind, id, "Name").Row(hardware)["Name"];
|
|
CheckIoName(kind, id, old, newName, false);
|
|
var links = new List<PointField>();
|
|
foreach (var row in hardware["Cylinders"].AsArray())
|
|
foreach (var field in fields)
|
|
{
|
|
var name = (string)row[field];
|
|
if (name == newName) throw new ProjectConflictException($"气缸 {row["Name"]} 的 {field} 已引用新名称。");
|
|
if (!string.IsNullOrEmpty(old) && name == old)
|
|
links.Add(new("Cylinders", Guid.Parse((string)row["ID"]), field));
|
|
}
|
|
return PlanReferences(kind, id, Guid.Empty, old, newName) with { CylinderReferences = links };
|
|
}
|
|
|
|
internal Action PrepareIoView(ProjectRename plan, ControllersModel controllers, CylinderModel cylinders)
|
|
{
|
|
IEnumerable<object> channels = plan.Kind == "DI" ? controllers.DI : controllers.DQ;
|
|
var entries = channels.Select(c => Data.Entry(c)).ToList();
|
|
var target = entries.SingleOrDefault(e => (Guid)e.Property("ID").CurrentValue == plan.ObjectId);
|
|
if (target == null || (string)target.Property("Name").CurrentValue != plan.OldName ||
|
|
entries.Any(e => e != target && e.Property("Name").CurrentValue is string name && name.Length > 0 &&
|
|
(name == plan.OldName || name == plan.NewName)))
|
|
throw new ProjectConflictException("编辑器中的通道名称已改变,请先保存。");
|
|
|
|
var links = plan.CylinderReferences.ToHashSet();
|
|
var updates = new List<Action> { () => target.Property("Name").CurrentValue = plan.NewName };
|
|
foreach (var cylinder in cylinders.Cylinders)
|
|
foreach (var field in CylinderFields(plan.Kind))
|
|
{
|
|
var property = Data.Entry(cylinder).Property(field);
|
|
var name = (string)property.CurrentValue;
|
|
bool referencesOld = !string.IsNullOrEmpty(name) && name == plan.OldName;
|
|
if ((!string.IsNullOrEmpty(name) && name == plan.NewName) ||
|
|
links.Remove(new("Cylinders", cylinder.ID, field)) != referencesOld)
|
|
throw new ProjectConflictException("编辑器中的气缸绑定已改变,请先保存。");
|
|
if (referencesOld) updates.Add(() => property.CurrentValue = plan.NewName);
|
|
}
|
|
if (links.Count != 0) throw new ProjectConflictException("气缸列表已改变,请先保存。");
|
|
return () => updates.ForEach(update => update());
|
|
}
|
|
}
|