refactor: share duplicated fill, cache, settings and toggle code

- fill_plate and fill_area share one MCP fill helper
- FillLinear uses FillHelpers.HasOverlappingParts
- BestFitCache and FillResultCache entries derive from DrawingCacheEntry
- NestDefaults and ClampSettings read through SettingsFile.TryReadText
- MainForm view toggles share one ToggleView handler

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 09:47:29 -05:00
1 parent 4b61d249c0
commit d77693aaff
10 files changed
+178 -217

No files matched your search

+9 -29
View File
@@ -227,23 +227,13 @@ namespace OpenNest.Engine.BestFit
}
}
private static Entry GetEntry(Drawing drawing)
{
var source = CanonicalFrame.SourceOf(drawing);
if (_entries.TryGetValue(source, out var entry) && entry.IsCurrentFor(source))
return entry;
lock (_entriesLock)
{
if (_entries.TryGetValue(source, out entry) && entry.IsCurrentFor(source))
return entry;
entry = new Entry(source);
_entries.AddOrUpdate(source, entry);
return entry;
}
}
private static Entry GetEntry(Drawing drawing) =>
DrawingCacheEntry.GetOrCreate(
_entries,
_entriesLock,
CanonicalFrame.SourceOf(drawing),
static source => new Entry(source)
);
private static List<BestFitResult> GetCandidates(Entry entry, Drawing drawing, double spacing)
{
@@ -407,11 +397,8 @@ namespace OpenNest.Engine.BestFit
return copy;
}
private sealed class Entry
private sealed class Entry : DrawingCacheEntry
{
private readonly CNC.Program _program;
private readonly double _sourceAngle;
public readonly ConcurrentDictionary<double, Lazy<List<BestFitResult>>> Unfiltered = new();
public readonly ConcurrentDictionary<
@@ -420,14 +407,7 @@ namespace OpenNest.Engine.BestFit
> Filtered = new();
public Entry(Drawing source)
{
_program = source.Program;
_sourceAngle = source.Source?.Angle ?? 0.0;
}
public bool IsCurrentFor(Drawing source) =>
ReferenceEquals(_program, source.Program)
&& _sourceAngle == (source.Source?.Angle ?? 0.0);
: base(source) { }
}
/// <summary>The geometry a drawing was reconstructed from, and the program built then.</summary>
+51
View File
@@ -0,0 +1,51 @@
using System;
using System.Runtime.CompilerServices;
namespace OpenNest.Engine
{
/// <summary>
/// A per-drawing cache entry that is current while the drawing keeps the program
/// reference and source angle it was built from.
/// </summary>
internal abstract class DrawingCacheEntry
{
private readonly CNC.Program program;
private readonly double sourceAngle;
protected DrawingCacheEntry(Drawing source)
{
program = source.Program;
sourceAngle = source.Source?.Angle ?? 0.0;
}
public bool IsCurrentFor(Drawing source) =>
ReferenceEquals(program, source.Program)
&& sourceAngle == (source.Source?.Angle ?? 0.0);
/// <summary>
/// Returns the current entry for <paramref name="source"/>, replacing a missing or
/// stale one under <paramref name="gate"/>.
/// </summary>
public static TEntry GetOrCreate<TEntry>(
ConditionalWeakTable<Drawing, TEntry> entries,
object gate,
Drawing source,
Func<Drawing, TEntry> create
)
where TEntry : DrawingCacheEntry
{
if (entries.TryGetValue(source, out var entry) && entry.IsCurrentFor(source))
return entry;
lock (gate)
{
if (entries.TryGetValue(source, out entry) && entry.IsCurrentFor(source))
return entry;
entry = create(source);
entries.AddOrUpdate(source, entry);
return entry;
}
}
}
}
+1 -33
View File
@@ -504,39 +504,7 @@ namespace OpenNest.Engine.Fill
List<Part> parts,
out int overlapA,
out int overlapB
)
{
var checker = new PartOverlapChecker();
for (var i = 0; i < parts.Count; i++)
{
var b1 = parts[i].BoundingBox;
for (var j = i + 1; j < parts.Count; j++)
{
var b2 = parts[j].BoundingBox;
var overlapX =
System.Math.Min(b1.Right, b2.Right) - System.Math.Max(b1.Left, b2.Left);
var overlapY =
System.Math.Min(b1.Top, b2.Top) - System.Math.Max(b1.Bottom, b2.Bottom);
if (overlapX <= Tolerance.Epsilon || overlapY <= Tolerance.Epsilon)
continue;
if (checker.Overlaps(parts[i], parts[j]))
{
overlapA = i;
overlapB = j;
return true;
}
}
}
overlapA = -1;
overlapB = -1;
return false;
}
) => Strategies.FillHelpers.HasOverlappingParts(parts, out overlapA, out overlapB);
/// <summary>
/// Creates a seed pattern containing a single part positioned at the work area origin.
+12 -29
View File
@@ -85,38 +85,20 @@ public static class FillResultCache
}
}
private static Entry GetEntry(Drawing source)
private static Entry GetEntry(Drawing source) =>
DrawingCacheEntry.GetOrCreate(
_entries,
_entriesLock,
source,
static drawing => new Entry(drawing)
);
private sealed class Entry : DrawingCacheEntry
{
if (_entries.TryGetValue(source, out var entry) && entry.IsCurrentFor(source))
return entry;
lock (_entriesLock)
{
if (_entries.TryGetValue(source, out entry) && entry.IsCurrentFor(source))
return entry;
entry = new Entry(source);
_entries.AddOrUpdate(source, entry);
return entry;
}
}
private sealed class Entry
{
private readonly CNC.Program program;
private readonly double sourceAngle;
public readonly ConcurrentDictionary<CacheKey, List<Part>> Results = new();
public Entry(Drawing source)
{
program = source.Program;
sourceAngle = source.Source?.Angle ?? 0.0;
}
public bool IsCurrentFor(Drawing source) =>
ReferenceEquals(program, source.Program)
&& sourceAngle == (source.Source?.Angle ?? 0.0);
: base(source) { }
}
private readonly record struct CacheKey(bool IsCanonical, double Width, double Height, double Spacing)
@@ -127,6 +109,7 @@ public static class FillResultCache
System.Math.Round(width, 2),
System.Math.Round(height, 2),
spacing
) { }
)
{ }
}
}
+18 -1
View File
@@ -222,7 +222,18 @@ namespace OpenNest.Engine.Strategies
/// Checks if any pair of parts geometrically overlap. Uses bounding box
/// pre-filtering for performance, then falls back to shape intersection.
/// </summary>
internal static bool HasOverlappingParts(List<Part> parts)
internal static bool HasOverlappingParts(List<Part> parts) =>
HasOverlappingParts(parts, out _, out _);
/// <summary>
/// Finds the first pair of overlapping parts, reporting their indices
/// (or -1 when none overlap).
/// </summary>
internal static bool HasOverlappingParts(
List<Part> parts,
out int overlapA,
out int overlapB
)
{
var checker = new PartOverlapChecker();
@@ -243,10 +254,16 @@ namespace OpenNest.Engine.Strategies
continue;
if (checker.Overlaps(parts[i], parts[j]))
{
overlapA = i;
overlapB = j;
return true;
}
}
}
overlapA = -1;
overlapB = -1;
return false;
}
}