fix cut file calculation

This commit is contained in:
kwan.nguyen 2024-10-30 18:16:08 +07:00
parent f5d9cce2ea
commit 5e1344d9d5
4 changed files with 12 additions and 11 deletions

View File

@ -27,7 +27,6 @@ namespace PCUT.Extensions
public static IEnumerable<(string Path, TimeSpan EstimateTime)> ToHPGL( public static IEnumerable<(string Path, TimeSpan EstimateTime)> ToHPGL(
this XDocument svg, this XDocument svg,
(double Width, double Height) size, (double Width, double Height) size,
(double Width, double Height) sheetSize,
int speed, // mm/s int speed, // mm/s
int pen = 0) int pen = 0)
{ {
@ -36,7 +35,7 @@ namespace PCUT.Extensions
var widthUnit = (int)Math.Ceiling(size.Width / UnitLength); var widthUnit = (int)Math.Ceiling(size.Width / UnitLength);
var heightUnit = (int)Math.Ceiling(size.Height / UnitLength); var heightUnit = (int)Math.Ceiling(size.Height / UnitLength);
SvgTransform transform = new TranslateTransform(0, (int)Math.Ceiling(sheetSize.Height / UnitLength)); SvgTransform transform = new TranslateTransform(0, heightUnit);
var flipTransform = new ScaleTransform(1, -1); var flipTransform = new ScaleTransform(1, -1);
flipTransform.SetNext(transform); flipTransform.SetNext(transform);
@ -64,7 +63,7 @@ namespace PCUT.Extensions
{ {
var lengthX = Math.Abs(maxX - moveCommand.X); var lengthX = Math.Abs(maxX - moveCommand.X);
builder.AddHpglMove(moveCommand.X, moveCommand.Y, ref maxX); builder.AddHpglMove(moveCommand.X, moveCommand.Y, ref maxX);
length += (lengthX + (sheetSize.Height / UnitLength)) * 0.75; length += (lengthX + (heightUnit / UnitLength)) * 0.75;
break; break;
} }
case LineCommand lineCommand: case LineCommand lineCommand:

View File

@ -49,23 +49,23 @@ namespace PCUT.Pages
StartButton.IsEnabled = !(portName is null); StartButton.IsEnabled = !(portName is null);
ViewModels = DataContext as CutDialogViewModel; ViewModels = DataContext as CutDialogViewModel;
ViewModels.Width = Math.Ceiling(_designCenterViewModel.UseOrigin ? _designCenterViewModel.Width : _svgData.NestedWidth.Value);
ViewModels.Height = Math.Ceiling(_designCenterViewModel.UseOrigin ? _designCenterViewModel.Height : _svgData.NestedHeight.Value);
} }
private Task _printTask; private Task _printTask;
private void ContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args) private void ContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{ {
var data = _designCenterViewModel.UseOrigin ? _svgData.Data : _svgData.NestedData; var data = _designCenterViewModel.UseOrigin ? _svgData.Data : _svgData.NestedData;
PreviewWidthDisplay.Text = Math.Ceiling(_designCenterViewModel.UseOrigin ? _svgData.OriginWidth.Value : _svgData.NestedWidth.Value).ToString(); PreviewWidthDisplay.Text = ViewModels.Width.ToString();
PreviewHeightDisplay.Text = Math.Ceiling(_designCenterViewModel.UseOrigin ? _svgData.OriginHeight.Value : _svgData.NestedHeight.Value).ToString(); PreviewHeightDisplay.Text = ViewModels.Height.ToString();
DisplayModel.SetSource(data); DisplayModel.SetSource(data);
} }
private void MainScroll_Loading(FrameworkElement sender, object args) private void MainScroll_Loading(FrameworkElement sender, object args)
{ {
var borderRatio = MainColumnDefinition.ActualWidth / PreviewRowDefinition.ActualHeight; var borderRatio = MainColumnDefinition.ActualWidth / PreviewRowDefinition.ActualHeight;
var width = Math.Ceiling(_designCenterViewModel.UseOrigin ? _svgData.OriginWidth.Value : _svgData.NestedWidth.Value); var ratio = ViewModels.Width / ViewModels.Height;
var height = Math.Ceiling(_designCenterViewModel.UseOrigin ? _svgData.OriginHeight.Value : _svgData.NestedHeight.Value);
var ratio = width / height;
if (ratio < borderRatio) if (ratio < borderRatio)
((ComponentStyle)Resources["previewStyle"]).SetSize(PreviewRowDefinition, ratio); ((ComponentStyle)Resources["previewStyle"]).SetSize(PreviewRowDefinition, ratio);
else else
@ -98,8 +98,7 @@ namespace PCUT.Pages
await Task.Factory.StartNew(() => await Task.Factory.StartNew(() =>
{ {
var data = nested.ToHPGL( var data = nested.ToHPGL(
(_svgData.OriginWidth.Value, _svgData.OriginHeight.Value), (ViewModels.Width, ViewModels.Height),
(_designCenterViewModel.Width, _designCenterViewModel.Height),
_portSettings.Speed); _portSettings.Speed);
serialPort.Open(); serialPort.Open();
Task waitTask = Task.CompletedTask; Task waitTask = Task.CompletedTask;

View File

@ -401,7 +401,7 @@ namespace PCUT.Pages
var width = _svgData.SheetInfo.Width; var width = _svgData.SheetInfo.Width;
var height = _svgData.SheetInfo.Height; var height = _svgData.SheetInfo.Height;
var viewBox = _svgData.OriginViewBox.GetViewBoxData(); var viewBox = _svgData.OriginViewBox.GetViewBoxData();
nestedDoc.Root.SetAttributeValue("viewBox", $"{viewBox.MinX - 100} {viewBox.MinY - 100} {nestedWidth + 100} {nestedHeight + 100}"); nestedDoc.Root.SetAttributeValue("viewBox", $"0 0 {nestedWidth} {nestedHeight}");
nestedDoc.Root.Add(XElement.Parse($"<rect id=\"PCUT_BOUNDING\" x=\"0\" y=\"0\" width=\"{_designCenterModel.Width}\" height=\"{_designCenterModel.Height}\" fill=\"none\" stroke=\"blue\" stroke-dasharray=\"{_designCenterModel.Width / 200}\" stroke-width=\"{_designCenterModel.Height / 2000}\" />")); nestedDoc.Root.Add(XElement.Parse($"<rect id=\"PCUT_BOUNDING\" x=\"0\" y=\"0\" width=\"{_designCenterModel.Width}\" height=\"{_designCenterModel.Height}\" fill=\"none\" stroke=\"blue\" stroke-dasharray=\"{_designCenterModel.Width / 200}\" stroke-width=\"{_designCenterModel.Height / 2000}\" />"));
nestedDoc.Root.Add(XElement.Parse($"<rect id=\"PCUT_BOUNDING_NESTED\" x=\"0\" y=\"0\" width=\"{nestedWidth}\" height=\"{nestedHeight}\" fill=\"none\" stroke=\"blue\" stroke-dasharray=\"{width / 200}\" stroke-width=\"{width / 2000}\" />")); nestedDoc.Root.Add(XElement.Parse($"<rect id=\"PCUT_BOUNDING_NESTED\" x=\"0\" y=\"0\" width=\"{nestedWidth}\" height=\"{nestedHeight}\" fill=\"none\" stroke=\"blue\" stroke-dasharray=\"{width / 200}\" stroke-width=\"{width / 2000}\" />"));
return (nestedDoc, nestedWidth, nestedHeight); return (nestedDoc, nestedWidth, nestedHeight);

View File

@ -12,6 +12,9 @@ namespace PCUT.ViewModels
{ {
internal class CutDialogViewModel : NotificationBase internal class CutDialogViewModel : NotificationBase
{ {
public double Width { get; set; }
public double Height { get; set; }
private string _printCommand; private string _printCommand;
public string PrintCommand public string PrintCommand
{ {