예제 #1
0
        private void button3_Click_1(object sender, EventArgs e) // Save result
        {
            string TextFileName;

            if (radioButton1.Checked)
            {
                int indSlash = -1;
                int indDot   = -1;
                for (int i = 0; i < FileName.Length; i++)
                {
                    if (FileName[i] == '\\')
                    {
                        indSlash = i;
                    }
                    if (FileName[i] == '.')
                    {
                        indDot = i;
                    }
                }
                char[] Path1 = new char[100];

                FileName.CopyTo(indSlash + 1, Path1, 0, indDot - indSlash - 1);

                string Name = new String(Path1, 0, indDot - indSlash - 1);
                TextFileName = "C:\\VC_Projects\\TEXT\\Bike_.txt";
                TextFileName = TextFileName.Insert(TextFileName.IndexOf(".txt"), Name);
            }
            else
            {
                SaveFileDialog dialog = new SaveFileDialog();
                MessageBox.Show("Save circles: Do not open an image in the comming window; look for the directory 'TEXT' and click a file " +
                                " or write a name with extension '.txt' in the line below.");

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    TextFileName = dialog.FileName;
                }
                else
                {
                    TextFileName = "";
                }
            }

            string[] dir = new string[3];
            dir[0] = "right";
            dir[2] = "left";

            // Create a file to write to:
            using (StreamWriter sw = File.CreateText(TextFileName)) // creating the file
            {
                sw.WriteLine("Bike going to " + dir[Dir] + " has elliptical wheels:");
                sw.WriteLine("First:  a= " + (int)ListEllipse[0].a + "; b= " + (int)ListEllipse[0].b + "; c=" +
                             (int)ListEllipse[0].c + "; d=" + (int)ListEllipse[0].d + ".");
                sw.WriteLine("Second: a=" + (int)ListEllipse[1].a + "; b=" + (int)ListEllipse[1].b + "; c=" +
                             (int)ListEllipse[1].c + "; d=" + (int)ListEllipse[1].d + ".");
            }

            // Open the file to read from.
            using (StreamReader sr = File.OpenText(TextFileName))
            {
                MessageBox.Show("TextFileName=" + TextFileName + ".  Contents of the saved text file:");
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    MessageBox.Show(s);
                }
            }
        } //********************************** end Save result **************************************************************
        } //*************************** end Click for "DrawComb" **********************************

        private void button4_Click(object sender, EventArgs e) // Save circles
        {
            if (!POLY)
            {
                MessageBox.Show("Click the button 'Polygons'");
                return;
            }
            string TextFileName;

            if (!radioButton3.Checked && !radioButton4.Checked)
            {
                if (MessReturn("Save circles: please click one of the the radio buttons at right side and click 'Save circles' again.") < 0)
                {
                    return;
                }
                return;
            }

            if (radioButton3.Checked) // automatic
            {
                int indSlash = -1;
                int indDot   = -1;
                for (int i = 0; i < OpenFileName.Length; i++)
                {
                    if (OpenFileName[i] == '\\')
                    {
                        indSlash = i;                     // Position of the last '\'
                    }
                    if (OpenFileName[i] == '.')
                    {
                        indDot = i;                    // Position of the last '.'
                    }
                }
                char[] Path1 = new char[100];

                OpenFileName.CopyTo(indSlash + 1, Path1, 0, indDot - indSlash - 1); //Path1 must be char[], no string
                //Copies indDot-indSlash-1 charakters from the pos. (indSlash+1) in 'OpenFileName' to 'Path1'


                string Name = new String(Path1, 0, indDot - indSlash - 1);
                // Takes indDot-IS-1 characters starting at the beginning of 'Path1'

                TextFileName = "C:\\VC_Projects\\TEXT\\CirclesOf_.txt";                 // New path for the text file

                TextFileName = TextFileName.Insert(TextFileName.IndexOf(".txt"), Name); //sets Name for ".txt"
            }
            else // radioButton4.Checked; defined:
            {
                SaveFileDialog dialog = new SaveFileDialog();
                MessageBox.Show("Save circles: Do not open an image in the comming window; look for the directory 'TEXT' and click a file " +
                                " or write a name with extension '.txt' in the line below.");

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    TextFileName = dialog.FileName;
                }
                else
                {
                    TextFileName = "";
                }
            } //---------------------------- end if (radioButton3.Checked) -------------------------------------

            // Create a file to write to:
            using (StreamWriter sw = File.CreateText(TextFileName)) // creating the file
            {
                if (radioButton3.Checked)
                {
                    sw.WriteLine("List of " + nBest + " recognized circles");
                    if (radioButton1.Checked)
                    {
                        for (int ic = 0; ic < nBest; ic++)
                        {
                            sw.WriteLine("Circle " + ic + " Center (Mx=" + Math.Round(BestCircles1[ic].Mx, 1) + "; My=" +
                                         Math.Round(BestCircles1[ic].My, 1) + "); Radius R=" + Math.Round(BestCircles1[ic].R, 1) + ")");
                        }
                    }
                    else // radioButton2.Checked
                    {
                        for (int ic = 0; ic < nBest; ic++)
                        {
                            sw.WriteLine("Circle " + ic + " Center (Mx=" + Math.Round(BestCircles2[ic].Mx, 1) + "; My=" +
                                         Math.Round(BestCircles2[ic].My, 1) + "); Radius R=" + Math.Round(BestCircles2[ic].R, 1) + ")");
                        }
                    }
                    if (MessReturn("List of " + nBest + " circles saved in the text file " + TextFileName) < 0)
                    {
                        return;
                    }
                    sw.Close();
                }
                else // radioButton4.Checked
                {
                    sw.WriteLine("List of " + nBest + " recognized circles");
                    if (radioButton1.Checked)
                    {
                        for (int ic = 0; ic < nBest; ic++)
                        {
                            sw.WriteLine("Circle " + ic + " Center (Mx=" + Math.Round(BestCircles1[ic].Mx, 1) + "; My=" +
                                         Math.Round(BestCircles1[ic].My, 1) + "); Radius R=" + Math.Round(BestCircles1[ic].R, 1) + ".");
                        }
                    }
                    else // radioButton2.Checked
                    {
                        for (int ic = 0; ic < nBest; ic++)
                        {
                            sw.WriteLine("Circle " + ic + " Center (Mx=" + Math.Round(BestCircles2[ic].Mx, 1) + "; My=" +
                                         Math.Round(BestCircles2[ic].My, 1) + "); Radius R=" + Math.Round(BestCircles2[ic].R, 1) + ".");
                        }
                    }

                    sw.Close();
                    if (MessReturn("List of " + nBest + " circles saved in the text file " + TextFileName) < 0)
                    {
                        return;
                    }
                }

                // Open the file to read from.
                using (StreamReader sr = File.OpenText(TextFileName))
                {
                    if (MessReturn("Contents of the saved text file:") < 0)
                    {
                        return;
                    }
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        if (MessReturn(s) < 0)
                        {
                            return;
                        }
                    }
                }
            } //*************************** end using ****************************************
        }     //**************************** end Save circles ***********************************