EDSController.cs 11 KB

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