Extender.cs 58 KB

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