| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 | using OTSDataType;using FEIApiControl;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using OTSCLRINTERFACE;namespace OTSModelSharp.ServiceInterface{    using  OTSCLRINTERFACE;    using System.Drawing;    using System.Windows.Forms;    public class EDSController : IEDSController    {        private COTSControlFunExport eds;        static EDSController edsctrl = null;        public static APIClass ApiClass = null;        private static bool isFEI = false;        private int AnalyExpCount = 100000;        private string strResolution = "";        public static EDSController GetEDSController()        {            if (FileHelper.GetXMLInformations("EDSName") == "FEI")            {                isFEI = true;            }            if (edsctrl == null)            {                edsctrl = new EDSController();            }            return edsctrl;        }        private EDSController()        {            if (isFEI)            {                ApiClass = new APIClass();                Connect();            }            else            {                eds = OTSCLRINTERFACE.COTSControlFunExport.GetControllerInstance();            }        }        private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.        {         var eleChemistry =  partWithXrayInfo.GetXray().GetElementQuantifyData();            Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();            bool hasRepeatEle = false;            foreach (var ele in eleChemistry)            {                if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;                {                    eleInfoDic[ele.GetName()].Add(ele.GetPercentage());                    hasRepeatEle = true;                }                else                {                    eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });                }            }            if (hasRepeatEle)            {                Dictionary<string, double> resultInfo = new Dictionary<string, double>();                foreach (var eleInfo in eleInfoDic)                {                    double newPercentData=0;                    foreach (var p  in eleInfo.Value)                    {                        newPercentData += p;                    }                    newPercentData = newPercentData / eleInfo.Value.Count;                    resultInfo.Add(eleInfo.Key, newPercentData);                              }                foreach (var e in eleChemistry)                {                    e.SetPercentage(resultInfo[e.GetName()]);                                }                partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);            }        }        public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles,  double a_nXRayAQTime, bool a_bElementInfo)        {            bool result = false;            if (isFEI)            {                for (int i = 0; i < a_listParticles.Count; i++)                {                    Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect();                    Dictionary<string, double> eleItems = new Dictionary<string, double>();                    uint[] spectrumItems = new uint[2000];                    if (!ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))                    {                        return false;                    }                    a_listParticles[i].GetXray().SetXrayData(spectrumItems);                    if (a_bElementInfo)                    {                        List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();                        for (int j = 0; j < eleItems.Count; j++)                        {                            CElementChemistryClr chemistryClr = new CElementChemistryClr();                            chemistryClr.SetName(eleItems.ElementAt(j).Key);                            chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);                            elementChemistryClrs.Add(chemistryClr);                        }                        a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);                    }                }                result = true;                //return true;            }            else            {                if (!eds.IsConnected())                {                    return false;                }                COTSParticleClr[] parts = a_listParticles.ToArray();                result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);            }            if (result == true)            {                foreach (var p in a_listParticles)                {                    ProcessXrayInfo(p);                }                                       }            return result;        }        public bool GetXRayByPoints(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)        {            bool result = false;            if (isFEI)            {                for (int i = 0; i < a_listParticles.Count; i++)                {                    Point point = (Point)a_listParticles[i].GetXRayPos();                    Dictionary<string, double> eleItems = new Dictionary<string, double>();                    uint[] spectrumItems = new uint[2000];                    if (!ApiClass.GetXRayByPoint(point.X, point.Y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))                    {                        return false;                    }                    a_listParticles[i].GetXray().SetXrayData(spectrumItems);                    if (a_bElementInfo)                    {                        List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();                        for (int j = 0; j < eleItems.Count; j++)                        {                            CElementChemistryClr chemistryClr = new CElementChemistryClr();                            chemistryClr.SetName(eleItems.ElementAt(j).Key);                            chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);                            elementChemistryClrs.Add(chemistryClr);                        }                        a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);                    }                }                result= true;            }            else            {                if (!eds.IsConnected())                {                    return false;                }                int xrayNum = a_listParticles.Count;                Point[] Ps = new Point[xrayNum];                for (int i = 0; i < xrayNum; i++)                {                    Point p = (Point)a_listParticles[i].GetXRayPos();                    Ps[i].X = p.X;                    Ps[i].Y = p.Y;                }                COTSParticleClr[] parts = a_listParticles.ToArray();                result= eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);            }            if (result == true)            {                foreach (var p in a_listParticles)                {                    ProcessXrayInfo(p);                }            }            return result;        }        public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)        {            if (isFEI)            {                Dictionary<string, double> eleItems = new Dictionary<string, double>();                return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);            }            else            {                if (!eds.IsConnected())                {                    return false;                }                return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);            }        }        public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)        {            if (isFEI)            {                double x = point.X;                double y = point.Y;                double z = 0;                double r = 0;                double t = 0;                Dictionary<string, double> eleItems = new Dictionary<string, double>();                if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))                {                    return false;                }                return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);            }            else            {                if (!eds.IsConnected())                {                    return false;                }                return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);            }        }        public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)        {            if (isFEI)            {                Rectangle rectangle = new Rectangle();                rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());                rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());                rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());                rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);                Dictionary<string, double> eleItems = new Dictionary<string, double>();                return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);            }            else            {                if (!eds.IsConnected())                {                    return false;                }                return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);            }        }        public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)        {            if (isFEI)            {                Dictionary<string, double> eleItems = new Dictionary<string, double>();                bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);                a_nElementNum = eleItems.Count;                for (int i = 0; i < eleItems.Count; i++)                {                    a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";                }                return isTrue;            }            else            {                if (!eds.IsConnected())                {                    return false;                }                return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);            }        }        public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)        {            if (isFEI)            {                Dictionary<string, double> eleItems = new Dictionary<string, double>();                bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);                a_nElementNum = eleItems.Count;                for (int i = 0; i < eleItems.Count; i++)                {                    a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";                }                return isTrue;            }            else            {                if (!eds.IsConnected())                {                    return false;                }                return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);            }        }        public bool Init()        {            if (isFEI)            {                return ApiClass.isConnect();            }            else            {                bool m_init = eds.EDSInit();                return m_init;            }        }        public bool ScanInit()        {            if (isFEI)            {                return ApiClass.isConnect();            }            else            {                bool m_init = eds.ScanInit();                return m_init;            }        }        public bool Connect()        {            if (isFEI)            {                string FEIIP = FileHelper.GetXMLInformations("FEIIP");                string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");                if (FEIIP == "" || FEIPORT == "")                {                    NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");                    return false;                }                if (ApiClass.isConnect())                {                    return true;                }                return ApiClass.Connect(FEIIP, FEIPORT);            }            else            {                if (!eds.Init())                {                    return false;                }                if (eds.IsConnected())                {                    return true;                }                return eds.ConncetSem();            }        }        public bool DisConnect()        {            if (isFEI)            {                return ApiClass.DisConnect();            }            else            {                if (eds.IsConnected())                {                    return eds.DisconnectSem();                }                return true;            }        }        public bool SetResolution(int ResolutionWidth, int ResolutionHeight)        {            if (isFEI)            {                strResolution = ResolutionWidth + "x" + ResolutionHeight;            }            return true;        }        public bool SetAnalyExpCount(int MaxCounts)        {            if (isFEI)            {                AnalyExpCount = MaxCounts;            }            return true;        }    }}
 |