EDSController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. Point[] allpoints = new Point[nSize];
  157. COTSParticleClr[] partsTemp = new COTSParticleClr[nSize];
  158. for (int m = 0; m < nSize; m++)
  159. {
  160. partsTemp[m] = a_listParticles[m];
  161. allpoints[m] = Ps[m];
  162. }
  163. result = eds.GetXRayByPoints(a_nXRayAQTime, allpoints, partsTemp, a_bElementInfo);
  164. if (!result)
  165. {
  166. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  167. return false;
  168. }
  169. if (result == true)
  170. {
  171. foreach (var p in a_listParticles)
  172. {
  173. ProcessXrayInfo(p);
  174. }
  175. }
  176. return result;
  177. }
  178. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  179. {
  180. if (!eds.IsConnected())
  181. {
  182. return false;
  183. }
  184. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  185. }
  186. public bool Connect()
  187. {
  188. if (!eds.IsConnected())
  189. {
  190. eds.ConncetSem();
  191. }
  192. bool m_init = eds.EDSInit();
  193. return m_init;
  194. }
  195. public EDSTYPE GetEDSType()
  196. {
  197. EDSTYPE t;
  198. switch (eds.EDSGetType())
  199. {
  200. case 1:
  201. t = EDSTYPE.OFFLINE;
  202. break;
  203. case 3:
  204. t = EDSTYPE.BRUKER;
  205. break;
  206. case 4:
  207. t = EDSTYPE.OXFORD;
  208. break;
  209. default:
  210. t = EDSTYPE.OFFLINE;
  211. break;
  212. }
  213. return t;
  214. }
  215. public void SetFilterKeyEleNames(List<string> KeyNameList)
  216. {
  217. this.keyElenamelist = KeyNameList;
  218. }
  219. public void SetResolution(int resolutionWidth, int resolutionHeight)
  220. {
  221. eds.SetImageSize(resolutionWidth, resolutionHeight);
  222. return ;
  223. }
  224. public bool QuantifyXrayByPart(COTSParticleClr part)
  225. {
  226. return eds.QuantifyXrayByPart(part);
  227. }
  228. public int GetExpectCount()
  229. {
  230. return eds.GetExpectCount();
  231. }
  232. public bool GetIfDelayQuantify()
  233. {
  234. return delayQuant;
  235. }
  236. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  237. {
  238. eds.SetQuantificationParam(IfAutoId, knownElements);
  239. }
  240. public bool GetXRayByExpandFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  241. {
  242. bool result = false;
  243. if (!eds.IsConnected())
  244. {
  245. return false;
  246. }
  247. if (keyElenamelist.Count > 0)
  248. {
  249. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  250. for (int j = 0; j < keyElenamelist.Count; j++)
  251. {
  252. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  253. chemistryClr.SetName(keyElenamelist[j]);
  254. elementChemistryClrs.Add(chemistryClr);
  255. }
  256. for (int i = 0; i < a_listParticles.Count; i++)
  257. {
  258. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  259. }
  260. }
  261. COTSParticleClr[] parts = a_listParticles.ToArray();
  262. if (delayQuant)
  263. {
  264. a_bElementInfo = false;
  265. }
  266. result = eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  267. if (result == true)
  268. {
  269. foreach (var p in a_listParticles)
  270. {
  271. ProcessXrayInfo(p);
  272. }
  273. }
  274. return result;
  275. }
  276. }
  277. }