EDSController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.ServiceInterface
  11. {
  12. using OTSCLRINTERFACE;
  13. using System.Drawing;
  14. using System.Windows.Forms;
  15. public class EDSController : IEDSController
  16. {
  17. public COTSControlFunExport eds;
  18. static EDSController edsctrl = null;
  19. public static APIClass ApiClass = null;
  20. private static bool isFEI = false;
  21. private int AnalyExpCount = 100000;
  22. private string strResolution = "";
  23. private int width = 0;
  24. private int height = 0;
  25. public List<string> nameList = new List<string>();
  26. public static EDSController GetEDSController()
  27. {
  28. if (FileHelper.GetXMLInformations("EDSName") == "FEI")
  29. {
  30. isFEI = true;
  31. }
  32. if (edsctrl == null)
  33. {
  34. edsctrl = new EDSController();
  35. }
  36. return edsctrl;
  37. }
  38. private EDSController()
  39. {
  40. if (isFEI)
  41. {
  42. ApiClass = new APIClass();
  43. Connect();
  44. }
  45. else
  46. {
  47. eds = OTSCLRINTERFACE.COTSControlFunExport.GetControllerInstance();
  48. }
  49. }
  50. private void ProcessXrayInfo(COTSParticleClr partWithXrayInfo)//sometime the result will contain repeat percentage data for one element.It must be processed.
  51. {
  52. var eleChemistry = partWithXrayInfo.GetXray().GetElementQuantifyData();
  53. Dictionary<string, List<double>> eleInfoDic = new Dictionary<string, List<double>>();
  54. bool hasRepeatEle = false;
  55. foreach (var ele in eleChemistry)
  56. {
  57. if (eleInfoDic.ContainsKey(ele.GetName()))//contain repeat data;
  58. {
  59. eleInfoDic[ele.GetName()].Add(ele.GetPercentage());
  60. hasRepeatEle = true;
  61. }
  62. else
  63. {
  64. eleInfoDic.Add(ele.GetName(), new List<double>() { ele.GetPercentage() });
  65. }
  66. }
  67. if (hasRepeatEle)
  68. {
  69. Dictionary<string, double> resultInfo = new Dictionary<string, double>();
  70. foreach (var eleInfo in eleInfoDic)
  71. {
  72. double newPercentData=0;
  73. foreach (var p in eleInfo.Value)
  74. {
  75. newPercentData += p;
  76. }
  77. newPercentData = newPercentData / eleInfo.Value.Count;
  78. resultInfo.Add(eleInfo.Key, newPercentData);
  79. }
  80. foreach (var e in eleChemistry)
  81. {
  82. e.SetPercentage(resultInfo[e.GetName()]);
  83. }
  84. partWithXrayInfo.GetXray().SetElementQuantifyData(eleChemistry);
  85. }
  86. }
  87. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  88. {
  89. bool result = false;
  90. if (isFEI)
  91. {
  92. for (int i = 0; i < a_listParticles.Count; i++)
  93. {
  94. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  95. uint[] spectrumItems = new uint[2000];
  96. if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫
  97. {
  98. List<Point> points = CImageHandler.FindContoursBySegment(width, height, a_listParticles[i].GetFeature().GetSegmentsList());
  99. if (!ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  100. {
  101. return false;
  102. }
  103. }
  104. else
  105. {
  106. Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect();
  107. if (!ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  108. {
  109. return false;
  110. }
  111. }
  112. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  113. if (a_bElementInfo)
  114. {
  115. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  116. for (int j = 0; j < eleItems.Count; j++)
  117. {
  118. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  119. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  120. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  121. elementChemistryClrs.Add(chemistryClr);
  122. }
  123. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  124. }
  125. }
  126. result = true;
  127. //return true;
  128. }
  129. else
  130. {
  131. if (!eds.IsConnected())
  132. {
  133. return false;
  134. }
  135. if (eds.GetEDSName() == "EDS Bruker")
  136. {
  137. for (int i = 0; i < a_listParticles.Count; i++)
  138. {
  139. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  140. for (int j = 0; j < nameList.Count; j++)
  141. {
  142. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  143. chemistryClr.SetName(nameList[j]);
  144. elementChemistryClrs.Add(chemistryClr);
  145. }
  146. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  147. }
  148. }
  149. COTSParticleClr[] parts = a_listParticles.ToArray();
  150. result= eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  151. }
  152. if (result == true)
  153. {
  154. foreach (var p in a_listParticles)
  155. {
  156. ProcessXrayInfo(p);
  157. }
  158. }
  159. return result;
  160. }
  161. public bool GetXRayByPoints(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  162. {
  163. bool result = false;
  164. if (isFEI)
  165. {
  166. for (int i = 0; i < a_listParticles.Count; i++)
  167. {
  168. Point point = (Point)a_listParticles[i].GetXRayPos();
  169. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  170. uint[] spectrumItems = new uint[2000];
  171. if (!ApiClass.GetXRayByPoint(point.X, point.Y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  172. {
  173. return false;
  174. }
  175. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  176. if (a_bElementInfo)
  177. {
  178. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  179. for (int j = 0; j < eleItems.Count; j++)
  180. {
  181. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  182. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  183. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  184. elementChemistryClrs.Add(chemistryClr);
  185. }
  186. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  187. }
  188. }
  189. result= true;
  190. }
  191. else
  192. {
  193. if (!eds.IsConnected())
  194. {
  195. return false;
  196. }
  197. int xrayNum = a_listParticles.Count;
  198. Point[] Ps = new Point[xrayNum];
  199. for (int i = 0; i < xrayNum; i++)
  200. {
  201. Point p = (Point)a_listParticles[i].GetXRayPos();
  202. Ps[i].X = p.X;
  203. Ps[i].Y = p.Y;
  204. }
  205. if (eds.GetEDSName() == "EDS Bruker")
  206. {
  207. for (int i = 0; i < a_listParticles.Count; i++)
  208. {
  209. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  210. for (int j = 0; j < nameList.Count; j++)
  211. {
  212. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  213. chemistryClr.SetName(nameList[j]);
  214. elementChemistryClrs.Add(chemistryClr);
  215. }
  216. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  217. }
  218. }
  219. COTSParticleClr[] parts = a_listParticles.ToArray();
  220. result= eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  221. }
  222. if (result == true)
  223. {
  224. foreach (var p in a_listParticles)
  225. {
  226. ProcessXrayInfo(p);
  227. }
  228. }
  229. return result;
  230. }
  231. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  232. {
  233. if (isFEI)
  234. {
  235. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  236. return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);
  237. }
  238. else
  239. {
  240. if (!eds.IsConnected())
  241. {
  242. return false;
  243. }
  244. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  245. }
  246. }
  247. public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  248. {
  249. if (isFEI)
  250. {
  251. double x = point.X;
  252. double y = point.Y;
  253. double z = 0;
  254. double r = 0;
  255. double t = 0;
  256. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  257. if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))
  258. {
  259. return false;
  260. }
  261. return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  262. }
  263. else
  264. {
  265. if (!eds.IsConnected())
  266. {
  267. return false;
  268. }
  269. return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);
  270. }
  271. }
  272. public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  273. {
  274. if (isFEI)
  275. {
  276. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  277. if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫
  278. {
  279. List<Point> points = CImageHandler.FindContoursBySegment(width, height, fea.GetSegmentsList());
  280. return ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  281. }
  282. else
  283. {
  284. Rectangle rectangle = new Rectangle();
  285. rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());
  286. rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());
  287. rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());
  288. rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);
  289. return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  290. }
  291. }
  292. else
  293. {
  294. if (!eds.IsConnected())
  295. {
  296. return false;
  297. }
  298. return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);
  299. }
  300. }
  301. public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  302. {
  303. if (isFEI)
  304. {
  305. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  306. bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);
  307. a_nElementNum = eleItems.Count;
  308. for (int i = 0; i < eleItems.Count; i++)
  309. {
  310. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  311. }
  312. return isTrue;
  313. }
  314. else
  315. {
  316. if (!eds.IsConnected())
  317. {
  318. return false;
  319. }
  320. return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  321. }
  322. }
  323. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  324. {
  325. if (isFEI)
  326. {
  327. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  328. bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);
  329. a_nElementNum = eleItems.Count;
  330. for (int i = 0; i < eleItems.Count; i++)
  331. {
  332. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  333. }
  334. return isTrue;
  335. }
  336. else
  337. {
  338. if (!eds.IsConnected())
  339. {
  340. return false;
  341. }
  342. return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  343. }
  344. }
  345. public bool Init()
  346. {
  347. if (isFEI)
  348. {
  349. return ApiClass.isConnect();
  350. }
  351. else
  352. {
  353. bool m_init = eds.EDSInit();
  354. return m_init;
  355. }
  356. }
  357. public bool ScanInit()
  358. {
  359. if (isFEI)
  360. {
  361. return ApiClass.isConnect();
  362. }
  363. else
  364. {
  365. bool m_init = eds.ScanInit();
  366. return m_init;
  367. }
  368. }
  369. public bool Connect()
  370. {
  371. if (isFEI)
  372. {
  373. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  374. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  375. if (FEIIP == "" || FEIPORT == "")
  376. {
  377. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  378. return false;
  379. }
  380. if (ApiClass.isConnect())
  381. {
  382. return true;
  383. }
  384. return ApiClass.Connect(FEIIP, FEIPORT);
  385. }
  386. else
  387. {
  388. if (!eds.Init())
  389. {
  390. return false;
  391. }
  392. if (eds.IsConnected())
  393. {
  394. return true;
  395. }
  396. return eds.ConncetSem();
  397. }
  398. }
  399. public bool DisConnect()
  400. {
  401. if (isFEI)
  402. {
  403. return ApiClass.DisConnect();
  404. }
  405. else
  406. {
  407. if (eds.IsConnected())
  408. {
  409. return eds.DisconnectSem();
  410. }
  411. return true;
  412. }
  413. }
  414. public bool SetResolution(int ResolutionWidth, int ResolutionHeight)
  415. {
  416. if (isFEI)
  417. {
  418. width = ResolutionWidth;
  419. height = ResolutionHeight;
  420. strResolution = ResolutionWidth + "x" + ResolutionHeight;
  421. }
  422. return true;
  423. }
  424. public bool SetAnalyExpCount(int MaxCounts)
  425. {
  426. if (isFEI)
  427. {
  428. AnalyExpCount = MaxCounts;
  429. }
  430. return true;
  431. }
  432. }
  433. }