diff --git a/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs b/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs index efb90f82..1ebbf35e 100644 --- a/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs +++ b/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using OpenNest.Converters; using OpenNest.Geometry; using OpenNest.Math; @@ -630,9 +631,15 @@ namespace OpenNest.CNC.CuttingStrategy Parameters.GetEffectivePierceClearance(), out leadOutNormal); } - program.Codes.AddRange(leadIn.Generate(point, leadInNormal, turn)); + var leadInCodes = leadIn.Generate(point, leadInNormal, turn); + program.Codes.AddRange(leadInCodes); program.Codes.AddRange(ConvertShapeToMoves(reindexedShape, point)); var leadOutCodes = leadOut.Generate(leadOutPoint, leadOutNormal, turn); + // A run-out angled toward the tab can reach the lead-in on the tab's far side, and the + // two cuts then sever the tab: the cut stops at its tab instead. + if (tabbed && Parameters.TabConfig.Size > Tolerance.Epsilon + && LeadsMeet(leadInCodes, point, leadOutCodes, leadOutPoint, Parameters.Kerf)) + leadOutCodes = []; program.Codes.AddRange(leadOutCodes); return contourType != ContourType.External && leadOutCodes.Count > 0; } @@ -1050,6 +1057,60 @@ namespace OpenNest.CNC.CuttingStrategy return true; } + /// + /// Whether a tabbed outside's lead-out, leaving from , + /// touches its lead-in, which enters at , or passes within + /// of it. Either way the two cuts close across the tab gap + /// and cut the part free. + /// + private static bool LeadsMeet(List leadInCodes, Vector entry, List leadOutCodes, + Vector leadOutStart, double kerf) + { + var leadIn = LeadEntities(leadInCodes, entry); + var leadOut = LeadEntities(leadOutCodes, leadOutStart); + if (leadIn.Count == 0 || leadOut.Count == 0) + return false; + var reach = double.IsFinite(kerf) && kerf > 0 ? kerf : 0; + foreach (var cut in leadOut) + { + foreach (var lead in leadIn) + { + if (Crosses(cut, lead) || reach > 0 && (Near(cut, lead) || Near(lead, cut))) + return true; + } + } + return false; + + static bool Crosses(Entity a, Entity b) => b switch + { + Line line => a.Intersects(line), + Arc arc => a.Intersects(arc), + _ => false, + }; + + bool Near(Entity a, Entity b) => + Ends(a).Any(end => b.ClosestPointTo(end).DistanceTo(end) < reach - Tolerance.Epsilon); + } + + // A lead's cutting motions as entities; a lead-in starts at its own rapid to the pierce. + private static List LeadEntities(List codes, Vector start) + { + var program = new Program(Mode.Absolute); + if (codes.Count == 0 || codes[0] is not RapidMove) + program.Codes.Add(new RapidMove(start)); + program.Codes.AddRange(codes); + return ConvertProgram.ToGeometry(program) + .Where(e => e.Layer != SpecialLayers.Rapid && e is Line or Arc) + .ToList(); + } + + private static IEnumerable Ends(Entity entity) => entity switch + { + Line line => [line.StartPoint, line.EndPoint], + Arc arc => [arc.StartPoint(), arc.EndPoint()], + _ => [], + }; + private static Vector EntityEndPoint(Entity entity) { if (entity is Line line) diff --git a/OpenNest.Tests/CuttingPlanning/HoleLookAheadTests.cs b/OpenNest.Tests/CuttingPlanning/HoleLookAheadTests.cs index 56ba7539..b9a260e9 100644 --- a/OpenNest.Tests/CuttingPlanning/HoleLookAheadTests.cs +++ b/OpenNest.Tests/CuttingPlanning/HoleLookAheadTests.cs @@ -156,7 +156,7 @@ public class HoleLookAheadTests [Theory] [InlineData(false)] [InlineData(true)] - public void HoleProgramsRetainScribesOnceAndNeverInstallOnFailure(bool tabs) + public void HoleProgramsRetainScribesOnceAndNeverInstall(bool tabs) { var clean = MixedHoles(); clean.MoveTo(-2, -2); @@ -169,21 +169,14 @@ public class HoleLookAheadTests var fingerprint = ExplicitContourTests.Fingerprint(part.Program); var result = CuttingPlanService.Plan(new CuttingPlanRequest([part], Start, confirmedParameters: parameters, preservePartOrder: true)); - if (tabs) - { - Assert.NotEqual(CuttingPlanStatus.Ready, result.Status); - Assert.Empty(result.ProposedOrder); - Assert.Contains(result.Findings, f => f.Kind == PostVerificationKind.Incomplete); - } - else - { - Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result)); - Assert.True(result.IndependentlyReplayed); - var actual = result.ProposedOrder[0]; - Assert.Equal(2, actual.ContourChoices[^1].ContourOrdinal); - Assert.Equal(3, actual.ContourChoices.Select(c => c.ContourOrdinal).Distinct().Count()); - Assert.Single(actual.Execution.Motions.Where(m => !m.Rapid && m.Layer == LayerType.Scribe)); - } + Assert.True(result.Status == CuttingPlanStatus.Ready, Describe(result)); + Assert.True(result.IndependentlyReplayed); + var actual = result.ProposedOrder[0]; + Assert.Equal(2, actual.ContourChoices[^1].ContourOrdinal); + Assert.Equal(3, actual.ContourChoices.Select(c => c.ContourOrdinal).Distinct().Count()); + Assert.Single(actual.Execution.Motions.Where(m => !m.Rapid && m.Layer == LayerType.Scribe)); + // The arc lead-out would cut through the tab into the lead-in, so a tabbed outside has none. + Assert.Equal(!tabs, actual.Execution.Motions.Any(m => m.Layer == LayerType.Leadout)); Assert.Equal(fingerprint, ExplicitContourTests.Fingerprint(part.Program)); } diff --git a/OpenNest.Tests/CuttingPlanning/JointCuttingPlanTests.cs b/OpenNest.Tests/CuttingPlanning/JointCuttingPlanTests.cs index cefef4b8..795e3cbb 100644 --- a/OpenNest.Tests/CuttingPlanning/JointCuttingPlanTests.cs +++ b/OpenNest.Tests/CuttingPlanning/JointCuttingPlanTests.cs @@ -315,18 +315,31 @@ public class JointCuttingPlanTests } [Fact] - public void TabbedArcCandidate_UncertifiedTabGap_IsRefusedWithoutRepairOrException() + public void TabbedArcCandidate_LeadOutThatWouldCutTheTab_IsLeftOutAndPlanned() { - // The arc lead-out is now well formed; the open tab gap is still not certified. + // The arc lead-out would run over the tab gap into the lead-in, so the cut stops at its tab. var parameters = ExplicitContourTests.Parameters(); parameters.TabsEnabled = true; parameters.TabConfig = new NormalTab { Size = 0.2 }; parameters.ExternalLeadOut = new ArcLeadOut { Radius = 0.2 }; var part = new Part(new Drawing("tabbed", LeadPathValidationTests.Rectangle(0, 0, 10, 10))); var result = CuttingPlanService.Plan(new CuttingPlanRequest([part], new Vector(-2, 5), confirmedParameters: parameters)); - Assert.Equal(CuttingPlanStatus.NoSolutionWithinBudget, result.Status); - Assert.Empty(result.ProposedOrder); Assert.True(result.Expansions > 0); - Assert.Contains(result.Findings, f => f.Kind == PostVerificationKind.Incomplete); - Assert.Contains(result.Findings, f => f.Message.Contains("retention gap")); + Assert.Equal(CuttingPlanStatus.Ready, result.Status); + Assert.True(result.IndependentlyReplayed); + Assert.DoesNotContain(Assert.Single(result.ProposedOrder).Execution.Motions, m => m.Layer == LayerType.Leadout); + } + + [Fact] + public void TabbedPerimeter_LeadOutCrossingItsLeadIn_IsRefusedAsCuttingTheRetentionGap() + { + // Wound counterclockwise, entered at (0, 5) and stopped 0.2 short of it: the lead-out runs + // forward over the tab gap and crosses the lead-in, cutting the tab free. + var program = new Program(Mode.Absolute); + program.Codes.Add(new RapidMove(-0.3, 5.5)); + program.Codes.Add(new LinearMove(0, 5) { Layer = LayerType.Leadin }); + program.LineTo(0, 0); program.LineTo(10, 0); program.LineTo(10, 10); program.LineTo(0, 10); program.LineTo(0, 5.2); + program.Codes.Add(new LinearMove(-0.3, 4.9) { Layer = LayerType.Leadout }); + var findings = new ReleasedContourState().Check(Read(program, Vector.Zero), Vector.Zero, 1); + Assert.Contains(findings, f => f.Kind == PostVerificationKind.Incomplete && f.Message.Contains("retention gap")); } [Fact] diff --git a/OpenNest.Tests/CuttingStrategy/TabbedLeadOutTests.cs b/OpenNest.Tests/CuttingStrategy/TabbedLeadOutTests.cs index ca0cce90..5c13583b 100644 --- a/OpenNest.Tests/CuttingStrategy/TabbedLeadOutTests.cs +++ b/OpenNest.Tests/CuttingStrategy/TabbedLeadOutTests.cs @@ -9,19 +9,18 @@ namespace OpenNest.Tests.CuttingStrategy; /// /// A tab trims the perimeter short of its entry, so the lead-out leaves from where the /// trimmed cut actually ends. Generating it from the nominal entry emitted an arc whose -/// start was off its own radius. +/// start was off its own radius. A lead-out that would reach the lead-in across the tab gap +/// would cut the tab free, so the cut then stops at its tab with no lead-out. /// public class TabbedLeadOutTests { private const double TabSize = 0.2; private const double LeadRadius = 0.2; - [Theory] - [InlineData(false)] - [InlineData(true)] - public void ArcLeadOut_LeavesTangentlyFromTheTrimmedCutEnd(bool reversed) + [Fact] + public void ArcLeadOut_LeavesTangentlyFromTheTrimmedCutEnd() { - var execution = Emit(ExplicitContourTests.Square(reversed), new Vector(-2, 5), + var execution = Emit(ExplicitContourTests.Square(false), new Vector(-2, 5), new ArcLeadOut { Radius = LeadRadius }, tabs: true); var (cuts, leadout) = Split(execution); @@ -31,11 +30,65 @@ public class TabbedLeadOutTests Assert.Equal(end, leadout.Start!.Value); var arc = Assert.IsType(leadout.Curve.ToEntity()); Assert.Equal(LeadRadius, arc.Radius, 9); - // Centerd on the actual end's outward normal, so it touches the edge only where it starts. + // Centered on the actual end's outward normal, so it touches the edge only where it starts. Assert.Equal(-LeadRadius, arc.Center.X, 9); Assert.Equal(end.Y, arc.Center.Y, 9); } + [Fact] + public void ArcLeadOut_ThatWouldCrossTheLeadIn_IsLeftOut() + { + // Wound the other way, the arc runs forward over the tab gap and through the lead-in. + var execution = Emit(ExplicitContourTests.Square(true), new Vector(-2, 5), + new ArcLeadOut { Radius = LeadRadius }, tabs: true); + + var cuts = execution.Motions.Where(m => m.Layer == LayerType.Display && !m.Rapid).ToArray(); + Assert.Equal(TabSize, System.Math.Abs(cuts[^1].End.Y - 5), 9); + Assert.DoesNotContain(execution.Motions, m => m.Layer == LayerType.Leadout); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void LineLeadOut_AngledIntoASmallTab_IsLeftOutOnlyWhenItWouldCrossTheLeadIn(bool reversed) + { + // A strip tabbed for support with 60 degree leads and a tab shorter than the lead-out's + // forward run: wound one way the lead-out angles away from the tab, the other way it + // crosses the lead-in and would cut the tab free. + var clean = new Program(Mode.Absolute); + if (reversed) + { + clean.MoveTo(0, 0); clean.LineTo(11.8, 0); clean.LineTo(11.8, 3.8); clean.LineTo(0, 3.8); clean.LineTo(0, 0); + } + else + { + clean.MoveTo(0, 0); clean.LineTo(0, 3.8); clean.LineTo(11.8, 3.8); clean.LineTo(11.8, 0); clean.LineTo(0, 0); + } + var parameters = new CuttingParameters + { + Kerf = 0.014, + ExternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 }, + ExternalLeadOut = new LineLeadOut { Length = 0.1, ApproachAngle = 60 }, + InternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 }, + InternalLeadOut = new NoLeadOut(), + TabsEnabled = true, + TabConfig = new NormalTab { Size = 0.03 }, + }; + var emitted = new ContourCuttingStrategy { Parameters = parameters }.Apply(clean, new Vector(5, -2)); + var execution = ExecutionMotionReader.Read(emitted.Program, Vector.Zero, null, default); + + var leadIn = Assert.Single(execution.Motions.Where(m => m.Layer == LayerType.Leadin)); + var leadOuts = execution.Motions.Where(m => m.Layer == LayerType.Leadout).ToArray(); + if (reversed) + Assert.Empty(leadOuts); + else + { + var leadOut = Assert.Single(leadOuts); + Assert.False(new Line(leadIn.Start!.Value, leadIn.End).Intersects(new Line(leadOut.Start!.Value, leadOut.End))); + } + Assert.Empty(new ReleasedContourState().Check(execution, new Vector(5, -2), 1)); + } + [Fact] public void ArcLeadOut_OnCurvedPerimeter_IsTangentAtTheTrimmedEnd() { @@ -84,30 +137,20 @@ public class TabbedLeadOutTests Assert.Equal(5, arc.Center.Y, 9); } - [Theory] - [InlineData(false)] - [InlineData(true)] - public void LineLeads_TabWrappingAFilletedCorner_ShareTheCornerBisector(bool reversed) + [Fact] + public void LineLeads_TabWrappingAFilletedCorner_ShareTheCornerBisector() { // Entered at the fillet joint the tab trims back around: on its own edge's normal the // lead-in and lead-out would sit 150 degrees apart, not the 60 degree V of a straight edge. const double fillet = 0.015; const double tab = 0.03; var clean = new Program(Mode.Absolute); - if (reversed) - { - clean.MoveTo(fillet, 10); clean.ArcTo(0, 10 - fillet, fillet, 10 - fillet, RotationType.CCW); - clean.LineTo(0, 0); clean.LineTo(10, 0); clean.LineTo(10, 10); clean.LineTo(fillet, 10); - } - else - { - clean.MoveTo(0, 0); clean.LineTo(0, 10 - fillet); - clean.ArcTo(fillet, 10, fillet, 10 - fillet, RotationType.CW); - clean.LineTo(10, 10); clean.LineTo(10, 0); clean.LineTo(0, 0); - } + clean.MoveTo(0, 0); clean.LineTo(0, 10 - fillet); + clean.ArcTo(fillet, 10, fillet, 10 - fillet, RotationType.CW); + clean.LineTo(10, 10); clean.LineTo(10, 0); clean.LineTo(0, 0); var part = new Part(new Drawing("tabbed-fillet", clean)); var perimeter = LeadInTestHelpers.Profile(part).Perimeter; - var entry = reversed ? new Vector(0, 10 - fillet) : new Vector(fillet, 10); + var entry = new Vector(fillet, 10); var entity = perimeter.Entities.OfType().First(l => l.StartPoint.DistanceTo(entry) < 1e-9); var parameters = new CuttingParameters { @@ -125,7 +168,7 @@ public class TabbedLeadOutTests LeadInTestHelpers.AssertPoint(entry, leadIn.EndPoint); Assert.Equal(tab, leadOut.StartPoint.DistanceTo(entry), 9); // The cut stops on the other edge, past the corner. - Assert.Equal(0, reversed ? 10 - leadOut.StartPoint.Y : leadOut.StartPoint.X, 9); + Assert.Equal(0, leadOut.StartPoint.X, 9); const double bisector = 3 * System.Math.PI / 4; var spread = OpenNest.Math.Angle.ToRadians(30); @@ -133,6 +176,36 @@ public class TabbedLeadOutTests LeadInTestHelpers.AssertPoint(leadOut.StartPoint + Polar(0.1, bisector + spread), leadOut.EndPoint); } + [Fact] + public void LineLeads_TabWrappingAFilletedCornerTheOtherWay_LeaveOutTheLeadOutThatCrossesTheLeadIn() + { + // Wound the other way, the shared bisector puts the lead-out across the lead-in. + const double fillet = 0.015; + var clean = new Program(Mode.Absolute); + clean.MoveTo(fillet, 10); clean.ArcTo(0, 10 - fillet, fillet, 10 - fillet, RotationType.CCW); + clean.LineTo(0, 0); clean.LineTo(10, 0); clean.LineTo(10, 10); clean.LineTo(fillet, 10); + var part = new Part(new Drawing("tabbed-fillet", clean)); + var perimeter = LeadInTestHelpers.Profile(part).Perimeter; + var entry = new Vector(0, 10 - fillet); + var entity = perimeter.Entities.OfType().First(l => l.StartPoint.DistanceTo(entry) < 1e-9); + var parameters = new CuttingParameters + { + ExternalLeadIn = new LineLeadIn { Length = 0.15, ApproachAngle = 60 }, + ExternalLeadOut = new LineLeadOut { Length = 0.1, ApproachAngle = 60 }, + TabsEnabled = true, + TabConfig = new NormalTab { Size = 0.03 }, + }; + + part.ApplySingleLeadIn(parameters, entry, entity, ContourType.External); + + var geometry = part.Program.ToGeometry(); + var leadIn = Assert.IsType(Assert.Single(geometry.Where(e => e.Layer == SpecialLayers.Leadin))); + LeadInTestHelpers.AssertPoint(entry, leadIn.EndPoint); + LeadInTestHelpers.AssertPoint(entry + Polar(0.15, 3 * System.Math.PI / 4 - OpenNest.Math.Angle.ToRadians(30)), + leadIn.StartPoint); + Assert.DoesNotContain(geometry, e => e.Layer == SpecialLayers.Leadout); + } + private static Vector Polar(double length, double angle) => new(length * System.Math.Cos(angle), length * System.Math.Sin(angle)); diff --git a/docs/support-tabs.md b/docs/support-tabs.md index dc4e38b9..fa206ddb 100644 --- a/docs/support-tabs.md +++ b/docs/support-tabs.md @@ -104,6 +104,11 @@ plate settings, so parts that need no tab are not tabbed. The emitter tabs a perimeter at its entry: the cut stops short of the entry by the tab size. A round perimeter, one circle, is cut as an arc that stops the tab's chord short of its entry. +The lead-out leaves from where the cut stops. One that touches the lead-in, or passes within the +kerf of it, would cut the tab free, so the emitter leaves it out and the cut ends at its tab. A +line lead-out angled forward does that whenever its run along the edge is longer than the tab, +as a 0.1 lead-out at 60 degrees (0.05 along the edge) beside a 0.03 tab does. This holds for every +tabbed outside, support-tabbed or not. A support-tabbed perimeter's automatic entries are therefore limited to catalog points the analysis accepts, plus the closest native point to its best tab point (`AutomaticEntryKind.SupportTab`, never on a reflex or cusp vertex). Ranking and lead checks are unchanged.