EDSController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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(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. }
  46. else if (deviceType == "Coxm")
  47. {
  48. edsctrl = new CoxmEDSController( imgwidth, imgheight, expectCount, ifautoid, knownelements);
  49. }
  50. edsctrl.SetResolution(imgwidth, imgheight);
  51. }
  52. return edsctrl;
  53. }
  54. protected EDSController(string deviceType,int expectcount,bool ifautoid,string knownelements)
  55. {
  56. eds = COTSControlFunExport.GetControllerInstance(deviceType);
  57. eds.SetQuantificationParam(ifautoid, knownelements);
  58. eds.SetExpectCount(expectcount);
  59. }
  60. private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.
  61. {
  62. var eleChemistry = partWithXrayInfo.GetXray().GetElementQuantifyData();
  63. Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();
  64. bool hasRepeatEle = false;
  65. foreach (var ele in eleChemistry)
  66. {
  67. if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;
  68. {
  69. eleInfoDic[ele.GetName()].Add(ele.GetPercentage());
  70. hasRepeatEle = true;
  71. }
  72. else
  73. {
  74. eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });
  75. }
  76. }
  77. if (hasRepeatEle)
  78. {
  79. Dictionary<string, double> resultInfo = new Dictionary<string, double>();
  80. foreach (var eleInfo in eleInfoDic)
  81. {
  82. double newPercentData=0;
  83. foreach (var p in eleInfo.Value)
  84. {
  85. newPercentData += p;
  86. }
  87. newPercentData = newPercentData / eleInfo.Value.Count;
  88. resultInfo.Add(eleInfo.Key, newPercentData);
  89. }
  90. foreach (var e in eleChemistry)
  91. {
  92. e.SetPercentage(resultInfo[e.GetName()]);
  93. }
  94. partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);
  95. }
  96. }
  97. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  98. {
  99. bool result = false;
  100. if (!eds.IsConnected())
  101. {
  102. return false;
  103. }
  104. if (keyElenamelist.Count > 0)
  105. {
  106. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  107. for (int j = 0; j < keyElenamelist.Count; j++)
  108. {
  109. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  110. chemistryClr.SetName(keyElenamelist[j]);
  111. elementChemistryClrs.Add(chemistryClr);
  112. }
  113. for (int i = 0; i < a_listParticles.Count; i++)
  114. {
  115. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  116. }
  117. }
  118. COTSParticleClr[] parts = a_listParticles.ToArray();
  119. if (delayQuant)
  120. {
  121. a_bElementInfo = false;
  122. }
  123. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  124. if (result == true)
  125. {
  126. foreach (var p in a_listParticles)
  127. {
  128. ProcessXrayInfo(p);
  129. }
  130. }
  131. return result;
  132. }
  133. public bool GetXRayByParts(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  134. {
  135. bool result = false;
  136. if (!eds.IsConnected())
  137. {
  138. return false;
  139. }
  140. int xrayNum = a_listParticles.Count;
  141. Point[] Ps = new Point[xrayNum];
  142. for (int i = 0; i < xrayNum; i++)
  143. {
  144. Point p = (Point)a_listParticles[i].GetXRayPos();
  145. Ps[i].X = p.X;
  146. Ps[i].Y = p.Y;
  147. }
  148. if (keyElenamelist.Count > 0)
  149. {
  150. if (this.GetEDSType() == EDSTYPE.BRUKER)
  151. {
  152. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  153. for (int j = 0; j < keyElenamelist.Count; j++)
  154. {
  155. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  156. chemistryClr.SetName(keyElenamelist[j]);
  157. elementChemistryClrs.Add(chemistryClr);
  158. }
  159. for (int i = 0; i < a_listParticles.Count; i++)
  160. {
  161. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  162. }
  163. }
  164. }
  165. int nSize = a_listParticles.Count;
  166. if (nSize > 1024)
  167. {
  168. COTSParticleClr[] partsTemp = new COTSParticleClr[1024];
  169. Point[] PsTemp = new Point[1024];
  170. int nTimes = nSize / 1024;
  171. for (int i = 0; i < nTimes; i++)
  172. {
  173. NLog.LogManager.GetCurrentClassLogger().Warn("begin 1024 batch");
  174. for (int m = 0; m < 1024; m++)
  175. {
  176. partsTemp[m]=a_listParticles[i * 1024 + m];
  177. PsTemp[m] = Ps[i * 1024 + m];
  178. }
  179. if (!eds.GetXRayByPoints(a_nXRayAQTime, PsTemp, partsTemp, a_bElementInfo))
  180. {
  181. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  182. return false;
  183. }
  184. }
  185. int nLast = nSize % 1024;
  186. if (nLast != 0)
  187. {
  188. COTSParticleClr[] lastParts = new COTSParticleClr[nLast];
  189. Point[] lastPs = new Point[nLast];
  190. for (int m = 0; m < nLast; m++)
  191. {
  192. lastParts[m] = a_listParticles[nTimes * 1024 + m];
  193. lastPs[m] = Ps[nTimes * 1024 + m];
  194. }
  195. if (!eds.GetXRayByPoints(a_nXRayAQTime, lastPs, lastParts, a_bElementInfo))
  196. {
  197. NLog.LogManager.GetCurrentClassLogger().Error("GetXRayByPoints: failed to get element.");
  198. return false;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. COTSParticleClr[] parts = a_listParticles.ToArray();
  205. if (delayQuant)
  206. {
  207. a_bElementInfo = false;
  208. }
  209. result = eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  210. }
  211. if (result == true)
  212. {
  213. foreach (var p in a_listParticles)
  214. {
  215. ProcessXrayInfo(p);
  216. }
  217. }
  218. return result;
  219. }
  220. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  221. {
  222. if (!eds.IsConnected())
  223. {
  224. return false;
  225. }
  226. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  227. }
  228. public bool Connect()
  229. {
  230. if (!eds.IsConnected())
  231. {
  232. eds.ConncetSem();
  233. }
  234. bool m_init = eds.EDSInit();
  235. return m_init;
  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. public bool GetIfDelayQuantify()
  275. {
  276. return delayQuant;
  277. }
  278. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  279. {
  280. eds.SetQuantificationParam(IfAutoId, knownElements);
  281. }
  282. public bool GetXRayByExpandFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  283. {
  284. throw new NotImplementedException();
  285. }
  286. }
  287. }