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
{
///
/// : cuts forward from the plate's origin corner, each next
/// part the one with the least travel seconds plus
/// times the estimated heat where it is cut (). 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;
/// 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.
///
public class HeatAwareSequencer : IPartSequencer
{
private readonly SequenceParameters parameters;
private readonly ThermalParameters thermal;
/// The sheet's material and thickness; null orders by travel alone.
public HeatAwareSequencer(SequenceParameters parameters, ThermalParameters thermal)
{
this.parameters = parameters;
this.thermal = thermal;
}
public List Sequence(IReadOnlyList parts, Plate plate)
{
if (parts.Count == 0)
return new List();
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[items.Length];
System.Array.Fill(none, System.Array.Empty());
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();
}
}