private void apictureBox1_Paint(object sender, PaintEventArgs e) { var G2 = e.Graphics; PolyLineSet.Bounds Bounds = new PolyLineSet.Bounds(); foreach (var a in Document.Gerbers.OrderBy(x => x.sortindex)) { Bounds.AddBox(a.File.BoundingBox); } if (Cache == null) { Cache = new Bitmap(Width, Height); Graphics G = Graphics.FromImage(Cache); GerberImageCreator.ApplyAASettings(G); GraphicsGraphicsInterface GGI = new GraphicsGraphicsInterface(G); GGI.Clear(Document.Colors.BackgroundColor); // DrawGerbersToGraphicsInterface(Bounds, GGI); } G2.DrawImage(Cache, 0, 0); GerberImageCreator.ApplyAASettings(G2); { if (Document.CrossHairActive) { if (Document.Gerbers.Count > 0) { float S = GetScaleAndBuildTransform(G2, Bounds); Color DimensionColor = Color.FromArgb(255, 255, 200); Pen P = new Pen(DimensionColor, 1.0f / S); P.DashPattern = new float[2] { 2, 2 }; G2.DrawLine(P, (float)Bounds.TopLeft.X - 1000, Document.MouseY, (float)Bounds.BottomRight.X + 1000, Document.MouseY); G2.DrawLine(P, (float)Document.MouseX, (float)Bounds.TopLeft.Y - 1000, (float)Document.MouseX, (float)Bounds.BottomRight.Y + 1000); DrawLabel(G2, String.Format("{0:N2}", Document.MouseX - Bounds.TopLeft.X), S, 12, DimensionColor, 5, 0, (float)Document.MouseX, (float)Bounds.TopLeft.Y, DisplaySide == BoardSide.Bottom); DrawLabel(G2, String.Format("{0:N2}", Document.MouseY - Bounds.TopLeft.Y), S, 12, DimensionColor, 0, -14, (float)Bounds.TopLeft.X, (float)Document.MouseY, DisplaySide == BoardSide.Bottom); //DrawUpsideDown(G2, String.Format("{0:N2}", Document.MouseX), S, 12, Color.Yellow, 5 / S + (float)Document.MouseX, (float)Bounds.TopLeft.Y); } } } }
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); }
public string MinimalDXFSave(string outputfile, double offset = 3.0, double holediameter = 3.2) { if (Directory.Exists(outputfile)) { outputfile = Path.Combine(outputfile, "SickOfBeige"); } PolyLine Biggest = null; double BiggestArea = 0; List <PointD> Holes = new List <PointD>(); var holeradius = holediameter / 2.0; double Circumference = 2 * Math.PI * holeradius; foreach (var a in PLSs.Where(x => x.Layer == BoardLayer.Outline)) { foreach (var b in a.OutlineShapes) { var A = b.toPolygon(); double LRatio = (b.OutlineLength() / Circumference); double Lperc = Math.Abs(LRatio - 1); if (Lperc < 0.1) { if (b.Vertices.Count > 5) { var C = b.GetCentroid(); bool round = true; foreach (var v in b.Vertices) { var L = (C - v).Length(); if (Math.Abs(L - holeradius) > 0.2) { // not very round! round = false; } } if (round) { Console.WriteLine("Hole detected in outline:{0} {1} {2} {3} {4} {5}", a.Layer, a.Side, C, LRatio, Lperc, b.Vertices.Count); Holes.Add(C); } } // might be hole! } var Area = Clipper.Area(A); if (Area > BiggestArea) { Biggest = b; BiggestArea = Area; } } } Polygons Offsetted = new Polygons(); List <String> Lines = new List <string>(); Lines.Add("0"); Lines.Add("SECTION"); Lines.Add("2 "); Lines.Add("ENTITIES"); if (Biggest != null) { Polygons clips = new Polygons(); clips.Add(Biggest.toPolygon()); Offsetted = Clipper.OffsetPolygons(clips, offset * 100000.0f, JoinType.jtRound); foreach (var poly in Offsetted) { PolyLine P = new PolyLine(PolyLine.PolyIDs.Temp); P.fromPolygon(poly); for (int i = 0; i < P.Vertices.Count; i++) { var V1 = P.Vertices[i]; var V2 = P.Vertices[(i + 1) % P.Vertices.Count]; Lines.Add("0"); Lines.Add("LINE"); Lines.Add("8"); Lines.Add("Outline"); Lines.Add("10"); Lines.Add(V1.X.ToString().Replace(',', '.')); Lines.Add("20"); Lines.Add(V1.Y.ToString().Replace(',', '.')); Lines.Add("11"); Lines.Add(V2.X.ToString().Replace(',', '.')); Lines.Add("21"); Lines.Add(V2.Y.ToString().Replace(',', '.')); } } } else { Errors.Add("No longest outline found - not generating offset curve"); } foreach (var a in Excellons) { foreach (var t in a.Tools) { var R = t.Value.Radius; if (Math.Abs(R * 2 - holediameter) < 0.05) { foreach (var h in t.Value.Drills) { Holes.Add(h); } } } } foreach (var a in Holes) { for (int i = 0; i < 40; i++) { double P = i * Math.PI * 2.0 / 40.0; double P2 = (i + 1) * Math.PI * 2.0 / 40.0; var C1 = Math.Cos(P) * holeradius; var C2 = Math.Cos(P2) * holeradius; var S1 = Math.Sin(P) * holeradius; var S2 = Math.Sin(P2) * holeradius; double x1 = a.X + C1; double y1 = a.Y + S1; double x2 = a.X + C2; double y2 = a.Y + S2; Lines.Add("0"); Lines.Add("LINE"); Lines.Add("8"); Lines.Add("Holes"); Lines.Add("10"); Lines.Add(x1.ToString().Replace(',', '.')); Lines.Add("20"); Lines.Add(y1.ToString().Replace(',', '.')); Lines.Add("11"); Lines.Add(x2.ToString().Replace(',', '.')); Lines.Add("21"); Lines.Add(y2.ToString().Replace(',', '.')); } } Lines.Add("0"); Lines.Add("ENDSEC"); Lines.Add("0"); Lines.Add("EOF"); File.WriteAllLines(outputfile + ".dxf", Lines); float scalefac = 10; Console.WriteLine("Report: {0} holes created in case ({1} spacers and {1} screws needed!)", Holes.Count, Holes.Count * 2); { var BB = new GerberLibrary.Bounds(); BB.AddPolygons(Offsetted); BB.AddPolyLine(Biggest); Bitmap B = new Bitmap((int)((BB.Width()) * scalefac) + 6, (int)((BB.Height()) * scalefac) + 6); Graphics G = Graphics.FromImage(B); G.Clear(Color.Transparent); G.Clear(Color.White); G.TranslateTransform(3, 3); G.ScaleTransform(scalefac, scalefac); G.TranslateTransform((float)-(BB.TopLeft.X), (float)-(BB.TopLeft.Y)); Pen pen = new Pen(Color.Black, 0.1f); Pen pen2 = new Pen(Color.FromArgb(160, 160, 160), 0.1f); pen2.DashPattern = new float[2] { 2, 2 }; GerberImageCreator.ApplyAASettings(G); RectangleF R = new RectangleF(0, 0, (float)holediameter, (float)holediameter); foreach (var a in Holes) { R.X = (float)a.X - (float)holeradius; R.Y = (float)a.Y - (float)holeradius; G.DrawEllipse(pen, R); } foreach (var poly in Offsetted) { PolyLine Pl = new PolyLine(PolyLine.PolyIDs.Temp); Pl.fromPolygon(poly); var Points = new List <PointF>(Pl.Vertices.Count); for (int i = 0; i < Pl.Vertices.Count; i++) { Points.Add(Pl.Vertices[i].ToF()); } Points.Add(Pl.Vertices[0].ToF()); G.DrawLines(pen, Points.ToArray()); } { PolyLine Pl = Biggest; var Points = new List <PointF>(Pl.Vertices.Count); for (int i = 0; i < Pl.Vertices.Count; i++) { Points.Add(Pl.Vertices[i].ToF()); } Points.Add(Pl.Vertices[0].ToF()); G.DrawLines(pen2, Points.ToArray()); } var ImagePNG = outputfile + ".png"; B.Save(ImagePNG); return(ImagePNG); } }