public void Write(string p, double inWidth = 0.0)
        {
            List <string> lines = new List <string>();

            GerberNumberFormat GNF = new GerberNumberFormat();

            GNF.DigitsBefore = 3;
            GNF.DigitsAfter  = 6;
            GNF.SetImperialMode();

            lines.Add(Gerber.INCH);
            lines.Add("%OFA0B0*%");
            lines.Add(GNF.BuildGerberFormatLine());
            lines.Add("%IPPOS*%");
            lines.Add("%LPD*%");
            GerberApertureType Apt = new GerberApertureType();

            Apt.SetCircle(inWidth);
            Apt.ID = 10;
            lines.Add(Apt.BuildGerber(GNF));
            lines.Add(Apt.BuildSelectGerber());
            foreach (var a in PolyLines)
            {
                lines.Add(Gerber.MoveTo(a.Vertices[a.Vertices.Count - 1], GNF));
                for (int i = 0; i < a.Vertices.Count; i++)
                {
                    lines.Add(Gerber.LineTo(a.Vertices[i], GNF));
                }
            }
            lines.Add(Gerber.EOF);
            Gerber.WriteAllLines(p, lines);
        }
Esempio n. 2
0
        // Simplify the aperture macro.
        public static bool SimplifyApertureMacro(ApertureDefinition aperture, double scale)
        {
            const int extraStackSize     = 10;
            bool      success            = true;
            int       numberOfParameters = 0;
            bool      clearOperatorUsed  = false;

            double[]                localParameters = new double[Gerber.MaximumApertureParameters]; // Local copy of parameters.
            double[]                tmp             = { 0.0, 0.0 };
            int                     index           = 0;
            GerberApertureType      type            = GerberApertureType.None;
            SimplifiedApertureMacro macro;

            if (aperture == null || aperture.ApertureMacro == null)
            {
                throw new GerberApertureException("In SimplifyApertureMacro, aperture = null");
            }

            // Allocate stack for VM.
            MacroStack.InitializeStack(aperture.ApertureMacro.NufPushes + extraStackSize);

            // Make a copy of the parameter list that we can rewrite if necessary.
            localParameters = new double[Gerber.MaximumApertureParameters];
            foreach (double p in aperture.Parameters)
            {
                localParameters[index++] = p;
            }

            foreach (GerberInstruction instruction in aperture.ApertureMacro.InstructionList)
            {
                switch (instruction.Opcode)
                {
                case GerberOpcodes.NOP:
                    break;

                case GerberOpcodes.Push:
                    MacroStack.Push(instruction.Data.DoubleValue);
                    break;

                case GerberOpcodes.PushParameter:
                    MacroStack.Push(localParameters[instruction.Data.IntValue - 1]);
                    break;

                case GerberOpcodes.PopParameter:
                    MacroStack.Pop(ref tmp[0]);
                    localParameters[instruction.Data.IntValue - 1] = tmp[0];
                    break;

                case GerberOpcodes.Add:
                    MacroStack.Pop(ref tmp[0]);
                    MacroStack.Pop(ref tmp[1]);
                    MacroStack.Push(tmp[1] + tmp[0]);
                    break;

                case GerberOpcodes.Subtract:
                    MacroStack.Pop(ref tmp[0]);
                    MacroStack.Pop(ref tmp[1]);
                    MacroStack.Push(tmp[1] - tmp[0]);
                    break;

                case GerberOpcodes.Multiple:
                    MacroStack.Pop(ref tmp[0]);
                    MacroStack.Pop(ref tmp[1]);
                    MacroStack.Push(tmp[1] * tmp[0]);
                    break;

                case GerberOpcodes.Divide:
                    MacroStack.Pop(ref tmp[0]);
                    MacroStack.Pop(ref tmp[1]);
                    MacroStack.Push(tmp[1] / tmp[0]);
                    break;

                case GerberOpcodes.Primative:
                    // This handles the exposure thing in the aperture macro.
                    // The exposure is always the first element on stack independent
                    // of aperture macro.
                    switch (instruction.Data.IntValue)
                    {
                    case 1:
                        //Debug.Write("    Aperture macro circle [1] (");
                        type = GerberApertureType.MacroCircle;
                        numberOfParameters = 4;
                        break;

                    case 3:
                        break;          // EOF.

                    case 4:
                        //Debug.Write("    Aperture macro outline [4] (");
                        type = GerberApertureType.MacroOutline;
                        // Number of parameters are:
                        // Number of points defined in entry 1 of the stack + start point. Times two since it is both X and Y.
                        // Then three more; exposure, nuf points and rotation.
                        numberOfParameters = ((int)MacroStack.Values[1] + 1) * 2 + 3;
                        break;

                    case 5:
                        //Debug.Write("    Aperture macro polygon [5] (");
                        type = GerberApertureType.MacroPolygon;
                        numberOfParameters = 6;
                        break;

                    case 6:
                        //Debug.WriteLine("    Aperture macro moiré [6] (");
                        type = GerberApertureType.MacroMoire;
                        numberOfParameters = 9;
                        break;

                    case 7:
                        //Debug.Write("    Aperture macro thermal [7] (");
                        type = GerberApertureType.MacroThermal;
                        numberOfParameters = 6;
                        break;

                    case 2:
                    case 20:
                        //Debug.Write("    Aperture macro line 20/2 (");
                        type = GerberApertureType.MacroLine20;
                        numberOfParameters = 7;
                        break;

                    case 21:
                        //Debug.Write("    Aperture macro line 21 (");
                        type = GerberApertureType.MarcoLine21;
                        numberOfParameters = 6;
                        break;

                    case 22:
                        //Debug.Write("    Aperture macro line 22 (");
                        type = GerberApertureType.MacroLine22;
                        numberOfParameters = 6;
                        break;

                    default:
                        success = false;
                        break;
                    }

                    if (type != GerberApertureType.None)
                    {
                        if (numberOfParameters > Gerber.MaximumApertureParameters)
                        {
                            throw new GerberApertureException("Too many parameters for aperture macro;");
                            // GERB_COMPILE_ERROR("Number of parameters to aperture macro are more than gerbv is able to store\n");
                        }

                        // Create a new simplified aperture macro and start filling in the blanks.
                        macro = new SimplifiedApertureMacro();
                        macro.ApertureType = type;
                        index = 0;
                        for (int i = 0; i < numberOfParameters; i++)
                        {
                            macro.Parameters[i] = MacroStack.Values[i];
                        }

                        // Convert any mm values to inches.
                        switch (type)
                        {
                        case GerberApertureType.MacroCircle:
                            if (Math.Abs(macro.Parameters[0]) < 0.001)
                            {
                                clearOperatorUsed = true;
                            }

                            macro.Parameters[1] /= scale;
                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            break;

                        case GerberApertureType.MacroOutline:
                            if (Math.Abs(macro.Parameters[0]) < 0.001)
                            {
                                clearOperatorUsed = true;
                            }

                            for (int i = 2; i < numberOfParameters - 1; i++)
                            {
                                macro.Parameters[i] /= scale;
                            }

                            break;

                        case GerberApertureType.MacroPolygon:
                            if (Math.Abs(macro.Parameters[0]) < 0.001)
                            {
                                clearOperatorUsed = true;
                            }

                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            macro.Parameters[4] /= scale;
                            break;

                        case GerberApertureType.MacroMoire:
                            macro.Parameters[0] /= scale;
                            macro.Parameters[1] /= scale;
                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            macro.Parameters[4] /= scale;
                            macro.Parameters[6] /= scale;
                            macro.Parameters[7] /= scale;
                            break;

                        case GerberApertureType.MacroThermal:
                            macro.Parameters[0] /= scale;
                            macro.Parameters[1] /= scale;
                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            macro.Parameters[4] /= scale;
                            break;

                        case GerberApertureType.MacroLine20:
                            if (Math.Abs(macro.Parameters[0]) < 0.001)
                            {
                                clearOperatorUsed = true;
                            }

                            macro.Parameters[1] /= scale;
                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            macro.Parameters[4] /= scale;
                            macro.Parameters[5] /= scale;
                            break;

                        case GerberApertureType.MarcoLine21:
                        case GerberApertureType.MacroLine22:
                            if (Math.Abs(macro.Parameters[0]) < 0.001)
                            {
                                clearOperatorUsed = true;
                            }

                            macro.Parameters[1] /= scale;
                            macro.Parameters[2] /= scale;
                            macro.Parameters[3] /= scale;
                            macro.Parameters[4] /= scale;
                            break;

                        default:
                            break;
                        }

                        aperture.SimplifiedMacroList.Add(macro);
                        //for (int i = 0; i < numberOfParameters; i++)
                        //    Debug.Write(String.Format("{0}, ", MacroStack.Values[i]));

                        //Debug.WriteLine(")");
                    }

                    // Here we reset the stack pointer. It's not generally
                    // correct to do this, but since I know how the compiler works
                    // I can do this. The correct way to do this should be to
                    // subtract number of used elements in each primitive operation.
                    MacroStack.Reset();
                    break;

                default:
                    break;
                }
            }

            // Store a flag to let the renderer know if it should expect any "clear" primatives.
            aperture.Parameters[0] = clearOperatorUsed ? 1.0f : 0.0f;
            return(success);
        }
