Extender.cs 58 KB

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