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:
2026-04-02 09:58:36 -04:00
parent 95b9613e2d
commit 27afa04e4a
2 changed files with 37 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
using OpenNest.Converters;
using OpenNest.Geometry;
using OpenNest.Math;
using System;
using System.Collections.Generic;
namespace OpenNest.CNC
@@ -9,6 +10,8 @@ namespace OpenNest.CNC
{
public List<ICode> Codes;
public Dictionary<string, VariableDefinition> Variables { get; } = new(StringComparer.OrdinalIgnoreCase);
private Mode mode;
public Program(Mode mode = Mode.Absolute)
@@ -454,6 +457,9 @@ namespace OpenNest.CNC
pgm.Codes.AddRange(codes);
foreach (var kvp in Variables)
pgm.Variables[kvp.Key] = kvp.Value;
return pgm;
}

View File

@@ -101,4 +101,35 @@ public class ProgramVariableTests
move.Offset(5.0, 0);
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);
}
}