Files
OpenNest/OpenNest.Data/DrawingNestingSettings.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

104 lines
4.0 KiB
C#

using System.Drawing;
using System.Text.Json.Serialization;
using OpenNest;
namespace OpenNest.Data;
/// <summary>
/// Per-drawing nesting settings a library record owns. They are structured server fields,
/// not package content: canonical packages leave them at their defaults, and adding a library
/// drawing to a nest applies these values. Editing them adds a revision without new geometry.
/// A record, so two settings are equal when every value is.
/// </summary>
public sealed record DrawingNestingSettings
{
/// <summary>Automatic sequencing cuts this drawing's parts first (for example test squares).</summary>
public bool CutFirst { get; init; }
/// <summary>Nesting order: higher numbers nest first.</summary>
[JsonIgnore]
public int Priority { get; init; }
/// <summary>
/// The lower-first legacy value (<see cref="OpenNest.LegacyPriority"/>) older clients read
/// and send. Written before <see cref="NestPriority"/>, so a reader that sees both ends on it.
/// </summary>
[JsonPropertyName("priority")]
public int LegacyPriorityValue
{
get => LegacyPriority.Convert(Priority);
init => Priority = LegacyPriority.Convert(value);
}
/// <summary><see cref="Priority"/> on the wire; absent from older clients.</summary>
[JsonPropertyName("nestPriority")]
public int NestPriority
{
get => Priority;
init => Priority = value;
}
/// <summary>Rotation step in radians; zero means the engine's default.</summary>
public double StepAngle { get; init; }
/// <summary>Rotation start angle in radians.</summary>
public double StartAngle { get; init; }
/// <summary>Rotation end angle in radians.</summary>
public double EndAngle { get; init; }
public bool Allow180Equivalent { get; init; }
/// <summary>
/// Display color as a 32-bit ARGB value. A fully transparent value (alpha 0, the default)
/// means no stored color: the drawing gets the next part color when it is added.
/// </summary>
public int ColorArgb { get; init; }
/// <summary>The settings <paramref name="drawing"/> currently has.</summary>
public static DrawingNestingSettings FromDrawing(Drawing drawing)
{
ArgumentNullException.ThrowIfNull(drawing);
var constraints = drawing.Constraints ?? new NestConstraints();
return new DrawingNestingSettings
{
CutFirst = drawing.CutFirst,
Priority = drawing.Priority,
StepAngle = constraints.StepAngle,
StartAngle = constraints.StartAngle,
EndAngle = constraints.EndAngle,
Allow180Equivalent = constraints.Allow180Equivalent,
// Zero means "use the palette", so only a custom color is carried into the library.
ColorArgb = drawing.HasCustomColor ? drawing.Color.ToArgb() : 0,
};
}
/// <summary>Writes these settings onto <paramref name="drawing"/>.</summary>
public void ApplyTo(Drawing drawing)
{
ArgumentNullException.ThrowIfNull(drawing);
drawing.CutFirst = CutFirst;
drawing.Priority = Priority;
drawing.Constraints = new NestConstraints
{
StepAngle = StepAngle,
StartAngle = StartAngle,
EndAngle = EndAngle,
Allow180Equivalent = Allow180Equivalent,
};
var color = Color.FromArgb(ColorArgb);
drawing.HasCustomColor = color.A != 0;
drawing.Color = drawing.HasCustomColor ? color : Drawing.GetNextColor();
}
/// <summary>The first rule these settings break, or null when they may be saved.</summary>
public string? GetValidationError()
{
if (!double.IsFinite(StepAngle) || !double.IsFinite(StartAngle) || !double.IsFinite(EndAngle))
return "settings angles must be finite numbers.";
// Ranges are not checked: nest drawings carry whatever their files hold, and
// saving one to the library must not be refused for it.
return StepAngle < 0 ? "settings.stepAngle must be zero or greater." : null;
}
}