Extender.cs 60 KB

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