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 { public COTSControlFunExport eds; static EDSController edsctrl = null; public static APIClass ApiClass = null; private static bool isFEI = false; private int AnalyExpCount = 100000; private string strResolution = ""; private int width = 0; private int height = 0; public List nameList = new List(); 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> 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 (isFEI) { for (int i = 0; i < a_listParticles.Count; i++) { Dictionary eleItems = new Dictionary(); uint[] spectrumItems = new uint[2000]; if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫 { List points = CImageHandler.FindContoursBySegment(width, height, a_listParticles[i].GetFeature().GetSegmentsList()); if (!ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems)) { return false; } } else { Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect(); 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 elementChemistryClrs = new List(); 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; } if (eds.GetEDSName() == "EDS Bruker") { for (int i = 0; i < a_listParticles.Count; i++) { List elementChemistryClrs = new List(); for (int j = 0; j < nameList.Count; j++) { CElementChemistryClr chemistryClr = new CElementChemistryClr(); chemistryClr.SetName(nameList[j]); elementChemistryClrs.Add(chemistryClr); } a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs); } } 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 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 eleItems = new Dictionary(); 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 elementChemistryClrs = new List(); 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; } if (eds.GetEDSName() == "EDS Bruker") { for (int i = 0; i < a_listParticles.Count; i++) { List elementChemistryClrs = new List(); for (int j = 0; j < nameList.Count; j++) { CElementChemistryClr chemistryClr = new CElementChemistryClr(); chemistryClr.SetName(nameList[j]); elementChemistryClrs.Add(chemistryClr); } a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs); } } 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 eleItems = new Dictionary(); 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 eleItems = new Dictionary(); 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) { Dictionary eleItems = new Dictionary(); if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫 { List points = CImageHandler.FindContoursBySegment(width, height, fea.GetSegmentsList()); return ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData); } else { 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); 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 eleItems = new Dictionary(); 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 eleItems = new Dictionary(); 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) { width = ResolutionWidth; height = ResolutionHeight; strResolution = ResolutionWidth + "x" + ResolutionHeight; } return true; } public bool SetAnalyExpCount(int MaxCounts) { if (isFEI) { AnalyExpCount = MaxCounts; } return true; } } }