mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Merge M13 Drawings panel and Drawing Properties into the Avalonia port work
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> # Conflicts: # OpenNest.Avalonia/NestView.axaml.cs
This commit is contained in:
commit
c1e2e9ea2d
15 files changed
+1711
-181
No files matched your search
@@ -0,0 +1,78 @@
|
||||
using System.ComponentModel;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Media.Imaging;
|
||||
using OpenNest.Avalonia.Rendering;
|
||||
using OpenNest.Desktop;
|
||||
|
||||
namespace OpenNest.Avalonia
|
||||
{
|
||||
/// <summary>
|
||||
/// One row of the Drawings tab list: the shared <see cref="DrawingListRow"/> with its picture
|
||||
/// and the WinForms row colors (a pale yellow row once nothing remains to nest, a green or
|
||||
/// orange bar for a required quantity that is or is not all nested). The list updates a row
|
||||
/// in place while the drawings and their order stay the same.
|
||||
/// </summary>
|
||||
public sealed class DrawingListItem : INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>The picture's size, as the WinForms list draws it.</summary>
|
||||
public const int ThumbnailWidth = 85;
|
||||
|
||||
public const int ThumbnailHeight = 75;
|
||||
|
||||
private static readonly IBrush Plain = Brushes.White;
|
||||
private static readonly IBrush Highlight = new SolidColorBrush(Color.FromRgb(255, 255, 225));
|
||||
private static readonly IBrush CompleteBar = new SolidColorBrush(Color.FromRgb(76, 175, 80));
|
||||
private static readonly IBrush ShortBar = new SolidColorBrush(Color.FromRgb(255, 152, 0));
|
||||
|
||||
private DrawingListRow row;
|
||||
private double scaling;
|
||||
|
||||
public DrawingListItem(DrawingListRow row, double scaling)
|
||||
{
|
||||
this.row = row;
|
||||
this.scaling = scaling;
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public Drawing Drawing => row.Drawing;
|
||||
|
||||
public string Name => row.Name;
|
||||
|
||||
public string Quantity => row.Quantity;
|
||||
|
||||
public bool HasQuantity => row.Quantity != null;
|
||||
|
||||
public string Size => row.Size;
|
||||
|
||||
public string Area => row.Area;
|
||||
|
||||
public IBrush Background => row.IsHighlighted ? Highlight : Plain;
|
||||
|
||||
public IBrush Bar => row.Bar switch
|
||||
{
|
||||
DrawingQuantityBar.Complete => CompleteBar,
|
||||
DrawingQuantityBar.Short => ShortBar,
|
||||
_ => Brushes.Transparent,
|
||||
};
|
||||
|
||||
// Drawn when first shown and cached per drawing until its program or color changes.
|
||||
public Bitmap Thumbnail => DrawingThumbnail.For(row.Drawing, ThumbnailWidth, ThumbnailHeight, scaling);
|
||||
|
||||
/// <summary>Shows <paramref name="value"/>, the same drawing's current row.</summary>
|
||||
public void Update(DrawingListRow value, double renderScaling)
|
||||
{
|
||||
row = value;
|
||||
scaling = renderScaling;
|
||||
// Every property: a color or program change redraws the picture too.
|
||||
foreach (var name in Properties)
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||
}
|
||||
|
||||
private static readonly string[] Properties =
|
||||
{
|
||||
nameof(Drawing), nameof(Name), nameof(Quantity), nameof(HasQuantity), nameof(Size), nameof(Area),
|
||||
nameof(Background), nameof(Bar), nameof(Thumbnail),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
using OpenNest.Avalonia.Rendering;
|
||||
using OpenNest.Desktop;
|
||||
using DrawingColor = System.Drawing.Color;
|
||||
|
||||
namespace OpenNest.Avalonia
|
||||
{
|
||||
/// <summary>
|
||||
/// The Drawings tab's double-click, as the WinForms Drawing Properties dialog: the drawing's
|
||||
/// picture beside its name, customer and formed-after-cutting answer, its quantity (with how
|
||||
/// many are nested and remain), priority, cut first and allow common line, its color (picked,
|
||||
/// or Automatic from the color scheme) and its size, area and source file. OK returns the
|
||||
/// edited <see cref="DrawingProperties"/> (the caller saves them as one undo step); Cancel or
|
||||
/// Esc returns null and changes nothing.
|
||||
/// </summary>
|
||||
internal sealed class DrawingPropertiesDialog : Window
|
||||
{
|
||||
private const int PreviewSize = 240;
|
||||
|
||||
private readonly DrawingProperties properties;
|
||||
private readonly TextBox name = new() { Name = "DrawingName", MinWidth = 260 };
|
||||
private readonly TextBox customer = new() { Name = "Customer", MinWidth = 260 };
|
||||
private readonly CheckBox formed = new() { Name = "Formed", Content = "Formed after cutting" };
|
||||
private readonly NumericUpDown quantity = new()
|
||||
{
|
||||
Name = "Quantity",
|
||||
Minimum = 0,
|
||||
Maximum = DrawingProperties.MaxQuantity,
|
||||
Increment = 1,
|
||||
FormatString = "0",
|
||||
ClipValueToMinMax = true,
|
||||
MinWidth = 140,
|
||||
};
|
||||
private readonly NumericUpDown priority = new()
|
||||
{
|
||||
Name = "Priority",
|
||||
Minimum = DrawingProperties.MinPriority,
|
||||
Maximum = DrawingProperties.MaxPriority,
|
||||
Increment = 1,
|
||||
FormatString = "0",
|
||||
ClipValueToMinMax = true,
|
||||
MinWidth = 140,
|
||||
};
|
||||
private readonly CheckBox cutFirst = new() { Name = "CutFirst", Content = "Cut first" };
|
||||
private readonly CheckBox allowCommonLine = new() { Name = "AllowCommonLine", Content = "Allow common line" };
|
||||
private readonly TextBlock nested = new() { Name = "Nested", VerticalAlignment = VerticalAlignment.Center };
|
||||
private readonly Border colorSwatch = new()
|
||||
{
|
||||
Name = "ColorSwatch",
|
||||
Width = 28,
|
||||
Height = 20,
|
||||
BorderBrush = Brushes.Gray,
|
||||
BorderThickness = new Thickness(1),
|
||||
Cursor = new Cursor(StandardCursorType.Hand),
|
||||
};
|
||||
private readonly TextBlock colorMode = new() { Name = "ColorMode", VerticalAlignment = VerticalAlignment.Center };
|
||||
private readonly Button automaticColor = new() { Name = "AutomaticColor", Content = "Automatic" };
|
||||
private readonly Image preview = new() { Width = PreviewSize, Height = PreviewSize };
|
||||
|
||||
private DrawingPropertiesDialog(Drawing drawing, Nest nest)
|
||||
{
|
||||
properties = new DrawingProperties(drawing, nest);
|
||||
Title = properties.Title;
|
||||
SizeToContent = SizeToContent.WidthAndHeight;
|
||||
CanResize = false;
|
||||
ShowInTaskbar = false;
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||
|
||||
name.Text = properties.Name;
|
||||
customer.Text = properties.Customer;
|
||||
formed.IsChecked = properties.Formed;
|
||||
quantity.Value = properties.Quantity;
|
||||
priority.Value = properties.Priority;
|
||||
cutFirst.IsChecked = properties.CutFirst;
|
||||
allowCommonLine.IsChecked = properties.AllowCommonLine;
|
||||
quantity.ValueChanged += (_, _) => UpdateNested();
|
||||
|
||||
var changeColor = new Button { Name = "ChangeColor", Content = "Change Color..." };
|
||||
changeColor.Click += async (_, _) => await ChangeColorAsync();
|
||||
colorSwatch.PointerReleased += async (_, _) => await ChangeColorAsync();
|
||||
automaticColor.Click += (_, _) =>
|
||||
{
|
||||
properties.UseAutomaticColor();
|
||||
UpdateColor();
|
||||
};
|
||||
|
||||
var drawingGroup = Fields(
|
||||
("Name", name, null),
|
||||
("Customer", customer, null),
|
||||
(null, formed, "Bent or rolled; marked F on the nest report"));
|
||||
var nestingGroup = Fields(
|
||||
("Quantity", Row(quantity, nested), null),
|
||||
("Priority", priority, "Higher numbers nest first"),
|
||||
(null, cutFirst, "Cut before other parts on the plate"),
|
||||
(null, allowCommonLine, "Copies may share a cut, one kerf apart"));
|
||||
var colorGroup = new StackPanel
|
||||
{
|
||||
Spacing = 6,
|
||||
Children =
|
||||
{
|
||||
Row(colorSwatch, colorMode),
|
||||
Row(changeColor, automaticColor),
|
||||
},
|
||||
};
|
||||
var source = new TextBlock { Name = "Source", Text = properties.SourceText };
|
||||
if (properties.SourcePath != null)
|
||||
ToolTip.SetTip(source, properties.SourcePath);
|
||||
var detailsGroup = Fields(
|
||||
("Size", new TextBlock { Name = "DrawingSize", Text = properties.SizeText }, null),
|
||||
("Area", new TextBlock { Name = "Area", Text = properties.AreaText }, null),
|
||||
("Source", source, null));
|
||||
|
||||
var ok = new Button { Name = "Ok", Content = "OK", IsDefault = true, MinWidth = 90 };
|
||||
ok.Click += (_, _) => Close(Values());
|
||||
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
||||
cancel.Click += (_, _) => Close(null);
|
||||
|
||||
var fields = new StackPanel
|
||||
{
|
||||
Spacing = 14,
|
||||
Width = 420,
|
||||
Children =
|
||||
{
|
||||
Group("Drawing", drawingGroup),
|
||||
Group("Nesting", nestingGroup),
|
||||
Group("Color", colorGroup),
|
||||
Group("Details", detailsGroup),
|
||||
},
|
||||
};
|
||||
var previewBox = new Border
|
||||
{
|
||||
Background = Brushes.White,
|
||||
BorderBrush = Brush.Parse("#C8CDD2"),
|
||||
BorderThickness = new Thickness(1),
|
||||
Padding = new Thickness(12),
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
Child = preview,
|
||||
};
|
||||
Content = new StackPanel
|
||||
{
|
||||
Margin = new Thickness(18),
|
||||
Spacing = 18,
|
||||
Children =
|
||||
{
|
||||
new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
Spacing = 18,
|
||||
Children = { previewBox, fields },
|
||||
},
|
||||
new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
HorizontalAlignment = HorizontalAlignment.Right,
|
||||
Spacing = 8,
|
||||
Children = { ok, cancel },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
AddHandler(KeyDownEvent, (_, e) =>
|
||||
{
|
||||
if (e.Key != Key.Escape)
|
||||
return;
|
||||
e.Handled = true;
|
||||
Close(null);
|
||||
}, RoutingStrategies.Tunnel);
|
||||
Opened += (_, _) =>
|
||||
{
|
||||
UpdateColor();
|
||||
name.SelectAll();
|
||||
name.Focus();
|
||||
};
|
||||
UpdateNested();
|
||||
}
|
||||
|
||||
/// <summary>The edited values, or null when the user cancelled.</summary>
|
||||
public static Task<DrawingProperties> ShowAsync(Window owner, Drawing drawing, Nest nest) =>
|
||||
new DrawingPropertiesDialog(drawing, nest).ShowDialog<DrawingProperties>(owner);
|
||||
|
||||
private DrawingProperties Values()
|
||||
{
|
||||
properties.Name = name.Text ?? "";
|
||||
properties.Customer = customer.Text ?? "";
|
||||
properties.Quantity = (int)(quantity.Value ?? 0);
|
||||
properties.Priority = (int)(priority.Value ?? 0);
|
||||
properties.CutFirst = cutFirst.IsChecked == true;
|
||||
properties.AllowCommonLine = allowCommonLine.IsChecked == true;
|
||||
properties.Formed = formed.IsChecked == true;
|
||||
return properties;
|
||||
}
|
||||
|
||||
private void UpdateNested()
|
||||
{
|
||||
properties.Quantity = (int)(quantity.Value ?? 0);
|
||||
nested.Text = properties.NestedText;
|
||||
}
|
||||
|
||||
private void UpdateColor()
|
||||
{
|
||||
var color = properties.Color;
|
||||
colorSwatch.Background = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
|
||||
colorMode.Text = properties.ColorModeText;
|
||||
automaticColor.IsEnabled = properties.CanUseAutomaticColor;
|
||||
preview.Source = DrawingThumbnail.Preview(properties.Drawing, color, PreviewSize, PreviewSize,
|
||||
RenderScaling);
|
||||
}
|
||||
|
||||
private async Task ChangeColorAsync()
|
||||
{
|
||||
var picked = await ColorChoiceDialog.ShowAsync(this, properties.Color);
|
||||
if (picked is not { } color)
|
||||
return;
|
||||
// A picked color cancels a pending Automatic.
|
||||
properties.PickColor(color);
|
||||
UpdateColor();
|
||||
}
|
||||
|
||||
private static StackPanel Row(params Control[] controls)
|
||||
{
|
||||
var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8 };
|
||||
foreach (var control in controls)
|
||||
row.Children.Add(control);
|
||||
return row;
|
||||
}
|
||||
|
||||
// Labeled fields, each with an optional gray hint below it.
|
||||
private static Grid Fields(params (string Label, Control Field, string Hint)[] rows)
|
||||
{
|
||||
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("Auto,*"), ColumnSpacing = 10, RowSpacing = 6 };
|
||||
var row = 0;
|
||||
foreach (var (label, field, hint) in rows)
|
||||
{
|
||||
grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
if (label != null)
|
||||
{
|
||||
var text = new TextBlock { Text = label, VerticalAlignment = VerticalAlignment.Center };
|
||||
Grid.SetRow(text, row);
|
||||
grid.Children.Add(text);
|
||||
}
|
||||
Grid.SetRow(field, row);
|
||||
Grid.SetColumn(field, 1);
|
||||
grid.Children.Add(field);
|
||||
row++;
|
||||
if (hint == null)
|
||||
continue;
|
||||
grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
var note = new TextBlock { Text = hint, Foreground = Brushes.Gray, FontSize = 12, Margin = new Thickness(26, -4, 0, 0) };
|
||||
Grid.SetRow(note, row);
|
||||
Grid.SetColumn(note, 1);
|
||||
grid.Children.Add(note);
|
||||
row++;
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
// A titled box, as the WinForms group boxes.
|
||||
private static Control Group(string header, Control content) => new StackPanel
|
||||
{
|
||||
Spacing = 4,
|
||||
Children =
|
||||
{
|
||||
new TextBlock { Text = header, FontWeight = FontWeight.SemiBold },
|
||||
new Border
|
||||
{
|
||||
BorderBrush = Brush.Parse("#C8CDD2"),
|
||||
BorderThickness = new Thickness(1),
|
||||
Padding = new Thickness(10),
|
||||
Child = content,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change Color's chooser: the basic colors of the Windows color dialog the WinForms app
|
||||
/// shows, and a "#RRGGBB" box for any other. OK returns the color; Cancel or Esc returns null.
|
||||
/// </summary>
|
||||
internal sealed class ColorChoiceDialog : Window
|
||||
{
|
||||
private static readonly string[] BasicColors =
|
||||
{
|
||||
"#FF8080", "#FFFF80", "#80FF80", "#00FF80", "#80FFFF", "#0080FF", "#FF80C0", "#FF80FF",
|
||||
"#FF0000", "#FFFF00", "#80FF00", "#00FF40", "#00FFFF", "#0080C0", "#8080C0", "#FF00FF",
|
||||
"#804040", "#FF8040", "#00FF00", "#008080", "#004080", "#8080FF", "#800040", "#FF0080",
|
||||
"#800000", "#FF8000", "#008000", "#008040", "#0000FF", "#0000A0", "#800080", "#8000FF",
|
||||
"#400000", "#804000", "#004000", "#004040", "#000080", "#000040", "#400040", "#400080",
|
||||
"#000000", "#808000", "#808040", "#808080", "#408080", "#C0C0C0", "#404040", "#FFFFFF",
|
||||
};
|
||||
|
||||
private readonly TextBox hex = new() { Name = "Hex", Width = 110 };
|
||||
private readonly Border sample = new() { Width = 40, Height = 24, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1) };
|
||||
private readonly Button ok = new() { Content = "OK", IsDefault = true, MinWidth = 90 };
|
||||
|
||||
private ColorChoiceDialog(DrawingColor current)
|
||||
{
|
||||
Title = "Color";
|
||||
SizeToContent = SizeToContent.WidthAndHeight;
|
||||
CanResize = false;
|
||||
ShowInTaskbar = false;
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||
|
||||
var swatches = new UniformGrid { Columns = 8 };
|
||||
foreach (var text in BasicColors)
|
||||
{
|
||||
var color = Color.Parse(text);
|
||||
var swatch = new Button
|
||||
{
|
||||
Width = 26,
|
||||
Height = 22,
|
||||
Margin = new Thickness(2),
|
||||
Padding = new Thickness(0),
|
||||
Background = new SolidColorBrush(color),
|
||||
BorderBrush = Brushes.Gray,
|
||||
BorderThickness = new Thickness(1),
|
||||
};
|
||||
ToolTip.SetTip(swatch, text);
|
||||
swatch.Click += (_, _) => hex.Text = text;
|
||||
swatch.DoubleTapped += (_, _) => Close(Parse(text));
|
||||
swatches.Children.Add(swatch);
|
||||
}
|
||||
|
||||
hex.Text = $"#{current.R:X2}{current.G:X2}{current.B:X2}";
|
||||
hex.TextChanged += (_, _) => UpdateSample();
|
||||
ok.Click += (_, _) => Close(Parse(hex.Text));
|
||||
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
||||
cancel.Click += (_, _) => Close(null);
|
||||
|
||||
Content = new StackPanel
|
||||
{
|
||||
Margin = new Thickness(18),
|
||||
Spacing = 14,
|
||||
Children =
|
||||
{
|
||||
new TextBlock { Text = "Basic colors" },
|
||||
swatches,
|
||||
new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
Spacing = 8,
|
||||
Children = { new TextBlock { Text = "Color", VerticalAlignment = VerticalAlignment.Center }, hex, sample },
|
||||
},
|
||||
new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
HorizontalAlignment = HorizontalAlignment.Right,
|
||||
Spacing = 8,
|
||||
Children = { ok, cancel },
|
||||
},
|
||||
},
|
||||
};
|
||||
AddHandler(KeyDownEvent, (_, e) =>
|
||||
{
|
||||
if (e.Key != Key.Escape)
|
||||
return;
|
||||
e.Handled = true;
|
||||
Close(null);
|
||||
}, RoutingStrategies.Tunnel);
|
||||
UpdateSample();
|
||||
}
|
||||
|
||||
/// <summary>The chosen color, or null when the user cancelled.</summary>
|
||||
public static Task<DrawingColor?> ShowAsync(Window owner, DrawingColor current) =>
|
||||
new ColorChoiceDialog(current).ShowDialog<DrawingColor?>(owner);
|
||||
|
||||
// "#RRGGBB" (the # optional) as an opaque color; null when it is not one.
|
||||
internal static DrawingColor? Parse(string text)
|
||||
{
|
||||
var value = text?.Trim().TrimStart('#');
|
||||
if (value is not { Length: 6 }
|
||||
|| !int.TryParse(value, System.Globalization.NumberStyles.HexNumber,
|
||||
System.Globalization.CultureInfo.InvariantCulture, out var rgb))
|
||||
return null;
|
||||
return DrawingColor.FromArgb(255, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
|
||||
}
|
||||
|
||||
private void UpdateSample()
|
||||
{
|
||||
var color = Parse(hex.Text);
|
||||
ok.IsEnabled = color != null;
|
||||
sample.Background = color is { } c ? new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B)) : Brushes.Transparent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
using OpenNest.Collections;
|
||||
using OpenNest.Data;
|
||||
using OpenNest.Desktop;
|
||||
|
||||
namespace OpenNest.Avalonia
|
||||
{
|
||||
/// <summary>
|
||||
/// The Drawings tab, as the WinForms nest window's drawing list: each drawing's picture,
|
||||
/// name, "[on this plate] nested of required nested", size and area, refreshed shortly after
|
||||
/// parts come and go on the shown plate or the nest is edited. Its toolbar sorts the list
|
||||
/// (the choice shared with the WinForms app) and hides the fully nested drawings; its menu
|
||||
/// deletes a drawing or removes the unused ones, and a double-click opens Drawing Properties.
|
||||
/// Each edit is one undo step of the nest; none runs while Auto Nest works on it.
|
||||
/// </summary>
|
||||
public partial class NestView
|
||||
{
|
||||
// As the WinForms list's timer: a fill's many per-part events refresh the list once.
|
||||
private static readonly TimeSpan DrawingListDelay = TimeSpan.FromMilliseconds(50);
|
||||
|
||||
private readonly ObservableCollection<DrawingListItem> drawingItems = new();
|
||||
private DispatcherTimer drawingListTimer;
|
||||
private readonly MenuFlyout sortDrawingsMenu = new() { Placement = PlacementMode.BottomEdgeAlignedLeft };
|
||||
private DrawingSortOrder drawingSortOrder;
|
||||
private Plate watchedPlate;
|
||||
|
||||
private void InitializeDrawingList()
|
||||
{
|
||||
DrawingList.ItemsSource = drawingItems;
|
||||
drawingListTimer = new DispatcherTimer { Interval = DrawingListDelay };
|
||||
drawingListTimer.Tick += (_, _) =>
|
||||
{
|
||||
drawingListTimer.Stop();
|
||||
RefreshDrawingList();
|
||||
};
|
||||
// Recorded edits (Drawing Properties included) can change any row.
|
||||
Tab.Undo.Changed += (_, _) => ScheduleDrawingListRefresh();
|
||||
|
||||
drawingSortOrder = DrawingSort.Parse(UserSettingText.ReadString(
|
||||
UserSettingsFile.Read(UserSettingsFile.DefaultPath), DrawingSort.SettingName, null));
|
||||
foreach (var (order, text) in DrawingSort.Choices)
|
||||
{
|
||||
var item = new MenuItem { Header = text, Tag = order, ToggleType = MenuItemToggleType.Radio };
|
||||
item.Click += (_, _) => SetDrawingSortOrder(order);
|
||||
sortDrawingsMenu.Items.Add(item);
|
||||
}
|
||||
SortDrawingsButton.Flyout = sortDrawingsMenu;
|
||||
UpdateSortDrawingsChoice();
|
||||
|
||||
HideNestedButton.IsCheckedChanged += (_, _) => RefreshDrawingList();
|
||||
DrawingEmptyText.Text = DrawingListRow.AllHiddenText;
|
||||
DrawingEmptyText.PointerReleased += (_, e) =>
|
||||
{
|
||||
if (e.InitialPressMouseButton == MouseButton.Left)
|
||||
HideNestedButton.IsChecked = false;
|
||||
};
|
||||
|
||||
DrawingList.DoubleTapped += async (_, e) =>
|
||||
{
|
||||
if (DrawingAt(e.Source) is { } drawing)
|
||||
await EditDrawingPropertiesAsync(drawing);
|
||||
};
|
||||
DeleteDrawingMenuItem.Click += async (_, _) => await DeleteSelectedDrawingAsync();
|
||||
RemoveUnusedDrawingsMenuItem.Click += async (_, _) => await RemoveUnusedDrawingsAsync();
|
||||
DrawingMenu.Opening += (_, _) =>
|
||||
{
|
||||
// As WinForms: Delete needs a selected drawing; Remove Unused acts on the nest.
|
||||
DeleteDrawingMenuItem.IsVisible = SelectedDrawing != null;
|
||||
DrawingMenuSeparator.IsVisible = SelectedDrawing != null;
|
||||
DeleteDrawingMenuItem.IsEnabled = CanEditDrawings;
|
||||
RemoveUnusedDrawingsMenuItem.IsEnabled = CanEditDrawings;
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The drawing selected in the list, or null.</summary>
|
||||
public Drawing SelectedDrawing => (DrawingList.SelectedItem as DrawingListItem)?.Drawing;
|
||||
|
||||
/// <summary>Whether the drawing commands can run: not while Auto Nest works on the nest.</summary>
|
||||
public bool CanEditDrawings => !Canvas.EditsLocked;
|
||||
|
||||
/// <summary>Whether Hide Fully Nested Drawings is on.</summary>
|
||||
public bool HidesFullyNestedDrawings
|
||||
{
|
||||
get => HideNestedButton.IsChecked == true;
|
||||
set => HideNestedButton.IsChecked = value;
|
||||
}
|
||||
|
||||
private static Drawing DrawingAt(object source) =>
|
||||
(source as Visual)?.FindAncestorOfType<ListBoxItem>(includeSelf: true)?.DataContext is DrawingListItem item
|
||||
? item.Drawing
|
||||
: null;
|
||||
|
||||
/// <summary>Refreshes the list shortly, once however many parts change before then.</summary>
|
||||
private void ScheduleDrawingListRefresh()
|
||||
{
|
||||
if (!Dispatcher.UIThread.CheckAccess())
|
||||
{
|
||||
Dispatcher.UIThread.Post(ScheduleDrawingListRefresh);
|
||||
return;
|
||||
}
|
||||
drawingListTimer.Stop();
|
||||
drawingListTimer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the nest's drawings in the chosen order, less the fully nested ones when hidden.
|
||||
/// Rows update in place while the drawings and their order are unchanged; otherwise the
|
||||
/// list is rebuilt keeping the selected drawing (by reference, or by name for the new
|
||||
/// objects an Undo brings) and the scroll position.
|
||||
/// </summary>
|
||||
private void RefreshDrawingList()
|
||||
{
|
||||
drawingListTimer?.Stop();
|
||||
var nest = Tab.Document.Nest;
|
||||
var units = nest.Units;
|
||||
var plate = Canvas.Plate;
|
||||
var scaling = TopLevel.GetTopLevel(this)?.RenderScaling ?? 1;
|
||||
var arranged = DrawingSort.Arrange(nest.Drawings.Where(d => !d.IsCutOff), drawingSortOrder,
|
||||
HidesFullyNestedDrawings);
|
||||
|
||||
var unchanged = arranged.Count == drawingItems.Count;
|
||||
for (var i = 0; unchanged && i < arranged.Count; i++)
|
||||
unchanged = ReferenceEquals(arranged[i], drawingItems[i].Drawing);
|
||||
|
||||
if (unchanged)
|
||||
{
|
||||
for (var i = 0; i < arranged.Count; i++)
|
||||
drawingItems[i].Update(DrawingListRow.For(arranged[i], plate, units), scaling);
|
||||
}
|
||||
else
|
||||
{
|
||||
var selected = SelectedDrawing;
|
||||
var scroller = DrawingList.FindDescendantOfType<ScrollViewer>();
|
||||
var offset = scroller?.Offset ?? default;
|
||||
drawingItems.Clear();
|
||||
foreach (var drawing in arranged)
|
||||
drawingItems.Add(new DrawingListItem(DrawingListRow.For(drawing, plate, units), scaling));
|
||||
DrawingList.SelectedItem = selected == null
|
||||
? null
|
||||
: drawingItems.FirstOrDefault(item => ReferenceEquals(item.Drawing, selected))
|
||||
?? drawingItems.FirstOrDefault(item => item.Drawing.Name == selected.Name);
|
||||
if (scroller != null)
|
||||
Dispatcher.UIThread.Post(() => scroller.Offset = offset, DispatcherPriority.Background);
|
||||
}
|
||||
|
||||
DrawingEmptyText.IsVisible = drawingItems.Count == 0
|
||||
&& DrawingListRow.EmptyText(HidesFullyNestedDrawings, nest.Drawings.Count) != null;
|
||||
}
|
||||
|
||||
// The shown plate's parts change the "[n]" counts and the nested quantities.
|
||||
private void WatchShownPlate(Plate plate)
|
||||
{
|
||||
if (ReferenceEquals(watchedPlate, plate))
|
||||
return;
|
||||
if (watchedPlate != null)
|
||||
{
|
||||
watchedPlate.PartAdded -= OnShownPlatePartAdded;
|
||||
watchedPlate.PartRemoved -= OnShownPlatePartRemoved;
|
||||
watchedPlate.PartsReordered -= OnShownPlatePartsReordered;
|
||||
}
|
||||
watchedPlate = plate;
|
||||
if (plate != null)
|
||||
{
|
||||
plate.PartAdded += OnShownPlatePartAdded;
|
||||
plate.PartRemoved += OnShownPlatePartRemoved;
|
||||
plate.PartsReordered += OnShownPlatePartsReordered;
|
||||
}
|
||||
// The counts on the newly shown plate.
|
||||
if (drawingListTimer != null)
|
||||
ScheduleDrawingListRefresh();
|
||||
}
|
||||
|
||||
private void OnShownPlatePartAdded(object sender, ItemAddedEventArgs<Part> e) => ScheduleDrawingListRefresh();
|
||||
|
||||
private void OnShownPlatePartRemoved(object sender, ItemRemovedEventArgs<Part> e) => ScheduleDrawingListRefresh();
|
||||
|
||||
private void OnShownPlatePartsReordered(object sender, EventArgs e) => ScheduleDrawingListRefresh();
|
||||
|
||||
private void SetDrawingSortOrder(DrawingSortOrder order)
|
||||
{
|
||||
if (order != drawingSortOrder)
|
||||
{
|
||||
drawingSortOrder = order;
|
||||
// As WinForms' DrawingSortOrder setting, which the WinForms app reads too.
|
||||
UserSettingsFile.Update(UserSettingsFile.DefaultPath,
|
||||
values => values[DrawingSort.SettingName] = order.ToString());
|
||||
RefreshDrawingList();
|
||||
}
|
||||
UpdateSortDrawingsChoice();
|
||||
}
|
||||
|
||||
private void UpdateSortDrawingsChoice()
|
||||
{
|
||||
foreach (var item in sortDrawingsMenu.Items.OfType<MenuItem>())
|
||||
item.IsChecked = item.Tag is DrawingSortOrder order && order == drawingSortOrder;
|
||||
var text = DrawingSort.Choices.First(c => c.Order == drawingSortOrder).Text;
|
||||
ToolTip.SetTip(SortDrawingsButton, $"Sort drawings: {text}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a drawing command, then rebuilds both lists and redraws the plate (a drawing's
|
||||
/// color may have changed), keeping its zoom, and its selection while its parts are
|
||||
/// unchanged. A command that removes drawings first ends the plate tool
|
||||
/// (<paramref name="endTool"/>), which may be placing one of them.
|
||||
/// </summary>
|
||||
private void RunDrawingCommand(Action command, bool endTool)
|
||||
{
|
||||
if (endTool)
|
||||
{
|
||||
Canvas.Tools.EndTool();
|
||||
pickup.PutDown();
|
||||
pressedDrawing = null;
|
||||
}
|
||||
runningPlateCommand = true;
|
||||
try
|
||||
{
|
||||
command();
|
||||
}
|
||||
finally
|
||||
{
|
||||
runningPlateCommand = false;
|
||||
}
|
||||
ShowPlates(System.Math.Max(0, PlateList.SelectedIndex), keep: true);
|
||||
}
|
||||
|
||||
/// <summary>The drawing menu's Delete: confirms, then deletes the drawing and its parts.</summary>
|
||||
public async Task DeleteSelectedDrawingAsync()
|
||||
{
|
||||
var drawing = SelectedDrawing;
|
||||
if (drawing == null || !CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
||||
return;
|
||||
if (!await ConfirmDialog.AskAsync(owner, DrawingCommands.DeleteTitle,
|
||||
DrawingCommands.DeletePrompt(drawing), "Delete"))
|
||||
return;
|
||||
// Auto Nest may have started, or an Undo swapped the nest, while the question was open.
|
||||
if (!CanEditDrawings || !Tab.Document.Nest.Drawings.Contains(drawing))
|
||||
return;
|
||||
RunDrawingCommand(() => DrawingCommands.DeleteDrawing(Tab.Document.Nest, drawing,
|
||||
label => Tab.Undo.BeginChange(label)), endTool: true);
|
||||
}
|
||||
|
||||
/// <summary>The drawing menu's Remove Unused Drawings: confirms, then removes them.</summary>
|
||||
public async Task RemoveUnusedDrawingsAsync()
|
||||
{
|
||||
if (!CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
||||
return;
|
||||
if (!await ConfirmDialog.AskAsync(owner, DrawingCommands.CleanUnusedTitle,
|
||||
DrawingCommands.CleanUnusedPrompt, "Yes"))
|
||||
return;
|
||||
if (!CanEditDrawings)
|
||||
return;
|
||||
if (DrawingCommands.HasUnusedDrawings(Tab.Document.Nest))
|
||||
RunDrawingCommand(() => DrawingCommands.CleanUnusedDrawings(Tab.Document.Nest,
|
||||
label => Tab.Undo.BeginChange(label)), endTool: true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A double-click on a drawing: the Drawing Properties dialog; OK saves the values as one
|
||||
/// undo step and redraws the drawing's parts in their color.
|
||||
/// </summary>
|
||||
public async Task EditDrawingPropertiesAsync(Drawing drawing)
|
||||
{
|
||||
// As WinForms: the double-click puts a picked-up drawing down.
|
||||
pickup.PutDown();
|
||||
pressedDrawing = null;
|
||||
if (drawing == null || !CanEditDrawings || TopLevel.GetTopLevel(this) is not Window owner)
|
||||
return;
|
||||
var nest = Tab.Document.Nest;
|
||||
var properties = await DrawingPropertiesDialog.ShowAsync(owner, drawing, nest);
|
||||
if (properties == null || !CanEditDrawings || !ReferenceEquals(nest, Tab.Document.Nest)
|
||||
|| !nest.Drawings.Contains(drawing))
|
||||
return;
|
||||
RunDrawingCommand(() => DrawingCommands.ApplyProperties(properties,
|
||||
label => Tab.Undo.BeginChange(label)), endTool: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,16 +40,63 @@
|
||||
</Panel>
|
||||
</TabItem>
|
||||
<TabItem Header="Drawings" FontSize="14" MinHeight="34">
|
||||
<DataGrid x:Name="DrawingGrid" x:DataType="local:DrawingRow" IsReadOnly="True"
|
||||
SelectionMode="Single"
|
||||
FontSize="13" AutoGenerateColumns="False"
|
||||
GridLinesVisibility="Horizontal" CanUserSortColumns="True">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Drawing" Binding="{Binding Name}" Width="*" />
|
||||
<DataGridTextColumn Header="Nested" Binding="{Binding Nested}" />
|
||||
<DataGridTextColumn Header="Required" Binding="{Binding Required}" />
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<!-- As the WinForms drawing list and its toolbar (NestView.Drawings.cs): a press picks
|
||||
a drawing up for the plate, a drag drops one part, a double-click opens Drawing
|
||||
Properties. -->
|
||||
<DockPanel>
|
||||
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="4" Margin="4,3">
|
||||
<!-- Its menu lists the shared sort choices (NestView.Drawings.cs). -->
|
||||
<Button x:Name="SortDrawingsButton" Content="Sort ▾" FontSize="12" />
|
||||
<ToggleButton x:Name="HideNestedButton" Content="Hide nested" FontSize="12"
|
||||
ToolTip.Tip="Hide fully nested drawings" />
|
||||
</StackPanel>
|
||||
<Panel>
|
||||
<ListBox x:Name="DrawingList" SelectionMode="Single" Background="White">
|
||||
<ListBox.Styles>
|
||||
<Style Selector="ListBoxItem">
|
||||
<Setter Property="Padding" Value="0" />
|
||||
<Setter Property="BorderThickness" Value="2" />
|
||||
<Setter Property="BorderBrush" Value="Transparent" />
|
||||
</Style>
|
||||
<!-- As WinForms: the selected row is outlined in the highlight color. -->
|
||||
<Style Selector="ListBoxItem:selected /template/ ContentPresenter">
|
||||
<Setter Property="BorderBrush" Value="#0078D7" />
|
||||
</Style>
|
||||
</ListBox.Styles>
|
||||
<ListBox.ContextMenu>
|
||||
<ContextMenu x:Name="DrawingMenu">
|
||||
<MenuItem x:Name="DeleteDrawingMenuItem" Header="Delete" />
|
||||
<Separator x:Name="DrawingMenuSeparator" />
|
||||
<MenuItem x:Name="RemoveUnusedDrawingsMenuItem" Header="Remove Unused Drawings..." />
|
||||
</ContextMenu>
|
||||
</ListBox.ContextMenu>
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate x:DataType="local:DrawingListItem">
|
||||
<Border Background="{Binding Background}" MinHeight="85"
|
||||
BorderBrush="LightGray" BorderThickness="0,0,0,1">
|
||||
<Grid ColumnDefinitions="4,Auto,*">
|
||||
<Border Grid.Column="0" Background="{Binding Bar}" />
|
||||
<Image Grid.Column="1" Source="{Binding Thumbnail}" Width="85" Height="75"
|
||||
Margin="1,5,10,5" VerticalAlignment="Top" />
|
||||
<StackPanel Grid.Column="2" Margin="0,4,4,4">
|
||||
<TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="13"
|
||||
Foreground="Black" TextTrimming="CharacterEllipsis" />
|
||||
<TextBlock Text="{Binding Quantity}" Foreground="Gray" FontSize="12"
|
||||
Margin="0,3,0,0" IsVisible="{Binding HasQuantity}" />
|
||||
<TextBlock Text="{Binding Size}" Foreground="Gray" FontSize="12" />
|
||||
<TextBlock Text="{Binding Area}" Foreground="Gray" FontSize="12" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
<!-- Shown when Hide nested emptied the list; clicking it shows the drawings again. -->
|
||||
<TextBlock x:Name="DrawingEmptyText" IsVisible="False" Foreground="Gray"
|
||||
TextWrapping="Wrap" TextAlignment="Center" Margin="12"
|
||||
HorizontalAlignment="Stretch" VerticalAlignment="Top" Cursor="Hand" />
|
||||
</Panel>
|
||||
</DockPanel>
|
||||
</TabItem>
|
||||
</TabControl>
|
||||
</Border>
|
||||
|
||||
@@ -12,8 +12,6 @@ using OpenNest.Desktop.Interaction;
|
||||
|
||||
namespace OpenNest.Avalonia
|
||||
{
|
||||
public sealed record DrawingRow(string Name, int Nested, int Required, Drawing Drawing);
|
||||
|
||||
/// <summary>
|
||||
/// One open nest: Plates and Drawings tabs on the left, the plate header and plate view on
|
||||
/// the right. Each tab keeps its own view, so switching nests keeps each one's plate and zoom.
|
||||
@@ -38,14 +36,15 @@ namespace OpenNest.Avalonia
|
||||
plates = new PlateManager(tab.Document.Nest);
|
||||
// Starts watching the last two plates (the tab already put the empty plate there).
|
||||
plates.EnsureSentinel();
|
||||
InitializeDrawingList();
|
||||
Refresh(0);
|
||||
// Adding parts from the drawing list, as the WinForms nest window: a press picks a
|
||||
// drawing up for the plate view, a drag drops one part on the plate.
|
||||
DrawingGrid.AddHandler(PointerPressedEvent, OnDrawingPressed, RoutingStrategies.Tunnel,
|
||||
DrawingList.AddHandler(PointerPressedEvent, OnDrawingPressed, RoutingStrategies.Tunnel,
|
||||
handledEventsToo: true);
|
||||
DrawingGrid.AddHandler(PointerMovedEvent, OnDrawingDragged, RoutingStrategies.Tunnel,
|
||||
DrawingList.AddHandler(PointerMovedEvent, OnDrawingDragged, RoutingStrategies.Tunnel,
|
||||
handledEventsToo: true);
|
||||
DrawingGrid.AddHandler(PointerReleasedEvent, (_, _) => pressedDrawing = null,
|
||||
DrawingList.AddHandler(PointerReleasedEvent, (_, _) => pressedDrawing = null,
|
||||
RoutingStrategies.Tunnel, handledEventsToo: true);
|
||||
Canvas.GotFocus += OnCanvasFocused;
|
||||
DragDrop.SetAllowDrop(Canvas, true);
|
||||
@@ -62,17 +61,13 @@ namespace OpenNest.Avalonia
|
||||
private Drawing pressedDrawing;
|
||||
private Point pressPoint;
|
||||
|
||||
private static Drawing DrawingUnder(PointerEventArgs e) =>
|
||||
(e.Source as Visual)?.FindAncestorOfType<DataGridRow>(includeSelf: true)
|
||||
?.DataContext is DrawingRow row
|
||||
? row.Drawing
|
||||
: null;
|
||||
private static Drawing DrawingUnder(PointerEventArgs e) => DrawingAt(e.Source);
|
||||
|
||||
// As the WinForms list: a single left press picks the drawing up (a drag can start from
|
||||
// it); a double-click puts it down.
|
||||
private void OnDrawingPressed(object sender, PointerPressedEventArgs e)
|
||||
{
|
||||
var point = e.GetCurrentPoint(DrawingGrid);
|
||||
var point = e.GetCurrentPoint(DrawingList);
|
||||
pressPoint = point.Position;
|
||||
pressedDrawing = null;
|
||||
if (!point.Properties.IsLeftButtonPressed)
|
||||
@@ -91,7 +86,7 @@ namespace OpenNest.Avalonia
|
||||
private async void OnDrawingDragged(object sender, PointerEventArgs e)
|
||||
{
|
||||
var drawing = pressedDrawing;
|
||||
var point = e.GetCurrentPoint(DrawingGrid);
|
||||
var point = e.GetCurrentPoint(DrawingList);
|
||||
if (drawing == null || !point.Properties.IsLeftButtonPressed)
|
||||
return;
|
||||
var dx = point.Position.X - pressPoint.X;
|
||||
@@ -126,7 +121,7 @@ namespace OpenNest.Avalonia
|
||||
|
||||
private void OnCanvasFocused(object sender, GotFocusEventArgs e)
|
||||
{
|
||||
var selected = (DrawingGrid.SelectedItem as DrawingRow)?.Drawing;
|
||||
var selected = SelectedDrawing;
|
||||
if (pickup.TakeOnPlateEnter(selected) is { } drawing)
|
||||
Canvas.PlaceDrawing(drawing);
|
||||
}
|
||||
@@ -245,17 +240,11 @@ namespace OpenNest.Avalonia
|
||||
{
|
||||
rebuildingPlateList = false;
|
||||
}
|
||||
// As the WinForms list: the selected drawing stays selected when the list is rebuilt,
|
||||
// matched as WinForms matches it (by name), so it survives an Undo's new objects.
|
||||
var selected = (DrawingGrid.SelectedItem as DrawingRow)?.Drawing;
|
||||
DrawingGrid.ItemsSource = nest.Drawings
|
||||
.Where(d => !d.IsCutOff)
|
||||
.Select(d => new DrawingRow(d.Name, d.Quantity.Nested, d.Quantity.Required, d))
|
||||
.ToList();
|
||||
DrawingGrid.SelectedItem = ((IEnumerable<DrawingRow>)DrawingGrid.ItemsSource)
|
||||
.FirstOrDefault(row => Equals(row.Drawing, selected));
|
||||
shownCounts = CountsText(nest);
|
||||
ShowPlate();
|
||||
// After the plate: each row counts the drawing's parts on the shown plate. The
|
||||
// selected drawing stays selected, matched by name after an Undo's new objects.
|
||||
RefreshDrawingList();
|
||||
}
|
||||
|
||||
// As the WinForms nest window binding a nest: the saved head-lock defaults in the nest's
|
||||
@@ -335,6 +324,7 @@ namespace OpenNest.Avalonia
|
||||
plate?.Parts.Count == keptSelection.PartCount ? keptSelection.Indices : Array.Empty<int>());
|
||||
else
|
||||
Canvas.Plate = plate;
|
||||
WatchShownPlate(plate);
|
||||
PlateLabel.Text = plate == null
|
||||
? "No plates"
|
||||
: $"Plate {index + 1} of {nest.Plates.Count} "
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Avalonia.Media.Imaging;
|
||||
using OpenNest.Desktop.Rendering;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace OpenNest.Avalonia.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// A drawing's picture for the Drawings tab and the Drawing Properties dialog, drawn as the
|
||||
/// WinForms <c>Program.GetImage</c> draws it: the material filled in the drawing's color, the
|
||||
/// etches in the scheme's etch color and the cuts over them in the part outline color, fitted
|
||||
/// inside a 5 pixel margin with the y axis up. Lead-ins are not drawn.
|
||||
/// </summary>
|
||||
internal static class DrawingThumbnail
|
||||
{
|
||||
private sealed record Key(CNC.Program Program, uint Fill, uint Outline, uint Etch, int Width, int Height);
|
||||
|
||||
private sealed record Entry(Key Key, Bitmap Image);
|
||||
|
||||
// One picture per drawing, redrawn when its program, color, the scheme or the size changes.
|
||||
private static readonly ConditionalWeakTable<Drawing, Entry> Cache = new();
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="drawing"/>'s picture <paramref name="width"/> by <paramref name="height"/>
|
||||
/// device-independent pixels, drawn at <paramref name="scaling"/> pixels each; null when
|
||||
/// it has nothing to draw.
|
||||
/// </summary>
|
||||
public static Bitmap For(Drawing drawing, int width, int height, double scaling = 1)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
if (drawing.Program == null || width <= 0 || height <= 0)
|
||||
return null;
|
||||
|
||||
var palette = SkiaSchemePalette.For(ColorScheme.Default);
|
||||
var fill = SkiaSchemePalette.ToSk(drawing.Color);
|
||||
var pixelWidth = System.Math.Max(1, (int)System.Math.Round(width * scaling));
|
||||
var pixelHeight = System.Math.Max(1, (int)System.Math.Round(height * scaling));
|
||||
var key = new Key(drawing.Program, (uint)fill, (uint)palette.PartOutline(fill), (uint)palette.Etch,
|
||||
pixelWidth, pixelHeight);
|
||||
if (Cache.TryGetValue(drawing, out var entry) && entry.Key == key)
|
||||
return entry.Image;
|
||||
|
||||
var image = Render(drawing.Program, pixelWidth, pixelHeight, scaling, fill, palette.PartOutline(fill), palette.Etch);
|
||||
Cache.AddOrUpdate(drawing, new Entry(key, image));
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <paramref name="drawing"/>'s picture filled in <paramref name="color"/> rather than its
|
||||
/// own, not cached: the Drawing Properties dialog's preview of a color not yet saved.
|
||||
/// </summary>
|
||||
public static Bitmap Preview(Drawing drawing, System.Drawing.Color color, int width, int height, double scaling = 1)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
if (drawing.Program == null || width <= 0 || height <= 0)
|
||||
return null;
|
||||
var palette = SkiaSchemePalette.For(ColorScheme.Default);
|
||||
var fill = SkiaSchemePalette.ToSk(color);
|
||||
return Render(drawing.Program,
|
||||
System.Math.Max(1, (int)System.Math.Round(width * scaling)),
|
||||
System.Math.Max(1, (int)System.Math.Round(height * scaling)),
|
||||
scaling, fill, palette.PartOutline(fill), palette.Etch);
|
||||
}
|
||||
|
||||
private static Bitmap Render(CNC.Program program, int width, int height, double scaling,
|
||||
SKColor fill, SKColor outline, SKColor etch)
|
||||
{
|
||||
var layers = ProgramGeometry.Layers(program);
|
||||
using var cutPath = SkiaFigures.ToPath(layers.Cut);
|
||||
using var etchPath = SkiaFigures.ToPath(layers.Etch);
|
||||
using var fillPath = SkiaFigures.ToPath(ProgramGeometry.Fill(program));
|
||||
var hasCuts = cutPath.PointCount > 0;
|
||||
var hasEtches = etchPath.PointCount > 0;
|
||||
if (!hasCuts && !hasEtches)
|
||||
return null;
|
||||
|
||||
var bounds = hasCuts ? cutPath.Bounds : etchPath.Bounds;
|
||||
if (hasCuts && hasEtches)
|
||||
bounds = SKRect.Union(bounds, etchPath.Bounds);
|
||||
|
||||
// As WinForms: fit 10 pixels inside the picture (5 each side), centered, y up.
|
||||
var margin = (float)(10 * scaling);
|
||||
var scale = System.Math.Min(
|
||||
(height - margin) / System.Math.Max(bounds.Height, 0.001f),
|
||||
(width - margin) / System.Math.Max(bounds.Width, 0.001f));
|
||||
var matrix = SKMatrix.CreateScale(scale, -scale);
|
||||
matrix = matrix.PostConcat(SKMatrix.CreateTranslation(
|
||||
(width - bounds.Width * scale) / 2 - bounds.Left * scale,
|
||||
(height - bounds.Height * scale) / 2 + bounds.Bottom * scale));
|
||||
cutPath.Transform(matrix);
|
||||
etchPath.Transform(matrix);
|
||||
fillPath.Transform(matrix);
|
||||
|
||||
using var surface = SKSurface.Create(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul));
|
||||
var canvas = surface.Canvas;
|
||||
canvas.Clear(SKColors.Transparent);
|
||||
using (var paint = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Fill, Color = fill })
|
||||
canvas.DrawPath(fillPath, paint);
|
||||
using (var paint = new SKPaint
|
||||
{
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
Color = etch,
|
||||
StrokeWidth = (float)(1.5 * scaling),
|
||||
})
|
||||
canvas.DrawPath(etchPath, paint);
|
||||
// A coincident cut must remain visible over an etch.
|
||||
using (var paint = new SKPaint
|
||||
{
|
||||
IsAntialias = true,
|
||||
Style = SKPaintStyle.Stroke,
|
||||
Color = outline,
|
||||
StrokeWidth = (float)scaling,
|
||||
})
|
||||
canvas.DrawPath(cutPath, paint);
|
||||
|
||||
using var snapshot = surface.Snapshot();
|
||||
using var png = snapshot.Encode(SKEncodedImageFormat.Png, 100);
|
||||
using var stream = new MemoryStream();
|
||||
png.SaveTo(stream);
|
||||
stream.Position = 0;
|
||||
return new Bitmap(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using System.Drawing;
|
||||
using OpenNest.Undo;
|
||||
|
||||
namespace OpenNest.Desktop.Tests.Drawings;
|
||||
|
||||
public class DrawingCommandsTests
|
||||
{
|
||||
/// <summary>
|
||||
/// A nest window's state: "square" (two required) with one part on each of two plates,
|
||||
/// "unused" with none, the trailing empty plate and a history started on it.
|
||||
/// </summary>
|
||||
private sealed class Env
|
||||
{
|
||||
public Env()
|
||||
{
|
||||
var nest = TestNests.OneSquare();
|
||||
Square = nest.Drawings.First();
|
||||
Square.Quantity.Required = 2;
|
||||
Unused = new Drawing("unused", (CNC.Program)Square.Program.Clone()) { Color = Color.Coral };
|
||||
nest.Drawings.Add(Unused);
|
||||
nest.Plates[0].Quantity = 1;
|
||||
var second = new Plate(new Geometry.Size(48, 96)) { Quantity = 1 };
|
||||
second.Parts.Add(new Part(Square));
|
||||
nest.Plates.Add(second);
|
||||
nest.UpdateDrawingQuantities();
|
||||
Document = new Document { Nest = nest };
|
||||
Plates = new PlateManager(nest);
|
||||
Plates.EnsureSentinel();
|
||||
Undo = new NestUndoRecorder(Document);
|
||||
Undo.Reset();
|
||||
}
|
||||
|
||||
public Drawing Square { get; }
|
||||
public Drawing Unused { get; }
|
||||
public Document Document { get; }
|
||||
public Nest Nest => Document.Nest;
|
||||
public PlateManager Plates { get; }
|
||||
public NestUndoRecorder Undo { get; }
|
||||
|
||||
public ChangeScope BeginChange(string label) => Undo.BeginChange(label);
|
||||
|
||||
public int Steps => Undo.History.UndoCount;
|
||||
|
||||
public int SquareParts => Nest.Plates.Sum(p => p.Parts.Count(part => part.BaseDrawing.Name == "square"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDrawing_RemovesItsPartsAndTheDrawing_AsOneStep()
|
||||
{
|
||||
var env = new Env();
|
||||
Assert.Equal(2, env.SquareParts);
|
||||
|
||||
DrawingCommands.DeleteDrawing(env.Nest, env.Square, env.BeginChange);
|
||||
|
||||
Assert.Equal(0, env.SquareParts);
|
||||
Assert.DoesNotContain(env.Square, env.Nest.Drawings);
|
||||
Assert.Single(env.Nest.Drawings);
|
||||
Assert.Equal(1, env.Steps);
|
||||
Assert.Equal("Undo Delete Drawing", env.Undo.UndoText);
|
||||
|
||||
env.Undo.Undo();
|
||||
Assert.Equal(2, env.SquareParts);
|
||||
Assert.Equal(2, env.Nest.Drawings.Count);
|
||||
Assert.Equal(2, env.Nest.Drawings.Single(d => d.Name == "square").Quantity.Nested);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeletePrompt_NamesTheDrawing() =>
|
||||
Assert.Equal("Delete drawing 'square' and all its parts from every plate?",
|
||||
DrawingCommands.DeletePrompt(new Drawing("square")));
|
||||
|
||||
[Fact]
|
||||
public void CleanUnusedDrawings_RemovesOnlyDrawingsWithNoParts_AsOneStep()
|
||||
{
|
||||
var env = new Env();
|
||||
Assert.True(DrawingCommands.HasUnusedDrawings(env.Nest));
|
||||
|
||||
Assert.True(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange));
|
||||
|
||||
Assert.Equal(new[] { "square" }, env.Nest.Drawings.Select(d => d.Name));
|
||||
Assert.Equal(1, env.Steps);
|
||||
Assert.Equal("Undo Clean Unused Drawings", env.Undo.UndoText);
|
||||
|
||||
env.Undo.Undo();
|
||||
Assert.Equal(2, env.Nest.Drawings.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CleanUnusedDrawings_WithNoneUnused_RecordsNothing()
|
||||
{
|
||||
var env = new Env();
|
||||
DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange);
|
||||
|
||||
Assert.False(DrawingCommands.CleanUnusedDrawings(env.Nest, env.BeginChange));
|
||||
|
||||
Assert.False(DrawingCommands.HasUnusedDrawings(env.Nest));
|
||||
Assert.Equal(1, env.Steps);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ApplyProperties_SavesTheValues_AsOneStep()
|
||||
{
|
||||
var env = new Env();
|
||||
var properties = new DrawingProperties(env.Square, env.Nest)
|
||||
{
|
||||
Name = "bracket",
|
||||
Customer = "Acme",
|
||||
Quantity = 7,
|
||||
Priority = -3,
|
||||
CutFirst = true,
|
||||
AllowCommonLine = true,
|
||||
Formed = true,
|
||||
};
|
||||
|
||||
DrawingCommands.ApplyProperties(properties, env.BeginChange);
|
||||
|
||||
Assert.Equal("bracket", env.Square.Name);
|
||||
Assert.Equal("Acme", env.Square.Customer);
|
||||
Assert.Equal(7, env.Square.Quantity.Required);
|
||||
Assert.Equal(2, env.Square.Quantity.Nested);
|
||||
Assert.Equal(-3, env.Square.Priority);
|
||||
Assert.True(env.Square.CutFirst);
|
||||
Assert.True(env.Square.AllowCommonLine);
|
||||
Assert.True(env.Square.IsFormed);
|
||||
Assert.False(env.Square.HasCustomColor);
|
||||
Assert.Equal(1, env.Steps);
|
||||
Assert.Equal("Undo Drawing Properties", env.Undo.UndoText);
|
||||
|
||||
env.Undo.Undo();
|
||||
var restored = env.Nest.Drawings.Single(d => d.Quantity.Nested == 2);
|
||||
Assert.Equal("square", restored.Name);
|
||||
Assert.Equal(2, restored.Quantity.Required);
|
||||
Assert.False(restored.CutFirst);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
namespace OpenNest.Desktop.Tests.Drawings;
|
||||
|
||||
public class DrawingListRowTests
|
||||
{
|
||||
private static (Nest Nest, Drawing Drawing) Nested(int required)
|
||||
{
|
||||
var nest = TestNests.OneSquare();
|
||||
var drawing = nest.Drawings.First();
|
||||
drawing.Quantity.Required = required;
|
||||
nest.Plates[0].Quantity = 1;
|
||||
nest.UpdateDrawingQuantities();
|
||||
return (nest, drawing);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShowsTheNameQuantitySizeAndArea()
|
||||
{
|
||||
var (_, drawing) = Nested(3);
|
||||
|
||||
var row = DrawingListRow.For(drawing, plate: null, Units.Inches);
|
||||
|
||||
Assert.Same(drawing, row.Drawing);
|
||||
Assert.Equal("square", row.Name);
|
||||
Assert.Equal("1 of 3 nested", row.Quantity);
|
||||
Assert.Equal("10 x 10", row.Size);
|
||||
Assert.Equal("100 sq/in", row.Area);
|
||||
Assert.False(row.IsHighlighted);
|
||||
Assert.Equal(DrawingQuantityBar.Short, row.Bar);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PrefixesTheCountOnTheShownPlate()
|
||||
{
|
||||
var (nest, drawing) = Nested(3);
|
||||
|
||||
Assert.Equal("[1] 1 of 3 nested", DrawingListRow.For(drawing, nest.Plates[0], Units.Inches).Quantity);
|
||||
Assert.Equal("1 of 3 nested", DrawingListRow.For(drawing, new Plate(), Units.Inches).Quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllNested_IsHighlightedWithACompleteBar()
|
||||
{
|
||||
var (_, drawing) = Nested(1);
|
||||
|
||||
var row = DrawingListRow.For(drawing, null, Units.Millimeters);
|
||||
|
||||
Assert.True(row.IsHighlighted);
|
||||
Assert.Equal(DrawingQuantityBar.Complete, row.Bar);
|
||||
Assert.Equal("100 sq/mm", row.Area);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoRequiredQuantity_HasNoBar_ButNestedPartsHighlight()
|
||||
{
|
||||
var (_, drawing) = Nested(0);
|
||||
|
||||
var row = DrawingListRow.For(drawing, null, Units.Inches);
|
||||
|
||||
Assert.Equal(DrawingQuantityBar.None, row.Bar);
|
||||
Assert.True(row.IsHighlighted);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HiddenQuantity_LeavesOutTheQuantityHighlightAndBar()
|
||||
{
|
||||
var (_, drawing) = Nested(1);
|
||||
|
||||
var row = DrawingListRow.For(drawing, null, Units.Inches, hideQuantity: true);
|
||||
|
||||
Assert.Null(row.Quantity);
|
||||
Assert.False(row.IsHighlighted);
|
||||
Assert.Equal(DrawingQuantityBar.None, row.Bar);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, 2, DrawingListRow.AllHiddenText)]
|
||||
[InlineData(true, 0, null)]
|
||||
[InlineData(false, 2, null)]
|
||||
public void EmptyText_ExplainsOnlyAListTheFilterEmptied(bool hide, int drawings, string expected) =>
|
||||
Assert.Equal(expected, DrawingListRow.EmptyText(hide, drawings));
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenNest.Desktop.Tests.Drawings;
|
||||
|
||||
[Collection(ColorSchemeState.Collection)]
|
||||
public class DrawingPropertiesTests
|
||||
{
|
||||
private static Drawing Square(string name = "square")
|
||||
{
|
||||
var drawing = TestNests.OneSquare().Drawings.First();
|
||||
drawing.Name = name;
|
||||
return drawing;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadsTheDrawingsValues()
|
||||
{
|
||||
var drawing = Square("bracket");
|
||||
drawing.Customer = "Acme";
|
||||
drawing.Quantity.Required = 5;
|
||||
drawing.Priority = 2;
|
||||
drawing.CutFirst = true;
|
||||
drawing.Source = new SourceInfo { Path = "/parts/bracket.dxf" };
|
||||
|
||||
var properties = new DrawingProperties(drawing);
|
||||
|
||||
Assert.Equal("bracket", properties.Name);
|
||||
Assert.Equal("Acme", properties.Customer);
|
||||
Assert.Equal(5, properties.Quantity);
|
||||
Assert.Equal(2, properties.Priority);
|
||||
Assert.True(properties.CutFirst);
|
||||
Assert.False(properties.AllowCommonLine);
|
||||
Assert.False(properties.Formed);
|
||||
Assert.Equal("Drawing Properties - bracket", properties.Title);
|
||||
Assert.Equal("10 x 10", properties.SizeText);
|
||||
Assert.Equal("100", properties.AreaText);
|
||||
Assert.Equal("bracket.dxf", properties.SourceText);
|
||||
Assert.Equal("/parts/bracket.dxf", properties.SourcePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithoutASource_ShowsADash()
|
||||
{
|
||||
var properties = new DrawingProperties(Square());
|
||||
|
||||
Assert.Null(properties.SourcePath);
|
||||
Assert.Equal("-", properties.SourceText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NestedText_CountsRemainingAgainstTheEditedQuantity()
|
||||
{
|
||||
var nest = TestNests.OneSquare();
|
||||
nest.Plates[0].Quantity = 1;
|
||||
nest.UpdateDrawingQuantities();
|
||||
var properties = new DrawingProperties(nest.Drawings.First(), nest);
|
||||
Assert.Equal("1 nested, 0 remaining", properties.NestedText);
|
||||
|
||||
properties.Quantity = 4;
|
||||
|
||||
Assert.Equal("1 nested, 3 remaining", properties.NestedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnlyAPickedColorBecomesCustom()
|
||||
{
|
||||
var drawing = Square();
|
||||
drawing.Color = Color.Coral;
|
||||
var unchanged = new DrawingProperties(drawing);
|
||||
Assert.Equal(DrawingProperties.AutomaticColorText, unchanged.ColorModeText);
|
||||
Assert.False(unchanged.CanUseAutomaticColor);
|
||||
unchanged.ApplyTo(drawing);
|
||||
Assert.False(drawing.HasCustomColor);
|
||||
|
||||
var picked = new DrawingProperties(drawing);
|
||||
picked.PickColor(Color.Red);
|
||||
Assert.Equal(DrawingProperties.CustomColorText, picked.ColorModeText);
|
||||
Assert.True(picked.CanUseAutomaticColor);
|
||||
picked.ApplyTo(drawing);
|
||||
|
||||
Assert.True(drawing.HasCustomColor);
|
||||
Assert.Equal(Color.Red.ToArgb(), drawing.Color.ToArgb());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AutomaticColor_ClearsTheCustomColor_WithTheDrawingsPaletteSlot()
|
||||
{
|
||||
var state = new ColorSchemeState();
|
||||
try
|
||||
{
|
||||
ColorSchemeRegistry.Apply(ColorSchemeRegistry.Get("Classic"));
|
||||
var nest = new Nest("Automatic");
|
||||
nest.Drawings.Add(new Drawing("First"));
|
||||
nest.Drawings.Add(new Drawing("Cutoff") { IsCutOff = true });
|
||||
var drawing = new Drawing("Bracket") { Color = Color.Red, HasCustomColor = true };
|
||||
nest.Drawings.Add(drawing);
|
||||
|
||||
var properties = new DrawingProperties(drawing, nest);
|
||||
Assert.Equal(DrawingProperties.CustomColorText, properties.ColorModeText);
|
||||
|
||||
properties.UseAutomaticColor();
|
||||
|
||||
Assert.Equal(DrawingProperties.AutomaticColorText, properties.ColorModeText);
|
||||
Assert.False(properties.CanUseAutomaticColor);
|
||||
Assert.True(drawing.HasCustomColor);
|
||||
|
||||
properties.ApplyTo(drawing);
|
||||
|
||||
Assert.False(drawing.HasCustomColor);
|
||||
Assert.Equal(Drawing.GetPartColor(1).ToArgb(), drawing.Color.ToArgb());
|
||||
Assert.Equal(DrawingProperties.AutomaticColorText, new DrawingProperties(drawing, nest).ColorModeText);
|
||||
}
|
||||
finally
|
||||
{
|
||||
state.Restore();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void APickedColorCancelsAPendingAutomatic()
|
||||
{
|
||||
var drawing = Square();
|
||||
drawing.Color = Color.Red;
|
||||
drawing.HasCustomColor = true;
|
||||
var properties = new DrawingProperties(drawing);
|
||||
properties.UseAutomaticColor();
|
||||
|
||||
properties.PickColor(Color.Blue);
|
||||
properties.ApplyTo(drawing);
|
||||
|
||||
Assert.True(drawing.HasCustomColor);
|
||||
Assert.Equal(Color.Blue.ToArgb(), drawing.Color.ToArgb());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Desktop;
|
||||
|
||||
/// <summary>The bar down a Drawings tab row's left edge, for a drawing with a required quantity.</summary>
|
||||
public enum DrawingQuantityBar
|
||||
{
|
||||
/// <summary>No quantity is required (or quantities are hidden): no bar.</summary>
|
||||
None,
|
||||
|
||||
/// <summary>All of the required quantity is nested: a green bar.</summary>
|
||||
Complete,
|
||||
|
||||
/// <summary>Some of the required quantity is still to nest: an orange bar.</summary>
|
||||
Short,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One row of the Drawings tab list, as the WinForms list draws it beside the drawing's
|
||||
/// thumbnail: its name, "[on this plate] nested of required nested", its size and its area in
|
||||
/// square units. A drawing whose nested parts left none remaining is highlighted, and a drawing
|
||||
/// with a required quantity carries a bar saying whether all of it is nested.
|
||||
/// </summary>
|
||||
public sealed record DrawingListRow(
|
||||
Drawing Drawing,
|
||||
string Name,
|
||||
string Quantity,
|
||||
string Size,
|
||||
string Area,
|
||||
bool IsHighlighted,
|
||||
DrawingQuantityBar Bar)
|
||||
{
|
||||
/// <summary>The note an emptied list shows when Hide Fully Nested Drawings hid every drawing.</summary>
|
||||
public const string AllHiddenText = "All drawings are fully nested and hidden.\n\nClick here to show them.";
|
||||
|
||||
/// <summary>
|
||||
/// The row of <paramref name="drawing"/>. Its count on <paramref name="plate"/> (the plate
|
||||
/// shown beside the list; null omits it) prefixes the quantity when there is one. With
|
||||
/// <paramref name="hideQuantity"/> the row has no quantity, highlight or bar.
|
||||
/// </summary>
|
||||
public static DrawingListRow For(Drawing drawing, Plate plate, Units units, bool hideQuantity = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
var quantity = drawing.Quantity;
|
||||
string quantityText = null;
|
||||
var bar = DrawingQuantityBar.None;
|
||||
if (!hideQuantity)
|
||||
{
|
||||
quantityText = $"{quantity.Nested} of {quantity.Required} nested";
|
||||
var onPlate = plate?.Parts.Count(part => part.BaseDrawing == drawing) ?? 0;
|
||||
if (onPlate > 0)
|
||||
quantityText = $"[{onPlate}] {quantityText}";
|
||||
if (quantity.Required > 0)
|
||||
bar = quantity.Nested >= quantity.Required ? DrawingQuantityBar.Complete : DrawingQuantityBar.Short;
|
||||
}
|
||||
|
||||
return new DrawingListRow(
|
||||
drawing,
|
||||
drawing.Name,
|
||||
quantityText,
|
||||
drawing.Program.BoundingBox().Size.ToString(4),
|
||||
$"{System.Math.Round(drawing.Area, 4)} sq/{UnitsHelper.GetShortString(units)}",
|
||||
!hideQuantity && quantity.Nested > 0 && quantity.Remaining == 0,
|
||||
bar);
|
||||
}
|
||||
|
||||
/// <summary>The rows of <paramref name="drawings"/>, in their order.</summary>
|
||||
public static IReadOnlyList<DrawingListRow> For(IEnumerable<Drawing> drawings, Plate plate, Units units)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawings);
|
||||
return drawings.Select(drawing => For(drawing, plate, units)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The note the list shows in place of rows: <see cref="AllHiddenText"/> when Hide Fully
|
||||
/// Nested Drawings (<paramref name="hideFullyNested"/>) emptied a list of a nest that has
|
||||
/// drawings, so they don't look lost; otherwise null. The filter stays the user's choice.
|
||||
/// </summary>
|
||||
public static string EmptyText(bool hideFullyNested, int drawingCount) =>
|
||||
hideFullyNested && drawingCount > 0 ? AllHiddenText : null;
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenNest.Desktop;
|
||||
|
||||
/// <summary>
|
||||
/// The Drawing Properties dialog's values and rules, toolkit-neutral: read from a drawing, edited
|
||||
/// by the dialog, then saved back with <see cref="ApplyTo"/>. Its color is either one picked here
|
||||
/// (custom) or the color scheme's palette color for the drawing's slot in the nest (Automatic);
|
||||
/// only a color actually picked becomes custom, so reopening and saving keeps the palette color.
|
||||
/// </summary>
|
||||
public sealed class DrawingProperties
|
||||
{
|
||||
/// <summary>The quantity and priority boxes' limits.</summary>
|
||||
public const int MaxQuantity = 999999;
|
||||
|
||||
public const int MinPriority = -999999;
|
||||
|
||||
public const int MaxPriority = 999999;
|
||||
|
||||
public const string CustomColorText = "Custom color";
|
||||
|
||||
public const string AutomaticColorText = "Automatic color from the color scheme";
|
||||
|
||||
// The palette color Automatic asked for, while the color is still that one.
|
||||
private Color? automaticColor;
|
||||
|
||||
/// <summary>Reads <paramref name="drawing"/>'s values; <paramref name="nest"/> holds it (or null).</summary>
|
||||
public DrawingProperties(Drawing drawing, Nest nest = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
Drawing = drawing;
|
||||
Nest = nest;
|
||||
Color = drawing.Color;
|
||||
Name = drawing.Name;
|
||||
Customer = drawing.Customer;
|
||||
Quantity = drawing.Quantity.Required;
|
||||
Priority = drawing.Priority;
|
||||
CutFirst = drawing.CutFirst;
|
||||
AllowCommonLine = drawing.AllowCommonLine;
|
||||
Formed = drawing.IsFormed;
|
||||
}
|
||||
|
||||
public Drawing Drawing { get; }
|
||||
|
||||
public Nest Nest { get; }
|
||||
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Customer { get; set; }
|
||||
|
||||
public int Quantity { get; set; }
|
||||
|
||||
public int Priority { get; set; }
|
||||
|
||||
public bool CutFirst { get; set; }
|
||||
|
||||
public bool AllowCommonLine { get; set; }
|
||||
|
||||
/// <summary>Formed (bent or rolled) after cutting.</summary>
|
||||
public bool Formed { get; set; }
|
||||
|
||||
/// <summary>The color shown; set it directly to keep a pending Automatic when it is unchanged.</summary>
|
||||
public Color Color { get; set; }
|
||||
|
||||
/// <summary>The dialog's title.</summary>
|
||||
public string Title => string.IsNullOrEmpty(Drawing.Name)
|
||||
? "Drawing Properties"
|
||||
: $"Drawing Properties - {Drawing.Name}";
|
||||
|
||||
/// <summary>A color picked in the dialog: it cancels a pending Automatic.</summary>
|
||||
public void PickColor(Color color)
|
||||
{
|
||||
automaticColor = null;
|
||||
Color = color;
|
||||
}
|
||||
|
||||
/// <summary>Automatic: the palette color for the drawing's slot, saved as not custom.</summary>
|
||||
public void UseAutomaticColor()
|
||||
{
|
||||
automaticColor = AutomaticColor();
|
||||
Color = automaticColor.Value;
|
||||
}
|
||||
|
||||
public bool IsAutomaticColorRequested => automaticColor?.ToArgb() == Color.ToArgb();
|
||||
|
||||
/// <summary>Whether saving makes (or keeps) the color custom.</summary>
|
||||
public bool IsCustomColor =>
|
||||
!IsAutomaticColorRequested
|
||||
&& (Drawing.HasCustomColor || Color.ToArgb() != Drawing.Color.ToArgb());
|
||||
|
||||
/// <summary>The note beside the color swatch.</summary>
|
||||
public string ColorModeText => IsCustomColor ? CustomColorText : AutomaticColorText;
|
||||
|
||||
/// <summary>Whether Automatic can be chosen: only for a custom color.</summary>
|
||||
public bool CanUseAutomaticColor => IsCustomColor;
|
||||
|
||||
/// <summary>The palette color for the drawing's slot among the nest's non-cutoff drawings.</summary>
|
||||
public Color AutomaticColor()
|
||||
{
|
||||
var index = 0;
|
||||
if (Nest != null)
|
||||
{
|
||||
foreach (var d in Nest.Drawings)
|
||||
{
|
||||
if (ReferenceEquals(d, Drawing))
|
||||
break;
|
||||
if (!d.IsCutOff)
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return Drawing.GetPartColor(index);
|
||||
}
|
||||
|
||||
/// <summary>"n nested, m remaining", the remaining counted against the quantity being edited.</summary>
|
||||
public string NestedText
|
||||
{
|
||||
get
|
||||
{
|
||||
var nested = Drawing.Quantity.Nested;
|
||||
var remaining = System.Math.Max(Quantity - nested, 0);
|
||||
return $"{nested} nested, {remaining} remaining";
|
||||
}
|
||||
}
|
||||
|
||||
public string SizeText
|
||||
{
|
||||
get
|
||||
{
|
||||
var box = Drawing.Program.BoundingBox();
|
||||
return $"{box.Length:0.###} x {box.Width:0.###}";
|
||||
}
|
||||
}
|
||||
|
||||
public string AreaText => $"{Drawing.Area:0.###}";
|
||||
|
||||
/// <summary>The full path of the file the drawing came from, or null.</summary>
|
||||
public string SourcePath => string.IsNullOrEmpty(Drawing.Source?.Path) ? null : Drawing.Source.Path;
|
||||
|
||||
/// <summary>The source file's name, or "-" when there is none.</summary>
|
||||
public string SourceText => SourcePath == null ? "-" : System.IO.Path.GetFileName(SourcePath);
|
||||
|
||||
/// <summary>Saves the values to <paramref name="drawing"/>; Automatic recolors the nest's drawings.</summary>
|
||||
public void ApplyTo(Drawing drawing)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(drawing);
|
||||
drawing.Name = Name;
|
||||
drawing.Customer = Customer;
|
||||
drawing.Quantity.Required = Quantity;
|
||||
drawing.Priority = Priority;
|
||||
drawing.CutFirst = CutFirst;
|
||||
drawing.AllowCommonLine = AllowCommonLine;
|
||||
drawing.SetFormed(Formed);
|
||||
if (IsAutomaticColorRequested)
|
||||
{
|
||||
if (!drawing.HasCustomColor)
|
||||
return;
|
||||
|
||||
drawing.HasCustomColor = false;
|
||||
if (Nest != null && Nest.Drawings.Contains(drawing))
|
||||
ColorSchemeRegistry.RecolorDrawings(Nest);
|
||||
else
|
||||
drawing.Color = AutomaticColor();
|
||||
}
|
||||
else if (IsCustomColor)
|
||||
{
|
||||
drawing.Color = Color;
|
||||
drawing.HasCustomColor = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using OpenNest.Desktop;
|
||||
using Size = System.Drawing.Size;
|
||||
|
||||
namespace OpenNest.Controls
|
||||
@@ -204,12 +205,8 @@ namespace OpenNest.Controls
|
||||
return;
|
||||
|
||||
var isSelected = (e.State & DrawItemState.Selected) != 0;
|
||||
Brush bgBrush;
|
||||
|
||||
if (!HideQuantity && dwg.Quantity.Nested > 0 && dwg.Quantity.Remaining == 0)
|
||||
bgBrush = SystemBrushes.Info;
|
||||
else
|
||||
bgBrush = Brushes.White;
|
||||
var row = DrawingListRow.For(dwg, Plate, Units, HideQuantity);
|
||||
var bgBrush = row.IsHighlighted ? SystemBrushes.Info : Brushes.White;
|
||||
|
||||
e.Graphics.FillRectangle(bgBrush, e.Bounds);
|
||||
|
||||
@@ -225,11 +222,11 @@ namespace OpenNest.Controls
|
||||
e.Graphics.DrawRectangle(borderPen, borderRect);
|
||||
}
|
||||
|
||||
if (!HideQuantity && dwg.Quantity.Required > 0)
|
||||
if (row.Bar != DrawingQuantityBar.None)
|
||||
{
|
||||
var barWidth = 4;
|
||||
var barColor =
|
||||
dwg.Quantity.Nested >= dwg.Quantity.Required
|
||||
row.Bar == DrawingQuantityBar.Complete
|
||||
? Color.FromArgb(76, 175, 80)
|
||||
: Color.FromArgb(255, 152, 0);
|
||||
using var barBrush = new SolidBrush(barColor);
|
||||
@@ -259,43 +256,23 @@ namespace OpenNest.Controls
|
||||
var textBrush = Brushes.Black;
|
||||
var detailBrush = Brushes.Gray;
|
||||
|
||||
e.Graphics.DrawString(dwg.Name, nameFont, textBrush, pt);
|
||||
e.Graphics.DrawString(row.Name, nameFont, textBrush, pt);
|
||||
|
||||
var bounds = dwg.Program.BoundingBox();
|
||||
var text2 = bounds.Size.ToString(4);
|
||||
var text3 = string.Format(
|
||||
"{0} sq/{1}",
|
||||
System.Math.Round(dwg.Area, 4),
|
||||
UnitsHelper.GetShortString(Units)
|
||||
);
|
||||
|
||||
if (HideQuantity)
|
||||
if (row.Quantity != null)
|
||||
{
|
||||
pt.Y += 22;
|
||||
e.Graphics.DrawString(text2, Font, detailBrush, pt);
|
||||
e.Graphics.DrawString(row.Quantity, Font, detailBrush, pt);
|
||||
pt.Y += 18;
|
||||
e.Graphics.DrawString(text3, Font, detailBrush, pt);
|
||||
}
|
||||
else
|
||||
{
|
||||
var text1 = string.Format(
|
||||
"{0} of {1} nested",
|
||||
dwg.Quantity.Nested,
|
||||
dwg.Quantity.Required
|
||||
);
|
||||
var onPlate = Plate?.Parts.Count(part => part.BaseDrawing == dwg) ?? 0;
|
||||
|
||||
if (onPlate > 0)
|
||||
text1 = $"[{onPlate}] {text1}";
|
||||
|
||||
pt.Y += 22;
|
||||
e.Graphics.DrawString(text1, Font, detailBrush, pt);
|
||||
pt.Y += 18;
|
||||
e.Graphics.DrawString(text2, Font, detailBrush, pt);
|
||||
pt.Y += 18;
|
||||
e.Graphics.DrawString(text3, Font, detailBrush, pt);
|
||||
}
|
||||
|
||||
e.Graphics.DrawString(row.Size, Font, detailBrush, pt);
|
||||
pt.Y += 18;
|
||||
e.Graphics.DrawString(row.Area, Font, detailBrush, pt);
|
||||
|
||||
using var separatorPen = new Pen(Color.LightGray);
|
||||
var separatorY = e.Bounds.Bottom - 1;
|
||||
e.Graphics.DrawLine(separatorPen, e.Bounds.X, separatorY, e.Bounds.Right, separatorY);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using OpenNest.Desktop;
|
||||
|
||||
namespace OpenNest.Forms
|
||||
{
|
||||
@@ -9,8 +10,7 @@ namespace OpenNest.Forms
|
||||
private const int PreviewInset = 24;
|
||||
|
||||
private Drawing drawing;
|
||||
private Nest nest;
|
||||
private Color? automaticColor;
|
||||
private DrawingProperties properties;
|
||||
|
||||
public EditDrawingForm()
|
||||
{
|
||||
@@ -89,9 +89,9 @@ namespace OpenNest.Forms
|
||||
private void UpdateColor()
|
||||
{
|
||||
colorSwatch.BackColor = colorDialog1.Color;
|
||||
var customColor = IsCustomColor;
|
||||
colorModeLabel.Text = customColor ? "Custom color" : "Automatic color from the color scheme";
|
||||
automaticColorButton.Enabled = customColor;
|
||||
properties.Color = colorDialog1.Color;
|
||||
colorModeLabel.Text = properties.ColorModeText;
|
||||
automaticColorButton.Enabled = properties.CanUseAutomaticColor;
|
||||
UpdateImage();
|
||||
}
|
||||
|
||||
@@ -100,74 +100,32 @@ namespace OpenNest.Forms
|
||||
if (drawing == null)
|
||||
return;
|
||||
|
||||
var nested = drawing.Quantity.Nested;
|
||||
var remaining = System.Math.Max(Quantity - nested, 0);
|
||||
nestedLabel.Text = $"{nested} nested, {remaining} remaining";
|
||||
properties.Quantity = Quantity;
|
||||
nestedLabel.Text = properties.NestedText;
|
||||
}
|
||||
|
||||
private void LoadDetails()
|
||||
{
|
||||
var box = drawing.Program.BoundingBox();
|
||||
sizeLabel.Text = $"{box.Length:0.###} x {box.Width:0.###}";
|
||||
areaLabel.Text = $"{drawing.Area:0.###}";
|
||||
|
||||
var path = drawing.Source?.Path;
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
sourceLabel.Text = "-";
|
||||
toolTip.SetToolTip(sourceLabel, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
sourceLabel.Text = System.IO.Path.GetFileName(path);
|
||||
toolTip.SetToolTip(sourceLabel, path);
|
||||
}
|
||||
sizeLabel.Text = properties.SizeText;
|
||||
areaLabel.Text = properties.AreaText;
|
||||
sourceLabel.Text = properties.SourceText;
|
||||
toolTip.SetToolTip(sourceLabel, properties.SourcePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The palette color for the drawing's slot among the nest's non-cutoff drawings.
|
||||
/// </summary>
|
||||
private Color AutomaticColor()
|
||||
{
|
||||
var index = 0;
|
||||
if (nest != null)
|
||||
{
|
||||
foreach (var d in nest.Drawings)
|
||||
{
|
||||
if (ReferenceEquals(d, drawing))
|
||||
break;
|
||||
if (!d.IsCutOff)
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return Drawing.GetPartColor(index);
|
||||
}
|
||||
|
||||
private bool IsAutomaticColorRequested =>
|
||||
automaticColor?.ToArgb() == colorDialog1.Color.ToArgb();
|
||||
|
||||
// Only a color actually picked here becomes custom; reopening and saving keeps the palette color.
|
||||
private bool IsCustomColor =>
|
||||
!IsAutomaticColorRequested
|
||||
&& (drawing.HasCustomColor || colorDialog1.Color.ToArgb() != drawing.Color.ToArgb());
|
||||
|
||||
public void LoadDrawing(Drawing drawing, Nest nest = null)
|
||||
{
|
||||
this.drawing = drawing;
|
||||
this.nest = nest;
|
||||
automaticColor = null;
|
||||
properties = new DrawingProperties(drawing, nest);
|
||||
|
||||
colorDialog1.Color = drawing.Color;
|
||||
DrawingName = drawing.Name;
|
||||
Customer = drawing.Customer;
|
||||
Quantity = drawing.Quantity.Required;
|
||||
Priority = drawing.Priority;
|
||||
CutFirst = drawing.CutFirst;
|
||||
AllowCommonLine = drawing.AllowCommonLine;
|
||||
Formed = drawing.IsFormed;
|
||||
Text = string.IsNullOrEmpty(drawing.Name)
|
||||
? "Drawing Properties"
|
||||
: $"Drawing Properties - {drawing.Name}";
|
||||
colorDialog1.Color = properties.Color;
|
||||
DrawingName = properties.Name;
|
||||
Customer = properties.Customer;
|
||||
Quantity = properties.Quantity;
|
||||
Priority = properties.Priority;
|
||||
CutFirst = properties.CutFirst;
|
||||
AllowCommonLine = properties.AllowCommonLine;
|
||||
Formed = properties.Formed;
|
||||
Text = properties.Title;
|
||||
LoadDetails();
|
||||
UpdateNestedLabel();
|
||||
UpdateColor();
|
||||
@@ -175,29 +133,16 @@ namespace OpenNest.Forms
|
||||
|
||||
public void SaveDrawing(Drawing drawing)
|
||||
{
|
||||
drawing.Name = DrawingName;
|
||||
drawing.Customer = Customer;
|
||||
drawing.Quantity.Required = Quantity;
|
||||
drawing.Priority = Priority;
|
||||
drawing.CutFirst = CutFirst;
|
||||
drawing.AllowCommonLine = AllowCommonLine;
|
||||
drawing.SetFormed(Formed);
|
||||
if (IsAutomaticColorRequested)
|
||||
{
|
||||
if (!drawing.HasCustomColor)
|
||||
return;
|
||||
|
||||
drawing.HasCustomColor = false;
|
||||
if (nest != null && nest.Drawings.Contains(drawing))
|
||||
ColorSchemeRegistry.RecolorDrawings(nest);
|
||||
else
|
||||
drawing.Color = AutomaticColor();
|
||||
}
|
||||
else if (IsCustomColor)
|
||||
{
|
||||
drawing.Color = colorDialog1.Color;
|
||||
drawing.HasCustomColor = true;
|
||||
}
|
||||
properties.Name = DrawingName;
|
||||
properties.Customer = Customer;
|
||||
properties.Quantity = Quantity;
|
||||
properties.Priority = Priority;
|
||||
properties.CutFirst = CutFirst;
|
||||
properties.AllowCommonLine = AllowCommonLine;
|
||||
properties.Formed = Formed;
|
||||
// The dialog's color is the one shown; a pending Automatic holds while it is unchanged.
|
||||
properties.Color = colorDialog1.Color;
|
||||
properties.ApplyTo(drawing);
|
||||
}
|
||||
|
||||
protected override void OnShown(EventArgs e)
|
||||
@@ -215,14 +160,14 @@ namespace OpenNest.Forms
|
||||
return;
|
||||
|
||||
// A picked color cancels a pending Automatic.
|
||||
automaticColor = null;
|
||||
properties.PickColor(colorDialog1.Color);
|
||||
UpdateColor();
|
||||
}
|
||||
|
||||
private void AutomaticColor_Click(object sender, EventArgs e)
|
||||
{
|
||||
automaticColor = AutomaticColor();
|
||||
colorDialog1.Color = automaticColor.Value;
|
||||
properties.UseAutomaticColor();
|
||||
colorDialog1.Color = properties.Color;
|
||||
UpdateColor();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -674,10 +674,7 @@ namespace OpenNest.Forms
|
||||
/// </summary>
|
||||
private void UpdateDrawingListEmptyText()
|
||||
{
|
||||
drawingListBox1.EmptyText =
|
||||
hideNestedButton.Checked && Nest.Drawings.Count > 0
|
||||
? "All drawings are fully nested and hidden.\n\nClick here to show them."
|
||||
: null;
|
||||
drawingListBox1.EmptyText = DrawingListRow.EmptyText(hideNestedButton.Checked, Nest.Drawings.Count);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
@@ -1753,8 +1750,8 @@ namespace OpenNest.Forms
|
||||
private void drawingListBox1_RemoveUnusedRequested(object sender, EventArgs e)
|
||||
{
|
||||
var result = MessageBox.Show(
|
||||
"Remove unused drawings?",
|
||||
"Clean Drawings",
|
||||
DrawingCommands.CleanUnusedPrompt,
|
||||
DrawingCommands.CleanUnusedTitle,
|
||||
MessageBoxButtons.YesNoCancel,
|
||||
MessageBoxIcon.Question,
|
||||
MessageBoxDefaultButton.Button1
|
||||
@@ -1767,10 +1764,9 @@ namespace OpenNest.Forms
|
||||
/// <summary>Removes the drawings with no part on any plate.</summary>
|
||||
public void CleanUnusedDrawings()
|
||||
{
|
||||
if (!Nest.Drawings.Any(d => d.Quantity.Nested == 0))
|
||||
if (!DrawingCommands.CleanUnusedDrawings(Nest, label => BeginChange(label)))
|
||||
return;
|
||||
|
||||
RecordChange("Clean Unused Drawings", () => Nest.Drawings.RemoveWhere(d => d.Quantity.Nested == 0));
|
||||
UpdateDrawingList();
|
||||
}
|
||||
|
||||
@@ -1890,7 +1886,7 @@ namespace OpenNest.Forms
|
||||
|
||||
if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
||||
{
|
||||
RecordChange("Drawing Properties", () => form.SaveDrawing(drawing));
|
||||
RecordChange(DrawingCommands.PropertiesLabel, () => form.SaveDrawing(drawing));
|
||||
|
||||
RefreshDrawingColor(drawing);
|
||||
|
||||
@@ -1910,8 +1906,8 @@ namespace OpenNest.Forms
|
||||
private void drawingListBox1_DeleteRequested(object sender, Drawing drawing)
|
||||
{
|
||||
var result = MessageBox.Show(
|
||||
$"Delete drawing '{drawing.Name}' and all its parts from every plate?",
|
||||
"Delete Drawing",
|
||||
DrawingCommands.DeletePrompt(drawing),
|
||||
DrawingCommands.DeleteTitle,
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Warning,
|
||||
MessageBoxDefaultButton.Button2
|
||||
@@ -1926,20 +1922,7 @@ namespace OpenNest.Forms
|
||||
/// <summary>Deletes <paramref name="drawing"/> and its parts from every plate.</summary>
|
||||
public void DeleteDrawing(Drawing drawing)
|
||||
{
|
||||
RecordChange("Delete Drawing", () =>
|
||||
{
|
||||
// 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);
|
||||
});
|
||||
DrawingCommands.DeleteDrawing(Nest, drawing, label => BeginChange(label));
|
||||
UpdateDrawingList();
|
||||
UpdatePlateList();
|
||||
PlateView.Invalidate();
|
||||
|
||||
Reference in new issue
Block a user