EDSController.cs 17 KB

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