Extender.cs 61 KB

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