diff --git a/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs b/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs index 31866307..081f295a 100644 --- a/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs +++ b/OpenNest.Core/CNC/CuttingStrategy/ContourCuttingStrategy.cs @@ -507,8 +507,8 @@ namespace OpenNest.CNC.CuttingStrategy var winding = DetermineWinding(shape); var normal = ComputeNormal(point, entity, contourType, winding); - var leadIn = SelectLeadIn(contourType); - var leadOut = SelectLeadOut(contourType); + var leadIn = Parameters.LeadInFor(contourType); + var leadOut = Parameters.LeadOutFor(contourType); // A cutout the planner pierced by erosion is too small for its configured leads. if (scrapPierce != null && contourType != ContourType.External) leadOut = new NoLeadOut(); @@ -1497,24 +1497,6 @@ namespace OpenNest.CNC.CuttingStrategy private const int CenterLineRefinements = 16; private const double MinimumCenterLineRate = 0.01; - private LeadIn SelectLeadIn(ContourType contourType) - { - return contourType switch - { - ContourType.Internal or ContourType.ArcCircle => Parameters.InternalLeadIn, - _ => Parameters.ExternalLeadIn, - }; - } - - private LeadOut SelectLeadOut(ContourType contourType) - { - return contourType switch - { - ContourType.Internal or ContourType.ArcCircle => Parameters.InternalLeadOut, - _ => Parameters.ExternalLeadOut, - }; - } - private static Shape TrimShapeForTab(Shape shape, Vector center, double tabSize) { // A round perimeter is one circle entered at `center`: cut it as an arc from the entry diff --git a/OpenNest.Core/CNC/CuttingStrategy/CuttingParameters.cs b/OpenNest.Core/CNC/CuttingStrategy/CuttingParameters.cs index a2fa31fb..7907f8c6 100644 --- a/OpenNest.Core/CNC/CuttingStrategy/CuttingParameters.cs +++ b/OpenNest.Core/CNC/CuttingStrategy/CuttingParameters.cs @@ -23,6 +23,17 @@ namespace OpenNest.CNC.CuttingStrategy new LineLeadIn { Length = 0.125, ApproachAngle = 90 }; public LeadOut InternalLeadOut { get; set; } = new NoLeadOut(); + /// The lead-in for a contour of this type: cutouts and circles take the internal one. + public LeadIn LeadInFor(ContourType contourType) => + IsInternal(contourType) ? InternalLeadIn : ExternalLeadIn; + + /// The lead-out for a contour of this type: cutouts and circles take the internal one. + public LeadOut LeadOutFor(ContourType contourType) => + IsInternal(contourType) ? InternalLeadOut : ExternalLeadOut; + + private static bool IsInternal(ContourType contourType) => + contourType is ContourType.Internal or ContourType.ArcCircle; + public double PierceClearance { get; set; } = 0.0625; /// diff --git a/OpenNest.Desktop/Rendering/PlateOverlays.cs b/OpenNest.Desktop/Rendering/PlateOverlays.cs index 1d1fedc3..f9d076db 100644 --- a/OpenNest.Desktop/Rendering/PlateOverlays.cs +++ b/OpenNest.Desktop/Rendering/PlateOverlays.cs @@ -189,7 +189,7 @@ public sealed class SupportAnalysisCache private sealed record Entry(Program Program, Vector Location, double Rotation, SupportBed Bed, int Quadrant, double Clearance, PartSupport Support); - /// The part's support on ; cut-offs are never analysed (null). + /// The part's support on ; cut-offs are never analyzed (null). public PartSupport Analyze(Part part, SupportBed bed, int quadrant, double clearance) { if (part?.BaseDrawing == null || part.BaseDrawing.IsCutOff) diff --git a/OpenNest.Engine/Fill/IterativeShrinkFiller.cs b/OpenNest.Engine/Fill/IterativeShrinkFiller.cs index c97b4903..f96a586c 100644 --- a/OpenNest.Engine/Fill/IterativeShrinkFiller.cs +++ b/OpenNest.Engine/Fill/IterativeShrinkFiller.cs @@ -54,17 +54,7 @@ namespace OpenNest.Engine.Fill var estimatedMax = bbox.Area() > 0 ? (int)(workArea.Area() / bbox.Area()) * 2 : 1000; - workItems.Add( - new NestItem - { - Drawing = item.Drawing, - Quantity = System.Math.Max(1, estimatedMax), - Priority = item.Priority, - StepAngle = item.StepAngle, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - } - ); + workItems.Add(item.WithQuantity(System.Math.Max(1, estimatedMax))); } else { @@ -173,17 +163,7 @@ namespace OpenNest.Engine.Fill var remaining = item.Quantity - placedCount; if (remaining > 0) { - leftovers.Add( - new NestItem - { - Drawing = item.Drawing, - Quantity = remaining, - Priority = item.Priority, - StepAngle = item.StepAngle, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - } - ); + leftovers.Add(item.WithQuantity(remaining)); } } diff --git a/OpenNest.Engine/Jobs/CurrentPlatesNest.cs b/OpenNest.Engine/Jobs/CurrentPlatesNest.cs index d5b7839d..657d1373 100644 --- a/OpenNest.Engine/Jobs/CurrentPlatesNest.cs +++ b/OpenNest.Engine/Jobs/CurrentPlatesNest.cs @@ -272,19 +272,9 @@ public static class CurrentPlatesNest private static List Copy(IEnumerable items) => items .Where(i => i?.Drawing != null && i.Quantity > 0) - .Select(i => With(i, i.Quantity)) + .Select(i => i.WithQuantity(i.Quantity)) .ToList(); - private static NestItem With(NestItem item, int quantity) => new() - { - Drawing = item.Drawing, - Quantity = quantity, - Priority = item.Priority, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - StepAngle = item.StepAngle, - }; - private static List Subtract(List items, IReadOnlyDictionary placed) { var left = new List(); @@ -294,7 +284,7 @@ public static class CurrentPlatesNest var take = System.Math.Min(item.Quantity, used.GetValueOrDefault(item.Drawing)); used[item.Drawing] = used.GetValueOrDefault(item.Drawing) - take; if (item.Quantity - take > 0) - left.Add(With(item, item.Quantity - take)); + left.Add(item.WithQuantity(item.Quantity - take)); } return left; } diff --git a/OpenNest.Engine/Jobs/Placement/Fillers/LeftoverPacker.cs b/OpenNest.Engine/Jobs/Placement/Fillers/LeftoverPacker.cs index 9afa679e..ad38a48c 100644 --- a/OpenNest.Engine/Jobs/Placement/Fillers/LeftoverPacker.cs +++ b/OpenNest.Engine/Jobs/Placement/Fillers/LeftoverPacker.cs @@ -39,15 +39,7 @@ internal static class LeftoverPacker var packed = new List(); var remaining = items .Where(item => item.Quantity > 0) - .Select(item => new NestItem - { - Drawing = item.Drawing, - Quantity = item.Quantity, - Priority = item.Priority, - StepAngle = item.StepAngle, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - }) + .Select(item => item.WithQuantity(item.Quantity)) .ToList(); var sizes = remaining.ToDictionary( item => item, diff --git a/OpenNest.Engine/Jobs/Placement/Fillers/PlateFillOrchestrator.cs b/OpenNest.Engine/Jobs/Placement/Fillers/PlateFillOrchestrator.cs index c64a2d8a..4ce3deb8 100644 --- a/OpenNest.Engine/Jobs/Placement/Fillers/PlateFillOrchestrator.cs +++ b/OpenNest.Engine/Jobs/Placement/Fillers/PlateFillOrchestrator.cs @@ -131,15 +131,7 @@ internal static class PlateFillOrchestrator private static bool IsPairable(NestItem item) => item.Quantity >= 2 && item.Quantity % 2 == 0; private static List Copies(IEnumerable items) => - items.Select(item => new NestItem - { - Drawing = item.Drawing, - Priority = item.Priority, - Quantity = item.Quantity, - StepAngle = item.StepAngle, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - }).ToList(); + items.Select(item => item.WithQuantity(item.Quantity)).ToList(); /// Packs everything but quantity-two items, then places those as pairs. private static List PackThenPairs( diff --git a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs index 122fd302..ab27fdb6 100644 --- a/OpenNest.Engine/Jobs/Placement/PlateFillService.cs +++ b/OpenNest.Engine/Jobs/Placement/PlateFillService.cs @@ -75,7 +75,7 @@ public static partial class PlateFillService var dropped = false; for (var attempt = 0; attempt <= MaxClampRefills; attempt++) { - var proposed = fill(CopyDemand(item, request), area, progress); + var proposed = fill(item.WithQuantity(request), area, progress); result = new List(); AddClear(result, proposed, plate, area, demand); dropped = result.Count < proposed.Count; @@ -129,7 +129,7 @@ public static partial class PlateFillService if (!Fits(slot, item.Drawing.Program.BoundingBox())) continue; var remaining = item.Quantity > 0 ? item.Quantity - repacked.Count : item.Quantity; - AddClear(repacked, fill(CopyDemand(item, remaining), slot, null), plate, slot, demand); + AddClear(repacked, fill(item.WithQuantity(remaining), slot, null), plate, slot, demand); } FillGapsBesideClamps(fill, plate, item, demand, area, repacked, token); return repacked.Count > gapFilled.Count ? repacked : gapFilled; @@ -247,7 +247,7 @@ public static partial class PlateFillService if (!Fits(gap, size)) continue; var remaining = item.Quantity > 0 ? item.Quantity - result.Count : item.Quantity; - AddClear(result, fill(CopyDemand(item, remaining), gap, null), plate, gap, demand); + AddClear(result, fill(item.WithQuantity(remaining), gap, null), plate, gap, demand); } if (result.Count == before) return; @@ -359,7 +359,7 @@ public static partial class PlateFillService foreach (var area in ClearRegionBoxes(plate, box)) { token.ThrowIfCancellationRequested(); - var remaining = items.Select(item => CopyDemand(item, + var remaining = items.Select(item => item.WithQuantity( item.Quantity > 0 ? System.Math.Max(0, item.Quantity - result.Count(part => ReferenceEquals(part.BaseDrawing, item.Drawing))) : item.Quantity)).ToList(); var proposed = filler.PackArea(area, remaining, null, token); @@ -471,16 +471,6 @@ public static partial class PlateFillService Description = $"Clamp-clear proposal: {parts.Count} parts", }); - private static NestItem CopyDemand(NestItem item, int quantity) => new() - { - Drawing = item.Drawing, - Quantity = quantity, - Priority = item.Priority, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - StepAngle = item.StepAngle, - }; - private static List FillClearAreas(Plate plate, Box box, NestItem item, CancellationToken token, Func> fill, List previouslyPlaced = null) => @@ -496,7 +486,7 @@ public static partial class PlateFillService token.ThrowIfCancellationRequested(); if (quantity > 0 && result.Count >= quantity) break; - var demand = item == null ? null : CopyDemand(item, + var demand = item == null ? null : item.WithQuantity( quantity > 0 ? quantity - result.Count : quantity); AddClear(result, fill(area, demand), plate, area, item == null ? null : new List { item }, previouslyPlaced, groupSize); diff --git a/OpenNest.Engine/Jobs/Placement/PlateLeftoverFill.cs b/OpenNest.Engine/Jobs/Placement/PlateLeftoverFill.cs index dfbe2d45..1fb5264b 100644 --- a/OpenNest.Engine/Jobs/Placement/PlateLeftoverFill.cs +++ b/OpenNest.Engine/Jobs/Placement/PlateLeftoverFill.cs @@ -165,15 +165,7 @@ public static class PlateLeftoverFill break; if (!Fits(region, size)) continue; - var demand = new NestItem - { - Drawing = item.Drawing, - Quantity = cap - placed, - Priority = item.Priority, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - StepAngle = item.StepAngle, - }; + var demand = item.WithQuantity(cap - placed); var proposed = PlateFillService.FillItem(strategy, plate, demand, region, null, token) ?.Where(p => rotation.Allows(p.Rotation)) .ToList(); diff --git a/OpenNest.Engine/NestItem.cs b/OpenNest.Engine/NestItem.cs index ffba5241..7ff11c78 100644 --- a/OpenNest.Engine/NestItem.cs +++ b/OpenNest.Engine/NestItem.cs @@ -31,5 +31,19 @@ namespace OpenNest.Engine /// The rotation end angle in radians. /// public double RotationEnd { get; set; } + + /// + /// A copy of this item with the given quantity and every other setting unchanged. + /// + public NestItem WithQuantity(int quantity) => + new() + { + Drawing = Drawing, + Priority = Priority, + Quantity = quantity, + StepAngle = StepAngle, + RotationStart = RotationStart, + RotationEnd = RotationEnd, + }; } } diff --git a/OpenNest.Mcp/Tools/NestingTools.cs b/OpenNest.Mcp/Tools/NestingTools.cs index 37dd2967..48200112 100644 --- a/OpenNest.Mcp/Tools/NestingTools.cs +++ b/OpenNest.Mcp/Tools/NestingTools.cs @@ -380,15 +380,7 @@ namespace OpenNest.Mcp.Tools return $"Error: drawing '{item.Drawing.Name}' already exceeds requested quantity ({nested} > {item.Quantity}). Nothing committed."; counts[item.Drawing] = nested; if (nested < item.Quantity) - remaining.Add(new NestItem - { - Drawing = item.Drawing, - Quantity = (int)(item.Quantity - nested), - Priority = item.Priority, - StepAngle = item.StepAngle, - RotationStart = item.RotationStart, - RotationEnd = item.RotationEnd, - }); + remaining.Add(item.WithQuantity((int)(item.Quantity - nested))); } if (remaining.Count == 0) return "Job already complete: all requested quantities are present. Nothing committed."; diff --git a/OpenNest/Actions/ActionLeadIn.cs b/OpenNest/Actions/ActionLeadIn.cs index 67566653..c4da52be 100644 --- a/OpenNest/Actions/ActionLeadIn.cs +++ b/OpenNest/Actions/ActionLeadIn.cs @@ -325,7 +325,7 @@ namespace OpenNest.Actions var parameters = GetCurrentParameters(); - var leadIn = SelectLeadIn(parameters, snapContourType); + var leadIn = parameters.LeadInFor(snapContourType); if (leadIn == null) return; @@ -603,15 +603,6 @@ namespace OpenNest.Actions ); } - private static LeadIn SelectLeadIn(CuttingParameters parameters, ContourType contourType) - { - return contourType switch - { - ContourType.Internal or ContourType.ArcCircle => parameters.InternalLeadIn, - _ => parameters.ExternalLeadIn, - }; - } - private class ShapeInfo { public Shape Shape { get; set; } diff --git a/OpenNest/Controls/SupportOverlay.cs b/OpenNest/Controls/SupportOverlay.cs index 1e314fed..531ed0c7 100644 --- a/OpenNest/Controls/SupportOverlay.cs +++ b/OpenNest/Controls/SupportOverlay.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; -using System.Runtime.CompilerServices; +using OpenNest.Desktop.Rendering; using OpenNest.Geometry; using OpenNest.Support; @@ -29,10 +29,7 @@ namespace OpenNest.Controls private static readonly Color TippingColor = Color.FromArgb(240, 210, 30, 30); private static readonly Color TabColor = Color.FromArgb(240, 240, 140, 0); - private readonly ConditionalWeakTable cache = new(); - - private sealed record Entry(CNC.Program Program, Vector Location, double Rotation, SupportBed Bed, int Quadrant, - double Clearance, PartSupport Support); + private readonly SupportAnalysisCache cache = new(); public void Draw(Graphics g, PlateView view) { @@ -51,14 +48,11 @@ namespace OpenNest.Controls using var tabBrush = new SolidBrush(TabColor); // Tab points keep clear of inside corners as Plan Cutting's do. A proposal preview has no // parameters of its own, so it uses the live plate's. - var parameters = plate.CuttingParameters ?? view.Plate?.CuttingParameters; - var clearance = SupportAnalyzer.InsideCornerClearance(parameters?.TabConfig?.Size ?? 0); + var clearance = PlateOverlays.SupportClearance(plate, view.Plate); foreach (var part in plate.Parts) { - if (part?.BaseDrawing == null || part.BaseDrawing.IsCutOff) - continue; - var support = Analyze(part, bed, plate.Quadrant, clearance); - if (!support.IsComplete) + // Cut-offs are never analyzed. + if (cache.Analyze(part, bed, plate.Quadrant, clearance) is not { IsComplete: true } support) continue; foreach (var contact in support.Contacts) @@ -69,18 +63,6 @@ namespace OpenNest.Controls } } - private PartSupport Analyze(Part part, SupportBed bed, int quadrant, double clearance) - { - if (cache.TryGetValue(part, out var entry) && ReferenceEquals(entry.Program, part.Program) - && entry.Location == part.Location && entry.Rotation == part.Rotation && Equals(entry.Bed, bed) - && entry.Quadrant == quadrant && entry.Clearance == clearance) - return entry.Support; - var support = SupportAnalyzer.Analyze(bed, quadrant, part, clearance); - cache.AddOrUpdate(part, new Entry(part.Program, part.Location, part.Rotation, bed, quadrant, clearance, - support)); - return support; - } - private static Box Sheet(Plate plate) { var left = plate.Quadrant is 2 or 3 ? -plate.Size.Length : 0;