Files
OpenNest/OpenNest.Engine/Jobs/NestJobCandidateComparer.cs
T
ajandClaude Opus 5.5 fbc23d6b64 feat(nesting): nest higher priority numbers first
Priority worked like a rank (lower first) with a default of 0, and the
Edit Drawing form stopped at 0, so moving one part ahead of the rest
meant renumbering every other drawing. Higher-first lets a user raise a
single part to 1 and leave everything else at the default.

Every engine, filler and candidate comparer now serves the highest tier
first. Stored values keep their order:
- .nest files add `nestPriority` and keep writing the negated value under
  `priority`, so older readers nest in the same order; files without
  `nestPriority` are negated on read. Drawing packages strip both, so
  package hashes are unchanged.
- Library settings send both keys the same way for older clients.
- Server schema v18 negates stored drawing priorities (an older server
  refuses the upgraded database).
- Job documents move to version 2; version 1 jobs are converted, and an
  older worker refuses version 2 instead of reversing the order.

The Edit Drawing form now accepts negative priorities and says higher
numbers nest first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 11:02:07 -05:00

72 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenNest.Engine.Jobs;
/// <summary>Ranks independent plate trials: priority fulfillment, sheet cost, placement envelope, then input order.</summary>
public sealed class NestJobCandidateComparer
{
private readonly IReadOnlyList<NestJobPart> parts;
private readonly NestJob job;
public NestJobCandidateComparer(NestJob job) : this(job.Parts) => this.job = job;
public NestJobCandidateComparer(IReadOnlyList<NestJobPart> parts)
{
this.parts = parts ?? throw new ArgumentNullException(nameof(parts));
}
/// <summary>Returns positive when the left trial is preferred.</summary>
public int Compare(
PlateCandidate left,
NestPlateStock leftStock,
int leftIndex,
PlateCandidate right,
NestPlateStock rightStock,
int rightIndex
)
{
var priorities = parts
.Select(part => part.Priority)
.Distinct()
.OrderByDescending(priority => priority);
foreach (var priority in priorities)
{
var leftCount = Count(left, priority);
var rightCount = Count(right, priority);
if (leftCount != rightCount)
return leftCount.CompareTo(rightCount);
}
var area = Score(right, rightStock).CompareTo(Score(left, leftStock));
if (area != 0)
return area;
var envelope = Envelope(right).CompareTo(Envelope(left));
if (envelope != 0)
return envelope;
return rightIndex.CompareTo(leftIndex);
}
private int Count(PlateCandidate candidate, int priority) =>
candidate.Placements.Count(placement =>
parts.First(part => part.Id == placement.PartId).Priority == priority
);
private double Score(PlateCandidate candidate, NestPlateStock stock) =>
job != null && stock.Cost.HasValue
? NestJobCost.NetSheetCost(job, new NestJobPlateResult(0, stock, candidate.Placements))
: NestJobCost.GrossSheetCost(stock);
private static double Envelope(PlateCandidate candidate)
{
if (candidate.Placements.Count == 0)
return 0;
var xs = candidate.Placements.Select(placement => placement.X);
var ys = candidate.Placements.Select(placement => placement.Y);
return (xs.Max() - xs.Min()) * (ys.Max() - ys.Min());
}
}