Example #1
0
        private void exportBoardImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedInstance == null)
            {
                return;
            }
            if (SelectedInstance.GetType() == typeof(GerberInstance))
            {
                string path = (SelectedInstance as GerberInstance).GerberPath;
                try
                {
                    System.Windows.Forms.SaveFileDialog OFD = new System.Windows.Forms.SaveFileDialog();

                    OFD.DefaultExt = "";
                    if (OFD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    Console.WriteLine("path selected: {0}", path);

                    GerberImageCreator GIC = new GerberImageCreator();

                    foreach (var a in Directory.GetFiles(path, "*.*"))
                    {
                        GIC.AddBoardToSet(a, new StandardConsoleLog());
                    }

                    GIC.WriteImageFiles(OFD.FileName);
                }
                catch (Exception)
                {
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Count() >= 1)
            {
                bool backside = false;
                if (args.Count() > 1)
                {
                    backside = true;
                }
                if (System.IO.Directory.Exists(args[0]))
                {
                    var F = System.IO.Directory.GetFiles(args[0], "*.gko");
                    foreach (var a in F)
                    {
                        string basename = a.Substring(0, a.Length - 4);
                        string front    = basename + "Silk.png";
                        string back     = basename + "BottomSilk.png";
                        if (System.IO.File.Exists(front))
                        {
                            ConvertFile(a, false);
                        }
                        if (System.IO.File.Exists(back))
                        {
                            ConvertFile(a, true);
                        }

                        GerberImageCreator GIC = new GerberImageCreator();
                        GIC.AddBoardsToSet(System.IO.Directory.GetFiles(basename).ToList());
                        GIC.WriteImageFiles(basename + "/render");
                    }
                }
            }
        }
Example #3
0
        private void LoadStuff()
        {
            if (stock.Length > 0 && File.Exists(stock))
            {
                try
                {
                    log.PushActivity("Loading stock");
                    StockDoc = StockDocument.Load(stock);
                    if (StockDoc == null)
                    {
                        StockDoc = new StockDocument();
                    }
                }
                catch (Exception)
                {
                    StockDoc = new StockDocument();
                }
                log.PopActivity();
            }
            else
            {
                StockDoc = new StockDocument();
            }
            log.PushActivity("Loading document");
            B = new BOM();

            if (bom.Length > 0 && pnp.Length > 0)
            {
                String DirBaseName = Path.GetFileNameWithoutExtension(pnp);
                log.PushActivity("Processing " + DirBaseName);

                log.PushActivity("Loading BOM");
                log.AddString(String.Format("Loading BOM! {0},{1}", Path.GetFileName(bom), Path.GetFileName(pnp)));
                B.LoadJLC(bom, pnp);
                log.PopActivity();

                if (gerberzip != null && File.Exists(gerberzip))
                {
                    Set = LoadGerberZip(gerberzip, log);
                }
                else
                {
                    Set = new GerberImageCreator();
                }
                Box = Set.BoundingBox;

                BuildPostBom();

                log.PopActivity();
            }
            else
            {
                log.AddString(String.Format("pnp and bom need to be valid! bom:{0} pnp:{1}", bom, pnp));
            }


            loaded = true;
            log.AddString("Done!");
            log.PopActivity();
        }
Example #4
0
        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);
            }
        }
Example #5
0
        private static void CreateImageForSingleFile11(double dpi, List <string> RestList)
        {
            string             pcbcolor           = "green";
            string             silkcolor          = "white";
            string             tracecolor         = "auto";
            string             coppercolor        = "gold";
            GerberImageCreator GIC                = new GerberImageCreator();
            string             TargetFileBaseName = "";

            if (RestList.Count() >= 1)
            {
                TargetFileBaseName = RestList[0];
            }

            List <String> FileList = new List <string>();

            foreach (var a in RestList)
            {
                if (Directory.Exists(a))
                {
                    FileList.AddRange(Directory.GetFiles(a, "*.*"));
                }
                else
                {
                    if (File.Exists(a))
                    {
                        FileList.Add(a);
                    }
                }
            }

            for (int i = 0; i < FileList.Count; i++)
            {
                if (FileList[i][FileList[i].Count() - 1] == '\\')
                {
                    FileList[i] = Path.GetDirectoryName(FileList[i]);
                }
            }
            GIC.AddBoardsToSet(FileList, new StandardConsoleLog(), true, false, 0.8);
            BoardRenderColorSet colors = new BoardRenderColorSet();

            if (pcbcolor == "")
            {
                pcbcolor = "black";
            }
            colors.SetupColors(pcbcolor, silkcolor, tracecolor, coppercolor);


            GIC.SetColors(colors);
            //GIC.WriteImageFiles(TargetFileBaseName, 96, false, xray, normal, new StandardConsoleLog());
            GIC.DrawAllFilesForPrintPcb(TargetFileBaseName, dpi);
            Console.WriteLine("Done writing {0}", TargetFileBaseName);
            GIC.Dispose();
            GC.Collect();
        }
Example #6
0
        public void BuildPostBom()
        {
            BPost = new BOM();
            BOMNumberSet s = new BOMNumberSet();

            switch (FlipBoard)
            {
            case FlipMode.NoFlip:
                FixOffset = new PointD(Set.BoundingBox.TopLeft.X, Set.BoundingBox.TopLeft.Y);
                break;

            case FlipMode.FlipDiagonal:
                FixOffset = new PointD(Set.BoundingBox.TopLeft.X, Set.BoundingBox.TopLeft.Y);
                break;

            case FlipMode.FlipHorizontal:
                FixOffset = new PointD(Set.BoundingBox.TopLeft.X, Set.BoundingBox.TopLeft.Y);
                break;
            }

            BPost.MergeBOM(B, s, 0, 0, -FixOffset.X, -FixOffset.Y, 0);

            FixSet = new GerberImageCreator();
            FixSet.CopyFrom(Set);

            switch (FlipBoard)
            {
            case FlipMode.NoFlip:
                FixSet.SetBottomLeftToZero();
                break;

            case FlipMode.FlipDiagonal:
                FixSet.SetBottomRightToZero();
                FixSet.FlipXY();
                FixSet.Translate(0, FixSet.BoundingBox.Height());
                BPost.SwapXY();
                BPost.FlipSides();
                //                    BPost.Translate(0, FixSet.BoundingBox.Height());
                break;

            case FlipMode.FlipHorizontal:
                FixSet.SetBottomRightToZero();
                FixSet.FlipX();
                BPost.FlipSides();
                BPost.FlipX();
                BPost.Translate(FixSet.BoundingBox.Width(), 0);
                break;
            }
            BPost.FixupAngles(StockDoc);
            BPost.WriteJLCPnpFile(B.OriginalBasefolder, B.OriginalPnpName + "_rotated", true);
            BPost.WriteQuantPnPFiles(B.OriginalBasefolder, B.OriginalPnpName + "_QUANTSET");
            BPost.WriteQuantBOMFile(B.OriginalBasefolder, B.OriginalPnpName + "_QUANTSET");

            BPost.WriteRefDesGerber(Path.Combine(B.OriginalBasefolder, B.OriginalPnpName + "_refdes.gbr"));
        }
Example #7
0
        private static GerberImageCreator LoadGerberZip(string v, ProgressLog log)
        {
            log.PushActivity("LoadingGerbers");
            GerberImageCreator GIC   = new GerberImageCreator();
            List <String>      Files = new List <string>();

            Files.Add(v);
            GIC.AddBoardsToSet(Files, log, true, false);

            // log.AddString(GIC.GetOutlineBoundingBox().ToString());
            log.PopActivity();
            return(GIC);
        }
Example #8
0
        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);
                    }
                }
            }
        }
Example #9
0
        static void Main(string[] args)
        {
            if (args.Count() < 2)
            {
                Console.WriteLine("usage: ");
                Console.WriteLine("GerberMover <inputfile> <outputfile> <X> <Y> <CX> <CY> <Angle>");
                Console.WriteLine("<inputfile>: file to load - this can be either gerber or excellon file");
                Console.WriteLine("<outputfile>: file to write - this will be the same format as the input file");
                Console.WriteLine("<X> <Y>: amount of millimeter to move the input - this will be the offset of the rotation center point too");
                Console.WriteLine("<CX> <CY> <Angle>: center of rotation and angle. Millimeters and degrees");
                Console.WriteLine("Only <inputfile> and <outputfile> are needed, the rest is optional.");

                return;
            }

            if (File.Exists(args[0]) == false)
            {
                if (Directory.Exists(args[0]))
                {
                    if (Directory.Exists(args[1]) == false)
                    {
                        Directory.CreateDirectory(args[1]);
                    }
                    List <string> Files = new List <string>();
                    foreach (var a in Directory.GetFiles(args[0]))
                    {
                        var newfile = Path.Combine(args[1], Path.GetFileName(a));
                        RotateFile(a, newfile, args);
                        Files.Add(newfile);
                    }
                    GerberImageCreator GIC = new GerberImageCreator();
                    GIC.AddBoardsToSet(Files);
                    GIC.WriteImageFiles(args[1] + "\\render");
                }
                else
                {
                }
                Console.WriteLine("File {0} not found!", args[0]);
            }
            else
            {
                RotateFile(args[0], args[1], args);
            }

            Console.WriteLine("Press any key to continue..");
            Console.ReadKey();
        }
