Extender.cs 60 KB

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