ExtenderWrapper.cs 53 KB

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