/*
         * public void PuntosExtremos()
         * {
         *  Mecha mechaAux1 = (Mecha)listaMechas[0];
         *  Mecha mechaAux2 = (Mecha)listaMechas[1];
         *  foreach (Agujero agujero in listaAgujeros)
         *  {
         *      if (agujero.mecha.nombre == mechaAux1.nombre)
         *      {
         *          if (punto1.x < agujero.x || (punto1.x == agujero.x && punto1.y < agujero.y))
         *          {
         *              punto1 = agujero;
         *          }
         *      }
         *      if (agujero.mecha.nombre == mechaAux1.nombre || agujero.mecha.nombre == mechaAux2.nombre)
         *      {
         *          if (punto2.y < agujero.y)
         *          {
         *              punto2 = agujero;
         *          }
         *          else if (punto2.x == agujero.x && punto2.y < agujero.y)
         *          {
         *              punto2 = agujero;
         *          }
         *      }
         *  }
         *  CuadroTexto.Text += "Puntos para calibración elegidos" + Environment.NewLine;
         * }
         */
        public void PuntosExtremosMech()
        {
            float diametroMax = ((Mecha)listaMechas[0]).diametro;

            if (listaMechas.Count >= 2)
            {
                if (((Mecha)listaMechas[1]).diametro <= 2 * diametroMax)
                {
                    diametroMax = ((Mecha)listaMechas[1]).diametro;
                }
            }

            extremoMin   = new Agujero(esquinas[2].cadena);
            extremoMin.x = float.Parse(extremoMin.xy.Substring(1, 7)) / CORRECCIONMECHA;
            extremoMin.y = float.Parse(extremoMin.xy.Substring(9, 7)) / CORRECCIONMECHA;
            extremoMax   = new Agujero(esquinas[0].cadena);
            extremoMax.x = float.Parse(extremoMax.xy.Substring(1, 7)) / CORRECCIONMECHA;
            extremoMax.y = float.Parse(extremoMax.xy.Substring(9, 7)) / CORRECCIONMECHA;

            foreach (Agujero agujero in listaAgujeros)
            {
                if (agujero.x <= extremoMin.x && agujero.y <= extremoMin.y && (agujero.mecha.diametro <= diametroMax))
                {
                    extremoMin = agujero;
                }
                if (agujero.x >= extremoMax.x && agujero.y >= extremoMax.y && (agujero.mecha.diametro <= diametroMax))
                {
                    extremoMax = agujero;
                }
            }
        }
 // Envia posicion de agujero
 public void EnviarAgujero(Agujero agujero)
 {
     Enviar(Convertir_xy_int_a_string(agujero.x, agujero.y));    // Envia posicion de cambio de mecha para bajar y hacer prueba de altura
 }
        // Metodo de lectura del archivo en cuestion
        public void Leer_Archivo(string path)
        {
            string[] lineasArchivo = null;
            try
            {
                System.IO.File.OpenRead(path);
                lineasArchivo = System.IO.File.ReadAllLines(path);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("No se pudo abrir el archivo");
                return;
            }
            catch (ArgumentException e)
            {
                return; // No se seleccionó un archivo
            }

            /// Escritura de la lista de mechas
            int indexArchivo = 0;

            while (!lineasArchivo[indexArchivo].Contains("%"))
            {
                if (lineasArchivo[indexArchivo].Substring(0, 1) == "T")
                {
                    listaMechas.Add(new Mecha(lineasArchivo[indexArchivo].Substring(0, 3),
                                              float.Parse(lineasArchivo[indexArchivo].Substring(4, 6)) / CORRECCIONMECHA,
                                              lineasArchivo[indexArchivo].Substring(4, 6)));
                }
                indexArchivo++;
            }
            Mecha mechaAux  = new Mecha();
            Mecha mechaComp = new Mecha();

            for (int i = 0; i < listaMechas.Count; i++)
            {
                for (int j = i + 1; j < listaMechas.Count; j++)
                {
                    mechaAux  = (Mecha)listaMechas[i];
                    mechaComp = (Mecha)listaMechas[j];

                    if (mechaAux.diametro > mechaComp.diametro)
                    {
                        listaMechas[j] = mechaAux;
                        listaMechas[i] = mechaComp;
                    }
                }
            }

            CuadroTexto.Text += "Mechas cargadas: " + listaMechas.Count + Environment.NewLine + Environment.NewLine;

            int indexMecha = -1;

            while (indexArchivo != lineasArchivo.Length)
            {
                if (lineasArchivo[indexArchivo].Substring(0, 1) == "T")
                {
                    indexMecha++;
                }
                if (lineasArchivo[indexArchivo].Substring(0, 1) == "X")            // Busca agujeros a realizar por esa mecha
                {
                    Agujero agujero = new Agujero
                    {
                        x    = float.Parse(lineasArchivo[indexArchivo].Substring(1, lineasArchivo[indexArchivo].IndexOf('Y', 1) - 1)) / CORRECCIONMECHA,
                        y    = float.Parse(lineasArchivo[indexArchivo].Substring(lineasArchivo[indexArchivo].IndexOf('Y', 1) + 1, lineasArchivo[indexArchivo].Length - (lineasArchivo[indexArchivo].IndexOf('Y', 1) + 1))) / CORRECCIONMECHA,
                        xStr = lineasArchivo[indexArchivo].Substring(1, lineasArchivo[indexArchivo].IndexOf('Y', 1) - 1),
                        yStr = lineasArchivo[indexArchivo].Substring(lineasArchivo[indexArchivo].IndexOf('Y', 1) + 1, lineasArchivo[indexArchivo].Length - (lineasArchivo[indexArchivo].IndexOf('Y', 1) + 1))
                    };
                    agujero.mecha = new Mecha
                    {
                        diametro = ((Mecha)listaMechas[indexMecha]).diametro,
                        nombre   = ((Mecha)listaMechas[indexMecha]).nombre
                    };
                    if (agujero.xStr.Length - 1 < 6)
                    {
                        string ceros      = null;
                        int    largoCoord = agujero.xStr.Length - 1;
                        for (int j = 0; j < 6 - largoCoord; j++)
                        {
                            ceros += '0';
                        }
                        agujero.xStr = agujero.xStr.Substring(0, 1) + ceros + agujero.xStr.Substring(1, agujero.xStr.Length - 1);
                    }
                    if (agujero.yStr.Length - 1 < 6)
                    {
                        string ceros      = null;
                        int    largoCoord = agujero.yStr.Length - 1;
                        for (int j = 0; j < 6 - largoCoord; j++)
                        {
                            ceros += '0';
                        }
                        agujero.yStr = agujero.yStr.Substring(0, 1) + ceros + agujero.yStr.Substring(1, agujero.yStr.Length - 1);
                    }
                    agujero.xy = "X" + agujero.xStr + "Y" + agujero.yStr;
                    //agujero.xy = Convertir_xy_int_a_string(agujero.x, agujero.y);
                    listaAgujeros.Add(agujero);
                }
                indexArchivo++;
            }

            CuadroTexto.Text += "Listas Terminadas" + Environment.NewLine;
            PuntosExtremosMech();
            CuadroTexto.Text += "Puntos para calibración guardados" + Environment.NewLine + Environment.NewLine;
        }