Extender.cs 60 KB

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