EDSController.cs 11 KB

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