public void GenerateGCODE() { //calcMovementSideToSide(); calcMovementUpDown(); AllLines.Clear(); GeneratedGCODE.Clear(); double xMinVal = BlackLines[0].X0; double xMaxVal = BlackLines[0].X1; double yMinVal = BlackLines[0].Y0; double yMaxVal = BlackLines[0].Y1; for (int i = 0; i < BlackLines.Count - 1; i++) { AllLines.Add(new TraceLine(BlackLines[i].X0, BlackLines[i].Y0, BlackLines[i].X1, BlackLines[i].Y1, true)); //stores the current line to be drawn AllLines.Add(new TraceLine(BlackLines[i].X1, BlackLines[i].Y1, BlackLines[i + 1].X0, BlackLines[i + 1].Y0, false)); //moves draw head to position for next line //finding the minimum and maximum values for both X and Y positions. used to draw the bounding box in GUI if (BlackLines[i].X0 < xMinVal || BlackLines[i].X1 < xMinVal) { xMinVal = Math.Min(BlackLines[i].X0, BlackLines[i].X1); } if (BlackLines[i].X0 > xMaxVal || BlackLines[i].X1 > xMaxVal) { xMaxVal = Math.Max(BlackLines[i].X0, BlackLines[i].X1); } if (BlackLines[i].Y0 < yMinVal || BlackLines[i].Y1 < yMinVal) { yMinVal = Math.Min(BlackLines[i].Y0, BlackLines[i].Y1); } if (BlackLines[i].Y0 > yMaxVal || BlackLines[i].Y1 > yMaxVal) { yMaxVal = Math.Max(BlackLines[i].Y0, BlackLines[i].Y1); } } BoundingCoordinates = new TraceLine(xMinVal, yMinVal, xMaxVal, yMaxVal); //saves X and Y positions to draw bounding box later GeneratedGCODE.Add(StartGCODE + "\n"); GeneratedGCODE.Add(string.Format("G1 X{0} Y{1}\n", AllLines[0].X0, AllLines[0].Y0)); //goes from home position to start of first line to draw foreach (TraceLine line in AllLines) { GeneratedGCODE.Add("G1 Z" + Convert.ToInt32(!line.Draw) + "\n"); GeneratedGCODE.Add(string.Format("G1 X{0} Y{1}\nL{2}", line.X1, line.Y1, AllLines.IndexOf(line))); //added L to save the line number, makes gui stuff easier in main window } GeneratedGCODE.Add(EndGCODE + "\n"); }
private async void btnBoundingBox_Click(object sender, RoutedEventArgs e) { //selection box to choose if pen is touching canvas //define lines from stored coordinates //draw box in preview canvas //draw box on canvas //go to home position TraceLine topLeftToRight = new TraceLine(myPlot.BoundingCoordinates.X0 * scaleToPreview, myPlot.BoundingCoordinates.Y0 * scaleToPreview, myPlot.BoundingCoordinates.X1 * scaleToPreview, myPlot.BoundingCoordinates.Y0 * scaleToPreview); TraceLine rightDown = new TraceLine(myPlot.BoundingCoordinates.X1 * scaleToPreview, myPlot.BoundingCoordinates.Y0 * scaleToPreview, myPlot.BoundingCoordinates.X1 * scaleToPreview, myPlot.BoundingCoordinates.Y1 * scaleToPreview); TraceLine downRightToLeft = new TraceLine(myPlot.BoundingCoordinates.X1 * scaleToPreview, myPlot.BoundingCoordinates.Y1 * scaleToPreview, myPlot.BoundingCoordinates.X0 * scaleToPreview, myPlot.BoundingCoordinates.Y1 * scaleToPreview); TraceLine leftUp = new TraceLine(myPlot.BoundingCoordinates.X0 * scaleToPreview, myPlot.BoundingCoordinates.Y1 * scaleToPreview, myPlot.BoundingCoordinates.X0 * scaleToPreview, myPlot.BoundingCoordinates.Y0 * scaleToPreview); Line myLine = new Line(); myLine.Stroke = System.Windows.Media.Brushes.Red; myLine.StrokeThickness = 2; myLine.X1 = topLeftToRight.X0; myLine.Y1 = topLeftToRight.Y0; myLine.X2 = topLeftToRight.X1; myLine.Y2 = topLeftToRight.Y1; canvasPreview.Children.Add(myLine); myLine = new Line(); myLine.Stroke = System.Windows.Media.Brushes.Red; myLine.StrokeThickness = 2; myLine.X1 = rightDown.X0; myLine.Y1 = rightDown.Y0; myLine.X2 = rightDown.X1; myLine.Y2 = rightDown.Y1; canvasPreview.Children.Add(myLine); myLine = new Line(); myLine.Stroke = System.Windows.Media.Brushes.Red; myLine.StrokeThickness = 2; myLine.X1 = downRightToLeft.X0; myLine.Y1 = downRightToLeft.Y0; myLine.X2 = downRightToLeft.X1; myLine.Y2 = downRightToLeft.Y1; canvasPreview.Children.Add(myLine); myLine = new Line(); myLine.Stroke = System.Windows.Media.Brushes.Red; myLine.StrokeThickness = 2; myLine.X1 = leftUp.X0; myLine.Y1 = leftUp.Y0; myLine.X2 = leftUp.X1; myLine.Y2 = leftUp.Y1; canvasPreview.Children.Add(myLine); await sendSerialStringAsync(string.Format("G1 X{0} Y{1}\n", myPlot.BoundingCoordinates.X0, myPlot.BoundingCoordinates.Y0)); //goes from home position await sendSerialStringAsync(string.Format("G1 X{0} Y{1} Z{2}\n", myPlot.BoundingCoordinates.X1, myPlot.BoundingCoordinates.Y0, !(bool)checkBoxDrawingBoundingBox.IsChecked)); //draws first line await sendSerialStringAsync(string.Format("G1 X{0} Y{1}\n", myPlot.BoundingCoordinates.X1, myPlot.BoundingCoordinates.Y1)); //draws second line await sendSerialStringAsync(string.Format("G1 X{0} Y{1}\n", myPlot.BoundingCoordinates.X0, myPlot.BoundingCoordinates.Y1)); //draws third line await sendSerialStringAsync(string.Format("G1 X{0} Y{1}\n", myPlot.BoundingCoordinates.X0, myPlot.BoundingCoordinates.Y0)); //draws fourth line if ((bool)checkBoxDrawingBoundingBox.IsChecked) { await sendSerialStringAsync("G1 Z1\n"); //pen touches the canvas } await sendSerialStringAsync("G28\n"); //goes to home position }