diff --git a/GerberCombiner/CombinerProgram.cs b/GerberCombiner/CombinerProgram.cs index 4b985c2..3b09ddc 100644 --- a/GerberCombiner/CombinerProgram.cs +++ b/GerberCombiner/CombinerProgram.cs @@ -15,9 +15,10 @@ static void Main(string[] args) { - GerberSplitter GS = new GerberSplitter(); - GS.Split("G03*", new GerberLibrary.Core.Primitives.GerberNumberFormat()); - Console.WriteLine("{0}", GS.Pairs.Count); + //GerberSplitter GS = new GerberSplitter(); + //GS.Split("G03*", new GerberLibrary.Core.Primitives.GerberNumberFormat()); + //GS.Split("%SRX1Y1I0.00000J1.76100*%", new GerberLibrary.Core.Primitives.GerberNumberFormat(),true); + //Console.WriteLine("{0}", GS.Pairs.Count); Gerber.ShowProgress = true; if (args.Count() < 2) { @@ -51,10 +52,8 @@ static void Main(string[] args) { Console.WriteLine("Warning! Filetypes seem to be mismatched! First file ({0}) is a {1}, but {2} is a {3}", args[1], FileType, args[j], FileType2); } - } - if (FileType == BoardFileType.Drill) { List ExcellonFiles = new List(); @@ -67,8 +66,8 @@ static void Main(string[] args) GerberFiles.AddRange(args.Skip(1)); GerberMerger.MergeAll(GerberFiles, args[0], new StandardConsoleLog()); } - // Console.WriteLine("Press any key to continue.."); - // Console.ReadKey(); + // Console.WriteLine("Press any key to continue.."); + // Console.ReadKey(); } } diff --git a/GerberLibrary/Core/GerberMerger.cs b/GerberLibrary/Core/GerberMerger.cs index b5a0a7e..a00bebc 100644 --- a/GerberLibrary/Core/GerberMerger.cs +++ b/GerberLibrary/Core/GerberMerger.cs @@ -32,10 +32,10 @@ public static void MergeAll(List Files, string output, ProgressLog Log) { Log.AddString("Need files to do anything??"); } - Log.PopActivity(Check); + Log.PopActivity(Check); return; } - + List TempFiles = new List(); if (true) @@ -45,8 +45,8 @@ public static void MergeAll(List Files, string output, ProgressLog Log) foreach (var a in Files) { LeftOverFiles.Add(a); - } - + } + while (LeftOverFiles.Count > 1) { Log.AddString(String.Format("{0} files left in mergequeue", LeftOverFiles.Count)); @@ -92,7 +92,7 @@ public static void MergeAll(List Files, string output, ProgressLog Log) public static void MultiMerge(string file1, List filestomergein, string output, ProgressLog Log) { - int check = Log.PushActivity("Gerber MultiMerge"); + int check = Log.PushActivity("Gerber MultiMerge"); if (File.Exists(file1) == false) { Log.AddString(String.Format("{0} not found! stopping process!", Path.GetFileName(file1))); @@ -118,7 +118,7 @@ public static void MultiMerge(string file1, List filestomergein, string OtherFiles.Add(OtherFileParsed); OtherFileParsed.OriginalLines = OtherLines; MaxDigitsAfter = Math.Max(OtherFileParsed.State.CoordinateFormat.DigitsAfter, MaxDigitsAfter); - MaxDigitsBefore= Math.Max(OtherFileParsed.State.CoordinateFormat.DigitsBefore, MaxDigitsBefore); + MaxDigitsBefore = Math.Max(OtherFileParsed.State.CoordinateFormat.DigitsBefore, MaxDigitsBefore); } List File1Lines = File.ReadAllLines(file1).ToList(); @@ -126,9 +126,9 @@ public static void MultiMerge(string file1, List filestomergein, string File1Lines = PolyLineSet.SanitizeInputLines(File1Lines); ParsedGerber File1Parsed = PolyLineSet.ParseGerber274x(Log, File1Lines, true, false, new GerberParserState() { PreCombinePolygons = false, GenerateGeometry = false }); - - - + + + List OutputLines = new List(); GerberNumberFormat GNF = new GerberNumberFormat(); GNF.DigitsBefore = Math.Max(File1Parsed.State.CoordinateFormat.DigitsBefore, MaxDigitsBefore); @@ -173,7 +173,7 @@ public static void MultiMerge(string file1, List filestomergein, string { OutputLines.Add(a.Value.BuildGerber(GNF)); } - + int ApertureOffset = 0; if (File1Parsed.State.Apertures.Count > 0) ApertureOffset = File1Parsed.State.Apertures.Keys.Max() + 1; @@ -196,6 +196,9 @@ public static void MultiMerge(string file1, List filestomergein, string // stuff goes here. //OutputLines.Add(String.Format("G04 :starting {0}", Path.GetFileNameWithoutExtension(file1))); string CurrentPolarity = "LPD"; + bool Repeating = false; + double X = 0; + double Y = 0; for (int i = 0; i < File1Lines.Count; i++) { string CurrentLine = File1Lines[i]; @@ -222,12 +225,38 @@ public static void MultiMerge(string file1, List filestomergein, string if (CurrentPolarity != "LPC") { OutputLines.Add("%LPC*%"); CurrentPolarity = "LPC"; } break; default: - if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + if (CurrentLine.StartsWith("%SR")) { - while (CurrentLine[CurrentLine.Length - 1] != '%') + OutputLines.Add(CurrentLine); + // Console.WriteLine("StepRepeatYay!", CurrentLine); + if (GCC.numbercommands.Count > 0) { - i++; - CurrentLine = File1Lines[i]; + int x = (int)GCC.numbercommands[0]; + int y = (int)GCC.numbercommands[1]; + if (x > 1 && y > 1) + { + Repeating = true; + } + else + { + Repeating = false; + } + } + else + { + Repeating = false; + } + } + else + { + //Console.WriteLine("eating % commands... {0}", CurrentLine); + if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + { + while (CurrentLine[CurrentLine.Length - 1] != '%') + { + i++; + CurrentLine = File1Lines[i]; + } } } break; @@ -252,6 +281,21 @@ public static void MultiMerge(string file1, List filestomergein, string //GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); GS.ScaleToMM(File1Parsed.State.CoordinateFormat); + + if (File1Parsed.State.CoordinateFormat.Relativemode) + { + if (GS.Has("X")) + { + X += GS.Get("X"); + GS.Set("X", X); + } + if (GS.Has("Y")) + { + Y += GS.Get("Y"); + GS.Set("Y", Y); + } + } + GS.ScaleToFile(GNF); @@ -300,8 +344,11 @@ public static void MultiMerge(string file1, List filestomergein, string default: // move commands -> transform { GerberSplitter GS = new GerberSplitter(); - double X = 0; - double Y = 0; + if (File1Parsed.State.CoordinateFormat.Relativemode == false) + { + X = 0; + Y = 0; + } GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); GS.ApplyMirroring(File1Parsed.State); if (GS.Has("D") || GS.Has("X") || GS.Has("Y")) @@ -322,8 +369,8 @@ public static void MultiMerge(string file1, List filestomergein, string if (GS.Has("X") && GS.Has("Y")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); } @@ -331,14 +378,14 @@ public static void MultiMerge(string file1, List filestomergein, string { if (GS.Has("X")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); OutLine = String.Format("X{0}", GNF.Format(GNF._ScaleMMToFile(X))); } else { if (GS.Has("Y")) { - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("Y{0}", GNF.Format(GNF._ScaleMMToFile(Y))); } else @@ -389,13 +436,22 @@ public static void MultiMerge(string file1, List filestomergein, string } } // TODO: set polarity back to "neutral" - or should each file do this at the start anyway? + + if (Repeating) + { + OutputLines.Add("G04 previous file was still repeating.. stopping that*"); + OutputLines.Add("%SR*%"); + Repeating = false; + } if (Gerber.ShowProgress) { - Log.AddString(String.Format("File 1 format: {0}", File1Parsed.State.CoordinateFormat)); + Log.AddString(String.Format("File 1 format: {0}", File1Parsed.State.CoordinateFormat)); } foreach (var otherfile in OtherFiles) { + X = 0; + Y = 0; if (Gerber.ShowProgress) { Log.AddString(String.Format("File 2 format: {0}", otherfile.State.CoordinateFormat)); @@ -430,12 +486,39 @@ public static void MultiMerge(string file1, List filestomergein, string break; default: - if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + if (CurrentLine.StartsWith("%SR")) { - while (CurrentLine[CurrentLine.Length - 1] != '%') + OutputLines.Add(CurrentLine); + //Console.WriteLine("StepRepeatYay!", CurrentLine); + if (GCC.numbercommands.Count > 0) { - i++; - CurrentLine = otherfile.OriginalLines[i]; + int x = (int)GCC.numbercommands[0]; + int y = (int)GCC.numbercommands[1]; + if (x > 1 && y > 1) + { + Repeating = true; + } + else + { + Repeating = false; + } + } + else + { + Repeating = false; + } + + } + else + { + //Console.WriteLine("Eating gerber % commands here too! {0}", FinalLine); + if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + { + while (CurrentLine[CurrentLine.Length - 1] != '%') + { + i++; + CurrentLine = otherfile.OriginalLines[i]; + } } } break; @@ -464,10 +547,23 @@ public static void MultiMerge(string file1, List filestomergein, string case 2: case 3: { - // double X = 0; + // double X = 0; // double Y = 0; //GS.Split(GCC.originalline, otherfile.State.CoordinateFormat); GS.ScaleToMM(otherfile.State.CoordinateFormat); + if (otherfile.State.CoordinateFormat.Relativemode) + { + if (GS.Has("X")) + { + X += GS.Get("X"); + GS.Set("X", X); + } + if (GS.Has("Y")) + { + Y += GS.Get("Y"); + GS.Set("Y", Y); + } + } GS.ScaleToFile(GNF); // if (GS.Has("X")) GS.Set("X", File2Parsed.CoordinateFormat.ScaleFileToMM(GS.Get("X"))); // if (GS.Has("Y")) GS.Set("Y", File2Parsed.CoordinateFormat.ScaleFileToMM(GS.Get("Y"))); @@ -506,8 +602,11 @@ public static void MultiMerge(string file1, List filestomergein, string default: // move commands -> transform { GerberSplitter GS = new GerberSplitter(); - double X = 0; - double Y = 0; + if (otherfile.State.CoordinateFormat.Relativemode == false) + { + X = 0; + Y = 0; + } GS.Split(GCC.originalline, otherfile.State.CoordinateFormat); GS.ApplyMirroring(otherfile.State); @@ -529,8 +628,8 @@ public static void MultiMerge(string file1, List filestomergein, string if (GS.Has("X") && GS.Has("Y")) { - X = otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - Y = otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + X += otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); } @@ -538,14 +637,14 @@ public static void MultiMerge(string file1, List filestomergein, string { if (GS.Has("X")) { - X = otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + X += otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); OutLine = String.Format("X{0}", GNF.Format(GNF._ScaleMMToFile(X))); } else { if (GS.Has("Y")) { - Y = otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + Y += otherfile.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("Y{0}", GNF.Format(GNF._ScaleMMToFile(Y))); } else @@ -619,6 +718,14 @@ public static void MultiMerge(string file1, List filestomergein, string } catch (Exception) { } } + + if (Repeating) + { + OutputLines.Add("G04 previous file was still repeating.. stopping that*"); + OutputLines.Add("%SR*%"); + Repeating = false; + } + } @@ -630,7 +737,7 @@ public static void MultiMerge(string file1, List filestomergein, string } - internal static void MergeAllByFileType(List allFiles, string targetfolder,string combinedfilename, ProgressLog Logger) + internal static void MergeAllByFileType(List allFiles, string targetfolder, string combinedfilename, ProgressLog Logger) { Dictionary> FilesPerExt = new Dictionary>(); Dictionary FileTypePerExt = new Dictionary(); @@ -638,12 +745,12 @@ internal static void MergeAllByFileType(List allFiles, string targetfold { string ext = Path.GetExtension(s).ToLower(); ; if (ext == "xln") ext = "txt"; - if (ext == "drl") ext = "txt"; + if (ext == "drl") ext = "txt"; BoardLayer layer; BoardSide Side; Gerber.DetermineBoardSideAndLayer(s, out Side, out layer); - ext = String.Format(".{0}_{1}", layer, Side); + ext = String.Format(".{0}_{1}", layer, Side); if (FilesPerExt.ContainsKey(ext) == false) { @@ -672,7 +779,7 @@ internal static void MergeAllByFileType(List allFiles, string targetfold if (a.Key.ToLower() != ".gko") { string Filename = Path.Combine(targetfolder, combinedfilename + a.Key); - // FinalFiles.Add(Filename); + // FinalFiles.Add(Filename); GerberMerger.MergeAll(a.Value, Filename, Logger); } } @@ -684,7 +791,7 @@ internal static void MergeAllByFileType(List allFiles, string targetfold private static void CheckAllApertures(ParsedGerber file1Parsed, List File1Lines, ProgressLog Log) { - + for (int i = 0; i < File1Lines.Count; i++) { string CurrentLine = File1Lines[i]; @@ -717,7 +824,8 @@ private static void CheckAllApertures(ParsedGerber file1Parsed, List Fil } } - }break; + } + break; case 'D': { GerberSplitter GS = new GerberSplitter(); @@ -732,7 +840,8 @@ private static void CheckAllApertures(ParsedGerber file1Parsed, List Fil file1Parsed.State.Apertures[ApertureID] = GAT; } } - }break; + } + break; } } catch (Exception) { } @@ -759,8 +868,8 @@ public static void Merge(string file1, string file2, string output, ProgressLog List File1Lines = File.ReadAllLines(file1).ToList(); File1Lines = PolyLineSet.SanitizeInputLines(File1Lines); - ParsedGerber File1Parsed = PolyLineSet.ParseGerber274x(Log, File1Lines, true,false, new GerberParserState(){ PreCombinePolygons = false, GenerateGeometry = false}); - + ParsedGerber File1Parsed = PolyLineSet.ParseGerber274x(Log, File1Lines, true, false, new GerberParserState() { PreCombinePolygons = false, GenerateGeometry = false }); + List File2Lines = File.ReadAllLines(file2).ToList(); File2Lines = PolyLineSet.SanitizeInputLines(File2Lines); ParsedGerber File2Parsed = PolyLineSet.ParseGerber274x(Log, File2Lines, true, false, new GerberParserState() { PreCombinePolygons = false, GenerateGeometry = false }); @@ -787,7 +896,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog } GNF.SetImperialMode(); - + //OutputLines.Add(String.Format("G04 merged from {0} and {1}", Path.GetFileNameWithoutExtension(file1), Path.GetFileNameWithoutExtension(file2))); OutputLines.Add(GNF.BuildMetricImperialFormatLine()); OutputLines.Add("%OFA0B0*%"); @@ -801,17 +910,17 @@ public static void Merge(string file1, string file2, string output, ProgressLog foreach (var a in File1Parsed.State.ApertureMacros) { OutputLines.Add(a.Value.BuildGerber(GNF, 0).Trim()); - MacroDict[a.Value.Name+"____"] = a.Value.Name; + MacroDict[a.Value.Name + "____"] = a.Value.Name; } foreach (var a in File2Parsed.State.ApertureMacros) { int off = 0; - string name = string.Format("{0}{1}", a.Value.Name, off); + string name = string.Format("{0}{1}", a.Value.Name, off); while (MacroDict.Values.Contains(name)) { off++; - name = string.Format("{0}{1}", a.Value.Name, off); + name = string.Format("{0}{1}", a.Value.Name, off); } MacroDict[a.Value.Name] = name; a.Value.Name = name; @@ -834,7 +943,10 @@ public static void Merge(string file1, string file2, string output, ProgressLog } // stuff goes here. //OutputLines.Add(String.Format("G04 :starting {0}", Path.GetFileNameWithoutExtension(file1))); - string CurrentPolarity = "LPD"; + string CurrentPolarity = "LPD"; + bool Repeating = false; + double X = 0; + double Y = 0; for (int i = 0; i < File1Lines.Count; i++) { string CurrentLine = File1Lines[i]; @@ -861,12 +973,40 @@ public static void Merge(string file1, string file2, string output, ProgressLog if (CurrentPolarity != CurrentLine) { OutputLines.Add("%LPC*%"); CurrentPolarity = CurrentLine; } break; default: - if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + if (CurrentLine.StartsWith("%SR")) { - while (CurrentLine[CurrentLine.Length - 1] != '%') + OutputLines.Add(CurrentLine); + //Console.WriteLine("StepRepeatYay!", CurrentLine); + if (GCC.numbercommands.Count > 0) { - i++; - CurrentLine = File1Lines[i]; + int x = (int)GCC.numbercommands[0]; + int y = (int)GCC.numbercommands[1]; + if (x > 1 && y > 1) + { + Repeating = true; + } + else + { + Repeating = false; + } + } + else + { + Repeating = false; + } + + + } + else + { + //Console.WriteLine("eating % commands! {0}", CurrentLine); + if (CurrentLine.Length > 1 && CurrentLine[CurrentLine.Length - 1] != '%') + { + while (CurrentLine[CurrentLine.Length - 1] != '%') + { + i++; + CurrentLine = File1Lines[i]; + } } } break; @@ -877,7 +1017,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog { GerberSplitter GS = new GerberSplitter(); GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); - if ((int)GS.Get("G")==54) + if ((int)GS.Get("G") == 54) { if (GS.Has("D") && GS.Get("D") > 3) { @@ -887,11 +1027,11 @@ public static void Merge(string file1, string file2, string output, ProgressLog else if ((int)GCC.numbercommands[0] == 3 || (int)GCC.numbercommands[0] == 2 || (int)GCC.numbercommands[0] == 1) { - + //GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); GS.ScaleToMM(File1Parsed.State.CoordinateFormat); GS.ScaleToFile(GNF); - + OutputLines.Add(GS.Rebuild(GNF)); // Log.AddString("Unsupported arc command found!"); @@ -938,10 +1078,13 @@ public static void Merge(string file1, string file2, string output, ProgressLog default: // move commands -> transform { GerberSplitter GS = new GerberSplitter(); - double X = 0; - double Y = 0; + if (File1Parsed.State.CoordinateFormat.Relativemode == false) + { + X = 0; + Y = 0; + } GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); - + if (GS.Has("D") || GS.Has("X") || GS.Has("Y")) { if (GS.Get("D") > 3) @@ -960,23 +1103,23 @@ public static void Merge(string file1, string file2, string output, ProgressLog if (GS.Has("X") && GS.Has("Y")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); - - OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + + OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); } else { if (GS.Has("X")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); OutLine = String.Format("X{0}", GNF.Format(GNF._ScaleMMToFile(X))); } else { if (GS.Has("Y")) { - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("Y{0}", GNF.Format(GNF._ScaleMMToFile(Y))); } else @@ -996,7 +1139,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog } if (GS.Has("J")) { - var J = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("J")); + var J = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("J")); OutLine += String.Format("J{0}", GNF.Format(GNF._ScaleMMToFile(J))); } if (GS.Has("D")) @@ -1018,12 +1161,20 @@ public static void Merge(string file1, string file2, string output, ProgressLog break; } } - catch(Exception) + catch (Exception) { - + } } // TODO: set polarity back to "neutral" - or should each file do this at the start anyway? + + if (Repeating) + { + OutputLines.Add("G04 previous file was still repeating.. stopping that*"); + OutputLines.Add("%SR*%"); + Repeating = false; + } + if (Gerber.ShowProgress) { Log.AddString(String.Format("File 1 format: {0}", File1Parsed.State.CoordinateFormat)); @@ -1032,7 +1183,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog OutputLines.Add("G04 next file*"); if (CurrentPolarity != "LPD") { OutputLines.Add("%LPD*%"); CurrentPolarity = "LPD"; } - + X = 0;Y = 0; //OutputLines.Add( String.Format("G04 :starting {0}", Path.GetFileNameWithoutExtension(file2))); for (int i = 0; i < File2Lines.Count; i++) { @@ -1046,16 +1197,16 @@ public static void Merge(string file1, string file2, string output, ProgressLog { case '%': { - string FinalLine = (""+ CurrentLine).Replace("%", "").Replace("*", "").Trim(); + string FinalLine = ("" + CurrentLine).Replace("%", "").Replace("*", "").Trim(); switch (FinalLine) { case "LPD": if (CurrentPolarity != "LPD") { OutputLines.Add("%LPD*%"); CurrentPolarity = "LPD"; } - + break; case "LPC": - if (CurrentPolarity != "LPC") { OutputLines.Add("%LPC*%" ); CurrentPolarity = "LPC"; } - + if (CurrentPolarity != "LPC") { OutputLines.Add("%LPC*%"); CurrentPolarity = "LPC"; } + break; @@ -1092,10 +1243,10 @@ public static void Merge(string file1, string file2, string output, ProgressLog case 2: case 3: { - //double X = 0; - // double Y = 0; - // GS.Split(GCC.originalline, File2Parsed.State.CoordinateFormat); - GS.ScaleToMM(File2Parsed.State.CoordinateFormat); + //double X = 0; + // double Y = 0; + // GS.Split(GCC.originalline, File2Parsed.State.CoordinateFormat); + GS.ScaleToMM(File2Parsed.State.CoordinateFormat); GS.ScaleToFile(GNF); // if (GS.Has("X")) GS.Set("X", File2Parsed.CoordinateFormat.ScaleFileToMM(GS.Get("X"))); // if (GS.Has("Y")) GS.Set("Y", File2Parsed.CoordinateFormat.ScaleFileToMM(GS.Get("Y"))); @@ -1104,22 +1255,23 @@ public static void Merge(string file1, string file2, string output, ProgressLog OutputLines.Add(GS.Rebuild(GNF)); // Log.AddString("Unsupported arc command found!"); // MessageBox.Show("Unsupported arc type found during merge! File will NOT be exported correctly!", "Error during export", MessageBoxButtons.OK, MessageBoxIcon.Error); - } break; + } + break; default: - - - - switch ((int)GS.Get("G")) + + + + switch ((int)GS.Get("G")) { case 70: OutputLines.Add("G04 skipping 70"); File2Parsed.State.CoordinateFormat.SetImperialMode(); - + break; case 71: OutputLines.Add("G04 skipping 71"); File2Parsed.State.CoordinateFormat.SetMetricMode(); - + break; default: OutputLines.Add(CurrentLine); @@ -1133,8 +1285,11 @@ public static void Merge(string file1, string file2, string output, ProgressLog default: // move commands -> transform { GerberSplitter GS = new GerberSplitter(); - double X = 0; - double Y = 0; + if (File2Parsed.State.CoordinateFormat.Relativemode == false) + { + X = 0; + Y = 0; + } GS.Split(GCC.originalline, File2Parsed.State.CoordinateFormat); @@ -1156,8 +1311,8 @@ public static void Merge(string file1, string file2, string output, ProgressLog if (GS.Has("X") && GS.Has("Y")) { - X = File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - Y = File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + X += File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); } @@ -1165,14 +1320,14 @@ public static void Merge(string file1, string file2, string output, ProgressLog { if (GS.Has("X")) { - X = File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + X += File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); OutLine = String.Format("X{0}", GNF.Format(GNF._ScaleMMToFile(X))); } else { if (GS.Has("Y")) { - Y = File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + Y += File2Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("Y{0}", GNF.Format(GNF._ScaleMMToFile(Y))); } else @@ -1204,7 +1359,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog OutLine += "*"; OutputLines.Add(OutLine); - + } } } @@ -1234,7 +1389,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog { OutputLines.Add(CurrentLine); } - + } } } @@ -1246,6 +1401,12 @@ public static void Merge(string file1, string file2, string output, ProgressLog catch (Exception) { } } + if (Repeating) + { + OutputLines.Add("G04 previous file was still repeating.. stopping that*"); + OutputLines.Add("%SR*%"); + Repeating = false; + } OutputLines.Add(Gerber.EOF); Gerber.WriteAllLines(output, PolyLineSet.SanitizeInputLines(OutputLines)); @@ -1254,7 +1415,7 @@ public static void Merge(string file1, string file2, string output, ProgressLog } - public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string outputfilename, ProgressLog Log) + public static void WriteContainedOnly(string inputfile, PolyLine Boundary, string outputfilename, ProgressLog Log) { Log.PushActivity("Gerber Clipper"); if (File.Exists(inputfile) == false) @@ -1263,7 +1424,7 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string Log.PopActivity(); return; } - + Log.AddString(String.Format("Clipping {0} to {1}", Path.GetFileName(inputfile), Path.GetFileName(outputfilename))); List File1Lines = File.ReadAllLines(inputfile).ToList(); @@ -1306,7 +1467,7 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string MacroDict[a.Value.Name + "____"] = a.Value.Name; } - + foreach (var a in File1Parsed.State.Apertures) { OutputLines.Add(a.Value.BuildGerber(GNF)); @@ -1315,6 +1476,8 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string // stuff goes here. //OutputLines.Add(String.Format("G04 :starting {0}", Path.GetFileNameWithoutExtension(file1))); string CurrentPolarity = "LPD"; + double X = 0; + double Y = 0; for (int i = 0; i < File1Lines.Count; i++) { string CurrentLine = File1Lines[i]; @@ -1376,15 +1539,15 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string if (GS.Has("X") && GS.Has("Y")) { - double X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - double Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); if (Boundary.PointInPoly(new PointD(X, Y)) == false) { skipline = true; } - // OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); + // OutLine = String.Format("X{0}Y{1}", GNF.Format(GNF._ScaleMMToFile(X)), GNF.Format(GNF._ScaleMMToFile(Y))); } if (!skipline) OutputLines.Add(GS.Rebuild(GNF)); // Log.AddString("Unsupported arc command found!"); @@ -1431,8 +1594,11 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string default: // move commands -> transform { GerberSplitter GS = new GerberSplitter(); - double X = 0; - double Y = 0; + if (File1Parsed.State.CoordinateFormat.Relativemode == false) + { + X = 0; + Y = 0; + } GS.Split(GCC.originalline, File1Parsed.State.CoordinateFormat); if (GS.Has("D") || GS.Has("X") || GS.Has("Y")) @@ -1445,7 +1611,7 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string { if (GS.Has("X") == false && GS.Has("Y") == false) { - + OutputLines.Add(CurrentLine); } else @@ -1455,10 +1621,10 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string if (GS.Has("X") && GS.Has("Y")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); - if (Boundary.PointInPoly(new PointD(X,Y)) == false) + if (Boundary.PointInPoly(new PointD(X, Y)) == false) { skipline = true; } @@ -1469,14 +1635,14 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string { if (GS.Has("X")) { - X = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); + X += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); OutLine = String.Format("X{0}", GNF.Format(GNF._ScaleMMToFile(X))); } else { if (GS.Has("Y")) { - Y = File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); + Y += File1Parsed.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); OutLine = String.Format("Y{0}", GNF.Format(GNF._ScaleMMToFile(Y))); } else @@ -1527,15 +1693,15 @@ public static void WriteContainedOnly(string inputfile,PolyLine Boundary, string if (Gerber.ShowProgress) { Log.AddString(String.Format("File 1 format: {0}", File1Parsed.State.CoordinateFormat)); - + } OutputLines.Add("G04 next file*"); if (CurrentPolarity != "LPD") { OutputLines.Add("%LPD*%"); CurrentPolarity = "LPD"; } //OutputLines.Add( String.Format("G04 :starting {0}", Path.GetFileNameWithoutExtension(file2))); - - + + OutputLines.Add(Gerber.EOF); Gerber.WriteAllLines(outputfilename, PolyLineSet.SanitizeInputLines(OutputLines)); diff --git a/GerberLibrary/Core/PolyLineSet.cs b/GerberLibrary/Core/PolyLineSet.cs index ce68292..d3f09b8 100644 --- a/GerberLibrary/Core/PolyLineSet.cs +++ b/GerberLibrary/Core/PolyLineSet.cs @@ -1054,6 +1054,11 @@ private static bool BasicLineCommands(string Line, GerberParserState State) private static void DoRepeating(GerberParserState State) { + if (State.Repeater == false) + { + // cant stop a repeat that is not happening! + return; + } int LastThin = State.NewThinShapes.Count(); int LastShape = State.NewShapes.Count(); for (int x = 0; x < State.RepeatXCount; x++) @@ -1195,7 +1200,7 @@ private static void ParseGerber274_Lines(bool forcezerowidth, GerberParserState { if (Gerber.ShowProgress) Console.WriteLine("Setting up step and repeat "); GerberSplitter GS2 = new GerberSplitter(); - GS2.Split(GCC.originalline, State.CoordinateFormat); + GS2.Split(GCC.originalline, State.CoordinateFormat, true); if (GCC.numbercommands.Count == 0) { DoRepeating(State); @@ -1210,6 +1215,11 @@ private static void ParseGerber274_Lines(bool forcezerowidth, GerberParserState { SetupRepeater(State, Xcount, Ycount, Xoff, Yoff); } + else + { + // apparently this is a 1:1 command! + DoRepeating(State); + } } } break;