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