Files
OpenNest/OpenNest.Avalonia/MainWindow.Arrange.cs
T
ajandClaude Opus 5.5 044b59463b feat(avalonia): arrange parts and common lines from Plate > Arrange
Plate > Arrange runs the shared Arrange edits on the active nest's
plate, each one undo step under the WinForms label:

- Left, Right, Top, Bottom, Horizontally and Vertically line the
  selection up with its first selected part; Evenly Space Horizontally
  and Vertically spread it; Center Parts centers every part in the work
  area.
- Join Common Line joins the selection at the kerf of the plate's
  cutting parameters, Remove Common Line removes its groups (each says
  why when it cannot), and Cut Common Lines Twice is a checked toggle
  showing the plate's setting.
- Expand Spacing and Fix Spacing search on a worker behind a modal
  Cancel window (OperationProgressDialog, the WinForms
  OperationProgressForm's rules): it appears once a run outlasts
  300 ms, Cancel, Escape and the close box only request cancellation,
  and it closes once the worker stops. The plate takes no edits until
  then. Expand needs two selected parts; while Auto Nest works on the
  nest both say to finish it first. Their result shows in the status
  bar.

A right click released on a selected part in Select opens a menu with
Join and Remove Common Line, enabled as in WinForms. While Auto Nest
works on the nest the other Arrange commands are disabled, as the
WinForms modal progress window keeps them out of reach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:11:35 -05:00

183 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using OpenNest.Avalonia.Controls;
using OpenNest.Desktop;
using OpenNest.Desktop.Interaction;
using OpenNest.Undo;
namespace OpenNest.Avalonia
{
/// <summary>
/// Plate &gt; Arrange on the active nest's plate, through the shared <see cref="PlateArrange"/>,
/// each one undo step under the WinForms label: align, evenly space and center parts, Expand
/// Spacing and Fix Spacing behind a Cancel window, and the common-line commands, which the
/// right-click menu on a selected part also offers. While Auto Nest works on the nest its plate
/// takes none of them; Expand and Fix Spacing then say why.
/// </summary>
public partial class MainWindow
{
// The nest whose Expand or Fix Spacing search is running: its plate is locked until the
// worker stops, as the WinForms Cancel window keeps it.
private NestTab arrangingTab;
private PlateCanvas ArrangeCanvas =>
ActiveView?.PlateView is { Plate: not null } canvas ? canvas : null;
/// <summary>Shows the part menu when a selected part on <paramref name="view"/>'s plate is right-clicked.</summary>
private void WatchPartMenu(NestView view) =>
view.PlateView.PartMenuRequested += (_, _) => ShowPartMenu(view);
// As WinForms (ArrangeMenu_Opening): Cut Common Lines Twice shows the plate's setting.
// The other commands do nothing, or say why, when they have nothing to act on.
private void OnArrangeMenuOpened(object sender, RoutedEventArgs e)
{
var canvas = ArrangeCanvas;
var editable = canvas is { EditsLocked: false };
foreach (var item in new MenuItem[]
{
AlignLeftMenuItem, AlignRightMenuItem, AlignTopMenuItem, AlignBottomMenuItem,
AlignHorizontallyMenuItem, AlignVerticallyMenuItem, EvenlySpaceHorizontallyMenuItem,
EvenlySpaceVerticallyMenuItem, CenterPartsMenuItem, JoinCommonLineMenuItem,
RemoveCommonLineMenuItem, CutCommonLinesTwiceMenuItem,
})
item.IsEnabled = editable;
// Enabled during Auto Nest so they can say why they wait, as WinForms does.
ExpandSpacingMenuItem.IsEnabled = canvas != null && arrangingTab == null;
FixSpacingMenuItem.IsEnabled = canvas != null && arrangingTab == null;
CutCommonLinesTwiceMenuItem.IsChecked = canvas?.Plate.CutCommonLinesTwice == true;
}
private void OnAlign(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas && (sender as MenuItem)?.Tag is string tag
&& Enum.TryParse<AlignType>(tag, out var type))
PlateArrange.Align(canvas, type);
}
private void OnCenterParts(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas)
PlateArrange.CenterParts(canvas);
}
private async void OnJoinCommonLine(object sender, RoutedEventArgs e) => await JoinCommonLineAsync();
private async void OnRemoveCommonLine(object sender, RoutedEventArgs e) => await RemoveCommonLineAsync();
private void OnCutCommonLinesTwice(object sender, RoutedEventArgs e)
{
if (ArrangeCanvas is { } canvas)
PlateArrange.ToggleCutCommonLinesTwice(canvas);
CutCommonLinesTwiceMenuItem.IsChecked = ArrangeCanvas?.Plate.CutCommonLinesTwice == true;
}
// Joined at the kerf of the plate's cutting parameters, as WinForms.
private async Task JoinCommonLineAsync()
{
if (ActiveView is not { PlateView: { Plate: { } plate } canvas } view)
return;
var kerf = DesktopSettings.Machines.CuttingParametersFor(plate, view.Tab.Document.Nest).Kerf;
if (PlateArrange.JoinCommonLine(canvas, kerf) is { Succeeded: false } result)
await ConfirmDialog.ShowAsync(this, "Common Line", result.Failure);
}
private async Task RemoveCommonLineAsync()
{
if (ArrangeCanvas is { } canvas && PlateArrange.RemoveCommonLines(canvas) == false)
await ConfirmDialog.ShowAsync(this, "Common Line", PlateArrange.NoCommonLineNotice);
}
// As the WinForms part menu: Join needs two units to bring together, Remove a grouped part.
private void ShowPartMenu(NestView view)
{
var canvas = view.PlateView;
if (view != ActiveView || canvas.Plate == null)
return;
var (canJoin, canRemove) = PlateArrange.CommonLineCommands(canvas.Plate,
PlateArrange.EditableSelection(canvas) ?? Array.Empty<Part>());
var join = new MenuItem { Header = "Join Common Line", IsEnabled = canJoin };
join.Click += async (_, _) => await JoinCommonLineAsync();
var remove = new MenuItem { Header = "Remove Common Line", IsEnabled = canRemove };
remove.Click += async (_, _) => await RemoveCommonLineAsync();
var menu = new ContextMenu { ItemsSource = new List<MenuItem> { join, remove } };
menu.Open(canvas);
}
private async void OnExpandSpacing(object sender, RoutedEventArgs e)
{
const string title = "Expand Spacing";
if (!await CanStartSpacingAsync(title))
return;
var view = ActiveView;
var canvas = view.PlateView;
var parts = PlateArrange.EditableSelection(canvas);
if (parts is not { Count: >= 2 })
{
await ConfirmDialog.ShowAsync(this, title, PlateArrange.ExpandNeedsTwoPartsNotice);
return;
}
// Complex layouts can search for minutes; run behind a Cancel window.
await RunSpacingAsync(view, title, begin => PlateArrange.ExpandSpacingAsync(canvas.Plate, parts,
canvas.CutOffSettings, begin, work => OperationProgressDialog.RunAsync(this, title,
"Expanding spacing between the selected parts...", work)));
}
private async void OnFixSpacing(object sender, RoutedEventArgs e)
{
const string title = "Fix Spacing";
if (!await CanStartSpacingAsync(title))
return;
var view = ActiveView;
var canvas = view.PlateView;
var parts = PlateArrange.EditableSelection(canvas) ?? Array.Empty<Part>();
await RunSpacingAsync(view, title, begin => PlateArrange.FixSpacingAsync(canvas.Plate, parts,
canvas.CutOffSettings, begin, work => OperationProgressDialog.RunAsync(this, title,
"Pushing apart the overlapping and too-close parts...", work)));
}
// The search reads the plate on a worker, so a nest that can still add parts must finish first.
private async Task<bool> CanStartSpacingAsync(string title)
{
if (ArrangeCanvas == null || arrangingTab != null)
return false;
if (ActiveView.Tab != nestingTab)
return true;
await ConfirmDialog.ShowAsync(this, title, PlateArrange.BusyNotice(title));
return false;
}
// Locks the nest's plate while the search runs: the window is modal once shown and the
// caller blocks until then, and the open undo step keeps Undo and Redo off.
private async Task RunSpacingAsync<TResult>(NestView view, string title,
Func<Func<string, ChangeScope>, Task<PlateArrange.SpacingOutcome<TResult>>> command)
{
var canvas = view.PlateView;
var tab = view.Tab;
var locked = canvas.EditsLocked;
arrangingTab = tab;
canvas.EditsLocked = true;
try
{
var outcome = await command(label => tab.Undo.BeginChange(label));
if (outcome != null)
ShowNotice(outcome.Status);
}
catch (Exception ex)
{
// The open step rolls back whatever the failed run changed.
ShowNotice($"{title} failed: {ex.Message}");
}
finally
{
canvas.EditsLocked = locked;
arrangingTab = null;
canvas.InvalidateVisual();
UpdateUndoMenu();
}
}
}
}