ExtenderWrapper1.cs 57 KB

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