EDSController.cs 9.4 KB

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