//时间：20200610
//作者：王琦
//功能：SmartSEM remote API控制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using APILib;
using System.Threading;

namespace SmartSEMControl
{
    //可能遇到的错误提示部分？？在log加入之后添加
    class SmartSEM : ISEMControl
    {
        #region 模拟参数
        //放大缩小
        private String AP_MAG = "AP_MAG";
        //焦距
        private String AP_WD = "AP_WD";
        //亮度
        private String AP_BRIGHTNESS = "AP_BRIGHTNESS";
        //对比度
        private String AP_CONTRAST = "AP_CONTRAST";
        //亮度目标
        private String AP_AUTO_BRIGHT = "AP_AUTO_BRIGHT";
        //对比度目标
        private String AP_AUTO_CONTRAST = "AP_AUTO_CONTRAST";
        //消像散X轴
        private String AP_STIG_X = "AP_STIG_X";
        //消像散Y轴
        private String AP_STIG_Y = "AP_STIG_Y";
        //角度补偿
        private String AP_TILT_ANGLE = "AP_TILT_ANGLE";
        //SEM移动X
        private String AP_BEAMSHIFT_X = "AP_BEAMSHIFT_X";
        //SEM移动Y
        private String AP_BEAMSHIFT_Y = "AP_BEAMSHIFT_Y";
        //X轴移动到
        private String AP_STAGE_GOTO_X = "AP_STAGE_GOTO_X";
        //Y轴移动到
        private String AP_STAGE_GOTO_Y = "AP_STAGE_GOTO_Y";
        //Z轴移动到
        private String AP_STAGE_GOTO_Z = "AP_STAGE_GOTO_Z";
        //T轴移动到
        private String AP_STAGE_GOTO_T = "AP_STAGE_GOTO_T";
        //R轴移动到
        private String AP_STAGE_GOTO_R = "AP_STAGE_GOTO_R";
        //M轴移动到
        private String AP_STAGE_GOTO_M = "AP_STAGE_GOTO_M";
        //Scan Rotate角度
        private String AP_SCANROTATION = "AP_SCANROTATION";
        //像素大小
        private String AP_PIXEL_SIZE = "AP_PIXEL_SIZE";
        //X轴坐标
        private String AP_STAGE_AT_X = "AP_STAGE_AT_X";
        //Y轴坐标
        private String AP_STAGE_AT_Y = "AP_STAGE_AT_Y";
        //Z轴坐标
        private String AP_STAGE_AT_Z = "AP_STAGE_AT_Z";
        //T轴坐标
        private String AP_STAGE_AT_T = "AP_STAGE_AT_T";
        //R轴坐标
        private String AP_STAGE_AT_R = "AP_STAGE_AT_R";
        //M轴坐标
        private String AP_STAGE_AT_M = "AP_STAGE_AT_M";
        #endregion

        #region 数字参数
        //Scan Rotate开关
        private String DP_SCAN_ROT = "DP_SCAN_ROT";
        //取消电子束校正
        private String DP_TILT_CORRECTION = "DP_TILT_CORRECTION";
        //自动亮度对比度
        private String DP_AUTO_VIDEO = "DP_AUTO_VIDEO";
        //图像解冻
        private String DP_FROZEN = "DP_FROZEN";
        //图像类型切换
        private String DP_DETECTOR_TYPE = "DP_DETECTOR_TYPE";
        //图像大小
        private String DP_IMAGE_STORE = "DP_IMAGE_STORE";

        //样品台状态
        private String DP_STAGE_IS = "DP_STAGE_IS";
        //自动函数状态
        private String DP_AUTO_FUNCTION = "DP_AUTO_FUNCTION";        
        #endregion

