ExtenderWrapper.cs 54 KB

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