140 lines
7.8 KiB
C#
140 lines
7.8 KiB
C#
using PLCSharp.Models;
|
|
using PLCSharp.VVMs.GlobalVariables;
|
|
using PLCSharp.VVMs.Homepage;
|
|
using PLCSharp.VVMs.MotionController;
|
|
using PLCSharp.VVMs.Robots;
|
|
using PLCSharp.VVMs.Vision;
|
|
using PLCSharp.VVMs.Workflows;
|
|
using System.IO;
|
|
|
|
namespace PLCSharp.Tests;
|
|
|
|
public class ObjectRenameTests
|
|
{
|
|
[Fact]
|
|
public void NestedMembersChangeOnlyTheirOwnerAndReverseExactly()
|
|
{
|
|
var root = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var project = ProjectDirectory.New();
|
|
var recipe = project.Recipes.Single();
|
|
var function = new VisionFunction { RecipeID = recipe.ID, Name = "Inspect" };
|
|
function.Params.Variables.Add(new LocalVariableItem("Reading", "Double", 7.0));
|
|
var coord = new VisionFlow { Type = VisionFlowType.坐标转换 };
|
|
coord.StringParams["InputVar"] = "Reading";
|
|
function.VisionFlows.Add(coord);
|
|
var control = new CustomControl { RecipeID = recipe.ID, Name = "Table" };
|
|
control.RangeValues.Add(new() { Name = "Reading" });
|
|
var group = new InterpolationGroup { RecipeID = recipe.ID, Name = "Path" };
|
|
var segment = new Interpolation { Name = "Line" };
|
|
segment.InterpolationPoints.Add(new() { Name = "End" });
|
|
group.Interpolations.Add(segment);
|
|
project.Data.AddRange(function, control, group);
|
|
var flow = new Workflow { Name = "Inspect", RecipeID = recipe.ID };
|
|
flow.Code = $$"""
|
|
using PLCSharp.Models;
|
|
using PLCSharp.Core.Common;
|
|
using System.Linq;
|
|
public class Extension_{{flow.ID:N}} {
|
|
public void Run(GlobalModel model, FlowModel flow) {
|
|
_ = model.GetVisionFunction("Inspect").Params.Variables.First(v => v.Name == "Reading");
|
|
model.GetCustomControl("Table").SetMeasurementValue("Reading", 2);
|
|
model.GetCustomControl("Table").SetMeasurementValue(flow.Step.ToString(), 3);
|
|
_ = model.ControllersModel.InterpolationGroups.First(g => g.Name == "Path").Interpolations.First(s => s.Name == "Line").InterpolationPoints.First(p => p.Name == "End");
|
|
}
|
|
}
|
|
""";
|
|
project.Flows(recipe.ID).Add(flow);
|
|
project.Save(root.FullName);
|
|
var initial = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllBytes);
|
|
var history = new Stack<ProjectRename>();
|
|
foreach (var (kind, owner, member, parent) in new[] { ("VisionLocalVariables", function.ID, "Reading", ""),
|
|
("ControlMeasurements", control.ID, "Reading", ""), ("InterpolationSegments", group.ID, "Line", ""),
|
|
("InterpolationPoints", group.ID, "End", "InterpolationSegments") })
|
|
{
|
|
var plan = project.PlanObjectRename(kind, owner, kind, member, parent);
|
|
Assert.True(plan.Compiled);
|
|
Assert.Single(plan.Scripts);
|
|
if (kind == "ControlMeasurements") Assert.Single(plan.ManualReferences);
|
|
project.ApplyRename(plan);
|
|
Assert.Contains($"\"{kind}\"", flow.Code);
|
|
history.Push(plan);
|
|
}
|
|
Assert.Equal("VisionLocalVariables", coord.StringParams["InputVar"]);
|
|
Assert.Equal("InterpolationPoints", segment.InterpolationPoints[0].Name);
|
|
while (history.TryPop(out var saved)) project.ApplyRename(project.PlanUndo(Guid.NewGuid(), saved));
|
|
foreach (var (path, bytes) in initial) Assert.Equal(bytes, File.ReadAllBytes(path));
|
|
}
|
|
finally { root.Delete(true); }
|
|
}
|
|
|
|
[Fact]
|
|
public void SixRemainingKindsCompileSaveAndUndoAsOneSequence()
|
|
{
|
|
var root = Directory.CreateTempSubdirectory();
|
|
try
|
|
{
|
|
using var project = ProjectDirectory.New();
|
|
var recipe = project.Recipes.Single();
|
|
recipe.Name = "Product";
|
|
var other = new PLCSharp.VVMs.Recipe.Recipe { Name = "Other" };
|
|
project.Recipes.Add(other);
|
|
var robot = new Robot { Name = "Robot" };
|
|
var matrix = new Matrix { RecipeID = recipe.ID, Name = "Grid" };
|
|
var grid = new RobotMatrix { RecipeID = recipe.ID, RobotID = robot.ID, Name = "Grid" };
|
|
var group = new InterpolationGroup { RecipeID = recipe.ID, Name = "Path" };
|
|
var system = new SystemVariable { RecipeID = recipe.ID, Name = "Transform" };
|
|
var control = new CustomControl { RecipeID = recipe.ID, Name = "Display" };
|
|
var function = new VisionFunction { RecipeID = recipe.ID, Name = "Inspect", ControlName = "Display" };
|
|
var step = new VisionFlow { Type = VisionFlowType.坐标转换 };
|
|
step.StringParams["TransformMat"] = system.Name;
|
|
function.VisionFlows.Add(step);
|
|
var display = new VisionFlow { Type = VisionFlowType.显示图像到主页 };
|
|
display.StringParams["CustomControlName"] = control.Name;
|
|
function.VisionFlows.Add(display);
|
|
project.Data.AddRange(robot, matrix, grid, group, system, control, function);
|
|
var flow = new Workflow { RecipeID = recipe.ID, Name = "Check" };
|
|
flow.Code = $$"""
|
|
using System.Linq;
|
|
using PLCSharp.Models;
|
|
using PLCSharp.Core.Common;
|
|
public class Extension_{{flow.ID:N}} {
|
|
public void Run(GlobalModel model, FlowModel flow) {
|
|
_ = model.CurrentRecipe.Name == "Product";
|
|
_ = model.ControllersModel.Matrices.First(m => m.Name == "Grid");
|
|
_ = model.GetRobot("Robot").RobotMatrices.First(m => m.Name == "Grid");
|
|
_ = model.ControllersModel.InterpolationGroups.First(g => g.Name == "Path");
|
|
_ = model.VariablesModel.SystemVariables.First(v => v.Name == "Transform");
|
|
_ = model.GetCustomControl("Display");
|
|
_ = model.GetCustomControl(flow.Step.ToString());
|
|
}
|
|
}
|
|
""";
|
|
project.Flows(recipe.ID).Add(flow);
|
|
project.Save(root.FullName);
|
|
var initial = Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).ToDictionary(p => p, File.ReadAllBytes);
|
|
var history = new Stack<ProjectRename>();
|
|
Assert.ThrowsAny<IOException>(() => project.PlanObjectRename("Recipes", recipe.ID, other.Name));
|
|
foreach (var (kind, id) in new[] { ("Recipes", recipe.ID), ("Matrices", matrix.ID), ("RobotMatrices", grid.ID),
|
|
("InterpolationGroups", group.ID), ("SystemVariables", system.ID), ("CustomControls", control.ID) })
|
|
{
|
|
var plan = project.PlanObjectRename(kind, id, kind);
|
|
Assert.True(plan.Compiled);
|
|
Assert.Single(plan.Scripts);
|
|
if (kind == "CustomControls") Assert.Single(plan.ManualReferences);
|
|
project.ApplyRename(plan);
|
|
Assert.Contains($"\"{kind}\"", flow.Code);
|
|
history.Push(plan);
|
|
}
|
|
Assert.Equal("SystemVariables", step.StringParams["TransformMat"]);
|
|
Assert.Equal("CustomControls", function.ControlName);
|
|
Assert.Equal("CustomControls", display.StringParams["CustomControlName"]);
|
|
while (history.TryPop(out var saved)) project.ApplyRename(project.PlanUndo(Guid.NewGuid(), saved));
|
|
Assert.Equal(initial.Keys.Order(), Directory.GetFiles(root.FullName, "*", SearchOption.AllDirectories).Order());
|
|
foreach (var (path, bytes) in initial) Assert.Equal(bytes, File.ReadAllBytes(path));
|
|
}
|
|
finally { root.Delete(true); }
|
|
}
|
|
}
|