ExtenderWrapper.cs 58 KB

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