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