        #region 命令
        //开启电压
        private String CMD_BEAM_ON = "CMD_BEAM_ON";
        //关闭电压
        private String CMD_EHT_OFF = "CMD_EHT_OFF";
        //自动对焦1
        private String CMD_AUTO_FOCUS_COARSE = "CMD_AUTO_FOCUS_COARSE";
        //自动对焦2
        private String CMD_AUTO_FOCUS_FINE = "CMD_AUTO_FOCUS_FINE";
        //自动消像散
        private String CMD_AUTO_STIG = "CMD_AUTO_STIG";
        //样品台急停
        private String CMD_STAGE_ABORT = "CMD_STAGE_ABORT";
        #endregion

        #region 宏文件命令
        private String MCF_FILENAME = "MCF_";
        #endregion

        #region APILib
        private Api CZEMApi = new Api();
        private Boolean m_bInitialised = false;
        #endregion

        #region 构造函数
        public SmartSEM()
        {
            Init();
        }
        #endregion

        #region 设备初始化
        /// <summary>
        /// 设备初始化
        /// </summary>
        /// <returns>return:true or false</returns>
        public Boolean Init()
        {
            long lrt = 0;
            #if ROMOTE
                lrt = CZEMApi.InitialiseRemoting();
            #else
                lrt = CZEMApi.Initialise("");
            #endif
            if (lrt != 0)
            {
                m_bInitialised = false;
                return false;
            }
            else
            {
                m_bInitialised = true;
                return true;
            }
        }
        #endregion

        #region 设备远程初始化
        /// <summary>
        /// 设备远程初始化
        /// </summary>
        /// <returns>return:true or false</returns>
        public Boolean InitRemoting()
        {
            long lrt = CZEMApi.InitialiseRemoting();
            if (lrt != 0)
            {
                m_bInitialised = false;
                return false;
            }
            else
            {
                m_bInitialised = true;
                return true;
            }
        }
        #endregion

        #region 连接状态
        public Boolean ConnectStatus()
        {
            return m_bInitialised;
        }
        #endregion

