fix(io): restore hole sub-programs with negative IDs
This commit is contained in:
@@ -89,7 +89,7 @@ namespace OpenNest.IO
|
||||
private static void ReadSubPrograms(Program parent, Stream stream)
|
||||
{
|
||||
using var reader = new StreamReader(stream);
|
||||
var currentId = -1;
|
||||
var currentId = (int?)null;
|
||||
var lines = new List<string>();
|
||||
|
||||
string line;
|
||||
@@ -100,18 +100,18 @@ namespace OpenNest.IO
|
||||
if (trimmed.StartsWith(":") && int.TryParse(trimmed.Substring(1), out var id))
|
||||
{
|
||||
// Flush previous sub-program
|
||||
if (currentId >= 0 && lines.Count > 0)
|
||||
parent.SubPrograms[currentId] = ParseSubProgram(lines);
|
||||
if (currentId.HasValue && lines.Count > 0)
|
||||
parent.SubPrograms[currentId.Value] = ParseSubProgram(lines);
|
||||
|
||||
currentId = id;
|
||||
lines.Clear();
|
||||
}
|
||||
else if (trimmed == "M99")
|
||||
{
|
||||
if (currentId >= 0 && lines.Count > 0)
|
||||
parent.SubPrograms[currentId] = ParseSubProgram(lines);
|
||||
if (currentId.HasValue && lines.Count > 0)
|
||||
parent.SubPrograms[currentId.Value] = ParseSubProgram(lines);
|
||||
|
||||
currentId = -1;
|
||||
currentId = null;
|
||||
lines.Clear();
|
||||
}
|
||||
else
|
||||
|
||||
@@ -46,6 +46,27 @@ public class SubProgramSerializationTests
|
||||
Assert.True(drawing.Program.SubPrograms.ContainsKey(call.Id));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-42)]
|
||||
[InlineData(-1)]
|
||||
public void NegativeSubProgramId_RoundTripsAndBinds(int id)
|
||||
{
|
||||
var nest = CreateNestWithHoleSubProgram();
|
||||
var program = nest.Drawings.First().Program;
|
||||
var sub = program.SubPrograms[42];
|
||||
program.SubPrograms.Clear();
|
||||
program.SubPrograms[id] = sub;
|
||||
program.Codes.OfType<SubProgramCall>().Single().Id = id;
|
||||
using var stream = new MemoryStream();
|
||||
new NestWriter(nest).Write(stream);
|
||||
stream.Position = 0;
|
||||
|
||||
var restored = new NestReader(stream).Read().Drawings.First().Program;
|
||||
|
||||
Assert.True(restored.SubPrograms.ContainsKey(id));
|
||||
Assert.Same(restored.SubPrograms[id], restored.Codes.OfType<SubProgramCall>().Single().Program);
|
||||
}
|
||||
|
||||
private static Nest CreateNestWithHoleSubProgram()
|
||||
{
|
||||
var sub = new Program(Mode.Incremental);
|
||||
|
||||
Reference in New Issue
Block a user