using System; using System.Collections.Generic; namespace OTSModelSharp.ServiceCenter { using OTSCLRINTERFACE; using OTSMeasureApp.ServiceCenter; using System.Drawing; public class EDSController : IEDSController { private COTSControlFunExport eds; static IEDSController edsctrl = null; private List keyElenamelist = new List(); private bool delayQuant=false; public static IEDSController GetEDSController(int imgwidth,int imgheight,int expectCount,bool ifautoid,string knownelements) { string deviceType = FileHelper.GetXMLInformations("EDSName"); if (edsctrl == null) { if (deviceType == "FEI") { edsctrl = new FEIEDSController(expectCount,ifautoid,knownelements); } else if (deviceType == "Oxford") { edsctrl = new OxfordEDSController(expectCount,ifautoid,knownelements); } else if (deviceType == "Bruker") { var ctrl = new EDSController("Bruker",expectCount,ifautoid,knownelements); //var delayQuant = Convert.ToBoolean(FileHelper.GetIfDelayQuantify()); var delayQuant = false;//Now it has proved that this method won't increase the speed of xray analysis.So deactivate it here. ctrl.delayQuant = delayQuant; edsctrl = ctrl; } else if (deviceType == "OffLine") { edsctrl = new EDSController("OffLine",expectCount,ifautoid,knownelements); } edsctrl.SetResolution(imgwidth, imgheight); } return edsctrl; } private EDSController(string deviceType,int expectcount,bool ifautoid,string knownelements) { eds = COTSControlFunExport.GetControllerInstance(deviceType); eds.SetQuantificationParam(ifautoid, knownelements); eds.SetExpectCount(expectcount); } 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> eleInfoDic = new Dictionary>(); 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() { ele.GetPercentage() }); } } if (hasRepeatEle) { Dictionary resultInfo = new Dictionary(); 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 a_listParticles, double a_nXRayAQTime, bool a_bElementInfo) { bool result = false; if (!eds.IsConnected()) { return false; } if (keyElenamelist.Count > 0) { List elementChemistryClrs = new List(); for (int j = 0; j < keyElenamelist.Count; j++) { CElementChemistryClr chemistryClr = new CElementChemistryClr(); chemistryClr.SetName(keyElenamelist[j]); elementChemistryClrs.Add(chemistryClr); } for (int i = 0; i < a_listParticles.Count; i++) { a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs); } } COTSParticleClr[] parts = a_listParticles.ToArray(); if (delayQuant) { a_bElementInfo = false; } result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo); if (result == true) { foreach (var p in a_listParticles) { ProcessXrayInfo(p); } } return result; } public bool GetXRayByParts(List a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo) { bool result = false; 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; } if (keyElenamelist.Count > 0) { if (this.GetEDSType() == EDSTYPE.BRUKER) { List elementChemistryClrs = new List(); for (int j = 0; j < keyElenamelist.Count; j++) { CElementChemistryClr chemistryClr = new CElementChemistryClr(); chemistryClr.SetName(keyElenamelist[j]); elementChemistryClrs.Add(chemistryClr); } for (int i = 0; i < a_listParticles.Count; i++) { a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs); } } } int nSize = a_listParticles.Count; Point[] allpoints = new Point[nSize]; COTSParticleClr[] partsTemp = new COTSParticleClr[nSize]; for (int m = 0; m < nSize; m++) { partsTemp[m] = a_listParticles[m]; allpoints[m] = Ps[m]; } result = eds.GetXRayByPoints(a_nXRayAQTime, allpoints, partsTemp, a_bElementInfo); if (!result) { NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element."); return false; } if (result == true) { foreach (var p in a_listParticles) { ProcessXrayInfo(p); } } return result; } public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData) { if (!eds.IsConnected()) { return false; } return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData); } public bool Connect() { if (!eds.IsConnected()) { eds.ConncetSem(); } bool m_init = eds.EDSInit(); return m_init; } public EDSTYPE GetEDSType() { EDSTYPE t; switch (eds.EDSGetType()) { case 1: t = EDSTYPE.OFFLINE; break; case 3: t = EDSTYPE.BRUKER; break; case 4: t = EDSTYPE.OXFORD; break; default: t = EDSTYPE.OFFLINE; break; } return t; } public void SetFilterKeyEleNames(List KeyNameList) { this.keyElenamelist = KeyNameList; } public void SetResolution(int resolutionWidth, int resolutionHeight) { eds.SetImageSize(resolutionWidth, resolutionHeight); return ; } public bool QuantifyXrayByPart(COTSParticleClr part) { return eds.QuantifyXrayByPart(part); } public int GetExpectCount() { return eds.GetExpectCount(); } public bool GetIfDelayQuantify() { return delayQuant; } public void SetQuantifiCationParam(bool IfAutoId, string knownElements) { eds.SetQuantificationParam(IfAutoId, knownElements); } public bool GetXRayByExpandFeatures(List a_listParticles, double a_nXRayAQTime, bool a_bElementInfo) { bool result = false; if (!eds.IsConnected()) { return false; } if (keyElenamelist.Count > 0) { List elementChemistryClrs = new List(); for (int j = 0; j < keyElenamelist.Count; j++) { CElementChemistryClr chemistryClr = new CElementChemistryClr(); chemistryClr.SetName(keyElenamelist[j]); elementChemistryClrs.Add(chemistryClr); } for (int i = 0; i < a_listParticles.Count; i++) { a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs); } } COTSParticleClr[] parts = a_listParticles.ToArray(); if (delayQuant) { a_bElementInfo = false; } result = eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo); if (result == true) { foreach (var p in a_listParticles) { ProcessXrayInfo(p); } } return result; } } }