//时间：20200618
//作者：郝爽
//功能：测量参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using FileManager;
using System.Xml;
using System.IO;
using System.Configuration;

namespace MeasureData
{
    public class MeasureParam : ISlo
    {

        #region 内容
        //工作条件，一次测量的全部切孔都是一类的

        // 样品类型
        private string m_SampleName;
        public string SampleType
        {
            get { return this.m_SampleName; }
            set { this.m_SampleName = value; }
        }
        public string SampleTypeNo()
        {
            return m_SampleName.Split('_')[0];//样品类型的命名必须是“0_XXXX" "1_XXXXX"的形式，前面的数字是与谭博士通信的类型指示

        }

        //是否仅拍照
        private Boolean is_Photograph;
        public Boolean Is_Photograph
        {
            get { return this.is_Photograph; }
            set { this.is_Photograph = value; }
        }


        //是否有pt工序
        private Boolean m_pt;
        public Boolean PT
        {
            get { return this.m_pt; }
            set { this.m_pt = value; }
        }

        //倾斜样品台
        private Boolean m_Tilt;
        public Boolean Tilt
        {
            get { return this.m_Tilt; }
            set { this.m_Tilt = value; }
        }

        //是否有EDS
        private Boolean m_EDS;
        public Boolean EDS
        {
            get { return this.m_EDS; }
            set { this.m_EDS = value; }
        }

        //能谱Z轴移动的位置
        private float edsZ;
        public float EDSZ
        {
            get { return this.edsZ; }
            set { this.edsZ = value; }
        }

        //能谱电压设定值
        private float edsV;
        public float EDSV
        {
            get { return this.edsV; }
            set { this.edsV = value; }
        }

        //能谱电压设定值
        private float edsA;
        public float EDSA
        {
            get { return this.edsA; }
            set { this.edsA = value; }
        }

        //Z轴移动的范围
        //private float m_dZ;
        //public float ZDistance
        //{
        //    get { return this.m_dZ; }
        //    set { this.m_dZ = value; }
        //}

        //FIB使用的ELY文件
        private string m_fibTemp;
        public string FIBTemp
        {
            get { return this.m_fibTemp; }
            set { this.m_fibTemp = value; }
        }

        //PT使用的ELY文件
        private string m_ptTemp;
        public string PTTemp
        {
            get { return this.m_ptTemp; }
            set { this.m_ptTemp = value; }
        }
       public string RemoteELYPath ;
       public string   RemoteMLFPath;
 

        //拉直操作需要的放大位数
        private float straighten_Magnification;
        public float Straighten_Magnification
        {
            get { return this.straighten_Magnification; }
            set { this.straighten_Magnification = value; }
        }
        public bool StraightenToRun;
        public bool StraightenAutoBC;
        public float StraightenManualBrightness;
        public float StraightenManualContrast;
        //定位切割位置的放大倍数
        private float location_Magnification;
        public float Location_Magnification
        {
            get { return this.location_Magnification; }
            set { this.location_Magnification = value; }
        }

        //定位切割位置的工作电压
        private float mVoltage;
        public float Voltage
        {
            get { return this.mVoltage; }
            set { this.mVoltage = value; }
        }
        public float Iprobe;
        //拍照的放大倍数
        private float photograph_Magnification;
        public float Photograph_Magnification
        {
            get { return this.photograph_Magnification; }
            set { this.photograph_Magnification = value; }
        }
        public bool ShotSectionAutoBC;
        public float  ShotSectionManualBrightness;
        public float ShotSectionManualConstrast;
        //拍照的工作电压
        private float photograph_Voltage;
        public float Photograph_Voltage
        {
            get { return this.photograph_Voltage; }
            set { this.photograph_Voltage = value; }
        }
        
        //FIB拍照时的放大倍数
        private float fib_Magnification;
        public float FIB_Magnification
        {
            get { return this.fib_Magnification; }
            set { this.fib_Magnification = value; }
        }

        //校正角度选择
        private float correction_Angle_Inside;
        public float Correction_Angle_Inside
        {
            get { return this.correction_Angle_Inside; }
            set { this.correction_Angle_Inside = value; }
        }
        private float correction_Angle_OutSide;
        public float Correction_Angle_OutSide
        {
            get { return this.correction_Angle_OutSide; }
            set { this.correction_Angle_OutSide = value; }
        }
        //厂商
        private String firm;
        public String Firm
        {
            get { return this.firm; }
            set { this.firm = value; }
        }

      
      

      

