ExtenderWrapper.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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=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. }
  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. bool ifMagRead=false;
  842. public float GetMagnification()
  843. {
  844. ReadMicroscopeColumn();
  845. if (m_dMagnification != 0)
  846. {
  847. ifMagRead=true;
  848. }
  849. return (float)m_dMagnification;
  850. }
  851. public Boolean SetMagnification(float set)
  852. {
  853. if (!ifMagRead)
  854. {
  855. return false;
  856. }
  857. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  858. {
  859. { Column.Magnification, (double)set }
  860. };
  861. m_CollumnUpdated = false;
  862. this.microscopeController.SetColumnConditions(columnDictionary);
  863. int time1 = Environment.TickCount;
  864. int time2;
  865. while (!m_CollumnUpdated)
  866. {
  867. Application.DoEvents();
  868. time2 = Environment.TickCount;
  869. if (time2 - time1 > 10000)
  870. {
  871. currentCommand.returnType = false;
  872. return false;
  873. }
  874. }
  875. return true;
  876. }
  877. bool ifwdread = false;
  878. //焦距
  879. public float GetWorkingDistance()
  880. {
  881. ReadMicroscopeColumn();
  882. if (m_dWorkingDistance != 0)
  883. {
  884. bool ifwdread = true;
  885. }
  886. return (float)m_dWorkingDistance;
  887. }
  888. public Boolean SetWorkingDistance(float set)
  889. {
  890. if (!ifwdread)
  891. {
  892. return false;
  893. }
  894. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  895. {
  896. { Column.WorkingDistance, (double)set }
  897. };
  898. m_CollumnUpdated = false;
  899. this.microscopeController.SetColumnConditions(columnDictionary);
  900. int time1 = Environment.TickCount;
  901. int time2;
  902. while (!m_CollumnUpdated)
  903. {
  904. Application.DoEvents();
  905. time2 = Environment.TickCount;
  906. if (time2 - time1 > 10000)
  907. {
  908. currentCommand.returnType = false;
  909. return false;
  910. }
  911. }
  912. return true;
  913. }
  914. bool ifBrightnessRead = false;
  915. //亮度
  916. public float GetBrightness()
  917. {
  918. ReadMicroscopeColumn();
  919. if (m_dBirghtness != 0)
  920. {
  921. ifBrightnessRead = true;
  922. }
  923. return (float)m_dBirghtness;
  924. }
  925. public Boolean SetBrightness(float set)
  926. {
  927. if (!ifBrightnessRead)
  928. {
  929. return false;
  930. }
  931. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  932. {
  933. { Column.Brightness, (double)set }
  934. };
  935. m_CollumnUpdated = false;
  936. this.microscopeController.SetColumnConditions(columnDictionary);
  937. int time1 = Environment.TickCount;
  938. int time2;
  939. while (!m_CollumnUpdated)
  940. {
  941. Application.DoEvents();
  942. time2 = Environment.TickCount;
  943. if (time2 - time1 > 2000)
  944. {
  945. currentCommand.returnType = false;
  946. return false;
  947. }
  948. }
  949. return true;
  950. }
  951. bool ifContrastRead = false;
  952. //对比度
  953. public float GetContrast()
  954. {
  955. ReadMicroscopeColumn();
  956. if (m_dContrast != 0)
  957. {
  958. ifContrastRead = true;
  959. }
  960. return (float)m_dContrast;
  961. }
  962. public Boolean SetContrast(float set)
  963. {
  964. if (!ifContrastRead)
  965. {
  966. return false;
  967. }
  968. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  969. {
  970. { Column.Contrast, (double)set }
  971. };
  972. m_CollumnUpdated = false;
  973. this.microscopeController.SetColumnConditions(columnDictionary);
  974. int time1 = Environment.TickCount;
  975. int time2;
  976. while (!m_CollumnUpdated)
  977. {
  978. Application.DoEvents();
  979. time2 = Environment.TickCount;
  980. if (time2 - time1 > 2000)
  981. {
  982. currentCommand.returnType = false;
  983. return false;
  984. //break;
  985. }
  986. }
  987. return true;
  988. }
  989. //SEM电压
  990. public float GetSEMVoltage()
  991. {
  992. ReadMicroscopeColumn();
  993. log.Warn(m_dHighVoltage);
  994. return (float)m_dHighVoltage;
  995. }
  996. public Boolean SetSEMVoltage(float set)
  997. {
  998. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  999. {
  1000. { Column.HighVoltage, (double)set }
  1001. };
  1002. m_CollumnUpdated = false;
  1003. this.microscopeController.SetColumnConditions(columnDictionary);
  1004. int time1 = Environment.TickCount;
  1005. int time2;
  1006. while (!m_CollumnUpdated)
  1007. {
  1008. Application.DoEvents();
  1009. time2 = Environment.TickCount;
  1010. if (time2 - time1 > 10000)
  1011. {
  1012. currentCommand.returnType = false;
  1013. return false;
  1014. //break;
  1015. }
  1016. }
  1017. return true;
  1018. }
  1019. public bool SetSemScanExternal(bool b)
  1020. {
  1021. m_ExternalScanUpdated = false;
  1022. if (!b)
  1023. {
  1024. EndMultipleAquisition();
  1025. }
  1026. this.microscopeController.SetExternalScan(b);
  1027. int time1 = Environment.TickCount;
  1028. int time2;
  1029. while (!m_ExternalScanUpdated)
  1030. {
  1031. Application.DoEvents();
  1032. time2 = Environment.TickCount;
  1033. if (time2 - time1 > 2000)
  1034. {
  1035. currentCommand.returnType = false;
  1036. return false;
  1037. //break;
  1038. }
  1039. }
  1040. return true;
  1041. }
  1042. //样品台
  1043. public float[] GetStagePosition()
  1044. {
  1045. ReadStage();
  1046. float[] ret = new float[5];
  1047. ret[0] = (float)m_dStageX;
  1048. ret[1] = (float)m_dStageY;
  1049. ret[2] = (float)m_dStageZ;
  1050. ret[3] = (float)m_dStageT;
  1051. ret[4] = (float)m_dStageR;
  1052. return ret;
  1053. }
  1054. public Boolean SetStagePosition(float[] set)
  1055. {
  1056. double stageX = (double)set[0];
  1057. double stageY = (double)set[1];
  1058. double stageZ = (double)set[2];
  1059. double stageT = (double)set[3];
  1060. double stageR = (double)set[4];
  1061. var stageDictionary = new Dictionary<Stage, double>
  1062. {
  1063. { Stage.StageX, (double)stageX/1000.0 },
  1064. { Stage.StageY, (double)stageY/1000.0 },
  1065. { Stage.StageZ, (double)stageZ },
  1066. { Stage.StageT, (double)stageT },
  1067. { Stage.StageR, (double)stageR }
  1068. };
  1069. m_StageUpdated = false;
  1070. this.microscopeController.SetStageConditions(stageDictionary);
  1071. int time1 = Environment.TickCount;
  1072. int time2;
  1073. while (!m_StageUpdated)
  1074. {
  1075. Application.DoEvents();
  1076. time2 = Environment.TickCount;
  1077. if (time2 - time1 > 20000)
  1078. {
  1079. currentCommand.returnType = false;
  1080. return false;
  1081. //break;
  1082. }
  1083. }
  1084. return true;
  1085. }
  1086. public float GetStageAtX()
  1087. {
  1088. ReadStage();
  1089. return (float)m_dStageX;
  1090. }
  1091. public float GetStageAtY()
  1092. {
  1093. ReadStage();
  1094. return (float)m_dStageY;
  1095. }
  1096. public float GetStageAtZ()
  1097. {
  1098. ReadStage();
  1099. return (float)m_dStageZ;
  1100. }
  1101. public float GetStageAtT()
  1102. {
  1103. ReadStage();
  1104. return (float)m_dStageT;
  1105. }
  1106. public float GetStageAtR()
  1107. {
  1108. ReadStage();
  1109. return (float)m_dStageR;
  1110. }
  1111. public Boolean SetStageGotoX(float set)
  1112. {
  1113. double stageX = (double)set;
  1114. var stageDictionary = new Dictionary<Stage, double>
  1115. {
  1116. { Stage.StageX, (double)stageX/1000.0 }
  1117. };
  1118. m_StageUpdated = false;
  1119. this.microscopeController.SetStageConditions(stageDictionary);
  1120. int time1 = Environment.TickCount;
  1121. int time2;
  1122. while (!m_StageUpdated)
  1123. {
  1124. Application.DoEvents();
  1125. time2 = Environment.TickCount;
  1126. if (time2 - time1 > 20000)
  1127. {
  1128. currentCommand.returnType = false;
  1129. return false;
  1130. //break;
  1131. }
  1132. }
  1133. return true;
  1134. }
  1135. public Boolean SetStageGotoY(float set)
  1136. {
  1137. double stageY = (double)set;
  1138. var stageDictionary = new Dictionary<Stage, double>
  1139. {
  1140. { Stage.StageY, (double)stageY/1000.0 }
  1141. };
  1142. m_StageUpdated = false;
  1143. this.microscopeController.SetStageConditions(stageDictionary);
  1144. int time1 = Environment.TickCount;
  1145. int time2;
  1146. while (!m_StageUpdated)
  1147. {
  1148. Application.DoEvents();
  1149. time2 = Environment.TickCount;
  1150. if (time2 - time1 > 20000)
  1151. {
  1152. currentCommand.returnType = false;
  1153. return false;
  1154. //break;
  1155. }
  1156. }
  1157. return true;
  1158. }
  1159. public Boolean SetStageGotoZ(float set)
  1160. {
  1161. double stageZ = (double)set;
  1162. var stageDictionary = new Dictionary<Stage, double>
  1163. {
  1164. { Stage.StageZ, (double)stageZ }
  1165. };
  1166. m_StageUpdated = false;
  1167. this.microscopeController.SetStageConditions(stageDictionary);
  1168. int time1 = Environment.TickCount;
  1169. int time2;
  1170. while (!m_StageUpdated)
  1171. {
  1172. Application.DoEvents();
  1173. time2 = Environment.TickCount;
  1174. if (time2 - time1 > 20000)
  1175. {
  1176. currentCommand.returnType = false;
  1177. return false;
  1178. //break;
  1179. }
  1180. }
  1181. return true;
  1182. }
  1183. public Boolean SetStageGotoT(float set)
  1184. {
  1185. double stageT = (double)set;
  1186. var stageDictionary = new Dictionary<Stage, double>
  1187. {
  1188. { Stage.StageT, (double)stageT }
  1189. };
  1190. m_StageUpdated = false;
  1191. this.microscopeController.SetStageConditions(stageDictionary);
  1192. int time1 = Environment.TickCount;
  1193. int time2;
  1194. while (!m_StageUpdated)
  1195. {
  1196. Application.DoEvents();
  1197. time2 = Environment.TickCount;
  1198. if (time2 - time1 > 20000)
  1199. {
  1200. currentCommand.returnType = false;
  1201. return false;
  1202. }
  1203. }
  1204. return true;
  1205. }
  1206. public Boolean SetStageGotoR(float set)
  1207. {
  1208. double stageR = (double)set;
  1209. var stageDictionary = new Dictionary<Stage, double>
  1210. {
  1211. { Stage.StageR, (double)stageR }
  1212. };
  1213. m_StageUpdated = false;
  1214. this.microscopeController.SetStageConditions(stageDictionary);
  1215. int time1 = Environment.TickCount;
  1216. int time2;
  1217. while (!m_StageUpdated)
  1218. {
  1219. Application.DoEvents();
  1220. time2 = Environment.TickCount;
  1221. if (time2 - time1 > 20000)
  1222. {
  1223. currentCommand.returnType = false;
  1224. return false;
  1225. //break;
  1226. }
  1227. }
  1228. return true;
  1229. }
  1230. #region 拍图
  1231. //图像扫描尺寸
  1232. public double[] ImageScanSize =
  1233. {
  1234. 32,
  1235. 64,
  1236. 128,
  1237. 256,
  1238. 512,
  1239. 704,
  1240. 768,
  1241. 1408,
  1242. 1024,
  1243. 1536,
  1244. 2048,
  1245. 3072,
  1246. 4096,
  1247. 8192
  1248. };
  1249. public double GetDImagePixelsize()
  1250. {
  1251. return m_dImagePixelsize;
  1252. }
  1253. public void SetDImagePixelsize(double value)
  1254. {
  1255. m_dImagePixelsize = value;
  1256. }
  1257. void InitImageAcquisition()
  1258. {
  1259. imageAcquisitionController = AcquireFactory.CreateImageServer();
  1260. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  1261. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  1262. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  1263. }
  1264. void InitXrayAcquistion()
  1265. {
  1266. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  1267. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  1268. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  1269. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1270. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  1271. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  1272. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  1273. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  1274. }
  1275. //控制电镜释放
  1276. void CloseImageAcquisition()
  1277. {
  1278. if (imageAcquisitionController != null)
  1279. {
  1280. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  1281. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  1282. imageAcquisitionController = null;
  1283. }
  1284. }
  1285. void CloseXrayAcquistion()
  1286. {
  1287. if (EdSpectrumAcquisitionController != null)
  1288. {
  1289. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  1290. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  1291. EdSpectrumAcquisitionController = null;
  1292. }
  1293. }
  1294. /// <summary>
  1295. /// OnImageExperimentStarted
  1296. /// </summary>
  1297. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  1298. {
  1299. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1300. log.Info("拍图开始事件!");
  1301. }
  1302. //int m_nState;
  1303. /// <summary>
  1304. /// OnImageExperimentFinished
  1305. /// </summary>
  1306. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  1307. {
  1308. IElectronImage electronImage = e.Value[0];
  1309. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  1310. {
  1311. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  1312. }
  1313. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  1314. {
  1315. m_bAcquistionDone = true;
  1316. }
  1317. }
  1318. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1319. {
  1320. a_nImageHeight = 0;
  1321. a_nImageWidth = 0;
  1322. a_nPixelSize = 0;
  1323. a_pImageBits = null;
  1324. if (a_electronImage == null)
  1325. {
  1326. return false;
  1327. }
  1328. a_nImageHeight = a_electronImage.Height;
  1329. a_nImageWidth = a_electronImage.Width;
  1330. a_nPixelSize = a_electronImage.PixelSize;
  1331. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1332. long nImageSize = a_nImageHeight * a_nImageWidth;
  1333. long nBufferSize = nImageSize * nBytesPerPixel;
  1334. Byte[] imageData = new Byte[nBufferSize];
  1335. a_electronImage.GetData(imageData);
  1336. a_pImageBits = new Byte[nImageSize];
  1337. // default, oxford will return short image, we need to convert to byte
  1338. if (nBytesPerPixel == 2)
  1339. {
  1340. int nBSEValue = 0;
  1341. for (int i = 0; i < nImageSize; ++i)
  1342. {
  1343. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1344. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1345. }
  1346. }
  1347. else
  1348. {
  1349. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1350. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1351. int nOffset = nBytesPerPixel - 1;
  1352. for (int i = 0; i < nImageSize; ++i)
  1353. {
  1354. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1355. }
  1356. }
  1357. return true;
  1358. }
  1359. //a_dDwellTime : 1~100000之间的数
  1360. //a_sImageType : 1: SE, 2: Bse
  1361. public bool SetImageAcquistionSetting(ImageAquistionParam p)
  1362. {
  1363. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1364. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1365. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1366. if (p.DwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1367. {
  1368. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1369. }
  1370. if (p.DwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1371. {
  1372. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1373. }
  1374. else
  1375. {
  1376. imageSettings.DwellTimeMicroSeconds = p.DwellTime;
  1377. }
  1378. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1379. imageSettings.EnableInputSource((ImageInputSources)p.sourceType, true);
  1380. if (!ImageScanSize.Contains(p.width))
  1381. {
  1382. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1383. return false;
  1384. }
  1385. var pixelSize = 1d / p.width;
  1386. if (p.regionType == ImageAquistionParam.ImageRegionType.FullField)
  1387. {
  1388. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1389. }
  1390. else
  1391. {
  1392. scanSettings.AcquisitionRegion.CreateRectangleRegion(p.ImgRectRegion, pixelSize);
  1393. }
  1394. return true;
  1395. }
  1396. #endregion
  1397. #region X-ray
  1398. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1399. {
  1400. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1401. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1402. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1403. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1404. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1405. }
  1406. private bool GetAtomicNumberByEleName(string name, out int atomicNum)
  1407. {
  1408. var allelenames = theElementNameList.Split(',');
  1409. for (int i = 0; i < allelenames.Length; i++)
  1410. {
  1411. if (allelenames[i] == name)
  1412. {
  1413. atomicNum = i + 1;
  1414. return true;
  1415. }
  1416. }
  1417. atomicNum = 0;
  1418. return false;
  1419. }
  1420. /// <summary>
  1421. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1422. /// </summary>
  1423. /// <param name="sender">sender object</param>
  1424. /// <param name="e">The instance containing the event data.</param>
  1425. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1426. {
  1427. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1428. uint[] m_XrayData;
  1429. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1430. IEdSpectrum edSpectrum = e.Value;
  1431. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1432. {
  1433. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1434. }
  1435. long nXraycount = 0;
  1436. for (int i = 0; i < 2000; i++)
  1437. {
  1438. nXraycount += m_XrayData[i];
  1439. }
  1440. //Quantify processing
  1441. bool bquant = false;
  1442. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1443. {
  1444. bquant = currentCommand.pointXrayPrm.b_quant;
  1445. }
  1446. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1447. {
  1448. bquant = currentCommand.areaXrayPrm.b_quant;
  1449. }
  1450. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1451. {
  1452. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1453. bquant = curXrayData.b_quant;
  1454. }
  1455. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1456. {
  1457. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1458. bquant = curXrayData.b_quant;
  1459. }
  1460. var m_listElement = new Dictionary<string, double>();
  1461. if (bquant)
  1462. {
  1463. if (m_ifautoid == false)
  1464. {
  1465. log.Warn(autoIdSettings.KnownElements);
  1466. autoIdSettings.ClearKnownElements();
  1467. //m_knownelements = "K,Ca,Sc,Ti,V,Mn,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr";
  1468. var knownelenames = m_knownelements.Split(',');
  1469. foreach (var ele in knownelenames)
  1470. {
  1471. int atomicnum;
  1472. if (GetAtomicNumberByEleName(ele, out atomicnum))
  1473. {
  1474. autoIdSettings.SetKnownElement(atomicnum, true);
  1475. }
  1476. }
  1477. log.Warn(autoIdSettings.KnownElements);
  1478. }
  1479. else
  1480. {
  1481. }
  1482. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1483. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1484. quantSettings.CombinedElement = 8;
  1485. quantSettings.Normalised = true;
  1486. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1487. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1488. var ie = Results.GetEnumerator();
  1489. while (ie.MoveNext())
  1490. {
  1491. ISEMQuantResult result = ie.Current;
  1492. if (result.WeightPercent != 0)
  1493. {
  1494. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1495. }
  1496. }
  1497. }
  1498. //------------------------
  1499. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1500. {
  1501. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1502. {
  1503. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1504. currentCommand.pointXrayPrm.listElement = m_listElement;
  1505. m_bXrayDone = true;
  1506. }
  1507. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1508. {
  1509. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1510. currentCommand.areaXrayPrm.listElement = m_listElement;
  1511. m_bXrayDone = true;
  1512. }
  1513. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1514. {
  1515. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1516. curXrayData.XrayData = m_XrayData;
  1517. curXrayData.listElement = m_listElement;
  1518. currentCommand.PointXrayDataReceived += 1;
  1519. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1520. {
  1521. m_bXrayDone = true;
  1522. }
  1523. }
  1524. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1525. {
  1526. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1527. curXrayData.XrayData = m_XrayData;
  1528. curXrayData.listElement = m_listElement;
  1529. currentCommand.AreaXrayDataReceived += 1;
  1530. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1531. {
  1532. m_bXrayDone = true;
  1533. }
  1534. }
  1535. }
  1536. }
  1537. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1538. {
  1539. a_pSpectrumData = new uint[a_nBufferSize];
  1540. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1541. a_spectrum.GetChannelData(xrayData);
  1542. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1543. int nChannelStart = 0;
  1544. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1545. {
  1546. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1547. }
  1548. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1549. double dStep1 = 1.0 / nDataLength;
  1550. double dStep2 = 1.0 / a_nBufferSize;
  1551. for (int i = 0; i < nDataLength; ++i)
  1552. {
  1553. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1554. double dBinPos = i * dStep1;
  1555. long nLeftBin = (long)(dBinPos / dStep2);
  1556. // calculate % into left bin
  1557. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1558. // calculate data into the left bin
  1559. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1560. // put data into bins
  1561. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1562. if ((nLeftBin + 1) < a_nBufferSize)
  1563. {
  1564. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1565. }
  1566. }
  1567. return true;
  1568. }
  1569. public bool IsAcquiringSpectrum()
  1570. {
  1571. return EdSpectrumAcquisitionController.IsAcquiring;
  1572. }
  1573. public void BeginMultipleAquisition()
  1574. {
  1575. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1576. }
  1577. public void EndMultipleAquisition()
  1578. {
  1579. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1580. }
  1581. public bool GetSemBeamOn()
  1582. {
  1583. var beamon = microscopeController.ColumnConditions.BeamOn;
  1584. if (beamon == 1)
  1585. {
  1586. return true;
  1587. }
  1588. else
  1589. {
  1590. return false;
  1591. }
  1592. }
  1593. public bool SetSemBeamOnOrOff(bool val)
  1594. {
  1595. double beamon;
  1596. if (val)
  1597. {
  1598. beamon = 1;
  1599. }
  1600. else
  1601. {
  1602. beamon = 0;
  1603. }
  1604. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1605. {
  1606. { Column.BeamOn, (double)beamon }
  1607. };
  1608. m_CollumnUpdated = false;
  1609. this.microscopeController.SetColumnConditions(columnDictionary);
  1610. int time1 = Environment.TickCount;
  1611. int time2;
  1612. while (!m_CollumnUpdated)
  1613. {
  1614. Application.DoEvents();
  1615. time2 = Environment.TickCount;
  1616. if (time2 - time1 > 10000)
  1617. {
  1618. currentCommand.returnType = false;
  1619. return false;
  1620. }
  1621. }
  1622. return true;
  1623. }
  1624. public bool SetSemFilamentOnOrOff(bool val)
  1625. {
  1626. double beamon;
  1627. if (val)
  1628. {
  1629. beamon = 1;
  1630. }
  1631. else
  1632. {
  1633. beamon = 0;
  1634. }
  1635. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1636. {
  1637. { Column.FilamentOn, (double)beamon }
  1638. };
  1639. m_CollumnUpdated = false;
  1640. this.microscopeController.SetColumnConditions(columnDictionary);
  1641. int time1 = Environment.TickCount;
  1642. int time2;
  1643. while (!m_CollumnUpdated)
  1644. {
  1645. Application.DoEvents();
  1646. time2 = Environment.TickCount;
  1647. if (time2 - time1 > 10000)
  1648. {
  1649. currentCommand.returnType = false;
  1650. return false;
  1651. }
  1652. }
  1653. return true;
  1654. }
  1655. public void StopXrayAquisition()
  1656. {
  1657. EdSpectrumAcquisitionController.StopAcquisition();
  1658. }
  1659. public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
  1660. {
  1661. m_ifautoid = IfAutoId;
  1662. m_knownelements = knownElements;
  1663. }
  1664. #endregion
  1665. }
  1666. }