void DoThread() { double DPI = 0; if (!double.TryParse(DPIbox.Text, out DPI)) { DPI = 200; } string OutlineFile = OutlineFileBox.Text; ParsedGerber PLS = null; PLS = PolyLineSet.LoadGerberFile(OutlineFile); string SilkFile = SilkFileTopBox.Text; string BitmapFile = BitmapFileTopBox.Text; bool Flipped = FlipBox.Checked; bool FlippedBottom = FlipInputBottom.Checked; bool Invert = InvertBox.Checked; bool InvertBottom = InvertBitmapBottom.Checked; string CopperFile = copperfilebox.Text; string soldermaskfile = soldermaskfilebox.Text; statustext = "processing top!"; CreateStuff(DPI, PLS, OutlineFile, SilkFile, BitmapFile, Flipped, Invert, CopperFile, soldermaskfile); statustext = "processing bottom!"; CreateStuff(DPI, PLS, OutlineFile, SilkFileBottomBox.Text, BitmapFileBottomBox.Text, FlippedBottom, InvertBottom, CopperFile, soldermaskfile); BGThread = null; statustext = "done!"; }
private void ProcessButton_Click(object sender, EventArgs e) { // Process double DPI = 0; if (!double.TryParse(DPIbox.Text, out DPI)) { DPI = 200; } string OutlineFile = OutlineFileBox.Text; ParsedGerber PLS = null; PLS = PolyLineSet.LoadGerberFile(OutlineFile); string SilkFile = SilkFileTopBox.Text; string BitmapFile = BitmapFileTopBox.Text; bool Flipped = FlipBox.Checked; bool Invert = InvertBox.Checked; string CopperFile = copperfilebox.Text; string soldermaskfile = soldermaskfilebox.Text; CreateStuff(DPI, PLS, OutlineFile, SilkFile, BitmapFile, Flipped, Invert, CopperFile, soldermaskfile); ///CreateStuff(DPI, PLS, OutlineFile, SilkFileBottomBox.Text, BitmapFileBottomBox.Text, FlipInputBottom.Checked, InvertBitmapBottom.Checked); }
static void ConvertFile(string from, string to, bool displayshapes, bool outlineshapes) { ParsedGerber PLS = PolyLineSet.LoadGerberFile(new StandardConsoleLog(), from, true, State: new GerberParserState() { PreCombinePolygons = true }); DxfDocument dxf = new DxfDocument(); // add your entities here if (outlineshapes) { foreach (var a in PLS.OutlineShapes) { List <netDxf.Entities.PolylineVertex> Vertices = new List <netDxf.Entities.PolylineVertex>(); foreach (var v in a.Vertices) { Vertices.Add(new netDxf.Entities.PolylineVertex(v.X, v.Y, 0)); } netDxf.Entities.Polyline pl = new netDxf.Entities.Polyline(Vertices, true); pl.Color = new AciColor(System.Drawing.Color.Blue); dxf.AddEntity(pl); } } if (displayshapes) { foreach (var a in PLS.DisplayShapes) { List <netDxf.Entities.PolylineVertex> Vertices = new List <netDxf.Entities.PolylineVertex>(); foreach (var v in a.Vertices) { Vertices.Add(new netDxf.Entities.PolylineVertex(v.X, v.Y, 0)); } netDxf.Entities.Polyline pl = new netDxf.Entities.Polyline(Vertices, true); pl.Color = new AciColor(System.Drawing.Color.Green); dxf.AddEntity(pl); } } if (false) { foreach (var a in PLS.Shapes) { List <netDxf.Entities.PolylineVertex> Vertices = new List <netDxf.Entities.PolylineVertex>(); foreach (var v in a.Vertices) { Vertices.Add(new netDxf.Entities.PolylineVertex(v.X, v.Y, 0)); } netDxf.Entities.Polyline pl = new netDxf.Entities.Polyline(Vertices, true); pl.Color = new AciColor(System.Drawing.Color.Red); dxf.AddEntity(pl); } } // save to file dxf.Save(to); }
private void OutlineFileBox_TextChanged(object sender, EventArgs e) { ParsedGerber PLS = null; string OutlineFile = OutlineFileBox.Text; if (System.IO.File.Exists(OutlineFile) == false) { return; } PLS = PolyLineSet.LoadGerberFile(OutlineFile); sizebox.Text = String.Format("{0}x{1}mm", PLS.BoundingBox.Width(), PLS.BoundingBox.Height()); }
public static PolyLineSet.Bounds GetBoundingBox(List <string> generatedFiles) { PolyLineSet.Bounds A = new PolyLineSet.Bounds(); foreach (var a in generatedFiles) { ParsedGerber PLS = PolyLineSet.LoadGerberFile(a, State: new GerberParserState() { PreCombinePolygons = false }); A.AddBox(PLS.BoundingBox); } return(A); }
private void CreateStuff(double DPI, ParsedGerber PLS, string OutlineFile, string SilkFile, string BitmapFile, bool Flipped, bool Invert) { if (BitmapFile.Length == 0) { return; } if (System.IO.File.Exists(BitmapFile) == false) { MessageBox.Show("Bitmap not found or not specified... aborting", "Aborting..", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (System.IO.File.Exists(OutlineFile) == false) { MessageBox.Show("Outline not found or not specified... aborting", "Aborting..", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } Bitmap B = (Bitmap)Image.FromFile(BitmapFile); if (Flipped) { B.RotateFlip(RotateFlipType.RotateNoneFlipX); } B.RotateFlip(RotateFlipType.RotateNoneFlipY); bool UseSilkFile = false; if (System.IO.File.Exists(SilkFile)) { UseSilkFile = true; } string output = OutputFolderBox.Text; string OutSilk = BitmapFile + ".SILK"; GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(OutSilk, PLS, DPI, B, Invert ? -128 : 128); if (UseSilkFile) { // merge things! GerberLibrary.GerberMerger.Merge(OutSilk, SilkFile, Path.Combine(output, Path.GetFileName(SilkFile)), this); } else { File.Copy(OutSilk, Path.Combine(output, Path.GetFileName(SilkFile)), true); } }
private static void DrawToInterface(ParsedGerber PLS, SVGGraphicsInterface SG) { Random R = new Random(); foreach (var a in PLS.DisplayShapes) { Pen P = new Pen(Color.FromArgb((byte)R.Next(), (byte)R.Next(), (byte)R.Next())); for (int i = 0; i < a.Vertices.Count; i++) { var v1 = a.Vertices[i]; var v2 = a.Vertices[(i + 1) % a.Vertices.Count]; SG.DrawLine(P, (float)v1.X, (float)v1.Y, (float)v2.X, (float)v2.Y); } } }
private void DrawGerber(GerberVBO G, ParsedGerber file, Color C, bool dotted = false) { Pen P = new Pen(C, 1.0f); if (dotted) { P.DashPattern = new float[2] { 2, 2 } } ; SolidBrush B = new SolidBrush(C); //GraphicsPath GP = new GraphicsPath(); var Out = file.IsOutline(); G.Dotted = dotted; int Vbefore = G.VertexCount(); foreach (var a in file.DisplayShapes) { if (a.Vertices.Count > 1) { PointF[] Points = new PointF[a.Vertices.Count]; for (int i = 0; i < a.Vertices.Count; i++) { Points[i] = a.Vertices[i].ToF(); } // GP.AddPolygon(Points); if (Out == false) { G.FillPath(C, Points); // GP = new GraphicsPath(); } else { G.DrawPath(C, Points, 1.0f, true); //G.DrawPath(C, Points, 1.0f); // GP = new GraphicsPath(); } } } if (Out) { } int Vafter = G.VertexCount(); Console.WriteLine("Drawing file: {0} - {1} shapes, {2} vertices", file.Name, file.DisplayShapes.Count, Vafter - Vbefore); }
static void Slice(string slicefile, List <string> inputgerbers) { List <PolyLine> SliceSet = new List <PolyLine>(); var OutputFolder = Path.GetDirectoryName(slicefile) + @"\Output\" + Path.GetFileNameWithoutExtension(slicefile); ParsedGerber P = PolyLineSet.LoadGerberFile(new StandardConsoleLog(), slicefile); foreach (var l in P.Shapes) { /* Polygon Pp = new Polygon(); * Pp.Closed = true; * var vertL = from i in l.Vertices select new vec2(i.X, i.Y); * Pp.Vertices.AddRange(vertL);*/ SliceSet.Add(l); } int slid = 1; foreach (var S in SliceSet) { Console.WriteLine("Slicing {0}/{1}", slid, SliceSet.Count); var SliceOutputFolder = Path.GetDirectoryName(slicefile) + @"\Output\" + Path.GetFileNameWithoutExtension(slicefile) + "\\Slice" + slid.ToString(); if (Directory.Exists(SliceOutputFolder) == false) { Directory.CreateDirectory(SliceOutputFolder); } foreach (var a in inputgerbers) { try { var bf = GerberLibrary.Gerber.FindFileType(a); if (bf == BoardFileType.Gerber) { BoardSide bs; BoardLayer L; GerberLibrary.Gerber.DetermineBoardSideAndLayer(a, out bs, out L); GerberMerger.WriteContainedOnly(a, S, Path.Combine(SliceOutputFolder + "\\", Path.GetFileName(a)), new StandardConsoleLog()); } } catch (Exception) { }; } slid++; } }
private static void RenderOutline(Graphics G, float S, ParsedGerber d, Color C, bool lines = true) { foreach (var ds in d.DisplayShapes) { List <PointF> Pts = new List <PointF>(); foreach (var V in ds.Vertices) { Pts.Add(new PointF((float)((V.X)), (float)((V.Y)))); } if (Pts.Count > 2) { if (lines) { G.DrawLines(new Pen(C, 1 / S), Pts.ToArray()); } else { G.FillPolygon(new SolidBrush(C), Pts.ToArray()); } } } }
private static void ConvertFile(string a, bool back) { string basename = a.Substring(0, a.Length - 4); string png = basename + "Silk.png"; string goldpng = basename + "Gold.png"; if (back) { png = basename + "BottomSilk.png"; goldpng = basename + "BottomGold.png"; } Bitmap B = (Bitmap)Image.FromFile(png); Bitmap B2 = null; if (System.IO.File.Exists(goldpng)) { B2 = (Bitmap)Image.FromFile(goldpng); } try { System.IO.Directory.CreateDirectory(basename); } catch (Exception) { } string newa = basename + "\\" + System.IO.Path.GetFileName(a);; string gko = basename + ".gko"; string pnl = basename + ".pnl"; if (System.IO.File.Exists(gko) == false && System.IO.File.Exists(pnl) == true) { gko = pnl; } string newgko = basename + "\\" + System.IO.Path.GetFileNameWithoutExtension(a) + ".gko";; if (System.IO.File.Exists(gko)) { System.IO.File.Copy(gko, newgko, true); } System.IO.File.Copy(a, newa, true); a = newa; string p = basename + "/topsilk.gto"; if (back) { p = basename + "/bottomsilk.gbo"; } if (back) { B.RotateFlip(RotateFlipType.RotateNoneFlipX); } B.RotateFlip(RotateFlipType.RotateNoneFlipY); if (B2 != null) { B2.RotateFlip(RotateFlipType.RotateNoneFlipY); } if (B != null) { double Res = 200.0 / 25.4; ParsedGerber PLS = null; string f = basename + ".gko"; if (System.IO.File.Exists(f)) { int polyid = 0; PLS = PolyLineSet.LoadGerberFile(f); string bottomcopper = basename + "/bottomcopper.gbl"; string topcopper = basename + "/topcopper.gtl"; string bottomsoldermask = basename + "/bottomsoldermask.gbs"; string topsoldermask = basename + "/topsoldermask.gts"; string bottomsilk = basename + "/bottomsilk.gbo"; if (back) { bottomsilk = basename + "/topsilk.gto"; } GerberArtWriter GAW3 = new GerberLibrary.GerberArtWriter(); GAW3.Write(topsoldermask); GerberArtWriter GAW = new GerberLibrary.GerberArtWriter(); PolyLine PL = new PolyLine(polyid++); PL.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.TopLeft.Y); PL.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.TopLeft.Y); PL.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.TopLeft.Y + 8); PL.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.TopLeft.Y + 8); GAW.AddPolygon(PL); PolyLine PL3 = new PolyLine(polyid++); PL3.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.BottomRight.Y - 8); PL3.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.BottomRight.Y - 8); PL3.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.BottomRight.Y); PL3.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.BottomRight.Y); GAW.AddPolygon(PL3); GAW.Write(bottomsoldermask); GerberArtWriter GAW2 = new GerberLibrary.GerberArtWriter(); PolyLine PL2 = new PolyLine(polyid++); PL2.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.TopLeft.Y); PL2.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.TopLeft.Y); PL2.Add(PLS.BoundingBox.BottomRight.X, PLS.BoundingBox.BottomRight.Y); PL2.Add(PLS.BoundingBox.TopLeft.X, PLS.BoundingBox.BottomRight.Y); GAW2.AddPolygon(PL2); GAW2.Write(bottomcopper); if (B2 != null) { GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(topcopper, PLS, Res, B2, -128); GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(topsoldermask, PLS, Res, B2, -128); } else { GAW2.Write(topcopper); } } GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(p, PLS, Res, B, -128); } }
/// <summary> /// /// </summary> /// <param name="sourcefile"></param> /// <param name="destfile"></param> /// <param name="DX">MM</param> /// <param name="DY">MM</param> /// <param name="Angle">Degrees</param> public static void Transform(string sourcefile, string destfile, double DX, double DY, double DXp, double DYp, double AngleInDeg = 0) { List <String> lines = new List <string>(); List <String> outlines = new List <string>(); bool WriteMove = false; int moveswritten = 0; double Angle = AngleInDeg * (Math.PI * 2.0) / 360.0; double CA = Math.Cos(Angle); double SA = Math.Sin(Angle); using (StreamReader sr = new StreamReader(sourcefile)) { while (sr.EndOfStream == false) { String line = sr.ReadLine(); if (line.Length > 0) { lines.Add(line); } } } lines = PolyLineSet.SanitizeInputLines(lines); if (Gerber.WriteSanitized) { Gerber.WriteAllLines(sourcefile + ".sanitized.txt", lines); } // PolyLineSet Parsed = new PolyLineSet("parsed gerber"); ParsedGerber Parsed = PolyLineSet.ParseGerber274x(lines, true, false, new GerberParserState() { GenerateGeometry = false }); if (Gerber.ShowProgress) { Console.WriteLine("found apertures: "); foreach (var a in Parsed.State.Apertures) { Console.WriteLine(a.Value.ToString()); } } GerberNumberFormat CoordinateFormat = new GerberNumberFormat(); CoordinateFormat.SetImperialMode(); // CoordinateFormat = Parsed.State.CoordinateFormat; int cur = 0; bool formatparsed = false; while (cur < lines.Count && formatparsed == false) { if (lines[cur].Length >= 2 && lines[cur].Substring(0, 2) == "%F") { CoordinateFormat.Parse(lines[cur]); formatparsed = true; } cur++; } // double coordmultiplier = 1.0; double LastX = 0; double LastY = 0; for (int i = 0; i < lines.Count; i++) { GerberSplitter GS = new GerberSplitter(); string FinalLine = lines[i].Replace("%", "").Replace("*", "").Trim(); bool DumpToOutput = false; bool metaopen = false; if (lines[i][0] == '%') { DumpToOutput = true; metaopen = true; } else { GS.Split(lines[i], CoordinateFormat); } switch (FinalLine) { case "G71": CoordinateFormat.SetMetricMode(); break; case "G70": CoordinateFormat.SetImperialMode(); break; case "MOIN": { CoordinateFormat.SetImperialMode(); //CoordinateFormat.Multiplier = 25.4f; } break; case "MOMM": { CoordinateFormat.SetMetricMode(); //CoordinateFormat.Multiplier = 1.0f; } break; } if (lines[i].Length > 3 && lines[i].Substring(0, 3) == "%AM") { string name = lines[i].Substring(3).Split('*')[0]; var M = Parsed.State.ApertureMacros[name]; M.Written = true; var gerb = M.BuildGerber(CoordinateFormat, AngleInDeg).Split('\n'); foreach (var l in gerb) { if (l.Trim().Length > 0) { outlines.Add(l.Trim()); } } // outlines.Add(lines[i]); // if (lines[i][lines[i].Length - 1] != '%') /// { // i++; while (lines[i][lines[i].Length - 1] != '%') { // outlines.Add(lines[i]); i++; } // outlines.Add(lines[i]); // } } else if (lines[i].Length > 3 && lines[i].Substring(0, 3) == "%AD") { GCodeCommand GCC = new GCodeCommand(); GCC.Decode(lines[i], CoordinateFormat); if (GCC.numbercommands.Count < 1) { Console.WriteLine("Skipping bad aperture definition: {0}", lines[i]); } else { int ATID = (int)GCC.numbercommands[0]; var Aperture = Parsed.State.Apertures[ATID]; if (Gerber.ShowProgress) { Console.WriteLine("found " + Aperture.ToString()); } string gerb = Aperture.BuildGerber(CoordinateFormat, AngleInDeg); if ((Aperture.ShapeType == GerberApertureShape.Compound || Aperture.ShapeType == GerberApertureShape.Macro) && Parsed.State.ApertureMacros[Aperture.MacroName].Written == false) { Console.WriteLine("Macro type defined - skipping"); } else { outlines.Add(gerb); } // outlines.Add(lines[i]); if (lines[i][lines[i].Length - 1] != '%') { i++; while (lines[i] != "%") { // outlines.Add(lines[i]); i++; } // outlines.Add(lines[i]); } } } else { bool PureD = false; if (GS.Has("G")) { int GCode = (int)GS.Get("G"); switch (GCode) { case 4: DumpToOutput = true; break; case 90: CoordinateFormat.Relativemode = false; break; case 91: CoordinateFormat.Relativemode = true; break; case 71: CoordinateFormat.Multiplier = 1.0d; break; case 70: CoordinateFormat.Multiplier = 25.4d; break; } } if (DumpToOutput) { outlines.Add(lines[i]); if (lines[i].Contains("LNData")) { Console.WriteLine(" heh"); } if (lines[i][0] == '%') { int starti = i; if (lines[i].Length == 1) { i++; } while (lines[i][lines[i].Length - 1] != '%') { if (i > starti) { outlines.Add(lines[i]); } i++; } if (i > starti) { outlines.Add(lines[i]); } } } else { bool translate = true; if (CoordinateFormat.Relativemode) { translate = false; } if (GS.Has("X") == false && GS.Has("Y") == false && (GS.Has("D") && GS.Get("D") < 10)) { PureD = true; int Dcode = (int)GS.Get("D"); if (Dcode == 1 || Dcode == 3) { if (moveswritten == 0) { WriteMove = true; } } moveswritten++; Console.WriteLine(" Pure D Code: {0}", lines[i]); } else if (GS.Has("X") || GS.Has("Y") || (GS.Has("D") && GS.Get("D") < 10)) { int Dcode = (int)GS.Get("D"); if (Dcode == 1 || Dcode == 3) { if (moveswritten == 0) { WriteMove = true; } } moveswritten++; double X = LastX; if (GS.Has("X")) { X = GS.Get("X"); } double Y = LastY; if (GS.Has("Y")) { Y = GS.Get("Y"); } LastX = X; LastY = Y; GetTransformedCoord(DX, DY, DXp, DYp, Angle, CA, SA, CoordinateFormat, translate, ref X, ref Y); if ((GS.Has("I") || GS.Has("J")) && Angle != 0) { // int g = (int)GS.Get("G"); // if (g == 2 || g == 3) { double I = 0; double J = 0; bool arc = false; if (GS.Has("I")) { I = GS.Get("I"); arc = true; } ; if (GS.Has("J")) { J = GS.Get("J"); arc = true; } ; if (arc) { double nJ = J * CA + I * SA; double nI = -J * SA + I * CA; I = nI; J = nJ; // GS.Set("I", Math.Abs(I)); // GS.Set("J", Math.Abs(J)); GS.Set("I", I); GS.Set("J", J); } } } GS.Set("X", X); GS.Set("Y", Y); } if (WriteMove) { GerberSplitter GS2 = new GerberSplitter(); GS2.Set("D", 2); double X0 = 0; double Y0 = 0; GetTransformedCoord(DX, DY, DXp, DYp, Angle, CA, SA, CoordinateFormat, translate, ref X0, ref Y0); GS2.Set("X", X0); GS2.Set("Y", Y0); WriteMove = false; outlines.Add(GS2.Rebuild(CoordinateFormat)); } outlines.Add(GS.Rebuild(CoordinateFormat)); if (PureD) { Console.WriteLine("pureD"); } } } } try { List <String> PostProcLines = new List <string>(); foreach (var a in outlines) { if (a == "%") { PostProcLines[PostProcLines.Count - 1] += "%"; } else { PostProcLines.Add(a); } } Gerber.WriteAllLines(destfile, PolyLineSet.SanitizeInputLines(PostProcLines)); } catch (Exception E) { Console.WriteLine(E.Message); } }
public void Build(ParsedGerber TheLines, List <Tool> Tools, Tool DefaultTool) { List <PolyLine> Shapes = new List <PolyLine>(); foreach (var A in TheLines.Shapes) { Shapes.Add(A); } Reset(); double lastx = 0; double lasty = 0; double lastdx = 0; double lastdy = 0; // add a final point to make the bezier curve neatly downward at the last PolyLine. This gets added AFTER optimize to ensure it is actually the last shape. PolyLine EndPoint = new PolyLine(-3); EndPoint.Add(0, 0); EndPoint.Draw = false; Shapes.Add(EndPoint); Reset(); // go to homing point Up(DefaultTool, true); Move(0, 0); // loop through all the polylines, add bezier-curves between them for continuous movements past the edges. This should also be done at line-kinks.. but not yet foreach (var a in Shapes) { double newx = a.Vertices[0].X; double newy = a.Vertices[0].Y; if (bezier) { double newdx = lastdx; double newdy = lastdy; if (a.Count() > 1) { newdx = a.Vertices[1].X - a.Vertices[0].X; newdy = a.Vertices[1].Y - a.Vertices[0].Y; } PointD D1 = Helpers.Normalize(new PointD((float)lastdx, (float)lastdy)); PointD D2 = Helpers.Normalize(new PointD((float)newdx, (float)newdy)); PointD AM = new PointD((float)lastx - D1.X * 10, (float)lasty - D1.Y * 10);; PointD A = new PointD((float)lastx, (float)lasty); PointD B = new PointD((float)lastx + D1.X * 7, (float)lasty + D1.Y * 7); PointD C = new PointD((float)newx - D2.X * 7, (float)newy - D2.Y * 7); PointD D = new PointD((float)newx, (float)newy); PointD DP = new PointD((float)newx + D2.X * 10, (float)newy + D2.Y * 10); for (int i = 0; i < 10; i++) { PointD T = Helpers.BezierPoint(A, B, C, D, i * 0.1f); Move(T.X, T.Y); } } Move(newx, newy); bool force = false; if (a.Count() == 1) { force = true; } if (a.Draw == true) { Down(null, force); } lastx = newx; lasty = newy; for (int i = 1; i < a.Count(); i++) { newx = a.Vertices[i].X; newy = a.Vertices[i].Y; Move(newx, newy); lastdx = newx - lastx; lastdy = newy - lasty; lastx = newx; lasty = newy; } if (a.Draw == true) { Up(null, force); } } // move back to homing point Move(0, 0); if (Gerber.ShowProgress) { Console.WriteLine("command count: {0}", CommandList.Count); } }
private static Dictionary <string, Tuple <GerberApertureType, List <PointD> > > FillOverlapList(ProgressLog log, Dictionary <string, Tuple <GerberApertureType, List <PointD> > > overlapList, ParsedGerber subtr, string subtractfile) { log.PushActivity("Filling overlap list coords"); List <String> lines = new List <string>(); List <String> outlines = new List <string>(); int CurrentAperture = 10; bool WriteMove = false; int moveswritten = 0; using (StreamReader sr = new StreamReader(subtractfile)) { while (sr.EndOfStream == false) { String line = sr.ReadLine(); if (line.Length > 0) { lines.Add(line); } } } lines = PolyLineSet.SanitizeInputLines(lines); //ParsedGerber Parsed = PolyLineSet.ParseGerber274x(log, lines, true, false, new GerberParserState() { GenerateGeometry = false }); GerberNumberFormat CoordinateFormat = new GerberNumberFormat(); CoordinateFormat.SetImperialMode(); // CoordinateFormat = Parsed.State.CoordinateFormat; int cur = 0; bool formatparsed = false; while (cur < lines.Count && formatparsed == false) { if (lines[cur].Length >= 2 && lines[cur].Substring(0, 2) == "%F") { CoordinateFormat.Parse(lines[cur]); formatparsed = true; } cur++; } // double coordmultiplier = 1.0; double LastX = 0; double LastY = 0; for (int i = 0; i < lines.Count; i++) { GerberSplitter GS = new GerberSplitter(); string FinalLine = lines[i].Replace("%", "").Replace("*", "").Trim(); bool DumpToOutput = false; bool metaopen = false; if (lines[i][0] == '%') { DumpToOutput = true; metaopen = true; } else { GS.Split(lines[i], CoordinateFormat); } switch (FinalLine) { case "G71": CoordinateFormat.SetMetricMode(); break; case "G70": CoordinateFormat.SetImperialMode(); break; case "MOIN": { CoordinateFormat.SetImperialMode(); //CoordinateFormat.Multiplier = 25.4f; } break; case "MOMM": { CoordinateFormat.SetMetricMode(); //CoordinateFormat.Multiplier = 1.0f; } break; } if (lines[i].Length > 3 && lines[i].Substring(0, 3) == "%AM") { string name = lines[i].Substring(3).Split('*')[0]; var M = subtr.State.ApertureMacros[name]; M.Written = true; var gerb = M.BuildGerber(CoordinateFormat, 0, "").Split('\n'); foreach (var l in gerb) { if (l.Trim().Length > 0) { outlines.Add(l.Trim()); } } // outlines.Add(lines[i]); // if (lines[i][lines[i].Length - 1] != '%') /// { // i++; while (lines[i][lines[i].Length - 1] != '%') { // outlines.Add(lines[i]); i++; } // outlines.Add(lines[i]); // } } else if (lines[i].Length > 3 && lines[i].Substring(0, 3) == "%AD") { GCodeCommand GCC = new GCodeCommand(); GCC.Decode(lines[i], CoordinateFormat); if (GCC.numbercommands.Count < 1) { log.AddString(String.Format("Skipping bad aperture definition: {0}", lines[i])); } else { int ATID = (int)GCC.numbercommands[0]; var Aperture = subtr.State.Apertures[ATID]; if (Gerber.ShowProgress) { log.AddString(String.Format("found " + Aperture.ToString())); } string gerb = Aperture.BuildGerber(CoordinateFormat, "", 0); if ((Aperture.ShapeType == GerberApertureShape.Compound || Aperture.ShapeType == GerberApertureShape.Macro) && subtr.State.ApertureMacros[Aperture.MacroName].Written == false) { log.AddString(String.Format("Macro type defined - skipping")); } else { outlines.Add(gerb); } // outlines.Add(lines[i]); if (lines[i][lines[i].Length - 1] != '%') { i++; while (lines[i] != "%") { // outlines.Add(lines[i]); i++; } // outlines.Add(lines[i]); } } } else { bool PureD = false; if (GS.Has("G")) { int GCode = (int)GS.Get("G"); switch (GCode) { case 4: DumpToOutput = true; break; case 90: CoordinateFormat.Relativemode = false; break; case 91: CoordinateFormat.Relativemode = true; break; case 71: CoordinateFormat.Multiplier = 1.0d; break; case 70: CoordinateFormat.Multiplier = 25.4d; break; } } if (DumpToOutput) { outlines.Add(lines[i]); if (lines[i].Contains("LNData")) { log.AddString(String.Format(" heh")); } if (lines[i][0] == '%') { int starti = i; if (lines[i].Length == 1) { i++; } while (lines[i][lines[i].Length - 1] != '%') { if (i > starti) { outlines.Add(lines[i]); } i++; } if (i > starti) { outlines.Add(lines[i]); } } } else { bool translate = true; if (CoordinateFormat.Relativemode) { translate = false; } if (GS.Has("X") == false && GS.Has("Y") == false && (GS.Has("D") && GS.Get("D") < 10)) { PureD = true; int Dcode = (int)GS.Get("D"); if (Dcode == 1 || Dcode == 3) { if (moveswritten == 0) { WriteMove = true; } } moveswritten++; //log.AddString(String.Format("Pure D Code: {0}", lines[i])); } else if (GS.Has("X") || GS.Has("Y") || (GS.Has("D") && GS.Get("D") < 10)) { int Dcode = (int)GS.Get("D"); if (Dcode == 1 || Dcode == 3) { if (moveswritten == 0) { WriteMove = true; } } moveswritten++; double X = LastX; if (GS.Has("X")) { X = GS.Get("X"); } double Y = LastY; if (GS.Has("Y")) { Y = GS.Get("Y"); } LastX = X; LastY = Y; GS.Set("X", X); GS.Set("Y", Y); } if (GS.Get("D") >= 10) { CurrentAperture = (int)GS.Get("D"); // Select Aperture; } string hash = subtr.State.Apertures[CurrentAperture].GetApertureHash(); if (overlapList.ContainsKey(hash)) { overlapList[hash].Item2.Add(new PointD(LastX, LastY)); } if (WriteMove) { GerberSplitter GS2 = new GerberSplitter(); GS2.Set("D", 2); double X0 = 0; double Y0 = 0; if (overlapList.ContainsKey(hash)) { overlapList[hash].Item2.Add(new PointD(0, 0)); } //GetTransformedCoord(DX, DY, DXp, DYp, Angle, CA, SA, CoordinateFormat, translate, ref X0, ref Y0); GS2.Set("X", X0); GS2.Set("Y", Y0); WriteMove = false; // outlines.Add(GS2.Rebuild(CoordinateFormat)); } // outlines.Add(GS.Rebuild(CoordinateFormat)); if (PureD) { //log.AddString(String.Format("pureD")); } } } } try { // List<String> PostProcLines = new List<string>(); // foreach (var a in outlines) // { // if (a == "%") // { // PostProcLines[PostProcLines.Count - 1] += "%"; // } // else // { // PostProcLines.Add(a); // } // } // Gerber.WriteAllLines(outputfile, PolyLineSet.SanitizeInputLines(PostProcLines)); } catch (Exception E) { log.AddString(String.Format(E.Message)); } log.PopActivity(); return(overlapList); }
private void CreateStuff(double DPI, ParsedGerber PLS, string OutlineFile, string SilkFile, string BitmapFile, bool Flipped, bool Invert, string CopperFile, string SoldermaskFile) { if (BitmapFile.Length == 0) { return; } if (System.IO.File.Exists(BitmapFile) == false) { MessageBox.Show("Bitmap not found or not specified... aborting", "Aborting..", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (System.IO.File.Exists(OutlineFile) == false) { MessageBox.Show("Outline not found or not specified... aborting", "Aborting..", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } Bitmap B = (Bitmap)Image.FromFile(BitmapFile); if (Flipped) { B.RotateFlip(RotateFlipType.RotateNoneFlipX); } B.RotateFlip(RotateFlipType.RotateNoneFlipY); Bitmap B2 = null; if (File.Exists(CopperFile)) { B2 = (Bitmap)Image.FromFile(CopperFile); if (Flipped) { B2.RotateFlip(RotateFlipType.RotateNoneFlipX); } B2.RotateFlip(RotateFlipType.RotateNoneFlipY); } Bitmap B3 = null; if (File.Exists(SoldermaskFile)) { B3 = (Bitmap)Image.FromFile(SoldermaskFile); if (Flipped) { B3.RotateFlip(RotateFlipType.RotateNoneFlipX); } B3.RotateFlip(RotateFlipType.RotateNoneFlipY); } bool UseSilkFile = false; if (System.IO.File.Exists(SilkFile)) { UseSilkFile = true; } string output = OutputFolderBox.Text; string OutSilk = BitmapFile + ".SILK"; string OutCopper = BitmapFile + ".GTL"; string OutSoldermask = BitmapFile + ".GTS"; if (B != null) { GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(OutSilk, PLS, DPI, B, Invert ? -128 : 128); } if (B2 != null) { GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(OutCopper, PLS, DPI, B2, Invert ? -128 : 128); } if (B3 != null) { GerberLibrary.ArtWork.Functions.WriteBitmapToGerber(OutSoldermask, PLS, DPI, B3, Invert ? -128 : 128); } if (UseSilkFile) { // merge things! GerberLibrary.GerberMerger.Merge(OutSilk, SilkFile, Path.Combine(output, Path.GetFileName(SilkFile)), new StandardConsoleLog()); } else { if (!Directory.Exists(Path.Combine(output, Path.GetFileName(SilkFile)))) { File.Copy(OutSilk, Path.Combine(output, Path.GetFileName(SilkFile)), true); } else { } } }