feat: emit SubProgramCalls for circle holes in ContourCuttingStrategy

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-09 14:35:56 -04:00
parent c641b3b68e
commit 4aed231611
2 changed files with 125 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
using OpenNest.CNC;
using OpenNest.CNC.CuttingStrategy;
using OpenNest.Geometry;
using System.Linq;
namespace OpenNest.Tests.CuttingStrategy;
@@ -76,4 +78,84 @@ public class HoleSubProgramTests
Assert.NotSame(sub, clone.SubPrograms[1]);
Assert.Equal(Mode.Incremental, clone.SubPrograms[1].Mode);
}
[Fact]
public void Apply_CircleHole_EmitsSubProgramCall()
{
// Create a program with a square perimeter and a circle hole at (5, 5) radius 0.5
var pgm = new Program(Mode.Absolute);
// Square perimeter
pgm.Codes.Add(new RapidMove(0, 0));
pgm.Codes.Add(new LinearMove(0, 10));
pgm.Codes.Add(new LinearMove(10, 10));
pgm.Codes.Add(new LinearMove(10, 0));
pgm.Codes.Add(new LinearMove(0, 0));
// Circle hole at (5, 5) radius 0.5
pgm.Codes.Add(new RapidMove(5.5, 5));
pgm.Codes.Add(new ArcMove(new Vector(5.5, 5), new Vector(5, 5), RotationType.CW));
var strategy = new ContourCuttingStrategy
{
Parameters = new CuttingParameters
{
ArcCircleLeadIn = new LineLeadIn { Length = 0.125, ApproachAngle = 90 },
ArcCircleLeadOut = new NoLeadOut()
}
};
var result = strategy.Apply(pgm, new Vector(10, 10));
// Should contain at least one SubProgramCall
var calls = result.Program.Codes.OfType<SubProgramCall>().ToList();
Assert.Single(calls);
// The call's offset should be approximately at the hole center (5, 5)
var call = calls[0];
Assert.Equal(5, call.Offset.X, 1);
Assert.Equal(5, call.Offset.Y, 1);
// The parent program should have a sub-program registered
Assert.True(result.Program.SubPrograms.ContainsKey(call.Id));
}
[Fact]
public void Apply_TwoIdenticalCircles_ShareSubProgram()
{
// Square perimeter with two identical circle holes at different positions
var pgm = new Program(Mode.Absolute);
// Square perimeter
pgm.Codes.Add(new RapidMove(0, 0));
pgm.Codes.Add(new LinearMove(0, 10));
pgm.Codes.Add(new LinearMove(10, 10));
pgm.Codes.Add(new LinearMove(10, 0));
pgm.Codes.Add(new LinearMove(0, 0));
// Circle 1 at (2, 2) radius 0.5
pgm.Codes.Add(new RapidMove(2.5, 2));
pgm.Codes.Add(new ArcMove(new Vector(2.5, 2), new Vector(2, 2), RotationType.CW));
// Circle 2 at (6, 6) radius 0.5
pgm.Codes.Add(new RapidMove(6.5, 6));
pgm.Codes.Add(new ArcMove(new Vector(6.5, 6), new Vector(6, 6), RotationType.CW));
var strategy = new ContourCuttingStrategy
{
Parameters = new CuttingParameters
{
RoundLeadInAngles = true,
LeadInAngleIncrement = 5.0,
ArcCircleLeadIn = new LineLeadIn { Length = 0.125, ApproachAngle = 90 },
ArcCircleLeadOut = new NoLeadOut()
}
};
var result = strategy.Apply(pgm, new Vector(10, 10));
var calls = result.Program.Codes.OfType<SubProgramCall>().ToList();
Assert.Equal(2, calls.Count);
// Both calls should reference the same sub-program ID (same radius, same quantized angle)
Assert.Equal(calls[0].Id, calls[1].Id);
// But different offsets
Assert.NotEqual(calls[0].Offset.X, calls[1].Offset.X);
}
}