/// <summary> /// plot the virtual path in screen /// </summary> public async Task <PlotResult> PlotPath(double x, double y, PlotPath path) { x = 0; y = 0; var points = path.Points.Length <= 2 ? path.Points.ToArray() : (this.FastPreview ? PathSimplifier.SimplifyPathsPoints(path.Points, tolerance: PathSimplifier.ScreenFastTolerance).ToArray() : PathSimplifier.SimplifyPathsPoints(path.Points, tolerance: PathSimplifier.PlotTolerance).ToArray() ); var zoom = this.ZoomFactor; for (int i = 0; i < points.Length - 1; i++) { canvas.Children.Add( new Line() { Stroke = this.paintBrush, StrokeThickness = strokeThickness, X1 = (points[i].X + x) * zoom, Y1 = (points[i].Y + y) * zoom, X2 = (points[i + 1].X + x) * zoom, Y2 = (points[i + 1].Y + y) * zoom, }); } if (this.DelayMsPerPath > 0) { await Task.Delay(DelayMsPerPath); } return(new PlotResult { Success = true }); }
public async Task <PlotResult> PlotPath(double x, double y, PlotPath path) { var ready = await this.GetReady(); if (!ready.Success) { return(ready); } bool fast = true; // simplify the path? var points = fast && path.Points.Length > 2 ? PathSimplifier.SimplifyPathsPoints(path.Points, tolerance: PathSimplifier.PlotTolerance).ToArray() : path.Points; if (points.Length < 2) { return new PlotResult { Success = false, ErrorMessage = "path contains less than 2 points" } } ; for (int i = 0; i < points.Length; i++) { var plotX = x + points[i].X; var plotY = y - points[i].Y; if (i == 0) { if (actualX.Equals(plotX) && actualY.Equals(plotY)) { await this.plotterHardware.PenDown(); await this.plotterHardware.PaintSpeed(); } else { await this.plotterHardware.PenUp(); await this.plotterHardware.TravelSpeed(); } } if (!await this.plotterHardware.MoveTo(plotX, plotY)) { // timeout happened, try again! await Task.Delay(10000); // Wait for the plotter com port to set up var penWasDown = !this.plotterHardware.PenIsUp; await this.plotterHardware.PenUp(); await this.plotterHardware.AutoHome(); this.actualX = 0; this.actualY = 0; if (await this.plotterHardware.MoveTo(plotX, plotY)) { if (penWasDown) { await this.plotterHardware.PenDown(); } } else { // timeout happened again 😱 throw new Exception("unable to fix timeout problem!"); } } this.actualX = plotX; this.actualY = plotY; if (this.plotterHardware.PenIsUp) { await this.plotterHardware.PenDown(); } } if (!this.plotterHardware.PenIsUp) { await this.plotterHardware.PenUp(); } return(new PlotResult { Success = true }); }