//时间：
//作者：
//功能：单元测试功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using SmartSEMControl;

namespace HOZProject
{
    public partial class FormUnitControl : Form
    {
        //全局只有一个fatorySEM
        static FactoryHardware factorySEM = FactoryHardware.Instance;
        ISEMControl iSEM = factorySEM.ISEM;

        #region 系统参数
        private String path = Directory.GetCurrentDirectory();
        #endregion

        #region 构造函数
        public FormUnitControl()
        {
            InitializeComponent();     
            

            //Calling Notification for updated status
            //CZEMApi.Notify += new _EMApiEvents_NotifyEventHandler(CZEMApi_Notify);
            //CZEMApi.NotifyWithCurrentValue += new _EMApiEvents_NotifyWithCurrentValueEventHandler(CZEMApi_NotifyWithCurrentValue);
            //加载参数

        }
        #endregion
		
		#region 设备初始化按键
        private void btnInit_Click(object sender, EventArgs e)
        {
            //if (sem.Init())
            //{
            //    btnInit.BackColor = Color.Lime;
            //}
            //else
            //{
            //    btnInit.BackColor = Color.Red;
            //}
        }
        #endregion
		
        #region 设备关闭按键
        private void btnCloseCtrl_Click(object sender, EventArgs e)
        {
            //if (sem.UnInit())
            //{
            //    btnInit.BackColor = Control.DefaultBackColor;
            //}
        }
        #endregion

        #region 设置参数更新通知
        private void btnSetNotify_Click(object sender, EventArgs e)
        {
            //if (m_bInitialised)
            //{
            //    // Magnification
            //    if (CZEMApi.SetNotify("AP_MAG", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_MAG");
            //        return;
            //    }
            //    // Working distance
            //    if (CZEMApi.SetNotify("AP_WD", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_WD");
            //        return;
            //    }
            //    // BRIGHTNESS 亮度
            //    if (CZEMApi.SetNotify("AP_BRIGHTNESS", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_BRIGHTNESS");
            //        return;
            //    }
            //    // AP_CONTRAST,对比度
            //    if (CZEMApi.SetNotify("AP_CONTRAST", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_CONTRAST");
            //        return;
            //    }
            //    // Vacuum
            //    if (CZEMApi.SetNotify("DP_VACSTATUS", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_VACSTATUS");
            //        return;
            //    }
            //    // Gun / EHT state
            //    if (CZEMApi.SetNotify("DP_RUNUPSTATE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_RUNUPSTATE");
            //        return;
            //    }
            //    // actual KV / EHT
            //    if (CZEMApi.SetNotify("AP_ACTUALKV", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_ACTUALKV");
            //        return;
            //    }
            //    // actual current
            //    if (CZEMApi.SetNotify("AP_ACTUALCURRENT", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_ACTUALCURRENT");
            //        return;
            //    }
            //    // column type
            //    if (CZEMApi.SetNotify("DP_COLUMN_TYPE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_COLUMN_TYPE");
            //        return;
            //    }
            //    // mode
            //    if (CZEMApi.SetNotify("DP_OPERATING_MODE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_OPERATING_MODE");
            //        return;
            //    }
            //    // scan rate
            //    if (CZEMApi.SetNotify("DP_SCANRATE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_SCANRATE");
            //        return;
            //    }
            //    // auto function active
            //    if (CZEMApi.SetNotify("DP_AUTO_FUNCTION", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_AUTO_FUNCTION");
            //        return;
            //    }


            //    // probe current
            //    if (CZEMApi.SetNotify("AP_IPROBE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "AP_IPROBE");
            //        return;
            //    }
            //    // detector
            //    if (CZEMApi.SetNotify("DP_DETECTOR_TYPE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_DETECTOR_TYPE");
            //        return;
            //    }
            //    // stage initialised
            //    if (CZEMApi.SetNotify("DP_STAGE_INIT", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_STAGE_INIT");
            //        return;
            //    }
            //    // stage busy?
            //    if (CZEMApi.SetNotify("DP_STAGE_IS", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_STAGE_IS");
            //        return;
            //    }
            //    // fib mode
            //    if (CZEMApi.SetNotify("DP_FIB_MODE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_FIB_MODE");
            //        return;
            //    }
            //    // fib gun state
            //    if (CZEMApi.SetNotify("DP_FIB_GUN_STATE", 1) != 0)
            //    {
            //        DisplayError("SetNotify", "DP_FIB_GUN_STATE");
            //        return;
            //    }

            //listmsg.Items.Add("Notification enabled");
            // }
        }
        #endregion
        