        //EDS参数
        private EDSParam EdsParam;
        public EDSParam EDSParam
        {
            get { return this.EdsParam; }
            set { this.EdsParam = value; }
        }

       

      

        //ScanRotation修正角度
        private float scanRotCor;
        public float ScanRotCor
        {
            get { return this.scanRotCor; }
            set { this.scanRotCor = value; }
        }

        //ScanRotation修正角度
        private float pixelSizeCor;
        public float PixelSizeCor
        {
            get { return this.pixelSizeCor; }
            set { this.pixelSizeCor = value; }
        }

        //能谱参数

      

        //add by sun 2020-12-17 增加调试时是否切割开关
        private bool m_IsCutingForDebug;
        public bool IsCutingForDebug
        {
            get { return this.m_IsCutingForDebug; }
            set { this.m_IsCutingForDebug = value; }
        }
        //add by sun 2020-12-17 增加调试时是否切割开关 end

        //add by zjx 2020-12-18 为了测试只做能谱部分
        private bool m_IsonlyEDSForDebug;
        public bool IsonlyEDSForDebug
        {
            get { return this.m_IsonlyEDSForDebug; }
            set { this.m_IsonlyEDSForDebug = value; }
        }
        //add by zjx 2020-12-18 为了测试只做能谱部分 end

        #endregion
       


        public float  FindCrossMagnification;

        //构造函数
        public MeasureParam()
        {
            Init();
        }
        public bool GetMeasureParamFromConfigration()
        {
            MeasureParam cfm = this;

            //设置配置文件默认值
            bool bResult = false;

            bool.TryParse(ConfigurationManager.AppSettings["Is_Photograph"].ToString(), out bResult);
            cfm.Is_Photograph = bResult;

            bool.TryParse(ConfigurationManager.AppSettings["PT_Depostion"].ToString(), out bResult);
            cfm.PT = bResult;

            cfm.PTTemp = ConfigurationManager.AppSettings["PT_ELYFile"].ToString();
            cfm.FIBTemp = ConfigurationManager.AppSettings["FIB_ELYFile"].ToString();



            cfm.SampleType = ConfigurationManager.AppSettings["LastSampleType"].ToString();

            cfm.Firm = ConfigurationManager.AppSettings["LastSelectFirm"].ToString();



            bool.TryParse(ConfigurationManager.AppSettings["Is_Title"].ToString(), out bResult);
            cfm.Tilt = bResult;

            bool.TryParse(ConfigurationManager.AppSettings["Is_EDS"].ToString(), out bResult);
            cfm.EDS = bResult;

            cfm.ScanRotCor = Convert.ToSingle(ConfigurationManager.AppSettings["ScanRotCur"].ToString());
            cfm.PixelSizeCor = Convert.ToSingle(ConfigurationManager.AppSettings["PixelSize_Y_Cur"].ToString());

            cfm.EDS = Convert.ToBoolean(ConfigurationManager.AppSettings["Is_EDS"].ToString());
            cfm.EDSZ = Convert.ToSingle(ConfigurationManager.AppSettings["EDS_Z"].ToString()) / 1000;
            cfm.EDSV = Convert.ToSingle(ConfigurationManager.AppSettings["EDS_V"].ToString());
            cfm.EDSA = Convert.ToSingle(ConfigurationManager.AppSettings["EDS_A"].ToString()) / 1000000000;
            cfm.EDSParam.XrayCollectMode = Convert.ToInt16(ConfigurationManager.AppSettings["EDS_XrayMode"].ToString());

          cfm.IsCutingForDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["IsCutingForDebug"]);
            //add by sun 2020-12-17 增加调试时是否切割开关 end

            // add by zjx 2020-12-18 为了测试只做能谱部分
            cfm.IsonlyEDSForDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["IsonlyEDSForDebug"]);
            // add by zjx 2020-12-18 为了测试只做能谱部分 end

