CSmplMeasure.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using OTSModelSharp.ServiceCenter;
  5. using static OTSDataType.otsdataconst;
  6. using OTSDataType;
  7. using System.Drawing;
  8. using System.Threading;
  9. using OTSCLRINTERFACE;
  10. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  11. namespace OTSModelSharp
  12. {
  13. public class CSmplMeasure
  14. {
  15. protected static NLog.Logger log ;
  16. protected bool bSaveThreadWorking;
  17. protected System.Threading.Thread m_thread_ptr;
  18. protected COTSSample m_Sample;
  19. protected CMeasure m_pMsrThread;
  20. string m_strWorkingFolder;
  21. protected CSmplMsrResult m_pSampleRstFile;
  22. internal ISemController m_SemHardwareMgr;
  23. internal IScanController m_ScanHardwareMgr;
  24. internal IEDSController m_EDSController;
  25. protected Queue<COTSField> fieldQueue=new Queue<COTSField>();
  26. protected IClassifyEngine m_classifyEngine;
  27. private bool m_ifAquireClearParticleImage=false;
  28. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  29. {
  30. m_Sample = a_pSample;
  31. log = NLog.LogManager.GetCurrentClassLogger();
  32. var expC = m_Sample.GetMsrParams().GetXRayParam().GetAnalyExpCount();
  33. var imgwidth = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cx;
  34. var imgheight = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cy;
  35. m_strWorkingFolder = a_strWorkingFolder;
  36. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);
  37. m_SemHardwareMgr = SemController.GetSEMController();
  38. m_ScanHardwareMgr = ScanController.GetScanController();
  39. var ifautoid = m_Sample.GetMsrParams().GetXRayParam().IfAutoId;
  40. var knownelements = m_Sample.GetMsrParams().GetXRayParam().AnalysisElements;
  41. m_EDSController = EDSController.GetEDSController(imgwidth,imgheight,expC,ifautoid,knownelements);
  42. m_ifAquireClearParticleImage = FileHelper.GetIfAquireClearParticleImage();
  43. }
  44. public void SetSample(COTSSample a_pSample)
  45. {
  46. m_Sample = a_pSample;
  47. m_pSampleRstFile.SetSample(a_pSample);
  48. }
  49. public COTSSample GetSample() { return m_Sample; }
  50. public void SetMsrThread(CMeasure mt)
  51. {
  52. m_pMsrThread = mt;
  53. }
  54. bool SetSEMDataMrs(CSEMFieldData pSEMDataMsr)
  55. {
  56. //var pSEMDataMsr = sample.GetSEMDataMsr();
  57. double dMag = pSEMDataMsr.GetMagnification();
  58. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  59. double bri=pSEMDataMsr.Brightness;
  60. double contra = pSEMDataMsr.Contrast;
  61. try
  62. {
  63. var pSEMCtrl = m_pMsrThread.GetSEMController();
  64. pSEMCtrl.SetMagnification(dMag);
  65. pSEMCtrl.SetWorkingDistance(dWorkDis);
  66. //pSEMCtrl.SetSemBrightness(bri);
  67. //pSEMCtrl.SetSemContrast(contra);
  68. log.Warn("set magnification " + dMag);
  69. log.Warn("set wd "+ dWorkDis);
  70. //log.Warn("set brightness "+bri);
  71. //log.Warn("set contrast "+contra);
  72. }
  73. catch (Exception e)
  74. {
  75. log.Error(e.Message);
  76. }
  77. return true;
  78. }
  79. // set SEM external off
  80. bool SetSEMExteralOff()
  81. {
  82. var pSEMCtrlPtr = m_SemHardwareMgr;
  83. log.Warn("Set SEM Exteral Off!");
  84. pSEMCtrlPtr.SetScanExternal(false);
  85. return true;
  86. }
  87. internal bool InitScanControllerBSEParam()
  88. {
  89. // get scan controller
  90. var pScanController = m_ScanHardwareMgr;
  91. // scan parameters
  92. var pMsrParam = m_Sample.GetMsrParams();
  93. var pImgScanParam = pMsrParam.GetImageScanParam();
  94. // get image size
  95. var nImageSizeId = pImgScanParam.GetImageResulotion();
  96. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  97. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  98. // get dwell time
  99. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  100. // convert dwell time to bruker dwell time
  101. DwellTimeLevel DwellTimeId;
  102. switch (nDwellTime)
  103. {
  104. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  105. DwellTimeId = DwellTimeLevel.Low;
  106. break;
  107. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  108. DwellTimeId = DwellTimeLevel.Medium;
  109. break;
  110. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  111. DwellTimeId = DwellTimeLevel.High;
  112. break;
  113. default:
  114. DwellTimeId = DwellTimeLevel.Low;
  115. break;
  116. }
  117. if (!pScanController.Init())
  118. {
  119. log.Error("InitScanControllerBSEParam: failed to get scan control.");
  120. return false;
  121. }
  122. // set dwell time
  123. if (!pScanController.SetDwellTime(DwellTimeId))
  124. {
  125. return false;
  126. }
  127. // set image size
  128. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  129. {
  130. // failed to set dwell time
  131. log.Error("InitScanControllerBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  132. return false;
  133. }
  134. return true;
  135. }
  136. public void SetWorkingFolder(String a_strWorkingFolder)
  137. {
  138. // add "\\" at the string end if it is not "\\"
  139. if (a_strWorkingFolder.PadRight(1)!="\\")
  140. {
  141. a_strWorkingFolder += "\\";
  142. }
  143. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  144. }
  145. protected bool IsAborted()
  146. {
  147. return m_pMsrThread.IsMeasureStopped();
  148. }
  149. protected bool IsPaused()
  150. {
  151. var statu = m_pMsrThread.GetMsrThreadStatus();
  152. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  153. {
  154. return true;
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160. }
  161. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  162. {
  163. string sStopMode = a_pScanParam.GetStopMode();
  164. int nStopField = a_pScanParam.GetStopParamFields();
  165. // completed fields number
  166. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  167. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  168. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  169. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  170. int nNumParticle = 0;
  171. foreach (var pResult in listMsrResult)
  172. {
  173. if (pResult.GetTypeId() > (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  174. {
  175. nNumParticle += (int)pResult.GetNumber();
  176. }
  177. }
  178. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  179. int nDay = timeSpan.Days;
  180. int nHour = timeSpan.Hours;
  181. int nMin = timeSpan.Minutes;
  182. int nSec = timeSpan.Seconds;
  183. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  184. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  185. int nParticlAim = a_pScanParam.GetStopParamParticles();
  186. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  187. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  188. bool bRet = false;
  189. string[] str = sStopMode.Replace(" ", "").Split('+');
  190. for(int i=0;i< str.Length;i++)
  191. {
  192. switch (int.Parse(str[i].Split(':')[0])-1)
  193. {
  194. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  195. // completed fields number
  196. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  197. {
  198. bRet = true;
  199. }
  200. break;
  201. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  202. if (nCompeltedField >= nStopField)
  203. {
  204. bRet = true;
  205. }
  206. break;
  207. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  208. if (nNumParticle >= nParticlAim)
  209. {
  210. bRet = true;
  211. }
  212. break;
  213. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  214. if (nUsedTime >= nMeasTimeAim)
  215. {
  216. bRet = true;
  217. }
  218. break;
  219. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  220. if (NMeasArea >= NMeasAreaAim)
  221. {
  222. bRet = true;
  223. }
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. return bRet;
  230. }
  231. // move SEM to the point
  232. internal bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  233. {
  234. // get SEM controller
  235. var pSEMController = m_SemHardwareMgr;
  236. PointF a_SEMpt = new Point();
  237. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  238. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  239. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  240. {
  241. return false;
  242. }
  243. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  244. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  245. // move SEM to the position (rotation 0)
  246. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  247. {
  248. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  249. return false;
  250. }
  251. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  252. if (pMsrParam.SlopParam.IsUsingSlopParam)
  253. {
  254. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  255. double originWd = 0;
  256. pSEMController.GetWorkingDistance(ref originWd);
  257. if ((wd - originWd) > 2)
  258. {
  259. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  260. }
  261. else
  262. {
  263. Thread.Sleep(hardWareDelay);
  264. pSEMController.SetWorkingDistance(wd);
  265. }
  266. }
  267. if (hardWareDelay > 0)
  268. {
  269. Thread.Sleep(hardWareDelay);
  270. }
  271. return true;
  272. }
  273. // Acquire a BSE image
  274. CBSEImgClr AcquireABSEImage()
  275. {
  276. // BSE image
  277. CBSEImgClr pBSEImage = null;
  278. // get scan controller
  279. var pScanController = m_ScanHardwareMgr;
  280. pBSEImage = pScanController.AcquireBSEImage();
  281. return pBSEImage;
  282. }
  283. public bool IsLowCounts(COTSParticleClr particle)
  284. {
  285. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  286. if (pXRayParam.GetUsingXray() == true)
  287. {
  288. var thecount = particle.GetXray().GetTotalCount();
  289. var expect = pXRayParam.GetAnalyExpCount();
  290. if (thecount < expect)
  291. {
  292. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  293. particle.SetClassifyId((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  294. particle.SetTypeColor("#000000");
  295. particle.SetTypeName("LowCounts");
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. public virtual void ClassifyFieldParticles(COTSField curFldData)
  302. {
  303. return;
  304. }
  305. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  306. {
  307. double kv = 0, brightness = 0, contrast = 0;
  308. var hw = SemController.GetSEMController();
  309. hw.GetSemHighTension(ref kv);
  310. hw.GetSemBrightness(ref brightness);
  311. hw.GetSemContrast(ref contrast);
  312. SemDataGnr.SetValue(kv, brightness, contrast);
  313. return true;
  314. }
  315. public void DoMeasureForOneSample()
  316. {
  317. try
  318. {
  319. // let the main thread to know that this sample measurement starts
  320. var pStatus = m_Sample.GetMsrStatus();
  321. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  322. // set current time to current time
  323. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  324. // let main App know that the sample begin to measure
  325. ST_MSTMsg MsgSmpStart = new ST_MSTMsg(m_Sample);
  326. MsgSmpStart.InitSampleStartMsg();
  327. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStart);
  328. log.Info(m_Sample.GetName() + " Measurement started!");
  329. // get SEM controller to set magnification and working distance
  330. if (!SetSEMDataMrs(m_Sample.GetSEMDataMsr()))
  331. {
  332. log.Error("DoMeasure: fail to set SEM data.");
  333. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  334. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  335. return;
  336. }
  337. // set the BSE scan param
  338. if (!InitScanControllerBSEParam())
  339. {
  340. log.Error("DoMeasure: fail to set BSE param.");
  341. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  342. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  343. return;
  344. }
  345. var pSEMDataGnr = new CSEMDataGnr();
  346. log.Info("Get current Kv, Brightness and Contrast!");
  347. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  348. m_pSampleRstFile.SetSEMDataGnr(pSEMDataGnr);
  349. // record SEM data
  350. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  351. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  352. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  353. CSampleHolder pSampleStage = pProjMgrFile.GetStageHolder();
  354. m_pSampleRstFile.SetSEMStage(pSampleStage);
  355. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  356. log.Info("Create result file!");
  357. if (!m_pSampleRstFile.CreateResultFiles())
  358. {// failed to call measure result file Save method
  359. log.Error("DoMeasure: failed to call measure result file Save method.");
  360. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  361. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  362. return;
  363. }
  364. //------
  365. var FldDatas = m_Sample.GetFieldsData();
  366. for (int i = 0; i < FldDatas.Count; ++i)
  367. {// check and break if stop button is clicked
  368. try
  369. {
  370. var curFld = FldDatas[i];
  371. if (curFld.GetIsMeasureComplete())
  372. {
  373. continue;
  374. }
  375. if (curFld.Enable == false)
  376. {
  377. continue;
  378. }
  379. if (IsPaused())
  380. {// measure stopped
  381. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  382. // record end time
  383. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  384. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  385. while (fieldQueue.Count() > 0)
  386. {
  387. Thread.Sleep(100);
  388. }
  389. SetSEMExteralOff();
  390. // update thread measure status class, let the main thread know that this sample measurement stopped
  391. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  392. MsgSmpStop.InitSamplePausedMsg();
  393. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  394. while (IsPaused())
  395. {
  396. Thread.Sleep(300);
  397. }
  398. }
  399. if (IsAborted())
  400. {// measure stopped
  401. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  402. // record end time
  403. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  404. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  405. while (fieldQueue.Count() > 0)
  406. {
  407. Thread.Sleep(100);
  408. }
  409. break;
  410. }
  411. // check if sample measurement completes
  412. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  413. if (IsSampleOver(pScanParam))
  414. {
  415. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  416. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  417. break;
  418. }
  419. // get a field center
  420. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  421. // update thread measure status class, let the main thread know that starts a new field
  422. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample, curFld);
  423. MsgFieldStart.InitFieldStartMsg();
  424. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  425. int fldNo = curFld.GetId();
  426. log.Warn("Current field:" + fldNo.ToString());
  427. // move SEM to the field center
  428. if (!MoveSEMToPoint(poiFieldCentre))
  429. {// failed to move SEM to the position
  430. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  431. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  432. // record end time
  433. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  434. return;
  435. }
  436. log.Info("Begin to Acquire BSE image!");
  437. // take BSE image for the fields
  438. CBSEImgClr pBSEImg = AcquireABSEImage();
  439. // let the main thread to know that image process is completed
  440. if (pBSEImg == null)
  441. {
  442. log.Error("ImageProcess: can't get BSE image.");
  443. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  444. return;
  445. }
  446. curFld.SetBSEImage(pBSEImg);
  447. //BSEData
  448. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample, curFld);
  449. MsgFieldBSE.InitFieldBSEImageMsg();
  450. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  451. log.Info("Acquire BSE image success! Processing image...");
  452. // image process
  453. FieldImageProcess(curFld);
  454. MsgFieldBSE.InitFieldBSEAnalysisPartsDataMsg();
  455. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  456. if (m_ifAquireClearParticleImage)
  457. {
  458. var listAnalysisParts = curFld.GetListAnalysisParticles();
  459. foreach (var p in listAnalysisParts)
  460. {
  461. Rectangle r = (Rectangle)p.GetParticleRect();
  462. var img = m_ScanHardwareMgr.AcquireRectangleBSEImage(r);
  463. if (img != null)
  464. {
  465. curFld.particleImages.Add(img);
  466. }
  467. }
  468. }
  469. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  470. if (pXRayParam.GetUsingXray() == true)
  471. {
  472. foreach (var p in curFld.GetListAnalysisParticles())
  473. {
  474. p.SetIsXrayParticle(true);
  475. }
  476. try
  477. {
  478. CollectParticlesXrayData(curFld);
  479. }
  480. catch (Exception e)
  481. {
  482. log.Error(e.Message);
  483. }
  484. }
  485. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  486. curFld.SetIsMeasureComplete(true);
  487. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  488. try
  489. {
  490. ClassifyFieldParticles(curFld);
  491. }
  492. catch (Exception e)
  493. {
  494. log.Error(e.Message);
  495. }
  496. MsgFieldBSE.InitFieldSTDColoredPartsDataMsg();
  497. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  498. //start db save
  499. StartSaveFileThread(ref curFld);
  500. // record
  501. pStatus.AddCompletedFieldCenter(curFld.GetOTSPosition());
  502. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  503. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  504. }
  505. catch (Exception e)
  506. {
  507. log.Error(e.Message);
  508. }
  509. }
  510. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  511. {
  512. Thread.Sleep(1000);
  513. log.Warn("db saving!");
  514. }
  515. TheLastWorkOfSampleMeasure();
  516. }
  517. catch (Exception e)
  518. {
  519. log.Error(e.Message);
  520. }
  521. finally
  522. {
  523. SetSEMExteralOff();
  524. }
  525. }
  526. public void TheLastWorkOfSampleMeasure()
  527. {
  528. COTSSample theSample = m_Sample;
  529. var pStatus = theSample.GetMsrStatus();
  530. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  531. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  532. // let main thread to know that this sample measurement completes
  533. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  534. MsgSmplEnd.InitSampleCompleteMsg();
  535. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  536. }
  537. public bool DoMEasureForReMeasure()
  538. {
  539. return true;
  540. }
  541. public virtual void FieldImageProcess(COTSField curFldData)
  542. {
  543. PointF fldCenter = curFldData.GetOTSPosition();
  544. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  545. PointF semPos = new Point();
  546. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  547. curFldData.SetSemPos(semPos);
  548. //first step:remove background of the bse image and compound all the finded particles.
  549. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  550. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  551. log.Info("Begin to process image and get all particles!");
  552. //according to the ECD scope,filter out the effective particles,remove the noise.
  553. curFldData.GetOriginalParticles(m_Sample.GetMsrParams(), m_Sample.CalculatePixelSize());
  554. if (curFldData.Sample.GetMsrParams().GetImageProcessParam().GetBGRemoveType() == OTS_BGREMOVE_TYPE.MATRIX)
  555. {
  556. curFldData.SetListAnalysisParticles(curFldData.GetAllParticles());
  557. }
  558. else
  559. {
  560. log.Info("Begin to filter particles!");
  561. curFldData.FilterParticles(m_Sample.GetMsrParams().GetXRayParam());//filter according to the xraylimit and outermost border
  562. log.Info("Begin to Calculate the image property of every particle!");
  563. var analysisparts = curFldData.GetListAnalysisParticles();
  564. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  565. curFldData.SelectParticlesAccordingImgProp(pImgProcessParam);
  566. var overlap = pImgProcessParam.GetOverlapParam();
  567. if (overlap > 0)
  568. {
  569. try
  570. {
  571. curFldData.RemoveDuplicateOverlapParticles(overlap);
  572. }
  573. catch (Exception e)
  574. {
  575. log.Error(e.Message);
  576. }
  577. }
  578. }
  579. curFldData.CalculateParticleAbsolutPos(m_pMsrThread.GetProjResultData().GetSEMStageData());
  580. curFldData.InitParticles(pImgProcessParam);
  581. return ;
  582. }
  583. public virtual void CollectParticlesXrayData(COTSField curFldData)
  584. {
  585. var allParts = curFldData.GetListXrayParticles();
  586. var smallparts = new List<COTSParticleClr>();
  587. var bigparts = new List<COTSParticleClr>();
  588. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  589. foreach (var part in allParts)
  590. {
  591. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  592. if (equalCircleDiameter < quantifyThreshold)
  593. {
  594. smallparts.Add(part);
  595. }
  596. else
  597. {
  598. bigparts.Add(part);
  599. }
  600. }
  601. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  602. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  603. curFldData.CreateXrayList(smallparts);
  604. curFldData.CreateXrayList(bigparts);
  605. // get x-ray parameters
  606. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  607. var workmode = pXRayParam.GetScanMode();
  608. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  609. {
  610. uint nXRayAQTime;
  611. if (bigparts.Count > 0)
  612. {
  613. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  614. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  615. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  616. }
  617. if (smallparts.Count > 0)
  618. {
  619. Thread.Sleep(1000);//add delay here,or else the eds system will halt.
  620. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  621. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  622. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  623. }
  624. }
  625. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  626. {
  627. uint nXRayAQTime;
  628. if (bigparts.Count > 0)
  629. {
  630. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  631. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  632. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  633. }
  634. if (smallparts.Count > 0)
  635. {
  636. Thread.Sleep(1000);
  637. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  638. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  639. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  640. }
  641. }
  642. else if (workmode == OTS_X_RAY_SCAN_MODE.ExpandMode)
  643. {
  644. uint nXRayAQTime;
  645. if (bigparts.Count > 0)
  646. {
  647. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  648. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  649. m_EDSController.GetXRayByExpandFeatures(bigparts, nXRayAQTime, true);
  650. }
  651. if (smallparts.Count > 0)
  652. {
  653. Thread.Sleep(500);
  654. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  655. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  656. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  657. }
  658. }
  659. return;
  660. }
  661. public virtual void QuantifyParticlesXrayData(COTSField curFldData)
  662. {
  663. var parts = curFldData.GetListXrayParticles();
  664. foreach (var p in parts)
  665. {
  666. m_EDSController.QuantifyXrayByPart(p);
  667. }
  668. }
  669. // Cumulate field data info
  670. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  671. {
  672. // get measure result items of the sample
  673. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  674. // go through the particles list
  675. foreach (COTSParticleClr pParticle in listParticles)
  676. {
  677. // create a measure result item
  678. pMsrResults.CumulateMeasureResult(pParticle);
  679. }
  680. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  681. // cumulate ratio
  682. double dRatio = pMsrResults.GetTotalParticleArea();
  683. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  684. pMsrResults.SetRatio(dRatio);
  685. m_Sample.SetMsrResults(pMsrResults);
  686. return true;
  687. }
  688. public bool SaveFieldData(COTSField fldData, string filedFileFoler)
  689. {
  690. string strFieldFileFolder = filedFileFoler;
  691. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  692. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  693. int nId = fldData.GetId();
  694. string sFieldId;
  695. sFieldId = nId.ToString();
  696. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  697. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  698. {
  699. log.Error("SaveFieldFiles: save BSE file failed.");
  700. return false;
  701. }
  702. if (fldData.particleImages.Count > 0)
  703. {
  704. for (int i=0;i<fldData.particleImages.Count;i++)
  705. {
  706. strBSEFilePathname= m_pSampleRstFile.GetParticleImageFolder() + "\\" + sFieldId +"_" +i.ToString()+pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  707. pBSEImgFileMgr.SetBSEImg(fldData.particleImages[i]);
  708. pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname);
  709. }
  710. }
  711. // IncA Data list
  712. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  713. pDBFileMgr.SaveStatusDataToDB();
  714. var fldDB = pDBFileMgr.GetFieldDB();
  715. log.Warn("Start saving particle data.");
  716. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  717. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  718. fldcmds.Add(fldcmd);
  719. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  720. //remomove the invalid particles
  721. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  722. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  723. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  724. var listAnalysisPosXray = new List<CPosXrayClr>();
  725. foreach (var p in fldData.GetListXrayParticles())
  726. {
  727. listAnalysisPosXray.Add(p.GetXray());
  728. }
  729. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  730. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  731. return true;
  732. }
  733. protected void SaveFieldMgrData()
  734. {
  735. while (bSaveThreadWorking)
  736. {
  737. while (fieldQueue.Count() > 0)
  738. {
  739. COTSField f = fieldQueue.Dequeue();
  740. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  741. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  742. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  743. }
  744. if (fieldQueue.Count() == 0)
  745. {
  746. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  747. log.Warn("finished batch saving");
  748. return;
  749. }
  750. }
  751. return;
  752. }
  753. private void SendFieldParticlesInfoToGUI(COTSField curFld,CMsrSampleStatus pStatus)
  754. {
  755. double measuredArea = 0; // this area should be the field area
  756. var a_pBSEImg = curFld.GetBSEImage();
  757. double dPixelSize = curFld.GetPixelSize();
  758. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  759. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  760. log.Info("Send field data to screen!");
  761. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  762. MsgFieldEnd.InitFieldDataMsg();
  763. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  764. }
  765. protected void StartSaveFileThread(ref COTSField a_pFieldMgr)
  766. {
  767. fieldQueue.Enqueue(a_pFieldMgr);
  768. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  769. {
  770. if (bSaveThreadWorking == false)
  771. {
  772. bSaveThreadWorking = true;
  773. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  774. m_thread_ptr.IsBackground = true;
  775. m_thread_ptr.Start();
  776. }
  777. }
  778. }
  779. }
  780. }