CSmplMeasure.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OTSModelSharp.ServiceInterface;
  7. using static OTSDataType.otsdataconst;
  8. using OTSDataType;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.Threading;
  12. using OTSModelSharp.DTLBase;
  13. using OTSCLRINTERFACE;
  14. namespace OTSModelSharp
  15. {
  16. using CHoleBSEImgsList = List<CHoleBSEImg>;
  17. class CSmplMeasure
  18. {
  19. protected static NLog.Logger log ;
  20. protected bool bSaveThreadWorking;
  21. protected System.Threading.Thread m_thread_ptr;
  22. protected COTSSample m_Sample;
  23. protected CMsrThread m_pMsrThread;
  24. string m_strWorkingFolder;
  25. protected CSmplMsrResult m_pSampleRstFile;
  26. CHoleBSEImgsList m_listHoleBSEImg;
  27. //CFunExportClass loger;
  28. protected SemController m_SemHardwareMgr;
  29. protected ScanController m_ScanHardwareMgr;
  30. protected EDSController m_EDSHardwareMgr;
  31. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  32. protected COTSFieldData curFldData;
  33. protected IClassifyEngine m_classifyEngine;
  34. public CSmplMeasure()
  35. {
  36. log = NLog.LogManager.GetCurrentClassLogger();
  37. m_pSampleRstFile = new CSmplMsrResult();
  38. m_SemHardwareMgr = new SemController();
  39. m_ScanHardwareMgr = new ScanController();
  40. m_EDSHardwareMgr = new EDSController();
  41. m_listHoleBSEImg = new CHoleBSEImgsList();
  42. }
  43. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  44. {
  45. log = NLog.LogManager.GetCurrentClassLogger();
  46. m_strWorkingFolder = a_strWorkingFolder;
  47. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);// new CSmplMsrResultFile(m_strWorkingFolder,a_pSample);
  48. m_SemHardwareMgr = new SemController();
  49. m_ScanHardwareMgr = new ScanController();
  50. m_EDSHardwareMgr = new EDSController();
  51. m_listHoleBSEImg = new CHoleBSEImgsList();
  52. m_Sample = a_pSample;
  53. }
  54. public void SetSample(COTSSample a_pSample)
  55. {
  56. m_Sample = a_pSample;
  57. m_pSampleRstFile.SetSample(a_pSample);
  58. }
  59. public void SetMsrThread(CMsrThread mt)
  60. {
  61. m_pMsrThread = mt;
  62. }
  63. bool SetSEMDataMrs()
  64. {
  65. var pSEMDataMsr = m_Sample.GetSEMDataMsr();
  66. //ASSERT(pSEMDataMsr);
  67. double dMag = pSEMDataMsr.GetMagnification();
  68. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  69. //CSemBasePtr pSEMCtrlPtr = GetSEMControl();
  70. var pSEMCtrl = m_pMsrThread.GetSEMController();
  71. // get SEM controller
  72. if (!pSEMCtrl.IsConnected())
  73. {
  74. if (!pSEMCtrl.Connect())
  75. {
  76. log.Error("SetSEMDataMrs: can't connect SEM.");
  77. return false;
  78. }
  79. }
  80. log.Info("set magnification:" + dMag.ToString());
  81. log.Info("set Working Distance:" + dWorkDis.ToString());
  82. pSEMCtrl.SetMagnification(dMag);
  83. pSEMCtrl.SetWorkingDistance(dWorkDis);
  84. return true;
  85. }
  86. bool SetSEMExteralOn()
  87. {
  88. var pSEMCtrl = m_SemHardwareMgr;
  89. // get SEM controller
  90. if (!pSEMCtrl.IsConnected())
  91. {
  92. if (!pSEMCtrl.Connect())
  93. {
  94. log.Error("SetSEMExteralOn: can't connect SEM.");
  95. return false;
  96. }
  97. }
  98. log.Warn("Set Scan Exteral on!");
  99. pSEMCtrl.SetScanExternal(true);
  100. return true;
  101. }
  102. // set SEM external off
  103. bool SetSEMExteralOff()
  104. {
  105. var pSEMCtrlPtr = m_SemHardwareMgr;
  106. // get SEM controller
  107. if (!pSEMCtrlPtr.IsConnected())
  108. {
  109. if (!pSEMCtrlPtr.Connect())
  110. {
  111. log.Error("SetSEMExteralOff: can't connect SEM.");
  112. return false;
  113. }
  114. }
  115. log.Warn("Set SEM Exteral Off!");
  116. pSEMCtrlPtr.SetScanExternal(false);
  117. return true;
  118. }
  119. bool SetBSEParam()
  120. {
  121. // get SEM controller
  122. var pSEMController = m_SemHardwareMgr;
  123. // get scan controller
  124. var pScanController = m_ScanHardwareMgr;
  125. // scan parameters
  126. var pMsrParam = m_Sample.GetMsrParams();
  127. var pImgScanParam = pMsrParam.GetImageScanParam();
  128. // get image size
  129. var nImageSizeId = pImgScanParam.GetImagePixelSize();
  130. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  131. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  132. // get SEM controller
  133. if (!pSEMController.IsConnected())
  134. {
  135. if (!pSEMController.Connect())
  136. {
  137. log.Error("SetBSEParam: can't connect SEM.");
  138. return false;
  139. }
  140. }
  141. // get dwell time
  142. OTS_THREE_TIES_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  143. // convert dwell time to bruker dwell time (8, 16, 32)
  144. int nBrukerDwellTimeId=3;
  145. switch (nDwellTime)
  146. {
  147. case OTS_THREE_TIES_OPTIONS.low:
  148. nBrukerDwellTimeId = 3;
  149. break;
  150. case OTS_THREE_TIES_OPTIONS.meddium:
  151. nBrukerDwellTimeId = 4;
  152. break;
  153. case OTS_THREE_TIES_OPTIONS.high:
  154. nBrukerDwellTimeId = 5;
  155. break;
  156. default:
  157. nBrukerDwellTimeId = 3;
  158. break;
  159. }
  160. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  161. if (!pScanController.Init())
  162. {
  163. log.Error("SetBSEParam: failed to get scan control.");
  164. return false;
  165. }
  166. // set dwell time
  167. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  168. {
  169. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  170. return false;
  171. }
  172. // set image size
  173. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  174. {
  175. // failed to set dwell time
  176. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  177. return false;
  178. }
  179. return true;
  180. }
  181. bool SetHoleBSEParam()
  182. {
  183. // get SEM controller
  184. var pSEMController = m_SemHardwareMgr;
  185. // get scan controller
  186. var pScanController = m_ScanHardwareMgr;
  187. // scan parameters
  188. var pMsrParam = m_Sample.GetMsrParams();
  189. var pImgScanParam = pMsrParam.GetImageScanParam();
  190. // get image size
  191. var nImageSizeId = pImgScanParam.GetImagePixelSize();
  192. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  193. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  194. // get SEM controller
  195. if (!pSEMController.IsConnected())
  196. {
  197. if (!pSEMController.Connect())
  198. {
  199. log.Error("SetBSEParam: can't connect SEM.");
  200. return false;
  201. }
  202. }
  203. // get dwell time
  204. OTS_THREE_TIES_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  205. // convert dwell time to bruker dwell time (6, 16, 32)
  206. long nBrukerDwellTime = DWELLTIME_BRUKER_VALUES[2];// choose the third option, so the dwell time will be 4 . there's no need to change here. it shoud be a const.
  207. if (!pScanController.Init())
  208. {
  209. log.Error("SetBSEParam: failed to get scan control.");
  210. return false;
  211. }
  212. // set dwell time
  213. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  214. {
  215. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  216. return false;
  217. }
  218. // set image size
  219. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  220. {
  221. // failed to set dwell time
  222. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  223. return false;
  224. }
  225. return true;
  226. }
  227. public void SetWorkingFolder(String a_strWorkingFolder)
  228. {
  229. // add "\\" at the string end if it is not "\\"
  230. if (a_strWorkingFolder.PadRight(1)!="\\")
  231. {
  232. a_strWorkingFolder += "\\";
  233. }
  234. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  235. }
  236. bool CalculateUnMeasuredFieldsCenters(out List<System.Drawing.Point> a_allpieldcenter,out List<System.Drawing.Point> a_listFieldCenter)
  237. {
  238. // sample measure parameters
  239. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  240. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  241. CSEMDataMsr poSEMDataMsr = m_Sample.GetSEMDataMsr();
  242. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  243. // measured field centers list
  244. List<System.Drawing.Point> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  245. // field centers list manager
  246. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  247. // init field centers list manager
  248. if (!pFieldMgr.Init(m_Sample.GetMsrArea(), poImageScanParam, poSEMDataMsr, listCompletedCenter))
  249. {
  250. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  251. a_listFieldCenter = new List<System.Drawing.Point>();
  252. a_allpieldcenter = new List<Point>();
  253. return false;
  254. }
  255. // get field centers list
  256. a_listFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  257. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  258. // ok, return TRUE
  259. return true;
  260. }
  261. protected bool IsAborted()
  262. {
  263. //Debug.Assert(m_pMsrThread == null);
  264. return m_pMsrThread.IsMeasureStopped();
  265. }
  266. bool IsSampleOver(COTSImgScanPrm a_pScanParam, int a_nTotalFields)
  267. {
  268. int nStopMode = (int)a_pScanParam.GetStopMode();
  269. int nStopField = a_pScanParam.GetStopParamFields();
  270. // completed fields number
  271. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  272. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  273. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  274. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  275. int nNumParticle = 0;
  276. foreach (var pResult in listMsrResult)
  277. {
  278. if (pResult.GetTypeId() > (int)OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY)//summarize the number of the identified particle in this condition
  279. {
  280. nNumParticle += (int)pResult.GetNumber();
  281. }
  282. }
  283. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  284. int nDay = timeSpan.Days;
  285. int nHour = timeSpan.Hours;
  286. int nMin = timeSpan.Minutes;
  287. int nSec = timeSpan.Seconds;
  288. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  289. int nParticlAim = a_pScanParam.GetStopParamParticles();
  290. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  291. bool bRet = true;
  292. switch (nStopMode)
  293. {
  294. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  295. // completed fields number
  296. if (nCompeltedField < a_nTotalFields)
  297. {
  298. bRet = false;
  299. }
  300. break;
  301. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  302. if (nCompeltedField < nStopField)
  303. {
  304. bRet = false;
  305. }
  306. break;
  307. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  308. if (nNumParticle < nParticlAim)
  309. {
  310. bRet = false;
  311. }
  312. break;
  313. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  314. if (nUsedTime < nMeasTimeAim)
  315. {
  316. bRet = false;
  317. }
  318. break;
  319. default:
  320. break;
  321. }
  322. return bRet;
  323. }
  324. // move SEM to the point
  325. bool MoveSEMToPoint(System.Drawing.Point a_poi)
  326. {
  327. // get SEM controller
  328. var pSEMController = m_SemHardwareMgr;
  329. Point a_SEMpt = new Point();
  330. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  331. if (!a_pCSEMStageData.ConverOTSToSEMPoint(a_poi, ref a_SEMpt))
  332. {
  333. return false;
  334. }
  335. if (!pSEMController.IsConnected())
  336. {
  337. if (!pSEMController.Connect())
  338. {
  339. log.Error("MoveSEMToPoint: can't connect SEM.");
  340. return false;
  341. }
  342. }
  343. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  344. Thread.Sleep(100);
  345. // move SEM to the position (rotation 0)
  346. if (!pSEMController.MoveSEMToPoint(a_SEMpt, 0))
  347. {
  348. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  349. return false;
  350. }
  351. return true;
  352. }
  353. // Acquire a BSE image
  354. CBSEImgClr AcquireABSEImage()
  355. {
  356. // BSE image
  357. CBSEImgClr pBSEImage = null;
  358. // get scan controller
  359. var pScanController = m_ScanHardwareMgr;
  360. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  361. return pBSEImage;
  362. }
  363. public virtual bool FieldImageProcess(Point fldCenter, CBSEImgClr imageData)
  364. {
  365. return true;
  366. }
  367. public virtual void ClassifyFieldParticles()
  368. {
  369. return;
  370. }
  371. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  372. {
  373. return;
  374. }
  375. private class AutoResetSEMControl:IDisposable
  376. {
  377. CSmplMeasure sm;
  378. public AutoResetSEMControl(CSmplMeasure s)
  379. {
  380. sm = s;
  381. }
  382. public void Dispose()
  383. {
  384. sm.SetSEMExteralOff();
  385. //sm.ResetScan();
  386. }
  387. }
  388. public void DoMeasureForOneSample()
  389. {
  390. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  391. {
  392. // let the main thread to know that this sample measurement starts
  393. var pStatus = m_Sample.GetMsrStatus();
  394. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  395. // set current time to current time
  396. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  397. // let main App know that the sample begin to measure
  398. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  399. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  400. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  401. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  402. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  403. log.Info(m_Sample.GetName() + " Measurement started!");
  404. // get SEM controller to set magnification and working distance
  405. if (!SetSEMDataMrs())
  406. {
  407. log.Error("DoMeasure: fail to set SEM data.");
  408. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  409. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  410. return;
  411. }
  412. // get SEM external controll on
  413. if (!SetSEMExteralOn())
  414. {
  415. log.Error("DoMeasure: fail to set SEM external.");
  416. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  417. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  418. return;
  419. }
  420. // set the BSE scan param
  421. if (!SetBSEParam())
  422. {
  423. log.Error("DoMeasure: fail to set BSE param.");
  424. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  425. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  426. return;
  427. }
  428. var pSEMDataGnr = new CSEMDataGnr();
  429. log.Info("Get Kv, Brightness and Contrast!");
  430. m_SemHardwareMgr.GetSEMDataGnrFromHw(ref pSEMDataGnr);
  431. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  432. // record SEM data
  433. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  434. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  435. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  436. // record stage
  437. CStage pStage = pProjMgrFile.GetStage();
  438. m_pSampleRstFile.SetSEMStage(pStage);
  439. // calculate field centers
  440. List<System.Drawing.Point> umMeasuredlistFieldCenter;
  441. List<System.Drawing.Point> alllistFieldCenter;
  442. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out umMeasuredlistFieldCenter))
  443. {// failed to calculate field centers
  444. log.Error("DoMeasure: failed to calculate field centers.");
  445. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  446. // record end time
  447. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  448. return;
  449. }
  450. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  451. log.Info("Create result file!");
  452. if (!m_pSampleRstFile.CreateResultFiles(alllistFieldCenter))
  453. {// failed to call measure result file Save method
  454. log.Error("DoMeasure: failed to call measure result file Save method.");
  455. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  456. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  457. return;
  458. }
  459. //------
  460. int nNewFieldId = 0;
  461. int numOfAllFields = pStatus.GetCompletedFields() + umMeasuredlistFieldCenter.Count;//
  462. int completedFields = pStatus.GetCompletedFields();
  463. log.Info("Unmeasured fields:" + umMeasuredlistFieldCenter.Count);
  464. for (int i = 0; i < (int)umMeasuredlistFieldCenter.Count; ++i)
  465. {// check and break if stop button is clicked
  466. if (IsAborted())
  467. {// measure stopped
  468. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  469. // record end time
  470. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  471. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  472. while (fieldQueue.Count() > 0)
  473. {
  474. Thread.Sleep(100);
  475. }
  476. return;
  477. }
  478. // check if sample measurement completes
  479. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  480. int nTotalFieldSize = (int)umMeasuredlistFieldCenter.Count;
  481. if (IsSampleOver(pScanParam, numOfAllFields))
  482. {
  483. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  484. break;
  485. }
  486. // get a field center
  487. System.Drawing.Point poiFieldCentre = umMeasuredlistFieldCenter[i];
  488. // update thread measure status class, let the main thread know that starts a new field
  489. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  490. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  491. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  492. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  493. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  494. int fldNo = completedFields + i + 1;
  495. log.Warn("Current field:" + fldNo.ToString());
  496. // move SEM to the field center
  497. if (!MoveSEMToPoint(poiFieldCentre))
  498. {// failed to move SEM to the position
  499. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  500. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  501. // record end time
  502. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  503. return;
  504. }
  505. log.Info("Begin to Acquire BSE image!");
  506. // take BSE image for the fields
  507. CBSEImgClr pBSEImg = AcquireABSEImage();
  508. // let the main thread to know that image process is completed
  509. if (pBSEImg == null)
  510. {
  511. log.Error("ImageProcess: can't get no background image.");
  512. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  513. return;
  514. }
  515. //BSEData
  516. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  517. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  518. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  519. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = poiFieldCentre.X;
  520. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = poiFieldCentre.Y;
  521. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  522. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  523. byte[] pImgData = pBSEImg.GetImageDataPtr();
  524. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  525. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  526. // get a new field id
  527. log.Info("Acquire BSE image success! Processing image...");
  528. // image process
  529. var rst = FieldImageProcess(poiFieldCentre, pBSEImg);
  530. if (rst == true)
  531. {
  532. // add the field into the field
  533. m_pSampleRstFile.AddAField(curFldData);
  534. }
  535. ++nNewFieldId;
  536. double measuredArea = 0; // this area should be the field area
  537. var a_pBSEImg = curFldData.GetBSEImage();
  538. double dPixelSize = m_Sample.CalculatePixelSize();
  539. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  540. if (!CumulateFieldData(curFldData.ListAnalysisParticles, measuredArea))
  541. { // failed to call SaveFieldFile method
  542. log.Error("ImageProcess: call CumulateFieldData method.");
  543. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  544. return;
  545. }
  546. log.Info("Send field data to screen!");
  547. // completed fields
  548. pStatus.SetCompletedFields(pStatus.GetCompletedFields() + 1);
  549. // completed fieldCenter
  550. List<System.Drawing.Point> listCpltedCenter = pStatus.GetCompletedFieldsCenter();
  551. listCpltedCenter.Add(poiFieldCentre);
  552. //Field Data
  553. // record end time
  554. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  555. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  556. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  557. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  558. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  559. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = numOfAllFields;
  560. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFldData.ListAnalysisParticles.Count;
  561. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  562. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  563. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  564. }
  565. while (fieldQueue.Count > 0)//wait untill all the field data has been saved.
  566. {
  567. Thread.Sleep(100);
  568. }
  569. //merging particles
  570. log.Info("Merging big particles on the field edge!");
  571. CImageHandler imgpro = new CImageHandler();
  572. int scanfldsize = m_Sample.GetSEMDataMsr().GetScanFieldSize();
  573. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  574. imgpro.MergeBigBoundaryParticles(m_pSampleRstFile.GetFieldData(), m_Sample.CalculatePixelSize(), scanfldsize, m_Sample.GetResolutionSize(), ref mergedParticles);
  575. ClassifyMergedParticles(mergedParticles);
  576. log.Warn("begin particle data db saving...");
  577. SaveMergedParticles(mergedParticles);
  578. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  579. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  580. // let main thread to know that this sample measurement completes
  581. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  582. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  583. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  584. MsgSmplEnd.STMSampleStu.cSampleName = m_Sample.GetName();
  585. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  586. }
  587. }
  588. public void DoHolePreview(int a_nHoleID)
  589. {
  590. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  591. {
  592. // let the main thread to know that this sample measurement starts
  593. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  594. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  595. // set current time to current time
  596. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  597. // let main App know that the sample begin to measure
  598. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  599. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  600. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  601. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  602. // get SEM controller to set magnification and working distance
  603. if (!SetSEMDataMrs())
  604. {
  605. log.Error("DoHolePreview: fail to set SEM data.");
  606. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  607. // record end time
  608. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  609. return;
  610. }
  611. // get SEM external controll on
  612. if (!SetSEMExteralOn())
  613. {
  614. log.Error("DoMeasure: fail to set SEM external.");
  615. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  616. // record end time
  617. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  618. return;
  619. }
  620. // set the BSE scan param
  621. if (!SetHoleBSEParam())
  622. {
  623. log.Error("DoMeasure: fail to set BSE param.");
  624. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  625. // record end time
  626. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  627. return;
  628. }
  629. // check if measurement is aborted
  630. if (IsAborted())
  631. {// measurement aborted
  632. log.Trace("DoHolePreview: measurement aborted before get SEM condition.");
  633. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  634. // record end time
  635. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  636. return;
  637. }
  638. // calculate field centers
  639. List<System.Drawing.Point> listFieldCenter=new List<System.Drawing.Point>();
  640. List<System.Drawing.Point> alllistFieldCenter = new List<System.Drawing.Point>();
  641. // listFieldCenter.clear();
  642. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out listFieldCenter))
  643. {// failed to calculate field centers
  644. log.Error("DoHolePreview: failed to calculate field centers.");
  645. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  646. // record end time
  647. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  648. return;
  649. }
  650. // go through each field
  651. int nNewFieldId = 0;
  652. for ( int i = 0; i < listFieldCenter.Count; ++i)
  653. {// check and break if stop button is clicked
  654. if (IsAborted())
  655. {// measure stopped
  656. log.Trace("DoHolePreview: measure thread is stopped.");
  657. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  658. // record end time
  659. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  660. return;
  661. }
  662. // check if sample measurement completes
  663. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  664. int nTotalFieldSize = listFieldCenter.Count;
  665. if (IsSampleOver(pScanParam, nTotalFieldSize))
  666. {
  667. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  668. break;
  669. }
  670. // get a field center
  671. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  672. // update thread measure status class, let the main thread know that starts a new field
  673. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  674. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  675. //memset(&MsgFieldStart, 0, sizeof(ST_MSTMsg));
  676. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  677. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  678. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  679. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  680. // move SEM to the field center
  681. if (!MoveSEMToPoint(poiFieldCentre))
  682. {// failed to move SEM to the position
  683. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  684. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  685. // record end time
  686. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  687. return;
  688. }
  689. // take BSE image for the fields
  690. CBSEImgClr pBSEIamge = AcquireABSEImage();
  691. if (pBSEIamge==null)
  692. {
  693. // failed to acquire a BSE image
  694. log.Error("DoHolePreview: failed to acquire a BSE image.");
  695. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  696. // record end time
  697. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  698. return;
  699. }
  700. //BSEData
  701. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  702. // memset(&MsgFieldBSE, 0, sizeof(ST_MSTMsg));
  703. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  704. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  705. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  706. //MsgFieldBSE.STMSampleRetData.BSEData.pos.y = poiFieldCentre.y;
  707. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  708. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  709. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  710. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  711. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  712. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  713. {
  714. // measurement failed or stopped
  715. log.Error("DoHolePreview: measurement failed or stopped.");
  716. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  717. // record end time
  718. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  719. return;
  720. }
  721. //save the result to project file
  722. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg();
  723. pHoleBSEImg.SetHoleID(a_nHoleID);
  724. pHoleBSEImg.SetPosition(poiFieldCentre);
  725. Rectangle oImageRect =(Rectangle) pBSEIamge.GetImageRect();
  726. int nImageSize = pBSEIamge.GetHeight() * pBSEIamge.GetWidth();
  727. pHoleBSEImg.SetImageRect(oImageRect);
  728. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  729. m_listHoleBSEImg.Add(pHoleBSEImg);
  730. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  731. pProjMgrFile.SetHoleBESImgList(m_listHoleBSEImg,true);
  732. // prepare for the next
  733. ++nNewFieldId;
  734. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  735. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  736. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  737. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  738. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  739. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  740. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  741. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  742. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  743. //break;
  744. }
  745. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  746. //calculate measure time
  747. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  748. // let main thread to know that this sample measurement completes
  749. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  750. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  751. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  752. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  753. }
  754. }
  755. // Cumulate field data info
  756. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  757. {
  758. // get measure result items of the sample
  759. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  760. // cumulate field data info
  761. //pMsrResults.CumulateMeasureResults(listResultItems);
  762. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  763. // cumulate ratio
  764. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  765. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  766. pMsrResults.SetRatio(dRatio);
  767. // go through the particles list
  768. foreach (COTSParticleClr pParticle in listParticles)
  769. {
  770. // create a measure result item
  771. pMsrResults.CumulateMeasureResult(pParticle);
  772. }
  773. // ok, return TRUE
  774. return true;
  775. }
  776. // hole BSE images list
  777. void SetHoleBESImgList(CHoleBSEImgsList a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  778. {// clear the hole BSE image list if necessary
  779. if (a_bClear)
  780. {
  781. m_listHoleBSEImg.Clear();
  782. }
  783. // go through the input list
  784. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  785. {// add the new hole BSE image into HoleBSEImage list
  786. m_listHoleBSEImg.Add(pHoleBSEImg);
  787. }
  788. }
  789. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  790. {
  791. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  792. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  793. foreach (COTSParticleClr part in mergedParts)
  794. {
  795. mergedpartdb.SaveAParticle(part, part.GetXray(), new System.Drawing.Point(0, 0));
  796. }
  797. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  798. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  799. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  800. foreach (COTSParticleClr part in mergedParts)
  801. {
  802. ches.Add(part.GetXray());
  803. }
  804. xraydb.SaveElementChemistriesList(ches);
  805. return true;
  806. }
  807. }
  808. }