Extender.cs 70 KB

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