mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Plate > Calculate Cut Time and Nest > Calculate Cut Time ask for the outside feed, rapid, pierce time, hole feed, acceleration machine and etch choice (remembered in the settings both apps share, saved however the dialog closes, as WinForms), then show the timing as the WinForms Timing Information window. Plate > Simulate Cutting and its toolbar button open the Cut Simulator: play and pause at 1x to 250x, restart and end, part and cutout jumps, move stepping with the arrow keys, scrubbing, and the time, part, drawing and motion at the playhead, retimed as the feeds change and following the shown plate. The plate view draws the cut trail, travel, the torch and pierces, and mutes parts and cut-off segments the torch has not reached. While it is open the nest is view-only, as WinForms: only the plate view and its header stay live, Escape closes it and Ctrl+Plus and Ctrl+Minus still zoom. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
8.7 KiB
C#
205 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Layout;
|
|
using OpenNest.Data;
|
|
using OpenNest.Desktop;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// The cut-time values Calculate Cut Time asks for, as the WinForms Cut Parameters form:
|
|
/// outside feed, rapid, pierce time, hole feed, the machine whose acceleration times the
|
|
/// moves (or full speed) and whether etch-only contours are skipped. It opens with the values
|
|
/// last used here, in the WinForms app or in either simulator, and saves them however it
|
|
/// closes, as WinForms does.
|
|
/// </summary>
|
|
internal sealed class CutTimeDialog : Window
|
|
{
|
|
private readonly NumberField feed;
|
|
private readonly NumberField rapid;
|
|
private readonly NumberField pierce;
|
|
private readonly NumberField holeFeed;
|
|
private readonly ComboBox machineBox;
|
|
private readonly CheckBox skipEtch;
|
|
private readonly Units units;
|
|
private readonly bool saveSettings;
|
|
|
|
private CutTimeDialog(Units units, IReadOnlyList<MachineConfig> machines, bool saveSettings)
|
|
{
|
|
this.units = units;
|
|
this.saveSettings = saveSettings;
|
|
Title = "Cut Parameters";
|
|
SizeToContent = SizeToContent.WidthAndHeight;
|
|
MinWidth = 380;
|
|
CanResize = false;
|
|
ShowInTaskbar = false;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
|
var saved = CutTimeSettingsFile.Read();
|
|
var choices = CutTimeMachineChoice.For(machines);
|
|
var loaded = saved.ToCutParameters(units, SavedCutTimeSettings.ChooseMachine(machines, saved.Machine));
|
|
var rate = UnitsHelper.GetShortTimeUnitPair(units);
|
|
feed = new NumberField("Feed", loaded.Feedrate, CutTimeEntry.MinimumRate, CutTimeEntry.MaximumRate, 0, rate);
|
|
rapid = new NumberField("Rapid", loaded.RapidTravelRate, CutTimeEntry.MinimumRate, CutTimeEntry.MaximumRate, 0, rate);
|
|
pierce = new NumberField("Pierce", loaded.PierceTime.TotalSeconds, 0, CutTimeEntry.DialogMaximumPierce, 1, "seconds");
|
|
holeFeed = new NumberField("HoleFeed", loaded.HoleFeedrate ?? loaded.Feedrate, CutTimeEntry.MinimumRate,
|
|
CutTimeEntry.MaximumRate, 0, rate);
|
|
machineBox = new ComboBox
|
|
{
|
|
Name = "Machine",
|
|
ItemsSource = choices,
|
|
SelectedIndex = CutTimeMachineChoice.SavedIndex(choices, saved.Machine),
|
|
HorizontalAlignment = HorizontalAlignment.Stretch,
|
|
};
|
|
skipEtch = new CheckBox { Name = "SkipEtch", Content = "Skip etch-only contours", IsChecked = loaded.SkipScribe };
|
|
|
|
var grid = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto,Auto,Auto"),
|
|
ColumnSpacing = 10,
|
|
RowSpacing = 8,
|
|
};
|
|
AddRow(grid, 0, "Outside feed :", feed.View);
|
|
AddRow(grid, 1, "Rapid Feedrate :", rapid.View);
|
|
AddRow(grid, 2, "Pierce Time :", pierce.View);
|
|
AddRow(grid, 3, "Hole feed :", holeFeed.View);
|
|
AddRow(grid, 4, "Acceleration :", machineBox);
|
|
Grid.SetRow(skipEtch, 5);
|
|
Grid.SetColumn(skipEtch, 1);
|
|
grid.Children.Add(skipEtch);
|
|
|
|
var accept = new Button { Name = "Accept", Content = "Accept", IsDefault = true, MinWidth = 80 };
|
|
accept.Click += (_, _) => Close(GetCutParameters());
|
|
var cancel = new Button { Name = "Cancel", Content = "Cancel", IsCancel = true, MinWidth = 80 };
|
|
cancel.Click += (_, _) => Close(null);
|
|
var buttons = new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
Spacing = 8,
|
|
HorizontalAlignment = HorizontalAlignment.Right,
|
|
};
|
|
buttons.Children.Add(accept);
|
|
buttons.Children.Add(cancel);
|
|
|
|
var panel = new StackPanel { Margin = new Thickness(12), Spacing = 14 };
|
|
panel.Children.Add(grid);
|
|
panel.Children.Add(buttons);
|
|
Content = panel;
|
|
|
|
AddHandler(KeyDownEvent, (_, e) =>
|
|
{
|
|
if (e.Key != Key.Escape)
|
|
return;
|
|
e.Handled = true;
|
|
Close(null);
|
|
}, RoutingStrategies.Tunnel);
|
|
Opened += (_, _) => feed.Box.Focus();
|
|
// As WinForms: the values are remembered whether the dialog was accepted or not.
|
|
Closing += (_, _) =>
|
|
{
|
|
if (this.saveSettings)
|
|
CutTimeSettingsFile.Save(GetCutParameters(), Machine);
|
|
};
|
|
}
|
|
|
|
private MachineConfig Machine => (machineBox.SelectedItem as CutTimeMachineChoice)?.Machine;
|
|
|
|
private CutParameters GetCutParameters() =>
|
|
CutTimeEntry.Parameters(feed.Value, holeFeed.Value, rapid.Value, pierce.Value,
|
|
skipEtch.IsChecked == true, units, Machine);
|
|
|
|
private static void AddRow(Grid grid, int row, string label, Control field)
|
|
{
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The parameters entered, in <paramref name="units"/>, or null when the dialog was
|
|
/// cancelled. <paramref name="saveSettings"/> false leaves the remembered values alone.
|
|
/// </summary>
|
|
public static Task<CutParameters> ShowAsync(
|
|
Window owner, Units units, IReadOnlyList<MachineConfig> machines, bool saveSettings = true) =>
|
|
new CutTimeDialog(units, machines, saveSettings).ShowDialog<CutParameters>(owner);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A number box with its unit beside it, holding the last value typed when emptied and
|
|
/// keeping values within its range, as the WinForms boxes do.
|
|
/// </summary>
|
|
internal sealed class NumberField
|
|
{
|
|
private readonly decimal minimum;
|
|
private readonly decimal maximum;
|
|
private decimal last;
|
|
|
|
public NumberField(string name, double value, decimal minimum, decimal maximum, int decimals, string unit,
|
|
double width = 120)
|
|
{
|
|
this.minimum = minimum;
|
|
this.maximum = maximum;
|
|
last = CutTimeEntry.Clamp(value, minimum, maximum);
|
|
Box = new NumericUpDown
|
|
{
|
|
Name = name,
|
|
Minimum = minimum,
|
|
Maximum = maximum,
|
|
Increment = decimals == 0 ? 10 : 0.1m,
|
|
ClipValueToMinMax = true,
|
|
FormatString = decimals == 0 ? "0" : "0.0",
|
|
Value = last,
|
|
Width = width,
|
|
};
|
|
Box.ValueChanged += (_, e) =>
|
|
{
|
|
if (e.NewValue is not { } v)
|
|
return;
|
|
last = v;
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
};
|
|
var panel = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 6 };
|
|
panel.Children.Add(Box);
|
|
panel.Children.Add(new TextBlock { Text = unit, VerticalAlignment = VerticalAlignment.Center });
|
|
View = panel;
|
|
}
|
|
|
|
public NumericUpDown Box { get; }
|
|
|
|
/// <summary>The box with its unit.</summary>
|
|
public Control View { get; }
|
|
|
|
public decimal Value => System.Math.Clamp(last, minimum, maximum);
|
|
|
|
/// <summary>Raised when a new value is entered.</summary>
|
|
public event EventHandler Changed;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The cut-time values last used, in the user settings both apps share (WinForms names and
|
|
/// text, see <see cref="SavedCutTimeSettings"/>).
|
|
/// </summary>
|
|
internal static class CutTimeSettingsFile
|
|
{
|
|
public static SavedCutTimeSettings Read() =>
|
|
SavedCutTimeSettings.Read(UserSettingsFile.Read(UserSettingsFile.DefaultPath));
|
|
|
|
/// <summary>Remembers the feeds, pierce, etch choice and machine (null for full speed).</summary>
|
|
public static void Save(CutParameters parameters, MachineConfig machine) =>
|
|
UserSettingsFile.Update(UserSettingsFile.DefaultPath, settings =>
|
|
(SavedCutTimeSettings.Read(settings).With(parameters) with
|
|
{
|
|
Machine = SavedCutTimeSettings.MachineChoice(machine),
|
|
}).WriteTo(settings));
|
|
}
|
|
}
|