Extender.cs 60 KB

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