Example #10
0
        private void LoadStuff()
        {
            log.PushActivity("Loading document");
            B = new BOM();

            if (bom.Length > 0 && pnp.Length > 0)
            {
                String DirBaseName = Path.GetFileNameWithoutExtension(pnp);
                log.PushActivity("Processing " + DirBaseName);

                log.PushActivity("Loading BOM");
                log.AddString(String.Format("Loading BOM! {0},{1}", Path.GetFileName(bom), Path.GetFileName(pnp)));
                B.LoadJLC(bom, pnp);
                BuildPostBom();
                log.PopActivity();

                if (gerberzip != null && File.Exists(gerberzip))
                {
                    Set = LoadGerberZip(gerberzip, log);
                }
                else
                {
                    Set = new GerberImageCreator();
                    Set.AddBoardToSet(silk, log);
                    Set.AddBoardToSet(outline, log);
                }
                Box = Set.BoundingBox;


                //            string OutputGerberName = fixedoutputfolder + "\\" + Path.GetFileName(boardfile);
                //B.WriteRefDesGerber(OutputGerberName+"ORIGPLACEMENT");
                FixOffset = new PointD(-Set.BoundingBox.TopLeft.X, -Set.BoundingBox.BottomRight.Y);
                //                B.WriteRefDesGerber(OutputGerberName);
                //              B.WriteJLCCSV(fixedoutputfolder, Path.GetFileName(filebase), true);

                log.PopActivity();
            }
            else
            {
                log.AddString(String.Format("pnp and bom need to be valid! bom:{0} pnp:{1}", bom, pnp));
            }


            loaded = true;
            log.AddString("Done!");
            log.PopActivity();
        }
Example #11
0
        private void exportBoardImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = "";

            if (treeView1.SelectedNode.GetType() == typeof(GerberFileNode))
            {
                path = (treeView1.SelectedNode as GerberFileNode).pPath;
            }
            if (treeView1.SelectedNode.GetType() == typeof(InstanceTreeNode))
            {
                if ((treeView1.SelectedNode as InstanceTreeNode).TargetInstance.GetType() == typeof(GerberInstance))
                {
                    path = ((treeView1.SelectedNode as InstanceTreeNode).TargetInstance as GerberInstance).GerberPath;
                }
            }
            if (path.Length > 0)
            {
                try
                {
                    System.Windows.Forms.SaveFileDialog OFD = new System.Windows.Forms.SaveFileDialog();

                    OFD.DefaultExt = "";
                    if (OFD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    Console.WriteLine("path selected: {0}", path);

                    GerberImageCreator GIC = new GerberImageCreator();
                    foreach (var a in Directory.GetFiles(path, "*.*"))
                    {
                    }


                    foreach (var a in Directory.GetFiles(path, "*.*"))
                    {
                        GIC.AddBoardToSet(a);
                    }

                    GIC.WriteImageFiles(OFD.FileName, showimage: Gerber.DirectlyShowGeneratedBoardImages);
                }
                catch (Exception)
                { }
            }
        }
Example #12
0
        static void Main(string[] args)
        {
            if (args.Count() < 3)
            {
                Console.WriteLine("Usage: GerberClipper.exe <outlinegerber> <subject> <outputfile>");
                return;
            }

            string outline    = args[0];
            string infile     = args[1];
            string outputfile = args[2];

            GerberImageCreator GIC = new GerberImageCreator();

            GIC.AddBoardsToSet(new List <string>()
            {
                outline, infile
            }, new StandardConsoleLog(), true);
            GIC.ClipBoard(infile, outputfile, new StandardConsoleLog());
        }
Example #13
0
        static void Main(string[] args)
        {
            if (args.Count() == 1 && File.Exists(args[0]))
            {
                //  Gerber.WriteSanitized = true;
                Gerber.ExtremelyVerbose = false;
                //Gerber.Verbose = true;
                Gerber.WaitForKey   = true;
                Gerber.ShowProgress = true;

                CreateImageForSingleFile(args[0], Color.Black, Color.White);
                if (Gerber.WaitForKey)
                {
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                }
                return;
            }

            GerberImageCreator GIC = new GerberImageCreator();
            string             TargetFileBaseName = "";

            if (args.Count() >= 1)
            {
                TargetFileBaseName = args[0];
            }
            List <String> FileList = new List <string>();

            if (args.Count() <= 1 || Directory.Exists(args[1]))
            {
                foreach (var a in Directory.GetFiles(args[1]))
                {
                    try{
                        //   Console.WriteLine("Building layer image for: {0}", Path.GetFileName(a));
                        //  CreateImageForSingleFile(a);
                    }
                    catch (Exception E) {
                        Console.WriteLine("Error while writing image for {0}: {1}", Path.GetFileName(a), E.Message);
                    };
                }

                if (args.Count() == 0)
                {
                    System.Windows.Forms.SaveFileDialog OFD = new System.Windows.Forms.SaveFileDialog();
                    OFD.DefaultExt = "";
                    if (OFD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    TargetFileBaseName = OFD.FileName;
                }

                string foldername = "";

                if (args.Count() < 2)
                {
                    FolderBrowserDialog FBD = new FolderBrowserDialog();
                    if (FBD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    foldername = FBD.SelectedPath;
                }
                else
                {
                    foldername = args[1];
                }

                FileList.AddRange(Directory.GetFiles(foldername, "*.*"));
            }
            else
            {
                FileList.AddRange(args.Skip(1));
            }

            for (int i = 0; i < FileList.Count; i++)
            {
                if (FileList[i][FileList[i].Count() - 1] == '\\')
                {
                    FileList[i] = Path.GetDirectoryName(FileList[i]);
                }
            }
            GIC.AddBoardsToSet(FileList);
            GIC.WriteImageFiles(TargetFileBaseName, 200, Gerber.DirectlyShowGeneratedBoardImages, new GerberToImage());
            Console.WriteLine("Done writing {0}", TargetFileBaseName);
            Console.ReadKey();
        }
Example #14
0
        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);
        }
Example #15
0
        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);
            }
        }
