ExtenderWrapper.cs 53 KB

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