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