mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-26 20:37:42 -05:00
feat(desktop): sort plates by size from the Plates tab
A Sort Plates by Size toolbar button reorders the nest's plates largest or smallest first, as one undoable step. Equal areas order by the longer side, same-size sheets keep their order, the current plate stays current and the trailing empty new-plate sheet stays last. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
990f7660b2
commit
fee7df3544
4 files changed
+147
-1
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>Whether <see cref="SortPlatesBySize"/> would change the plate order.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// <see cref="MovePlate"/>, only the order changes, the current plate stays current and
|
||||
/// the trailing empty sentinel stays last.
|
||||
/// </summary>
|
||||
/// <returns>Whether the order changed.</returns>
|
||||
public bool SortPlatesBySize(bool largestFirst)
|
||||
{
|
||||
if (!CanSortPlatesBySize(largestFirst))
|
||||
return false;
|
||||
|
||||
var order = new List<Plate>(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<Plate> SortedBySize(bool largestFirst)
|
||||
{
|
||||
var movable = new List<Plate>(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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <see cref="DuplicatePlate"/> can copy the plate at <paramref name="fromIndex"/>
|
||||
/// to <paramref name="toIndex"/>: any plate but the trailing empty sentinel, landing
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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))));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <returns>Whether the order changed.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user