Files
OpenNest/OpenNest.Engine/Sequencing/HeatAwareSequencer.cs
T
ajandClaude Opus 5.5 97055c98be 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>
2026-11-30 22:47:18 -05:00

89 lines
4.1 KiB
C#

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();
}
}