Extender.cs 57 KB

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