ExtenderWrapper.cs 53 KB

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