Esempio n. 3
0
        private void SelectionListFrm_Load(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
            selectionText.Append("File: " + selectionInfo.Filename);
            foreach (GerberNet net in selectionInfo.SelectedNetList)
            {
                if (net.ApertureState == GerberApertureState.On)
                {
                    switch (net.Interpolation)
                    {
                    case GerberInterpolation.PolygonAreaStart:
                        selectionText.Append(Environment.NewLine);
                        selectionText.Append("Object type: Polygon");
                        break;

                    case GerberInterpolation.LinearX001:
                    case GerberInterpolation.LinearX01:
                    case GerberInterpolation.LinearX1:
                    case GerberInterpolation.LinearX10:
                        if (net.BoundingBox != null)
                        {
                            selectionText.Append(Environment.NewLine);
                            selectionText.Append(Environment.NewLine);
                            selectionText.Append("Object type: Line" + Environment.NewLine);
                            apertureNumber = net.Aperture;
                            selectionText.Append("  Aperture used: " + "D" + apertureNumber.ToString() + Environment.NewLine);
                            apertureType = selectionInfo.SelectionImage.ApertureArray[apertureNumber].ApertureType;
                            selectionText.Append("  Aperture type: " + apertureType.ToString() + Environment.NewLine);
                            parameter0 = selectionInfo.SelectionImage.ApertureArray[apertureNumber].Parameters[0] * 1000;
                            selectionText.Append("  Diameter: " + parameter0.ToString("0.0") + Environment.NewLine);
                            x = net.StartX * 1000;
                            y = net.StartY * 1000;
                            PointD start = new PointD(x, y);
                            selectionText.Append("  Start: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                            selectionText.Append(Environment.NewLine);
                            x = net.StopX * 1000;
                            y = net.StopY * 1000;
                            PointD stop = new PointD(x, y);
                            selectionText.Append("  Stop: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                            selectionText.Append(Environment.NewLine);
                            double length = GetLineLength(start, stop);
                            selectionText.Append("  Length: " + length);
                            selectionText.Append(Environment.NewLine);
                            selectionText.Append("  Level Name: ");
                            if (net.Level.LevelName == String.Empty)
                            {
                                selectionText.Append("<Unnamed Level>");
                            }

                            else
                            {
                                selectionText.Append(net.Level.LevelName);
                            }

                            selectionText.Append(Environment.NewLine);
                            selectionText.Append("  Net Label: ");
                            if (net.Label == String.Empty)
                            {
                                selectionText.Append("<Unlabeled Net>");
                            }

                            else
                            {
                                selectionText.Append(net.Label);
                            }
                        }
                        break;

                    case GerberInterpolation.ClockwiseCircular:
                    case GerberInterpolation.CounterClockwiseCircular:
                        selectionText.Append(Environment.NewLine);
                        selectionText.Append(Environment.NewLine);
                        selectionText.Append("Object type: Arc" + Environment.NewLine);
                        apertureNumber = net.Aperture;
                        selectionText.Append("  Aperture used: " + "D" + apertureNumber.ToString() + Environment.NewLine);
                        apertureType = selectionInfo.SelectionImage.ApertureArray[apertureNumber].ApertureType;
                        selectionText.Append("  Aperture type: " + apertureType.ToString() + Environment.NewLine);
                        parameter0 = selectionInfo.SelectionImage.ApertureArray[apertureNumber].Parameters[0] * 1000;
                        selectionText.Append("  Diameter: " + parameter0.ToString("0.0") + Environment.NewLine);
                        x = net.StartX * 1000;
                        y = net.StartY * 1000;
                        selectionText.Append("  Start: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                        selectionText.Append(Environment.NewLine);
                        x = net.StopX * 1000;
                        y = net.StopY * 1000;
                        selectionText.Append("  Stop: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                        selectionText.Append(Environment.NewLine);
                        x = net.CircleSegment.CenterX * 1000;
                        y = net.CircleSegment.CenterY * 1000;
                        selectionText.Append("  Centre: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                        selectionText.Append(Environment.NewLine);
                        x = net.CircleSegment.StartAngle;
                        y = net.CircleSegment.EndAngle;
                        selectionText.Append("  Angles [Deg]: (" + x.ToString("0.000") + ", " + y.ToString("0.000") + ")");
                        selectionText.Append(Environment.NewLine);
                        selectionText.Append("  Direction: ");
                        selectionText.Append(net.Interpolation == GerberInterpolation.ClockwiseCircular ? "CW" : "CCW");
                        selectionText.Append(Environment.NewLine);
                        selectionText.Append("  Level Name: ");
                        if (net.Level.LevelName == String.Empty)
                        {
                            selectionText.Append("<Unnamed Level>");
                        }

                        else
                        {
                            selectionText.Append(net.Level.LevelName);
                        }

                        selectionText.Append(Environment.NewLine);
                        selectionText.Append("  Net Label: ");
                        if (net.Label == String.Empty)
                        {
                            selectionText.Append("<Unlabeled Net>");
                        }

                        else
                        {
                            selectionText.Append(net.Label);
                        }

                        break;
                    }
                }

                if (net.ApertureState == GerberApertureState.Flash)
                {
                    apertureNumber = net.Aperture;
                    selectionText.Append(Environment.NewLine);
                    selectionText.Append(Environment.NewLine);
                    selectionText.Append("Object type: Flashed Aperture" + Environment.NewLine);
                    selectionText.Append("  Aperture used: " + "D" + apertureNumber.ToString() + Environment.NewLine);
                    apertureType = selectionInfo.SelectionImage.ApertureArray[apertureNumber].ApertureType;
                    if (apertureType != GerberApertureType.Macro)
                    {
                        selectionText.Append("  Aperture type: " + apertureType.ToString() + Environment.NewLine);
                        switch (apertureType)
                        {
                        case GerberApertureType.Circle:
                            parameter0 = selectionInfo.SelectionImage.ApertureArray[apertureNumber].Parameters[0] * 1000;
                            selectionText.Append("  Diameter: " + parameter0.ToString("0.0") + Environment.NewLine);
                            break;

                        case GerberApertureType.Rectangle:
                        case GerberApertureType.Oval:
                            parameter0 = selectionInfo.SelectionImage.ApertureArray[apertureNumber].Parameters[0] * 1000;
                            parameter1 = selectionInfo.SelectionImage.ApertureArray[apertureNumber].Parameters[1] * 1000;
                            selectionText.Append("  Dimension: " + parameter0.ToString("0.0") + " x " + parameter1.ToString("0.0") + Environment.NewLine);
                            break;
                        }
                    }

                    else
                    {
                        if (selectionInfo.SelectionImage.ApertureArray[apertureNumber].ApertureMacro != null)
                        {
                            apertureType = selectionInfo.SelectionImage.ApertureArray[apertureNumber].SimplifiedMacroList[0].ApertureType;
                            selectionText.Append("  Aperture type: " + apertureType.ToString() + Environment.NewLine);
                        }
                    }

                    x = net.StopX * 1000;
                    y = net.StopY * 1000;
                    selectionText.Append("  Location: (" + x.ToString("0.0") + ", " + y.ToString("0.0") + ")");
                    selectionText.Append(Environment.NewLine);
                    selectionText.Append("  Level Name: ");
                    if (net.Level.LevelName == String.Empty)
                    {
                        selectionText.Append("<Unnamed Level>");
                    }

                    else
                    {
                        selectionText.Append(net.Level.LevelName);
                    }

                    selectionText.Append(Environment.NewLine);
                    selectionText.Append("  Net Label: ");
                    if (net.Label == String.Empty)
                    {
                        selectionText.Append("<Unlabeled Net>");
                    }

                    else
                    {
                        selectionText.Append(net.Label);
                    }
                }
            }

            textBox1.Text            = selectionText.ToString();
            textBox1.SelectionStart  = 0;
            textBox1.SelectionLength = 0;
        }