mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 11:01:36 -05:00
refactor: share NestItem copies, support analysis cache and lead selection
- NestItem.WithQuantity replaces eight hand-written copies that changed only the quantity, so a new field is copied everywhere. - The WinForms support overlay uses Desktop's SupportAnalysisCache and SupportClearance instead of its own copy. - CuttingParameters.LeadInFor/LeadOutFor replace the contour-type switches in ContourCuttingStrategy and ActionLeadIn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b692ae463a
commit
be4b04c7b5
13 files changed
+47
-139
No files matched your search
@@ -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
|
||||
|
||||
@@ -23,6 +23,17 @@ namespace OpenNest.CNC.CuttingStrategy
|
||||
new LineLeadIn { Length = 0.125, ApproachAngle = 90 };
|
||||
public LeadOut InternalLeadOut { get; set; } = new NoLeadOut();
|
||||
|
||||
/// <summary>The lead-in for a contour of this type: cutouts and circles take the internal one.</summary>
|
||||
public LeadIn LeadInFor(ContourType contourType) =>
|
||||
IsInternal(contourType) ? InternalLeadIn : ExternalLeadIn;
|
||||
|
||||
/// <summary>The lead-out for a contour of this type: cutouts and circles take the internal one.</summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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);
|
||||
|
||||
/// <summary>The part's support on <paramref name="bed"/>; cut-offs are never analysed (null).</summary>
|
||||
/// <summary>The part's support on <paramref name="bed"/>; cut-offs are never analyzed (null).</summary>
|
||||
public PartSupport Analyze(Part part, SupportBed bed, int quadrant, double clearance)
|
||||
{
|
||||
if (part?.BaseDrawing == null || part.BaseDrawing.IsCutOff)
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -272,19 +272,9 @@ public static class CurrentPlatesNest
|
||||
|
||||
private static List<NestItem> Copy(IEnumerable<NestItem> 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<NestItem> Subtract(List<NestItem> items, IReadOnlyDictionary<Drawing, int> placed)
|
||||
{
|
||||
var left = new List<NestItem>();
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -39,15 +39,7 @@ internal static class LeftoverPacker
|
||||
var packed = new List<Part>();
|
||||
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,
|
||||
|
||||
@@ -131,15 +131,7 @@ internal static class PlateFillOrchestrator
|
||||
private static bool IsPairable(NestItem item) => item.Quantity >= 2 && item.Quantity % 2 == 0;
|
||||
|
||||
private static List<NestItem> Copies(IEnumerable<NestItem> 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();
|
||||
|
||||
/// <summary>Packs everything but quantity-two items, then places those as pairs.</summary>
|
||||
private static List<Part> PackThenPairs(
|
||||
|
||||
@@ -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<Part>();
|
||||
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<Part> FillClearAreas(Plate plate, Box box, NestItem item,
|
||||
CancellationToken token, Func<Box, NestItem, List<Part>> fill,
|
||||
List<Part> 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<NestItem> { item }, previouslyPlaced, groupSize);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -31,5 +31,19 @@ namespace OpenNest.Engine
|
||||
/// The rotation end angle in radians.
|
||||
/// </summary>
|
||||
public double RotationEnd { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A copy of this item with the given quantity and every other setting unchanged.
|
||||
/// </summary>
|
||||
public NestItem WithQuantity(int quantity) =>
|
||||
new()
|
||||
{
|
||||
Drawing = Drawing,
|
||||
Priority = Priority,
|
||||
Quantity = quantity,
|
||||
StepAngle = StepAngle,
|
||||
RotationStart = RotationStart,
|
||||
RotationEnd = RotationEnd,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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.";
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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<Part, Entry> 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;
|
||||
|
||||
Reference in new issue
Block a user