        #region 缩放Get
        private void btnenlargeGet_Click(object sender, EventArgs e)
        {

            float ret = iSEM.GetMagnification();
            if (float.IsNaN(ret))
            {
                lblenlargeGet.Text = "NaN";
                txtenlargeSet.Enabled = false;
                btnenlargeSet.Enabled = false;
            }
            else
            {
                lblenlargeGet.Text = ret.ToString();
                txtenlargeSet.Text = ret.ToString();
                btnenlargeSet.Enabled = true;
            }
        }
        #endregion

        #region 缩放Set
        private void btnenlargeSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtenlargeSet.Text, out set))
            {
                iSEM.SetMagnification(set);
            }

        }
        #endregion

        #region 焦距Get
        private void btnWDGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetWorkingDistance();
            if (float.IsNaN(ret))
            {
                lblWDGet.Text = "NaN";
                txtWDSet.Enabled = false;
                btnWDSet.Enabled = false;
            }
            else
            {
                lblWDGet.Text = ret.ToString();
                txtWDSet.Text = ret.ToString();
                btnWDSet.Enabled = true;
            }
        }
        #endregion

        #region 焦距Set
        private void btnWDSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtWDSet.Text, out set))
            {
                iSEM.SetWorkingDistance(set);
            }
        }
        #endregion

        #region 亮度Get
        private void btnBrightnessGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetBrightness();
            if (float.IsNaN(ret))
            {
                lblBrightnessGet.Text = "NaN";
                txtBrightnessSet.Enabled = false;
                btnBrightnessSet.Enabled = false;
            }
            else
            {
                lblBrightnessGet.Text = ret.ToString();
                txtBrightnessSet.Text = ret.ToString();
                btnBrightnessSet.Enabled = true;
            }
        }
        #endregion

        #region 亮度Set
        private void btnBrightnessSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtBrightnessSet.Text, out set))
            {
                iSEM.SetBrightness(set);
            }
        }
        #endregion

        #region 对比度Get
        private void btnContrastGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetContrast();
            if (float.IsNaN(ret))
            {
                lblContrastGet.Text = "NaN";
                txtContrastSet.Enabled = false;
                btnContrastSet.Enabled = false;
            }
            else
            {
                lblContrastGet.Text = ret.ToString();
                txtContrastSet.Text = ret.ToString();
                btnContrastSet.Enabled = true;
            }
        }
        #endregion

        #region 对比度Set
        private void btnContrastSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtContrastSet.Text, out set))
            {
                iSEM.SetContrast(set);
            }
        }
        #endregion

        #region 消像散X Get
        private void btnAstigmatismXGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetAstigmatismX();
            if (float.IsNaN(ret))
            {
                lblAstigmatismXGet.Text = "NaN";
                txtAstigmatismXSet.Enabled = false;
                btnAstigmatismXSet.Enabled = false;
            }
            else
            {
                lblAstigmatismXGet.Text = ret.ToString();
                txtAstigmatismXSet.Text = ret.ToString();
                btnAstigmatismXSet.Enabled = true;
            }
        }
        #endregion

        #region 消像散X Set
        private void btnAstigmatismXSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtAstigmatismXSet.Text, out set))
            {
                iSEM.SetAstigmatismX(set);
            }
        }
        #endregion

        #region 消像散Y Get
        private void btnAstigmatismYGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetAstigmatismY();
            if (float.IsNaN(ret))
            {
                lblAstigmatismYGet.Text = "NaN";
                txtAstigmatismYSet.Enabled = false;
                btnAstigmatismYSet.Enabled = false;
            }
            else
            {
                lblAstigmatismYGet.Text = ret.ToString();
                txtAstigmatismYSet.Text = ret.ToString();
                btnAstigmatismYSet.Enabled = true;
            }
        }
        #endregion

        #region 消像散Y Set
        private void btnAstigmatismYSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtAstigmatismYSet.Text, out set))
            {
                iSEM.SetAstigmatismY(set);
            }
        }

        #endregion

        #region 角度补偿Get
        private void btnTiltAngleGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetTiltAngle();
            if (float.IsNaN(ret))
            {
                lblTiltAngleGet.Text = "NaN";
                txtTiltAngleSet.Enabled = false;
                btnTiltAngleSet.Enabled = false;
            }
            else
            {
                lblTiltAngleGet.Text = ret.ToString();
                txtTiltAngleSet.Text = ret.ToString();
                btnTiltAngleSet.Enabled = true;
            }
        }
        #endregion

        #region 角度补偿Set
        private void btnTiltAngleSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtTiltAngleSet.Text, out set))
            {
                iSEM.SetTiltAngle(set);
            }
        }
        #endregion

        #region 抓取图像
        private void btnGrabImage_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "图像保存位置：";
            sfd.FileName = "test.tif";
            sfd.Filter = "TIF文件|*.tif";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string fn = sfd.FileName;
                iSEM.GrabImage(fn, 0, 0, 1024, 768, 0);
            }
        }
        #endregion

        #region ScanRotate Get
        private void btnScanRotationGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetScanRotation();
            if (float.IsNaN(ret))
            {
                lblScanRotationGet.Text = "NaN";
                txtScanRotationSet.Enabled = false;
                btnScanRotationSet.Enabled = false;
            }
            else
            {
                lblScanRotationGet.Text = ret.ToString();
                txtScanRotationSet.Text = ret.ToString();
                btnScanRotationSet.Enabled = true;
            }
        }
        #endregion

        #region ScanRotate Set
        private void btnScanRotationSet_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtScanRotationSet.Text, out set))
            {
                
                iSEM.SetScanRotation(set);
            }
        }
        #endregion

        #region PixelSize Get
        private void btnPixelSizeGet_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetPixelSize();
            if (float.IsNaN(ret))
            {
                lblPixelSizeGet.Text = "NaN";
            }
            else
            {
                lblPixelSizeGet.Text = ret.ToString();
            }
        }
        #endregion

        #region 开启电压
        private void btnOpenVoltage_Click(object sender, EventArgs e)
        {
            iSEM.CmdOpenVoltage();
        }
        #endregion

        #region 关闭电压
        private void btnCloseVoltage_Click(object sender, EventArgs e)
        {
            iSEM.CmdCloseVoltage();
        }
        #endregion

        #region 样品台位置获取
        private void btnStageGetX_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtX();
            if (float.IsNaN(ret))
            {
                lblStageX.Text = "NaN";
            }
            else
            {
                lblStageX.Text = ret.ToString();
            }
        }

        private void btnStageGetY_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtY();
            if (float.IsNaN(ret))
            {
                lblStageY.Text = "NaN";
            }
            else
            {
                lblStageY.Text = ret.ToString();
            }
        }

        private void btnStageGetZ_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtZ();
            if (float.IsNaN(ret))
            {
                lblStageZ.Text = "NaN";
            }
            else
            {
                lblStageZ.Text = ret.ToString();
            }
        }

        private void btnStageGetT_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtT();
            if (float.IsNaN(ret))
            {
                lblStageT.Text = "NaN";
            }
            else
            {
                lblStageT.Text = ret.ToString();
            }
        }

        private void btnStageGetR_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtR();
            if (float.IsNaN(ret))
            {
                lblStageR.Text = "NaN";
            }
            else
            {
                lblStageR.Text = ret.ToString();
            }
        }

        private void btnStageGetM_Click(object sender, EventArgs e)
        {
            float ret = iSEM.GetStageAtM();
            if (float.IsNaN(ret))
            {
                lblStageM.Text = "NaN";
            }
            else
            {
                lblStageM.Text = ret.ToString();
            }
        }
        #endregion

        #region 样品台位置设置
        private void btnStageSetX_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageX.Text, out set))
            {
                iSEM.SetStageGotoX(set);
            }
        }

        private void btnStageSetY_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageY.Text, out set))
            {
                iSEM.SetStageGotoY(set);
            }
        }

        private void btnStageSetZ_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageZ.Text, out set))
            {
                iSEM.SetStageGotoZ(set);
            }
        }

        private void btnStageSetT_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageT.Text, out set))
            {
                iSEM.SetStageGotoT(set);
            }
        }

        private void btnStageSetR_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageR.Text, out set))
            {
                iSEM.SetStageGotoR(set);
            }
        }

        private void btnStageSetM_Click(object sender, EventArgs e)
        {
            float set = 0;
            if (float.TryParse(txtStageM.Text, out set))
            {
                iSEM.SetStageGotoM(set);
            }
        }
        #endregion

        #region 获取样品台位置数组
        private void btnGetStagePosition_Click(object sender, EventArgs e)
        {
            float[] pt = iSEM.GetStagePosition();
            if (!float.IsNaN(pt[0]))
            {
                lblStageX.Text = pt[0].ToString();
            }
            if (!float.IsNaN(pt[1]))
            {
                lblStageY.Text = pt[1].ToString();
            }
            if (!float.IsNaN(pt[2]))
            {
                lblStageZ.Text = pt[2].ToString();
            }
            if (!float.IsNaN(pt[3]))
            {
                lblStageT.Text = pt[3].ToString();
            }
            if (!float.IsNaN(pt[4]))
            {
                lblStageR.Text = pt[4].ToString();
            }
            if (!float.IsNaN(pt[5]))
            {
                lblStageM.Text = pt[5].ToString();
            }
        }
        #endregion

        private void btnDoSemDriftCorrn_Click(object sender, EventArgs e)
        {
            iSEM.SetTiltCorrectionOff();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            iSEM.SetTiltCorrectionOn();
        }

        private void FormUnitControl_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(iSEM!=null)
            {
                iSEM.Dispose();
            }
        }

        private void btnCMDMCF_Click(object sender, EventArgs e)
        {
            iSEM.CMDMCFFilename("OPTON");
        }
    }
}
