EDSController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 GetXRayBySubParticles(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime)
  190. {
  191. if (isFEI)
  192. {
  193. for (int i = 0; i < a_listParticles.Count; i++)
  194. {
  195. foreach (var matrixP in a_listParticles[i].GetSubParticles())
  196. {
  197. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  198. uint[] spectrumItems = new uint[2000];
  199. Point xrayPos = (Point)matrixP.GetXRayPos();
  200. if (!ApiClass.GetXRayByPoint(xrayPos.X, xrayPos.Y, strResolution, a_nXRayAQTime, AnalyExpCount, false, ref eleItems, ref spectrumItems))
  201. {
  202. continue;
  203. }
  204. CPosXrayClr posxray = new CPosXrayClr();
  205. posxray.SetXrayData(spectrumItems);
  206. posxray.SetPosition(xrayPos);
  207. posxray.SetScanFieldId(matrixP.GetFieldId());
  208. matrixP.SetXray(posxray);
  209. }
  210. }
  211. return true;
  212. }
  213. else
  214. {
  215. if (!eds.IsConnected())
  216. {
  217. return false;
  218. }
  219. List<CPosXrayClr> posxrays = new List<CPosXrayClr>();
  220. foreach (var rawpart in a_listParticles)
  221. {
  222. var parts = rawpart.GetSubParticles();
  223. foreach (var part in parts)
  224. {
  225. var xray = new CPosXrayClr();
  226. xray.SetPosition((Point)part.GetXRayPos());
  227. xray.SetScanFieldId(part.GetFieldId());
  228. posxrays.Add(xray);
  229. part.SetXray(xray);
  230. }
  231. }
  232. var xrays = posxrays.ToArray();
  233. return eds.GetXRayByPoints(a_nXRayAQTime,ref xrays, false);
  234. }
  235. }
  236. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  237. {
  238. if (isFEI)
  239. {
  240. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  241. return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);
  242. }
  243. else
  244. {
  245. if (!eds.IsConnected())
  246. {
  247. return false;
  248. }
  249. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  250. }
  251. }
  252. public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  253. {
  254. if (isFEI)
  255. {
  256. double x = point.X;
  257. double y = point.Y;
  258. double z = 0;
  259. double r = 0;
  260. double t = 0;
  261. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  262. if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))
  263. {
  264. return false;
  265. }
  266. return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  267. }
  268. else
  269. {
  270. if (!eds.IsConnected())
  271. {
  272. return false;
  273. }
  274. return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);
  275. }
  276. }
  277. public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  278. {
  279. if (isFEI)
  280. {
  281. Rectangle rectangle = new Rectangle();
  282. rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());
  283. rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());
  284. rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());
  285. rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);
  286. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  287. return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  288. }
  289. else
  290. {
  291. if (!eds.IsConnected())
  292. {
  293. return false;
  294. }
  295. return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);
  296. }
  297. }
  298. public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  299. {
  300. if (isFEI)
  301. {
  302. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  303. bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);
  304. a_nElementNum = eleItems.Count;
  305. for (int i = 0; i < eleItems.Count; i++)
  306. {
  307. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  308. }
  309. return isTrue;
  310. }
  311. else
  312. {
  313. if (!eds.IsConnected())
  314. {
  315. return false;
  316. }
  317. return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  318. }
  319. }
  320. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  321. {
  322. if (isFEI)
  323. {
  324. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  325. bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);
  326. a_nElementNum = eleItems.Count;
  327. for (int i = 0; i < eleItems.Count; i++)
  328. {
  329. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  330. }
  331. return isTrue;
  332. }
  333. else
  334. {
  335. if (!eds.IsConnected())
  336. {
  337. return false;
  338. }
  339. return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  340. }
  341. }
  342. public bool Init()
  343. {
  344. if (isFEI)
  345. {
  346. return ApiClass.isConnect();
  347. }
  348. else
  349. {
  350. bool m_init = eds.EDSInit();
  351. return m_init;
  352. }
  353. }
  354. public bool ScanInit()
  355. {
  356. if (isFEI)
  357. {
  358. return ApiClass.isConnect();
  359. }
  360. else
  361. {
  362. bool m_init = eds.ScanInit();
  363. return m_init;
  364. }
  365. }
  366. public bool Connect()
  367. {
  368. if (isFEI)
  369. {
  370. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  371. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  372. if (FEIIP == "" || FEIPORT == "")
  373. {
  374. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  375. return false;
  376. }
  377. if (ApiClass.isConnect())
  378. {
  379. return true;
  380. }
  381. return ApiClass.Connect(FEIIP, FEIPORT);
  382. }
  383. else
  384. {
  385. if (!eds.Init())
  386. {
  387. return false;
  388. }
  389. if (eds.IsConnected())
  390. {
  391. return true;
  392. }
  393. return eds.ConncetSem();
  394. }
  395. }
  396. public bool DisConnect()
  397. {
  398. if (isFEI)
  399. {
  400. return ApiClass.DisConnect();
  401. }
  402. else
  403. {
  404. if (eds.IsConnected())
  405. {
  406. return eds.DisconnectSem();
  407. }
  408. return true;
  409. }
  410. }
  411. public bool SetResolution(int ResolutionWidth, int ResolutionHeight)
  412. {
  413. if (isFEI)
  414. {
  415. strResolution = ResolutionWidth + "x" + ResolutionHeight;
  416. }
  417. return true;
  418. }
  419. public bool SetAnalyExpCount(int MaxCounts)
  420. {
  421. if (isFEI)
  422. {
  423. AnalyExpCount = MaxCounts;
  424. }
  425. return true;
  426. }
  427. }
  428. }