Extender.cs 58 KB

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