EDSController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. for (int m = 0; m < 1024; m++)
  146. {
  147. partsTemp[m]=a_listParticles[i * 1024 + m];
  148. PsTemp[m] = Ps[i * 1024 + m];
  149. }
  150. if (!eds.GetXRayByPoints(a_nXRayAQTime, PsTemp, partsTemp, a_bElementInfo))
  151. {
  152. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  153. return false;
  154. }
  155. }
  156. int nLast = nSize % 1024;
  157. if (nLast != 0)
  158. {
  159. COTSParticleClr[] lastParts = new COTSParticleClr[nLast];
  160. Point[] lastPs = new Point[nLast];
  161. for (int m = 0; m < nLast; m++)
  162. {
  163. lastParts[m] = a_listParticles[nTimes * 1024 + m];
  164. lastPs[m] = Ps[nTimes * 1024 + m];
  165. }
  166. if (!eds.GetXRayByPoints(a_nXRayAQTime, lastPs, lastParts, a_bElementInfo))
  167. {
  168. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  169. return false;
  170. }
  171. }
  172. }
  173. else
  174. {
  175. COTSParticleClr[] parts = a_listParticles.ToArray();
  176. result = eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  177. }
  178. if (result == true)
  179. {
  180. foreach (var p in a_listParticles)
  181. {
  182. ProcessXrayInfo(p);
  183. }
  184. }
  185. return result;
  186. }
  187. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  188. {
  189. if (!eds.IsConnected())
  190. {
  191. return false;
  192. }
  193. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  194. }
  195. public bool Init()
  196. {
  197. if (!eds.IsConnected())
  198. {
  199. eds.ConncetSem();
  200. }
  201. bool m_init = eds.EDSInit();
  202. return m_init;
  203. }
  204. public bool Connect()
  205. {
  206. if (!eds.Init())
  207. {
  208. return false;
  209. }
  210. if (eds.IsConnected())
  211. {
  212. return true;
  213. }
  214. return eds.ConncetSem();
  215. }
  216. public EDSTYPE GetEDSType()
  217. {
  218. EDSTYPE t;
  219. switch (eds.EDSGetType())
  220. {
  221. case 1:
  222. t = EDSTYPE.OFFLINE;
  223. break;
  224. case 3:
  225. t = EDSTYPE.BRUKER;
  226. break;
  227. case 4:
  228. t = EDSTYPE.OXFORD;
  229. break;
  230. default:
  231. t = EDSTYPE.OFFLINE;
  232. break;
  233. }
  234. return t;
  235. }
  236. public void SetFilterKeyEleNames(List<string> KeyNameList)
  237. {
  238. this.keyElenamelist = KeyNameList;
  239. }
  240. }
  241. }