private static void ConvertDrill(string[] drillCode, string info) { Logger.Info(" ConvertDrill {0}", info); bool isHeader = false; int holeCount = 0; int drillCount = 0; int groupCount = 0; Dictionary <string, string> tNrToDiameter = new Dictionary <string, string>(); string attDiameter = ""; // string xmlString = ""; Logger.Info(" Amount Lines:{0}", drillCode.Length); if (backgroundWorker != null) { backgroundWorker.ReportProgress(0, new MyUserState { Value = 10, Content = "Read DXF vector data of " + drillCode.Length.ToString() + " lines" }); } int lineNr = 0; foreach (string line in drillCode) { if (backgroundWorker != null) { backgroundWorker.ReportProgress(lineNr++ *100 / drillCode.Length); if (backgroundWorker.CancellationPending) { backgroundEvent.Cancel = true; break; } } if (line.IndexOf("%") >= 0) { isHeader = (isHeader)? false:true; } // Header is between two % if (isHeader) { if ((line.IndexOf("T") >= 0) && (line.IndexOf("C") >= 0)) { string[] part = line.Split('C'); int tnr = 0; double dinch = 0; double dmm = 0; Int32.TryParse(part[0].Substring(1), out tnr); Double.TryParse(part[1].Substring(0), out dinch); dmm = dinch * 25.4; infoDrill[tnr] = part[1] + " Inch = " + dmm.ToString() + " mm"; Graphic.SetHeaderInfo(string.Format(" Tool-Nr.:{0} Diameter:{1:0.00} mm ({2:0.000} inch)", tnr, dmm, dinch)); Logger.Debug(" Tool-Nr.:{0} Diameter:{1:0.00} mm ({2:0.000} inch)", tnr, dmm, dinch); drillCount++; if (!tNrToDiameter.ContainsKey(part[0].Trim())) { tNrToDiameter.Add(part[0].Trim(), string.Format(" Diameter_mm=\"{0:0.00}\" Diameter_inch=\"{1:0.000}\"", dmm, dinch)); } } } else { if (line.IndexOf("T") >= 0) { attDiameter = ""; if (tNrToDiameter.ContainsKey(line)) { attDiameter = tNrToDiameter[line]; } groupCount++; int tnr = 0; Int32.TryParse(line.Substring(1), out tnr); Graphic.SetPenWidth(infoDrill[tnr].ToString()); Graphic.SetPenColor(line); } if ((line.IndexOf("X") >= 0) && (line.IndexOf("Y") >= 0)) { string[] part = line.Split('Y'); double x = 0; double y = 0; double.TryParse(part[0].Substring(1), out x); double.TryParse(part[1].Substring(0), out y); x = x * infoFraction; y = y * infoFraction; if (importUnitmm) { x = x * 25.4; y = y * 25.4; } string cmt = ""; if (importComments) { cmt = line; } Graphic.StartPath(new System.Windows.Point(x, y)); Graphic.AddDot(x, y); Graphic.StopPath(); holeCount++; } } } conversionInfo += string.Format("Drills:{0} Holes:{1}", drillCount, holeCount); }
/// <summary> /// Entrypoint for conversion: apply file-path /// </summary> /// <param name="file">String keeping file-name</param> /// <returns>String with GCode of imported data</returns> public static bool ConvertFromFile(string file, BackgroundWorker worker, DoWorkEventArgs e) { Logger.Info(" Create GCode from {0}", file); if (file == "") { MessageBox.Show("Empty file name"); return(false); } backgroundWorker = worker; backgroundEvent = e; Graphic.Init(Graphic.SourceTypes.Drill, file, backgroundWorker, backgroundEvent); Logger.Info(" convertDrill {0}", file); conversionInfo = ""; for (int i = 0; i < 20; i++) { infoDrill[i] = ""; } if (file.Substring(0, 4) == "http") { MessageBox.Show("Load via http is not supported up to now"); } else { string file_dri = "", file_drd = ""; if (file.Substring(file.Length - 3, 3).ToLower() == "dri") // build up filenames { file_dri = file; file_drd = file.Substring(0, file.Length - 3) + "drd"; } else if (file.Substring(file.Length - 3, 3).ToLower() == "drd") // build up filenames { file_drd = file; file_dri = file.Substring(0, file.Length - 3) + "dri"; } else { file_drd = file; // KiCad drl file_dri = ""; } /* if (File.Exists(file_dri)) * { try * { string[] drillInformation = File.ReadAllLines(file_dri); // get drill information * GetDrillInfos(drillInformation); * } * catch (Exception e) * { MessageBox.Show("Error '" + e.ToString() + "' in file " + file_dri); return ""; } * } * else { conversionInfo += "Error: DRI-File not found "; * MessageBox.Show("Drill information not found : " + file_dri + "\r\nTry to convert *.drd with default settings"); return ""; } */ if (File.Exists(file_drd)) { try { string[] drillCoordinates = File.ReadAllLines(file_drd); // get drill coordinates ConvertDrill(drillCoordinates, file_drd); } catch (Exception err) { MessageBox.Show("Error '" + err.ToString() + "' in file " + file_drd); return(false); } } else { conversionInfo += "Error: DRD-File not found "; MessageBox.Show("Drill file does not exist: " + file_drd); return(false); } } gcodeUseSpindle = Properties.Settings.Default.importGCZEnable; return(Graphic.CreateGCode()); }
private void generateGCode1D(string code, string type, double width, double height) { Graphic.Init(Graphic.SourceTypes.Barcode, ""); Graphic.graphicInformation.ResetOptions(false); Graphic.graphicInformation.SetGroup(Graphic.GroupOptions.ByType, Graphic.SortOptions.none); Graphic.SetHeaderInfo(string.Format(" Barcode type :{0} ", type)); Graphic.SetHeaderInfo(string.Format(" Barcode text :{0} ", textBox1.Text)); double scanGap = (double)Properties.Settings.Default.importBarcode1DScanGap; // 100 DPI = 3.94 DPmm int xwidth = (int)Properties.Settings.Default.importBarcode1DLines; bool last1 = false; bool lastUp = false; double overall = code.Length * scanGap * xwidth; Logger.Trace("generateGCode1D scanGap:{0} factor:{1} code.Length:{2} overall-width:{3}", scanGap, xwidth, code.Length, overall); lblWidth1D.Text = (scanGap * xwidth * code.Length).ToString(); Graphic.SetHeaderInfo(string.Format(" Barcode width:{0} height:{1}", lblWidth1D.Text, height)); Graphic.SetType("Barcode"); Graphic.SetLayer(textBox1.Text); Graphic.SetPenColor("black"); Graphic.SetPenWidth(scanGap.ToString()); Graphic.SetGeometry(type); double xpos = 0; for (int pos = 0; pos < code.Length; pos++) { if (code[pos] == '1') { if (!last1) { if (!lastUp) { Graphic.StartPath(new System.Windows.Point(xpos, 0)); // start new on bottom } else { Graphic.StartPath(new System.Windows.Point(xpos, height)); // start new on bottom } } else { if (!lastUp) { Graphic.AddLine(new System.Windows.Point(xpos, 0)); // start new on top } else { Graphic.AddLine(new System.Windows.Point(xpos, height)); // start new on top } } for (int i = 0; i < xwidth; i++) { if (i > 0) { if (!lastUp) { Graphic.AddLine(new System.Windows.Point(xpos, 0)); // connect verticals on bottom } else { Graphic.AddLine(new System.Windows.Point(xpos, height)); // connect verticals on top } } if (!lastUp) { Graphic.AddLine(new System.Windows.Point(xpos, height)); // move up } else { Graphic.AddLine(new System.Windows.Point(xpos, 0)); // move down } xpos += scanGap; lastUp = !lastUp; } last1 = true; } else { if (last1) { Graphic.StopPath(); } xpos += scanGap * xwidth; last1 = false; } } barcodegcode = Graphic.CreateGCode(); }