          cfm.RemoteELYPath = ConfigurationManager.AppSettings["RemoteELYPath"];
       cfm.RemoteMLFPath = ConfigurationManager.AppSettings["RemoteMLFPath"];
            return true;
        }


        public bool loadParamFromSampleTypeTemplate(string filename)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(System.Environment.CurrentDirectory + "\\SampleTemplate");
            XmlDocument doc = new XmlDocument();
            doc.Load(TheFolder+"\\"+filename);//载入xml文件

            XmlNode root = doc.SelectSingleNode("XMLData");
            xString firm = new xString();
            
            Slo sFile = new Slo();
            sFile.Register("DefaultFirmName", firm);
            Slo locateP = new Slo();
           
            xInt locMag = new xInt();
            
            locateP.Register("Magnification", locMag);
            sFile.Register("Locate", locateP);

            Slo votandI = new Slo();
          
            xInt vot = new xInt();
            xDouble iprobe = new xDouble();
          
            votandI.Register("Votage", vot);
            votandI.Register("Iprobe", iprobe);
            sFile.Register("VotageAndIprobe", votandI);

            Slo tCorrect = new Slo();
            xInt Outside = new xInt();
            xInt inside = new xInt();
            tCorrect.Register("Outside", Outside);
            tCorrect.Register("Inside", inside);
            sFile.Register("TiltCorrect", tCorrect);

            Slo straighten = new Slo();
            xInt Straightenmag = new xInt();
            xBool torun = new xBool();
            xInt b1 = new xInt();
            xInt c1 = new xInt();
            xBool auto1 = new xBool();
            straighten.Register("Magnification", Straightenmag);
            straighten.Register("ToRun", torun);
            straighten.Register("Hand_Bright", b1);
            straighten.Register("Hand_Contrast", c1);
            straighten.Register("AutoGet", auto1);
            sFile.Register("Straighten", straighten);
           


      

            Slo findcross = new Slo();
            xInt crossmag = new xInt();
            findcross.Register("Magnification", crossmag);
            sFile.Register("FindCross", findcross);


            Slo shotsection = new Slo();
            xInt shotmag = new xInt();

            xBool auto2 = new xBool();
            xInt b2 = new xInt();
            xInt c2 = new xInt();
            shotsection.Register("Magnification", shotmag);
            shotsection.Register("Hand_Bright", b2);
            shotsection.Register("Hand_Contrast", c2);
            shotsection.Register("AutoGet", auto2);
            sFile.Register("ShotSection", shotsection);


            sFile.Serialize(false, doc, root);

            this.Location_Magnification = locMag.value();

            this.mVoltage = vot.value();
            this.Iprobe = (float)iprobe.value();

            this.correction_Angle_Inside = inside.value();
            this.correction_Angle_OutSide = Outside.value();
            this.StraightenToRun = torun.value();
            this.straighten_Magnification = Straightenmag.value();

            this.StraightenAutoBC = auto1.value();
            this.StraightenManualBrightness = b1.value();
            this.StraightenManualContrast = c1.value();

          

            this.FindCrossMagnification = crossmag.value();

            this.photograph_Magnification = shotmag.value();
            this.ShotSectionAutoBC = auto2.value();
            this.ShotSectionManualBrightness = b2.value();
            this.ShotSectionManualConstrast = c2.value();
          
            return true;
        }

        public void Init()
        {
            this.SampleType = @"";
            this.PT = false;
            this.FIBTemp = @"";
            this.PTTemp = @"";
           
           
            this.Tilt = false;
           
            this.EDS = false;
            this.EDSZ = 0.005f;
            this.EDSV = 10000;
            this.EDSA = 0.0000000005f;
            this.EDSParam = new EDSParam();
            
            this.ScanRotCor = 1;
            this.PixelSizeCor = 1;
            
  
        }

        //XML文件保存测量参数
        public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
        {
            Slo sFile = new Slo();

            //是否拍照和PT
            xBool isPhotograph = new xBool();
            xBool ptDepostion = new xBool();
            xBool isTilt = new xBool();
            xBool isEDS = new xBool();
            isTilt.AssignValue(this.Tilt);
            isEDS.AssignValue(this.EDS);
            isPhotograph.AssignValue(this.is_Photograph);
            ptDepostion.AssignValue(this.m_pt);
            sFile.Register("Is_Title", isTilt);
            sFile.Register("Is_Photograph", isPhotograph);
            sFile.Register("PT_Depostion", ptDepostion);
            sFile.Register("Is_EDS", isEDS);

            //ELY文件
            xString ptELYFile = new xString();
            xString fibELYFile = new xString();
            ptELYFile.AssignValue(this.m_ptTemp);
            fibELYFile.AssignValue(this.m_fibTemp);
            sFile.Register("PT_ELYFile", ptELYFile);
            sFile.Register("FIB_ELYFile", fibELYFile);

            //对焦方式
            xInt focusmode = new xInt();
         
            sFile.Register("FocusMode", focusmode);

            //放大倍数和电压参数
            xDouble stretchMagnification = new xDouble();
            xDouble locationMagnification = new xDouble();
            xDouble locationVoltage = new xDouble();
            xDouble photographMagnification = new xDouble();
            xDouble photographVoltage = new xDouble();
            xDouble fibMagnification = new xDouble();
            xDouble ZDistance = new xDouble();
            xDouble xEDSZ = new xDouble();
            xDouble xEDSV = new xDouble();
            xDouble xEDSA = new xDouble();

            xEDSZ.AssignValue(this.edsZ);
            xEDSV.AssignValue(this.edsV);
            xEDSA.AssignValue(this.edsA);
            //ZDistance.AssignValue(this.ZDistance);
            stretchMagnification.AssignValue(this.straighten_Magnification);
            locationMagnification.AssignValue(this.location_Magnification);
            locationVoltage.AssignValue(this.mVoltage);
            photographMagnification.AssignValue(this.photograph_Magnification);
            photographVoltage.AssignValue(this.photograph_Voltage);
            fibMagnification.AssignValue(this.fib_Magnification);
            sFile.Register("Strectch_Magnification", stretchMagnification);
            sFile.Register("Locatio_Magnification", locationMagnification);
            sFile.Register("Location_Voltage", locationVoltage);
            sFile.Register("Photograph_Magnification", photographMagnification);
            sFile.Register("Photograph_Voltage", photographVoltage);
            sFile.Register("FIB_Magnification", fibMagnification);
            //sFile.Register("ZDistance", ZDistance);
            sFile.Register("EDSZ", xEDSZ);
            sFile.Register("EDSV", xEDSV);
            sFile.Register("EDSA", xEDSA);

            //校正角度
            xDouble correctionAngle = new xDouble();
            correctionAngle.AssignValue(this.correction_Angle_Inside);
            sFile.Register("Correction_Angle", correctionAngle);

            //样品名称和厂商
            xString sampleType = new xString();
            xString _firm = new xString();
            sampleType.AssignValue(this.m_SampleName);
            _firm.AssignValue(this.firm);
            sFile.Register("Sample_Type", sampleType);
            sFile.Register("Firm", _firm);

        
            sFile.Register("EDS_Param", this.EDSParam);


            //ScanRotate修正
            xDouble scanRotate = new xDouble();
            scanRotate.AssignValue(this.scanRotCor);
            sFile.Register("ScanRotateCorrect", scanRotate);


            //PixelSize_Y轴补偿
            xDouble pscor = new xDouble();
            pscor.AssignValue(this.pixelSizeCor);
            sFile.Register("PixelSizeYCorrect", pscor);

            if (isStoring)
            {
                sFile.Serialize(true, xml, rootNode);
            }
            else
            {
                sFile.Serialize(false, xml, rootNode);

                this.is_Photograph = isPhotograph.value();
                this.m_pt = ptDepostion.value();
                this.Tilt = isTilt.value();
                this.EDS = isEDS.value();
                this.m_ptTemp = ptELYFile.value();
                this.m_fibTemp = fibELYFile.value();

              

                this.straighten_Magnification = Convert.ToSingle(stretchMagnification.value());
                this.location_Magnification = Convert.ToSingle(locationMagnification.value());
                this.mVoltage = Convert.ToSingle(locationVoltage.value());
                this.photograph_Magnification = Convert.ToSingle(photographMagnification.value());
                this.photograph_Voltage = Convert.ToSingle(photographVoltage.value());

                this.correction_Angle_Inside  = Convert.ToSingle(correctionAngle.value());

                this.m_SampleName = sampleType.value();
                this.firm = _firm.value();
                //this.ZDistance = Convert.ToSingle(ZDistance.value());
                this.EDSZ = Convert.ToSingle(xEDSZ.value());
                this.EDSV = Convert.ToSingle(xEDSV.value());
                this.EDSA = Convert.ToSingle(xEDSA.value());


                this.ScanRotCor = Convert.ToSingle(scanRotate.value());
                this.PixelSizeCor = Convert.ToSingle(pscor.value());

            }
        }

    }
}
