ExtenderWrapper.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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 = 256;
  236. private bool m_bXrayDone = false;
  237. //电压
  238. private double m_dHighVoltage;
  239. //放大倍数
  240. private double m_dMagnification;
  241. //工作距离
  242. private double m_dWorkingDistance;
  243. //亮度
  244. private double m_dBirghtness;
  245. //对比度
  246. private double m_dContrast;
  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. }
  516. int nLast = nSize % XrayQuantityLimitPerTime;
  517. if (nLast != 0)
  518. {
  519. singleGroup.Clear();
  520. for (int m = 0; m < nLast; m++)
  521. {
  522. singleGroup.Add(a_listPoints[m]);
  523. }
  524. if (!CollectXrayByPointsOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
  525. {
  526. return false;
  527. }
  528. }
  529. return true;
  530. }
  531. public bool CollectXrayByPointsOnHardwareLimit(ref List<PointXrayParam> a_listPoints, uint a_nXRayAQTime, bool a_bElementInfo)
  532. {
  533. if (a_listPoints.Count > XrayQuantityLimitPerTime)
  534. {
  535. return false;
  536. }
  537. currentCommand.XrayPrmForPoints = a_listPoints;
  538. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYPOINTS;
  539. //log.Info("线程:开始采集多点xray");
  540. var PointXrayDatas = currentCommand.XrayPrmForPoints;
  541. currentCommand.PointXrayDataReceived = 0;
  542. m_bXrayDone = false;
  543. var dMilliSecondsTime = PointXrayDatas[0].dMilliSecondsTime;
  544. SetXrayAcquisitionParam(dMilliSecondsTime);
  545. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  546. foreach (var prm in PointXrayDatas)
  547. {
  548. prm.b_quant = a_bElementInfo;
  549. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(prm.x * m_dImagePixelsize, prm.y * m_dImagePixelsize));
  550. try
  551. {
  552. m_bXrayDone = false;
  553. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  554. }
  555. catch (InvalidSettingsException invalidSettingsException)
  556. {
  557. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  558. invalidSettingsException.Message,
  559. invalidSettingsException.ValidationResults.ValidationErrors);
  560. log.Error(msg);
  561. }
  562. catch (AcquisitionStartException acquisitionStartException)
  563. {
  564. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  565. log.Error(msg);
  566. }
  567. }
  568. var time1 = Environment.TickCount;
  569. while (true)
  570. {
  571. if (m_bXrayDone)
  572. {
  573. currentCommand.returnType = true;
  574. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  575. break;
  576. }
  577. Application.DoEvents();
  578. var time2 = Environment.TickCount;
  579. if (time2 - time1 > EDSColletionTimeOut * PointXrayDatas.Count)
  580. {
  581. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  582. currentCommand.returnType = false;
  583. }
  584. }
  585. if (currentCommand.returnType == true)
  586. {
  587. return true;
  588. }
  589. else
  590. {
  591. return false;
  592. }
  593. }
  594. public bool CollectXrayByFeatures(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
  595. {
  596. int nSize = a_listFeatures.Count;
  597. List<AreaXrayParam> singleGroup = new List<AreaXrayParam>();
  598. int nTimes = nSize / XrayQuantityLimitPerTime;
  599. for (int i = 0; i < nTimes; i++)
  600. {
  601. singleGroup.Clear();
  602. for (int m = 0; m < XrayQuantityLimitPerTime; m++)
  603. {
  604. singleGroup.Add(a_listFeatures[i * XrayQuantityLimitPerTime + m]);
  605. }
  606. if (!CollectXrayByFeaturesOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
  607. {
  608. return false;
  609. }
  610. }
  611. int nLast = nSize % XrayQuantityLimitPerTime;
  612. if (nLast != 0)
  613. {
  614. singleGroup.Clear();
  615. for (int m = 0; m < nLast; m++)
  616. {
  617. singleGroup.Add(a_listFeatures[m]);
  618. }
  619. if (!CollectXrayByFeaturesOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
  620. {
  621. return false;
  622. }
  623. }
  624. return true;
  625. }
  626. public bool CollectXrayByFeaturesOnHardwareLimit(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
  627. {
  628. if (a_listFeatures.Count > XrayQuantityLimitPerTime)
  629. {
  630. return false;
  631. }
  632. currentCommand.XrayPrmForFeatures = a_listFeatures;
  633. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYFEATURES;
  634. var p = currentCommand.XrayPrmForFeatures;
  635. currentCommand.AreaXrayDataReceived = 0;
  636. m_bXrayDone = false;
  637. var dMilliSecondsTime = p[0].dMilliSecondsTime;
  638. SetXrayAcquisitionParam(p[0].dMilliSecondsTime);
  639. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  640. foreach (var prm in p)
  641. {
  642. prm.b_quant = a_bElementInfo;
  643. List<Chord> Chords = new List<Chord>();
  644. Chord chord;
  645. foreach (Segment seg in prm.a_listChord)
  646. {
  647. if (seg.Length >= 3)
  648. {
  649. chord = new Chord(seg.X + 1, seg.Y, seg.Length - 2);//get the middle part
  650. }
  651. else
  652. {
  653. chord = new Chord(seg.X, seg.Y, seg.Length);
  654. }
  655. Chords.Add(chord);
  656. }
  657. var chordsList = new ChordList(Chords, m_dImagePixelsize);
  658. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  659. try
  660. {
  661. m_bXrayDone = false;
  662. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  663. }
  664. catch (InvalidSettingsException invalidSettingsException)
  665. {
  666. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  667. invalidSettingsException.Message,
  668. invalidSettingsException.ValidationResults.ValidationErrors);
  669. log.Error(msg);
  670. }
  671. catch (AcquisitionStartException acquisitionStartException)
  672. {
  673. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  674. log.Error(msg);
  675. }
  676. }
  677. var time1 = Environment.TickCount;
  678. while (true)
  679. {
  680. if (m_bXrayDone)
  681. {
  682. currentCommand.returnType = true;
  683. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  684. break;
  685. }
  686. Application.DoEvents();
  687. var time2 = Environment.TickCount;
  688. if (time2 - time1 > EDSColletionTimeOut * p.Count)
  689. {
  690. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  691. currentCommand.returnType = false;
  692. break;
  693. }
  694. }
  695. if (currentCommand.returnType == true)
  696. {
  697. a_listFeatures = currentCommand.XrayPrmForFeatures;
  698. return true;
  699. }
  700. else
  701. {
  702. return false;
  703. }
  704. }
  705. //控制电镜初始化
  706. void InitMicroscopeController()
  707. {
  708. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  709. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  710. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  711. this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
  712. this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
  713. this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
  714. //ReadMicroscopeColumn();
  715. //ReadStage();
  716. }
  717. //控制电镜释放
  718. void CloseMicroscopeController()
  719. {
  720. if (microscopeController != null)
  721. {
  722. this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
  723. this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
  724. this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
  725. this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
  726. this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
  727. microscopeController = null;
  728. }
  729. }
  730. //读取当前的电镜控制值
  731. private void ReadMicroscopeColumn()
  732. {
  733. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  734. var columnConditions = this.microscopeController.ColumnConditions;
  735. if (columnCapabilities.Magnification.CanRead)
  736. {
  737. m_dMagnification = columnConditions.Magnification;
  738. }
  739. if (columnCapabilities.WorkingDistance.CanRead)
  740. {
  741. m_dWorkingDistance = columnConditions.WorkingDistance;
  742. }
  743. if (columnCapabilities.HighVoltage.CanRead)
  744. {
  745. m_dHighVoltage = columnConditions.HighVoltage;
  746. }
  747. if (columnCapabilities.Brightness.CanRead)
  748. {
  749. m_dBirghtness = columnConditions.Brightness;
  750. }
  751. if (columnCapabilities.Contrast.CanRead)
  752. {
  753. m_dContrast = columnConditions.Contrast;
  754. }
  755. if (columnCapabilities.BeamOn.CanRead)
  756. {
  757. m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
  758. }
  759. if (columnCapabilities.FilamentOn.CanRead)
  760. {
  761. m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
  762. }
  763. }
  764. //读取样品台位置
  765. private void ReadStage()
  766. {
  767. var stageCapabilities = this.microscopeController.StageCapabilities;
  768. var stageConditions = this.microscopeController.StageConditions;
  769. while (!stageCapabilities.StageX.CanRead)
  770. {
  771. Thread.Sleep(100);
  772. }
  773. if (stageCapabilities.StageX.CanRead)
  774. {
  775. this.m_dStageX = stageConditions.StageX * 1000.0;
  776. }
  777. if (stageCapabilities.StageY.CanRead)
  778. {
  779. this.m_dStageY = stageConditions.StageY * 1000.0;
  780. }
  781. if (stageCapabilities.StageZ.CanRead)
  782. {
  783. this.m_dStageZ = stageConditions.StageZ;
  784. }
  785. if (stageCapabilities.StageT.CanRead)
  786. {
  787. this.m_dStageT = stageConditions.StageT;
  788. }
  789. if (stageCapabilities.StageR.CanRead)
  790. {
  791. this.m_dStageR = stageConditions.StageR;
  792. }
  793. }
  794. //电镜控制改变事件
  795. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  796. {
  797. ReadMicroscopeColumn();
  798. }
  799. //样品台控制改变事件
  800. private void OnMicroscopeStageChange(object sender, EventArgs e)
  801. {
  802. ReadStage();
  803. }
  804. //镜筒控制连接或断开时的事件
  805. private void OnMicroscopeColumnConnected(object sender, EventArgs e)
  806. {
  807. ReadMicroscopeColumn();
  808. }
  809. //样品台控制连接或断开时的事件
  810. private void OnMicroscopeStageConnected(object sender, EventArgs e)
  811. {
  812. ReadStage();
  813. }
  814. //样品台控制、电镜控制、外围控制的事件改变完成
  815. private void OnMicroscopeChangeCompleted(object sender, CompletedEventArgs e)
  816. {
  817. if (e.Control == MicroscopeControlType.Stage)
  818. {
  819. if (e.Success)
  820. {
  821. m_StageUpdated = true;
  822. }
  823. }
  824. else if (e.Control == MicroscopeControlType.Column)
  825. {
  826. if (e.Success)
  827. {
  828. m_CollumnUpdated = true;
  829. }
  830. }
  831. else if (e.Control == MicroscopeControlType.ExternalScan)
  832. {
  833. if (e.Success)
  834. {
  835. m_ExternalScanUpdated = true;
  836. }
  837. }
  838. ReadMicroscopeColumn();
  839. ReadStage();
  840. }
  841. public float GetMagnification()
  842. {
  843. ReadMicroscopeColumn();
  844. return (float)m_dMagnification;
  845. }
  846. public Boolean SetMagnification(float set)
  847. {
  848. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  849. {
  850. { Column.Magnification, (double)set }
  851. };
  852. m_CollumnUpdated = false;
  853. this.microscopeController.SetColumnConditions(columnDictionary);
  854. int time1 = Environment.TickCount;
  855. int time2;
  856. while (!m_CollumnUpdated)
  857. {
  858. Application.DoEvents();
  859. time2 = Environment.TickCount;
  860. if (time2 - time1 > 10000)
  861. {
  862. currentCommand.returnType = false;
  863. return false;
  864. }
  865. }
  866. return true;
  867. }
  868. //焦距
  869. public float GetWorkingDistance()
  870. {
  871. ReadMicroscopeColumn();
  872. return (float)m_dWorkingDistance;
  873. }
  874. public Boolean SetWorkingDistance(float set)
  875. {
  876. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  877. {
  878. { Column.WorkingDistance, (double)set }
  879. };
  880. m_CollumnUpdated = false;
  881. this.microscopeController.SetColumnConditions(columnDictionary);
  882. int time1 = Environment.TickCount;
  883. int time2;
  884. while (!m_CollumnUpdated)
  885. {
  886. Application.DoEvents();
  887. time2 = Environment.TickCount;
  888. if (time2 - time1 > 10000)
  889. {
  890. currentCommand.returnType = false;
  891. return false;
  892. }
  893. }
  894. return true;
  895. }
  896. //亮度
  897. public float GetBrightness()
  898. {
  899. ReadMicroscopeColumn();
  900. return (float)m_dBirghtness;
  901. }
  902. public Boolean SetBrightness(float set)
  903. {
  904. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  905. {
  906. { Column.Brightness, (double)set }
  907. };
  908. m_CollumnUpdated = false;
  909. this.microscopeController.SetColumnConditions(columnDictionary);
  910. int time1 = Environment.TickCount;
  911. int time2;
  912. while (!m_CollumnUpdated)
  913. {
  914. Application.DoEvents();
  915. time2 = Environment.TickCount;
  916. if (time2 - time1 > 2000)
  917. {
  918. currentCommand.returnType = false;
  919. return false;
  920. }
  921. }
  922. return true;
  923. }
  924. //对比度
  925. public float GetContrast()
  926. {
  927. ReadMicroscopeColumn();
  928. return (float)m_dContrast;
  929. }
  930. public Boolean SetContrast(float set)
  931. {
  932. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  933. {
  934. { Column.Contrast, (double)set }
  935. };
  936. m_CollumnUpdated = false;
  937. this.microscopeController.SetColumnConditions(columnDictionary);
  938. int time1 = Environment.TickCount;
  939. int time2;
  940. while (!m_CollumnUpdated)
  941. {
  942. Application.DoEvents();
  943. time2 = Environment.TickCount;
  944. if (time2 - time1 > 2000)
  945. {
  946. currentCommand.returnType = false;
  947. return false;
  948. //break;
  949. }
  950. }
  951. return true;
  952. }
  953. //SEM电压
  954. public float GetSEMVoltage()
  955. {
  956. ReadMicroscopeColumn();
  957. return (float)m_dHighVoltage;
  958. }
  959. public Boolean SetSEMVoltage(float set)
  960. {
  961. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  962. {
  963. { Column.HighVoltage, (double)set }
  964. };
  965. m_CollumnUpdated = false;
  966. this.microscopeController.SetColumnConditions(columnDictionary);
  967. int time1 = Environment.TickCount;
  968. int time2;
  969. while (!m_CollumnUpdated)
  970. {
  971. Application.DoEvents();
  972. time2 = Environment.TickCount;
  973. if (time2 - time1 > 10000)
  974. {
  975. currentCommand.returnType = false;
  976. return false;
  977. //break;
  978. }
  979. }
  980. return true;
  981. }
  982. public bool SetSemScanExternal(bool b)
  983. {
  984. m_ExternalScanUpdated = false;
  985. if (!b)
  986. {
  987. EndMultipleAquisition();
  988. }
  989. this.microscopeController.SetExternalScan(b);
  990. int time1 = Environment.TickCount;
  991. int time2;
  992. while (!m_ExternalScanUpdated)
  993. {
  994. Application.DoEvents();
  995. time2 = Environment.TickCount;
  996. if (time2 - time1 > 2000)
  997. {
  998. currentCommand.returnType = false;
  999. return false;
  1000. //break;
  1001. }
  1002. }
  1003. return true;
  1004. }
  1005. //样品台
  1006. public float[] GetStagePosition()
  1007. {
  1008. ReadStage();
  1009. float[] ret = new float[5];
  1010. ret[0] = (float)m_dStageX;
  1011. ret[1] = (float)m_dStageY;
  1012. ret[2] = (float)m_dStageZ;
  1013. ret[3] = (float)m_dStageT;
  1014. ret[4] = (float)m_dStageR;
  1015. return ret;
  1016. }
  1017. public Boolean SetStagePosition(float[] set)
  1018. {
  1019. double stageX = (double)set[0];
  1020. double stageY = (double)set[1];
  1021. double stageZ = (double)set[2];
  1022. double stageT = (double)set[3];
  1023. double stageR = (double)set[4];
  1024. var stageDictionary = new Dictionary<Stage, double>
  1025. {
  1026. { Stage.StageX, (double)stageX/1000.0 },
  1027. { Stage.StageY, (double)stageY/1000.0 },
  1028. { Stage.StageZ, (double)stageZ },
  1029. { Stage.StageT, (double)stageT },
  1030. { Stage.StageR, (double)stageR }
  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 float GetStageAtX()
  1050. {
  1051. ReadStage();
  1052. return (float)m_dStageX;
  1053. }
  1054. public float GetStageAtY()
  1055. {
  1056. ReadStage();
  1057. return (float)m_dStageY;
  1058. }
  1059. public float GetStageAtZ()
  1060. {
  1061. ReadStage();
  1062. return (float)m_dStageZ;
  1063. }
  1064. public float GetStageAtT()
  1065. {
  1066. ReadStage();
  1067. return (float)m_dStageT;
  1068. }
  1069. public float GetStageAtR()
  1070. {
  1071. ReadStage();
  1072. return (float)m_dStageR;
  1073. }
  1074. public Boolean SetStageGotoX(float set)
  1075. {
  1076. double stageX = (double)set;
  1077. var stageDictionary = new Dictionary<Stage, double>
  1078. {
  1079. { Stage.StageX, (double)stageX/1000.0 }
  1080. };
  1081. m_StageUpdated = false;
  1082. this.microscopeController.SetStageConditions(stageDictionary);
  1083. int time1 = Environment.TickCount;
  1084. int time2;
  1085. while (!m_StageUpdated)
  1086. {
  1087. Application.DoEvents();
  1088. time2 = Environment.TickCount;
  1089. if (time2 - time1 > 20000)
  1090. {
  1091. currentCommand.returnType = false;
  1092. return false;
  1093. //break;
  1094. }
  1095. }
  1096. return true;
  1097. }
  1098. public Boolean SetStageGotoY(float set)
  1099. {
  1100. double stageY = (double)set;
  1101. var stageDictionary = new Dictionary<Stage, double>
  1102. {
  1103. { Stage.StageY, (double)stageY/1000.0 }
  1104. };
  1105. m_StageUpdated = false;
  1106. this.microscopeController.SetStageConditions(stageDictionary);
  1107. int time1 = Environment.TickCount;
  1108. int time2;
  1109. while (!m_StageUpdated)
  1110. {
  1111. Application.DoEvents();
  1112. time2 = Environment.TickCount;
  1113. if (time2 - time1 > 20000)
  1114. {
  1115. currentCommand.returnType = false;
  1116. return false;
  1117. //break;
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. public Boolean SetStageGotoZ(float set)
  1123. {
  1124. double stageZ = (double)set;
  1125. var stageDictionary = new Dictionary<Stage, double>
  1126. {
  1127. { Stage.StageZ, (double)stageZ }
  1128. };
  1129. m_StageUpdated = false;
  1130. this.microscopeController.SetStageConditions(stageDictionary);
  1131. int time1 = Environment.TickCount;
  1132. int time2;
  1133. while (!m_StageUpdated)
  1134. {
  1135. Application.DoEvents();
  1136. time2 = Environment.TickCount;
  1137. if (time2 - time1 > 20000)
  1138. {
  1139. currentCommand.returnType = false;
  1140. return false;
  1141. //break;
  1142. }
  1143. }
  1144. return true;
  1145. }
  1146. public Boolean SetStageGotoT(float set)
  1147. {
  1148. double stageT = (double)set;
  1149. var stageDictionary = new Dictionary<Stage, double>
  1150. {
  1151. { Stage.StageT, (double)stageT }
  1152. };
  1153. m_StageUpdated = false;
  1154. this.microscopeController.SetStageConditions(stageDictionary);
  1155. int time1 = Environment.TickCount;
  1156. int time2;
  1157. while (!m_StageUpdated)
  1158. {
  1159. Application.DoEvents();
  1160. time2 = Environment.TickCount;
  1161. if (time2 - time1 > 20000)
  1162. {
  1163. currentCommand.returnType = false;
  1164. return false;
  1165. }
  1166. }
  1167. return true;
  1168. }
  1169. public Boolean SetStageGotoR(float set)
  1170. {
  1171. double stageR = (double)set;
  1172. var stageDictionary = new Dictionary<Stage, double>
  1173. {
  1174. { Stage.StageR, (double)stageR }
  1175. };
  1176. m_StageUpdated = false;
  1177. this.microscopeController.SetStageConditions(stageDictionary);
  1178. int time1 = Environment.TickCount;
  1179. int time2;
  1180. while (!m_StageUpdated)
  1181. {
  1182. Application.DoEvents();
  1183. time2 = Environment.TickCount;
  1184. if (time2 - time1 > 20000)
  1185. {
  1186. currentCommand.returnType = false;
  1187. return false;
  1188. //break;
  1189. }
  1190. }
  1191. return true;
  1192. }
  1193. #region 拍图
  1194. //图像扫描尺寸
  1195. public double[] ImageScanSize =
  1196. {
  1197. 32,
  1198. 64,
  1199. 128,
  1200. 256,
  1201. 512,
  1202. 704,
  1203. 768,
  1204. 1408,
  1205. 1024,
  1206. 1536,
  1207. 2048,
  1208. 3072,
  1209. 4096,
  1210. 8192
  1211. };
  1212. public double GetDImagePixelsize()
  1213. {
  1214. return m_dImagePixelsize;
  1215. }
  1216. public void SetDImagePixelsize(double value)
  1217. {
  1218. m_dImagePixelsize = value;
  1219. }
  1220. void InitImageAcquisition()
  1221. {
  1222. imageAcquisitionController = AcquireFactory.CreateImageServer();
  1223. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  1224. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  1225. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  1226. }
  1227. void InitXrayAcquistion()
  1228. {
  1229. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  1230. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  1231. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  1232. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1233. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  1234. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  1235. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  1236. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  1237. }
  1238. //控制电镜释放
  1239. void CloseImageAcquisition()
  1240. {
  1241. if (imageAcquisitionController != null)
  1242. {
  1243. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  1244. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  1245. imageAcquisitionController = null;
  1246. }
  1247. }
  1248. void CloseXrayAcquistion()
  1249. {
  1250. if (EdSpectrumAcquisitionController != null)
  1251. {
  1252. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  1253. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  1254. EdSpectrumAcquisitionController = null;
  1255. }
  1256. }
  1257. /// <summary>
  1258. /// OnImageExperimentStarted
  1259. /// </summary>
  1260. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  1261. {
  1262. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1263. log.Info("拍图开始事件!");
  1264. }
  1265. //int m_nState;
  1266. /// <summary>
  1267. /// OnImageExperimentFinished
  1268. /// </summary>
  1269. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  1270. {
  1271. IElectronImage electronImage = e.Value[0];
  1272. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  1273. {
  1274. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  1275. }
  1276. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  1277. {
  1278. m_bAcquistionDone = true;
  1279. }
  1280. }
  1281. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1282. {
  1283. a_nImageHeight = 0;
  1284. a_nImageWidth = 0;
  1285. a_nPixelSize = 0;
  1286. a_pImageBits = null;
  1287. if (a_electronImage == null)
  1288. {
  1289. return false;
  1290. }
  1291. a_nImageHeight = a_electronImage.Height;
  1292. a_nImageWidth = a_electronImage.Width;
  1293. a_nPixelSize = a_electronImage.PixelSize;
  1294. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1295. long nImageSize = a_nImageHeight * a_nImageWidth;
  1296. long nBufferSize = nImageSize * nBytesPerPixel;
  1297. Byte[] imageData = new Byte[nBufferSize];
  1298. a_electronImage.GetData(imageData);
  1299. a_pImageBits = new Byte[nImageSize];
  1300. // default, oxford will return short image, we need to convert to byte
  1301. if (nBytesPerPixel == 2)
  1302. {
  1303. int nBSEValue = 0;
  1304. for (int i = 0; i < nImageSize; ++i)
  1305. {
  1306. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1307. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1308. }
  1309. }
  1310. else
  1311. {
  1312. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1313. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1314. int nOffset = nBytesPerPixel - 1;
  1315. for (int i = 0; i < nImageSize; ++i)
  1316. {
  1317. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1318. }
  1319. }
  1320. return true;
  1321. }
  1322. //a_dDwellTime : 1~100000之间的数
  1323. //a_sImageType : 1: SE, 2: Bse
  1324. public bool SetImageAcquistionSetting(ImageAquistionParam p)
  1325. {
  1326. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1327. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1328. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1329. if (p.DwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1330. {
  1331. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1332. }
  1333. if (p.DwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1334. {
  1335. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1336. }
  1337. else
  1338. {
  1339. imageSettings.DwellTimeMicroSeconds = p.DwellTime;
  1340. }
  1341. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1342. imageSettings.EnableInputSource((ImageInputSources)p.sourceType, true);
  1343. if (!ImageScanSize.Contains(p.width))
  1344. {
  1345. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1346. return false;
  1347. }
  1348. var pixelSize = 1d / p.width;
  1349. if (p.regionType == ImageAquistionParam.ImageRegionType.FullField)
  1350. {
  1351. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1352. }
  1353. else
  1354. {
  1355. scanSettings.AcquisitionRegion.CreateRectangleRegion(p.ImgRectRegion, pixelSize);
  1356. }
  1357. return true;
  1358. }
  1359. #endregion
  1360. #region X-ray
  1361. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1362. {
  1363. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1364. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1365. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1366. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1367. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1368. }
  1369. private bool GetAtomicNumberByEleName(string name, out int atomicNum)
  1370. {
  1371. var allelenames = theElementNameList.Split(',');
  1372. for (int i = 0; i < allelenames.Length; i++)
  1373. {
  1374. if (allelenames[i] == name)
  1375. {
  1376. atomicNum = i + 1;
  1377. return true;
  1378. }
  1379. }
  1380. atomicNum = 0;
  1381. return false;
  1382. }
  1383. /// <summary>
  1384. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1385. /// </summary>
  1386. /// <param name="sender">sender object</param>
  1387. /// <param name="e">The instance containing the event data.</param>
  1388. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1389. {
  1390. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1391. uint[] m_XrayData;
  1392. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1393. IEdSpectrum edSpectrum = e.Value;
  1394. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1395. {
  1396. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1397. }
  1398. long nXraycount = 0;
  1399. for (int i = 0; i < 2000; i++)
  1400. {
  1401. nXraycount += m_XrayData[i];
  1402. }
  1403. //Quantify processing
  1404. bool bquant = false;
  1405. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1406. {
  1407. bquant = currentCommand.pointXrayPrm.b_quant;
  1408. }
  1409. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1410. {
  1411. bquant = currentCommand.areaXrayPrm.b_quant;
  1412. }
  1413. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1414. {
  1415. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1416. bquant = curXrayData.b_quant;
  1417. }
  1418. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1419. {
  1420. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1421. bquant = curXrayData.b_quant;
  1422. }
  1423. var m_listElement = new Dictionary<string, double>();
  1424. if (bquant)
  1425. {
  1426. if (m_ifautoid == false)
  1427. {
  1428. log.Warn(autoIdSettings.KnownElements);
  1429. autoIdSettings.ClearKnownElements();
  1430. //m_knownelements = "K,Ca,Sc,Ti,V,Mn,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr";
  1431. var knownelenames = m_knownelements.Split(',');
  1432. foreach (var ele in knownelenames)
  1433. {
  1434. int atomicnum;
  1435. if (GetAtomicNumberByEleName(ele, out atomicnum))
  1436. {
  1437. autoIdSettings.SetKnownElement(atomicnum, true);
  1438. }
  1439. }
  1440. log.Warn(autoIdSettings.KnownElements);
  1441. }
  1442. else
  1443. {
  1444. }
  1445. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1446. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1447. quantSettings.CombinedElement = 8;
  1448. quantSettings.Normalised = true;
  1449. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1450. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1451. var ie = Results.GetEnumerator();
  1452. while (ie.MoveNext())
  1453. {
  1454. ISEMQuantResult result = ie.Current;
  1455. if (result.WeightPercent != 0)
  1456. {
  1457. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1458. }
  1459. }
  1460. }
  1461. //------------------------
  1462. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1463. {
  1464. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1465. {
  1466. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1467. currentCommand.pointXrayPrm.listElement = m_listElement;
  1468. m_bXrayDone = true;
  1469. }
  1470. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1471. {
  1472. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1473. currentCommand.areaXrayPrm.listElement = m_listElement;
  1474. m_bXrayDone = true;
  1475. }
  1476. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1477. {
  1478. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1479. curXrayData.XrayData = m_XrayData;
  1480. curXrayData.listElement = m_listElement;
  1481. currentCommand.PointXrayDataReceived += 1;
  1482. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1483. {
  1484. m_bXrayDone = true;
  1485. }
  1486. }
  1487. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1488. {
  1489. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1490. curXrayData.XrayData = m_XrayData;
  1491. curXrayData.listElement = m_listElement;
  1492. currentCommand.AreaXrayDataReceived += 1;
  1493. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1494. {
  1495. m_bXrayDone = true;
  1496. }
  1497. }
  1498. }
  1499. }
  1500. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1501. {
  1502. a_pSpectrumData = new uint[a_nBufferSize];
  1503. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1504. a_spectrum.GetChannelData(xrayData);
  1505. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1506. int nChannelStart = 0;
  1507. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1508. {
  1509. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1510. }
  1511. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1512. double dStep1 = 1.0 / nDataLength;
  1513. double dStep2 = 1.0 / a_nBufferSize;
  1514. for (int i = 0; i < nDataLength; ++i)
  1515. {
  1516. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1517. double dBinPos = i * dStep1;
  1518. long nLeftBin = (long)(dBinPos / dStep2);
  1519. // calculate % into left bin
  1520. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1521. // calculate data into the left bin
  1522. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1523. // put data into bins
  1524. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1525. if ((nLeftBin + 1) < a_nBufferSize)
  1526. {
  1527. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1528. }
  1529. }
  1530. return true;
  1531. }
  1532. public bool IsAcquiringSpectrum()
  1533. {
  1534. return EdSpectrumAcquisitionController.IsAcquiring;
  1535. }
  1536. public void BeginMultipleAquisition()
  1537. {
  1538. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1539. }
  1540. public void EndMultipleAquisition()
  1541. {
  1542. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1543. }
  1544. public bool GetSemBeamOn()
  1545. {
  1546. var beamon = microscopeController.ColumnConditions.BeamOn;
  1547. if (beamon == 1)
  1548. {
  1549. return true;
  1550. }
  1551. else
  1552. {
  1553. return false;
  1554. }
  1555. }
  1556. public bool SetSemBeamOnOrOff(bool val)
  1557. {
  1558. double beamon;
  1559. if (val)
  1560. {
  1561. beamon = 1;
  1562. }
  1563. else
  1564. {
  1565. beamon = 0;
  1566. }
  1567. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1568. {
  1569. { Column.BeamOn, (double)beamon }
  1570. };
  1571. m_CollumnUpdated = false;
  1572. this.microscopeController.SetColumnConditions(columnDictionary);
  1573. int time1 = Environment.TickCount;
  1574. int time2;
  1575. while (!m_CollumnUpdated)
  1576. {
  1577. Application.DoEvents();
  1578. time2 = Environment.TickCount;
  1579. if (time2 - time1 > 10000)
  1580. {
  1581. currentCommand.returnType = false;
  1582. return false;
  1583. }
  1584. }
  1585. return true;
  1586. }
  1587. public bool SetSemFilamentOnOrOff(bool val)
  1588. {
  1589. double beamon;
  1590. if (val)
  1591. {
  1592. beamon = 1;
  1593. }
  1594. else
  1595. {
  1596. beamon = 0;
  1597. }
  1598. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1599. {
  1600. { Column.FilamentOn, (double)beamon }
  1601. };
  1602. m_CollumnUpdated = false;
  1603. this.microscopeController.SetColumnConditions(columnDictionary);
  1604. int time1 = Environment.TickCount;
  1605. int time2;
  1606. while (!m_CollumnUpdated)
  1607. {
  1608. Application.DoEvents();
  1609. time2 = Environment.TickCount;
  1610. if (time2 - time1 > 10000)
  1611. {
  1612. currentCommand.returnType = false;
  1613. return false;
  1614. }
  1615. }
  1616. return true;
  1617. }
  1618. public void StopXrayAquisition()
  1619. {
  1620. EdSpectrumAcquisitionController.StopAcquisition();
  1621. }
  1622. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  1623. {
  1624. m_ifautoid = IfAutoId;
  1625. m_knownelements = knownElements;
  1626. }
  1627. #endregion
  1628. }
  1629. }