mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Moves the Drawings tab's row content (quantity, size, area, highlight and quantity bar, the hidden-list note), Delete Drawing, Remove Unused Drawings and the Drawing Properties dialog's values and color rules into OpenNest.Desktop so the Avalonia app can share them. The WinForms list, nest window and Edit Drawing form delegate to them unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.9 KiB
C#
101 lines
3.9 KiB
C#
using System;
|
|
using System.Linq;
|
|
using OpenNest.Undo;
|
|
|
|
namespace OpenNest.Desktop;
|
|
|
|
/// <summary>
|
|
/// The Drawings tab's nest edits, toolkit-neutral: Delete on a drawing's menu, Remove Unused
|
|
/// Drawings and the Drawing Properties dialog's OK. Each one is one undo step under the WinForms
|
|
/// label, opened with <paramref name="beginChange"/> (the window's <c>BeginChange</c>), with the
|
|
/// WinForms confirmation texts for the front ends to ask first.
|
|
/// </summary>
|
|
public static class DrawingCommands
|
|
{
|
|
public const string DeleteLabel = "Delete Drawing";
|
|
|
|
/// <summary>The title of Delete's confirmation.</summary>
|
|
public const string DeleteTitle = "Delete Drawing";
|
|
|
|
public const string CleanUnusedLabel = "Clean Unused Drawings";
|
|
|
|
/// <summary>The title of Remove Unused Drawings' confirmation.</summary>
|
|
public const string CleanUnusedTitle = "Clean Drawings";
|
|
|
|
/// <summary>Remove Unused Drawings' confirmation.</summary>
|
|
public const string CleanUnusedPrompt = "Remove unused drawings?";
|
|
|
|
public const string PropertiesLabel = "Drawing Properties";
|
|
|
|
/// <summary>Delete's confirmation for <paramref name="drawing"/>.</summary>
|
|
public static string DeletePrompt(Drawing drawing)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(drawing);
|
|
return $"Delete drawing '{drawing.Name}' and all its parts from every plate?";
|
|
}
|
|
|
|
/// <summary>Whether the nest has a drawing with no part on any plate.</summary>
|
|
public static bool HasUnusedDrawings(Nest nest)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
return nest.Drawings.Any(d => d.Quantity.Nested == 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the drawings with no part on any plate, as one "Clean Unused Drawings" step.
|
|
/// </summary>
|
|
/// <returns>Whether a drawing was removed; when none would be, nothing is recorded.</returns>
|
|
public static bool CleanUnusedDrawings(Nest nest, Func<string, ChangeScope> beginChange)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
ArgumentNullException.ThrowIfNull(beginChange);
|
|
if (!HasUnusedDrawings(nest))
|
|
return false;
|
|
|
|
using var change = beginChange(CleanUnusedLabel);
|
|
change.MarkChanged();
|
|
nest.Drawings.RemoveWhere(d => d.Quantity.Nested == 0);
|
|
change.Complete();
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes <paramref name="drawing"/> and its parts from every plate, as one "Delete Drawing"
|
|
/// step. The plates' part lists keep the drawings' nested quantities.
|
|
/// </summary>
|
|
public static void DeleteDrawing(Nest nest, Drawing drawing, Func<string, ChangeScope> beginChange)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(nest);
|
|
ArgumentNullException.ThrowIfNull(drawing);
|
|
ArgumentNullException.ThrowIfNull(beginChange);
|
|
using var change = beginChange(DeleteLabel);
|
|
change.MarkChanged();
|
|
// Emptying the last plates drops trailing empty ones, so walk a copy.
|
|
foreach (var plate in nest.Plates.ToList())
|
|
{
|
|
for (var i = plate.Parts.Count - 1; i >= 0; i--)
|
|
{
|
|
if (plate.Parts[i].BaseDrawing == drawing)
|
|
plate.Parts.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
nest.Drawings.Remove(drawing);
|
|
change.Complete();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The Drawing Properties dialog's OK: <paramref name="properties"/> saved to its drawing as
|
|
/// one "Drawing Properties" step. A cancelled dialog calls nothing, so nothing is recorded.
|
|
/// </summary>
|
|
public static void ApplyProperties(DrawingProperties properties, Func<string, ChangeScope> beginChange)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(properties);
|
|
ArgumentNullException.ThrowIfNull(beginChange);
|
|
using var change = beginChange(PropertiesLabel);
|
|
change.MarkChanged();
|
|
properties.ApplyTo(properties.Drawing);
|
|
change.Complete();
|
|
}
|
|
}
|