ExtenderWrapper.cs 56 KB

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