feat(sequencing): add a parked heat-aware part sequencer

Add SequenceMethod.HeatAware (100, outside PEP's numbering) and
SequenceParameters.HeatWeight. HeatAwareSequencer orders parts forward
by travel seconds plus the weight times the heat estimated where each
part is cut, from HeatSpreadEstimate: a Gaussian blob per finished part
that widens with conduction and fades with surface loss. Without a
thickness it is nearest neighbor. PlateSequencing.Apply takes the
sheet's thermal parameters for it and still places cut-offs as before.

The method and weight persist in the cutting settings DTO. Plan Cutting
does not use the method and no UI offers it: its strict search refuses
rapids over parts cut free, so a heat-spreading order needs 53-78 head
raises per plate on the measured nests. docs/plate-heat-map.md records
the numbers; revisit once thermal readings calibrate the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-30 22:47:18 -05:00
1 parent 1403517184
commit 97055c98be
16 files changed
+614 -9

No files matched your search

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenNest.CNC.Thermal;
using OpenNest.Geometry;
namespace OpenNest.Engine.Sequencing
{
/// <summary>
/// What the heat-aware order needs of one part: where its cut starts and ends, its cut extent
/// (where its heat goes) and how long the torch is on.
/// </summary>
internal readonly record struct HeatAwareItem(Vector Entry, Vector Exit, Box Extent, double TorchSeconds)
{
internal Vector Center => Extent.Center;
/// <summary>The heat's initial spread: a quarter of the extent's squared diagonal.</summary>
internal double Spread2 => (Extent.Length * Extent.Length + Extent.Width * Extent.Width) / 4;
}
/// <summary>
/// Orders parts forward from a start point: each next part is the dependency-ready one with the
/// least travel seconds plus <c>weight</c> times the estimated heat (°C) at its center when the
/// tool gets there, from a <see cref="HeatSpreadEstimate"/> of the parts already cut. So the
/// route leaves a hot zone, but only as far as it must. Without a thickness, or with no weight,
/// it is a nearest-neighbor order by travel. It ignores whether a move passes over parts already
/// cut; see docs/plate-heat-map.md#sequencing.
/// </summary>
internal static class HeatAwareOrder
{
/// <param name="items">Every part's data, by ordinal.</param>
/// <param name="parts">The ordinals to order; prerequisites outside this set count as cut.</param>
/// <param name="start">The tool position before the first part.</param>
/// <param name="prerequisites">Ordinals that must come before each part; must be acyclic.</param>
/// <param name="done">Parts already cut, in order, from <paramref name="origin"/>: their heat
/// is laid down first. Null for none.</param>
/// <param name="origin">Where the tool was before <paramref name="done"/>.</param>
internal static int[] Plan(IReadOnlyList<HeatAwareItem> items, IReadOnlyList<int> parts, Vector start,
IReadOnlyList<IReadOnlyCollection<int>> prerequisites, ThermalParameters thermal, double weight,
CancellationToken token, IReadOnlyList<int> done = null, Vector? origin = null)
{
var estimate = new HeatSpreadEstimate(thermal ?? new ThermalParameters());
var heated = estimate.HasThickness && double.IsFinite(weight) && weight > 0;
var time = 0.0;
if (done is { Count: > 0 })
{
var position = origin ?? start;
foreach (var part in done)
{
var item = items[part];
time += estimate.TravelSeconds(position.DistanceTo(item.Entry)) + item.TorchSeconds;
estimate.Add(item.Center, item.Spread2, item.TorchSeconds, time);
position = item.Exit;
}
}
var pending = new HashSet<int>(parts);
var remaining = parts.ToList();
var order = new int[parts.Count];
var current = start;
for (var step = 0; step < order.Length; step++)
{
token.ThrowIfCancellationRequested();
var best = -1;
var bestCost = double.MaxValue;
foreach (var part in remaining)
{
if (prerequisites[part].Any(pending.Contains))
continue;
var item = items[part];
var travel = estimate.TravelSeconds(current.DistanceTo(item.Entry));
var cost = heated ? travel + weight * estimate.TemperatureAt(item.Center, time + travel) : travel;
// Strictly cheaper only, so ties keep the given order.
if (cost < bestCost)
{
best = part;
bestCost = cost;
}
}
if (best < 0)
throw new InvalidOperationException("Part prerequisites form a cycle.");
var chosen = items[best];
time += estimate.TravelSeconds(current.DistanceTo(chosen.Entry)) + chosen.TorchSeconds;
if (heated)
estimate.Add(chosen.Center, chosen.Spread2, chosen.TorchSeconds, time);
current = chosen.Exit;
order[step] = best;
pending.Remove(best);
remaining.Remove(best);
}
return order;
}
}
}
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenNest.CNC;
using OpenNest.CNC.CuttingStrategy;
using OpenNest.CNC.Thermal;
using OpenNest.Converters;
using OpenNest.Geometry;
namespace OpenNest.Engine.Sequencing
{
/// <summary>
/// <see cref="SequenceMethod.HeatAware"/>: cuts forward from the plate's origin corner, each next
/// part the one with the least travel seconds plus <see cref="SequenceParameters.HeatWeight"/>
/// times the estimated heat where it is cut (<see cref="HeatAwareOrder"/>). Each part's cut
/// length, pierces, entry and exit come from its program as it stands. Cut-offs carry no heat
/// here and go last; <see cref="PlateSequencing.Apply(Plate, SequenceParameters, ThermalParameters)"/>
/// then moves each before the parts it crosses. Without a thickness the order is nearest neighbor.
/// Like every sequencer it returns the route exit-first, so the forward order comes back reversed.
/// </summary>
public class HeatAwareSequencer : IPartSequencer
{
private readonly SequenceParameters parameters;
private readonly ThermalParameters thermal;
/// <param name="thermal">The sheet's material and thickness; null orders by travel alone.</param>
public HeatAwareSequencer(SequenceParameters parameters, ThermalParameters thermal)
{
this.parameters = parameters;
this.thermal = thermal;
}
public List<SequencedPart> Sequence(IReadOnlyList<Part> parts, Plate plate)
{
if (parts.Count == 0)
return new List<SequencedPart>();
var heated = parts.Where(p => !p.BaseDrawing.IsCutOff).ToList();
var estimate = new HeatSpreadEstimate(thermal ?? new ThermalParameters());
var items = heated.Select(part => ItemOf(part, estimate)).ToArray();
var none = new IReadOnlyCollection<int>[items.Length];
System.Array.Fill(none, System.Array.Empty<int>());
var bounds = plate.BoundingBox(includeParts: false);
var order = HeatAwareOrder.Plan(items, Enumerable.Range(0, items.Length).ToArray(),
new Vector(bounds.X, bounds.Y), none, thermal, parameters.HeatWeight, CancellationToken.None);
var forward = order.Select(i => heated[i]).Concat(parts.Where(p => p.BaseDrawing.IsCutOff));
return forward.Reverse().Select(part => new SequencedPart { Part = part }).ToList();
}
private static HeatAwareItem ItemOf(Part part, HeatSpreadEstimate estimate)
{
var box = part.BoundingBox;
var entities = ConvertProgram.ToGeometry(DisplayPerimeterTail.WithoutTail(part.Program))
.Where(entity => entity is Line or Arc).ToList();
var length = 0.0;
var pierces = 0;
var torchOn = false;
Vector? entry = null;
var exit = box.Center;
foreach (var entity in entities)
{
var cuts = CutsMaterial(entity.Layer);
if (cuts)
{
length += entity.Length;
if (!torchOn)
pierces++;
entry ??= Start(entity) + part.Location;
exit = End(entity) + part.Location;
}
torchOn = cuts;
}
return entry is { } start
? new HeatAwareItem(start, exit, box, estimate.TorchSeconds(length, pierces))
: new HeatAwareItem(box.Center, box.Center, box, 0);
}
// Layers cut with the torch on (scribes etch, rapids and tabs travel head-up or uncut).
private static bool CutsMaterial(Layer layer) =>
layer == SpecialLayers.Cut || layer == SpecialLayers.Display || layer == SpecialLayers.Leadin
|| layer == SpecialLayers.Leadout || layer == SpecialLayers.Break;
private static Vector Start(Entity entity) => entity is Line line ? line.StartPoint : ((Arc)entity).StartPoint();
private static Vector End(Entity entity) => entity is Line line ? line.EndPoint : ((Arc)entity).EndPoint();
}
}
@@ -1,11 +1,16 @@
using System;
using OpenNest.CNC.CuttingStrategy;
using OpenNest.CNC.Thermal;
namespace OpenNest.Engine.Sequencing
{
public static class PartSequencerFactory
{
public static IPartSequencer Create(SequenceParameters parameters)
/// <param name="thermal">
/// The sheet's material and thickness, which only <see cref="SequenceMethod.HeatAware"/>
/// reads; without them it orders by travel alone.
/// </param>
public static IPartSequencer Create(SequenceParameters parameters, ThermalParameters thermal = null)
{
return parameters.Method switch
{
@@ -15,6 +20,7 @@ namespace OpenNest.Engine.Sequencing
SequenceMethod.EdgeStart => new EdgeStartSequencer(),
SequenceMethod.LeastCode => new LeastCodeSequencer(),
SequenceMethod.Advanced => new AdvancedSequencer(parameters),
SequenceMethod.HeatAware => new HeatAwareSequencer(parameters, thermal),
_ => throw new NotSupportedException(
$"Sequence method '{parameters.Method}' is not supported."
),
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenNest.CNC.CuttingStrategy;
using OpenNest.CNC.Thermal;
using OpenNest.CutOffs;
using OpenNest.Engine.CuttingPlanning;
@@ -10,18 +11,22 @@ namespace OpenNest.Engine.Sequencing
public static class PlateSequencing
{
/// <summary>Sequences the plates present when the operation starts.</summary>
public static void ApplyAll(IEnumerable<Plate> plates, SequenceParameters parameters)
/// <param name="thermal">The sheets' material and thickness, for
/// <see cref="SequenceMethod.HeatAware"/>; see <see cref="PartSequencerFactory.Create"/>.</param>
public static void ApplyAll(IEnumerable<Plate> plates, SequenceParameters parameters, ThermalParameters thermal = null)
{
// Reordering parts can make PlateManager replace the trailing empty
// plate. Snapshot before applying so those events cannot invalidate
// enumeration of the nest's live plate collection.
foreach (var plate in plates.ToArray())
Apply(plate, parameters);
Apply(plate, parameters, thermal);
}
public static void Apply(Plate plate, SequenceParameters parameters)
/// <param name="thermal">The sheet's material and thickness, for
/// <see cref="SequenceMethod.HeatAware"/>; see <see cref="PartSequencerFactory.Create"/>.</param>
public static void Apply(Plate plate, SequenceParameters parameters, ThermalParameters thermal = null)
{
var sequencer = PartSequencerFactory.Create(parameters);
var sequencer = PartSequencerFactory.Create(parameters, thermal);
// Cut-first parts (test coupons) get their own route ahead of the rest,
// so they stay grouped instead of interleaving with the main route.