private void mnuSaveImageToGCode_Click(object sender, EventArgs e) { OpenFileDialog opn = new OpenFileDialog(); opn.Title = "Select an image..."; opn.Filter = "Bitmap (*.bmp)|*.bmp"; DialogResult dr = opn.ShowDialog(); if (dr != DialogResult.None && dr != DialogResult.Cancel && System.IO.File.Exists(opn.FileName)) { Bitmap bmp = new Bitmap(opn.FileName); AutoEdge ae = new AutoEdge(bmp); trvCharacters.SelectedNode = trvCharacters.Nodes[0]; picCharacter.Image = ae.Image; SaveFileDialog sv = new SaveFileDialog(); sv.CheckPathExists = true; sv.OverwritePrompt = true; sv.Filter = "EIA (*.eia)|.eia|G-Code (*.gcode)|*.gcode"; dr = sv.ShowDialog(); if (dr != DialogResult.None && dr != DialogResult.Cancel) { var strOut = new StringBuilder(); strOut.AppendLine("O" + Properties.Settings.Default.GCodeProgram); strOut.AppendLine("(MACRO: STENCIL " + opn.FileName.Remove(0, opn.FileName.LastIndexOf("\\") + 1) + ")"); strOut.AppendLine("(DATETIME: " + DateTime.Now.ToString("yyyy-MM-dd hh-mm tt") + ")"); strOut.AppendLine(Properties.Settings.Default.GCodeInit + " (INITIALIZE)"); strOut.AppendLine(ImageToGCode(ae)); strOut.AppendLine("M99"); System.IO.File.WriteAllText(sv.FileName, strOut.ToString()); statStatus.Text = "Saved!"; } } }
private string ImageToGCode(AutoEdge ae) { double imgWidth = ae.Image.Width; double imgHeight = ae.Image.Height; double ratHeight = Convert.ToDouble(Properties.Settings.Default.GCodeHeight); double ratWidth = ((imgWidth * ratHeight) / imgHeight); var strOut = new StringBuilder(); strOut.AppendLine("(IMAGE HEIGHT: " + ratHeight.ToString("0.0000") + " IMAGE WIDTH: " + ratWidth.ToString("0.0000") + ")"); strOut.AppendLine("G91 (BEGIN INCREMENTAL PATH)"); int idx = 0; foreach (AutoEdge.Crawler path in ae.EdgeCrawler) { Point[] nomPts = path.GetPointPath(); Point[] paths = path.GetPointPath_Incremental(ae.Image.Size); strOut.AppendLine("(PATH " + (idx + 1).ToString() + ")"); for (int j = 0; j < paths.Count(); j++) { double x, y; x = (paths[j].X / imgWidth) * ratWidth; y = (paths[j].Y / imgHeight) * ratHeight; if (j == 0) { strOut.AppendLine("G00 X" + x.ToString() + " Y" + y.ToString() + " (GO TO FIRST POINT)"); strOut.AppendLine("G01 Z-#26 (INCREMENT DOWN TO CUT)"); } else { strOut.AppendLine("X" + x.ToString() + " Y" + y.ToString()); } } strOut.AppendLine("G00 Z#26 (INCREMENT OUT)"); strOut.AppendLine("X" + (((0 - nomPts[nomPts.Count() - 1].X) / imgWidth) * ratWidth).ToString() + " Y-" + (((imgHeight - nomPts[nomPts.Count() - 1].Y) / imgHeight) * ratHeight).ToString() + "(MOVE TO START)"); idx += 1; } strOut.AppendLine("X" + (ratWidth).ToString() + "(MOVE TO NEXT CORNER)"); strOut.AppendLine("G90 (BACK TO ABSOLUTE FOR SAFETY)"); return(strOut.ToString()); }
public Bitmap Draw(bool includePoints = false) { if (this.Image != null) { this.Image.Dispose(); } SizeF sz = GetImageSize(); this.Image = new Bitmap((int)sz.Width, (int)sz.Height); using (Graphics g = Graphics.FromImage(this.Image)) { g.Clear(Color.White); g.DrawString(this.Character, this.Font, Brushes.Black, 0, 0); } if (includePoints) { this.EdgeDetector = new AutoEdge(this.Image); this.Image = this.EdgeDetector.Image; } return(this.Image); }