ExtenderWrapper.cs 53 KB

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