EDSController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "OffLine":
  74. edsctrl = new EDSController("OffLine", expectCount, ifautoid, knownelements);
  75. break;
  76. default:
  77. edsctrl = new EDSController("OffLine", expectCount, ifautoid, knownelements);
  78. break;
  79. }
  80. edsctrl.SetResolution(imgwidth, imgheight);
  81. }
  82. return edsctrl;
  83. }
  84. public EDSController(string deviceType,int expectcount,bool ifautoid,string knownelements)
  85. {
  86. eds = COTSControlFunExport.GetControllerInstance(deviceType);
  87. eds.SetQuantificationParam(ifautoid, knownelements);
  88. eds.SetExpectCount(expectcount);
  89. }
  90. private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.
  91. {
  92. var eleChemistry = partWithXrayInfo.GetXray().GetElementQuantifyData();
  93. Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();
  94. bool hasRepeatEle = false;
  95. foreach (var ele in eleChemistry)
  96. {
  97. if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;
  98. {
  99. eleInfoDic[ele.GetName()].Add(ele.GetPercentage());
  100. hasRepeatEle = true;
  101. }
  102. else
  103. {
  104. eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });
  105. }
  106. }
  107. if (hasRepeatEle)
  108. {
  109. Dictionary<string, double> resultInfo = new Dictionary<string, double>();
  110. foreach (var eleInfo in eleInfoDic)
  111. {
  112. double newPercentData=0;
  113. foreach (var p in eleInfo.Value)
  114. {
  115. newPercentData += p;
  116. }
  117. newPercentData = newPercentData / eleInfo.Value.Count;
  118. resultInfo.Add(eleInfo.Key, newPercentData);
  119. }
  120. foreach (var e in eleChemistry)
  121. {
  122. e.SetPercentage(resultInfo[e.GetName()]);
  123. }
  124. partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);
  125. }
  126. }
  127. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  128. {
  129. bool result = false;
  130. if (!eds.IsConnected())
  131. {
  132. return false;
  133. }
  134. if (keyElenamelist.Count > 0)
  135. {
  136. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  137. for (int j = 0; j < keyElenamelist.Count; j++)
  138. {
  139. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  140. chemistryClr.SetName(keyElenamelist[j]);
  141. elementChemistryClrs.Add(chemistryClr);
  142. }
  143. for (int i = 0; i < a_listParticles.Count; i++)
  144. {
  145. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  146. }
  147. }
  148. COTSParticleClr[] parts = a_listParticles.ToArray();
  149. if (delayQuant)
  150. {
  151. a_bElementInfo = false;
  152. }
  153. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  154. if (result == true)
  155. {
  156. foreach (var p in a_listParticles)
  157. {
  158. ProcessXrayInfo(p);
  159. }
  160. }
  161. return result;
  162. }
  163. public bool GetXRayByParts(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  164. {
  165. bool result = false;
  166. if (!eds.IsConnected())
  167. {
  168. return false;
  169. }
  170. int xrayNum = a_listParticles.Count;
  171. Point[] Ps = new Point[xrayNum];
  172. for (int i = 0; i < xrayNum; i++)
  173. {
  174. Point p = (Point)a_listParticles[i].GetXRayPos();
  175. Ps[i].X = p.X;
  176. Ps[i].Y = p.Y;
  177. }
  178. if (keyElenamelist.Count > 0)
  179. {
  180. if (this.GetEDSType() == EDSTYPE.BRUKER)
  181. {
  182. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  183. for (int j = 0; j < keyElenamelist.Count; j++)
  184. {
  185. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  186. chemistryClr.SetName(keyElenamelist[j]);
  187. elementChemistryClrs.Add(chemistryClr);
  188. }
  189. for (int i = 0; i < a_listParticles.Count; i++)
  190. {
  191. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  192. }
  193. }
  194. }
  195. int nSize = a_listParticles.Count;
  196. if (nSize > 1024)
  197. {
  198. COTSParticleClr[] partsTemp = new COTSParticleClr[1024];
  199. Point[] PsTemp = new Point[1024];
  200. int nTimes = nSize / 1024;
  201. for (int i = 0; i < nTimes; i++)
  202. {
  203. NLog.LogManager.GetCurrentClassLogger().Warn("begin 1024 batch");
  204. for (int m = 0; m < 1024; m++)
  205. {
  206. partsTemp[m]=a_listParticles[i * 1024 + m];
  207. PsTemp[m] = Ps[i * 1024 + m];
  208. }
  209. if (!eds.GetXRayByPoints(a_nXRayAQTime, PsTemp, partsTemp, a_bElementInfo))
  210. {
  211. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  212. return false;
  213. }
  214. }
  215. int nLast = nSize % 1024;
  216. if (nLast != 0)
  217. {
  218. COTSParticleClr[] lastParts = new COTSParticleClr[nLast];
  219. Point[] lastPs = new Point[nLast];
  220. for (int m = 0; m < nLast; m++)
  221. {
  222. lastParts[m] = a_listParticles[nTimes * 1024 + m];
  223. lastPs[m] = Ps[nTimes * 1024 + m];
  224. }
  225. if (!eds.GetXRayByPoints(a_nXRayAQTime, lastPs, lastParts, a_bElementInfo))
  226. {
  227. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  228. return false;
  229. }
  230. }
  231. }
  232. else
  233. {
  234. COTSParticleClr[] parts = a_listParticles.ToArray();
  235. if (delayQuant)
  236. {
  237. a_bElementInfo = false;
  238. }
  239. result = eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  240. }
  241. if (result == true)
  242. {
  243. foreach (var p in a_listParticles)
  244. {
  245. ProcessXrayInfo(p);
  246. }
  247. }
  248. return result;
  249. }
  250. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  251. {
  252. if (!eds.IsConnected())
  253. {
  254. return false;
  255. }
  256. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  257. }
  258. public bool Connect()
  259. {
  260. if (!eds.IsConnected())
  261. {
  262. eds.ConncetSem();
  263. }
  264. bool m_init = eds.EDSInit();
  265. return m_init;
  266. }
  267. public EDSTYPE GetEDSType()
  268. {
  269. EDSTYPE t;
  270. switch (eds.EDSGetType())
  271. {
  272. case 1:
  273. t = EDSTYPE.OFFLINE;
  274. break;
  275. case 3:
  276. t = EDSTYPE.BRUKER;
  277. break;
  278. case 4:
  279. t = EDSTYPE.OXFORD;
  280. break;
  281. default:
  282. t = EDSTYPE.OFFLINE;
  283. break;
  284. }
  285. return t;
  286. }
  287. public void SetFilterKeyEleNames(List<string> KeyNameList)
  288. {
  289. this.keyElenamelist = KeyNameList;
  290. }
  291. public void SetResolution(int resolutionWidth, int resolutionHeight)
  292. {
  293. eds.SetImageSize(resolutionWidth, resolutionHeight);
  294. return ;
  295. }
  296. public bool QuantifyXrayByPart(COTSParticleClr part)
  297. {
  298. return eds.QuantifyXrayByPart(part);
  299. }
  300. public int GetExpectCount()
  301. {
  302. return eds.GetExpectCount();
  303. }
  304. public bool GetIfDelayQuantify()
  305. {
  306. return delayQuant;
  307. }
  308. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  309. {
  310. eds.SetQuantificationParam(IfAutoId, knownElements);
  311. }
  312. public bool GetXRayByExpandFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  313. {
  314. throw new NotImplementedException();
  315. }
  316. }
  317. }