ExtenderWrapper.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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. if (!b)
  884. {
  885. EndMultipleAquisition();
  886. }
  887. this.microscopeController.SetExternalScan(b);
  888. int time1 = Environment.TickCount;
  889. int time2;
  890. while (!m_ExternalScanUpdated)
  891. {
  892. Application.DoEvents();
  893. time2 = Environment.TickCount;
  894. if (time2 - time1 > 2000)
  895. {
  896. currentCommand.returnType = false;
  897. return false;
  898. //break;
  899. }
  900. }
  901. return true;
  902. }
  903. //样品台
  904. public float[] GetStagePosition()
  905. {
  906. ReadStage();
  907. float[] ret = new float[5];
  908. ret[0] = (float)m_dStageX;
  909. ret[1] = (float)m_dStageY;
  910. ret[2] = (float)m_dStageZ;
  911. ret[3] = (float)m_dStageT;
  912. ret[4] = (float)m_dStageR;
  913. return ret;
  914. }
  915. public Boolean SetStagePosition(float[] set)
  916. {
  917. double stageX = (double)set[0];
  918. double stageY = (double)set[1];
  919. double stageZ = (double)set[2];
  920. double stageT = (double)set[3];
  921. double stageR = (double)set[4];
  922. var stageDictionary = new Dictionary<Stage, double>
  923. {
  924. { Stage.StageX, (double)stageX/1000.0 },
  925. { Stage.StageY, (double)stageY/1000.0 },
  926. { Stage.StageZ, (double)stageZ },
  927. { Stage.StageT, (double)stageT },
  928. { Stage.StageR, (double)stageR }
  929. };
  930. m_StageUpdated = false;
  931. this.microscopeController.SetStageConditions(stageDictionary);
  932. int time1 = Environment.TickCount;
  933. int time2;
  934. while (!m_StageUpdated)
  935. {
  936. Application.DoEvents();
  937. time2 = Environment.TickCount;
  938. if (time2 - time1 > 20000)
  939. {
  940. currentCommand.returnType = false;
  941. return false;
  942. //break;
  943. }
  944. }
  945. return true;
  946. }
  947. public float GetStageAtX()
  948. {
  949. ReadStage();
  950. return (float)m_dStageX;
  951. }
  952. public float GetStageAtY()
  953. {
  954. ReadStage();
  955. return (float)m_dStageY;
  956. }
  957. public float GetStageAtZ()
  958. {
  959. ReadStage();
  960. return (float)m_dStageZ;
  961. }
  962. public float GetStageAtT()
  963. {
  964. ReadStage();
  965. return (float)m_dStageT;
  966. }
  967. public float GetStageAtR()
  968. {
  969. ReadStage();
  970. return (float)m_dStageR;
  971. }
  972. public Boolean SetStageGotoX(float set)
  973. {
  974. double stageX = (double)set;
  975. var stageDictionary = new Dictionary<Stage, double>
  976. {
  977. { Stage.StageX, (double)stageX/1000.0 }
  978. };
  979. m_StageUpdated = false;
  980. this.microscopeController.SetStageConditions(stageDictionary);
  981. int time1 = Environment.TickCount;
  982. int time2;
  983. while (!m_StageUpdated)
  984. {
  985. Application.DoEvents();
  986. time2 = Environment.TickCount;
  987. if (time2 - time1 > 20000)
  988. {
  989. currentCommand.returnType = false;
  990. return false;
  991. //break;
  992. }
  993. }
  994. return true;
  995. }
  996. public Boolean SetStageGotoY(float set)
  997. {
  998. double stageY = (double)set;
  999. var stageDictionary = new Dictionary<Stage, double>
  1000. {
  1001. { Stage.StageY, (double)stageY/1000.0 }
  1002. };
  1003. m_StageUpdated = false;
  1004. this.microscopeController.SetStageConditions(stageDictionary);
  1005. int time1 = Environment.TickCount;
  1006. int time2;
  1007. while (!m_StageUpdated)
  1008. {
  1009. Application.DoEvents();
  1010. time2 = Environment.TickCount;
  1011. if (time2 - time1 > 20000)
  1012. {
  1013. currentCommand.returnType = false;
  1014. return false;
  1015. //break;
  1016. }
  1017. }
  1018. return true;
  1019. }
  1020. public Boolean SetStageGotoZ(float set)
  1021. {
  1022. double stageZ = (double)set;
  1023. var stageDictionary = new Dictionary<Stage, double>
  1024. {
  1025. { Stage.StageZ, (double)stageZ }
  1026. };
  1027. m_StageUpdated = false;
  1028. this.microscopeController.SetStageConditions(stageDictionary);
  1029. int time1 = Environment.TickCount;
  1030. int time2;
  1031. while (!m_StageUpdated)
  1032. {
  1033. Application.DoEvents();
  1034. time2 = Environment.TickCount;
  1035. if (time2 - time1 > 20000)
  1036. {
  1037. currentCommand.returnType = false;
  1038. return false;
  1039. //break;
  1040. }
  1041. }
  1042. return true;
  1043. }
  1044. public Boolean SetStageGotoT(float set)
  1045. {
  1046. double stageT = (double)set;
  1047. var stageDictionary = new Dictionary<Stage, double>
  1048. {
  1049. { Stage.StageT, (double)stageT }
  1050. };
  1051. m_StageUpdated = false;
  1052. this.microscopeController.SetStageConditions(stageDictionary);
  1053. int time1 = Environment.TickCount;
  1054. int time2;
  1055. while (!m_StageUpdated)
  1056. {
  1057. Application.DoEvents();
  1058. time2 = Environment.TickCount;
  1059. if (time2 - time1 > 20000)
  1060. {
  1061. currentCommand.returnType = false;
  1062. return false;
  1063. }
  1064. }
  1065. return true;
  1066. }
  1067. public Boolean SetStageGotoR(float set)
  1068. {
  1069. double stageR = (double)set;
  1070. var stageDictionary = new Dictionary<Stage, double>
  1071. {
  1072. { Stage.StageR, (double)stageR }
  1073. };
  1074. m_StageUpdated = false;
  1075. this.microscopeController.SetStageConditions(stageDictionary);
  1076. int time1 = Environment.TickCount;
  1077. int time2;
  1078. while (!m_StageUpdated)
  1079. {
  1080. Application.DoEvents();
  1081. time2 = Environment.TickCount;
  1082. if (time2 - time1 > 20000)
  1083. {
  1084. currentCommand.returnType = false;
  1085. return false;
  1086. //break;
  1087. }
  1088. }
  1089. return true;
  1090. }
  1091. #region 拍图
  1092. //图像扫描尺寸
  1093. public double[] ImageScanSize =
  1094. {
  1095. 32,
  1096. 64,
  1097. 128,
  1098. 256,
  1099. 512,
  1100. 704,
  1101. 768,
  1102. 1024,
  1103. 2048,
  1104. 4096,
  1105. 8192
  1106. };
  1107. public double GetDImagePixelsize()
  1108. {
  1109. return m_dImagePixelsize;
  1110. }
  1111. public void SetDImagePixelsize(double value)
  1112. {
  1113. m_dImagePixelsize = value;
  1114. }
  1115. void InitImageAcquisition()
  1116. {
  1117. imageAcquisitionController = AcquireFactory.CreateImageServer();
  1118. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  1119. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  1120. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  1121. }
  1122. void InitXrayAcquistion()
  1123. {
  1124. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  1125. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  1126. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  1127. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1128. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  1129. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  1130. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  1131. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  1132. }
  1133. //控制电镜释放
  1134. void CloseImageAcquisition()
  1135. {
  1136. if (imageAcquisitionController != null)
  1137. {
  1138. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  1139. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  1140. imageAcquisitionController = null;
  1141. }
  1142. }
  1143. void CloseXrayAcquistion()
  1144. {
  1145. if (EdSpectrumAcquisitionController != null)
  1146. {
  1147. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  1148. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  1149. EdSpectrumAcquisitionController = null;
  1150. }
  1151. }
  1152. /// <summary>
  1153. /// OnImageExperimentStarted
  1154. /// </summary>
  1155. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  1156. {
  1157. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1158. log.Info("拍图开始事件!");
  1159. }
  1160. //int m_nState;
  1161. /// <summary>
  1162. /// OnImageExperimentFinished
  1163. /// </summary>
  1164. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  1165. {
  1166. IElectronImage electronImage = e.Value[0];
  1167. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  1168. {
  1169. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  1170. }
  1171. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  1172. {
  1173. m_bAcquistionDone = true;
  1174. }
  1175. }
  1176. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1177. {
  1178. a_nImageHeight = 0;
  1179. a_nImageWidth = 0;
  1180. a_nPixelSize = 0;
  1181. a_pImageBits = null;
  1182. if (a_electronImage == null)
  1183. {
  1184. return false;
  1185. }
  1186. a_nImageHeight = a_electronImage.Height;
  1187. a_nImageWidth = a_electronImage.Width;
  1188. a_nPixelSize = a_electronImage.PixelSize;
  1189. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1190. long nImageSize = a_nImageHeight * a_nImageWidth;
  1191. long nBufferSize = nImageSize * nBytesPerPixel;
  1192. Byte[] imageData = new Byte[nBufferSize];
  1193. a_electronImage.GetData(imageData);
  1194. a_pImageBits = new Byte[nImageSize];
  1195. // default, oxford will return short image, we need to convert to byte
  1196. if (nBytesPerPixel == 2)
  1197. {
  1198. int nBSEValue = 0;
  1199. for (int i = 0; i < nImageSize; ++i)
  1200. {
  1201. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1202. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1203. }
  1204. }
  1205. else
  1206. {
  1207. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1208. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1209. int nOffset = nBytesPerPixel - 1;
  1210. for (int i = 0; i < nImageSize; ++i)
  1211. {
  1212. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1213. }
  1214. }
  1215. return true;
  1216. }
  1217. //a_dDwellTime : 1~100000之间的数
  1218. //a_sImageType : 1: SE, 2: Bse
  1219. //a_dImageScanSize : 图像分辨率,图像的高
  1220. public bool SetImageAcquistionSetting(double a_dDwellTime, ImageInputSourceType a_nImageType, double a_dImageScanSize)
  1221. {
  1222. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1223. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1224. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1225. if (a_dDwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1226. {
  1227. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1228. }
  1229. if (a_dDwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1230. {
  1231. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1232. }
  1233. else
  1234. {
  1235. imageSettings.DwellTimeMicroSeconds = a_dDwellTime;
  1236. }
  1237. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1238. imageSettings.EnableInputSource((ImageInputSources)a_nImageType, true);
  1239. if (!ImageScanSize.Contains(a_dImageScanSize))
  1240. {
  1241. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1242. return false;
  1243. }
  1244. var pixelSize = 1d / a_dImageScanSize;
  1245. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1246. return true;
  1247. }
  1248. #endregion
  1249. #region X-ray
  1250. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1251. {
  1252. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1253. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1254. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1255. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1256. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1257. }
  1258. /// <summary>
  1259. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1260. /// </summary>
  1261. /// <param name="sender">sender object</param>
  1262. /// <param name="e">The instance containing the event data.</param>
  1263. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1264. {
  1265. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1266. uint[] m_XrayData;
  1267. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1268. IEdSpectrum edSpectrum = e.Value;
  1269. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1270. {
  1271. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1272. }
  1273. long nXraycount = 0;
  1274. for (int i = 0; i < 2000; i++)
  1275. {
  1276. nXraycount += m_XrayData[i];
  1277. }
  1278. //Quantify processing
  1279. bool bquant=false;
  1280. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1281. {
  1282. bquant = currentCommand.pointXrayPrm.b_quant;
  1283. }
  1284. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1285. {
  1286. bquant = currentCommand.areaXrayPrm.b_quant;
  1287. }
  1288. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1289. {
  1290. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1291. bquant = curXrayData.b_quant;
  1292. }
  1293. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1294. {
  1295. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1296. bquant = curXrayData.b_quant;
  1297. }
  1298. var m_listElement = new Dictionary<string, double>();
  1299. if (bquant)
  1300. {
  1301. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1302. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1303. quantSettings.CombinedElement = 8;
  1304. quantSettings.Normalised = true;
  1305. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1306. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1307. var ie = Results.GetEnumerator();
  1308. while (ie.MoveNext())
  1309. {
  1310. ISEMQuantResult result = ie.Current;
  1311. if (result.WeightPercent != 0)
  1312. {
  1313. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1314. }
  1315. }
  1316. }
  1317. //------------------------
  1318. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1319. {
  1320. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1321. {
  1322. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1323. currentCommand.pointXrayPrm.listElement = m_listElement;
  1324. m_bXrayDone = true;
  1325. }
  1326. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1327. {
  1328. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1329. currentCommand.areaXrayPrm.listElement = m_listElement;
  1330. m_bXrayDone = true;
  1331. }
  1332. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1333. {
  1334. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1335. curXrayData.XrayData = m_XrayData;
  1336. curXrayData.listElement = m_listElement;
  1337. currentCommand.PointXrayDataReceived += 1;
  1338. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1339. {
  1340. m_bXrayDone = true;
  1341. }
  1342. }
  1343. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1344. {
  1345. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1346. curXrayData.XrayData = m_XrayData;
  1347. curXrayData.listElement = m_listElement;
  1348. currentCommand.AreaXrayDataReceived += 1;
  1349. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1350. {
  1351. m_bXrayDone = true;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1357. {
  1358. a_pSpectrumData = new uint[a_nBufferSize];
  1359. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1360. a_spectrum.GetChannelData(xrayData);
  1361. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1362. int nChannelStart = 0;
  1363. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1364. {
  1365. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1366. }
  1367. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1368. double dStep1 = 1.0 / nDataLength;
  1369. double dStep2 = 1.0 / a_nBufferSize;
  1370. for (int i = 0; i < nDataLength; ++i)
  1371. {
  1372. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1373. double dBinPos = i * dStep1;
  1374. long nLeftBin = (long)(dBinPos / dStep2);
  1375. // calculate % into left bin
  1376. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1377. // calculate data into the left bin
  1378. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1379. // put data into bins
  1380. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1381. if ((nLeftBin + 1) < a_nBufferSize)
  1382. {
  1383. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1384. }
  1385. }
  1386. return true;
  1387. }
  1388. public bool IsAcquiringSpectrum()
  1389. {
  1390. return EdSpectrumAcquisitionController.IsAcquiring;
  1391. }
  1392. public void BeginMultipleAquisition()
  1393. {
  1394. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1395. }
  1396. public void EndMultipleAquisition()
  1397. {
  1398. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1399. }
  1400. public bool GetSemBeamOn()
  1401. {
  1402. var beamon = microscopeController.ColumnConditions.BeamOn;
  1403. if (beamon == 1)
  1404. {
  1405. return true;
  1406. }
  1407. else
  1408. {
  1409. return false;
  1410. }
  1411. }
  1412. public void StopXrayAquisition()
  1413. {
  1414. EdSpectrumAcquisitionController.StopAcquisition();
  1415. }
  1416. #endregion
  1417. }
  1418. }