Extender.cs 58 KB

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