EDSController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  51. {
  52. if (isFEI)
  53. {
  54. for (int i = 0; i < a_listParticles.Count; i++)
  55. {
  56. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  57. uint[] spectrumItems = new uint[2000];
  58. if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫
  59. {
  60. List<Point> points = CImageHandler.FindContoursBySegment(width, height, a_listParticles[i].GetFeature().GetSegmentsList());
  61. if (!ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  62. {
  63. return false;
  64. }
  65. }
  66. else
  67. {
  68. Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect();
  69. if (!ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  70. {
  71. return false;
  72. }
  73. }
  74. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  75. if (a_bElementInfo)
  76. {
  77. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  78. for (int j = 0; j < eleItems.Count; j++)
  79. {
  80. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  81. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  82. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  83. elementChemistryClrs.Add(chemistryClr);
  84. }
  85. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  86. }
  87. }
  88. return true;
  89. }
  90. else
  91. {
  92. if (!eds.IsConnected())
  93. {
  94. return false;
  95. }
  96. if (eds.GetEDSName() == "EDS Bruker")
  97. {
  98. for (int i = 0; i < a_listParticles.Count; i++)
  99. {
  100. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  101. for (int j = 0; j < nameList.Count; j++)
  102. {
  103. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  104. chemistryClr.SetName(nameList[j]);
  105. elementChemistryClrs.Add(chemistryClr);
  106. }
  107. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  108. }
  109. }
  110. COTSParticleClr[] parts = a_listParticles.ToArray();
  111. return eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  112. }
  113. }
  114. public bool GetXRayByPoints(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  115. {
  116. if (isFEI)
  117. {
  118. for (int i = 0; i < a_listParticles.Count; i++)
  119. {
  120. Point point = (Point)a_listParticles[i].GetXRayPos();
  121. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  122. uint[] spectrumItems = new uint[2000];
  123. if (!ApiClass.GetXRayByPoint(point.X, point.Y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  124. {
  125. return false;
  126. }
  127. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  128. if (a_bElementInfo)
  129. {
  130. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  131. for (int j = 0; j < eleItems.Count; j++)
  132. {
  133. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  134. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  135. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  136. elementChemistryClrs.Add(chemistryClr);
  137. }
  138. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  139. }
  140. }
  141. return true;
  142. }
  143. else
  144. {
  145. if (!eds.IsConnected())
  146. {
  147. return false;
  148. }
  149. int xrayNum = a_listParticles.Count;
  150. Point[] Ps = new Point[xrayNum];
  151. for (int i = 0; i < xrayNum; i++)
  152. {
  153. Point p = (Point)a_listParticles[i].GetXRayPos();
  154. Ps[i].X = p.X;
  155. Ps[i].Y = p.Y;
  156. }
  157. if (eds.GetEDSName() == "EDS Bruker")
  158. {
  159. for (int i = 0; i < a_listParticles.Count; i++)
  160. {
  161. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  162. for (int j = 0; j < nameList.Count; j++)
  163. {
  164. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  165. chemistryClr.SetName(nameList[j]);
  166. elementChemistryClrs.Add(chemistryClr);
  167. }
  168. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  169. }
  170. }
  171. COTSParticleClr[] parts = a_listParticles.ToArray();
  172. return eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  173. }
  174. }
  175. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  176. {
  177. if (isFEI)
  178. {
  179. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  180. return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);
  181. }
  182. else
  183. {
  184. if (!eds.IsConnected())
  185. {
  186. return false;
  187. }
  188. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  189. }
  190. }
  191. public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  192. {
  193. if (isFEI)
  194. {
  195. double x = point.X;
  196. double y = point.Y;
  197. double z = 0;
  198. double r = 0;
  199. double t = 0;
  200. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  201. if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))
  202. {
  203. return false;
  204. }
  205. return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  206. }
  207. else
  208. {
  209. if (!eds.IsConnected())
  210. {
  211. return false;
  212. }
  213. return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);
  214. }
  215. }
  216. public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  217. {
  218. if (isFEI)
  219. {
  220. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  221. if (ApiClass.GetSupportPolygon())//判断DLL是否支持多边形面扫
  222. {
  223. List<Point> points = CImageHandler.FindContoursBySegment(width, height, fea.GetSegmentsList());
  224. return ApiClass.GetXRayByPolygon(points, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  225. }
  226. else
  227. {
  228. Rectangle rectangle = new Rectangle();
  229. rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());
  230. rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());
  231. rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());
  232. rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);
  233. return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  234. }
  235. }
  236. else
  237. {
  238. if (!eds.IsConnected())
  239. {
  240. return false;
  241. }
  242. return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);
  243. }
  244. }
  245. public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  246. {
  247. if (isFEI)
  248. {
  249. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  250. bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);
  251. a_nElementNum = eleItems.Count;
  252. for (int i = 0; i < eleItems.Count; i++)
  253. {
  254. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  255. }
  256. return isTrue;
  257. }
  258. else
  259. {
  260. if (!eds.IsConnected())
  261. {
  262. return false;
  263. }
  264. return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  265. }
  266. }
  267. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  268. {
  269. if (isFEI)
  270. {
  271. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  272. bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);
  273. a_nElementNum = eleItems.Count;
  274. for (int i = 0; i < eleItems.Count; i++)
  275. {
  276. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  277. }
  278. return isTrue;
  279. }
  280. else
  281. {
  282. if (!eds.IsConnected())
  283. {
  284. return false;
  285. }
  286. return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  287. }
  288. }
  289. public bool Init()
  290. {
  291. if (isFEI)
  292. {
  293. return ApiClass.isConnect();
  294. }
  295. else
  296. {
  297. bool m_init = eds.EDSInit();
  298. return m_init;
  299. }
  300. }
  301. public bool ScanInit()
  302. {
  303. if (isFEI)
  304. {
  305. return ApiClass.isConnect();
  306. }
  307. else
  308. {
  309. bool m_init = eds.ScanInit();
  310. return m_init;
  311. }
  312. }
  313. public bool Connect()
  314. {
  315. if (isFEI)
  316. {
  317. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  318. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  319. if (FEIIP == "" || FEIPORT == "")
  320. {
  321. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  322. return false;
  323. }
  324. if (ApiClass.isConnect())
  325. {
  326. return true;
  327. }
  328. return ApiClass.Connect(FEIIP, FEIPORT);
  329. }
  330. else
  331. {
  332. if (!eds.Init())
  333. {
  334. return false;
  335. }
  336. if (eds.IsConnected())
  337. {
  338. return true;
  339. }
  340. return eds.ConncetSem();
  341. }
  342. }
  343. public bool DisConnect()
  344. {
  345. if (isFEI)
  346. {
  347. return ApiClass.DisConnect();
  348. }
  349. else
  350. {
  351. if (eds.IsConnected())
  352. {
  353. return eds.DisconnectSem();
  354. }
  355. return true;
  356. }
  357. }
  358. public bool SetResolution(int ResolutionWidth, int ResolutionHeight)
  359. {
  360. if (isFEI)
  361. {
  362. width = ResolutionWidth;
  363. height = ResolutionHeight;
  364. strResolution = ResolutionWidth + "x" + ResolutionHeight;
  365. }
  366. return true;
  367. }
  368. public bool SetAnalyExpCount(int MaxCounts)
  369. {
  370. if (isFEI)
  371. {
  372. AnalyExpCount = MaxCounts;
  373. }
  374. return true;
  375. }
  376. }
  377. }