plcsharp/PLCSharpWpf/Models/ObjectRename.cs

52 lines
2.7 KiB
C#

using System.Text.Json.Nodes;
namespace PLCSharp.Models;
public sealed partial class ProjectDirectory
{
internal ProjectRename PlanObjectRename(string kind, Guid id, string name) => kind switch
{
"Variables" => PlanRename(id, name),
"Workflows" => PlanWorkflowRename(id, name),
"VisionFunctions" => PlanVisionRename(id, name),
"Axes" => PlanAxisRename(id, name),
"AxisPoints" or "RobotPoints" => PlanPointRename(kind, id, name),
"DI" or "DQ" => PlanIoRename(kind, id, name),
"Cylinders" or "Robots" or "Connects" => PlanDeviceRename(kind, id, name),
"Cameras" or "ImageDatas" or "LocalImageDatas" or "SystemVariables" or "CustomControls" => PlanVisionAssetRename(kind, id, name),
"Recipes" or "Matrices" or "RobotMatrices" or "InterpolationGroups" => PlanAdditionalRename(kind, id, name),
_ => throw new ArgumentException($"对象类型 {kind} 没有可修改的名称。")
};
private ProjectRename PlanAdditionalRename(string kind, Guid id, string name)
{
CheckSavedRecipes();
var row = ProjectInspection.Query(this, "get", new() { ["kind"] = kind, ["id"] = id.ToString() });
string before = (string)row["Name"];
CheckAdditionalName(kind, id, before, name);
Guid recipe = kind == "Recipes" ? id : Guid.Parse((string)row["RecipeID"]);
return PlanReferences(kind, id, recipe, before, name) with
{
RobotScope = kind == "RobotMatrices" ? baseline[ConfigPath("hardware")] : null
};
}
private void CheckAdditionalName(string kind, Guid id, string before, string after, bool inverse = false)
{
if ((!inverse && string.IsNullOrWhiteSpace(after)) || after != after?.Trim() || before == after)
throw new ArgumentException("新名称不能为空、重复或带首尾空白。");
if (kind == "Recipes")
{
CheckSavedRecipes();
if (Recipes.Single(r => r.ID == id).Name != before || Recipes.Any(r => r.ID != id && (r.Name == before || r.Name == after)))
throw new ProjectConflictException("配方名称已改变或存在重名。");
return;
}
var rows = ProjectConfiguration.Capture(Data, [kind], false)[kind].AsArray();
var target = rows.Single(r => Guid.Parse((string)r["ID"]) == id);
if ((string)target["Name"] != before || rows.Any(r => r != target && SamePointScope(target, r) &&
(!string.IsNullOrEmpty(before) && (string)r["Name"] == before || !string.IsNullOrEmpty(after) && (string)r["Name"] == after)))
throw new ProjectConflictException("配置名称已改变或在所属范围内重名。");
}
}