public string BuildGerber(GerberNumberFormat GNF, double rotation, bool isrotationparam, bool isscaled)
            {
                double getval = value;

                if (isscaled)
                {
                    getval = GNF._ScaleMMToFile(scaledvalue);
                }
                if (boundparam > -1)
                {
                    if (rotation != 0 && isrotationparam)
                    {
                        return(String.Format("{1}+${0}", boundparam + 1, Gerber.ToFloatingPointString(rotation).Replace(',', '.')));
                    }
                    else
                    {
                        return(String.Format("${0}", boundparam + 1));
                    }
                }
                else
                {
                    if (isrotationparam)
                    {
                        return(Gerber.ToFloatingPointString((getval + rotation)).Replace(',', '.'));
                    }
                    else
                    {
                        return(Gerber.ToFloatingPointString(getval).Replace(',', '.'));
                    }
                }
            }
        public string BuildGerber(GerberNumberFormat GNF, double rotationdegrees)
        {
            string res = "";

            switch (Type)
            {
            case ApertureMacroTypes.Equation:
                res += String.Format("{0}={1}", EquationTarget, EquationSource);
                break;

            case ApertureMacroTypes.Line:
            case ApertureMacroTypes.Line_2:
            {
                var Rot = RotateParams(Params[3], Params[4], rotationdegrees);

                res += String.Format("20,1,{0},{1},{2},{3},{4},{5}",
                                     //Params[1].BuildGerber(rotationdegrees, false),
                                     Params[2].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[3].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[4].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[5].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[6].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[7].BuildGerber(GNF, rotationdegrees, true, false)
                                     );
                break;
            }

            default:
                Console.WriteLine("Sorry - I cant make gerber for macro parts of type {0} yet", Type);
                break;

            case ApertureMacroTypes.Outline:
                res += String.Format("4,1,{0}," + Gerber.LineEnding, OutlineVertices.Count - 1);
                if (rotationdegrees != 0)
                {
                    PolyLine P = new PolyLine(PolyLine.PolyIDs.Aperture);
                    for (int i = 0; i < OutlineVertices.Count; i++)
                    {
                        PointD B = OutlineVertices[i].Get(Params);
                        P.Add(B.X, B.Y);
                    }
                    P.RotateDegrees(rotationdegrees);
                    for (int i = 0; i < P.Vertices.Count(); i++)
                    {
                        res += String.Format("{0},{1}," + Gerber.LineEnding, Gerber.ToFloatingPointString(GNF._ScaleMMToFile(P.Vertices[i].X)).Replace(',', '.'), Gerber.ToFloatingPointString(GNF._ScaleMMToFile(P.Vertices[i].Y)).Replace(',', '.'));
                    }
                }
                else
                {
                    for (int i = 0; i < OutlineVertices.Count; i++)
                    {
                        PointD B = OutlineVertices[i].Get(Params);

                        res += String.Format("{0},{1}," + Gerber.LineEnding, Gerber.ToFloatingPointString(GNF._ScaleMMToFile(B.X)).Replace(',', '.'), Gerber.ToFloatingPointString(GNF._ScaleMMToFile(B.Y)).Replace(',', '.'));
                    }
                }
                res += "0";
                break;

            case ApertureMacroTypes.Polygon:
            {
                var Rot = RotateParams(Params[3], Params[4], rotationdegrees);

                res += String.Format("5,1,{0},{1},{2},{3},{4}",
                                     //Params[1].BuildGerber(rotationdegrees, false),
                                     Params[2].BuildGerberInt(GNF, rotationdegrees, false),
                                     Rot[0].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[1].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[5].BuildGerber(GNF, rotationdegrees, false, false),
                                     Params[6].BuildGerber(GNF, rotationdegrees, true, false)
                                     );
                break;
            }

            case ApertureMacroTypes.Thermal:
            {
                var Rot = RotateParams(Params[1], Params[2], rotationdegrees);

                res += String.Format("7,1,{0},{1},{2},{3},{4},{5}",
                                     Rot[0].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[1].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[3].BuildGerber(GNF, rotationdegrees, false, false),
                                     Params[4].BuildGerber(GNF, rotationdegrees, false, false),
                                     Params[5].BuildGerber(GNF, rotationdegrees, false, false),
                                     Params[6].BuildGerber(GNF, rotationdegrees, true, false)
                                     );
                break;
            }

            case ApertureMacroTypes.CenterLine:
            {
                var Rot = RotateParams(Params[4], Params[5], rotationdegrees);
                res += String.Format("21,1,{0},{1},{2},{3},{4}",
                                     //Params[1].BuildGerber(rotationdegrees, false),
                                     Params[2].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[3].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[0].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[1].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[6].BuildGerber(GNF, rotationdegrees, true, false)
                                     );
            }
            break;

            case ApertureMacroTypes.LowerLeftLine:
            {
                var Rot = RotateParams(Params[4], Params[5], rotationdegrees);
                res += String.Format("21,1,{0},{1},{2},{3},{4}",
                                     //Params[1].BuildGerber(rotationdegrees, false),
                                     Params[2].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[3].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[0].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[1].BuildGerber(GNF, rotationdegrees, false, true),
                                     Params[6].BuildGerber(GNF, rotationdegrees, true, false)
                                     );
            }
            break;

            case ApertureMacroTypes.Circle:
                //res += "circ";
            {
                var Rot = RotateParams(Params[3], Params[4], rotationdegrees);

                res += String.Format("1,1,{0},{1},{2}",
                                     //Params[1].BuildGerber(rotationdegrees, false),
                                     Params[2].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[0].BuildGerber(GNF, rotationdegrees, false, true),
                                     Rot[1].BuildGerber(GNF, rotationdegrees, false, true)
                                     );
            }
            break;
            }
            foreach (var a in Params)
            {
            }
            res += "*" + Gerber.LineEnding;
            return(res);
        }
        public string BuildGerber(GerberNumberFormat format, string MacroNameSuffix = "", double RotationAngle = 0)
        {
            string res       = String.Format("%ADD{0}", ID.ToString("D2"));
            int    parmcount = 0;

            switch (ShapeType)
            {
            case GerberApertureShape.Macro:

                res += MacroName + MacroNameSuffix;
                if (MacroParamList.Count > 0)
                {
                    res += ",";
                }
                foreach (var a in MacroParamList)
                {
                    parmcount++;
                    if (parmcount >= 2)
                    {
                        res += "X";
                    }
                    res += Gerber.ToFloatingPointString(format._ScaleMMToFile(a)).Replace(',', '.');
                }
                break;

            case GerberApertureShape.Circle:
            {
                if (CircleRadius > 3)
                {
                    //Console.WriteLine("test");
                }
                res += "C," + Gerber.ToFloatingPointString(format._ScaleMMToFile(CircleRadius * 2)).Replace(',', '.');
            }
            break;

            case GerberApertureShape.Rectangle:
                if (RotationAngle == 0 || (int)RotationAngle == 180)
                {
                    res += "R," + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectWidth)).Replace(',', '.') + "X" + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectHeight)).Replace(',', '.');
                }
                else
                {
                    if ((int)RotationAngle == 90 || (int)RotationAngle == (-90) || (int)RotationAngle == (270))
                    {
                        res += "R," + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectHeight)).Replace(',', '.') + "X" + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectWidth)).Replace(',', '.');
                    }
                    else
                    {
                        string macroname = "REC" + (MacroPostFix++).ToString();

                        PolyLine Rect = new PolyLine(PolyLine.PolyIDs.Temp);
                        Rect.MakeRectangle(RectWidth, RectHeight);
                        Rect.RotateDegrees(RotationAngle);
                        res = Gerber.BuildOutlineApertureMacro(macroname, Rect.Vertices, format) + res;

                        res += macroname;
                        if (Gerber.ShowProgress)
                        {
                            Console.WriteLine("generated rotated rect: ");
                        }
                        if (Gerber.ShowProgress)
                        {
                            Console.WriteLine(res);
                        }
                    }
                }
                break;

            case GerberApertureShape.Polygon:
                if (RotationAngle == 0)
                {
                    res += "P," + Gerber.ToFloatingPointString(format._ScaleMMToFile(NGonRadius * 2)).Replace(',', '.') + "X" + NGonSides.ToString() + "X" + Gerber.ToFloatingPointString(NGonRotation).Replace(',', '.');
                }
                else
                {
                    double newangle = NGonRotation + RotationAngle;
                    while (newangle < 0)
                    {
                        newangle += 360;
                    }
                    while (newangle > 360)
                    {
                        newangle -= 360;
                    }
                    res += "P," + Gerber.ToFloatingPointString(format._ScaleMMToFile(NGonRadius * 2)).Replace(',', '.') + "X" + NGonSides.ToString() + "X" + Gerber.ToFloatingPointString(newangle).Replace(',', '.');
                    if (Gerber.ShowProgress)
                    {
                        Console.WriteLine("generated rotated NGon: ");
                    }
                    if (Gerber.ShowProgress)
                    {
                        Console.WriteLine(res);
                    }
                }
                break;

            case GerberApertureShape.OBround:
                if (RotationAngle == 0 || Math.Abs(Math.Abs(RotationAngle) - 180) < 0.01)
                {
                    res += "O," + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectWidth)).Replace(',', '.') + "X" + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectHeight)).Replace(',', '.');
                }
                else
                {
                    if (Math.Abs(Math.Abs(RotationAngle) - 90) < 0.01)
                    {
                        res += "O," + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectHeight)).Replace(',', '.') + "X" + Gerber.ToFloatingPointString(format._ScaleMMToFile(RectWidth)).Replace(',', '.');
                    }
                    else
                    {
                        string macroname = "OBR" + (MacroPostFix++).ToString();


                        PolyLine Obround = new PolyLine(PolyLine.PolyIDs.Temp);

                        Obround.SetObround(RectWidth, RectHeight);
                        Obround.RotateDegrees(RotationAngle);

                        res  = Gerber.BuildOutlineApertureMacro(macroname, Obround.Vertices, format) + res;
                        res += macroname;


                        if (Gerber.ShowProgress)
                        {
                            Console.WriteLine("generated rotated obround: ");
                        }
                        if (Gerber.ShowProgress)
                        {
                            Console.WriteLine(res);
                        }
                    }
                }
                break;

            case GerberApertureShape.Compound:
            {
                string macroname = "COMP" + (MacroPostFix++).ToString() + MacroNameSuffix;
                string macrores  = Gerber.WriteMacroStart(macroname);
                foreach (var P in Parts)
                {
                    PolyLine Comp = new PolyLine(PolyLine.PolyIDs.Temp);

                    foreach (var a in P.Shape.Vertices)

                    {
                        Comp.Add(a.X, a.Y);
                    }
                    Comp.Close();
                    Comp.RotateDegrees(RotationAngle);

                    macrores += Gerber.WriteMacroPartVertices(Comp.Vertices, format);
                }

                macrores += Gerber.WriteMacroEnd();
                res       = macrores + res;
                res      += macroname;


                if (Parts.Count > 1)
                {
                    Console.WriteLine("Number of parts: {0} but only 1 written", Parts.Count);
                    Console.WriteLine(res);
                }

                // res += MacroName;
                if (MacroParamList.Count > 0)
                {
                    res += ",";
                }
                foreach (var a in MacroParamList)
                {
                    parmcount++;
                    if (parmcount >= 2)
                    {
                        res += "X";
                    }
                    res += Gerber.ToFloatingPointString(a).Replace(',', '.');
                }


                if (Gerber.ShowProgress)
                {
                    Console.WriteLine("generated rotated compound macro shape: ");
                }
                if (Gerber.ShowProgress)
                {
                    Console.WriteLine(res);
                }
            }
            break;

            default:
                Console.WriteLine("I don't know how to generate the source for this aperture yet.");
                break;
            }

            res += "*%";

            return(res);
        }