ExtenderWrapper.cs 54 KB

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