diff --git a/OpenNest.Core/PlateManager.cs b/OpenNest.Core/PlateManager.cs
index cb4d3600..d3bdd7b8 100644
--- a/OpenNest.Core/PlateManager.cs
+++ b/OpenNest.Core/PlateManager.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using OpenNest.Collections;
namespace OpenNest
@@ -221,6 +222,63 @@ namespace OpenNest
return true;
}
+ /// Whether would change the plate order.
+ public bool CanSortPlatesBySize(bool largestFirst)
+ {
+ var movable = MovableCount;
+ var sorted = SortedBySize(largestFirst);
+ for (var i = 0; i < movable; i++)
+ {
+ if (!ReferenceEquals(sorted[i], nest.Plates[i]))
+ return true;
+ }
+ return false;
+ }
+
+ ///
+ /// Orders the plates by sheet area, largest or smallest first; equal areas order by the
+ /// longer side, and plates of the same size keep their order among themselves. Like
+ /// , only the order changes, the current plate stays current and
+ /// the trailing empty sentinel stays last.
+ ///
+ /// Whether the order changed.
+ public bool SortPlatesBySize(bool largestFirst)
+ {
+ if (!CanSortPlatesBySize(largestFirst))
+ return false;
+
+ var order = new List(SortedBySize(largestFirst));
+ for (var i = MovableCount; i < Count; i++)
+ order.Add(nest.Plates[i]);
+
+ reorderCurrent = CurrentPlate;
+ try
+ {
+ nest.Plates.Reorder(order);
+ }
+ finally
+ {
+ reorderCurrent = null;
+ }
+ return true;
+ }
+
+ // The movable plates in size order; LINQ's ordering is stable, so ties keep their order.
+ private List SortedBySize(bool largestFirst)
+ {
+ var movable = new List(MovableCount);
+ for (var i = 0; i < MovableCount; i++)
+ movable.Add(nest.Plates[i]);
+
+ static double Area(Plate p) => p.Size.Width * p.Size.Length;
+ static double LongSide(Plate p) => System.Math.Max(p.Size.Width, p.Size.Length);
+
+ var sorted = largestFirst
+ ? movable.OrderByDescending(Area).ThenByDescending(LongSide)
+ : movable.OrderBy(Area).ThenBy(LongSide);
+ return sorted.ToList();
+ }
+
///
/// Whether can copy the plate at
/// to : any plate but the trailing empty sentinel, landing
diff --git a/OpenNest.Tests/PlateManagerTests.cs b/OpenNest.Tests/PlateManagerTests.cs
index a046837d..401878a2 100644
--- a/OpenNest.Tests/PlateManagerTests.cs
+++ b/OpenNest.Tests/PlateManagerTests.cs
@@ -706,6 +706,33 @@ public class PlateManagerTests
Assert.DoesNotContain(empty, nest.Plates);
}
+ [Fact]
+ public void SortPlatesBySize_OrdersByAreaThenLongSideAndKeepsTiesAndTheSentinel()
+ {
+ var (nest, mgr, filled) = NestWithFilledPlates(5);
+ using var _ = mgr;
+ filled[0].Size = new Size(48, 96);
+ filled[1].Size = new Size(60, 120);
+ filled[2].Size = new Size(96, 48);
+ filled[3].Size = new Size(32, 144);
+ filled[4].Size = new Size(10, 10);
+ var sentinel = nest.Plates[^1];
+ mgr.LoadAt(2);
+
+ Assert.True(mgr.SortPlatesBySize(largestFirst: true));
+
+ // 48x96, 96x48 and 32x144 share an area: the longest side first, then the original order.
+ Assert.Equal(new[] { filled[1], filled[3], filled[0], filled[2], filled[4], sentinel }, nest.Plates);
+ Assert.Same(filled[2], mgr.CurrentPlate);
+ Assert.Equal(3, mgr.CurrentIndex);
+
+ Assert.True(mgr.SortPlatesBySize(largestFirst: false));
+
+ Assert.Equal(new[] { filled[4], filled[0], filled[2], filled[3], filled[1], sentinel }, nest.Plates);
+ Assert.False(mgr.CanSortPlatesBySize(largestFirst: false));
+ Assert.False(mgr.SortPlatesBySize(largestFirst: false));
+ }
+
[Fact]
public void DuplicatePlate_InsertsACopyAtTheSlotAndMakesItCurrent()
{
diff --git a/OpenNest.WinForms.Tests/Forms/UndoRecordingTests.cs b/OpenNest.WinForms.Tests/Forms/UndoRecordingTests.cs
index d6f62aaa..bb2516f3 100644
--- a/OpenNest.WinForms.Tests/Forms/UndoRecordingTests.cs
+++ b/OpenNest.WinForms.Tests/Forms/UndoRecordingTests.cs
@@ -84,6 +84,19 @@ public class UndoRecordingTests
public void MovePlateRecordsOneEntry() => UndoTestEnv.Run(env =>
env.AssertOneUndoableEdit("Move Plate", () => Assert.True(env.Editor.MovePlate(3, 0))));
+ [Fact]
+ public void SortPlatesBySizeRecordsOneEntry() => UndoTestEnv.Run(env =>
+ {
+ using (var change = env.Editor.BeginChange("Resize"))
+ {
+ change.MarkChanged();
+ env.Nest.Plates[0].Size = new OpenNest.Geometry.Size(50, 50);
+ env.Nest.Plates[3].Size = new OpenNest.Geometry.Size(120, 60);
+ change.Complete();
+ }
+ env.AssertOneUndoableEdit("Sort Plates", () => Assert.True(env.Editor.SortPlatesBySize(true)));
+ });
+
[Fact]
public void DuplicatePlateRecordsOneEntry() => UndoTestEnv.Run(env =>
env.AssertOneUndoableEdit("Duplicate Plate", () => Assert.True(env.Editor.DuplicatePlate(0, 1))));
diff --git a/OpenNest/Forms/EditNestForm.PlateOrder.cs b/OpenNest/Forms/EditNestForm.PlateOrder.cs
index c4fd8388..a6e96e09 100644
--- a/OpenNest/Forms/EditNestForm.PlateOrder.cs
+++ b/OpenNest/Forms/EditNestForm.PlateOrder.cs
@@ -6,7 +6,7 @@ namespace OpenNest.Forms
{
// Reordering plates in the Plates list: drag a row to a new position, or Alt+Up/Alt+Down.
// Ctrl+drag drops a copy of the plate instead, as does Duplicate Plate on the row's menu;
- // Delete Plate on the same menu removes it.
+ // Delete Plate on the same menu removes it; Sort Plates by Size on the toolbar reorders them all.
// The trailing empty new-plate sentinel stays last and cannot be dragged.
public partial class EditNestForm
{
@@ -59,6 +59,54 @@ namespace OpenNest.Forms
deleteItem.Enabled = !IsInspecting && PlateManager.CanRemoveCurrent;
};
platesListView.ContextMenuStrip = menu;
+
+ InitializeSortPlatesButton();
+ }
+
+ // Sort Plates by Size reorders the nest once, as an undoable edit; the list has no
+ // standing sort, since the plate order is the nest's own (cutting and report order).
+ private void InitializeSortPlatesButton()
+ {
+ var largestItem = new ToolStripMenuItem("Largest First", null, (sender, e) => SortPlatesBySize(true));
+ var smallestItem = new ToolStripMenuItem("Smallest First", null, (sender, e) => SortPlatesBySize(false));
+ var button = new ToolStripDropDownButton
+ {
+ Name = "sortPlatesButton",
+ DisplayStyle = ToolStripItemDisplayStyle.Image,
+ Image = sortDrawingsImage ??= CreateSortDrawingsImage(),
+ Padding = new Padding(5, 0, 5, 0),
+ Text = "Sort Plates by Size",
+ ToolTipText = "Sort plates by size",
+ };
+ button.DropDownItems.Add(largestItem);
+ button.DropDownItems.Add(smallestItem);
+ button.DropDownOpening += (sender, e) =>
+ {
+ largestItem.Enabled = !IsInspecting && PlateManager.CanSortPlatesBySize(true);
+ smallestItem.Enabled = !IsInspecting && PlateManager.CanSortPlatesBySize(false);
+ };
+ toolStrip1.Items.Add(button);
+ }
+
+ ///
+ /// Orders the plates by sheet size, largest or smallest first, as one undoable step,
+ /// keeping the current plate current. The trailing empty new-plate sentinel stays last.
+ ///
+ /// Whether the order changed.
+ public bool SortPlatesBySize(bool largestFirst)
+ {
+ if (IsInspecting || !PlateManager.CanSortPlatesBySize(largestFirst))
+ return false;
+
+ using (var change = BeginChange("Sort Plates"))
+ {
+ change.MarkChanged();
+ PlateManager.SortPlatesBySize(largestFirst);
+ change.Complete();
+ }
+
+ FirePlateChanged();
+ return true;
}
///