Extender.cs 58 KB

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