ExtenderWrapper1.cs 57 KB

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