//时间：20200619
//作者：郝爽
//功能：测量线程的测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MeasureData;
using MeasureThread;

namespace HOZProject
{
    public partial class FormMeasureTest : Form
    {
        public MeasureFile m_MeasureFile;
        public Measure m_Ms;

        public FormMeasureTest()
        {
            InitializeComponent();
        }

        //新建测量文件
        private void button1_Click(object sender, EventArgs e)
        {
            m_MeasureFile = new MeasureFile();
            if (!m_MeasureFile.New())
            {
                return;
            }
            else
            {                
                this.IsPTcheckBox.Checked = m_MeasureFile.MParam.PT;
                MessageBox.Show("新建测量文件成功。");
            }
        }

        //初始化
        private void Init_Click(object sender, EventArgs e)
        {

        }

        //导入切孔
        private void LoadCutHole_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                if (!m_MeasureFile.GetCutHolesFromFile(""))
                {
                    MessageBox.Show("导入切孔失败");
                }

                List<CutHole> listHoles = m_MeasureFile.ListCutHole;
                foreach (CutHole hole in listHoles)
                {                    
                    //在CutHoleGridView中，添加切孔信息
                    int index = this.CutHoleGridView.Rows.Add();
                    this.CutHoleGridView.Rows[index].Cells[0].Value = hole.HoleName;

                    SemPosition pos = hole.Position;
                    this.CutHoleGridView.Rows[index].Cells[1].Value = pos.X;
                    this.CutHoleGridView.Rows[index].Cells[2].Value = pos.Y;
                    this.CutHoleGridView.Rows[index].Cells[3].Value = pos.Z;
                    this.CutHoleGridView.Rows[index].Cells[4].Value = pos.M;
                    this.CutHoleGridView.Rows[index].Cells[5].Value = pos.R;
                    this.CutHoleGridView.Rows[index].Cells[6].Value = pos.T;                  
                }
            }
        }

        private void IsPTcheckBox_CheckedChanged(object sender, EventArgs e)
        {
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           
        }

        //测量参数
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                m_MeasureFile.MParam.PT = this.IsPTcheckBox.Checked;
                m_MeasureFile.MParam.SampleName = this.SampleNametextBox.Text;
                m_MeasureFile.MParam.FIBTemp = this.FIBTemptextBox.Text;
                m_MeasureFile.MParam.FocusMode = this.ManualFocuscheckBox.Checked;

                MessageBox.Show("参数设置成功");
            }
        }

        private void ManualFocuscheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }

        //选择一个模板文件
        private void button2_Click(object sender, EventArgs e)
        {
            string FilePathName;
            string fileNameWithoutExtension;
            //新建一个文件对话框
            OpenFileDialog pOpenFileDialog = new OpenFileDialog();
            //设置对话框标题
            pOpenFileDialog.Title = "选择模板文件";
            //设置打开文件类型
            pOpenFileDialog.Filter = "ely文件（*.ely）|*.ely";
            //监测文件是否存在
            pOpenFileDialog.CheckFileExists = true;
            //文件打开后执行以下程序
            if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);                             //绝对路径
                fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FilePathName);
                this.FIBTemptextBox.Text = fileNameWithoutExtension;
            }            
        }

        //启动测量
        private void button3_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                m_Ms = new Measure();
                m_Ms.InitMeas(m_MeasureFile);
                m_Ms.DoMeasure();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                m_MeasureFile.Save();
            }
        }
    }
}
