public static bool SaveGerberFileToImage(string GerberFilename, string BitmapFilename, float dpi, Color Foreground, Color Background) { try { ParsedGerber PLS; GerberParserState State = new GerberParserState() { PreCombinePolygons = false }; var FileType = Gerber.FindFileType(GerberFilename); Gerber.DetermineBoardSideAndLayer(GerberFilename, out State.Side, out State.Layer); bool forcezero = false; if (State.Layer == BoardLayer.Outline) { // PLS.PreCombinePolygons = true; // forcezero = true; } if (FileType == BoardFileType.Drill) { PLS = PolyLineSet.LoadExcellonDrillFile(GerberFilename); } else { PLS = PolyLineSet.LoadGerberFile(GerberFilename, forcezero, Gerber.WriteSanitized, State); } double WidthInMM = PLS.BoundingBox.BottomRight.X - PLS.BoundingBox.TopLeft.X; double HeightInMM = PLS.BoundingBox.BottomRight.Y - PLS.BoundingBox.TopLeft.Y; int Width = (int)(Math.Ceiling((WidthInMM) * (dpi / 25.4))); int Height = (int)(Math.Ceiling((HeightInMM) * (dpi / 25.4))); Console.WriteLine("Progress: Exporting {0} ({2},{3}mm) to {1} ({4},{5})", GerberFilename, BitmapFilename, WidthInMM, HeightInMM, Width, Height); GerberImageCreator GIC = new GerberImageCreator(); GIC.scale = dpi / 25.4f; // dpi GIC.BoundingBox.AddBox(PLS.BoundingBox); var Tr = GIC.BuildMatrix(Width, Height); Bitmap B2 = GIC.RenderToBitmap(Width, Height, Tr, Foreground, Background, PLS, true); if (B2 == null) { return(false); } B2.Save(BitmapFilename); return(true); } catch (Exception E) { Console.WriteLine("Error: Errors while writing bitmap {0} for gerberfile {1} at dpi {2}:", BitmapFilename, GerberFilename, dpi); while (E != null) { Console.WriteLine("Error: \t{0}", E.Message); E = E.InnerException; } return(false); } }
public static bool SaveDebugImage(string GerberFilename, string BitmapFilename, float dpi, Color Foreground, Color Background) { ParsedGerber PLS; GerberParserState State = new GerberParserState() { PreCombinePolygons = false }; var FileType = Gerber.FindFileType(GerberFilename); Gerber.DetermineBoardSideAndLayer(GerberFilename, out State.Side, out State.Layer); bool forcezero = false; if (State.Layer == BoardLayer.Outline) { // PLS.PreCombinePolygons = true; // forcezero = true; } if (FileType == BoardFileType.Drill) { PLS = PolyLineSet.LoadExcellonDrillFile(GerberFilename); } else { PLS = PolyLineSet.LoadGerberFile(GerberFilename, forcezero, Gerber.WriteSanitized, State); } double WidthInMM = PLS.BoundingBox.BottomRight.X - PLS.BoundingBox.TopLeft.X; double HeightInMM = PLS.BoundingBox.BottomRight.Y - PLS.BoundingBox.TopLeft.Y; int Width = (int)(Math.Ceiling((WidthInMM) * (dpi / 25.4))); int Height = (int)(Math.Ceiling((HeightInMM) * (dpi / 25.4))); Console.WriteLine("Progress: Exporting {0} ({2},{3}mm) to {1} ({4},{5})", GerberFilename, BitmapFilename, WidthInMM, HeightInMM, Width, Height); GerberImageCreator GIC = new GerberImageCreator(); GIC.scale = dpi / 25.4f; // dpi GIC.BoundingBox.AddBox(PLS.BoundingBox); var Tr = GIC.BuildMatrix(Width, Height); Bitmap B2 = GIC.RenderToBitmap(Width, Height, Tr, Foreground, Background, PLS, true); if (B2 == null) { return(false); } var GerberLines = PolyLineSet.SanitizeInputLines(System.IO.File.ReadAllLines(GerberFilename).ToList()); double LastX = 0; double LastY = 0; Graphics G2 = Graphics.FromImage(B2); GerberImageCreator.ApplyAASettings(G2); //G2.Clear(Background); G2.Transform = Tr.Clone(); foreach (var L in GerberLines) { if (L[0] != '%') { GerberSplitter GS = new GerberSplitter(); GS.Split(L, PLS.State.CoordinateFormat); if (GS.Has("G") && (int)GS.Get("G") == 3) { double X = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); double Y = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); double I = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("I")); double J = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("J")); //Console.WriteLine("Counterclockwise Curve {0},{1} -> {2},{3}", LastX, LastY, X, Y); DrawCross(G2, X, Y, Color.Blue); DrawCross(G2, LastX, LastY, Color.Red); DrawCross(G2, LastX + I, LastY - J, Color.Yellow); DrawCross(G2, LastX + I, LastY + J, Color.Purple); DrawCross(G2, LastX - I, LastY - J, Color.Green); DrawCross(G2, LastX - I, LastY + J, Color.Orange); } if (GS.Has("X")) { LastX = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("X")); } if (GS.Has("Y")) { LastY = PLS.State.CoordinateFormat.ScaleFileToMM(GS.Get("Y")); } } } B2.Save(BitmapFilename); return(true); }