feat: add Variables dictionary to Program with deep-copy in Clone
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using OpenNest.Converters;
|
using OpenNest.Converters;
|
||||||
using OpenNest.Geometry;
|
using OpenNest.Geometry;
|
||||||
using OpenNest.Math;
|
using OpenNest.Math;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenNest.CNC
|
namespace OpenNest.CNC
|
||||||
@@ -9,6 +10,8 @@ namespace OpenNest.CNC
|
|||||||
{
|
{
|
||||||
public List<ICode> Codes;
|
public List<ICode> Codes;
|
||||||
|
|
||||||
|
public Dictionary<string, VariableDefinition> Variables { get; } = new(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
private Mode mode;
|
private Mode mode;
|
||||||
|
|
||||||
public Program(Mode mode = Mode.Absolute)
|
public Program(Mode mode = Mode.Absolute)
|
||||||
@@ -454,6 +457,9 @@ namespace OpenNest.CNC
|
|||||||
|
|
||||||
pgm.Codes.AddRange(codes);
|
pgm.Codes.AddRange(codes);
|
||||||
|
|
||||||
|
foreach (var kvp in Variables)
|
||||||
|
pgm.Variables[kvp.Key] = kvp.Value;
|
||||||
|
|
||||||
return pgm;
|
return pgm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,4 +101,35 @@ public class ProgramVariableTests
|
|||||||
move.Offset(5.0, 0);
|
move.Offset(5.0, 0);
|
||||||
Assert.Null(move.VariableRefs);
|
Assert.Null(move.VariableRefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Program_Variables_EmptyByDefault()
|
||||||
|
{
|
||||||
|
var pgm = new Program();
|
||||||
|
Assert.Empty(pgm.Variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Program_Variables_CaseInsensitive()
|
||||||
|
{
|
||||||
|
var pgm = new Program();
|
||||||
|
pgm.Variables["Diameter"] = new VariableDefinition("Diameter", "0.3", 0.3);
|
||||||
|
Assert.True(pgm.Variables.ContainsKey("diameter"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Program_Clone_DeepCopiesVariables()
|
||||||
|
{
|
||||||
|
var pgm = new Program();
|
||||||
|
pgm.Variables["diameter"] = new VariableDefinition("diameter", "0.3", 0.3);
|
||||||
|
pgm.Codes.Add(new LinearMove(1.0, 0));
|
||||||
|
|
||||||
|
var clone = (Program)pgm.Clone();
|
||||||
|
|
||||||
|
Assert.Single(clone.Variables);
|
||||||
|
Assert.Equal(0.3, clone.Variables["diameter"].Value);
|
||||||
|
// Verify it's a separate dictionary
|
||||||
|
clone.Variables.Remove("diameter");
|
||||||
|
Assert.Single(pgm.Variables);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user