ExtenderWrapper.cs 53 KB

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