Example #16
0
        static void Main(string[] args)
        {
            if (args.Count() < 1)
            {
                Console.WriteLine("need files to render...");
                Console.WriteLine("GerberToImage <files> [--dpi N] [--noxray] [--nopcb] [--silk color] [--trace color] [--copper color] [--mask color]");
                return;
            }

            int           dpi         = 400;
            Arguments     NextArg     = Arguments.None;
            bool          xray        = true;
            bool          normal      = true;
            string        pcbcolor    = "green";
            string        silkcolor   = "white";
            string        tracecolor  = "auto";
            string        coppercolor = "gold";
            List <string> RestList    = new List <string>();

            for (int i = 0; i < args.Count(); i++)
            {
                switch (NextArg)
                {
                case Arguments.dpi: dpi = Int32.Parse(args[i]); NextArg = Arguments.None; break;

                case Arguments.silk: silkcolor = args[i]; NextArg = Arguments.None; break;

                case Arguments.mask: pcbcolor = args[i]; NextArg = Arguments.None; break;

                case Arguments.trace: tracecolor = args[i]; NextArg = Arguments.None; break;

                case Arguments.copper: coppercolor = args[i]; NextArg = Arguments.None; break;

                case Arguments.None:
                    switch (args[i].ToLower())
                    {
                    case "-dpi":
                    case "--dpi": NextArg = Arguments.dpi; break;

                    case "-silk":
                    case "--silk": NextArg = Arguments.silk; break;

                    case "-trace":
                    case "--trace": NextArg = Arguments.trace; break;

                    case "-copper":
                    case "--copper": NextArg = Arguments.copper; break;

                    case "-mask":
                    case "--mask": NextArg = Arguments.mask; break;

                    case "-noxray":
                    case "--noxray": xray = false; NextArg = Arguments.None; break;

                    case "-nopcb":
                    case "--nopcb": normal = false; NextArg = Arguments.None; break;

                    default:
                        RestList.Add(args[i]); break;
                    }
                    break;
                }
            }

            Gerber.SaveIntermediateImages = false;
            Gerber.ShowProgress           = true;

            if (RestList.Count() == 1 && File.Exists(RestList[0]) && Path.GetExtension(RestList[0]).ToLower() != ".zip")
            {
                //  Gerber.WriteSanitized = true;
                Gerber.ExtremelyVerbose = false;
                //Gerber.Verbose = true;
                Gerber.ThrowExceptions = true;
                Gerber.WaitForKey      = true;
                Gerber.ShowProgress    = true;

                CreateImageForSingleFile(new StandardConsoleLog(), RestList[0], Color.Black, Color.White, dpi);
                if (Gerber.WaitForKey)
                {
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                }
                return;
            }

            GerberImageCreator GIC = new GerberImageCreator();
            string             TargetFileBaseName = "";

            if (RestList.Count() >= 1)
            {
                TargetFileBaseName = RestList[0];
            }

            List <String> FileList = new List <string>();

            foreach (var a in RestList)
            {
                if (Directory.Exists(a))
                {
                    FileList.AddRange(Directory.GetFiles(a, "*.*"));
                }
                else
                {
                    if (File.Exists(a))
                    {
                        FileList.Add(a);
                    }
                }
            }

            for (int i = 0; i < FileList.Count; i++)
            {
                if (FileList[i][FileList[i].Count() - 1] == '\\')
                {
                    FileList[i] = Path.GetDirectoryName(FileList[i]);
                }
            }
            var L = new GerberToImage(Path.GetFileNameWithoutExtension(TargetFileBaseName));

            GIC.AddBoardsToSet(FileList, new StandardConsoleLog(), true);
            BoardRenderColorSet colors = new BoardRenderColorSet();

            if (pcbcolor == "")
            {
                pcbcolor = "black";
            }
            colors.SetupColors(pcbcolor, silkcolor, tracecolor, coppercolor);


            GIC.SetColors(colors);
            GIC.WriteImageFiles(TargetFileBaseName, dpi, false, xray, normal, new StandardConsoleLog());
            Console.WriteLine("Done writing {0}", TargetFileBaseName);
        }
