EDSController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. private 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. public static EDSController GetEDSController()
  24. {
  25. if (FileHelper.GetXMLInformations("EDSName") == "FEI")
  26. {
  27. isFEI = true;
  28. }
  29. if (edsctrl == null)
  30. {
  31. edsctrl = new EDSController();
  32. }
  33. return edsctrl;
  34. }
  35. private EDSController()
  36. {
  37. if (isFEI)
  38. {
  39. ApiClass = new APIClass();
  40. Connect();
  41. }
  42. else
  43. {
  44. eds = OTSCLRINTERFACE.COTSControlFunExport.GetControllerInstance();
  45. }
  46. }
  47. public bool GetXRayByFeatures(List<COTSParticleClr> a_listParticles, double a_nXRayAQTime, bool a_bElementInfo)
  48. {
  49. if (isFEI)
  50. {
  51. for (int i = 0; i < a_listParticles.Count; i++)
  52. {
  53. Rectangle rectangle = (Rectangle)a_listParticles[i].GetParticleRect();
  54. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  55. uint[] spectrumItems = new uint[2000];
  56. if (!ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref spectrumItems))
  57. {
  58. return false;
  59. }
  60. a_listParticles[i].GetXray().SetXrayData(spectrumItems);
  61. if (a_bElementInfo)
  62. {
  63. List<CElementChemistryClr> elementChemistryClrs = new List<CElementChemistryClr>();
  64. for (int j = 0; j < eleItems.Count; j++)
  65. {
  66. CElementChemistryClr chemistryClr = new CElementChemistryClr();
  67. chemistryClr.SetName(eleItems.ElementAt(j).Key);
  68. chemistryClr.SetPercentage(eleItems.ElementAt(j).Value);
  69. elementChemistryClrs.Add(chemistryClr);
  70. }
  71. a_listParticles[i].GetXray().SetElementQuantifyData(elementChemistryClrs);
  72. }
  73. }
  74. return true;
  75. }
  76. else
  77. {
  78. if (!eds.IsConnected())
  79. {
  80. return false;
  81. }
  82. COTSParticleClr[] parts = a_listParticles.ToArray();
  83. return eds.GetXRayByFeatures((uint)a_nXRayAQTime, parts, a_bElementInfo);
  84. }
  85. }
  86. public bool GetXRayByPoints(List<COTSParticleClr> a_listParticles, uint a_nXRayAQTime, bool a_bElementInfo)
  87. {
  88. if (isFEI)
  89. {
  90. for (int i = 0; i < a_listParticles.Count; i++)
  91. {
  92. Point point = (Point)a_listParticles[i].GetXRayPos();
  93. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  94. uint[] spectrumItems = new uint[2000];
  95. if (!ApiClass.GetXRayByPoint(point.X, point.Y, 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. return true;
  114. }
  115. else
  116. {
  117. if (!eds.IsConnected())
  118. {
  119. return false;
  120. }
  121. int xrayNum = a_listParticles.Count;
  122. Point[] Ps = new Point[xrayNum];
  123. for (int i = 0; i < xrayNum; i++)
  124. {
  125. Point p = (Point)a_listParticles[i].GetXRayPos();
  126. Ps[i].X = p.X;
  127. Ps[i].Y = p.Y;
  128. }
  129. COTSParticleClr[] parts = a_listParticles.ToArray();
  130. return eds.GetXRayByPoints(a_nXRayAQTime, Ps, parts, a_bElementInfo);
  131. }
  132. }
  133. public bool CollectSpectrum(uint a_nXRayAQTime, ref uint[] a_XrayData)
  134. {
  135. if (isFEI)
  136. {
  137. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  138. return ApiClass.AcquireSpectrum(false, ref eleItems, ref a_XrayData);
  139. }
  140. else
  141. {
  142. if (!eds.IsConnected())
  143. {
  144. return false;
  145. }
  146. return eds.CollectSpectrum(a_nXRayAQTime, ref a_XrayData);
  147. }
  148. }
  149. public bool GetXRayBySinglePoint(uint a_nXRayAQTime, Point point, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  150. {
  151. if (isFEI)
  152. {
  153. double x = point.X;
  154. double y = point.Y;
  155. double z = 0;
  156. double r = 0;
  157. double t = 0;
  158. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  159. if (!ApiClass.GetSemPositionXYZRT(ref x, ref y, ref z, ref r, ref t))
  160. {
  161. return false;
  162. }
  163. return ApiClass.GetXRayByPoint((int)x, (int)y, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  164. }
  165. else
  166. {
  167. if (!eds.IsConnected())
  168. {
  169. return false;
  170. }
  171. return eds.GetXRayBySinglePoint(a_nXRayAQTime, point, ref a_XrayData, ref ele, a_bElementInfo);
  172. }
  173. }
  174. public bool GetXRayBySingleFeature(uint a_nXRayAQTime, COTSFeatureClr fea, ref uint[] a_XrayData, ref string ele, bool a_bElementInfo)
  175. {
  176. if (isFEI)
  177. {
  178. Rectangle rectangle = new Rectangle();
  179. rectangle.X = fea.GetSegmentsList().Min(a => a.GetStart());
  180. rectangle.Y = fea.GetSegmentsList().Min(a => a.GetHeight());
  181. rectangle.Width = fea.GetSegmentsList().Max(a => a.GetLength());
  182. rectangle.Height = Math.Abs(rectangle.Y - rectangle.X);
  183. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  184. return ApiClass.GetXRayByRect(rectangle, strResolution, a_nXRayAQTime, AnalyExpCount, a_bElementInfo, ref eleItems, ref a_XrayData);
  185. }
  186. else
  187. {
  188. if (!eds.IsConnected())
  189. {
  190. return false;
  191. }
  192. return eds.GetXRayBySingleFeature(a_nXRayAQTime, fea, ref a_XrayData, ref ele, false);
  193. }
  194. }
  195. public bool GetXRayElements(uint a_nXRayAQTime, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  196. {
  197. if (isFEI)
  198. {
  199. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  200. bool isTrue = ApiClass.AcquireSpectrum(true, ref eleItems, ref a_XrayData);
  201. a_nElementNum = eleItems.Count;
  202. for (int i = 0; i < eleItems.Count; i++)
  203. {
  204. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  205. }
  206. return isTrue;
  207. }
  208. else
  209. {
  210. if (!eds.IsConnected())
  211. {
  212. return false;
  213. }
  214. return eds.GetXRayElements(a_nXRayAQTime, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  215. }
  216. }
  217. public bool GetXRayAndElements(uint a_nXRayAQTime, int dMouseImgX, int dMouseImgY, ref uint[] a_XrayData, ref ValueType a_nElementNum, ref string a_strResult)
  218. {
  219. if (isFEI)
  220. {
  221. Dictionary<string, double> eleItems = new Dictionary<string, double>();
  222. bool isTrue = ApiClass.GetXRayByPoint(dMouseImgX, dMouseImgY, strResolution, a_nXRayAQTime, AnalyExpCount, true, ref eleItems, ref a_XrayData);
  223. a_nElementNum = eleItems.Count;
  224. for (int i = 0; i < eleItems.Count; i++)
  225. {
  226. a_strResult += eleItems.ElementAt(i).Key + ":" + eleItems.ElementAt(i).Value + "\n";
  227. }
  228. return isTrue;
  229. }
  230. else
  231. {
  232. if (!eds.IsConnected())
  233. {
  234. return false;
  235. }
  236. return eds.GetXRayAndElements(a_nXRayAQTime, dMouseImgX, dMouseImgY, ref a_XrayData, ref a_nElementNum, ref a_strResult);
  237. }
  238. }
  239. public bool Init()
  240. {
  241. if (isFEI)
  242. {
  243. return ApiClass.isConnect();
  244. }
  245. else
  246. {
  247. bool m_init = eds.EDSInit();
  248. return m_init;
  249. }
  250. }
  251. public bool ScanInit()
  252. {
  253. if (isFEI)
  254. {
  255. return ApiClass.isConnect();
  256. }
  257. else
  258. {
  259. bool m_init = eds.ScanInit();
  260. return m_init;
  261. }
  262. }
  263. public bool Connect()
  264. {
  265. if (isFEI)
  266. {
  267. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  268. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  269. if (FEIIP == "" || FEIPORT == "")
  270. {
  271. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  272. return false;
  273. }
  274. if (ApiClass.isConnect())
  275. {
  276. return true;
  277. }
  278. return ApiClass.Connect(FEIIP, FEIPORT);
  279. }
  280. else
  281. {
  282. if (!eds.Init())
  283. {
  284. return false;
  285. }
  286. if (eds.IsConnected())
  287. {
  288. return true;
  289. }
  290. return eds.ConncetSem();
  291. }
  292. }
  293. public bool DisConnect()
  294. {
  295. if (isFEI)
  296. {
  297. return ApiClass.DisConnect();
  298. }
  299. else
  300. {
  301. if (eds.IsConnected())
  302. {
  303. return eds.DisconnectSem();
  304. }
  305. return true;
  306. }
  307. }
  308. public bool SetResolution(int ResolutionWidth, int ResolutionHeight)
  309. {
  310. if (isFEI)
  311. {
  312. strResolution = ResolutionWidth + "x" + ResolutionHeight;
  313. }
  314. return true;
  315. }
  316. public bool SetAnalyExpCount(int MaxCounts)
  317. {
  318. if (isFEI)
  319. {
  320. AnalyExpCount = MaxCounts;
  321. }
  322. return true;
  323. }
  324. }
  325. }