CSmplMeasure.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 CMeasure 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(CMeasure 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.GetImageResulotion();
  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_IMAGE_SCANSPEED_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_IMAGE_SCANSPEED_OPTIONS.low:
  148. nBrukerDwellTimeId = 3;
  149. break;
  150. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  151. nBrukerDwellTimeId = 4;
  152. break;
  153. case OTS_IMAGE_SCANSPEED_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.GetImageResulotion();
  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_IMAGE_SCANSPEED_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. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  332. if (!a_pCSEMStageData.ConverOTSToSEMPoint(a_poi, ref a_SEMpt))
  333. {
  334. return false;
  335. }
  336. if (!pSEMController.IsConnected())
  337. {
  338. if (!pSEMController.Connect())
  339. {
  340. log.Error("MoveSEMToPoint: can't connect SEM.");
  341. return false;
  342. }
  343. }
  344. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  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. Thread.Sleep(hardWareDelay);
  352. return true;
  353. }
  354. // Acquire a BSE image
  355. CBSEImgClr AcquireABSEImage()
  356. {
  357. // BSE image
  358. CBSEImgClr pBSEImage = null;
  359. // get scan controller
  360. var pScanController = m_ScanHardwareMgr;
  361. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  362. return pBSEImage;
  363. }
  364. public virtual bool FieldImageProcess(Point fldCenter, CBSEImgClr imageData)
  365. {
  366. return true;
  367. }
  368. public virtual void ClassifyFieldParticles()
  369. {
  370. return;
  371. }
  372. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  373. {
  374. return;
  375. }
  376. private class AutoResetSEMControl:IDisposable
  377. {
  378. CSmplMeasure sm;
  379. public AutoResetSEMControl(CSmplMeasure s)
  380. {
  381. sm = s;
  382. }
  383. public void Dispose()
  384. {
  385. sm.SetSEMExteralOff();
  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())
  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.GetListAnalysisParticles(), 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.GetListAnalysisParticles().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 (bSaveThreadWorking)//wait untill all the field data has been saved.
  566. {
  567. Thread.Sleep(1000);
  568. log.Warn("db saving!");
  569. }
  570. //merging particles
  571. log.Info("Merging big particles which are crossing the field edge!");
  572. CImageHandler imgpro = new CImageHandler();
  573. int scanfldsize = m_Sample.GetSEMDataMsr().GetScanFieldSize();
  574. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  575. double pixelSize = m_Sample.CalculatePixelSize();
  576. imgpro.MergeBigBoundaryParticles(m_pSampleRstFile.GetFieldData(), pixelSize, scanfldsize, m_Sample.GetResolutionSize(), ref mergedParticles);
  577. CalculateMergedPartProperty(mergedParticles,pixelSize);
  578. ClassifyMergedParticles(mergedParticles);
  579. log.Info("begin merged particle data db saving...");
  580. SaveMergedParticles(mergedParticles);
  581. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  582. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  583. // let main thread to know that this sample measurement completes
  584. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  585. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  586. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  587. MsgSmplEnd.STMSampleStu.cSampleName = m_Sample.GetName();
  588. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  589. }
  590. }
  591. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  592. {
  593. var m_ImagePro = new CImageHandler();
  594. foreach (COTSParticleClr part in mergedParticles)
  595. {
  596. m_ImagePro.CalParticleImageProp(part, pixelSize);
  597. }
  598. }
  599. public void DoHolePreview(int a_nHoleID)
  600. {
  601. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  602. {
  603. // let the main thread to know that this sample measurement starts
  604. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  605. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  606. // set current time to current time
  607. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  608. // let main App know that the sample begin to measure
  609. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  610. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  611. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  612. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  613. // get SEM controller to set magnification and working distance
  614. if (!SetSEMDataMrs())
  615. {
  616. log.Error("DoHolePreview: fail to set SEM data.");
  617. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  618. // record end time
  619. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  620. return;
  621. }
  622. // get SEM external controll on
  623. if (!SetSEMExteralOn())
  624. {
  625. log.Error("DoMeasure: fail to set SEM external.");
  626. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  627. // record end time
  628. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  629. return;
  630. }
  631. // set the BSE scan param
  632. if (!SetHoleBSEParam())
  633. {
  634. log.Error("DoMeasure: fail to set BSE param.");
  635. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  636. // record end time
  637. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  638. return;
  639. }
  640. // check if measurement is aborted
  641. if (IsAborted())
  642. {// measurement aborted
  643. log.Trace("DoHolePreview: measurement aborted before get SEM condition.");
  644. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  645. // record end time
  646. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  647. return;
  648. }
  649. // calculate field centers
  650. List<System.Drawing.Point> listFieldCenter=new List<System.Drawing.Point>();
  651. List<System.Drawing.Point> alllistFieldCenter = new List<System.Drawing.Point>();
  652. // listFieldCenter.clear();
  653. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out listFieldCenter))
  654. {// failed to calculate field centers
  655. log.Error("DoHolePreview: failed to calculate field centers.");
  656. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  657. // record end time
  658. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  659. return;
  660. }
  661. // go through each field
  662. int nNewFieldId = 0;
  663. for ( int i = 0; i < listFieldCenter.Count; ++i)
  664. {// check and break if stop button is clicked
  665. if (IsAborted())
  666. {// measure stopped
  667. log.Trace("DoHolePreview: measure thread is stopped.");
  668. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  669. // record end time
  670. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  671. return;
  672. }
  673. // check if sample measurement completes
  674. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  675. int nTotalFieldSize = listFieldCenter.Count;
  676. if (IsSampleOver(pScanParam, nTotalFieldSize))
  677. {
  678. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  679. break;
  680. }
  681. // get a field center
  682. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  683. // update thread measure status class, let the main thread know that starts a new field
  684. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  685. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  686. //memset(&MsgFieldStart, 0, sizeof(ST_MSTMsg));
  687. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  688. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  689. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  690. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  691. // move SEM to the field center
  692. if (!MoveSEMToPoint(poiFieldCentre))
  693. {// failed to move SEM to the position
  694. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  695. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  696. // record end time
  697. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  698. return;
  699. }
  700. // take BSE image for the fields
  701. CBSEImgClr pBSEIamge = AcquireABSEImage();
  702. if (pBSEIamge==null)
  703. {
  704. // failed to acquire a BSE image
  705. log.Error("DoHolePreview: failed to acquire a BSE image.");
  706. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  707. // record end time
  708. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  709. return;
  710. }
  711. //BSEData
  712. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  713. // memset(&MsgFieldBSE, 0, sizeof(ST_MSTMsg));
  714. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  715. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  716. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  717. //MsgFieldBSE.STMSampleRetData.BSEData.pos.y = poiFieldCentre.y;
  718. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  719. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  720. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  721. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  722. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  723. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  724. {
  725. // measurement failed or stopped
  726. log.Error("DoHolePreview: measurement failed or stopped.");
  727. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  728. // record end time
  729. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  730. return;
  731. }
  732. //save the result to project file
  733. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  734. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,a_nHoleID, poiFieldCentre);
  735. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  736. m_listHoleBSEImg.Add(pHoleBSEImg);
  737. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  738. pProjMgrFile.SetHoleBESImgList(m_listHoleBSEImg,true);
  739. // prepare for the next
  740. ++nNewFieldId;
  741. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  742. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  743. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  744. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  745. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  746. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  747. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  748. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  749. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  750. //break;
  751. }
  752. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  753. //calculate measure time
  754. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  755. // let main thread to know that this sample measurement completes
  756. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  757. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  758. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  759. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  760. }
  761. }
  762. // Cumulate field data info
  763. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  764. {
  765. // get measure result items of the sample
  766. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  767. // cumulate field data info
  768. //pMsrResults.CumulateMeasureResults(listResultItems);
  769. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  770. // cumulate ratio
  771. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  772. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  773. pMsrResults.SetRatio(dRatio);
  774. // go through the particles list
  775. foreach (COTSParticleClr pParticle in listParticles)
  776. {
  777. // create a measure result item
  778. pMsrResults.CumulateMeasureResult(pParticle);
  779. }
  780. // ok, return TRUE
  781. return true;
  782. }
  783. // hole BSE images list
  784. void SetHoleBESImgList(CHoleBSEImgsList a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  785. {// clear the hole BSE image list if necessary
  786. if (a_bClear)
  787. {
  788. m_listHoleBSEImg.Clear();
  789. }
  790. // go through the input list
  791. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  792. {// add the new hole BSE image into HoleBSEImage list
  793. m_listHoleBSEImg.Add(pHoleBSEImg);
  794. }
  795. }
  796. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  797. {
  798. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  799. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  800. foreach (COTSParticleClr part in mergedParts)
  801. {
  802. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  803. }
  804. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  805. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  806. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  807. foreach (COTSParticleClr part in mergedParts)
  808. {
  809. ches.Add(part.GetXray());
  810. }
  811. xraydb.SaveElementChemistriesList(ches);
  812. return true;
  813. }
  814. }
  815. }