Example #17
0
        public static void MergeFrameIntoGerberSet(string FrameFolder, string OutlineFolder, string OutputFolder, FrameSettings FS, ProgressLog log, string basename)
        {
            log.PushActivity("MergeFrame");
            GerberPanel PNL = new GerberPanel();

            PNL.AddGerberFolder(log, FrameFolder);
            PNL.AddGerberFolder(log, OutlineFolder);
            PNL.TheSet.ClipToOutlines = false;



            var FrameInstance   = PNL.AddInstance(FrameFolder, new PointD(0, 0));
            var OutlineInstance = PNL.AddInstance(OutlineFolder, new PointD(0, 0));

            PNL.UpdateShape(log);

            var BB = OutlineInstance.BoundingBox;

            foreach (var s in OutlineInstance.TransformedOutlines)
            {
                bool ClockWise = s.ClockWise();

                if (s.Vertices.Count >= 2)
                {
                    for (int i = 0; i < s.Vertices.Count; i++)
                    {
                        PointD p1 = s.Vertices[i];
                        PointD p2 = s.Vertices[(i + 1) % s.Vertices.Count];

                        var D = p2 - p1;
                        if (Math.Abs(D.X) < 0.5 && Math.Abs(D.Y) >= FS.mmbetweentabs && FS.VerticalTabs)
                        {
                            // perfect vertical!
                            log.AddString(String.Format("vertical found: {0} -> {1}", p1, p2));

                            double dy = p2.Y - p1.Y;

                            double x         = 0;
                            double rad       = 0;
                            bool   rightside = (dy > 0);
                            if (ClockWise)
                            {
                                rightside = !rightside;
                            }

                            if (FS.InsideEdgeMode == FrameSettings.InsideMode.RegularEdge)
                            {
                                if (rightside)
                                {
                                    x   = (p1.X + (BB.BottomRight.X + FS.margin)) / 2;
                                    rad = Math.Abs((p1.X - (BB.BottomRight.X + FS.margin))) / 2.0 + FS.margin;
                                }
                                else
                                {
                                    x   = (p1.X + (BB.TopLeft.X - FS.margin)) / 2;
                                    rad = Math.Abs((p1.X - (BB.TopLeft.X - FS.margin))) / 2.0 + FS.margin;
                                }
                            }

                            if (FS.InsideEdgeMode == FrameSettings.InsideMode.FormFitting)
                            {
                                if (rightside)
                                {
                                    x   = p1.X + (FS.margin / 2);
                                    rad = FS.margin;
                                }
                                else
                                {
                                    x   = p1.X - (FS.margin / 2);
                                    rad = FS.margin;
                                }
                            }


                            int tabs = (int)Math.Floor(Math.Abs(dy) / FS.mmbetweentabs);
                            for (int j = 0; j < tabs; j++)
                            {
                                double y  = p1.Y + (dy / (float)tabs) * (j + 0.5);
                                var    BR = PNL.AddTab(new PointD(x, y));
                                log.AddString(String.Format("tab at {0} - radius {1}", BR.Center, rad));
                                BR.Radius = (float)rad;
                            }
                        }

                        if (Math.Abs(D.Y) < 0.5 && Math.Abs(D.X) >= FS.mmbetweentabs && FS.HorizontalTabs)
                        {
                            // perfect vertical!
                            log.AddString(String.Format("horizontal found: {0} -> {1}", p1, p2));

                            double dx = p2.X - p1.X;

                            double y         = 0;
                            double rad       = 0;
                            bool   rightside = (dx < 0);
                            if (ClockWise)
                            {
                                rightside = !rightside;
                            }

                            if (FS.InsideEdgeMode == FrameSettings.InsideMode.RegularEdge)
                            {
                                if (rightside)
                                {
                                    y   = (p1.Y + (BB.BottomRight.Y + FS.margin)) / 2;
                                    rad = Math.Abs((p1.Y - (BB.BottomRight.Y + FS.margin))) / 2.0 + FS.margin;
                                }
                                else
                                {
                                    y   = (p1.Y + (BB.TopLeft.Y - FS.margin)) / 2;
                                    rad = Math.Abs((p1.Y - (BB.TopLeft.Y - FS.margin))) / 2.0 + FS.margin;
                                }
                            }

                            if (FS.InsideEdgeMode == FrameSettings.InsideMode.FormFitting)
                            {
                                if (rightside)
                                {
                                    y   = p1.Y + (FS.margin / 2);
                                    rad = FS.margin;
                                }
                                else
                                {
                                    y   = p1.Y - (FS.margin / 2);
                                    rad = FS.margin;
                                }
                            }


                            int tabs = (int)Math.Floor(Math.Abs(dx) / FS.mmbetweentabs);
                            for (int j = 0; j < tabs; j++)
                            {
                                double x  = p1.X + (dx / (float)tabs) * (j + 0.5);
                                var    BR = PNL.AddTab(new PointD(x, y));
                                log.AddString(String.Format("tab at {0} - radius {1}", BR.Center, rad));
                                BR.Radius = (float)rad;
                            }
                        }
                    }
                }
            }


            PNL.UpdateShape(log);

            Directory.CreateDirectory(OutputFolder);
            var PNLFiles = PNL.SaveGerbersToFolder("MergedFrame", OutputFolder, log, true, false, true, basename);

            if (FS.RenderSample)
            {
                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(Directory.GetFiles(OutputFolder).ToList(), new SilentLog());
                GIC.WriteImageFiles(basename, 200, true, false, true, null);
            }

            log.PopActivity();

            //            PNL.SaveOutlineTo(OutputFolder, "mergedframeblended");
            return;



            var           FrameFiles   = Directory.GetFiles(FrameFolder);
            var           OutlineFiles = Directory.GetFiles(OutlineFolder);
            List <String> AllFiles     = new List <string>();

            AllFiles.AddRange(FrameFiles);
            foreach (var a in OutlineFiles)
            {
                BoardLayer layer;
                BoardSide  Side;

                Gerber.DetermineBoardSideAndLayer(a, out Side, out layer);

                if (layer != BoardLayer.Outline)
                {
                    AllFiles.Add(a);
                }
            }

            //  AllFiles.AddRange(OutlineFiles);
            GerberMerger.MergeAllByFileType(AllFiles, OutputFolder, "MergedFrame", log);
        }
        public static List <String> WriteProtoBoardFlowerStyle(double width, double height, double mountholediameter, double cornerrounding, double mountholeclearance, double edgeclearance = 1.26, bool WriteCombinedImage = true, bool WriteImages = false)
        {
            FontSet       FS             = FontSet.Load("Font.xml");
            int           polyid         = 0;
            List <String> FilesGenerated = new List <string>();
            double        LabelHeight    = 1.2;
            double        PadWidth       = 1.8;
            double        DrillWidth     = 1.0;

            string basename = String.Format("Generated_This_is_not_RocketScience_Flower_prototoboard_{0}x{1}cm", width / 10.0f, height / 10.0f);

            try
            {
                Directory.CreateDirectory(basename);
            }
            catch (Exception)
            {
            }
            basename = Path.Combine(basename, String.Format("proto_{0}x{1}cm", width / 10.0f, height / 10.0f));
            string OutlineFile      = basename + ".gko";
            string SilkFileTop      = basename + ".gto";
            string SilkFileBottom   = basename + ".gbo";
            string SolderMaskBottom = basename + ".gbs";
            string SolderMaskTop    = basename + ".gts";
            string CopperBottom     = basename + ".gbl";
            string CopperTop        = basename + ".gtl";
            string DrillFile        = basename + ".txt";


            int XE = (int)Math.Floor((width - Math.Max(edgeclearance, LabelHeight) * 2) / 2.54);
            int YE = (int)Math.Floor((height - Math.Max(edgeclearance, LabelHeight) * 2) / 2.54);

            PrepareGridSpace(XE, YE, true, true, 3, 2);

            string[][] Grid = new string[XE + 1][];
            for (int x = 0; x <= XE; x++)
            {
                Grid[x] = new string[YE + 1];
                for (int y = 0; y <= YE; y++)
                {
                    Grid[x][y] = GridSpaceType(x, y);
                    if (y == YE)
                    {
                        Grid[x][y] = "None";
                    }
                    if (x == XE)
                    {
                        Grid[x][y] = "None";
                    }
                }
            }

            double XO = (width / 2) - (XE / 2.0) * 2.54;
            double YO = (height / 2) - (YE / 2.0) * 2.54;

            Console.WriteLine("{0} rows and {1} columns", YE, XE);


            ExcellonFile EF = new ExcellonFile();
            PointD       C1 = new PointD(0, 0);
            PointD       C2 = new PointD(width, 0);
            PointD       C3 = new PointD(width, height);
            PointD       C4 = new PointD(0, height);

            if (mountholediameter > 0)
            {
                ExcellonTool MountHoleDrill = new ExcellonTool();
                MountHoleDrill.Radius = mountholediameter / 2.0;


                C1 = new PointD(mountholeclearance, mountholeclearance);
                C2 = new PointD(width - mountholeclearance, mountholeclearance);
                C3 = new PointD(width - mountholeclearance, height - mountholeclearance);
                C4 = new PointD(mountholeclearance, height - mountholeclearance);
                MountHoleDrill.Drills.Add(C1);
                MountHoleDrill.Drills.Add(C2);
                MountHoleDrill.Drills.Add(C3);
                MountHoleDrill.Drills.Add(C4);

                EF.Tools[10] = MountHoleDrill;
            }
            ExcellonTool ProtoHoleDrill = new ExcellonTool();

            ProtoHoleDrill.Radius = DrillWidth / 2.0;



            PolyLine PL = new PolyLine(polyid++);

            PL.Add(cornerrounding, 0);
            PL.Add(width - cornerrounding, 0);
            PL.ArcTo(new PointD(width, cornerrounding), new PointD(0, cornerrounding), InterpolationMode.CounterClockwise);
            PL.Add(width, height - cornerrounding);
            PL.ArcTo(new PointD(width - cornerrounding, height), new PointD(-cornerrounding, 0), InterpolationMode.CounterClockwise);
            PL.Add(cornerrounding, height);
            PL.ArcTo(new PointD(0, height - cornerrounding), new PointD(0, -cornerrounding), InterpolationMode.CounterClockwise);
            PL.Add(0, cornerrounding);
            PL.ArcTo(new PointD(cornerrounding, 0), new PointD(cornerrounding, 0), InterpolationMode.CounterClockwise);
            PL.Close();

            GerberArtWriter GAW = new GerberArtWriter();

            GAW.AddPolyLine(PL, 0);
            GAW.Write(OutlineFile);

            GerberArtWriter GAWTop    = new GerberArtWriter();
            GerberArtWriter GAWBottom = new GerberArtWriter();

            GerberArtWriter MaskTop    = new GerberArtWriter();
            GerberArtWriter MaskBottom = new GerberArtWriter();
            GerberArtWriter SilkTop    = new GerberArtWriter();
            GerberArtWriter SilkBottom = new GerberArtWriter();

            for (int x = 0; x < XE; x++)
            {
                double xc = XO + x * 2.54 + 2.54 / 2;
                for (int y = 0; y < YE; y++)
                {
                    double yc = YO + y * 2.54 + 2.54 / 2;

                    PointD P = new PointD(xc, yc);
                    if ((P - C1).Length() < edgeclearance + mountholediameter / 2 + 1.27)
                    {
                        Grid[x][y] = "None";
                    }
                    if ((P - C2).Length() < edgeclearance + mountholediameter / 2 + 1.27)
                    {
                        Grid[x][y] = "None";
                    }
                    if ((P - C3).Length() < edgeclearance + mountholediameter / 2 + 1.27)
                    {
                        Grid[x][y] = "None";
                    }
                    if ((P - C4).Length() < edgeclearance + mountholediameter / 2 + 1.27)
                    {
                        Grid[x][y] = "None";
                    }

                    if (Grid[x][y] != "None")
                    {
                        GAWTop.AddFlash(new PointD(xc, yc), DrillWidth * 1.3 / 2.0);
                        GAWBottom.AddFlash(new PointD(xc, yc), DrillWidth * 1.3 / 2.0);
                        MaskTop.AddFlash(new PointD(xc, yc), DrillWidth * 1.3 / 2.0);
                        MaskBottom.AddFlash(new PointD(xc, yc), DrillWidth * 1.3 / 2.0);
                        ProtoHoleDrill.Drills.Add(new PointD(xc, yc));
                    }
                }
            }

            //            SilkTop.DrawString(new PointD(5, 0), FS, "O0O", 5, 0.05, StringAlign.BottomCenter);
            //           SilkTop.DrawString(new PointD(20, 0), FS, "O0O", 5, 0.05, StringAlign.CenterCenter);
            //         SilkTop.DrawString(new PointD(40, 0), FS, "OMO", 5, 0.05, StringAlign.TopCenter);

            for (int x = 0; x < XE; x++)
            {
                double xc  = XO + x * 2.54 + 2.54 / 2;
                double yc  = YO + (0) * 2.54;
                double yc1 = YO + (YE) * 2.54 + 2.54;
                char   T   = (char)('A' + (char)((x) % 26));

                if (Grid[x][0] != "None")
                {
                    SilkTop.DrawString(new PointD(xc, yc), FS, T.ToString(), LabelHeight, 0.05, StringAlign.CenterCenter);
                    SilkBottom.DrawString(new PointD(xc, yc), FS, T.ToString(), LabelHeight, 0.05, StringAlign.CenterCenter, true);
                }

                if (Grid[x][YE - 1] != "None")
                {
                    SilkTop.DrawString(new PointD(xc, yc1), FS, T.ToString(), LabelHeight, 0.05, StringAlign.CenterCenter);
                    SilkBottom.DrawString(new PointD(xc, yc1), FS, T.ToString(), LabelHeight, 0.05, StringAlign.CenterCenter, true);
                }
            }
            for (int y = 0; y < YE; y++)
            {
                double xc  = XO + (0) * 2.54 - 2.54 / 2;
                double xc1 = XO + (XE + 1) * 2.54 - 2.54 / 2;
                double yc  = YO + (y) * 2.54 + 2.54;

                if (Grid[0][y] != "None")
                {
                    SilkTop.DrawString(new PointD(xc, yc), FS, (YE - y).ToString(), LabelHeight, 0.05, StringAlign.CenterCenter);
                    SilkBottom.DrawString(new PointD(xc, yc), FS, (YE - y).ToString(), LabelHeight, 0.05, StringAlign.CenterCenter, true);
                }

                if (Grid[XE - 1][y] != "None")
                {
                    SilkTop.DrawString(new PointD(xc1, yc), FS, (YE - y).ToString(), LabelHeight, 0.05, StringAlign.CenterCenter);
                    SilkBottom.DrawString(new PointD(xc1, yc), FS, (YE - y).ToString(), LabelHeight, 0.05, StringAlign.CenterCenter, true);
                }
            }

            PolyLine PL2 = new PolyLine(polyid++);
            double   rad = 0.6;

            for (int i = 0; i < 20; i++)
            {
                double P = i * Math.PI / 19.0;
                PL2.Add(Math.Sin(P) * rad, Math.Cos(P) * rad);
            }
            PL2.Close();
            PL2.Translate(-1.1, 0);


            for (int x = -1; x < XE + 1; x++)
            {
                for (int y = -1; y < YE + 1; y++)
                {
                    string s1 = "None";
                    if (y >= 0 && y < YE && x < XE)
                    {
                        s1 = Grid[x + 1][y];
                    }
                    string s2 = "None";
                    if (x >= 0 && x < XE && y < YE)
                    {
                        s2 = Grid[x][y + 1];
                    }
                    string s3 = "None";
                    if (x >= 0 && x < XE && y >= 0 && y < YE)
                    {
                        s3 = Grid[x][y];
                    }
                    double xc  = XO + x * 2.54 + 2.54 / 2;
                    double yc  = YO + y * 2.54 + 2.54 / 2;
                    double xc1 = XO + (x + 1) * 2.54 + 2.54 / 2;
                    double yc1 = YO + (y + 1) * 2.54 + 2.54 / 2;

                    if (s1 != "None" && s3 != "None")
                    {
                        PolyLine PLA = PL2.Copy();
                        PLA.Translate(xc1, yc);
                        PolyLine PLC = PL2.Copy();
                        PLC.RotateDegrees(180);
                        PLC.Translate(xc, yc);

                        GAWTop.AddPolygon(PLA);
                        MaskTop.AddPolygon(PLA);
                        GAWTop.AddPolygon(PLC);
                        MaskTop.AddPolygon(PLC);
                        GAWBottom.AddPolygon(PLA);
                        MaskBottom.AddPolygon(PLA);
                        GAWBottom.AddPolygon(PLC);
                        MaskBottom.AddPolygon(PLC);
                    }

                    if (s3 != "None" && s2 != "None")
                    {
                        PolyLine PLB = PL2.Copy();
                        PLB.RotateDegrees(90);
                        PLB.Translate(xc, yc1);
                        PolyLine PLD = PL2.Copy();
                        PLD.RotateDegrees(270);
                        PLD.Translate(xc, yc);

                        GAWTop.AddPolygon(PLB);
                        MaskTop.AddPolygon(PLB);
                        GAWTop.AddPolygon(PLD);
                        MaskTop.AddPolygon(PLD);
                        GAWBottom.AddPolygon(PLB);
                        MaskBottom.AddPolygon(PLB);
                        GAWBottom.AddPolygon(PLD);
                        MaskBottom.AddPolygon(PLD);
                        //       GAWTop.AddPolygon(PLD);
                        //     MaskTop.AddPolygon(PLD);
                    }
                }
            }

            GAWTop.Write(CopperTop);

            GAWBottom.Write(CopperBottom);
            SilkBottom.Write(SilkFileBottom);
            SilkTop.Write(SilkFileTop);

            MaskTop.Write(SolderMaskTop);
            MaskBottom.Write(SolderMaskBottom);
            EF.Tools[11] = ProtoHoleDrill;
            EF.Write(DrillFile, 0, 0, 0, 0);

            FilesGenerated.Add(CopperBottom);
            FilesGenerated.Add(CopperTop);
            FilesGenerated.Add(OutlineFile);
            FilesGenerated.Add(DrillFile);
            FilesGenerated.Add(SilkFileBottom);
            FilesGenerated.Add(SilkFileTop);
            FilesGenerated.Add(SolderMaskBottom);
            FilesGenerated.Add(SolderMaskTop);


            if (WriteCombinedImage)
            {
                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(FilesGenerated);
                GIC.WriteImageFiles(basename + "_render", 200, false);
            }
            if (WriteImages)
            {
                Gerber.SaveGerberFileToImage(OutlineFile, OutlineFile + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(CopperBottom, CopperBottom + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(CopperTop, CopperTop + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(DrillFile, DrillFile + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(SilkFileTop, SilkFileTop + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(SilkFileBottom, SilkFileBottom + "_render.png", 1000, Color.Black, Color.White);
            }

            return(FilesGenerated);
        }
        public static List <string> WriteSideEdgeFrame(PolyLine pl, FrameSettings FS, string basefile, bool insideedge = true)
        {
            List <string> Files = new List <string>();

            try
            {
                int    polyid     = 0;
                string fname      = System.IO.Path.GetFileName(basefile);
                string fnamenoext = System.IO.Path.GetFileNameWithoutExtension(basefile);
                string OutName    = Path.Combine(System.IO.Path.GetDirectoryName(basefile), fnamenoext);
                Console.WriteLine("writing frame files to {0}", OutName);
                Bounds B = new Bounds();
                B.AddPolyLine(pl);
//                FS.innerHeight = B.Height();
//              FS.innerWidth = B.Width();


                double W  = (double)FS.innerWidth;
                double H  = (double)FS.innerHeight;
                double TE = (double)FS.topEdge;
                double LE = (double)FS.leftEdge;

                double OuterWidth  = W + LE * 2.0;
                double OuterHeight = H + TE * 2.0;
                double InnerWidth  = W;
                double InnerHeight = H;


                PCBWriterSet PCB = new PCBWriterSet();

                double mountholediameter = (double)FS.holeDiameter;



                // board outline
                PolyLine PL = new PolyLine(polyid++);
                PL.MakeRoundedRect(new PointD(0, 0), new PointD(OuterWidth, OuterHeight), (double)FS.roundedOuterCorners);
                PCB.AddOutline(PL);

                if (insideedge)
                {
                    PolyLine PL2 = new PolyLine(polyid++);
                    PL2.MakeRoundedRect(new PointD(LE, TE), new PointD(InnerWidth + LE, InnerHeight + TE), (double)FS.roundedInnerCorners);
                    PCB.AddOutline(PL2);
                }

                #region fiducials

                List <PointD> Fiducials = new List <PointD>();

                foreach (var P in FS.fiducialsListData)
                {
                    PointD fiducialPoint = P.pos;
                    PCB.Fiducial(fiducialPoint, P.CopperDiameter, P.MaskDiameter, P.Top);
                }

                #endregion

                string FrameTitle = FS.FrameTitle;

                string FrameTopTitle            = FrameTitle + " - Top";
                string FrameBottomTitle         = FrameTitle + " - Bottom";
                double verticaltitleclearance   = 0;
                double horizontaltitleclearance = 0;
                if (FrameTitle.Length > 0)
                {
                    FontSet fnt = FontSet.Load("Font.xml");
                    horizontaltitleclearance = fnt.StringWidth(FrameBottomTitle, TE / 4) + mountholediameter * 2 + FS.topEdge;
                    verticaltitleclearance   = fnt.StringWidth(FrameBottomTitle, TE / 4) + mountholediameter * 2 + FS.topEdge;

                    PCB.Label(fnt, new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FrameTopTitle, TE / 4, StringAlign.CenterCenter, 0.1, true, true, 0);
                    PCB.Label(fnt, new PointD(OuterWidth - LE / 4, OuterHeight / 2), FrameTopTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, -90);
                    PCB.Label(fnt, new PointD(LE / 4, OuterHeight / 2), FrameTopTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, 90);
                    PCB.Label(fnt, new PointD(OuterWidth / 2.0, TE - TE / 4.0), FrameTopTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, 0);


                    PCB.Label(fnt, new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FrameBottomTitle, TE / 4, StringAlign.CenterCenter, 0.1, true, true, 0, true, true, true);
                    PCB.Label(fnt, new PointD(OuterWidth - LE / 4, OuterHeight / 2), FrameBottomTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, -90, true);
                    PCB.Label(fnt, new PointD(LE / 4, OuterHeight / 2), FrameBottomTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, 90, true);
                    PCB.Label(fnt, new PointD(OuterWidth / 2.0, TE - TE / 4.0), FrameBottomTitle, TE / 4.0, StringAlign.CenterCenter, 0.1, true, true, 0, true);
                }

                if (FS.addHoles)
                {
                    double side = LE / 2.0;
                    double top  = TE / 2.0;

                    PCB.Drill(new PointD(side, top), mountholediameter, 1);
                    PCB.Drill(new PointD(OuterWidth - side, top), mountholediameter, 1);
                    PCB.Drill(new PointD(OuterWidth - side, OuterHeight - top), mountholediameter, 1);
                    PCB.Drill(new PointD(side, OuterHeight - top), mountholediameter, 1);

                    double dx = (OuterWidth - side) - side;
                    double dy = (OuterHeight - top) - top;

                    dx -= horizontaltitleclearance;
                    dy -= verticaltitleclearance;
                    dx /= 2;
                    dy /= 2;

                    int horiz = (int)Math.Ceiling((dx / 2) / FS.MaxMountHoleSpacingMM);
                    int vert  = (int)Math.Ceiling((dy / 2) / FS.MaxMountHoleSpacingMM);

                    dx /= (float)horiz;
                    dy /= (float)vert;

                    if (dx < FS.MinMountHoleSpacingMM)
                    {
                        horiz = 0;
                    }
                    if (dy < FS.MinMountHoleSpacingMM)
                    {
                        vert = 0;
                    }

                    for (int i = 1; i <= horiz; i++)
                    {
                        PCB.Drill(new PointD(side + (dx) * i, top), mountholediameter, 1);
                        PCB.Drill(new PointD((OuterWidth - side) - (dx) * i, top), mountholediameter, 1);
                        PCB.Drill(new PointD(side + (dx) * i, OuterHeight - top), mountholediameter, 1);
                        PCB.Drill(new PointD((OuterWidth - side) - (dx) * i, OuterHeight - top), mountholediameter, 1);
                    }


                    for (int i = 1; i <= vert; i++)
                    {
                        PCB.Drill(new PointD(side, top + (dy) * i), mountholediameter, 1);
                        PCB.Drill(new PointD(side, (OuterHeight - top) - (dy) * i), mountholediameter, 1);
                        PCB.Drill(new PointD((OuterWidth - side), top + (dy) * i), mountholediameter, 1);
                        PCB.Drill(new PointD((OuterWidth - side), (OuterHeight - top) - (dy) * i), mountholediameter, 1);
                    }
                }

                PolyLine Left   = new PolyLine();
                PolyLine Right  = new PolyLine();
                PolyLine Top    = new PolyLine();
                PolyLine Bottom = new PolyLine();


                Top.MakePRectangle(OuterWidth + 1, FS.topEdge + 1, -1, -1);
                Bottom.MakePRectangle(OuterWidth, FS.topEdge, 0, OuterHeight - FS.topEdge);
                Left.MakePRectangle(FS.leftEdge + 1, OuterHeight + 1, -1, -1);
                Right.MakePRectangle(FS.leftEdge, OuterHeight, OuterWidth - FS.leftEdge, 0);
                PCB.ArtInclusions.Add(Left);
                PCB.ArtInclusions.Add(Right);
                PCB.ArtInclusions.Add(Top);
                PCB.ArtInclusions.Add(Bottom);



                //PCB.CellularArt();


                Files.AddRange(PCB.Write(Path.GetDirectoryName(basefile), Path.GetFileNameWithoutExtension(basefile), FS.offset));;



                if (FS.RenderSample)
                {
                    GerberImageCreator GIC = new GerberImageCreator();
                    GIC.AddBoardsToSet(Files, new SilentLog());
                    GIC.WriteImageFiles(OutName, 200, true, false, true, null);
                }
            }
            catch (Exception e)
            {
                Console.Write(" Exception while making frames:{0}", e.Message);
            }
            foreach (var s in Files)
            {
                Console.WriteLine("Writen edge file {0} succesfully", s);
            }
            return(Files);
        }
        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";

            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))
                {
                    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();
                    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();
                    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();
                    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);

                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(System.IO.Directory.GetFiles(basename).ToList());
                GIC.WriteImageFiles(basename + "/render");
            }
        }
Example #21
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int    polyid     = 0;
                string fname      = System.IO.Path.GetFileName(saveFileDialog1.FileName);
                string fnamenoext = System.IO.Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);
                string OutName    = "";
                if (fname.Length != fnamenoext.Length)
                {
                    int extlen = fname.Length - fnamenoext.Length;
                    OutName = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.Length - extlen);
                }
                else
                {
                    OutName = saveFileDialog1.FileName;
                }


                List <string> Files = new List <string>();

                double W  = (double)innerWidth.Value;
                double H  = (double)innerHeight.Value;
                double TE = (double)topEdge.Value;
                double LE = (double)leftEdge.Value;

                double OuterWidth  = W + LE * 2.0;
                double OuterHeight = H + TE * 2.0;
                double InnerWidth  = W;
                double InnerHeight = H;

                GerberArtWriter Outline       = new GerberArtWriter();
                GerberArtWriter TopSilk       = new GerberArtWriter();
                GerberArtWriter TopCopper     = new GerberArtWriter();
                GerberArtWriter TopSolderMask = new GerberArtWriter();

                GerberArtWriter BottomSilk       = new GerberArtWriter();
                GerberArtWriter BottomCopper     = new GerberArtWriter();
                GerberArtWriter BottomSolderMask = new GerberArtWriter();


                List <PointD> DrillHoles        = new List <PointD>();
                double        mountholediameter = (double)holeDiameter.Value;

                if (addHolesCheck.Checked)
                {
                    double side = LE / 2.0;
                    double top  = TE / 2.0;


                    DrillHoles.Add(new PointD(side, top));
                    DrillHoles.Add(new PointD(OuterWidth - side, top));
                    DrillHoles.Add(new PointD(OuterWidth - side, OuterHeight - top));
                    DrillHoles.Add(new PointD(side, OuterHeight - top));

                    ExcellonFile EF = new ExcellonFile();

                    ExcellonTool MountHoleDrill = new ExcellonTool();
                    MountHoleDrill.Radius = mountholediameter / 2.0;

                    foreach (var a in DrillHoles)
                    {
                        MountHoleDrill.Drills.Add(a);
                    }
                    EF.Tools[10] = MountHoleDrill;
                    EF.Write(OutName + ".txt", 0, 0, 0, 0);

                    Files.Add(OutName + ".txt");
                }

                // board outline
                PolyLine PL = new PolyLine(polyid++);
                PL.MakeRoundedRect(new PointD(0, 0), new PointD(OuterWidth, OuterHeight), (double)roundedOuterCorners.Value);
                Outline.AddPolyLine(PL, 0);
                PolyLine PL2 = new PolyLine(polyid++);

                PL2.MakeRoundedRect(new PointD(LE, TE), new PointD(InnerWidth + LE, InnerHeight + TE), (double)roundedInnerCorners.Value);
                Outline.AddPolyLine(PL2, 0);

                #region fiducials

                List <PointD> Fiducials = new List <PointD>();

                foreach (DataGridViewRow dataRow in fiducialsListData.Rows)
                {
                    PointD fiducialPoint = new PointD();

                    if (dataRow.Cells != null && dataRow.Cells["XCoordinate"].Value != null && dataRow.Cells["YCoordinate"].Value != null)
                    {
                        fiducialPoint.X = double.Parse(dataRow.Cells["XCoordinate"].Value.ToString());
                        fiducialPoint.Y = double.Parse(dataRow.Cells["YCoordinate"].Value.ToString());

                        if (string.Equals(dataRow.Cells["fiducialLayer"].Value.ToString(), "Top"))
                        {
                            TopCopper.AddFlash(fiducialPoint, double.Parse(dataRow.Cells["fiducialCopperDiameter"].Value.ToString()) / 2.0);
                            TopSolderMask.AddFlash(fiducialPoint, double.Parse(dataRow.Cells["fiducialSolderMaskDiam"].Value.ToString()) / 2.0);
                        }
                        else
                        {
                            BottomCopper.AddFlash(fiducialPoint, double.Parse(dataRow.Cells["fiducialCopperDiameter"].Value.ToString()) / 2.0);
                            BottomSolderMask.AddFlash(fiducialPoint, double.Parse(dataRow.Cells["fiducialSolderMaskDiam"].Value.ToString()) / 2.0);
                        }
                    }
                }

                #endregion

                string FrameTitle = FrameTitleBox.Text;

                if (FrameTitle.Length > 0)
                {
                    FontSet FS = FontSet.Load("Font.xml");
                    TopSilk.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - top", TE / 4.0, 0.1, StringAlign.CenterCenter);
                    TopCopper.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - top", TE / 4.0, 0.1, StringAlign.CenterCenter);
                    BottomSilk.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - bottom", TE / 4.0, 0.1, StringAlign.CenterCenter, true);
                    BottomCopper.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - bottom", TE / 4.0, 0.1, StringAlign.CenterCenter, true);
                }

                foreach (var a in DrillHoles)
                {
                    TopSolderMask.AddFlash(a, mountholediameter / 2.0 + 1.0);
                    BottomSolderMask.AddFlash(a, mountholediameter / 2.0 + 1.0);
                }

                Outline.Write(OutName + ".gko");
                TopSilk.Write(OutName + ".gto");
                TopCopper.Write(OutName + ".gtl");
                TopSolderMask.Write(OutName + ".gts");

                BottomSilk.Write(OutName + ".gbo");
                BottomCopper.Write(OutName + ".gbl");
                BottomSolderMask.Write(OutName + ".gbs");


                Files.Add(OutName + ".gko");
                Files.Add(OutName + ".gto");
                Files.Add(OutName + ".gtl");
                Files.Add(OutName + ".gts");
                Files.Add(OutName + ".gbo");
                Files.Add(OutName + ".gbl");
                Files.Add(OutName + ".gbs");
                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(Files, new StandardConsoleLog());

                GIC.WriteImageFiles(OutName, 50);
                // Gerber.SaveGerberFileToImage(OutName + ".gko", OutName + ".gko.png", 200, Color.Black, Color.White);
                // Gerber.SaveGerberFileToImage(OutName + ".gto", OutName + ".gto.png", 200, Color.Black, Color.White);
            }
        }
