using System; using System.Linq; using OpenNest.Undo; namespace OpenNest.Desktop; /// /// 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 (the window's BeginChange), with the /// WinForms confirmation texts for the front ends to ask first. /// public static class DrawingCommands { public const string DeleteLabel = "Delete Drawing"; /// The title of Delete's confirmation. public const string DeleteTitle = "Delete Drawing"; public const string CleanUnusedLabel = "Clean Unused Drawings"; /// The title of Remove Unused Drawings' confirmation. public const string CleanUnusedTitle = "Clean Drawings"; /// Remove Unused Drawings' confirmation. public const string CleanUnusedPrompt = "Remove unused drawings?"; public const string PropertiesLabel = "Drawing Properties"; /// Delete's confirmation for . public static string DeletePrompt(Drawing drawing) { ArgumentNullException.ThrowIfNull(drawing); return $"Delete drawing '{drawing.Name}' and all its parts from every plate?"; } /// Whether the nest has a drawing with no part on any plate. public static bool HasUnusedDrawings(Nest nest) { ArgumentNullException.ThrowIfNull(nest); return nest.Drawings.Any(d => d.Quantity.Nested == 0); } /// /// Removes the drawings with no part on any plate, as one "Clean Unused Drawings" step. /// /// Whether a drawing was removed; when none would be, nothing is recorded. public static bool CleanUnusedDrawings(Nest nest, Func 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; } /// /// Deletes and its parts from every plate, as one "Delete Drawing" /// step. The plates' part lists keep the drawings' nested quantities. /// public static void DeleteDrawing(Nest nest, Drawing drawing, Func 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(); } /// /// The Drawing Properties dialog's OK: saved to its drawing as /// one "Drawing Properties" step. A cancelled dialog calls nothing, so nothing is recorded. /// public static void ApplyProperties(DrawingProperties properties, Func beginChange) { ArgumentNullException.ThrowIfNull(properties); ArgumentNullException.ThrowIfNull(beginChange); using var change = beginChange(PropertiesLabel); change.MarkChanged(); properties.ApplyTo(properties.Drawing); change.Complete(); } }