EDSController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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)
  16. {
  17. string deviceType = FileHelper.GetXMLInformations("EDSName");
  18. if (edsctrl == null)
  19. {
  20. if (deviceType == "FEI")
  21. {
  22. edsctrl = new FEIEDSController(expectCount);
  23. }
  24. else if (deviceType == "Oxford")
  25. {
  26. edsctrl = new OxfordEDSController(expectCount);
  27. }
  28. else if (deviceType == "Bruker")
  29. {
  30. var ctrl = new EDSController("Bruker",expectCount);
  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);
  39. }
  40. edsctrl.SetResolution(imgwidth, imgheight);
  41. }
  42. return edsctrl;
  43. }
  44. private EDSController(string deviceType,int expectcount)
  45. {
  46. eds = COTSControlFunExport.GetControllerInstance(deviceType);
  47. eds.SetExpectCount(expectcount);
  48. }
  49. private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.
  50. {
  51. var eleChemistry = partWithXrayInfo.GetXray().GetElementQuantifyData();
  52. Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();
  53. bool hasRepeatEle = false;
  54. foreach (var ele in eleChemistry)
  55. {
  56. if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;
  57. {
  58. eleInfoDic[ele.GetName()].Add(ele.GetPercentage());
  59. hasRepeatEle = true;
  60. }
  61. else
  62. {
  63. eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });
  64. }
  65. }
  66. if (hasRepeatEle)
  67. {
  68. Dictionary<string, double> resultInfo = new Dictionary<string, double>();
  69. foreach (var eleInfo in eleInfoDic)
  70. {
  71. double newPercentData=0;
  72. foreach (var p in eleInfo.Value)
  73. {
  74. newPercentData += p;
  75. }
  76. newPercentData = newPercentData / eleInfo.Value.Count;
  77. resultInfo.Add(eleInfo.Key, newPercentData);
  78. }
  79. foreach (var e in eleChemistry)
  80. {
  81. e.SetPercentage(resultInfo[e.GetName()]);
  82. }
  83. partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);
  84. }
  85. }
  86. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  87. {
  88. bool result = false;
  89. if (!eds.IsConnected())
  90. {
  91. return false;
  92. }
  93. if (keyElenamelist.Count > 0)
  94. {
  95. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  96. for (int j = 0; j < keyElenamelist.Count; j++)
  97. {
  98. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  99. chemistryClr.SetName(keyElenamelist[j]);
  100. elementChemistryClrs.Add(chemistryClr);
  101. }
  102. for (int i = 0; i < a_listParticles.Count; i++)
  103. {
  104. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  105. }
  106. }
  107. COTSParticleClr[] parts = a_listParticles.ToArray();
  108. if (delayQuant)
  109. {
  110. a_bElementInfo = false;
  111. }
  112. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  113. if (result == true)
  114. {
  115. foreach (var p in a_listParticles)
  116. {
  117. ProcessXrayInfo(p);
  118. }
  119. }
  120. return result;
  121. }
  122. public bool GetXRayByParts(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  123. {
  124. bool result = false;
  125. if (!eds.IsConnected())
  126. {
  127. return false;
  128. }
  129. int xrayNum = a_listParticles.Count;
  130. Point[] Ps = new Point[xrayNum];
  131. for (int i = 0; i < xrayNum; i++)
  132. {
  133. Point p = (Point)a_listParticles[i].GetXRayPos();
  134. Ps[i].X = p.X;
  135. Ps[i].Y = p.Y;
  136. }
  137. if (keyElenamelist.Count > 0)
  138. {
  139. if (this.GetEDSType() == EDSTYPE.BRUKER)
  140. {
  141. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  142. for (int j = 0; j < keyElenamelist.Count; j++)
  143. {
  144. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  145. chemistryClr.SetName(keyElenamelist[j]);
  146. elementChemistryClrs.Add(chemistryClr);
  147. }
  148. for (int i = 0; i < a_listParticles.Count; i++)
  149. {
  150. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  151. }
  152. }
  153. }
  154. int nSize = a_listParticles.Count;
  155. if (nSize > 1024)
  156. {
  157. COTSParticleClr[] partsTemp = new COTSParticleClr[1024];
  158. Point[] PsTemp = new Point[1024];
  159. int nTimes = nSize / 1024;
  160. for (int i = 0; i < nTimes; i++)
  161. {
  162. NLog.LogManager.GetCurrentClassLogger().Warn("begin 1024 batch");
  163. for (int m = 0; m < 1024; m++)
  164. {
  165. partsTemp[m]=a_listParticles[i * 1024 + m];
  166. PsTemp[m] = Ps[i * 1024 + m];
  167. }
  168. if (!eds.GetXRayByPoints(a_nXRayAQTime, PsTemp, partsTemp, a_bElementInfo))
  169. {
  170. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  171. return false;
  172. }
  173. }
  174. int nLast = nSize % 1024;
  175. if (nLast != 0)
  176. {
  177. COTSParticleClr[] lastParts = new COTSParticleClr[nLast];
  178. Point[] lastPs = new Point[nLast];
  179. for (int m = 0; m < nLast; m++)
  180. {
  181. lastParts[m] = a_listParticles[nTimes * 1024 + m];
  182. lastPs[m] = Ps[nTimes * 1024 + m];
  183. }
  184. if (!eds.GetXRayByPoints(a_nXRayAQTime, lastPs, lastParts, a_bElementInfo))
  185. {
  186. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  187. return false;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. COTSParticleClr[] parts = a_listParticles.ToArray();
  194. if (delayQuant)
  195. {
  196. a_bElementInfo = false;
  197. }
  198. result = eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  199. }
  200. if (result == true)
  201. {
  202. foreach (var p in a_listParticles)
  203. {
  204. ProcessXrayInfo(p);
  205. }
  206. }
  207. return result;
  208. }
  209. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  210. {
  211. if (!eds.IsConnected())
  212. {
  213. return false;
  214. }
  215. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  216. }
  217. public bool Connect()
  218. {
  219. if (!eds.IsConnected())
  220. {
  221. eds.ConncetSem();
  222. }
  223. bool m_init = eds.EDSInit();
  224. return m_init;
  225. }
  226. public EDSTYPE GetEDSType()
  227. {
  228. EDSTYPE t;
  229. switch (eds.EDSGetType())
  230. {
  231. case 1:
  232. t = EDSTYPE.OFFLINE;
  233. break;
  234. case 3:
  235. t = EDSTYPE.BRUKER;
  236. break;
  237. case 4:
  238. t = EDSTYPE.OXFORD;
  239. break;
  240. default:
  241. t = EDSTYPE.OFFLINE;
  242. break;
  243. }
  244. return t;
  245. }
  246. public void SetFilterKeyEleNames(List<string> KeyNameList)
  247. {
  248. this.keyElenamelist = KeyNameList;
  249. }
  250. public void SetResolution(int resolutionWidth, int resolutionHeight)
  251. {
  252. eds.SetImageSize(resolutionWidth, resolutionHeight);
  253. return ;
  254. }
  255. public bool QuantifyXrayByPart(COTSParticleClr part)
  256. {
  257. return eds.QuantifyXrayByPart(part);
  258. }
  259. public int GetExpectCount()
  260. {
  261. return eds.GetExpectCount();
  262. }
  263. public bool GetIfDelayQuantify()
  264. {
  265. return delayQuant;
  266. }
  267. }
  268. }