        #region 关闭设备
        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <returns>true or false</returns>
        public Boolean Dispose()
        {
            if (m_bInitialised)
            {
                long lrt = CZEMApi.ClosingControl();
                if (lrt != 0)
                {
                    return false;
                }
                else
                {
                    m_bInitialised = false;
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 读参数函数
        private Boolean ReadParams(String str_param, ref float value)
        {
            if (m_bInitialised)
            {
                object varfloat = new VariantWrapper((float)0.0f);
                long lrt = CZEMApi.Get(str_param, ref varfloat);
                if (lrt != 0)
                {
                    return false;
                }
                else
                {
                    value = Convert.ToSingle(varfloat);
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 写模拟参数函数
        private Boolean WriteParams(String str_param, float value)
        {
            if (m_bInitialised)
            {
                //先取范围
                object varMin = new VariantWrapper((float)0.0f);
                object varMax = new VariantWrapper((float)0.0f);
                long lrt = CZEMApi.GetLimits(str_param, ref varMin, ref varMax);
                if (lrt != 0)
                {
                    return false;
                }
                if (value <= Convert.ToSingle(varMax) && value >= Convert.ToSingle(varMin))
                {
                    object varFloat = new VariantWrapper(value);
                    lrt = CZEMApi.Set(str_param, ref varFloat);// set value
                    if (lrt != 0)
                    {
                        return false;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 写数字参数函数
        private Boolean WriteDataParams(String str_param, float value)
        {
            if (m_bInitialised)
            {
                object varFloat = new VariantWrapper(value);
                long lrt = CZEMApi.Set(str_param, ref varFloat);// set value
                if (lrt != 0)
                {
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 执行命令
        private Boolean ExecuteCmd(String cmd)
        {
            if (m_bInitialised)
            {
                long lrt = CZEMApi.Execute(cmd);// execute command
                if (lrt != 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 放大缩小
        /// <summary>
        /// 读取缩放倍数
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetMagnification()
        {
            float ret = 0;
            if(ReadParams(AP_MAG, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置缩放倍数
        /// </summary>
        /// <param name="set">倍数</param>
        /// <returns>true or false</returns>
        public Boolean SetMagnification(float set)
        {
            return WriteParams(AP_MAG, set);
        }
        #endregion

        #region 焦距
        /// <summary>
        /// 读取工作距离
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetWorkingDistance()
        {
            float ret = 0;
            if (ReadParams(AP_WD, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置焦距
        /// </summary>
        /// <param name="set">焦距</param>
        /// <returns>true or false</returns>
        public Boolean SetWorkingDistance(float set)
        {
            return WriteParams(AP_WD, set);
        }
        #endregion  

        #region 亮度
        /// <summary>
        /// 读取亮度
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetBrightness()
        {
            float ret = 0;
            if (ReadParams(AP_BRIGHTNESS, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置亮度
        /// </summary>
        /// <param name="set">亮度</param>
        /// <returns>true or false</returns>
        public Boolean SetBrightness(float set)
        {
            return WriteParams(AP_BRIGHTNESS, set);
        }
        #endregion

        #region 对比度
        /// <summary>
        /// 读取对比度
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetContrast()
        {
            float ret = 0;
            if (ReadParams(AP_CONTRAST, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置对比度
        /// </summary>
        /// <param name="set">对比度</param>
        /// <returns>true or false</returns>
        public Boolean SetContrast(float set)
        {
            return WriteParams(AP_CONTRAST, set);
        }
        #endregion

        #region 自动亮度对比度
        /// <summary>
        /// 读取自动亮度对比度状态
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetAutoVideo()
        {
            float ret = 0;
            if (ReadParams(DP_AUTO_VIDEO, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置自动亮度对比度状态
        /// </summary>
        /// <param name="set">0:off;1:B(亮度);2:C(对比度);3:BC</param>
        /// <returns>true or false</returns>
        public Boolean SetAutoVideoOff()
        {
            return WriteDataParams(DP_AUTO_VIDEO, (float)0);
        }

        public Boolean SetAutoVideoBrightness()
        {
            return WriteDataParams(DP_AUTO_VIDEO, (float)1);
        }

        public Boolean SetAutoVideoContrast()
        {
            return WriteDataParams(DP_AUTO_VIDEO, (float)2);
        }

        public Boolean SetAutoVideoBrightnessAndContrast()
        {
            return WriteDataParams(DP_AUTO_VIDEO, (float)3);
        }
        #endregion

        #region 亮度目标
        /// <summary>
        /// 读取亮度目标
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetAutoBright()
        {
            float ret = 0;
            if (ReadParams(AP_AUTO_BRIGHT, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置亮度目标
        /// </summary>
        /// <param name="set">亮度</param>
        /// <returns>true or false</returns>
        public Boolean SetAutoBright(float set)
        {
            return WriteParams(AP_AUTO_BRIGHT, set);
        }
        #endregion

        #region 对比度目标
        /// <summary>
        /// 读取对比度目标
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetAutoContrast()
        {
            float ret = 0;
            if (ReadParams(AP_AUTO_CONTRAST, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置对比度目标
        /// </summary>
        /// <param name="set">对比度目标</param>
        /// <returns>true or false</returns>
        public Boolean SetAutoContrast(float set)
        {
            return WriteParams(AP_AUTO_CONTRAST, set);
        }
        #endregion

        #region 消像散
        /// <summary>
        /// 读取消像散X
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetAstigmatismX()
        {
            float ret = 0;
            if (ReadParams(AP_STIG_X, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 读取消像散Y
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetAstigmatismY()
        {
            float ret = 0;
            if (ReadParams(AP_STIG_Y, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置消像散X
        /// </summary>
        /// <param name="set">对比度</param>
        /// <returns>true or false</returns>
        public Boolean SetAstigmatismX(float set)
        {
            return WriteParams(AP_STIG_X, set);
        }

        /// <summary>
        /// 设置消像散Y
        /// </summary>
        /// <param name="set">对比度</param>
        /// <returns>true or false</returns>
        public Boolean SetAstigmatismY(float set)
        {
            return WriteParams(AP_STIG_Y, set);
        }
        #endregion

        #region 角度补偿
        /// <summary>
        /// 读取角度补偿
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetTiltAngle()
        {
            float ret = 0;
            if (ReadParams(AP_TILT_ANGLE, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置角度补偿
        /// </summary>
        /// <param name="set">角度补偿</param>
        /// <returns>true or false</returns>
        public Boolean SetTiltAngle(float set)
        {
            Boolean ret = false;
            ret = WriteDataParams(DP_TILT_CORRECTION, 1);
            ret = WriteParams(AP_TILT_ANGLE, set);
            ret = WriteDataParams(DP_TILT_CORRECTION, 0);
            return ret;
        }
        #endregion

        #region 图像大小
        public int[] GetImageStore()
        {
            int[] i_ret = new int[2];
            float ret = 0;
            if (ReadParams(DP_IMAGE_STORE, ref ret))
            {
                switch(ret)
                {
                    case 0:
                        i_ret[0] = 1024;
                        i_ret[1] = 768;
                        break;
                    case 1:
                        i_ret[0] = 512;
                        i_ret[1] = 384;
                        break;
                    case 2:
                        i_ret[0] = 2048;
                        i_ret[1] = 1536;
                        break;
                    case 3:
                        i_ret[0] = 3072;
                        i_ret[1] = 2304;
                        break;
                    case 4:
                        i_ret[0] = 4096;
                        i_ret[1] = 3072;
                        break;
                    case 5:
                        i_ret[0] = 5120;
                        i_ret[1] = 3840;
                        break;
                    case 6:
                        i_ret[0] = 6144;
                        i_ret[1] = 4608;
                        break;
                    case 7:
                        i_ret[0] = 8192;
                        i_ret[1] = 6144;
                        break;
                    case 8:
                        i_ret[0] = 12288;
                        i_ret[1] = 9216;
                        break;
                    case 9:
                        i_ret[0] = 16384;
                        i_ret[1] = 12288;
                        break;
                    case 10:
                        i_ret[0] = 24576;
                        i_ret[1] = 18432;
                        break;
                    case 11:
                        i_ret[0] = 32768;
                        i_ret[1] = 24576;
                        break;
                    default:
                        i_ret[0] = 0;
                        i_ret[1] = 0;
                        break;
                }
            }
            else
            {
                i_ret[0] = 0;
                i_ret[1] = 0;
            }
            return i_ret;
        }

        public Boolean SetImageStore(float set)
        {
            return WriteDataParams("DP_IMAGE_STORE", set);
        }
        #endregion

        #region 抓取图像
        /// <summary>
        /// 抓取图像
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public Boolean GrabImage(String filename, short xoff,short yoff, short width,short height,short type)
        {
            if (m_bInitialised)
            {
                long lrt = CZEMApi.Grab(xoff, yoff, width, height, type, filename);
                if (lrt != 0)
                {
                    return false;
                }
                else
                {
                    ImageFrozen();
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 图像解冻
        public Boolean ImageFrozen()
        {
            return WriteDataParams(DP_FROZEN, (float)1); 
        }
        public Boolean ImageLive()
        {
            return WriteDataParams(DP_FROZEN, (float)0);
        }
        #endregion

        #region 图像类型切换
        public Boolean DetectorType(float set)
        {
            return WriteDataParams(DP_DETECTOR_TYPE, set);
        }
        #endregion

        #region 电子束移动接口
        /// <summary>
        /// 获取电子束移动接口X
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetBeamShiftX()
        {
            float ret = 0;
            if (ReadParams(AP_BEAMSHIFT_X, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 获取电子束移动接口Y
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetBeamShiftY()
        {
            float ret = 0;
            if (ReadParams(AP_BEAMSHIFT_Y, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置电子束移动接口X
        /// </summary>
        /// <param name="set">X坐标</param>
        /// <returns>true or false</returns>
        public Boolean SetBeamShiftX(float set)
        {
            return WriteParams(AP_BEAMSHIFT_X, set);
        }

        /// <summary>
        /// 设置电子束移动接口Y
        /// </summary>
        /// <param name="set">X坐标</param>
        /// <returns>true or false</returns>
        public Boolean SetBeamShiftY(float set)
        {
            return WriteParams(AP_BEAMSHIFT_Y, set);
        }
        #endregion

        #region 样品台移动接口
        /// <summary>
        /// 返回样品台坐标数组
        /// </summary>
        /// <returns>Lenght=6</returns>
        public float[] GetStagePosition()
        {
            object retx = new VariantWrapper((float)0.0f);
            object rety = new VariantWrapper((float)0.0f);
            object retz = new VariantWrapper((float)0.0f);
            object rett = new VariantWrapper((float)0.0f);
            object retr = new VariantWrapper((float)0.0f);
            object retm = new VariantWrapper((float)0.0f);
            float[] ret = new float[6];
            if (m_bInitialised)
            {
                CZEMApi.GetStagePosition(ref retx, ref rety, ref retz, ref rett, ref retr, ref retm);
                ret[0] = Convert.ToSingle(retx);
                ret[1] = Convert.ToSingle(rety);
                ret[2] = Convert.ToSingle(retz);
                ret[3] = Convert.ToSingle(rett);
                ret[4] = Convert.ToSingle(retr);
                ret[5] = Convert.ToSingle(retm);
            }
            else
            {
                ret[0] = float.NaN;
                ret[1] = float.NaN;
                ret[2] = float.NaN;
                ret[3] = float.NaN;
                ret[4] = float.NaN;
                ret[5] = float.NaN;
            }
            return ret;
        }

        /// <summary>
        /// 设置样品台坐标数组
        /// </summary>
        /// <returns>Lenght=6</returns>
        private Boolean SetStagePosition(float[] set)
        {
            if (m_bInitialised)
            { 
                long lrt = CZEMApi.MoveStage(set[0], set[1], set[2], set[3], set[4], set[5]);
                if(lrt==0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 样品台位置 X轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtX()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_X, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 Y轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtY()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_Y, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 Z轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtZ()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_Z, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 T轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtT()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_T, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 R轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtR()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_R, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 M轴
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetStageAtM()
        {
            float ret = 0;
            if (ReadParams(AP_STAGE_AT_M, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 样品台位置 X轴
        /// </summary>
        /// <param name="set">X轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoX(float set)
        {
            return WriteParams(AP_STAGE_GOTO_X, set);
        }

        /// <summary>
        /// 样品台位置 Y轴
        /// </summary>
        /// <param name="set">Y轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoY(float set)
        {
            return WriteParams(AP_STAGE_GOTO_Y, set);
        }

        /// <summary>
        /// 样品台位置 Z轴
        /// </summary>
        /// <param name="set">Y轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoZ(float set)
        {
            return WriteParams(AP_STAGE_GOTO_Z, set);
        }

        /// <summary>
        /// 样品台位置 T轴
        /// </summary>
        /// <param name="set">Y轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoT(float set)
        {
            return WriteParams(AP_STAGE_GOTO_T, set);
        }

        /// <summary>
        /// 样品台位置 R轴
        /// </summary>
        /// <param name="set">Y轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoR(float set)
        {
            return WriteParams(AP_STAGE_GOTO_R, set);
        }

        /// <summary>
        /// 样品台位置 M轴
        /// </summary>
        /// <param name="set">M轴移动坐标</param>
        /// <returns></returns>
        public Boolean SetStageGotoM(float set)
        {
            return WriteParams(AP_STAGE_GOTO_M, set);
        }
        #endregion

        #region 移动样品台XY轴
        public Boolean MoveStageXY(float x,float y)
        {
            //先取范围
            object xMin = new VariantWrapper((float)0.0f);
            object xMax = new VariantWrapper((float)0.0f);
            long lrt = CZEMApi.GetLimits(AP_STAGE_GOTO_X, ref xMin, ref xMax);
            if (lrt != 0)
            {
                return false;
            }
            if (x > Convert.ToSingle(xMax) || x < Convert.ToSingle(xMin))
            {
                return false;
            }
            object yMin = new VariantWrapper((float)0.0f);
            object yMax = new VariantWrapper((float)0.0f);
            lrt = CZEMApi.GetLimits(AP_STAGE_GOTO_Y, ref yMin, ref yMax);
            if (lrt != 0)
            {
                return false;
            }
            if (y > Convert.ToSingle(yMax) || y < Convert.ToSingle(yMin))
            {
                return false;
            }
            float[] gsp = GetStagePosition();
            gsp[0] = x;
            gsp[1] = y;
            return SetStagePosition(gsp);
        }
        #endregion

        #region 样品台急停
        public Boolean CmdStageAbort()
        {
            return ExecuteCmd(CMD_STAGE_ABORT);
        }
        #endregion

        #region Scan Rotate角度接口
        /// <summary>
        /// 获取Scan Rotate角度接口
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetScanRotation()
        {
            float ret = 0;
            if (ReadParams(AP_SCANROTATION, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置Scan Rotate角度接口
        /// </summary>
        /// <param name="set">角度补偿</param>
        /// <returns>true or false</returns>
        public Boolean SetScanRotation(float set)
        {
            Boolean ret = false;
            ret = WriteDataParams(DP_SCAN_ROT, 1);
            ret = WriteParams(AP_SCANROTATION, set);
            ret = WriteDataParams(DP_SCAN_ROT, 0);
            return ret;
        }
        #endregion

        #region 像素读取PixelSize
        /// <summary>
        /// 获取Pixel Size 接口,只读
        /// </summary>
        /// <returns>true:float or false:NaN</returns>
        public float GetPixelSize()
        {
            float ret = 0;
            if (ReadParams(AP_PIXEL_SIZE, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }
        #endregion

        #region 电子束校正设置
        /// <summary>
        /// 读取电子束校正状态
        /// </summary>
        /// <returns></returns>
        public float GetTiltCorrection()
        {
            float ret = 0;
            if (ReadParams(DP_TILT_CORRECTION, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }

        /// <summary>
        /// 设置电子束校正状态
        /// </summary>
        /// <param name="set"></param>
        /// <returns></returns>
        public Boolean SetTiltCorrectionOff()
        {
            return WriteDataParams(DP_TILT_CORRECTION, (float)0);
        }
        public Boolean SetTiltCorrectionOn()
        {
            return WriteDataParams(DP_TILT_CORRECTION, (float)1);
        }
        #endregion

        #region 开启电压
        public Boolean CmdOpenVoltage()
        {
            return ExecuteCmd(CMD_BEAM_ON);
        }
        #endregion

        #region 关闭电压
        public Boolean CmdCloseVoltage()
        {
            return ExecuteCmd(CMD_EHT_OFF);
        }
        #endregion

        #region 自动对焦1
        public Boolean CmdAutoFocusCoarse()
        {
            return ExecuteCmd(CMD_AUTO_FOCUS_COARSE);
        }
        #endregion

        #region 自动对焦2
        public Boolean CmdAutoFocusFine()
        {
            return ExecuteCmd(CMD_AUTO_FOCUS_FINE);
        }
        #endregion

        #region 自动消像散
        public Boolean CmdAutoStig()
        {
            return ExecuteCmd(CMD_AUTO_STIG);
        }
        #endregion

        #region 调用宏文件命令
        public Boolean CMDMCFFilename(String filename)
        {
            String CMDMCF = MCF_FILENAME + filename;
            return ExecuteCmd(CMDMCF);
        }
        #endregion

        #region 读取样品台状态
        public float GetStageIs()
        {
            float ret = 0;
            if (ReadParams(DP_STAGE_IS, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }
        #endregion

        #region 读取自动函数的状态
        public float GetAutoFunction()
        {
            float ret = 0;
            if (ReadParams(DP_AUTO_FUNCTION, ref ret))
            {
                return ret;
            }
            else
            {
                return float.NaN;
            }
        }
        #endregion

        public void InsertPT()
        {
        }
        //PT针退出
        public void OutputPT()
        {
        }
    }
}
