EDSController.cs 12 KB

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