ExtenderWrapper1.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. 
  2. using OINA.Extender;
  3. using OINA.Extender.Acquisition;
  4. using OINA.Extender.Acquisition.Ed;
  5. using OINA.Extender.Acquisition.Image;
  6. using OINA.Extender.Data;
  7. using OINA.Extender.Data.Ed;
  8. using OINA.Extender.Data.Image;
  9. using OINA.Extender.MicroscopeControl;
  10. using OINA.Extender.Processing;
  11. using OINA.Extender.Processing.Ed;
  12. using OINA.Extender.Processing.Quant;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Windows.Forms;
  22. namespace OxfordExtenderWrapper
  23. {
  24. public class ExtenderWrapper1:MarshalByRefObject
  25. {
  26. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  27. private Thread controlThread;
  28. AutoResetEvent startEvent = new AutoResetEvent(false);
  29. AutoResetEvent endEvent = new AutoResetEvent(false);
  30. private oxfordCommandData currentCommand;
  31. #region 电镜控制、样品台
  32. //控制电镜
  33. private IMicroscopeController microscopeController = null;
  34. /// <summary>
  35. /// IImageAcquisitionController object
  36. /// </summary>
  37. private IImageAcquisitionController imageAcquisitionController = null;
  38. /// <summary>
  39. /// IImageSettings object
  40. /// </summary>
  41. private IImageAcquisitionSettings imageAcquisitionSettings = null;
  42. //控制器
  43. private IEdSpectrumAcquisitionController EdSpectrumAcquisitionController = null;
  44. private IEdSpectrumSettings EdSpectrumSettings = null;
  45. private IEdSpectrumProcessing EdSpectrumProcessing = null;
  46. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  47. private IAutoIdSettings autoIdSettings = null;
  48. private ISEMQuantSettings quantSettings = null;
  49. private IEdChordListAcquisitionController _edsChordListController = null;
  50. private OxfordControllerState m_nState;
  51. private IEdChordListSettings _edsChordListSetting;
  52. private int XRayChannelLength = 2000;
  53. private int EDSColletionTimeOut = 10000;
  54. private AutoResetEvent m_endControllerEvent;
  55. //当前feature的pixel数控制
  56. private long m_nCollectedPixelCounts;
  57. private long m_nPixelNum;
  58. private List<Element> m_listElementResult;
  59. const int g_nOxfordControllerProcessTime = 4;
  60. const int g_nOxfordControllerEnergyRange = 20;
  61. // feature计数控制
  62. private long m_nXrayDataCount;
  63. private long m_nCollectedXrayCounts;
  64. private bool m_bXrayDone = false;
  65. //电压
  66. private double m_dHighVoltage;
  67. //放大倍数
  68. private double m_dMagnification;
  69. //工作距离
  70. private double m_dWorkingDistance;
  71. //亮度
  72. private double m_dBirghtness;
  73. //对比度
  74. private double m_dContrast;
  75. //BeamOn
  76. private bool m_bBeamOn;
  77. //FilamentOn
  78. private bool m_bFilamentOn;
  79. //样品台位置
  80. private double m_dStageX;
  81. private double m_dStageY;
  82. private double m_dStageZ;
  83. private double m_dStageR;
  84. private double m_dStageT;
  85. private double m_dStageXMax;
  86. //构造函数
  87. public ExtenderWrapper1()
  88. {
  89. }
  90. public bool ConnectToEDSHardware()
  91. {
  92. try
  93. {
  94. InitMicroscopeController();
  95. InitImageAcquisition();
  96. InitXrayAcquistion();
  97. controlThread = new Thread(this.ControlerThread);
  98. controlThread.Start();
  99. return true;
  100. }
  101. catch (Exception e)
  102. {
  103. log.Error(e.Message);
  104. return false;
  105. }
  106. }
  107. //结束
  108. public void CloseExtender()
  109. {
  110. CloseMicroscopeController();
  111. CloseImageAcquisition();
  112. CloaseXrayAcquistion();
  113. currentCommand.commandType = OxfordCommandType.Exit;
  114. if (controlThread != null)
  115. {
  116. startEvent.Set();
  117. controlThread.Join();
  118. }
  119. controlThread = null;
  120. }
  121. private void ControlerThread()
  122. {
  123. log.Info("this has been initialized");
  124. while (true)
  125. {
  126. while (true)
  127. {
  128. if (startEvent.WaitOne(0, true))
  129. {
  130. break;
  131. }
  132. //Thread.Sleep(50);
  133. Application.DoEvents();
  134. }
  135. switch (currentCommand.commandType)
  136. {
  137. case OxfordCommandType.MoveStageXY:
  138. {
  139. MoveStageParam p = currentCommand.moveStagePrm;
  140. var stageDictionary = new Dictionary<Stage, double>
  141. {
  142. { Stage.StageX, (double)p.x/1000.0 },
  143. { Stage.StageY, (double)p.y /1000.0}
  144. };
  145. m_StageUpdated = false;
  146. this.microscopeController.SetStageConditions(stageDictionary);
  147. currentCommand.returnType = true;
  148. int time1 = Environment.TickCount;
  149. int time2;
  150. while (!m_StageUpdated)
  151. {
  152. Application.DoEvents();
  153. Thread.Sleep(100);
  154. time2 = Environment.TickCount;
  155. if (time2 - time1 > 20000)
  156. {
  157. currentCommand.returnType = false;
  158. break;
  159. }
  160. }
  161. endEvent.Set();
  162. }
  163. break;
  164. case OxfordCommandType.ImageAquisition:
  165. {
  166. ImageAquistionParam p = currentCommand.grabImgParam;
  167. SetImageAcquistionSetting(p.DwellTime, p.sourceType, p.width);
  168. try
  169. {
  170. int lastingTime = 0;
  171. m_bAcquistionDone = false;
  172. imageAcquisitionController.BeginMultipleAcquisition();
  173. IEnumerable<IElectronImage> images = imageAcquisitionController.StartAcquisition(imageAcquisitionSettings);
  174. while (true)
  175. {
  176. if (m_bAcquistionDone)
  177. {
  178. p.ImageData = m_ImageBit;
  179. currentCommand.returnType = true;
  180. break;
  181. }
  182. Application.DoEvents();
  183. Thread.Sleep(100);
  184. lastingTime += 100;
  185. if (lastingTime > EDSColletionTimeOut * 6)
  186. {
  187. currentCommand.returnType = false;
  188. }
  189. }
  190. }
  191. catch (InvalidSettingsException settingsException)
  192. {
  193. var sb = new StringBuilder(@"Invalid settings have been supplied: ");
  194. sb.AppendLine();
  195. settingsException.ValidationResults.ValidationErrors.ToList().ForEach(ve => sb.AppendFormat("{0}{1}", Environment.NewLine, ve));
  196. NLog.LogManager.GetCurrentClassLogger().Error(sb.ToString());
  197. }
  198. catch (AcquisitionStartException startException)
  199. {
  200. string msg = string.Format(@"AcquisitionStartException: {0}", startException.Message);
  201. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  202. }
  203. endEvent.Set();
  204. }
  205. break;
  206. case OxfordCommandType.XrayPointCollection:
  207. {
  208. PointXrayParam p = currentCommand.pointXrayPrm;
  209. m_bXrayDone = false;
  210. p.XrayData = new uint[XRayChannelLength];
  211. p.listElement = new Dictionary<string, double>();
  212. SetXrayAcquisitionParam(p.dMilliSecondsTime);
  213. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(p.x * m_dImagePixelsize, p.y * m_dImagePixelsize));
  214. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  215. try
  216. {
  217. m_bXrayDone = false;
  218. int lastingTime = 0;
  219. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  220. while (true)
  221. {
  222. if (m_bXrayDone)
  223. {
  224. currentCommand.returnType = true;
  225. break;
  226. }
  227. Application.DoEvents();
  228. Thread.Sleep(100);
  229. lastingTime += 100;
  230. if (lastingTime > EDSColletionTimeOut * 3)
  231. {
  232. log.Warn("XrayStartAcquisition 超时!");
  233. currentCommand.returnType = false;
  234. }
  235. }
  236. }
  237. catch (InvalidSettingsException invalidSettingsException)
  238. {
  239. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  240. invalidSettingsException.Message,
  241. invalidSettingsException.ValidationResults.ValidationErrors);
  242. log.Error(msg);
  243. }
  244. catch (AcquisitionStartException acquisitionStartException)
  245. {
  246. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  247. log.Error(msg);
  248. }
  249. endEvent.Set();
  250. }
  251. break;
  252. case OxfordCommandType.COLLECT_XRAYPOINTS:
  253. {
  254. var p = currentCommand.XrayPrmForPoints;
  255. var PointXrayDatas = currentCommand.XrayPrmForPoints;
  256. currentCommand.PointXrayDataReceived = 0;
  257. m_bXrayDone = false;
  258. int lastingTime = 0;
  259. var dMilliSecondsTime = PointXrayDatas[0].dMilliSecondsTime;
  260. SetXrayAcquisitionParam(dMilliSecondsTime);
  261. foreach (var prm in PointXrayDatas)
  262. {
  263. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(prm.x * m_dImagePixelsize, prm.y * m_dImagePixelsize));
  264. log.Info("开始XrayStartAcquisition:t=" + dMilliSecondsTime.ToString() + "pos=" + "(" + prm.x.ToString() + "," + prm.y.ToString() + ")");
  265. try
  266. {
  267. m_bXrayDone = false;
  268. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  269. }
  270. catch (InvalidSettingsException invalidSettingsException)
  271. {
  272. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  273. invalidSettingsException.Message,
  274. invalidSettingsException.ValidationResults.ValidationErrors);
  275. log.Error(msg);
  276. }
  277. catch (AcquisitionStartException acquisitionStartException)
  278. {
  279. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  280. log.Error(msg);
  281. }
  282. }
  283. while (true)
  284. {
  285. if (m_bXrayDone)
  286. {
  287. currentCommand.returnType = true;
  288. break;
  289. }
  290. Application.DoEvents();
  291. Thread.Sleep(100);
  292. lastingTime += 100;
  293. if (lastingTime > EDSColletionTimeOut * 3)
  294. {
  295. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  296. log.Warn("XrayStartAcquisition 超时!");
  297. currentCommand.returnType = false;
  298. }
  299. }
  300. endEvent.Set();
  301. }
  302. break;
  303. case OxfordCommandType.XrayAreaCollection:
  304. {
  305. AreaXrayParam p = currentCommand.areaXrayPrm;
  306. log.Info("线程:开始面扫xray");
  307. m_bXrayDone = false;
  308. SetXrayAcquisitionParam(p.dMilliSecondsTime);
  309. List<Chord> Chords = new List<Chord>();
  310. foreach (Segment seg in p.a_listChord)
  311. {
  312. Chord chord = new Chord(seg.X, seg.Y, seg.Length);
  313. Chords.Add(chord);
  314. }
  315. SetAreaAcquistionRegion(Chords);
  316. try
  317. {
  318. int lastingTime = 0;
  319. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  320. while (true)
  321. {
  322. if (m_bXrayDone)
  323. {
  324. currentCommand.returnType = true;
  325. break;
  326. }
  327. Thread.Sleep(100);
  328. Application.DoEvents();
  329. lastingTime += 100;
  330. if (lastingTime > EDSColletionTimeOut * 8)
  331. {
  332. currentCommand.returnType = false;
  333. }
  334. }
  335. }
  336. catch (InvalidSettingsException invalidSettingsException)
  337. {
  338. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  339. invalidSettingsException.Message,
  340. invalidSettingsException.ValidationResults.ValidationErrors);
  341. log.Error(msg);
  342. }
  343. catch (AcquisitionStartException acquisitionStartException)
  344. {
  345. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  346. log.Error(msg);
  347. }
  348. endEvent.Set();
  349. }
  350. break;
  351. case OxfordCommandType.COLLECT_XRAYFEATURES:
  352. {
  353. var p = currentCommand.XrayPrmForFeatures;
  354. currentCommand.AreaXrayDataReceived = 0;
  355. log.Info("线程:开始线扫xray");
  356. //m_XrayData = null;
  357. m_bXrayDone = false;
  358. int lastingTime = 0;
  359. var dMilliSecondsTime = p[0].dMilliSecondsTime;
  360. SetXrayAcquisitionParam(p[0].dMilliSecondsTime);
  361. foreach (var prm in p)
  362. {
  363. List<Chord> Chords = new List<Chord>();
  364. foreach (Segment seg in prm.a_listChord)
  365. {
  366. Chord chord = new Chord(seg.X, seg.Y, seg.Length);
  367. Chords.Add(chord);
  368. }
  369. SetAreaAcquistionRegion(Chords);
  370. try
  371. {
  372. m_bXrayDone = false;
  373. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  374. }
  375. catch (InvalidSettingsException invalidSettingsException)
  376. {
  377. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  378. invalidSettingsException.Message,
  379. invalidSettingsException.ValidationResults.ValidationErrors);
  380. log.Error(msg);
  381. }
  382. catch (AcquisitionStartException acquisitionStartException)
  383. {
  384. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  385. log.Error(msg);
  386. }
  387. }
  388. while (true)
  389. {
  390. if (m_bXrayDone)
  391. {
  392. currentCommand.returnType = true;
  393. break;
  394. }
  395. Application.DoEvents();
  396. Thread.Sleep(100);
  397. lastingTime += 100;
  398. if (lastingTime > EDSColletionTimeOut * 3)
  399. {
  400. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  401. log.Warn("XrayStartAcquisition 超时!");
  402. //return false;
  403. currentCommand.returnType = false;
  404. }
  405. }
  406. endEvent.Set();
  407. }
  408. break;
  409. case OxfordCommandType.Exit:
  410. {
  411. endEvent.Set();
  412. log.Info("receive exit command");
  413. }
  414. break;
  415. }
  416. if (currentCommand.commandType == OxfordCommandType.Exit)
  417. {
  418. log.Info("exiting the thread");
  419. break;
  420. }
  421. }
  422. }
  423. public bool AquisitionImage(ref ImageAquistionParam p)
  424. {
  425. currentCommand.grabImgParam = p;
  426. currentCommand.commandType = OxfordCommandType.ImageAquisition;
  427. SetImageAcquistionSetting(p.DwellTime, p.sourceType, p.width);
  428. try
  429. {
  430. int lastingTime = 0;
  431. m_bAcquistionDone = false;
  432. imageAcquisitionController.BeginMultipleAcquisition();
  433. IEnumerable<IElectronImage> images = imageAcquisitionController.StartAcquisition(imageAcquisitionSettings);
  434. while (true)
  435. {
  436. if (m_bAcquistionDone)
  437. {
  438. p.ImageData = m_ImageBit;
  439. currentCommand.returnType = true;
  440. break;
  441. }
  442. Application.DoEvents();
  443. Thread.Sleep(100);
  444. lastingTime += 100;
  445. if (lastingTime > EDSColletionTimeOut * 6)
  446. {
  447. currentCommand.returnType = false;
  448. break;
  449. }
  450. }
  451. }
  452. catch (InvalidSettingsException settingsException)
  453. {
  454. var sb = new StringBuilder(@"Invalid settings have been supplied: ");
  455. sb.AppendLine();
  456. settingsException.ValidationResults.ValidationErrors.ToList().ForEach(ve => sb.AppendFormat("{0}{1}", Environment.NewLine, ve));
  457. NLog.LogManager.GetCurrentClassLogger().Error(sb.ToString());
  458. }
  459. catch (AcquisitionStartException startException)
  460. {
  461. string msg = string.Format(@"AcquisitionStartException: {0}", startException.Message);
  462. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  463. }
  464. if (currentCommand.returnType == true)
  465. {
  466. return true;
  467. }
  468. else
  469. {
  470. return false;
  471. }
  472. }
  473. public bool MoveStageXY(float x, float y)
  474. {
  475. currentCommand.moveStagePrm = new MoveStageParam();
  476. currentCommand.moveStagePrm.x = x;
  477. currentCommand.moveStagePrm.y = y;
  478. currentCommand.commandType = OxfordCommandType.MoveStageXY;
  479. startEvent.Set();
  480. while (true)
  481. {
  482. if (endEvent.WaitOne(0, false))
  483. {
  484. break;
  485. }
  486. }
  487. if (currentCommand.returnType == true)
  488. {
  489. return true;
  490. }
  491. else
  492. {
  493. return false;
  494. }
  495. }
  496. public bool XrayAreaCollecting(ref AreaXrayParam p)
  497. {
  498. currentCommand.areaXrayPrm = p;
  499. currentCommand.commandType = OxfordCommandType.XrayAreaCollection;
  500. startEvent.Set();
  501. while (true)
  502. {
  503. if (endEvent.WaitOne(0, false))
  504. {
  505. break;
  506. }
  507. }
  508. if (currentCommand.returnType == true)
  509. {
  510. return true;
  511. }
  512. else
  513. {
  514. return false;
  515. }
  516. }
  517. public bool XrayPointCollecting(ref PointXrayParam p)
  518. {
  519. currentCommand.pointXrayPrm = p;
  520. currentCommand.commandType = OxfordCommandType.XrayPointCollection;
  521. startEvent.Set();
  522. while (true)
  523. {
  524. if (endEvent.WaitOne(0, false))
  525. {
  526. break;
  527. }
  528. Application.DoEvents();
  529. }
  530. if (currentCommand.returnType == true)
  531. {
  532. return true;
  533. }
  534. else
  535. {
  536. return false;
  537. }
  538. }
  539. public bool CollectXrayByPoints(ref List<PointXrayParam> a_listPoints, uint a_nXRayAQTime, bool a_bElementInfo)
  540. {
  541. currentCommand.XrayPrmForPoints = a_listPoints;
  542. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYPOINTS;
  543. startEvent.Set();
  544. while (true)
  545. {
  546. if (endEvent.WaitOne(0, false))
  547. {
  548. break;
  549. }
  550. Application.DoEvents();
  551. }
  552. if (currentCommand.returnType == true)
  553. {
  554. return true;
  555. }
  556. else
  557. {
  558. return false;
  559. }
  560. }
  561. public bool CollectXrayByFeatures(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
  562. {
  563. currentCommand.XrayPrmForFeatures = a_listFeatures;
  564. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYFEATURES;
  565. startEvent.Set();
  566. while (true)
  567. {
  568. if (endEvent.WaitOne(0, false))
  569. {
  570. break;
  571. }
  572. }
  573. if (currentCommand.returnType == true)
  574. {
  575. a_listFeatures = currentCommand.XrayPrmForFeatures;
  576. return true;
  577. }
  578. else
  579. {
  580. return false;
  581. }
  582. }
  583. internal oxfordCommandData GetCurrentCommand()
  584. {
  585. return currentCommand;
  586. }
  587. internal void SetCurrentCommand(oxfordCommandData value)
  588. {
  589. currentCommand = value;
  590. }
  591. //控制电镜初始化
  592. void InitMicroscopeController()
  593. {
  594. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  595. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  596. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  597. //this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
  598. //this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
  599. this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
  600. ReadMicroscopeColumn();
  601. ReadStage();
  602. }
  603. //控制电镜释放
  604. void CloseMicroscopeController()
  605. {
  606. if (microscopeController != null)
  607. {
  608. this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
  609. this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
  610. this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
  611. this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
  612. this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
  613. microscopeController = null;
  614. }
  615. }
  616. //读取当前的电镜控制值
  617. private void ReadMicroscopeColumn()
  618. {
  619. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  620. var columnConditions = this.microscopeController.ColumnConditions;
  621. if (columnCapabilities.Magnification.CanRead)
  622. {
  623. m_dMagnification = columnConditions.Magnification;
  624. }
  625. if (columnCapabilities.WorkingDistance.CanRead)
  626. {
  627. m_dWorkingDistance = columnConditions.WorkingDistance;
  628. }
  629. if (columnCapabilities.HighVoltage.CanRead)
  630. {
  631. m_dHighVoltage = columnConditions.HighVoltage;
  632. }
  633. if (columnCapabilities.Brightness.CanRead)
  634. {
  635. m_dBirghtness = columnConditions.Brightness;
  636. }
  637. if (columnCapabilities.Contrast.CanRead)
  638. {
  639. m_dContrast = columnConditions.Contrast;
  640. }
  641. if (columnCapabilities.BeamOn.CanRead)
  642. {
  643. m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
  644. }
  645. if (columnCapabilities.FilamentOn.CanRead)
  646. {
  647. m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
  648. }
  649. }
  650. //读取样品台位置
  651. private void ReadStage()
  652. {
  653. var stageCapabilities = this.microscopeController.StageCapabilities;
  654. var stageConditions = this.microscopeController.StageConditions;
  655. if (stageCapabilities.StageX.CanRead)
  656. {
  657. this.m_dStageX = stageConditions.StageX/1000.0;
  658. }
  659. if (stageCapabilities.StageY.CanRead)
  660. {
  661. this.m_dStageY = stageConditions.StageY/1000.0;
  662. }
  663. if (stageCapabilities.StageZ.CanRead)
  664. {
  665. this.m_dStageZ = stageConditions.StageZ;
  666. }
  667. if (stageCapabilities.StageT.CanRead)
  668. {
  669. this.m_dStageT = stageConditions.StageT;
  670. }
  671. if (stageCapabilities.StageR.CanRead)
  672. {
  673. this.m_dStageR = stageConditions.StageR;
  674. }
  675. }
  676. //电镜控制改变事件
  677. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  678. {
  679. ReadMicroscopeColumn();
  680. }
  681. //样品台控制改变事件
  682. private void OnMicroscopeStageChange(object sender, EventArgs e)
  683. {
  684. ReadStage();
  685. }
  686. //列控制连接或断开时的事件
  687. private void OnMicroscopeColumnConnected(object sender, EventArgs e)
  688. {
  689. }
  690. //样品台控制连接或断开时的事件
  691. private void OnMicroscopeStageConnected(object sender, EventArgs e)
  692. {
  693. ReadStage();
  694. }
  695. //样品台控制、电镜控制、外围控制的事件改变完成
  696. private void OnMicroscopeChangeCompleted(object sender, CompletedEventArgs e)
  697. {
  698. if (e.Control == MicroscopeControlType.Stage)
  699. {
  700. if (e.Success)
  701. {
  702. m_StageUpdated = true;
  703. }
  704. }
  705. else if (e.Control == MicroscopeControlType.Column)
  706. {
  707. if (e.Success)
  708. {
  709. m_CollumnUpdated = true;
  710. }
  711. }
  712. else if (e.Control == MicroscopeControlType.ExternalScan)
  713. {
  714. if (e.Success)
  715. {
  716. m_ExternalScanUpdated = true;
  717. }
  718. }
  719. ReadMicroscopeColumn();
  720. ReadStage();
  721. }
  722. public float GetMagnification()
  723. {
  724. return (float)m_dMagnification;
  725. }
  726. public Boolean SetMagnification(float set)
  727. {
  728. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  729. {
  730. { Column.Magnification, (double)set }
  731. };
  732. this.microscopeController.SetColumnConditions(columnDictionary);
  733. return true;
  734. }
  735. //焦距
  736. public float GetWorkingDistance()
  737. {
  738. return (float)m_dWorkingDistance;
  739. }
  740. public Boolean SetWorkingDistance(float set)
  741. {
  742. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  743. {
  744. { Column.WorkingDistance, (double)set }
  745. };
  746. this.microscopeController.SetColumnConditions(columnDictionary);
  747. return true;
  748. }
  749. //亮度
  750. public float GetBrightness()
  751. {
  752. return (float)m_dBirghtness;
  753. }
  754. public Boolean SetBrightness(float set)
  755. {
  756. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  757. {
  758. { Column.Brightness, (double)set }
  759. };
  760. this.microscopeController.SetColumnConditions(columnDictionary);
  761. return true;
  762. }
  763. //对比度
  764. public float GetContrast()
  765. {
  766. return (float)m_dContrast;
  767. }
  768. public Boolean SetContrast(float set)
  769. {
  770. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  771. {
  772. { Column.Contrast, (double)set }
  773. };
  774. this.microscopeController.SetColumnConditions(columnDictionary);
  775. return true;
  776. }
  777. //SEM电压
  778. public float GetSEMVoltage()
  779. {
  780. return (float)m_dHighVoltage;
  781. }
  782. public Boolean SetSEMVoltage(float set)
  783. {
  784. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  785. {
  786. { Column.HighVoltage, (double)set }
  787. };
  788. this.microscopeController.SetColumnConditions(columnDictionary);
  789. return true;
  790. }
  791. public bool SetSemScanExternal(bool b)
  792. {
  793. this.microscopeController.SetExternalScan(b);
  794. return true;
  795. }
  796. //样品台
  797. public float[] GetStagePosition()
  798. {
  799. float[] ret = new float[5];
  800. ret[0] = (float)m_dStageX;
  801. ret[1] = (float)m_dStageY;
  802. ret[2] = (float)m_dStageZ;
  803. ret[3] = (float)m_dStageT;
  804. ret[4] = (float)m_dStageR;
  805. return ret;
  806. }
  807. public Boolean SetStagePosition(float[] set)
  808. {
  809. double stageX = (double)set[0];
  810. double stageY = (double)set[1];
  811. double stageZ = (double)set[2];
  812. double stageT = (double)set[3];
  813. double stageR = (double)set[4];
  814. var stageDictionary = new Dictionary<Stage, double>
  815. {
  816. { Stage.StageX, (double)stageX },
  817. { Stage.StageY, (double)stageY },
  818. { Stage.StageZ, (double)stageZ },
  819. { Stage.StageT, (double)stageT },
  820. { Stage.StageR, (double)stageR }
  821. };
  822. this.microscopeController.SetStageConditions(stageDictionary);
  823. return true;
  824. }
  825. public float GetStageAtX()
  826. {
  827. return (float)m_dStageX;
  828. }
  829. public float GetStageAtY()
  830. {
  831. return (float)m_dStageY;
  832. }
  833. public float GetStageAtZ()
  834. {
  835. return (float)m_dStageZ;
  836. }
  837. public float GetStageAtT()
  838. {
  839. return (float)m_dStageT;
  840. }
  841. public float GetStageAtR()
  842. {
  843. return (float)m_dStageR;
  844. }
  845. public Boolean SetStageGotoX(float set)
  846. {
  847. double stageX = (double)set;
  848. var stageDictionary = new Dictionary<Stage, double>
  849. {
  850. { Stage.StageX, (double)stageX }
  851. };
  852. this.microscopeController.SetStageConditions(stageDictionary);
  853. return true;
  854. }
  855. public Boolean SetStageGotoY(float set)
  856. {
  857. double stageY = (double)set;
  858. var stageDictionary = new Dictionary<Stage, double>
  859. {
  860. { Stage.StageY, (double)stageY }
  861. };
  862. this.microscopeController.SetStageConditions(stageDictionary);
  863. return true;
  864. }
  865. public Boolean SetStageGotoZ(float set)
  866. {
  867. double stageZ = (double)set;
  868. var stageDictionary = new Dictionary<Stage, double>
  869. {
  870. { Stage.StageZ, (double)stageZ }
  871. };
  872. this.microscopeController.SetStageConditions(stageDictionary);
  873. return true;
  874. }
  875. public Boolean SetStageGotoT(float set)
  876. {
  877. double stageT = (double)set;
  878. var stageDictionary = new Dictionary<Stage, double>
  879. {
  880. { Stage.StageT, (double)stageT }
  881. };
  882. this.microscopeController.SetStageConditions(stageDictionary);
  883. return true;
  884. }
  885. public Boolean SetStageGotoR(float set)
  886. {
  887. double stageR = (double)set;
  888. var stageDictionary = new Dictionary<Stage, double>
  889. {
  890. { Stage.StageR, (double)stageR }
  891. };
  892. this.microscopeController.SetStageConditions(stageDictionary);
  893. return true;
  894. }
  895. private Boolean MoveStage(float x, float y)
  896. {
  897. double stageX = (double)x;
  898. double stageY = (double)y;
  899. var stageDictionary = new Dictionary<Stage, double>
  900. {
  901. { Stage.StageX, (double)stageX },
  902. { Stage.StageY, (double)stageY }
  903. };
  904. this.microscopeController.SetStageConditions(stageDictionary);
  905. return true;
  906. }
  907. #endregion
  908. #region 拍图
  909. //图像扫描尺寸
  910. public double[] ImageScanSize =
  911. {
  912. 32,
  913. 64,
  914. 128,
  915. 256,
  916. 512,
  917. 704,
  918. 768,
  919. 1024,
  920. 4096,
  921. 8192
  922. };
  923. private byte[] m_ImageBit = null;
  924. private long m_nImageWidth = 0;
  925. private long m_nImageHeight = 0;
  926. private double m_dImagePixelsize = 0;//it will be initialized when we get an image from the EDS.
  927. bool m_bAcquistionDone = false;
  928. private bool m_StageUpdated;
  929. private bool m_CollumnUpdated;
  930. private bool m_ExternalScanUpdated;
  931. private double dImagePixelsize;
  932. public double GetDImagePixelsize()
  933. {
  934. return m_dImagePixelsize;
  935. }
  936. internal void SetDImagePixelsize(double value)
  937. {
  938. m_dImagePixelsize = value;
  939. }
  940. //private Bitmap m_Bitmap = null;
  941. void InitImageAcquisition()
  942. {
  943. imageAcquisitionController = AcquireFactory.CreateImageServer();
  944. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  945. //imageAcquisitionSettings.
  946. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  947. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  948. }
  949. void InitXrayAcquistion()
  950. {
  951. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  952. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  953. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  954. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  955. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  956. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  957. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  958. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  959. }
  960. //控制电镜释放
  961. void CloseImageAcquisition()
  962. {
  963. if (imageAcquisitionController != null)
  964. {
  965. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  966. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  967. imageAcquisitionController = null;
  968. }
  969. }
  970. void CloaseXrayAcquistion()
  971. {
  972. if (EdSpectrumAcquisitionController != null)
  973. {
  974. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  975. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  976. EdSpectrumAcquisitionController = null;
  977. }
  978. }
  979. /// <summary>
  980. /// OnImageExperimentStarted
  981. /// </summary>
  982. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  983. {
  984. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  985. log.Info("拍图开始事件!");
  986. }
  987. //int m_nState;
  988. /// <summary>
  989. /// OnImageExperimentFinished
  990. /// </summary>
  991. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  992. {
  993. IElectronImage electronImage = e.Value[0];
  994. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  995. {
  996. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  997. }
  998. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  999. {
  1000. m_bAcquistionDone = true;
  1001. }
  1002. }
  1003. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1004. {
  1005. a_nImageHeight = 0;
  1006. a_nImageWidth = 0;
  1007. a_nPixelSize = 0;
  1008. a_pImageBits = null;
  1009. if (a_electronImage == null)
  1010. {
  1011. return false;
  1012. }
  1013. a_nImageHeight = a_electronImage.Height;
  1014. a_nImageWidth = a_electronImage.Width;
  1015. a_nPixelSize = a_electronImage.PixelSize;
  1016. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1017. long nImageSize = a_nImageHeight * a_nImageWidth;
  1018. long nBufferSize = nImageSize * nBytesPerPixel;
  1019. Byte[] imageData = new Byte[nBufferSize];
  1020. a_electronImage.GetData(imageData);
  1021. a_pImageBits = new Byte[nImageSize];
  1022. // default, oxford will return short image, we need to convert to byte
  1023. if (nBytesPerPixel == 2)
  1024. {
  1025. int nBSEValue = 0;
  1026. for (int i = 0; i < nImageSize; ++i)
  1027. {
  1028. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1029. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1030. }
  1031. }
  1032. else
  1033. {
  1034. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1035. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1036. int nOffset = nBytesPerPixel - 1;
  1037. for (int i = 0; i < nImageSize; ++i)
  1038. {
  1039. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1040. }
  1041. }
  1042. return true;
  1043. }
  1044. //a_dDwellTime : 1~100000之间的数
  1045. //a_sImageType : 1: SE, 2: Bse
  1046. //a_dImageScanSize : 图像分辨率,图像的高
  1047. public bool SetImageAcquistionSetting(double a_dDwellTime, ImageInputSources a_nImageType, double a_dImageScanSize)
  1048. {
  1049. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1050. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1051. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1052. if (a_dDwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1053. {
  1054. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1055. }
  1056. if (a_dDwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1057. {
  1058. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1059. }
  1060. else
  1061. {
  1062. imageSettings.DwellTimeMicroSeconds = a_dDwellTime;
  1063. }
  1064. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1065. imageSettings.EnableInputSource((ImageInputSources)a_nImageType, true);
  1066. if (!ImageScanSize.Contains(a_dImageScanSize))
  1067. {
  1068. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1069. return false;
  1070. }
  1071. var pixelSize = 1d / a_dImageScanSize;
  1072. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1073. return true;
  1074. }
  1075. #endregion
  1076. #region X-ray
  1077. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1078. {
  1079. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1080. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1081. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1082. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1083. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1084. // EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateFullFieldRegion(1.0 / 1024.0);
  1085. }
  1086. void SetAreaAcquistionRegion(List<Chord> a_nChords)
  1087. {
  1088. ChordList chordsList = null;
  1089. chordsList = new ChordList(a_nChords, m_dImagePixelsize);
  1090. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  1091. }
  1092. /// <summary>
  1093. /// Called when IEdSpectrumAcquisitionController Experiment Starting.
  1094. /// </summary>
  1095. /// <param name="sender">The sender.</param>
  1096. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  1097. private void OnEdSpectrumExperimentStarting(object sender, AcquisitionStartingEventArgs<IEdSpectrum> e)
  1098. {
  1099. }
  1100. /// <summary>
  1101. /// Called when IEdSpectrumAcquisitionController Experiment Started.
  1102. /// </summary>
  1103. /// <param name="sender">The sender.</param>
  1104. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  1105. private void OnEdSpectrumExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdSpectrum> e)
  1106. {
  1107. }
  1108. private IEdChordListSettings GetChordlistSettings()
  1109. {
  1110. if (_edsChordListSetting == null)
  1111. {
  1112. _edsChordListSetting = AcquireFactory.CreateEdChordListSettings();
  1113. if (_edsChordListSetting != null)
  1114. {
  1115. _edsChordListSetting.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime;
  1116. _edsChordListSetting.EdSettings.NumberOfChannels = 1024;
  1117. _edsChordListSetting.EdSettings.ProcessTime = g_nOxfordControllerProcessTime;
  1118. _edsChordListSetting.EdSettings.EnergyRange = g_nOxfordControllerEnergyRange;
  1119. _edsChordListSetting.AutoIdSettings.SetKnownElement(79, true);
  1120. _edsChordListSetting.NumberOfProcessingThreads = 1;
  1121. }
  1122. else
  1123. {
  1124. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1125. log.Error("Create EDS chordlist setting failed.");
  1126. }
  1127. }
  1128. return _edsChordListSetting;
  1129. }
  1130. struct Element
  1131. {
  1132. public int m_nAotomaticNo;
  1133. public double m_dWeight;
  1134. }
  1135. /// <summary>
  1136. ///
  1137. /// </summary>
  1138. /// <param name="sender"></param>
  1139. /// <param name="e"></param>
  1140. /// <summary>
  1141. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1142. /// </summary>
  1143. /// <param name="sender">sender object</param>
  1144. /// <param name="e">The instance containing the event data.</param>
  1145. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1146. {
  1147. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1148. uint[] m_XrayData;
  1149. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1150. IEdSpectrum edSpectrum = e.Value;
  1151. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1152. {
  1153. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1154. }
  1155. long nXraycount = 0;
  1156. for (int i = 0; i < 2000; i++)
  1157. {
  1158. nXraycount += m_XrayData[i];
  1159. }
  1160. //Quantify processing
  1161. bool bquant = false;
  1162. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1163. {
  1164. bquant = currentCommand.pointXrayPrm.b_quant;
  1165. }
  1166. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1167. {
  1168. bquant = currentCommand.areaXrayPrm.b_quant;
  1169. }
  1170. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1171. {
  1172. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1173. bquant = curXrayData.b_quant;
  1174. }
  1175. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1176. {
  1177. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1178. bquant = curXrayData.b_quant;
  1179. }
  1180. var m_listElement = new Dictionary<string, double>();
  1181. if (bquant)
  1182. {
  1183. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1184. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1185. quantSettings.CombinedElement = 8;
  1186. quantSettings.Normalised = true;
  1187. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1188. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1189. var ie = Results.GetEnumerator();
  1190. while (ie.MoveNext())
  1191. {
  1192. ISEMQuantResult result = ie.Current;
  1193. if (result.WeightPercent != 0)
  1194. {
  1195. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1196. }
  1197. }
  1198. }
  1199. //------------------------
  1200. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1201. {
  1202. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1203. {
  1204. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1205. currentCommand.pointXrayPrm.listElement = m_listElement;
  1206. m_bXrayDone = true;
  1207. }
  1208. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1209. {
  1210. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1211. currentCommand.areaXrayPrm.listElement = m_listElement;
  1212. m_bXrayDone = true;
  1213. }
  1214. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1215. {
  1216. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1217. curXrayData.XrayData = m_XrayData;
  1218. curXrayData.listElement = m_listElement;
  1219. currentCommand.PointXrayDataReceived += 1;
  1220. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1221. {
  1222. m_bXrayDone = true;
  1223. }
  1224. }
  1225. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1226. {
  1227. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1228. curXrayData.XrayData = m_XrayData;
  1229. curXrayData.listElement = m_listElement;
  1230. currentCommand.AreaXrayDataReceived += 1;
  1231. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1232. {
  1233. m_bXrayDone = true;
  1234. }
  1235. }
  1236. }
  1237. }
  1238. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1239. {
  1240. a_pSpectrumData = new uint[a_nBufferSize];
  1241. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1242. a_spectrum.GetChannelData(xrayData);
  1243. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1244. int nChannelStart = 0;
  1245. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1246. {
  1247. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1248. }
  1249. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1250. double dStep1 = 1.0 / nDataLength;
  1251. double dStep2 = 1.0 / a_nBufferSize;
  1252. for (int i = 0; i < nDataLength; ++i)
  1253. {
  1254. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1255. double dBinPos = i * dStep1;
  1256. long nLeftBin = (long)(dBinPos / dStep2);
  1257. // calculate % into left bin
  1258. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1259. // calculate data into the left bin
  1260. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1261. // put data into bins
  1262. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1263. if ((nLeftBin + 1) < a_nBufferSize)
  1264. {
  1265. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1266. }
  1267. }
  1268. return true;
  1269. }
  1270. /// <summary>
  1271. /// Called when IEdSpectrum DataChanged
  1272. /// </summary>
  1273. /// <param name="sender">Event source</param>
  1274. /// <param name="e">Event arguments</param>
  1275. private void OnDataChanged(object sender, EventArgs e)
  1276. {
  1277. }
  1278. public bool IsAcquiringSpectrum()
  1279. {
  1280. return EdSpectrumAcquisitionController.IsAcquiring;
  1281. }
  1282. public void BeginMultipleAquisition()
  1283. {
  1284. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1285. }
  1286. public void EndMultipleAquisition()
  1287. {
  1288. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1289. }
  1290. //public void BeginAreaModeMultipleAquisition()
  1291. //{
  1292. // var controller = CreateChordlistController();
  1293. // controller.BeginMultipleAcquisition();
  1294. //}
  1295. //public void EndAreaModeMultipleAquisition()
  1296. //{
  1297. // var controller = CreateChordlistController();
  1298. // controller.EndMultipleAcquisition();
  1299. //}
  1300. public bool GetSemBeamOn()
  1301. {
  1302. var beamon = microscopeController.ColumnConditions.BeamOn;
  1303. if (beamon == 1)
  1304. {
  1305. return true;
  1306. }
  1307. else
  1308. {
  1309. return false;
  1310. }
  1311. }
  1312. public void StopXrayAquisition()
  1313. {
  1314. EdSpectrumAcquisitionController.StopAcquisition();
  1315. }
  1316. #endregion
  1317. }
  1318. }