feat: add SubPrograms dictionary to Program with deep-copy support

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 14:28:37 -04:00
parent c270d8ea76
commit f3b27c32c3
2 changed files with 41 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ namespace OpenNest.CNC
public Dictionary<string, VariableDefinition> Variables { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<int, Program> SubPrograms { get; } = new();
private Mode mode;
public Program(Mode mode = Mode.Absolute)
@@ -460,6 +462,9 @@ namespace OpenNest.CNC
foreach (var kvp in Variables)
pgm.Variables[kvp.Key] = kvp.Value;
foreach (var kvp in SubPrograms)
pgm.SubPrograms[kvp.Key] = (Program)kvp.Value.Clone();
return pgm;
}

View File

@@ -40,4 +40,40 @@ public class HoleSubProgramTests
Assert.Contains("X1.5", str);
Assert.Contains("Y2.5", str);
}
[Fact]
public void Program_SubPrograms_EmptyByDefault()
{
var pgm = new Program();
Assert.NotNull(pgm.SubPrograms);
Assert.Empty(pgm.SubPrograms);
}
[Fact]
public void Program_SubPrograms_StoresAndRetrieves()
{
var pgm = new Program();
var sub = new Program(Mode.Incremental);
sub.Codes.Add(new LinearMove(0.1, 0.2));
pgm.SubPrograms[1] = sub;
Assert.Single(pgm.SubPrograms);
Assert.Same(sub, pgm.SubPrograms[1]);
}
[Fact]
public void Program_Clone_DeepCopiesSubPrograms()
{
var pgm = new Program();
var sub = new Program(Mode.Incremental);
sub.Codes.Add(new LinearMove(0.1, 0.2));
pgm.SubPrograms[1] = sub;
var clone = (Program)pgm.Clone();
Assert.Single(clone.SubPrograms);
Assert.NotSame(sub, clone.SubPrograms[1]);
Assert.Equal(Mode.Incremental, clone.SubPrograms[1].Mode);
}
}