Пример #1
0
 public void Complete()
 {
     Width  = Box.Width();
     Height = Box.Height();
 }
Пример #2
0
        public string MinimalDXFSave(string outputfile, double offset = 3.0, double holediameter = 3.2)
        {
            if (Directory.Exists(outputfile))
            {
                outputfile = Path.Combine(outputfile, "SickOfBeige");
            }
            List <String> Lines = new List <string>();

            Lines.Add("0");
            Lines.Add("SECTION");
            Lines.Add("2 ");
            Lines.Add("ENTITIES");

            PolyLine Biggest     = null;
            double   BiggestArea = 0;

            foreach (var a in PLSs.Where(x => x.Layer == BoardLayer.Outline))
            {
                foreach (var b in a.OutlineShapes)
                {
                    var A    = b.toPolygon();
                    var Area = Clipper.Area(A);
                    if (Area > BiggestArea)
                    {
                        Biggest     = b;
                        BiggestArea = Area;
                    }
                }
            }

            Polygons Offsetted = new Polygons();

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

                    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");
            }

            List <PointF> Holes      = new List <PointF>();
            var           holeradius = holediameter / 2.0;

            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(new PointF((float)h.X, (float)h.Y));
                            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 = h.X + C1;
                                double y1 = h.Y + S1;
                                double x2 = h.X + C2;
                                double y2 = h.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.PolyLineSet.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 = a.X - (float)holeradius;
                    R.Y = a.Y - (float)holeradius;
                    G.DrawEllipse(pen, R);
                }

                foreach (var poly in Offsetted)
                {
                    PolyLine Pl = new PolyLine();

                    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);
            }
        }