ExtenderWrapper.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  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. ReadStage();
  825. m_StageUpdated = true;
  826. }
  827. }
  828. else if (e.Control == MicroscopeControlType.Column)
  829. {
  830. if (e.Success)
  831. {
  832. ReadMicroscopeColumn();
  833. m_CollumnUpdated = true;
  834. }
  835. }
  836. else if (e.Control == MicroscopeControlType.ExternalScan)
  837. {
  838. if (e.Success)
  839. {
  840. m_ExternalScanUpdated = true;
  841. }
  842. }
  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. 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. }