ExtenderWrapper.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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. Chord chord;
  551. foreach (Segment seg in prm.a_listChord)
  552. {
  553. if (seg.Length >= 3)
  554. {
  555. chord = new Chord(seg.X+1, seg.Y, seg.Length-2);//get the middle part
  556. }
  557. else
  558. {
  559. chord = new Chord(seg.X, seg.Y, seg.Length);
  560. }
  561. Chords.Add(chord);
  562. }
  563. var chordsList = new ChordList(Chords, m_dImagePixelsize);
  564. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  565. try
  566. {
  567. m_bXrayDone = false;
  568. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  569. }
  570. catch (InvalidSettingsException invalidSettingsException)
  571. {
  572. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  573. invalidSettingsException.Message,
  574. invalidSettingsException.ValidationResults.ValidationErrors);
  575. log.Error(msg);
  576. }
  577. catch (AcquisitionStartException acquisitionStartException)
  578. {
  579. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  580. log.Error(msg);
  581. }
  582. }
  583. var time1 = Environment.TickCount;
  584. while (true)
  585. {
  586. if (m_bXrayDone)
  587. {
  588. currentCommand.returnType = true;
  589. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  590. break;
  591. }
  592. Application.DoEvents();
  593. var time2 = Environment.TickCount;
  594. if (time2-time1 > EDSColletionTimeOut * p.Count)
  595. {
  596. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  597. currentCommand.returnType = false;
  598. break;
  599. }
  600. }
  601. if (currentCommand.returnType == true)
  602. {
  603. a_listFeatures = currentCommand.XrayPrmForFeatures;
  604. return true;
  605. }
  606. else
  607. {
  608. return false;
  609. }
  610. }
  611. //控制电镜初始化
  612. void InitMicroscopeController()
  613. {
  614. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  615. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  616. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  617. this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
  618. this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
  619. this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
  620. //ReadMicroscopeColumn();
  621. //ReadStage();
  622. }
  623. //控制电镜释放
  624. void CloseMicroscopeController()
  625. {
  626. if (microscopeController != null)
  627. {
  628. this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
  629. this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
  630. this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
  631. this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
  632. this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
  633. microscopeController = null;
  634. }
  635. }
  636. //读取当前的电镜控制值
  637. private void ReadMicroscopeColumn()
  638. {
  639. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  640. var columnConditions = this.microscopeController.ColumnConditions;
  641. if (columnCapabilities.Magnification.CanRead)
  642. {
  643. m_dMagnification = columnConditions.Magnification;
  644. }
  645. if (columnCapabilities.WorkingDistance.CanRead)
  646. {
  647. m_dWorkingDistance = columnConditions.WorkingDistance;
  648. }
  649. if (columnCapabilities.HighVoltage.CanRead)
  650. {
  651. m_dHighVoltage = columnConditions.HighVoltage;
  652. }
  653. if (columnCapabilities.Brightness.CanRead)
  654. {
  655. m_dBirghtness = columnConditions.Brightness;
  656. }
  657. if (columnCapabilities.Contrast.CanRead)
  658. {
  659. m_dContrast = columnConditions.Contrast;
  660. }
  661. if (columnCapabilities.BeamOn.CanRead)
  662. {
  663. m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
  664. }
  665. if (columnCapabilities.FilamentOn.CanRead)
  666. {
  667. m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
  668. }
  669. }
  670. //读取样品台位置
  671. private void ReadStage()
  672. {
  673. var stageCapabilities = this.microscopeController.StageCapabilities;
  674. var stageConditions = this.microscopeController.StageConditions;
  675. while (!stageCapabilities.StageX.CanRead)
  676. {
  677. Thread.Sleep(100);
  678. }
  679. if (stageCapabilities.StageX.CanRead)
  680. {
  681. this.m_dStageX = stageConditions.StageX*1000.0;
  682. }
  683. if (stageCapabilities.StageY.CanRead)
  684. {
  685. this.m_dStageY = stageConditions.StageY*1000.0;
  686. }
  687. if (stageCapabilities.StageZ.CanRead)
  688. {
  689. this.m_dStageZ = stageConditions.StageZ;
  690. }
  691. if (stageCapabilities.StageT.CanRead)
  692. {
  693. this.m_dStageT = stageConditions.StageT;
  694. }
  695. if (stageCapabilities.StageR.CanRead)
  696. {
  697. this.m_dStageR = stageConditions.StageR;
  698. }
  699. }
  700. //电镜控制改变事件
  701. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  702. {
  703. ReadMicroscopeColumn();
  704. }
  705. //样品台控制改变事件
  706. private void OnMicroscopeStageChange(object sender, EventArgs e)
  707. {
  708. ReadStage();
  709. }
  710. //镜筒控制连接或断开时的事件
  711. private void OnMicroscopeColumnConnected(object sender, EventArgs e)
  712. {
  713. ReadMicroscopeColumn();
  714. }
  715. //样品台控制连接或断开时的事件
  716. private void OnMicroscopeStageConnected(object sender, EventArgs e)
  717. {
  718. ReadStage();
  719. }
  720. //样品台控制、电镜控制、外围控制的事件改变完成
  721. private void OnMicroscopeChangeCompleted(object sender, CompletedEventArgs e)
  722. {
  723. if (e.Control == MicroscopeControlType.Stage)
  724. {
  725. if (e.Success)
  726. {
  727. m_StageUpdated = true;
  728. }
  729. }
  730. else if (e.Control == MicroscopeControlType.Column)
  731. {
  732. if (e.Success)
  733. {
  734. m_CollumnUpdated = true;
  735. }
  736. }
  737. else if (e.Control == MicroscopeControlType.ExternalScan)
  738. {
  739. if (e.Success)
  740. {
  741. m_ExternalScanUpdated = true;
  742. }
  743. }
  744. ReadMicroscopeColumn();
  745. ReadStage();
  746. }
  747. public float GetMagnification()
  748. {
  749. ReadMicroscopeColumn();
  750. return (float)m_dMagnification;
  751. }
  752. public Boolean SetMagnification(float set)
  753. {
  754. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  755. {
  756. { Column.Magnification, (double)set }
  757. };
  758. m_CollumnUpdated = false;
  759. this.microscopeController.SetColumnConditions(columnDictionary);
  760. int time1 = Environment.TickCount;
  761. int time2;
  762. while (!m_CollumnUpdated)
  763. {
  764. Application.DoEvents();
  765. time2 = Environment.TickCount;
  766. if (time2 - time1 > 10000)
  767. {
  768. currentCommand.returnType = false;
  769. return false;
  770. }
  771. }
  772. return true;
  773. }
  774. //焦距
  775. public float GetWorkingDistance()
  776. {
  777. ReadMicroscopeColumn();
  778. return (float)m_dWorkingDistance;
  779. }
  780. public Boolean SetWorkingDistance(float set)
  781. {
  782. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  783. {
  784. { Column.WorkingDistance, (double)set }
  785. };
  786. m_CollumnUpdated = false;
  787. this.microscopeController.SetColumnConditions(columnDictionary);
  788. int time1 = Environment.TickCount;
  789. int time2;
  790. while (!m_CollumnUpdated)
  791. {
  792. Application.DoEvents();
  793. time2 = Environment.TickCount;
  794. if (time2 - time1 > 10000)
  795. {
  796. currentCommand.returnType = false;
  797. return false;
  798. }
  799. }
  800. return true;
  801. }
  802. //亮度
  803. public float GetBrightness()
  804. {
  805. ReadMicroscopeColumn();
  806. return (float)m_dBirghtness;
  807. }
  808. public Boolean SetBrightness(float set)
  809. {
  810. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  811. {
  812. { Column.Brightness, (double)set }
  813. };
  814. m_CollumnUpdated = false;
  815. this.microscopeController.SetColumnConditions(columnDictionary);
  816. int time1 = Environment.TickCount;
  817. int time2;
  818. while (!m_CollumnUpdated)
  819. {
  820. Application.DoEvents();
  821. time2 = Environment.TickCount;
  822. if (time2 - time1 > 2000)
  823. {
  824. currentCommand.returnType = false;
  825. return false;
  826. }
  827. }
  828. return true;
  829. }
  830. //对比度
  831. public float GetContrast()
  832. {
  833. ReadMicroscopeColumn();
  834. return (float)m_dContrast;
  835. }
  836. public Boolean SetContrast(float set)
  837. {
  838. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  839. {
  840. { Column.Contrast, (double)set }
  841. };
  842. m_CollumnUpdated = false;
  843. this.microscopeController.SetColumnConditions(columnDictionary);
  844. int time1 = Environment.TickCount;
  845. int time2;
  846. while (!m_CollumnUpdated)
  847. {
  848. Application.DoEvents();
  849. time2 = Environment.TickCount;
  850. if (time2 - time1 > 2000)
  851. {
  852. currentCommand.returnType = false;
  853. return false;
  854. //break;
  855. }
  856. }
  857. return true;
  858. }
  859. //SEM电压
  860. public float GetSEMVoltage()
  861. {
  862. ReadMicroscopeColumn();
  863. return (float)m_dHighVoltage;
  864. }
  865. public Boolean SetSEMVoltage(float set)
  866. {
  867. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  868. {
  869. { Column.HighVoltage, (double)set }
  870. };
  871. m_CollumnUpdated = false;
  872. this.microscopeController.SetColumnConditions(columnDictionary);
  873. int time1 = Environment.TickCount;
  874. int time2;
  875. while (!m_CollumnUpdated)
  876. {
  877. Application.DoEvents();
  878. time2 = Environment.TickCount;
  879. if (time2 - time1 > 10000)
  880. {
  881. currentCommand.returnType = false;
  882. return false;
  883. //break;
  884. }
  885. }
  886. return true;
  887. }
  888. public bool SetSemScanExternal(bool b)
  889. {
  890. m_ExternalScanUpdated = false;
  891. if (!b)
  892. {
  893. EndMultipleAquisition();
  894. }
  895. this.microscopeController.SetExternalScan(b);
  896. int time1 = Environment.TickCount;
  897. int time2;
  898. while (!m_ExternalScanUpdated)
  899. {
  900. Application.DoEvents();
  901. time2 = Environment.TickCount;
  902. if (time2 - time1 > 2000)
  903. {
  904. currentCommand.returnType = false;
  905. return false;
  906. //break;
  907. }
  908. }
  909. return true;
  910. }
  911. //样品台
  912. public float[] GetStagePosition()
  913. {
  914. ReadStage();
  915. float[] ret = new float[5];
  916. ret[0] = (float)m_dStageX;
  917. ret[1] = (float)m_dStageY;
  918. ret[2] = (float)m_dStageZ;
  919. ret[3] = (float)m_dStageT;
  920. ret[4] = (float)m_dStageR;
  921. return ret;
  922. }
  923. public Boolean SetStagePosition(float[] set)
  924. {
  925. double stageX = (double)set[0];
  926. double stageY = (double)set[1];
  927. double stageZ = (double)set[2];
  928. double stageT = (double)set[3];
  929. double stageR = (double)set[4];
  930. var stageDictionary = new Dictionary<Stage, double>
  931. {
  932. { Stage.StageX, (double)stageX/1000.0 },
  933. { Stage.StageY, (double)stageY/1000.0 },
  934. { Stage.StageZ, (double)stageZ },
  935. { Stage.StageT, (double)stageT },
  936. { Stage.StageR, (double)stageR }
  937. };
  938. m_StageUpdated = false;
  939. this.microscopeController.SetStageConditions(stageDictionary);
  940. int time1 = Environment.TickCount;
  941. int time2;
  942. while (!m_StageUpdated)
  943. {
  944. Application.DoEvents();
  945. time2 = Environment.TickCount;
  946. if (time2 - time1 > 20000)
  947. {
  948. currentCommand.returnType = false;
  949. return false;
  950. //break;
  951. }
  952. }
  953. return true;
  954. }
  955. public float GetStageAtX()
  956. {
  957. ReadStage();
  958. return (float)m_dStageX;
  959. }
  960. public float GetStageAtY()
  961. {
  962. ReadStage();
  963. return (float)m_dStageY;
  964. }
  965. public float GetStageAtZ()
  966. {
  967. ReadStage();
  968. return (float)m_dStageZ;
  969. }
  970. public float GetStageAtT()
  971. {
  972. ReadStage();
  973. return (float)m_dStageT;
  974. }
  975. public float GetStageAtR()
  976. {
  977. ReadStage();
  978. return (float)m_dStageR;
  979. }
  980. public Boolean SetStageGotoX(float set)
  981. {
  982. double stageX = (double)set;
  983. var stageDictionary = new Dictionary<Stage, double>
  984. {
  985. { Stage.StageX, (double)stageX/1000.0 }
  986. };
  987. m_StageUpdated = false;
  988. this.microscopeController.SetStageConditions(stageDictionary);
  989. int time1 = Environment.TickCount;
  990. int time2;
  991. while (!m_StageUpdated)
  992. {
  993. Application.DoEvents();
  994. time2 = Environment.TickCount;
  995. if (time2 - time1 > 20000)
  996. {
  997. currentCommand.returnType = false;
  998. return false;
  999. //break;
  1000. }
  1001. }
  1002. return true;
  1003. }
  1004. public Boolean SetStageGotoY(float set)
  1005. {
  1006. double stageY = (double)set;
  1007. var stageDictionary = new Dictionary<Stage, double>
  1008. {
  1009. { Stage.StageY, (double)stageY/1000.0 }
  1010. };
  1011. m_StageUpdated = false;
  1012. this.microscopeController.SetStageConditions(stageDictionary);
  1013. int time1 = Environment.TickCount;
  1014. int time2;
  1015. while (!m_StageUpdated)
  1016. {
  1017. Application.DoEvents();
  1018. time2 = Environment.TickCount;
  1019. if (time2 - time1 > 20000)
  1020. {
  1021. currentCommand.returnType = false;
  1022. return false;
  1023. //break;
  1024. }
  1025. }
  1026. return true;
  1027. }
  1028. public Boolean SetStageGotoZ(float set)
  1029. {
  1030. double stageZ = (double)set;
  1031. var stageDictionary = new Dictionary<Stage, double>
  1032. {
  1033. { Stage.StageZ, (double)stageZ }
  1034. };
  1035. m_StageUpdated = false;
  1036. this.microscopeController.SetStageConditions(stageDictionary);
  1037. int time1 = Environment.TickCount;
  1038. int time2;
  1039. while (!m_StageUpdated)
  1040. {
  1041. Application.DoEvents();
  1042. time2 = Environment.TickCount;
  1043. if (time2 - time1 > 20000)
  1044. {
  1045. currentCommand.returnType = false;
  1046. return false;
  1047. //break;
  1048. }
  1049. }
  1050. return true;
  1051. }
  1052. public Boolean SetStageGotoT(float set)
  1053. {
  1054. double stageT = (double)set;
  1055. var stageDictionary = new Dictionary<Stage, double>
  1056. {
  1057. { Stage.StageT, (double)stageT }
  1058. };
  1059. m_StageUpdated = false;
  1060. this.microscopeController.SetStageConditions(stageDictionary);
  1061. int time1 = Environment.TickCount;
  1062. int time2;
  1063. while (!m_StageUpdated)
  1064. {
  1065. Application.DoEvents();
  1066. time2 = Environment.TickCount;
  1067. if (time2 - time1 > 20000)
  1068. {
  1069. currentCommand.returnType = false;
  1070. return false;
  1071. }
  1072. }
  1073. return true;
  1074. }
  1075. public Boolean SetStageGotoR(float set)
  1076. {
  1077. double stageR = (double)set;
  1078. var stageDictionary = new Dictionary<Stage, double>
  1079. {
  1080. { Stage.StageR, (double)stageR }
  1081. };
  1082. m_StageUpdated = false;
  1083. this.microscopeController.SetStageConditions(stageDictionary);
  1084. int time1 = Environment.TickCount;
  1085. int time2;
  1086. while (!m_StageUpdated)
  1087. {
  1088. Application.DoEvents();
  1089. time2 = Environment.TickCount;
  1090. if (time2 - time1 > 20000)
  1091. {
  1092. currentCommand.returnType = false;
  1093. return false;
  1094. //break;
  1095. }
  1096. }
  1097. return true;
  1098. }
  1099. #region 拍图
  1100. //图像扫描尺寸
  1101. public double[] ImageScanSize =
  1102. {
  1103. 32,
  1104. 64,
  1105. 128,
  1106. 256,
  1107. 512,
  1108. 704,
  1109. 768,
  1110. 1024,
  1111. 2048,
  1112. 4096,
  1113. 8192
  1114. };
  1115. public double GetDImagePixelsize()
  1116. {
  1117. return m_dImagePixelsize;
  1118. }
  1119. public void SetDImagePixelsize(double value)
  1120. {
  1121. m_dImagePixelsize = value;
  1122. }
  1123. void InitImageAcquisition()
  1124. {
  1125. imageAcquisitionController = AcquireFactory.CreateImageServer();
  1126. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  1127. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  1128. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  1129. }
  1130. void InitXrayAcquistion()
  1131. {
  1132. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  1133. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  1134. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  1135. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1136. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  1137. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  1138. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  1139. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  1140. }
  1141. //控制电镜释放
  1142. void CloseImageAcquisition()
  1143. {
  1144. if (imageAcquisitionController != null)
  1145. {
  1146. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  1147. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  1148. imageAcquisitionController = null;
  1149. }
  1150. }
  1151. void CloseXrayAcquistion()
  1152. {
  1153. if (EdSpectrumAcquisitionController != null)
  1154. {
  1155. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  1156. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  1157. EdSpectrumAcquisitionController = null;
  1158. }
  1159. }
  1160. /// <summary>
  1161. /// OnImageExperimentStarted
  1162. /// </summary>
  1163. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  1164. {
  1165. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1166. log.Info("拍图开始事件!");
  1167. }
  1168. //int m_nState;
  1169. /// <summary>
  1170. /// OnImageExperimentFinished
  1171. /// </summary>
  1172. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  1173. {
  1174. IElectronImage electronImage = e.Value[0];
  1175. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  1176. {
  1177. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  1178. }
  1179. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  1180. {
  1181. m_bAcquistionDone = true;
  1182. }
  1183. }
  1184. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1185. {
  1186. a_nImageHeight = 0;
  1187. a_nImageWidth = 0;
  1188. a_nPixelSize = 0;
  1189. a_pImageBits = null;
  1190. if (a_electronImage == null)
  1191. {
  1192. return false;
  1193. }
  1194. a_nImageHeight = a_electronImage.Height;
  1195. a_nImageWidth = a_electronImage.Width;
  1196. a_nPixelSize = a_electronImage.PixelSize;
  1197. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1198. long nImageSize = a_nImageHeight * a_nImageWidth;
  1199. long nBufferSize = nImageSize * nBytesPerPixel;
  1200. Byte[] imageData = new Byte[nBufferSize];
  1201. a_electronImage.GetData(imageData);
  1202. a_pImageBits = new Byte[nImageSize];
  1203. // default, oxford will return short image, we need to convert to byte
  1204. if (nBytesPerPixel == 2)
  1205. {
  1206. int nBSEValue = 0;
  1207. for (int i = 0; i < nImageSize; ++i)
  1208. {
  1209. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1210. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1211. }
  1212. }
  1213. else
  1214. {
  1215. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1216. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1217. int nOffset = nBytesPerPixel - 1;
  1218. for (int i = 0; i < nImageSize; ++i)
  1219. {
  1220. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1221. }
  1222. }
  1223. return true;
  1224. }
  1225. //a_dDwellTime : 1~100000之间的数
  1226. //a_sImageType : 1: SE, 2: Bse
  1227. //a_dImageScanSize : 图像分辨率,图像的高
  1228. public bool SetImageAcquistionSetting(double a_dDwellTime, ImageInputSourceType a_nImageType, double a_dImageScanSize)
  1229. {
  1230. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1231. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1232. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1233. if (a_dDwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1234. {
  1235. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1236. }
  1237. if (a_dDwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1238. {
  1239. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1240. }
  1241. else
  1242. {
  1243. imageSettings.DwellTimeMicroSeconds = a_dDwellTime;
  1244. }
  1245. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1246. imageSettings.EnableInputSource((ImageInputSources)a_nImageType, true);
  1247. if (!ImageScanSize.Contains(a_dImageScanSize))
  1248. {
  1249. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1250. return false;
  1251. }
  1252. var pixelSize = 1d / a_dImageScanSize;
  1253. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1254. return true;
  1255. }
  1256. #endregion
  1257. #region X-ray
  1258. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1259. {
  1260. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1261. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1262. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1263. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1264. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1265. }
  1266. /// <summary>
  1267. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1268. /// </summary>
  1269. /// <param name="sender">sender object</param>
  1270. /// <param name="e">The instance containing the event data.</param>
  1271. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1272. {
  1273. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1274. uint[] m_XrayData;
  1275. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1276. IEdSpectrum edSpectrum = e.Value;
  1277. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1278. {
  1279. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1280. }
  1281. long nXraycount = 0;
  1282. for (int i = 0; i < 2000; i++)
  1283. {
  1284. nXraycount += m_XrayData[i];
  1285. }
  1286. //Quantify processing
  1287. bool bquant=false;
  1288. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1289. {
  1290. bquant = currentCommand.pointXrayPrm.b_quant;
  1291. }
  1292. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1293. {
  1294. bquant = currentCommand.areaXrayPrm.b_quant;
  1295. }
  1296. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1297. {
  1298. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1299. bquant = curXrayData.b_quant;
  1300. }
  1301. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1302. {
  1303. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1304. bquant = curXrayData.b_quant;
  1305. }
  1306. var m_listElement = new Dictionary<string, double>();
  1307. if (bquant)
  1308. {
  1309. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1310. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1311. quantSettings.CombinedElement = 8;
  1312. quantSettings.Normalised = true;
  1313. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1314. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1315. var ie = Results.GetEnumerator();
  1316. while (ie.MoveNext())
  1317. {
  1318. ISEMQuantResult result = ie.Current;
  1319. if (result.WeightPercent != 0)
  1320. {
  1321. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1322. }
  1323. }
  1324. }
  1325. //------------------------
  1326. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1327. {
  1328. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1329. {
  1330. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1331. currentCommand.pointXrayPrm.listElement = m_listElement;
  1332. m_bXrayDone = true;
  1333. }
  1334. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1335. {
  1336. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1337. currentCommand.areaXrayPrm.listElement = m_listElement;
  1338. m_bXrayDone = true;
  1339. }
  1340. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1341. {
  1342. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1343. curXrayData.XrayData = m_XrayData;
  1344. curXrayData.listElement = m_listElement;
  1345. currentCommand.PointXrayDataReceived += 1;
  1346. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1347. {
  1348. m_bXrayDone = true;
  1349. }
  1350. }
  1351. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1352. {
  1353. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1354. curXrayData.XrayData = m_XrayData;
  1355. curXrayData.listElement = m_listElement;
  1356. currentCommand.AreaXrayDataReceived += 1;
  1357. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1358. {
  1359. m_bXrayDone = true;
  1360. }
  1361. }
  1362. }
  1363. }
  1364. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1365. {
  1366. a_pSpectrumData = new uint[a_nBufferSize];
  1367. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1368. a_spectrum.GetChannelData(xrayData);
  1369. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1370. int nChannelStart = 0;
  1371. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1372. {
  1373. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1374. }
  1375. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1376. double dStep1 = 1.0 / nDataLength;
  1377. double dStep2 = 1.0 / a_nBufferSize;
  1378. for (int i = 0; i < nDataLength; ++i)
  1379. {
  1380. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1381. double dBinPos = i * dStep1;
  1382. long nLeftBin = (long)(dBinPos / dStep2);
  1383. // calculate % into left bin
  1384. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1385. // calculate data into the left bin
  1386. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1387. // put data into bins
  1388. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1389. if ((nLeftBin + 1) < a_nBufferSize)
  1390. {
  1391. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1392. }
  1393. }
  1394. return true;
  1395. }
  1396. public bool IsAcquiringSpectrum()
  1397. {
  1398. return EdSpectrumAcquisitionController.IsAcquiring;
  1399. }
  1400. public void BeginMultipleAquisition()
  1401. {
  1402. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1403. }
  1404. public void EndMultipleAquisition()
  1405. {
  1406. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1407. }
  1408. public bool GetSemBeamOn()
  1409. {
  1410. var beamon = microscopeController.ColumnConditions.BeamOn;
  1411. if (beamon == 1)
  1412. {
  1413. return true;
  1414. }
  1415. else
  1416. {
  1417. return false;
  1418. }
  1419. }
  1420. public void StopXrayAquisition()
  1421. {
  1422. EdSpectrumAcquisitionController.StopAcquisition();
  1423. }
  1424. #endregion
  1425. }
  1426. }