EDSController.cs 12 KB

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