ExtenderWrapper.cs 61 KB

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