Example #22
0
        public void WriteAntennaBoard(double width, double height, double mountholediameter, double cornerrounding, double mountholeclearance, double edgeclearance = 1.26, bool WriteCombinedImage = true, bool WriteImages = false)
        {
            int     polyid = 0;
            FontSet FS     = FontSet.Load("Font.xml");

            List <String> FilesGenerated = new List <string>();
            double        LabelHeight    = 1.2;
            //double PadWidth = 1.8;
            double DrillWidth = 1.0;

            string basename = String.Format("Generated_This_is_not_RocketScience_Antenna_{0}x{1}cm", width / 10.0f, height / 10.0f);

            try
            {
                Directory.CreateDirectory(basename);
            }
            catch (Exception)
            {
            }
            basename = Path.Combine(basename, String.Format("proto_{0}x{1}cm", width / 10.0f, height / 10.0f));
            string OutlineFile      = basename + ".gko";
            string SilkFileTop      = basename + ".gto";
            string SilkFileBottom   = basename + ".gbo";
            string SolderMaskBottom = basename + ".gbs";
            string SolderMaskTop    = basename + ".gts";
            string CopperBottom     = basename + ".gbl";
            string CopperTop        = basename + ".gtl";
            string DrillFile        = basename + ".txt";

            ExcellonFile EF = new ExcellonFile();

            ExcellonTool MountHoleDrill = new ExcellonTool();

            MountHoleDrill.Radius = mountholediameter / 2.0;


            PointD C1 = new PointD(mountholeclearance, mountholeclearance);
            PointD C2 = new PointD(width - mountholeclearance, mountholeclearance);
            PointD C3 = new PointD(width - mountholeclearance, height - mountholeclearance);
            PointD C4 = new PointD(mountholeclearance, height - mountholeclearance);

            MountHoleDrill.Drills.Add(C1);
            MountHoleDrill.Drills.Add(C2);
            MountHoleDrill.Drills.Add(C3);
            MountHoleDrill.Drills.Add(C4);

            EF.Tools[10] = MountHoleDrill;

            ViaDrill.Radius = 0.6 / 2.0;

            EF.Tools[11] = ViaDrill;


            ExcellonTool ProtoHoleDrill = new ExcellonTool();

            ProtoHoleDrill.Radius = DrillWidth / 2.0;

            PointD pC1 = new PointD(mountholeclearance, height / 2 - 2.54);
            PointD pC2 = new PointD(mountholeclearance, height / 2);
            PointD pC3 = new PointD(mountholeclearance, height / 2 + 2.54);

            ProtoHoleDrill.Drills.Add(pC1);
            ProtoHoleDrill.Drills.Add(pC2);
            ProtoHoleDrill.Drills.Add(pC3);

            MaskBottom.AddFlash(pC1, 1);
            MaskBottom.AddFlash(pC2, 1);
            MaskBottom.AddFlash(pC3, 1);
            MaskTop.AddFlash(pC1, 1);
            MaskTop.AddFlash(pC2, 1);
            MaskTop.AddFlash(pC3, 1);

            PolyLine PL = new PolyLine(polyid++);

            PL.Add(cornerrounding, 0);
            PL.Add(width - cornerrounding, 0);
            PL.ArcTo(new PointD(width, cornerrounding), new PointD(0, cornerrounding), InterpolationMode.CounterClockwise);
            PL.Add(width, height - cornerrounding);
            PL.ArcTo(new PointD(width - cornerrounding, height), new PointD(-cornerrounding, 0), InterpolationMode.CounterClockwise);
            PL.Add(cornerrounding, height);
            PL.ArcTo(new PointD(0, height - cornerrounding), new PointD(0, -cornerrounding), InterpolationMode.CounterClockwise);
            PL.Add(0, cornerrounding);
            PL.ArcTo(new PointD(cornerrounding, 0), new PointD(cornerrounding, 0), InterpolationMode.CounterClockwise);
            PL.Close();

            GAW.AddPolyLine(PL, 0);
            GAW.Write(OutlineFile);

            double AX  = C1.X + mountholeclearance;
            double AY  = C1.Y + mountholeclearance;
            double gap = 5;


            double W = width - AX * 2;
            double H = height - AY * 2 - gap;

            int rounds = 6;

            antennatracewidth = 0.3 / 2;

            double spacing = 0.8;

            for (int i = 0; i < rounds; i++)
            {
                double W2 = W - i * spacing * 2;
                double H2 = H - i * spacing * 2;

                double W3 = W - (i + 1) * spacing * 2;
                double H3 = H - (i + 1) * spacing * 2;

                double AX2 = AX + i * spacing;
                double AY2 = AY + i * spacing;

                double AX3 = AX + (i + 1) * spacing;
                double AY3 = AY + (i + 1) * spacing;

                PolyLine PL2 = new PolyLine(polyid++);

                PL2.Add(AX2, AY2 + H2 / 2);
                PL2.Add(AX2, AY2);
                PL2.Add(AX2 + W2, AY2);
                PL2.Add(AX2 + W2, AY2 + H2 / 2);
                AddAntennaTop(PL2);


                PolyLine PL3 = new PolyLine(polyid++);

                PL3.Add(AX2, AY2 + gap + H2 - (H2 / 2));
                PL3.Add(AX2, AY2 + gap + H2);
                PL3.Add(AX2 + W2, AY2 + gap + H2);
                PL3.Add(AX2 + W2, AY2 + gap + H2 - (H2 / 2));
                AddAntennaTop(PL3);
                double gs = 2;

                if (i % 2 == 1)
                {
                    if (i < rounds - 1)
                    {
                        PolyLine PL4 = new PolyLine(polyid++);
                        PL4.Add(AX2, AY2 + gap + H2 - (H2 / 2));
                        PL4.Add(AX2, AY2 + gap - gs + H2 - (H2 / 2));
                        PL4.Add(AX3, AY3 + gs + (H3 / 2));
                        PL4.Add(AX3, AY3 + (H3 / 2));
                        AddAntennaTop(PL4);

                        PolyLine PL5 = new PolyLine(polyid++);
                        PL5.Add(AX3, AY3 + gap + H3 - (H3 / 2));
                        PL5.Add(AX2, AY2 + (H2 / 2));
                        AddAntennaBottom(PL5);

                        AddAntennaVia(AX3, AY3 + gap + H3 - (H3 / 2));
                        AddAntennaVia(AX2, AY2 + (H2 / 2));
                    }
                    else
                    {
                        PolyLine PL4 = new PolyLine(polyid++);
                        PL4.Add(AX2, AY2 + gap + H2 - (H2 / 2));
                        PL4.Add(AX2, AY2 + (H2 / 2));
                        AddAntennaTop(PL4);

                        AddAntennaVia(AX2, AY2 + H2 / 2 + gap / 2);

                        PolyLine PL5 = new PolyLine(polyid++);
                        PL5.Add(AX2, AY2 + H2 / 2 + gap / 2);
                        PL5.Add(AX2, AY2 + H2 / 2 - gs);
                        PL5.Add(AX - spacing, AY2 + H2 / 2 - gs);
                        PL5.Add(AX - spacing, AY2 + H2 / 2 + gap / 2);

                        AddAntennaVia(AX - spacing, AY2 + H2 / 2 + gap / 2);

                        AddAntennaBottom(PL5);
                    }
                }
                else
                {
                    PolyLine PL4 = new PolyLine(polyid++);
                    PL4.Add(AX2 + W2, AY2 + gap + H2 - (H2 / 2));
                    PL4.Add(AX2 + W2, AY2 + gap - gs + H2 - (H2 / 2));
                    PL4.Add(AX3 + W3, AY3 + gs + (H3 / 2));
                    PL4.Add(AX3 + W3, AY3 + (H3 / 2));
                    AddAntennaTop(PL4);

                    PolyLine PL5 = new PolyLine(polyid++);
                    PL5.Add(AX3 + W3, AY3 + gap + H3 - (H3 / 2));
                    PL5.Add(AX2 + W2, AY2 + (H2 / 2));

                    AddAntennaBottom(PL5);

                    PolyLine PL6 = new PolyLine(polyid++);
                    PL6.Add(AX, AY + H / 2);
                    PL6.Add(pC1.X, pC1.Y);
                    AddAntennaTop(PL6);

                    PolyLine PL7 = new PolyLine(polyid++);
                    PL7.Add(AX, AY + H / 2 + gap);
                    PL7.Add(pC3.X, pC3.Y);
                    AddAntennaTop(PL7);

                    PolyLine PL8 = new PolyLine(polyid++);
                    PL8.Add(AX - spacing, AY2 + H2 / 2 + gap / 2);
                    PL8.Add(pC2.X, pC2.Y);
                    AddAntennaTop(PL8);


                    AddAntennaVia(AX3 + W3, AY3 + gap + H3 - (H3 / 2));
                    AddAntennaVia(AX2 + W2, AY2 + (H2 / 2));
                }
            }


            string R = "NFC antenna: " + AntennaLen.ToString("N2") + " mm";

            // MaskTop.DrawString(new PointD(width / 2, 5), FS, R, LabelHeight, 0.05, StringAlign.CenterCenter);
            // MaskBottom.DrawString(new PointD(width / 2, 5), FS, R, LabelHeight, 0.05, StringAlign.CenterCenter, true);
            SilkTop.DrawString(new PointD(width / 2, 5), FS, R, LabelHeight, 0.05, StringAlign.CenterCenter);
            SilkBottom.DrawString(new PointD(width / 2, 5), FS, R, LabelHeight, 0.05, StringAlign.CenterCenter, true);


            GAWTop.Write(CopperTop);

            GAWBottom.Write(CopperBottom);
            SilkBottom.Write(SilkFileBottom);
            SilkTop.Write(SilkFileTop);

            MaskTop.Write(SolderMaskTop);
            MaskBottom.Write(SolderMaskBottom);
            EF.Tools[12] = ProtoHoleDrill;
            EF.Write(DrillFile, 0, 0, 0, 0);

            FilesGenerated.Add(CopperBottom);
            FilesGenerated.Add(CopperTop);
            FilesGenerated.Add(OutlineFile);
            FilesGenerated.Add(DrillFile);
            FilesGenerated.Add(SilkFileBottom);
            FilesGenerated.Add(SilkFileTop);
            FilesGenerated.Add(SolderMaskBottom);
            FilesGenerated.Add(SolderMaskTop);


            if (WriteCombinedImage)
            {
                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(FilesGenerated);
                GIC.WriteImageFiles(basename + "_render", 200, false);
            }
            if (WriteImages)
            {
                Gerber.SaveGerberFileToImage(OutlineFile, OutlineFile + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(CopperBottom, CopperBottom + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(CopperTop, CopperTop + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(DrillFile, DrillFile + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(SilkFileTop, SilkFileTop + "_render.png", 1000, Color.Black, Color.White);
                Gerber.SaveGerberFileToImage(SilkFileBottom, SilkFileBottom + "_render.png", 1000, Color.Black, Color.White);
            }
        }
Example #23
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fname      = System.IO.Path.GetFileName(saveFileDialog1.FileName);
                string fnamenoext = System.IO.Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);
                string OutName    = "";
                if (fname.Length != fnamenoext.Length)
                {
                    int extlen = fname.Length - fnamenoext.Length;
                    OutName = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.Length - extlen);
                }
                else
                {
                    OutName = saveFileDialog1.FileName;
                }



                List <string> Files = new List <string>();

                double W  = (double)innerWidth.Value;
                double H  = (double)innerHeight.Value;
                double TE = (double)topEdge.Value;
                double LE = (double)leftEdge.Value;

                double OuterWidth  = W + LE * 2.0;
                double OuterHeight = H + TE * 2.0;
                double InnerWidth  = W;
                double InnerHeight = H;

                GerberArtWriter Outline       = new GerberArtWriter();
                GerberArtWriter TopSilk       = new GerberArtWriter();
                GerberArtWriter TopCopper     = new GerberArtWriter();
                GerberArtWriter TopSolderMask = new GerberArtWriter();

                GerberArtWriter BottomSilk       = new GerberArtWriter();
                GerberArtWriter BottomCopper     = new GerberArtWriter();
                GerberArtWriter BottomSolderMask = new GerberArtWriter();


                List <PointD> DrillHoles        = new List <PointD>();
                double        mountholediameter = (double)holeDiameter.Value;

                if (addHolesCheck.Checked)
                {
                    double side = LE / 2.0;
                    double top  = TE / 2.0;


                    DrillHoles.Add(new PointD(side, top));
                    DrillHoles.Add(new PointD(OuterWidth - side, top));
                    DrillHoles.Add(new PointD(OuterWidth - side, OuterHeight - top));
                    DrillHoles.Add(new PointD(side, OuterHeight - top));


                    ExcellonFile EF = new ExcellonFile();

                    ExcellonTool MountHoleDrill = new ExcellonTool();
                    MountHoleDrill.Radius = mountholediameter / 2.0;

                    foreach (var a in DrillHoles)
                    {
                        MountHoleDrill.Drills.Add(a);
                    }
                    EF.Tools[10] = MountHoleDrill;
                    EF.Write(OutName + ".txt", 0, 0, 0, 0);

                    Files.Add(OutName + ".txt");
                }


                PolyLine PL = new PolyLine();
                PL.MakeRoundedRect(new PointD(0, 0), new PointD(OuterWidth, OuterHeight), (double)roundedOuterCorners.Value);
                Outline.AddPolyLine(PL, 0);
                PolyLine PL2 = new PolyLine();

                PL2.MakeRoundedRect(new PointD(LE, TE), new PointD(InnerWidth + LE, InnerHeight + TE), (double)roundedInnerCorners.Value);
                Outline.AddPolyLine(PL2, 0);

                if (fiducials.Checked)
                {
                    List <PointD> Fiducials = new List <PointD>();
                    Fiducials.Add(new PointD(LE * 2.0, TE / 2.0));
                    Fiducials.Add(new PointD(OuterWidth - LE * 2.0, TE / 2.0));
                    Fiducials.Add(new PointD(OuterWidth - LE * 2.0, OuterHeight - TE / 2.0));
                    Fiducials.Add(new PointD(LE * 2.0, OuterHeight - TE / 2.0));
                    Fiducials.Add(new PointD(OuterWidth / 2, TE / 2.0)); //generate extra non symetric fiducial
                    foreach (var A in Fiducials)
                    {
                        TopCopper.AddFlash(A, 1.0);
                        TopSolderMask.AddFlash(A, 3.0);
                    }
                }
                string FrameTitle = FrameTitleBox.Text;

                if (FrameTitle.Length > 0)
                {
                    FontSet FS = FontSet.Load("Font.xml");
                    TopSilk.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - top", TE / 4.0, 0.1, StringAlign.CenterCenter);
                    TopCopper.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - top", TE / 4.0, 0.1, StringAlign.CenterCenter);
                    BottomSilk.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - bottom", TE / 4.0, 0.1, StringAlign.CenterCenter, true);
                    BottomCopper.DrawString(new PointD(OuterWidth / 2.0, OuterHeight - TE / 4.0), FS, FrameTitle + " - bottom", TE / 4.0, 0.1, StringAlign.CenterCenter, true);
                }

                foreach (var a in DrillHoles)
                {
                    TopSolderMask.AddFlash(a, mountholediameter / 2.0 + 1.0);
                    BottomSolderMask.AddFlash(a, mountholediameter / 2.0 + 1.0);
                }

                Outline.Write(OutName + ".gko");
                TopSilk.Write(OutName + ".gto");
                TopCopper.Write(OutName + ".gtl");
                TopSolderMask.Write(OutName + ".gts");

                BottomSilk.Write(OutName + ".gbo");
                BottomCopper.Write(OutName + ".gbl");
                BottomSolderMask.Write(OutName + ".gbs");


                Files.Add(OutName + ".gko");
                Files.Add(OutName + ".gto");
                Files.Add(OutName + ".gtl");
                Files.Add(OutName + ".gts");
                Files.Add(OutName + ".gbo");
                Files.Add(OutName + ".gbl");
                Files.Add(OutName + ".gbs");
                GerberImageCreator GIC = new GerberImageCreator();
                GIC.AddBoardsToSet(Files);

                GIC.WriteImageFiles(OutName, 50);
                // Gerber.SaveGerberFileToImage(OutName + ".gko", OutName + ".gko.png", 200, Color.Black, Color.White);
                // Gerber.SaveGerberFileToImage(OutName + ".gto", OutName + ".gto.png", 200, Color.Black, Color.White);
            }
        }