ExtenderWrapper1.cs 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Windows.Forms;
  22. //显示弹出信息
  23. //using System.Windows.Forms;
  24. namespace OxfordExtenderWrapper1
  25. {
  26. enum OxfordControllerState
  27. {
  28. READY = 0,
  29. WORKING = 1,
  30. SUCCEEDED = 2,
  31. FAILED = 3,
  32. ABORT = 4
  33. };
  34. [Serializable]
  35. public class Segment
  36. {
  37. private int m_nX;
  38. private int m_nY;
  39. private int m_nLength;
  40. public int X
  41. {
  42. get { return m_nX; }
  43. set
  44. {
  45. if (value > 0)
  46. {
  47. m_nX = value;
  48. }
  49. }
  50. }
  51. public int Y
  52. {
  53. get { return m_nY; }
  54. set
  55. {
  56. if (value > 0)
  57. {
  58. m_nY = value;
  59. }
  60. }
  61. }
  62. public int Length
  63. {
  64. get { return m_nLength; }
  65. set
  66. {
  67. if (value > 0)
  68. {
  69. m_nLength = value;
  70. }
  71. }
  72. }
  73. };
  74. /// <summary>
  75. /// Enum EDSConst definition.
  76. /// </summary>
  77. enum EDSConst
  78. {
  79. MAX_XRAY_BATCH = 1024,
  80. XANA_CHANNELS = 2000,
  81. XANA_CHANNELS_MAX = 4192,
  82. MAX_AMPTIME_CONSTANTS = 10,
  83. MAX_EV_PER_CHANNELS = 5,
  84. MAX_ANALYZERS = 5,
  85. MAX_LEN_ANALYZER_NAME = 16
  86. };
  87. [Serializable]
  88. struct OxfordChord
  89. {
  90. public long m_nX;
  91. public long m_nY;
  92. public long m_nLength;
  93. };
  94. enum OxfordCommandType
  95. {
  96. GetMagnification = 0,
  97. SetMagnification = 1,
  98. GetWorkingDistance = 2,
  99. SetWorkingDistance = 3,
  100. GetBrightness = 4,
  101. SetBrightness = 5,
  102. GetContrast = 6,
  103. SetContrast = 7,
  104. GetSEMVoltage = 8,
  105. SetSEMVoltage = 9,
  106. //样品台
  107. GetStagePosition = 10,
  108. SetStagePosition = 11,
  109. GetStageAtX = 12,
  110. GetStageAtY = 13,
  111. GetStageAtZ = 14,
  112. GetStageAtT = 15,
  113. GetStageAtR = 16,
  114. SetStageGotoX = 17,
  115. SetStageGotoY = 18,
  116. SetStageGotoZ = 19,
  117. SetStageGotoT = 20,
  118. SetStageGotoR = 21,
  119. MoveStageXY = 22,
  120. //拍图
  121. ImageAquisition = 23,
  122. //采集参数设置
  123. SetImageAcquistionSetting = 24,
  124. //获取分辨率
  125. GetImageResolution = 25,
  126. //设置分辨率
  127. SetImageResolution= 26,
  128. //获取bitmap
  129. GetBitmap = 27,
  130. IsAcquiringSpectrum = 28,
  131. //X-ray
  132. //点采集
  133. XrayPointCollection = 29,
  134. COLLECT_XRAYPOINTS=30,
  135. //面采集
  136. XrayAreaCollection = 31,
  137. COLLECT_XRAYFEATURES = 32,
  138. BeginMultipleAquisition = 33,
  139. EndMultipleAquisition = 34,
  140. BeginAreaModeMultipleAquisition = 35,
  141. EndAreaModeMultipleAquisition = 36,
  142. Exit = 100,
  143. }
  144. [Serializable]
  145. public class ImageAquistionParam
  146. {
  147. public ImageAquistionParam()
  148. {
  149. this.ImageData = new byte[width * height];
  150. }
  151. //in
  152. public int width;
  153. public int height;
  154. public double DwellTime;
  155. public ImageInputSources 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. }
  174. [Serializable]
  175. public class AreaXrayParam
  176. {
  177. public AreaXrayParam()
  178. {
  179. this.XrayData = new uint[2000];
  180. this.listElement = new Dictionary<string, double>();
  181. }
  182. public double dMilliSecondsTime;
  183. public List<Segment> a_listChord=new List<Segment>();
  184. public uint[] XrayData;
  185. public Dictionary<string, double> listElement;
  186. }
  187. [Serializable]
  188. public struct MoveStageParam
  189. {
  190. public float x;
  191. public float y;
  192. }
  193. struct oxfordCommandData
  194. {
  195. public OxfordCommandType commandType;
  196. public bool returnType;
  197. public MoveStageParam moveStagePrm;
  198. public ImageAquistionParam grabImgParam;
  199. public PointXrayParam pointXrayPrm;
  200. public AreaXrayParam areaXrayPrm;
  201. public List<PointXrayParam> XrayPrmForPoints;
  202. public int PointXrayDataReceived;
  203. public List<AreaXrayParam> XrayPrmForFeatures;
  204. public int AreaXrayDataReceived;
  205. }
  206. public class ExtenderWrapper:MarshalByRefObject
  207. {
  208. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  209. private Thread controlThread;
  210. AutoResetEvent startEvent = new AutoResetEvent(false);
  211. AutoResetEvent endEvent = new AutoResetEvent(false);
  212. private oxfordCommandData currentCommand;
  213. #region 电镜控制、样品台
  214. //控制电镜
  215. private IMicroscopeController microscopeController = null;
  216. /// <summary>
  217. /// IImageAcquisitionController object
  218. /// </summary>
  219. private IImageAcquisitionController imageAcquisitionController = null;
  220. /// <summary>
  221. /// IImageSettings object
  222. /// </summary>
  223. private IImageAcquisitionSettings imageAcquisitionSettings = null;
  224. //控制器
  225. private IEdSpectrumAcquisitionController EdSpectrumAcquisitionController = null;
  226. private IEdSpectrumSettings EdSpectrumSettings = null;
  227. private IEdSpectrumProcessing EdSpectrumProcessing = null;
  228. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  229. private IAutoIdSettings autoIdSettings = null;
  230. private ISEMQuantSettings quantSettings = null;
  231. private IEdChordListAcquisitionController _edsChordListController = null;
  232. private OxfordControllerState m_nState;
  233. private IEdChordListSettings _edsChordListSetting;
  234. private int XRayChannelLength = 2000;
  235. private int EDSColletionTimeOut = 10000;
  236. private AutoResetEvent m_endControllerEvent;
  237. //当前feature的pixel数控制
  238. private long m_nCollectedPixelCounts;
  239. private long m_nPixelNum;
  240. private List<Element> m_listElementResult;
  241. private OxfordXrayData[] m_pXrayData;
  242. const int g_nOxfordControllerProcessTime = 4;
  243. const int g_nOxfordControllerEnergyRange = 20;
  244. // feature计数控制
  245. private long m_nXrayDataCount;
  246. private long m_nCollectedXrayCounts;
  247. private bool m_bXrayDone = false;
  248. //电压
  249. private double m_dHighVoltage;
  250. //放大倍数
  251. private double m_dMagnification;
  252. //工作距离
  253. private double m_dWorkingDistance;
  254. //亮度
  255. private double m_dBirghtness;
  256. //对比度
  257. private double m_dContrast;
  258. //BeamOn
  259. private bool m_bBeamOn;
  260. //FilamentOn
  261. private bool m_bFilamentOn;
  262. //样品台位置
  263. private double m_dStageX;
  264. private double m_dStageY;
  265. private double m_dStageZ;
  266. private double m_dStageR;
  267. private double m_dStageT;
  268. private double m_dStageXMax;
  269. //构造函数
  270. public ExtenderWrapper()
  271. {
  272. }
  273. public bool ConnectToEDSHardware()
  274. {
  275. try
  276. {
  277. InitMicroscopeController();
  278. InitImageAcquisition();
  279. InitXrayAcquistion();
  280. controlThread = new Thread(this.ControlerThread);
  281. controlThread.Start();
  282. return true;
  283. }
  284. catch (Exception e)
  285. {
  286. log.Error(e.Message);
  287. return false;
  288. }
  289. }
  290. //结束
  291. public void CloseExtender()
  292. {
  293. CloseMicroscopeController();
  294. CloseImageAcquisition();
  295. CloaseXrayAcquistion();
  296. currentCommand.commandType = OxfordCommandType.Exit;
  297. if (controlThread != null)
  298. {
  299. startEvent.Set();
  300. controlThread.Join();
  301. }
  302. controlThread = null;
  303. }
  304. private void ControlerThread()
  305. {
  306. log.Info("this has been initialized");
  307. while (true)
  308. {
  309. while (true)
  310. {
  311. if (startEvent.WaitOne(0, true))
  312. {
  313. break;
  314. }
  315. //Thread.Sleep(50);
  316. Application.DoEvents();
  317. }
  318. switch (currentCommand.commandType)
  319. {
  320. case OxfordCommandType.MoveStageXY:
  321. {
  322. MoveStageParam p = currentCommand.moveStagePrm;
  323. double stageX = (double)p.x;
  324. if (stageX > StageXMax || stageX < StageXMin)
  325. {
  326. currentCommand.returnType = false;
  327. }
  328. double stageY = (double)p.y;
  329. if (stageY > StageYMax || stageY < StageYMin)
  330. {
  331. currentCommand.returnType = false;
  332. }
  333. var stageDictionary = new Dictionary<Stage, double>
  334. {
  335. { Stage.StageX, (double)stageX },
  336. { Stage.StageY, (double)stageY }
  337. };
  338. this.microscopeController.SetStageConditions(stageDictionary);
  339. currentCommand.returnType = true;
  340. endEvent.Set();
  341. }
  342. break;
  343. case OxfordCommandType.ImageAquisition:
  344. {
  345. ImageAquistionParam p = currentCommand.grabImgParam;
  346. log.Info("begin:GrabImage:oxford thread 线程ID:" + Thread.CurrentThread.ManagedThreadId.ToString());
  347. SetImageAcquistionSetting(p.DwellTime, p.sourceType, p.width);
  348. try
  349. {
  350. int lastingTime = 0;
  351. imageAcquisitionController.BeginMultipleAcquisition();
  352. IEnumerable<IElectronImage> images = imageAcquisitionController.StartAcquisition(imageAcquisitionSettings);
  353. log.Info("拍图开始 startAcquisition 完成");
  354. while (true)
  355. {
  356. if (m_bAcquistionDone)
  357. {
  358. p.ImageData = m_ImageBit;
  359. currentCommand.returnType = true;
  360. break;
  361. }
  362. Application.DoEvents();
  363. Thread.Sleep(100);
  364. lastingTime += 100;
  365. if (lastingTime > EDSColletionTimeOut * 6)
  366. {
  367. currentCommand.returnType = false;
  368. }
  369. }
  370. }
  371. catch (InvalidSettingsException settingsException)
  372. {
  373. var sb = new StringBuilder(@"Invalid settings have been supplied: ");
  374. sb.AppendLine();
  375. settingsException.ValidationResults.ValidationErrors.ToList().ForEach(ve => sb.AppendFormat("{0}{1}", Environment.NewLine, ve));
  376. NLog.LogManager.GetCurrentClassLogger().Error(sb.ToString());
  377. }
  378. catch (AcquisitionStartException startException)
  379. {
  380. string msg = string.Format(@"AcquisitionStartException: {0}", startException.Message);
  381. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  382. }
  383. endEvent.Set();
  384. }
  385. break;
  386. case OxfordCommandType.XrayPointCollection:
  387. {
  388. PointXrayParam p = currentCommand.pointXrayPrm;
  389. //log.Info("线程:开始采集单点xray");
  390. //m_XrayData = null;
  391. m_bXrayDone = false;
  392. p.XrayData = new uint[XRayChannelLength];
  393. p.listElement = new Dictionary<string, double>();
  394. SetXrayAcquisitionParam(p.dMilliSecondsTime);
  395. //SetPointAcquistionRegion(x*m_dImagePixelsize, y*m_dImagePixelsize);
  396. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(p.x * m_dImagePixelsize, p.y * m_dImagePixelsize));
  397. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  398. log.Info("开始XrayStartAcquisition:t=" + p.dMilliSecondsTime.ToString() + "pos=" + "(" + p.x.ToString() + "," + p.y.ToString() + ")");
  399. try
  400. {
  401. m_bXrayDone = false;
  402. int lastingTime = 0;
  403. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  404. while (true)
  405. {
  406. if (m_bXrayDone)
  407. {
  408. currentCommand.returnType = true;
  409. break;
  410. }
  411. Application.DoEvents();
  412. Thread.Sleep(100);
  413. lastingTime += 100;
  414. if (lastingTime > EDSColletionTimeOut * 3)
  415. {
  416. //EdSpectrumAcquisitionController.EndMultipleAcquisition();
  417. log.Warn("XrayStartAcquisition 超时!");
  418. currentCommand.returnType = false;
  419. }
  420. }
  421. }
  422. catch (InvalidSettingsException invalidSettingsException)
  423. {
  424. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  425. invalidSettingsException.Message,
  426. invalidSettingsException.ValidationResults.ValidationErrors);
  427. log.Error(msg);
  428. }
  429. catch (AcquisitionStartException acquisitionStartException)
  430. {
  431. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  432. log.Error(msg);
  433. }
  434. endEvent.Set();
  435. }
  436. break;
  437. case OxfordCommandType.COLLECT_XRAYPOINTS:
  438. {
  439. var p = currentCommand.XrayPrmForPoints;
  440. log.Info("线程:开始采集多点xray");
  441. var PointXrayDatas = currentCommand.XrayPrmForPoints;
  442. currentCommand.PointXrayDataReceived = 0;
  443. m_bXrayDone = false;
  444. int lastingTime = 0;
  445. var dMilliSecondsTime = PointXrayDatas[0].dMilliSecondsTime;
  446. SetXrayAcquisitionParam(dMilliSecondsTime);
  447. foreach (var prm in PointXrayDatas)
  448. {
  449. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(prm.x * m_dImagePixelsize, prm.y * m_dImagePixelsize));
  450. log.Info("开始XrayStartAcquisition:t=" + dMilliSecondsTime.ToString() + "pos=" + "(" + prm.x.ToString() + "," + prm.y.ToString() + ")");
  451. try
  452. {
  453. m_bXrayDone = false;
  454. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  455. }
  456. catch (InvalidSettingsException invalidSettingsException)
  457. {
  458. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  459. invalidSettingsException.Message,
  460. invalidSettingsException.ValidationResults.ValidationErrors);
  461. log.Error(msg);
  462. }
  463. catch (AcquisitionStartException acquisitionStartException)
  464. {
  465. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  466. log.Error(msg);
  467. }
  468. }
  469. while (true)
  470. {
  471. if (m_bXrayDone)
  472. {
  473. currentCommand.returnType = true;
  474. break;
  475. }
  476. Application.DoEvents();
  477. Thread.Sleep(100);
  478. lastingTime += 100;
  479. if (lastingTime > EDSColletionTimeOut * 3)
  480. {
  481. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  482. log.Warn("XrayStartAcquisition 超时!");
  483. currentCommand.returnType = false;
  484. }
  485. }
  486. endEvent.Set();
  487. }
  488. break;
  489. case OxfordCommandType.XrayAreaCollection:
  490. {
  491. AreaXrayParam p = currentCommand.areaXrayPrm;
  492. log.Info("线程:开始面扫xray");
  493. m_bXrayDone = false;
  494. SetXrayAcquisitionParam(p.dMilliSecondsTime);
  495. List<Chord> Chords = new List<Chord>();
  496. foreach (Segment seg in p.a_listChord)
  497. {
  498. Chord chord = new Chord(seg.X, seg.Y, seg.Length);
  499. Chords.Add(chord);
  500. }
  501. SetAreaAcquistionRegion(Chords);
  502. try
  503. {
  504. int lastingTime = 0;
  505. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  506. while (true)
  507. {
  508. if (m_bXrayDone)
  509. {
  510. currentCommand.returnType = true;
  511. break;
  512. }
  513. Thread.Sleep(100);
  514. Application.DoEvents();
  515. lastingTime += 100;
  516. if (lastingTime > EDSColletionTimeOut * 8)
  517. {
  518. currentCommand.returnType = false;
  519. }
  520. }
  521. }
  522. catch (InvalidSettingsException invalidSettingsException)
  523. {
  524. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  525. invalidSettingsException.Message,
  526. invalidSettingsException.ValidationResults.ValidationErrors);
  527. log.Error(msg);
  528. }
  529. catch (AcquisitionStartException acquisitionStartException)
  530. {
  531. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  532. log.Error(msg);
  533. }
  534. endEvent.Set();
  535. }
  536. break;
  537. case OxfordCommandType.COLLECT_XRAYFEATURES:
  538. {
  539. var p = currentCommand.XrayPrmForFeatures;
  540. currentCommand.AreaXrayDataReceived = 0;
  541. log.Info("线程:开始线扫xray");
  542. //m_XrayData = null;
  543. m_bXrayDone = false;
  544. int lastingTime = 0;
  545. var dMilliSecondsTime = p[0].dMilliSecondsTime;
  546. SetXrayAcquisitionParam(p[0].dMilliSecondsTime);
  547. foreach (var prm in p)
  548. {
  549. List<Chord> Chords = new List<Chord>();
  550. foreach (Segment seg in prm.a_listChord)
  551. {
  552. Chord chord = new Chord(seg.X, seg.Y, seg.Length);
  553. Chords.Add(chord);
  554. }
  555. SetAreaAcquistionRegion(Chords);
  556. try
  557. {
  558. m_bXrayDone = false;
  559. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  560. }
  561. catch (InvalidSettingsException invalidSettingsException)
  562. {
  563. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  564. invalidSettingsException.Message,
  565. invalidSettingsException.ValidationResults.ValidationErrors);
  566. log.Error(msg);
  567. }
  568. catch (AcquisitionStartException acquisitionStartException)
  569. {
  570. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  571. log.Error(msg);
  572. }
  573. }
  574. while (true)
  575. {
  576. if (m_bXrayDone)
  577. {
  578. currentCommand.returnType = true;
  579. break;
  580. }
  581. Application.DoEvents();
  582. Thread.Sleep(100);
  583. lastingTime += 100;
  584. if (lastingTime > EDSColletionTimeOut * 3)
  585. {
  586. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  587. log.Warn("XrayStartAcquisition 超时!");
  588. //return false;
  589. currentCommand.returnType = false;
  590. }
  591. }
  592. endEvent.Set();
  593. }
  594. break;
  595. case OxfordCommandType.Exit:
  596. {
  597. endEvent.Set();
  598. log.Info("receive exit command");
  599. }
  600. break;
  601. }
  602. if (currentCommand.commandType == OxfordCommandType.Exit)
  603. {
  604. log.Info("exiting the thread");
  605. break;
  606. }
  607. }
  608. }
  609. public bool AquisitionImage(ref ImageAquistionParam p)
  610. {
  611. currentCommand.grabImgParam = p;
  612. currentCommand.commandType = OxfordCommandType.ImageAquisition;
  613. startEvent.Set();
  614. while (true)
  615. {
  616. if (endEvent.WaitOne(0, false))
  617. {
  618. break;
  619. }
  620. Application.DoEvents();
  621. }
  622. log.Info("GrabImage:oxford thread return: " + currentCommand.returnType.ToString());
  623. if (currentCommand.returnType == true)
  624. {
  625. return true;
  626. }
  627. else
  628. {
  629. return false;
  630. }
  631. }
  632. public bool MoveStageXY(float x, float y)
  633. {
  634. currentCommand.moveStagePrm = new MoveStageParam();
  635. currentCommand.moveStagePrm.x = x;
  636. currentCommand.moveStagePrm.y = y;
  637. currentCommand.commandType = OxfordCommandType.MoveStageXY;
  638. startEvent.Set();
  639. while (true)
  640. {
  641. if (endEvent.WaitOne(0, false))
  642. {
  643. break;
  644. }
  645. }
  646. if (currentCommand.returnType == true)
  647. {
  648. return true;
  649. }
  650. else
  651. {
  652. return false;
  653. }
  654. }
  655. public bool XrayAreaCollecting(ref AreaXrayParam p)
  656. {
  657. currentCommand.areaXrayPrm = p;
  658. currentCommand.commandType = OxfordCommandType.XrayAreaCollection;
  659. startEvent.Set();
  660. while (true)
  661. {
  662. if (endEvent.WaitOne(0, false))
  663. {
  664. break;
  665. }
  666. }
  667. if (currentCommand.returnType == true)
  668. {
  669. return true;
  670. }
  671. else
  672. {
  673. return false;
  674. }
  675. }
  676. public bool XrayPointCollecting(ref PointXrayParam p)
  677. {
  678. currentCommand.pointXrayPrm = p;
  679. currentCommand.commandType = OxfordCommandType.XrayPointCollection;
  680. startEvent.Set();
  681. while (true)
  682. {
  683. if (endEvent.WaitOne(0, false))
  684. {
  685. break;
  686. }
  687. Application.DoEvents();
  688. }
  689. if (currentCommand.returnType == true)
  690. {
  691. return true;
  692. }
  693. else
  694. {
  695. return false;
  696. }
  697. }
  698. public bool CollectXrayByPoints(ref List<PointXrayParam> a_listPoints, uint a_nXRayAQTime, bool a_bElementInfo)
  699. {
  700. currentCommand.XrayPrmForPoints = a_listPoints;
  701. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYPOINTS;
  702. startEvent.Set();
  703. while (true)
  704. {
  705. if (endEvent.WaitOne(0, false))
  706. {
  707. break;
  708. }
  709. Application.DoEvents();
  710. }
  711. if (currentCommand.returnType == true)
  712. {
  713. //a_listPoints.Clear();
  714. //foreach (var p in currentCommand.XrayPrmForPoints)
  715. //{
  716. // a_listPoints.Add(p);
  717. //}
  718. //a_listPoints = currentCommand.XrayPrmForPoints;
  719. return true;
  720. }
  721. else
  722. {
  723. return false;
  724. }
  725. }
  726. public bool CollectXrayByFeatures(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
  727. {
  728. currentCommand.XrayPrmForFeatures = a_listFeatures;
  729. currentCommand.commandType = OxfordCommandType.COLLECT_XRAYFEATURES;
  730. startEvent.Set();
  731. while (true)
  732. {
  733. if (endEvent.WaitOne(0, false))
  734. {
  735. break;
  736. }
  737. }
  738. if (currentCommand.returnType == true)
  739. {
  740. a_listFeatures = currentCommand.XrayPrmForFeatures;
  741. return true;
  742. }
  743. else
  744. {
  745. return false;
  746. }
  747. }
  748. //public void BeginMultipleAquisition()
  749. //{
  750. // currentCommand.commandType = OxfordCommandType.BeginMultipleAquisition;
  751. // startEvent.Set();
  752. // while (true)
  753. // {
  754. // if (endEvent.WaitOne(0, false))
  755. // {
  756. // break;
  757. // }
  758. // //Application.DoEvents();
  759. // }
  760. //}
  761. //public void EndMultipleAquisition()
  762. //{
  763. // currentCommand.commandType = OxfordCommandType.EndMultipleAquisition;
  764. // startEvent.Set();
  765. // while (true)
  766. // {
  767. // if (endEvent.WaitOne(0, false))
  768. // {
  769. // break;
  770. // }
  771. // //Application.DoEvents();
  772. // }
  773. //}
  774. //public void BeginAreaModeMultipleAquisition()
  775. //{
  776. // currentCommand.commandType = OxfordCommandType.BeginAreaModeMultipleAquisition;
  777. // startEvent.Set();
  778. // while (true)
  779. // {
  780. // if (endEvent.WaitOne(0, false))
  781. // {
  782. // break;
  783. // }
  784. // //Application.DoEvents();
  785. // }
  786. //}
  787. //public void EndAreaModeMultipleAquisition()
  788. //{
  789. // currentCommand.commandType = OxfordCommandType.EndAreaModeMultipleAquisition;
  790. // startEvent.Set();
  791. // while (true)
  792. // {
  793. // if (endEvent.WaitOne(0, false))
  794. // {
  795. // break;
  796. // }
  797. // //Application.DoEvents();
  798. // }
  799. //}
  800. public double StageXMax
  801. {
  802. get { return this.m_dStageXMax; }
  803. set { this.m_dStageXMax = value; }
  804. }
  805. private double m_dStageXMin;
  806. public double StageXMin
  807. {
  808. get { return this.m_dStageXMin; }
  809. set { this.m_dStageXMin = value; }
  810. }
  811. private double m_dStageYMax;
  812. public double StageYMax
  813. {
  814. get { return this.m_dStageYMax; }
  815. set { this.m_dStageYMax = value; }
  816. }
  817. private double m_dStageYMin;
  818. public double StageYMin
  819. {
  820. get { return this.m_dStageYMin; }
  821. set { this.m_dStageYMin = value; }
  822. }
  823. private double m_dStageZMax;
  824. public double StageZMax
  825. {
  826. get { return this.m_dStageZMax; }
  827. set { this.m_dStageZMax = value; }
  828. }
  829. private double m_dStageZMin;
  830. public double StageZMin
  831. {
  832. get { return this.m_dStageZMin; }
  833. set { this.m_dStageZMin = value; }
  834. }
  835. private double m_dStageTMax;
  836. public double StageTMax
  837. {
  838. get { return this.m_dStageTMax; }
  839. set { this.m_dStageTMax = value; }
  840. }
  841. private double m_dStageTMin;
  842. public double StageTMin
  843. {
  844. get { return this.m_dStageTMin; }
  845. set { this.m_dStageTMin = value; }
  846. }
  847. private double m_dStageRMax;
  848. public double StageRMax
  849. {
  850. get { return this.m_dStageRMax; }
  851. set { this.m_dStageRMax = value; }
  852. }
  853. private double m_dStageRMin;
  854. public double StageRMin
  855. {
  856. get { return this.m_dStageRMin; }
  857. set { this.m_dStageRMin = value; }
  858. }
  859. internal oxfordCommandData GetCurrentCommand()
  860. {
  861. return currentCommand;
  862. }
  863. internal void SetCurrentCommand(oxfordCommandData value)
  864. {
  865. currentCommand = value;
  866. }
  867. //控制电镜初始化
  868. void InitMicroscopeController(float a_fStageXMin = 0, float a_fStageXMax = 130,
  869. float a_fStageYMin = 0, float a_fStageYMax = 130,
  870. float a_fStageZMin = 0, float a_fStageZMax = 40,
  871. float a_fStageTMin = 0, float a_fStageTMax = 90,
  872. float a_fStageRMin = 0, float a_fStageRMax = 360)
  873. {
  874. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  875. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  876. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  877. this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
  878. this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
  879. this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
  880. ReadMicroscopeColumn();
  881. ReadStage();
  882. StageXMax = a_fStageXMax;
  883. StageXMin = a_fStageXMin;
  884. StageYMax = a_fStageYMax;
  885. StageYMin = a_fStageYMin;
  886. StageZMax = a_fStageZMax;
  887. StageZMin = a_fStageZMin;
  888. StageTMax = a_fStageTMax;
  889. StageTMin = a_fStageTMin;
  890. StageRMax = a_fStageRMax;
  891. StageRMin = a_fStageRMin;
  892. }
  893. //控制电镜释放
  894. void CloseMicroscopeController()
  895. {
  896. if (microscopeController != null)
  897. {
  898. this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
  899. this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
  900. this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
  901. this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
  902. this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
  903. microscopeController = null;
  904. }
  905. }
  906. //读取当前的电镜控制值
  907. private void ReadMicroscopeColumn()
  908. {
  909. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  910. var columnConditions = this.microscopeController.ColumnConditions;
  911. if (columnCapabilities.Magnification.CanRead)
  912. {
  913. m_dMagnification = columnConditions.Magnification;
  914. }
  915. if (columnCapabilities.WorkingDistance.CanRead)
  916. {
  917. m_dWorkingDistance = columnConditions.WorkingDistance;
  918. }
  919. if (columnCapabilities.HighVoltage.CanRead)
  920. {
  921. m_dHighVoltage = columnConditions.HighVoltage;
  922. }
  923. if (columnCapabilities.Brightness.CanRead)
  924. {
  925. m_dBirghtness = columnConditions.Brightness;
  926. }
  927. if (columnCapabilities.Contrast.CanRead)
  928. {
  929. m_dContrast = columnConditions.Contrast;
  930. }
  931. if (columnCapabilities.BeamOn.CanRead)
  932. {
  933. m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
  934. }
  935. if (columnCapabilities.FilamentOn.CanRead)
  936. {
  937. m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
  938. }
  939. }
  940. //读取样品台位置
  941. private void ReadStage()
  942. {
  943. var stageCapabilities = this.microscopeController.StageCapabilities;
  944. var stageConditions = this.microscopeController.StageConditions;
  945. if (stageCapabilities.StageX.CanRead)
  946. {
  947. this.m_dStageX = stageConditions.StageX;
  948. }
  949. if (stageCapabilities.StageY.CanRead)
  950. {
  951. this.m_dStageY = stageConditions.StageY;
  952. }
  953. if (stageCapabilities.StageZ.CanRead)
  954. {
  955. this.m_dStageZ = stageConditions.StageZ;
  956. }
  957. if (stageCapabilities.StageT.CanRead)
  958. {
  959. this.m_dStageT = stageConditions.StageT;
  960. }
  961. if (stageCapabilities.StageR.CanRead)
  962. {
  963. this.m_dStageR = stageConditions.StageR;
  964. }
  965. }
  966. //电镜控制改变事件
  967. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  968. {
  969. ReadMicroscopeColumn();
  970. }
  971. //样品台控制改变事件
  972. private void OnMicroscopeStageChange(object sender, EventArgs e)
  973. {
  974. ReadStage();
  975. }
  976. //列控制连接或断开时的事件
  977. private void OnMicroscopeColumnConnected(object sender, EventArgs e)
  978. {
  979. }
  980. //样品台控制连接或断开时的事件
  981. private void OnMicroscopeStageConnected(object sender, EventArgs e)
  982. {
  983. ReadStage();
  984. }
  985. //样品台控制、电镜控制、外围控制的事件改变完成
  986. private void OnMicroscopeChangeCompleted(object sender, EventArgs e)
  987. {
  988. ReadMicroscopeColumn();
  989. ReadStage();
  990. }
  991. public float GetMagnification()
  992. {
  993. return (float)m_dMagnification;
  994. }
  995. public Boolean SetMagnification(float set)
  996. {
  997. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  998. {
  999. { Column.Magnification, (double)set }
  1000. };
  1001. this.microscopeController.SetColumnConditions(columnDictionary);
  1002. return true;
  1003. }
  1004. //焦距
  1005. public float GetWorkingDistance()
  1006. {
  1007. return (float)m_dWorkingDistance;
  1008. }
  1009. public Boolean SetWorkingDistance(float set)
  1010. {
  1011. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1012. {
  1013. { Column.WorkingDistance, (double)set }
  1014. };
  1015. this.microscopeController.SetColumnConditions(columnDictionary);
  1016. return true;
  1017. }
  1018. //亮度
  1019. public float GetBrightness()
  1020. {
  1021. return (float)m_dBirghtness;
  1022. }
  1023. public Boolean SetBrightness(float set)
  1024. {
  1025. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1026. {
  1027. { Column.Brightness, (double)set }
  1028. };
  1029. this.microscopeController.SetColumnConditions(columnDictionary);
  1030. return true;
  1031. }
  1032. //对比度
  1033. public float GetContrast()
  1034. {
  1035. return (float)m_dContrast;
  1036. }
  1037. public Boolean SetContrast(float set)
  1038. {
  1039. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1040. {
  1041. { Column.Contrast, (double)set }
  1042. };
  1043. this.microscopeController.SetColumnConditions(columnDictionary);
  1044. return true;
  1045. }
  1046. //SEM电压
  1047. public float GetSEMVoltage()
  1048. {
  1049. return (float)m_dHighVoltage;
  1050. }
  1051. public Boolean SetSEMVoltage(float set)
  1052. {
  1053. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  1054. {
  1055. { Column.HighVoltage, (double)set }
  1056. };
  1057. this.microscopeController.SetColumnConditions(columnDictionary);
  1058. return true;
  1059. }
  1060. public bool SetSemScanExternal(bool b)
  1061. {
  1062. this.microscopeController.SetExternalScan(b);
  1063. return true;
  1064. }
  1065. //样品台
  1066. public float[] GetStagePosition()
  1067. {
  1068. float[] ret = new float[5];
  1069. ret[0] = (float)m_dStageX;
  1070. ret[1] = (float)m_dStageY;
  1071. ret[2] = (float)m_dStageZ;
  1072. ret[3] = (float)m_dStageT;
  1073. ret[4] = (float)m_dStageR;
  1074. return ret;
  1075. }
  1076. public Boolean SetStagePosition(float[] set)
  1077. {
  1078. double stageX = (double)set[0];
  1079. if (stageX > StageXMax || stageX < StageXMin)
  1080. {
  1081. return false;
  1082. }
  1083. double stageY = (double)set[1];
  1084. if (stageY > StageYMax || stageY < StageYMin)
  1085. {
  1086. return false;
  1087. }
  1088. double stageZ = (double)set[2];
  1089. if (stageZ > StageZMax || stageZ < StageZMin)
  1090. {
  1091. return false;
  1092. }
  1093. double stageT = (double)set[3];
  1094. if (stageT > StageTMax || stageT < StageTMin)
  1095. {
  1096. return false;
  1097. }
  1098. double stageR = (double)set[4];
  1099. if (stageR > StageRMax || stageR < StageRMin)
  1100. {
  1101. return false;
  1102. }
  1103. var stageDictionary = new Dictionary<Stage, double>
  1104. {
  1105. { Stage.StageX, (double)stageX },
  1106. { Stage.StageY, (double)stageY },
  1107. { Stage.StageZ, (double)stageZ },
  1108. { Stage.StageT, (double)stageT },
  1109. { Stage.StageR, (double)stageR }
  1110. };
  1111. this.microscopeController.SetStageConditions(stageDictionary);
  1112. return true;
  1113. }
  1114. public float GetStageAtX()
  1115. {
  1116. return (float)m_dStageX;
  1117. }
  1118. public float GetStageAtY()
  1119. {
  1120. return (float)m_dStageY;
  1121. }
  1122. public float GetStageAtZ()
  1123. {
  1124. return (float)m_dStageZ;
  1125. }
  1126. public float GetStageAtT()
  1127. {
  1128. return (float)m_dStageT;
  1129. }
  1130. public float GetStageAtR()
  1131. {
  1132. return (float)m_dStageR;
  1133. }
  1134. public Boolean SetStageGotoX(float set)
  1135. {
  1136. double stageX = (double)set;
  1137. if (stageX > StageXMax || stageX < StageXMin)
  1138. {
  1139. return false;
  1140. }
  1141. var stageDictionary = new Dictionary<Stage, double>
  1142. {
  1143. { Stage.StageX, (double)stageX }
  1144. };
  1145. this.microscopeController.SetStageConditions(stageDictionary);
  1146. return true;
  1147. }
  1148. public Boolean SetStageGotoY(float set)
  1149. {
  1150. double stageY = (double)set;
  1151. if (stageY > StageYMax || stageY < StageYMin)
  1152. {
  1153. return false;
  1154. }
  1155. var stageDictionary = new Dictionary<Stage, double>
  1156. {
  1157. { Stage.StageY, (double)stageY }
  1158. };
  1159. this.microscopeController.SetStageConditions(stageDictionary);
  1160. return true;
  1161. }
  1162. public Boolean SetStageGotoZ(float set)
  1163. {
  1164. double stageZ = (double)set;
  1165. if (stageZ > StageZMax || stageZ < StageZMin)
  1166. {
  1167. return false;
  1168. }
  1169. var stageDictionary = new Dictionary<Stage, double>
  1170. {
  1171. { Stage.StageZ, (double)stageZ }
  1172. };
  1173. this.microscopeController.SetStageConditions(stageDictionary);
  1174. return true;
  1175. }
  1176. public Boolean SetStageGotoT(float set)
  1177. {
  1178. double stageT = (double)set;
  1179. if (stageT > StageTMax || stageT < StageTMin)
  1180. {
  1181. return false;
  1182. }
  1183. var stageDictionary = new Dictionary<Stage, double>
  1184. {
  1185. { Stage.StageT, (double)stageT }
  1186. };
  1187. this.microscopeController.SetStageConditions(stageDictionary);
  1188. return true;
  1189. }
  1190. public Boolean SetStageGotoR(float set)
  1191. {
  1192. double stageR = (double)set;
  1193. if (stageR > StageRMax || stageR < StageRMin)
  1194. {
  1195. return false;
  1196. }
  1197. var stageDictionary = new Dictionary<Stage, double>
  1198. {
  1199. { Stage.StageR, (double)stageR }
  1200. };
  1201. this.microscopeController.SetStageConditions(stageDictionary);
  1202. return true;
  1203. }
  1204. private Boolean MoveStage(float x, float y)
  1205. {
  1206. double stageX = (double)x;
  1207. if (stageX > StageXMax || stageX < StageXMin)
  1208. {
  1209. return false;
  1210. }
  1211. double stageY = (double)y;
  1212. if (stageY > StageYMax || stageY < StageYMin)
  1213. {
  1214. return false;
  1215. }
  1216. var stageDictionary = new Dictionary<Stage, double>
  1217. {
  1218. { Stage.StageX, (double)stageX },
  1219. { Stage.StageY, (double)stageY }
  1220. };
  1221. this.microscopeController.SetStageConditions(stageDictionary);
  1222. return true;
  1223. }
  1224. #endregion
  1225. #region 拍图
  1226. //图像扫描尺寸
  1227. public double[] ImageScanSize =
  1228. {
  1229. 32,
  1230. 64,
  1231. 128,
  1232. 256,
  1233. 512,
  1234. 704,
  1235. 768,
  1236. 1024,
  1237. 4096,
  1238. 8192
  1239. };
  1240. private byte[] m_ImageBit = null;
  1241. private long m_nImageWidth = 0;
  1242. private long m_nImageHeight = 0;
  1243. private double m_dImagePixelsize = 0;//it will be initialized when we get an image from the EDS.
  1244. bool m_bAcquistionDone = false;
  1245. //private Bitmap m_Bitmap = null;
  1246. void InitImageAcquisition()
  1247. {
  1248. imageAcquisitionController = AcquireFactory.CreateImageServer();
  1249. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  1250. //imageAcquisitionSettings.
  1251. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  1252. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  1253. }
  1254. void InitXrayAcquistion()
  1255. {
  1256. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  1257. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  1258. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  1259. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  1260. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  1261. quantSettings = ProcessingFactory.CreateSEMQuantSettings();
  1262. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  1263. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  1264. }
  1265. //控制电镜释放
  1266. void CloseImageAcquisition()
  1267. {
  1268. if (imageAcquisitionController != null)
  1269. {
  1270. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  1271. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  1272. imageAcquisitionController = null;
  1273. }
  1274. }
  1275. void CloaseXrayAcquistion()
  1276. {
  1277. if (EdSpectrumAcquisitionController != null)
  1278. {
  1279. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  1280. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  1281. EdSpectrumAcquisitionController = null;
  1282. }
  1283. }
  1284. /// <summary>
  1285. /// OnImageExperimentStarted
  1286. /// </summary>
  1287. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  1288. {
  1289. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1290. log.Info("拍图开始事件!");
  1291. }
  1292. //int m_nState;
  1293. /// <summary>
  1294. /// OnImageExperimentFinished
  1295. /// </summary>
  1296. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  1297. {
  1298. IElectronImage electronImage = e.Value[0];
  1299. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
  1300. {
  1301. //MessageBox.Show("图像采集完成,获取图像像素失败!");
  1302. NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
  1303. }
  1304. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  1305. {
  1306. m_bAcquistionDone = true;
  1307. }
  1308. }
  1309. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
  1310. {
  1311. a_nImageHeight = 0;
  1312. a_nImageWidth = 0;
  1313. a_nPixelSize = 0;
  1314. a_pImageBits = null;
  1315. if (a_electronImage == null)
  1316. {
  1317. return false;
  1318. }
  1319. a_nImageHeight = a_electronImage.Height;
  1320. a_nImageWidth = a_electronImage.Width;
  1321. a_nPixelSize = a_electronImage.PixelSize;
  1322. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  1323. long nImageSize = a_nImageHeight * a_nImageWidth;
  1324. long nBufferSize = nImageSize * nBytesPerPixel;
  1325. Byte[] imageData = new Byte[nBufferSize];
  1326. a_electronImage.GetData(imageData);
  1327. a_pImageBits = new Byte[nImageSize];
  1328. // default, oxford will return short image, we need to convert to byte
  1329. if (nBytesPerPixel == 2)
  1330. {
  1331. int nBSEValue = 0;
  1332. for (int i = 0; i < nImageSize; ++i)
  1333. {
  1334. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  1335. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  1336. }
  1337. }
  1338. else
  1339. {
  1340. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  1341. NLog.LogManager.GetCurrentClassLogger().Error(msg);
  1342. int nOffset = nBytesPerPixel - 1;
  1343. for (int i = 0; i < nImageSize; ++i)
  1344. {
  1345. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  1346. }
  1347. }
  1348. return true;
  1349. }
  1350. void AcquisitionParamInit()
  1351. {
  1352. m_ImageBit = null;
  1353. m_nImageWidth = 0;
  1354. m_nImageHeight = 0;
  1355. m_bAcquistionDone = false;
  1356. Bitmap m_Bitmap = null;
  1357. }
  1358. //a_dDwellTime : 1~100000之间的数
  1359. //a_sImageType : 1: SE, 2: Bse
  1360. //a_dImageScanSize : 图像分辨率,图像的高
  1361. public bool SetImageAcquistionSetting(double a_dDwellTime, ImageInputSources a_nImageType, double a_dImageScanSize)
  1362. {
  1363. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  1364. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  1365. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  1366. if (a_dDwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  1367. {
  1368. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  1369. }
  1370. if (a_dDwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  1371. {
  1372. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  1373. }
  1374. else
  1375. {
  1376. imageSettings.DwellTimeMicroSeconds = a_dDwellTime;
  1377. }
  1378. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  1379. imageSettings.EnableInputSource((ImageInputSources)a_nImageType, true);
  1380. if (!ImageScanSize.Contains(a_dImageScanSize))
  1381. {
  1382. NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
  1383. return false;
  1384. }
  1385. var pixelSize = 1d / a_dImageScanSize;
  1386. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  1387. return true;
  1388. }
  1389. #endregion
  1390. #region X-ray
  1391. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  1392. {
  1393. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1394. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1395. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1396. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1397. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1398. // EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateFullFieldRegion(1.0 / 1024.0);
  1399. }
  1400. void SetAreaXrayAcquisitionParam(IEdChordListSettings EdSpectrumSettings, double a_dMilliSecondsTime)
  1401. {
  1402. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  1403. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  1404. EdSpectrumSettings.EdSettings.ProcessTime = 4;
  1405. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  1406. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  1407. //EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateFullFieldRegion(1.0 / 1024.0);
  1408. }
  1409. void SetPointAcquistionRegion(double a_nX, double a_nY)
  1410. {
  1411. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(a_nX, a_nY));
  1412. }
  1413. void SetAreaAcquistionRegion(List<Chord> a_nChords)
  1414. {
  1415. ChordList chordsList = null;
  1416. chordsList = new ChordList(a_nChords, m_dImagePixelsize);
  1417. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  1418. }
  1419. /// <summary>
  1420. /// Called when IEdSpectrumAcquisitionController Experiment Starting.
  1421. /// </summary>
  1422. /// <param name="sender">The sender.</param>
  1423. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  1424. private void OnEdSpectrumExperimentStarting(object sender, AcquisitionStartingEventArgs<IEdSpectrum> e)
  1425. {
  1426. }
  1427. /// <summary>
  1428. /// Called when IEdSpectrumAcquisitionController Experiment Started.
  1429. /// </summary>
  1430. /// <param name="sender">The sender.</param>
  1431. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  1432. private void OnEdSpectrumExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdSpectrum> e)
  1433. {
  1434. }
  1435. private IEdChordListSettings GetChordlistSettings()
  1436. {
  1437. if (_edsChordListSetting == null)
  1438. {
  1439. _edsChordListSetting = AcquireFactory.CreateEdChordListSettings();
  1440. if (_edsChordListSetting != null)
  1441. {
  1442. _edsChordListSetting.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime;
  1443. _edsChordListSetting.EdSettings.NumberOfChannels = 1024;
  1444. _edsChordListSetting.EdSettings.ProcessTime = g_nOxfordControllerProcessTime;
  1445. _edsChordListSetting.EdSettings.EnergyRange = g_nOxfordControllerEnergyRange;
  1446. _edsChordListSetting.AutoIdSettings.SetKnownElement(79, true);
  1447. _edsChordListSetting.NumberOfProcessingThreads = 1;
  1448. }
  1449. else
  1450. {
  1451. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1452. log.Error("Create EDS chordlist setting failed.");
  1453. }
  1454. }
  1455. return _edsChordListSetting;
  1456. }
  1457. private IEdChordListAcquisitionController CreateChordlistController()
  1458. {
  1459. if (_edsChordListController == null)
  1460. {
  1461. _edsChordListController = AcquireFactory.CreateEdChordListServer();
  1462. _edsChordListController.PixelProcessed += new EventHandler<OINA.Extender.EventArgs<OINA.Extender.Processing.Quant.IPixelSEMQuantStatus>>(this.OnController_PixelProcessed);
  1463. _edsChordListController.ExperimentFinished += new EventHandler<OINA.Extender.Acquisition.AcquisitionFinishedEventArgs>(this.OnController_ExperimentFinished);
  1464. var edsChordListSettings = GetChordlistSettings();
  1465. while (true)
  1466. {
  1467. if (_edsChordListController.IsEdHardwareReady(edsChordListSettings))
  1468. {
  1469. if (edsChordListSettings.EdCapabilities.HasHardwareConnection)
  1470. {
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. }
  1476. return _edsChordListController;
  1477. }
  1478. struct Element
  1479. {
  1480. public int m_nAotomaticNo;
  1481. public double m_dWeight;
  1482. }
  1483. private struct OxfordXrayData
  1484. {
  1485. public long m_nPosX;
  1486. public long m_nPosY;
  1487. public static readonly long[] m_pXrayData = new long[(int)EDSConst.XANA_CHANNELS];
  1488. public static readonly char[] vs = new char[1024];
  1489. public static readonly OxfordChord[] m_ChordList = new OxfordChord[1024];
  1490. public long m_nChordNum;
  1491. public long m_nPixelNum;
  1492. internal char[] m_strElementResult;
  1493. }
  1494. private void OnController_ExperimentFinished(Object sender, OINA.Extender.Acquisition.AcquisitionFinishedEventArgs e)
  1495. {
  1496. if (m_nCollectedXrayCounts == m_nXrayDataCount)
  1497. {
  1498. m_nState = OxfordControllerState.SUCCEEDED;
  1499. }
  1500. else
  1501. {
  1502. m_nState = OxfordControllerState.FAILED;
  1503. }
  1504. m_endControllerEvent.Set();
  1505. }
  1506. /// <summary>
  1507. ///
  1508. /// </summary>
  1509. /// <param name="sender"></param>
  1510. /// <param name="e"></param>
  1511. private void OnController_PixelProcessed(object sender, EventArgs<IPixelSEMQuantStatus> e)
  1512. {
  1513. m_nCollectedPixelCounts = m_nCollectedPixelCounts++;
  1514. //更新当前feature的元素成分
  1515. //获取单点的元素结果
  1516. var ie = e.Value.QuantStatus.Results.GetEnumerator();
  1517. while (ie.MoveNext())
  1518. {
  1519. ISEMQuantResult result = ie.Current;
  1520. if (result.WeightPercent != 0)
  1521. {
  1522. Element Quant = new Element();
  1523. Quant.m_nAotomaticNo = result.AtomicNumber;
  1524. Quant.m_dWeight = result.WeightPercent;
  1525. if (m_listElementResult == null)
  1526. {
  1527. m_listElementResult = new List<Element>
  1528. {
  1529. Quant
  1530. };
  1531. }
  1532. else
  1533. {
  1534. bool bFind = false;
  1535. for (int i = 0; i < m_listElementResult.Count; i++)
  1536. {
  1537. Element element = m_listElementResult[i];
  1538. if (Quant.m_nAotomaticNo == element.m_nAotomaticNo)
  1539. {
  1540. element.m_dWeight += Quant.m_dWeight;
  1541. bFind = true;
  1542. }
  1543. }
  1544. if (!bFind)
  1545. {
  1546. m_listElementResult.Add(Quant);
  1547. }
  1548. }
  1549. }
  1550. }
  1551. //当前的chord数据中pixel没有采集完整
  1552. if (m_nCollectedPixelCounts == m_nPixelNum)
  1553. {
  1554. //形成quant数据
  1555. String Quant = "";
  1556. for (int i = 0; i < m_listElementResult.Count; i++)
  1557. {
  1558. Element result = m_listElementResult[i];
  1559. Quant += "Quant=";
  1560. String strAoto = ElementProperties.GetElementSymbol(result.m_nAotomaticNo);
  1561. Quant += strAoto;
  1562. Quant += ",";
  1563. Quant += "K-serials";
  1564. Quant += ",";
  1565. String strWeight = (result.m_dWeight / Convert.ToDouble(m_nPixelNum)).ToString();
  1566. Quant += strWeight;
  1567. Quant += "\n";
  1568. }
  1569. String msg = Quant;
  1570. char[] dst = m_pXrayData[m_nCollectedXrayCounts].m_strElementResult;
  1571. for (int i = 0; i < msg.Length; i++)
  1572. {
  1573. dst[i] = (char)msg[i];
  1574. }
  1575. m_nCollectedXrayCounts++;
  1576. if (m_nCollectedXrayCounts < m_nXrayDataCount)
  1577. {
  1578. m_nPixelNum = m_pXrayData[m_nCollectedXrayCounts].m_nPixelNum;
  1579. m_nCollectedPixelCounts = 0;
  1580. m_listElementResult.Clear();
  1581. }
  1582. }
  1583. }
  1584. /// <summary>
  1585. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  1586. /// </summary>
  1587. /// <param name="sender">sender object</param>
  1588. /// <param name="e">The instance containing the event data.</param>
  1589. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  1590. {
  1591. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  1592. uint[] m_XrayData;
  1593. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  1594. IEdSpectrum edSpectrum = e.Value;
  1595. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  1596. {
  1597. NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
  1598. }
  1599. long nXraycount = 0;
  1600. for (int i = 0; i < 2000; i++)
  1601. {
  1602. nXraycount += m_XrayData[i];
  1603. }
  1604. //Quantify processing
  1605. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  1606. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1607. quantSettings.CombinedElement = 8;
  1608. quantSettings.Normalised = true;
  1609. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  1610. IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
  1611. var ie = Results.GetEnumerator();
  1612. var m_listElement = new Dictionary<string, double>();
  1613. while (ie.MoveNext())
  1614. {
  1615. ISEMQuantResult result = ie.Current;
  1616. if (result.WeightPercent != 0)
  1617. {
  1618. m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
  1619. }
  1620. }
  1621. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  1622. {
  1623. if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
  1624. {
  1625. currentCommand.pointXrayPrm.XrayData = m_XrayData;
  1626. currentCommand.pointXrayPrm.listElement = m_listElement;
  1627. m_bXrayDone = true;
  1628. }
  1629. else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
  1630. {
  1631. currentCommand.areaXrayPrm.XrayData = m_XrayData;
  1632. currentCommand.areaXrayPrm.listElement = m_listElement;
  1633. m_bXrayDone = true;
  1634. }
  1635. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
  1636. {
  1637. var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
  1638. curXrayData.XrayData = m_XrayData;
  1639. curXrayData.listElement = m_listElement;
  1640. currentCommand.PointXrayDataReceived += 1;
  1641. if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
  1642. {
  1643. m_bXrayDone = true;
  1644. }
  1645. }
  1646. else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
  1647. {
  1648. var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
  1649. curXrayData.XrayData = m_XrayData;
  1650. curXrayData.listElement = m_listElement;
  1651. currentCommand.AreaXrayDataReceived += 1;
  1652. if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
  1653. {
  1654. m_bXrayDone = true;
  1655. }
  1656. }
  1657. }
  1658. }
  1659. bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
  1660. {
  1661. a_pSpectrumData = new uint[a_nBufferSize];
  1662. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  1663. a_spectrum.GetChannelData(xrayData);
  1664. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  1665. int nChannelStart = 0;
  1666. if (dZeroChannelValue < 0) // zero channel value should less than zero
  1667. {
  1668. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  1669. }
  1670. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  1671. double dStep1 = 1.0 / nDataLength;
  1672. double dStep2 = 1.0 / a_nBufferSize;
  1673. for (int i = 0; i < nDataLength; ++i)
  1674. {
  1675. uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
  1676. double dBinPos = i * dStep1;
  1677. long nLeftBin = (long)(dBinPos / dStep2);
  1678. // calculate % into left bin
  1679. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  1680. // calculate data into the left bin
  1681. uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
  1682. // put data into bins
  1683. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  1684. if ((nLeftBin + 1) < a_nBufferSize)
  1685. {
  1686. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  1687. }
  1688. }
  1689. return true;
  1690. }
  1691. /// <summary>
  1692. /// Called when IEdSpectrum DataChanged
  1693. /// </summary>
  1694. /// <param name="sender">Event source</param>
  1695. /// <param name="e">Event arguments</param>
  1696. private void OnDataChanged(object sender, EventArgs e)
  1697. {
  1698. }
  1699. public bool IsAcquiringSpectrum()
  1700. {
  1701. return EdSpectrumAcquisitionController.IsAcquiring;
  1702. }
  1703. public void BeginMultipleAquisition()
  1704. {
  1705. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1706. }
  1707. public void EndMultipleAquisition()
  1708. {
  1709. EdSpectrumAcquisitionController.EndMultipleAcquisition();
  1710. }
  1711. public void BeginAreaModeMultipleAquisition()
  1712. {
  1713. var controller = CreateChordlistController();
  1714. controller.BeginMultipleAcquisition();
  1715. }
  1716. public void EndAreaModeMultipleAquisition()
  1717. {
  1718. var controller = CreateChordlistController();
  1719. controller.EndMultipleAcquisition();
  1720. }
  1721. public bool GetSemBeamOn()
  1722. {
  1723. var beamon = microscopeController.ColumnConditions.BeamOn;
  1724. if (beamon == 1)
  1725. {
  1726. return true;
  1727. }
  1728. else
  1729. {
  1730. return false;
  1731. }
  1732. }
  1733. public void StopXrayAquisition()
  1734. {
  1735. EdSpectrumAcquisitionController.StopAcquisition();
  1736. }
  1737. #endregion
  1738. }
  1739. }