EDSController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. namespace OTSModelSharp.ServiceCenter
  5. {
  6. using OTSCLRINTERFACE;
  7. using OTSMeasureApp.ServiceCenter;
  8. using OTSMeasureApp.ServiceCenter.Coxm;
  9. using OTSMeasureApp.ServiceCenter.PicoSmart;
  10. using System.Drawing;
  11. public class EDSController : IEDSController
  12. {
  13. private COTSControlFunExport eds;
  14. static IEDSController edsctrl = null;
  15. private List<String> keyElenamelist = new List<string>();
  16. public bool delayQuant=false;
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. /// <param name="SemType">sem type</param>
  21. /// <param name="deviceType"> eds type</param>
  22. /// <param name="imgwidth"></param>
  23. /// <param name="imgheight"></param>
  24. /// <param name="expectCount"></param>
  25. /// <param name="ifautoid"></param>
  26. /// <param name="knownelements"></param>
  27. /// <returns></returns>
  28. public static IEDSController GetEDSController(string SemType,string deviceType,int imgwidth,int imgheight,int expectCount,bool ifautoid,string knownelements)
  29. {
  30. // string deviceType = FileHelper.GetXMLInformations("EDSName");
  31. if (edsctrl == null)
  32. {
  33. switch (SemType)
  34. {
  35. case "FEI":
  36. if (deviceType == "FEI")
  37. {
  38. edsctrl = new FEIEDSController(expectCount, ifautoid, knownelements);
  39. }
  40. else if (deviceType == "Oxford")
  41. {
  42. edsctrl = new OxfordEDSController(expectCount, ifautoid, knownelements);
  43. }
  44. else if (deviceType == "Bruker")
  45. {
  46. var ctrl = new EDSController("Bruker", expectCount, ifautoid, knownelements);
  47. //var delayQuant = Convert.ToBoolean(FileHelper.GetIfDelayQuantify());
  48. var delayQuant = false;//Now it has proved that this method won't increase the speed of xray analysis.So deactivate it here.
  49. ctrl.delayQuant = delayQuant;
  50. edsctrl = ctrl;
  51. }
  52. break;
  53. case "ZEISS":
  54. if (deviceType == "Oxford")
  55. {
  56. edsctrl = new OxfordEDSController(expectCount, ifautoid, knownelements);
  57. }
  58. else if (deviceType == "Bruker")
  59. {
  60. var ctrl = new EDSController("Bruker", expectCount, ifautoid, knownelements);
  61. //var delayQuant = Convert.ToBoolean(FileHelper.GetIfDelayQuantify());
  62. var delayQuant = false;//Now it has proved that this method won't increase the speed of xray analysis.So deactivate it here.
  63. ctrl.delayQuant = delayQuant;
  64. edsctrl = ctrl;
  65. }
  66. break;
  67. case "Coxm":
  68. edsctrl = new CoxmEDSController(SemType, deviceType, imgwidth, imgheight, expectCount, ifautoid, knownelements);
  69. break;
  70. case "PicoSmart":
  71. edsctrl = new PicoSmartEDSController(SemType, deviceType, imgwidth, imgheight, expectCount, ifautoid, knownelements);
  72. break;
  73. case "ComposeEds"://coxm picosmart ComposeEds;coxm picosmart 电镜组合eds
  74. if (deviceType == "Oxford")
  75. {
  76. edsctrl = new OxfordEDSController(expectCount, ifautoid, knownelements);
  77. }
  78. else if (deviceType == "Bruker")
  79. {
  80. var ctrl = new EDSController("Bruker", expectCount, ifautoid, knownelements);
  81. //var delayQuant = Convert.ToBoolean(FileHelper.GetIfDelayQuantify());
  82. var delayQuant = false;//Now it has proved that this method won't increase the speed of xray analysis.So deactivate it here.
  83. ctrl.delayQuant = delayQuant;
  84. edsctrl = ctrl;
  85. }
  86. break;
  87. case "OffLine":
  88. edsctrl = new EDSController("OffLine", expectCount, ifautoid, knownelements);
  89. break;
  90. default:
  91. edsctrl = new EDSController("OffLine", expectCount, ifautoid, knownelements);
  92. break;
  93. }
  94. edsctrl.SetResolution(imgwidth, imgheight);
  95. }
  96. return edsctrl;
  97. }
  98. public EDSController(string deviceType,int expectcount,bool ifautoid,string knownelements)
  99. {
  100. eds = COTSControlFunExport.GetControllerInstance(deviceType);
  101. eds.SetQuantificationParam(ifautoid, knownelements);
  102. eds.SetExpectCount(expectcount);
  103. }
  104. private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.
  105. {
  106. var eleChemistry = partWithXrayInfo.GetXray().GetElementQuantifyData();
  107. Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();
  108. bool hasRepeatEle = false;
  109. foreach (var ele in eleChemistry)
  110. {
  111. if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;
  112. {
  113. eleInfoDic[ele.GetName()].Add(ele.GetPercentage());
  114. hasRepeatEle = true;
  115. }
  116. else
  117. {
  118. eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });
  119. }
  120. }
  121. if (hasRepeatEle)
  122. {
  123. Dictionary<string, double> resultInfo = new Dictionary<string, double>();
  124. foreach (var eleInfo in eleInfoDic)
  125. {
  126. double newPercentData=0;
  127. foreach (var p in eleInfo.Value)
  128. {
  129. newPercentData += p;
  130. }
  131. newPercentData = newPercentData / eleInfo.Value.Count;
  132. resultInfo.Add(eleInfo.Key, newPercentData);
  133. }
  134. foreach (var e in eleChemistry)
  135. {
  136. e.SetPercentage(resultInfo[e.GetName()]);
  137. }
  138. partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);
  139. }
  140. }
  141. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  142. {
  143. bool result = false;
  144. if (!eds.IsConnected())
  145. {
  146. return false;
  147. }
  148. if (keyElenamelist.Count > 0)
  149. {
  150. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  151. for (int j = 0; j < keyElenamelist.Count; j++)
  152. {
  153. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  154. chemistryClr.SetName(keyElenamelist[j]);
  155. elementChemistryClrs.Add(chemistryClr);
  156. }
  157. for (int i = 0; i < a_listParticles.Count; i++)
  158. {
  159. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  160. }
  161. }
  162. COTSParticleClr[] parts = a_listParticles.ToArray();
  163. if (delayQuant)
  164. {
  165. a_bElementInfo = false;
  166. }
  167. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  168. if (result == true)
  169. {
  170. foreach (var p in a_listParticles)
  171. {
  172. ProcessXrayInfo(p);
  173. }
  174. }
  175. return result;
  176. }
  177. public bool GetXRayByParts(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  178. {
  179. bool result = false;
  180. if (!eds.IsConnected())
  181. {
  182. return false;
  183. }
  184. int xrayNum = a_listParticles.Count;
  185. Point[] Ps = new Point[xrayNum];
  186. for (int i = 0; i < xrayNum; i++)
  187. {
  188. Point p = (Point)a_listParticles[i].GetXRayPos();
  189. Ps[i].X = p.X;
  190. Ps[i].Y = p.Y;
  191. }
  192. if (keyElenamelist.Count > 0)
  193. {
  194. if (this.GetEDSType() == EDSTYPE.BRUKER)
  195. {
  196. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  197. for (int j = 0; j < keyElenamelist.Count; j++)
  198. {
  199. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  200. chemistryClr.SetName(keyElenamelist[j]);
  201. elementChemistryClrs.Add(chemistryClr);
  202. }
  203. for (int i = 0; i < a_listParticles.Count; i++)
  204. {
  205. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  206. }
  207. }
  208. }
  209. int nSize = a_listParticles.Count;
  210. if (nSize > 1024)
  211. {
  212. COTSParticleClr[] partsTemp = new COTSParticleClr[1024];
  213. Point[] PsTemp = new Point[1024];
  214. int nTimes = nSize / 1024;
  215. for (int i = 0; i < nTimes; i++)
  216. {
  217. NLog.LogManager.GetCurrentClassLogger().Warn("begin 1024 batch");
  218. for (int m = 0; m < 1024; m++)
  219. {
  220. partsTemp[m]=a_listParticles[i * 1024 + m];
  221. PsTemp[m] = Ps[i * 1024 + m];
  222. }
  223. if (!eds.GetXRayByPoints(a_nXRayAQTime, PsTemp, partsTemp, a_bElementInfo))
  224. {
  225. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  226. return false;
  227. }
  228. }
  229. int nLast = nSize % 1024;
  230. if (nLast != 0)
  231. {
  232. COTSParticleClr[] lastParts = new COTSParticleClr[nLast];
  233. Point[] lastPs = new Point[nLast];
  234. for (int m = 0; m < nLast; m++)
  235. {
  236. lastParts[m] = a_listParticles[nTimes * 1024 + m];
  237. lastPs[m] = Ps[nTimes * 1024 + m];
  238. }
  239. if (!eds.GetXRayByPoints(a_nXRayAQTime, lastPs, lastParts, a_bElementInfo))
  240. {
  241. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  242. return false;
  243. }
  244. }
  245. }
  246. else
  247. {
  248. COTSParticleClr[] parts = a_listParticles.ToArray();
  249. if (delayQuant)
  250. {
  251. a_bElementInfo = false;
  252. }
  253. result = eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  254. }
  255. if (result == true)
  256. {
  257. foreach (var p in a_listParticles)
  258. {
  259. ProcessXrayInfo(p);
  260. }
  261. }
  262. return result;
  263. }
  264. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  265. {
  266. if (!eds.IsConnected())
  267. {
  268. return false;
  269. }
  270. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  271. }
  272. public bool Connect()
  273. {
  274. if (!eds.IsConnected())
  275. {
  276. eds.ConncetSem();
  277. }
  278. bool m_init = eds.EDSInit();
  279. return m_init;
  280. }
  281. public EDSTYPE GetEDSType()
  282. {
  283. EDSTYPE t;
  284. switch (eds.EDSGetType())
  285. {
  286. case 1:
  287. t = EDSTYPE.OFFLINE;
  288. break;
  289. case 3:
  290. t = EDSTYPE.BRUKER;
  291. break;
  292. case 4:
  293. t = EDSTYPE.OXFORD;
  294. break;
  295. default:
  296. t = EDSTYPE.OFFLINE;
  297. break;
  298. }
  299. return t;
  300. }
  301. public void SetFilterKeyEleNames(List<string> KeyNameList)
  302. {
  303. this.keyElenamelist = KeyNameList;
  304. }
  305. public void SetResolution(int resolutionWidth, int resolutionHeight)
  306. {
  307. eds.SetImageSize(resolutionWidth, resolutionHeight);
  308. return ;
  309. }
  310. public bool QuantifyXrayByPart(COTSParticleClr part)
  311. {
  312. return eds.QuantifyXrayByPart(part);
  313. }
  314. public int GetExpectCount()
  315. {
  316. return eds.GetExpectCount();
  317. }
  318. public bool GetIfDelayQuantify()
  319. {
  320. return delayQuant;
  321. }
  322. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  323. {
  324. eds.SetQuantificationParam(IfAutoId, knownElements);
  325. }
  326. public bool GetXRayByExpandFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  327. {
  328. throw new NotImplementedException();
  329. }
  330. }
  331. }