EDSController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 
  2. using OTSDataType;
  3. using FEIApiControl;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using OTSCLRINTERFACE;
  10. namespace OTSModelSharp.ServiceCenter
  11. {
  12. using OTSCLRINTERFACE;
  13. //using OTSMeasureApp._0_OTSModel.Measure._5_OTSMining;
  14. using System.Drawing;
  15. using System.Windows.Forms;
  16. public class EDSController : IEDSController
  17. {
  18. private COTSControlFunExport eds;
  19. static EDSController edsctrl = null;
  20. public static APIClass ApiClass = null;
  21. private static bool isFEI = false;
  22. private int AnalyExpCount = 100000;
  23. private string strResolution = "";
  24. public static EDSController GetEDSController()
  25. {
  26. if (FileHelper.GetXMLInformations("EDSName") == "FEI")
  27. {
  28. isFEI = true;
  29. }
  30. if (edsctrl == null)
  31. {
  32. edsctrl = new EDSController();
  33. }
  34. return edsctrl;
  35. }
  36. private EDSController()
  37. {
  38. if (isFEI)
  39. {
  40. ApiClass = new APIClass();
  41. Connect();
  42. }
  43. else
  44. {
  45. eds = OTSCLRINTERFACE.COTSControlFunExport.GetControllerInstance();
  46. }
  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 (isFEI)
  89. {
  90. for (int i = 0; i < a_listParticles.Count; i++)
  91. {
  92. Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect();
  93. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  94. uint[] spectrumItems = new uint[2000];
  95. if (!ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  96. {
  97. return false;
  98. }
  99. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  100. if (a_bElementInfo)
  101. {
  102. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  103. for (int j = 0; j < eleItems.Count; j++)
  104. {
  105. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  106. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  107. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  108. elementChemistryClrs.Add(chemistryClr);
  109. }
  110. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  111. }
  112. }
  113. result = true;
  114. }
  115. else
  116. {
  117. if (!eds.IsConnected())
  118. {
  119. return false;
  120. }
  121. COTSParticleClr[] parts = a_listParticles.ToArray();
  122. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  123. }
  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 GetXRayByPoints(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  134. {
  135. bool result = false;
  136. if (isFEI)
  137. {
  138. for (int i = 0; i < a_listParticles.Count; i++)
  139. {
  140. Point point = (Point)a_listParticles[i].GetXRayPos();
  141. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  142. uint[] spectrumItems = new uint[2000];
  143. if (!ApiClass.GetXRayByPoint(point.X, point.Y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  144. {
  145. return false;
  146. }
  147. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  148. if (a_bElementInfo)
  149. {
  150. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  151. for (int j = 0; j < eleItems.Count; j++)
  152. {
  153. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  154. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  155. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  156. elementChemistryClrs.Add(chemistryClr);
  157. }
  158. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  159. }
  160. }
  161. result= true;
  162. }
  163. else
  164. {
  165. if (!eds.IsConnected())
  166. {
  167. return false;
  168. }
  169. int xrayNum = a_listParticles.Count;
  170. Point[] Ps = new Point[xrayNum];
  171. for (int i = 0; i < xrayNum; i++)
  172. {
  173. Point p = (Point)a_listParticles[i].GetXRayPos();
  174. Ps[i].X = p.X;
  175. Ps[i].Y = p.Y;
  176. }
  177. COTSParticleClr[] parts = a_listParticles.ToArray();
  178. result= eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  179. }
  180. if (result == true)
  181. {
  182. foreach (var p in a_listParticles)
  183. {
  184. ProcessXrayInfo(p);
  185. }
  186. }
  187. return result;
  188. }
  189. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  190. {
  191. if (isFEI)
  192. {
  193. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  194. return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);
  195. }
  196. else
  197. {
  198. if (!eds.IsConnected())
  199. {
  200. return false;
  201. }
  202. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  203. }
  204. }
  205. public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  206. {
  207. if (isFEI)
  208. {
  209. double x = point.X;
  210. double y = point.Y;
  211. double z = 0;
  212. double r = 0;
  213. double t = 0;
  214. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  215. if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))
  216. {
  217. return false;
  218. }
  219. return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  220. }
  221. else
  222. {
  223. if (!eds.IsConnected())
  224. {
  225. return false;
  226. }
  227. return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);
  228. }
  229. }
  230. public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  231. {
  232. if (isFEI)
  233. {
  234. Rectangle rectangle = new Rectangle();
  235. rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());
  236. rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());
  237. rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());
  238. rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);
  239. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  240. return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  241. }
  242. else
  243. {
  244. if (!eds.IsConnected())
  245. {
  246. return false;
  247. }
  248. return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);
  249. }
  250. }
  251. public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  252. {
  253. if (isFEI)
  254. {
  255. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  256. bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);
  257. a_nElementNum = eleItems.Count;
  258. for (int i = 0; i < eleItems.Count; i++)
  259. {
  260. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  261. }
  262. return isTrue;
  263. }
  264. else
  265. {
  266. if (!eds.IsConnected())
  267. {
  268. return false;
  269. }
  270. return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  271. }
  272. }
  273. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  274. {
  275. if (isFEI)
  276. {
  277. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  278. bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);
  279. a_nElementNum = eleItems.Count;
  280. for (int i = 0; i < eleItems.Count; i++)
  281. {
  282. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  283. }
  284. return isTrue;
  285. }
  286. else
  287. {
  288. if (!eds.IsConnected())
  289. {
  290. return false;
  291. }
  292. return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  293. }
  294. }
  295. public bool Init()
  296. {
  297. if (isFEI)
  298. {
  299. return ApiClass.isConnect();
  300. }
  301. else
  302. {
  303. bool m_init = eds.EDSInit();
  304. return m_init;
  305. }
  306. }
  307. public bool ScanInit()
  308. {
  309. if (isFEI)
  310. {
  311. return ApiClass.isConnect();
  312. }
  313. else
  314. {
  315. bool m_init = eds.ScanInit();
  316. return m_init;
  317. }
  318. }
  319. public bool Connect()
  320. {
  321. if (isFEI)
  322. {
  323. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  324. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  325. if (FEIIP == "" || FEIPORT == "")
  326. {
  327. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  328. return false;
  329. }
  330. if (ApiClass.isConnect())
  331. {
  332. return true;
  333. }
  334. return ApiClass.Connect(FEIIP, FEIPORT);
  335. }
  336. else
  337. {
  338. if (!eds.Init())
  339. {
  340. return false;
  341. }
  342. if (eds.IsConnected())
  343. {
  344. return true;
  345. }
  346. return eds.ConncetSem();
  347. }
  348. }
  349. public bool DisConnect()
  350. {
  351. if (isFEI)
  352. {
  353. return ApiClass.DisConnect();
  354. }
  355. else
  356. {
  357. if (eds.IsConnected())
  358. {
  359. return eds.DisconnectSem();
  360. }
  361. return true;
  362. }
  363. }
  364. public bool SetResolution(int ResolutionWidth, int ResolutionHeight)
  365. {
  366. if (isFEI)
  367. {
  368. strResolution = ResolutionWidth + "x" + ResolutionHeight;
  369. }
  370. return true;
  371. }
  372. public bool SetAnalyExpCount(int MaxCounts)
  373. {
  374. if (isFEI)
  375. {
  376. AnalyExpCount = MaxCounts;
  377. }
  378. return true;
  379. }
  380. }
  381. }