Extender.cs 57 KB

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