Files
OpenNest/OpenNest.Engine/Jobs/Adapters/DrawingJobMapper.cs
T
ajandClaude Fable 5.1 e6c87df372 perf(engine): pool best-fit pair searches by part geometry across jobs
Every Auto Nest run, and every Default-engine run, recomputed each part's
best-fit pairs: job snapshots are fresh objects and BestFitCache keyed
its entries on Drawing reference, so the Irregular engine paid
PairCatalog.Build (about half of a 118-part solve) on every run of the
same drawings.

BestFitCache now keeps a process-wide, bounded (512 sets, least recently
used first) pool of unfiltered candidate sets keyed by the exact content
of a PartGeometrySnapshot (mode and every motion value, bit for bit;
PartGeometrySnapshot.ContentComparer) and the spacing. Every drawing
DrawingJobMapper.CreateDrawing reconstructs from a snapshot declares
that geometry (BestFitCache.ShareByGeometry), so the Irregular pairs and
blocks, the cutout lattice and the Fill/Default job paths all read and
fill the pool. Candidates depend on the program geometry and spacing
alone, so a pooled set is the set the search would produce: benchmark
layouts, costs and placements are unchanged on the synthetic nests (all
engines) and on the 67-drawing, 118-part job. Failed searches are not
pooled, Invalidate leaves the pool alone, Clear empties it, and a
drawing whose Program is replaced stops vouching for its geometry.

Same 118-part job solved twice in one benchmark process (--parallel 1):
the second solve now takes 13.1 s after a 33.0 s first; before, 35.6 s
after 41.8 s.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-18 22:17:53 -04:00

132 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using OpenNest.CNC;
using OpenNest.Engine.BestFit;
namespace OpenNest.Engine.Jobs.Adapters;
/// <summary>Explicit-ID input mapping and exact supported-geometry reconstruction. Never retains caller objects.</summary>
public static class DrawingJobMapper
{
public static NestJobPart FromDrawing(string partId, Drawing drawing, int quantity)
{
ArgumentNullException.ThrowIfNull(drawing);
var constraints = drawing.Constraints;
return new NestJobPart(
partId,
PartGeometrySnapshot.FromProgram(drawing.Program),
quantity,
drawing.Priority,
constraints == null
? RotationPolicy.Automatic
: RotationPolicy.FromLegacy(
constraints.StepAngle,
constraints.StartAngle,
constraints.EndAngle,
constraints.Allow180Equivalent
)
);
}
public static NestJobPart FromItem(string partId, NestItem item)
{
ArgumentNullException.ThrowIfNull(item);
ArgumentNullException.ThrowIfNull(item.Drawing);
return new NestJobPart(
partId,
PartGeometrySnapshot.FromProgram(item.Drawing.Program),
item.Quantity,
item.Priority,
RotationPolicy.FromLegacy(
item.StepAngle,
item.RotationStart,
item.RotationEnd,
item.Drawing.Constraints?.Allow180Equivalent ?? false
)
);
}
/// <summary>Available stock is explicit; the legacy plate repeat count is not inventory.</summary>
public static NestPlateStock FromPlate(string stockId, Plate plate, int? quantity)
{
ArgumentNullException.ThrowIfNull(plate);
return new NestPlateStock(
stockId,
plate.Size,
quantity,
plate.PartSpacing,
plate.EdgeSpacing,
plate.Quadrant,
cost: null,
clampZones: new List<ClampZone>(plate.ClampZones)
)
{ ClampRail = plate.ClampRail };
}
public static Program ToProgram(PartGeometrySnapshot geometry)
{
ArgumentNullException.ThrowIfNull(geometry);
var program = new Program(geometry.Mode);
foreach (var motion in geometry.Motions)
{
var code = motion.Type switch
{
CodeType.RapidMove => (Motion)new RapidMove(motion.X, motion.Y),
CodeType.LinearMove => new LinearMove(motion.X, motion.Y) { Layer = motion.Layer },
CodeType.ArcMove => new ArcMove(
motion.X,
motion.Y,
motion.CenterX,
motion.CenterY,
motion.Rotation
)
{
Layer = motion.Layer,
},
_ => throw new NotSupportedException("Unsupported snapshot motion."),
};
code.Suppressed = motion.Suppressed;
program.Codes.Add(code);
}
return program;
}
/// <summary>A private drawing of the part for placement code. Its best-fit candidates are
/// pooled by geometry content (<see cref="BestFitCache.ShareByGeometry"/>), so the same part
/// geometry in a later job, engine instance or Auto Nest run does not search again.</summary>
internal static Drawing CreateDrawing(NestJobPart part)
{
var drawing = new Drawing(part.Id, ToProgram(part.Geometry)) { Priority = part.Priority };
drawing.Quantity.Required = part.Quantity;
drawing.Constraints = new NestConstraints
{
StepAngle = LegacyStep(part.Rotation),
StartAngle = part.Rotation.Start,
EndAngle = part.Rotation.End,
Allow180Equivalent = part.Rotation.Allow180Equivalent,
};
BestFitCache.ShareByGeometry(drawing, part.Geometry);
return drawing;
}
// A fixed angle needs a nonzero legacy step so it is not misread as automatic.
internal static double LegacyStep(RotationPolicy policy) =>
policy.Kind == RotationPolicyKind.Fixed ? OpenNest.Math.Angle.TwoPI : policy.Step;
internal static Plate CreatePlate(NestPlateStock stock)
{
var plate = new Plate(stock.Size)
{
Quantity = 1,
PartSpacing = stock.PartSpacing,
EdgeSpacing = stock.EdgeSpacing,
Quadrant = stock.Quadrant,
};
// Every Plate stores zones in plate (sheet lower-left) coordinates; consumers map
// them into the placement frame through ClampZoneFrame, exactly as the stock did.
foreach (var zone in stock.SheetClampZones)
plate.ClampZones.Add(new ClampZone(zone.Id, zone.Bounds));
return plate;
}
}