EDSController.cs 10 KB

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