CSmplMeasure.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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._0_OTSModel.OTSDataType;
  11. using System.Windows.Forms;
  12. using System.Data;
  13. using System.IO;
  14. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  15. namespace OTSModelSharp
  16. {
  17. using CHoleBSEImgsList = List<CHoleBSEImg>;
  18. public class CSmplMeasure
  19. {
  20. protected static NLog.Logger log ;
  21. protected bool bSaveThreadWorking;
  22. protected System.Threading.Thread m_thread_ptr;
  23. protected COTSSample m_Sample;
  24. protected COTSSample m_HolePreviewSample;
  25. protected CMeasure m_pMsrThread;
  26. string m_strWorkingFolder;
  27. protected CSmplMsrResult m_pSampleRstFile;
  28. CHoleBSEImgsList m_listHoleBSEImg;
  29. protected ISemController m_SemHardwareMgr;
  30. protected IScanController m_ScanHardwareMgr;
  31. protected IEDSController m_EDSController;
  32. protected Queue<COTSField> fieldQueue=new Queue<COTSField>();
  33. protected IClassifyEngine m_classifyEngine;
  34. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  35. {
  36. m_Sample = a_pSample;
  37. log = NLog.LogManager.GetCurrentClassLogger();
  38. var expC = m_Sample.GetMsrParams().GetXRayParam().GetAnalyExpCount();
  39. var imgwidth = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cx;
  40. var imgheight = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cy;
  41. m_strWorkingFolder = a_strWorkingFolder;
  42. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);
  43. m_SemHardwareMgr = SemController.GetSEMController();
  44. m_ScanHardwareMgr = ScanController.GetScanController();
  45. m_EDSController = EDSController.GetEDSController(imgwidth,imgheight,expC);
  46. m_listHoleBSEImg = new CHoleBSEImgsList();
  47. m_Sample = a_pSample;
  48. }
  49. public void SetSample(COTSSample a_pSample)
  50. {
  51. m_Sample = a_pSample;
  52. m_pSampleRstFile.SetSample(a_pSample);
  53. }
  54. public COTSSample GetSample() { return m_Sample; }
  55. public void SetHolePreviewSample(COTSSample a_pSample)
  56. {
  57. m_HolePreviewSample = a_pSample;
  58. }
  59. public COTSSample GetHolePreviewSample() { return m_HolePreviewSample; }
  60. public void SetMsrThread(CMeasure mt)
  61. {
  62. m_pMsrThread = mt;
  63. }
  64. bool SetSEMDataMrs(COTSSample sample)
  65. {
  66. var pSEMDataMsr = sample.GetSEMDataMsr();
  67. double dMag = pSEMDataMsr.GetMagnification();
  68. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  69. var pSEMCtrl = m_pMsrThread.GetSEMController();
  70. pSEMCtrl.SetMagnification(dMag);
  71. pSEMCtrl.SetWorkingDistance(dWorkDis);
  72. return true;
  73. }
  74. bool SetSEMExteralOn()
  75. {
  76. var pSEMCtrl = m_SemHardwareMgr;
  77. log.Warn("Set Scan Exteral on!");
  78. pSEMCtrl.SetScanExternal(true);
  79. return true;
  80. }
  81. // set SEM external off
  82. bool SetSEMExteralOff()
  83. {
  84. var pSEMCtrlPtr = m_SemHardwareMgr;
  85. log.Warn("Set SEM Exteral Off!");
  86. pSEMCtrlPtr.SetScanExternal(false);
  87. return true;
  88. }
  89. bool SetBSEParam()
  90. {
  91. // get scan controller
  92. var pScanController = m_ScanHardwareMgr;
  93. // scan parameters
  94. var pMsrParam = m_Sample.GetMsrParams();
  95. var pImgScanParam = pMsrParam.GetImageScanParam();
  96. // get image size
  97. var nImageSizeId = pImgScanParam.GetImageResulotion();
  98. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  99. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  100. // get dwell time
  101. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  102. // convert dwell time to bruker dwell time (8, 16, 32)
  103. int nBrukerDwellTimeId;
  104. switch (nDwellTime)
  105. {
  106. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  107. nBrukerDwellTimeId = 1;
  108. break;
  109. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  110. nBrukerDwellTimeId = 2;
  111. break;
  112. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  113. nBrukerDwellTimeId = 3;
  114. break;
  115. default:
  116. nBrukerDwellTimeId = 1;
  117. break;
  118. }
  119. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  120. if (!pScanController.Init())
  121. {
  122. log.Error("SetBSEParam: failed to get scan control.");
  123. return false;
  124. }
  125. // set dwell time
  126. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  127. {
  128. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  129. return false;
  130. }
  131. // set image size
  132. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  133. {
  134. // failed to set dwell time
  135. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool SetHoleBSEParam(COTSSample sample)
  141. {
  142. // get scan controller
  143. var pScanController = m_ScanHardwareMgr;
  144. // scan parameters
  145. var pMsrParam = sample.GetMsrParams();
  146. var pImgScanParam = pMsrParam.GetImageScanParam();
  147. // get image size
  148. var nImageSizeId = pImgScanParam.GetImageResulotion();
  149. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  150. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  151. // get dwell time
  152. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  153. // convert dwell time to bruker dwell time (6, 16, 32)
  154. 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.
  155. if (!pScanController.Init())
  156. {
  157. log.Error("SetBSEParam: failed to get scan control.");
  158. return false;
  159. }
  160. // set dwell time
  161. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  162. {
  163. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  164. return false;
  165. }
  166. // set image size
  167. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  168. {
  169. // failed to set dwell time
  170. log.Error("SetBSEParam: failed to set ImageSize");
  171. return false;
  172. }
  173. return true;
  174. }
  175. public void SetWorkingFolder(String a_strWorkingFolder)
  176. {
  177. // add "\\" at the string end if it is not "\\"
  178. if (a_strWorkingFolder.PadRight(1)!="\\")
  179. {
  180. a_strWorkingFolder += "\\";
  181. }
  182. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  183. }
  184. bool CalculateUnMeasuredHoleImgCenters(COTSSample sample, out List<System.Drawing.Point> a_allpieldcenter, out List<System.Drawing.Point> a_listUnMsrFieldCenter)
  185. {
  186. // sample measure parameters
  187. CSampleParam pMsrParam = sample.GetMsrParams();
  188. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  189. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  190. CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  191. CMsrSampleStatus pStatus = sample.GetMsrStatus();
  192. // measured field centers list
  193. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  194. // field centers list manager
  195. CFieldPositionHelper pFieldMgr = new CFieldPositionHelper();
  196. // init field centers list manager
  197. if (!pFieldMgr.Init(sample.GetMsrDomain(), poImageScanParam,0, poSEMDataMsr, listCompletedCenter))
  198. {
  199. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  200. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  201. a_allpieldcenter = new List<Point>();
  202. return false;
  203. }
  204. // get field centers list
  205. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  206. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  207. // ok, return TRUE
  208. return true;
  209. }
  210. protected bool IsAborted()
  211. {
  212. return m_pMsrThread.IsMeasureStopped();
  213. }
  214. protected bool IsPaused()
  215. {
  216. var statu = m_pMsrThread.GetMsrThreadStatus();
  217. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  218. {
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  227. {
  228. string sStopMode = a_pScanParam.GetStopMode();
  229. int nStopField = a_pScanParam.GetStopParamFields();
  230. // completed fields number
  231. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  232. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  233. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  234. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  235. int nNumParticle = 0;
  236. foreach (var pResult in listMsrResult)
  237. {
  238. if (pResult.GetTypeId() > (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  239. {
  240. nNumParticle += (int)pResult.GetNumber();
  241. }
  242. }
  243. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  244. int nDay = timeSpan.Days;
  245. int nHour = timeSpan.Hours;
  246. int nMin = timeSpan.Minutes;
  247. int nSec = timeSpan.Seconds;
  248. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  249. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  250. int nParticlAim = a_pScanParam.GetStopParamParticles();
  251. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  252. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  253. bool bRet = false;
  254. string[] str = sStopMode.Replace(" ", "").Split('+');
  255. for(int i=0;i< str.Length;i++)
  256. {
  257. switch (int.Parse(str[i].Split(':')[0])-1)
  258. {
  259. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  260. // completed fields number
  261. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  262. {
  263. bRet = true;
  264. }
  265. break;
  266. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  267. if (nCompeltedField >= nStopField)
  268. {
  269. bRet = true;
  270. }
  271. break;
  272. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  273. if (nNumParticle >= nParticlAim)
  274. {
  275. bRet = true;
  276. }
  277. break;
  278. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  279. if (nUsedTime >= nMeasTimeAim)
  280. {
  281. bRet = true;
  282. }
  283. break;
  284. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  285. if (NMeasArea >= NMeasAreaAim)
  286. {
  287. bRet = true;
  288. }
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. return bRet;
  295. }
  296. // move SEM to the point
  297. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  298. {
  299. // get SEM controller
  300. var pSEMController = m_SemHardwareMgr;
  301. PointF a_SEMpt = new Point();
  302. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  303. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  304. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  305. {
  306. return false;
  307. }
  308. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  309. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  310. // move SEM to the position (rotation 0)
  311. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  312. {
  313. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  314. return false;
  315. }
  316. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  317. if (pMsrParam.SlopParam.IsUsingSlopParam)
  318. {
  319. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  320. double originWd = 0;
  321. pSEMController.GetWorkingDistance(ref originWd);
  322. if ((wd - originWd) > 2)
  323. {
  324. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  325. }
  326. else
  327. {
  328. Thread.Sleep(hardWareDelay);
  329. pSEMController.SetWorkingDistance(wd);
  330. }
  331. }
  332. if (hardWareDelay > 0)
  333. {
  334. Thread.Sleep(hardWareDelay);
  335. }
  336. return true;
  337. }
  338. // Acquire a BSE image
  339. CBSEImgClr AcquireABSEImage()
  340. {
  341. // BSE image
  342. CBSEImgClr pBSEImage = null;
  343. // get scan controller
  344. var pScanController = m_ScanHardwareMgr;
  345. pBSEImage = pScanController.AcquireBSEImage();
  346. return pBSEImage;
  347. }
  348. public bool IsLowCounts(COTSParticleClr particle)
  349. {
  350. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  351. if (pXRayParam.GetUsingXray() == true)
  352. {
  353. var thecount = particle.GetXray().GetTotalCount();
  354. var expect = pXRayParam.GetAnalyExpCount();
  355. if (thecount < expect)
  356. {
  357. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  358. particle.SetTypeColor("#000000");
  359. particle.SetTypeName("LowCounts");
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. public virtual void ClassifyFieldParticles(COTSField curFldData)
  366. {
  367. return;
  368. }
  369. private class SEMStateObject
  370. {
  371. private PointF pos;
  372. private double workingDistance;
  373. private CSEMDataGnr semdata;
  374. private COTSSample originalSample;
  375. private double magnification;
  376. public PointF Pos { get => pos; set => pos = value; }
  377. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  378. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  379. public double Magnification { get => magnification; set => magnification = value; }
  380. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  381. }
  382. private class AutoResetSEMControl:IDisposable
  383. {
  384. CSmplMeasure sm;
  385. private SEMStateObject semState=null;
  386. public AutoResetSEMControl(CSmplMeasure s)
  387. {
  388. sm = s;
  389. }
  390. public SEMStateObject SemState
  391. {
  392. get => semState;
  393. set => semState = value;
  394. }
  395. public void Dispose()
  396. {
  397. if (semState != null)
  398. {
  399. sm.SetBSEParam();
  400. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  401. Thread.Sleep(100);
  402. sm.MoveSEMToPoint(semState.Pos);
  403. Thread.Sleep(100);
  404. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  405. }
  406. sm.SetSEMExteralOff();
  407. }
  408. }
  409. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  410. {
  411. double kv = 0, brightness = 0, contrast = 0;
  412. var hw = SemController.GetSEMController();
  413. hw.GetSemHighTension(ref kv);
  414. hw.GetSemBrightness(ref brightness);
  415. hw.GetSemContrast(ref contrast);
  416. SemDataGnr.SetValue(kv, brightness, contrast);
  417. return true;
  418. }
  419. public void DoMeasureForOneSample()
  420. {
  421. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  422. {
  423. // let the main thread to know that this sample measurement starts
  424. var pStatus = m_Sample.GetMsrStatus();
  425. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  426. // set current time to current time
  427. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  428. // let main App know that the sample begin to measure
  429. ST_MSTMsg MsgSmpStart = new ST_MSTMsg(m_Sample);
  430. MsgSmpStart.InitSampleStartMsg();
  431. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStart);
  432. log.Info(m_Sample.GetName() + " Measurement started!");
  433. // get SEM controller to set magnification and working distance
  434. if (!SetSEMDataMrs(m_Sample))
  435. {
  436. log.Error("DoMeasure: fail to set SEM data.");
  437. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  438. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  439. return;
  440. }
  441. // set the BSE scan param
  442. if (!SetBSEParam())
  443. {
  444. log.Error("DoMeasure: fail to set BSE param.");
  445. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  446. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  447. return;
  448. }
  449. var pSEMDataGnr = new CSEMDataGnr();
  450. log.Info("Get Kv, Brightness and Contrast!");
  451. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  452. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  453. // record SEM data
  454. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  455. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  456. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  457. // record stage
  458. CStage pStage = pProjMgrFile.GetStage();
  459. m_pSampleRstFile.SetSEMStage(pStage);
  460. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  461. log.Info("Create result file!");
  462. if (!m_pSampleRstFile.CreateResultFiles())
  463. {// failed to call measure result file Save method
  464. log.Error("DoMeasure: failed to call measure result file Save method.");
  465. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  466. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  467. return;
  468. }
  469. //------
  470. var FldDatas = m_Sample.GetFieldsData();
  471. for (int i = 0; i < FldDatas.Count; ++i)
  472. {// check and break if stop button is clicked
  473. var curFld = FldDatas[i];
  474. if (curFld.GetIsMeasureComplete())
  475. {
  476. continue;
  477. }
  478. if (IsPaused())
  479. {// measure stopped
  480. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  481. // record end time
  482. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  483. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  484. while (fieldQueue.Count() > 0)
  485. {
  486. Thread.Sleep(100);
  487. }
  488. SetSEMExteralOff();
  489. // update thread measure status class, let the main thread know that this sample measurement stopped
  490. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  491. MsgSmpStop.InitSamplePausedMsg();
  492. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  493. while (IsPaused())
  494. {
  495. Thread.Sleep(300);
  496. }
  497. }
  498. if (IsAborted())
  499. {// measure stopped
  500. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  501. // record end time
  502. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  503. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  504. while (fieldQueue.Count() > 0)
  505. {
  506. Thread.Sleep(100);
  507. }
  508. break;
  509. }
  510. // check if sample measurement completes
  511. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  512. if (IsSampleOver(pScanParam))
  513. {
  514. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  515. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  516. break;
  517. }
  518. // get a field center
  519. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  520. // update thread measure status class, let the main thread know that starts a new field
  521. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample,curFld);
  522. MsgFieldStart.InitFieldStartMsg();
  523. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  524. int fldNo = curFld.GetId();
  525. log.Warn("Current field:" + fldNo.ToString());
  526. // move SEM to the field center
  527. if (!MoveSEMToPoint(poiFieldCentre))
  528. {// failed to move SEM to the position
  529. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  530. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  531. // record end time
  532. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  533. return;
  534. }
  535. log.Info("Begin to Acquire BSE image!");
  536. // take BSE image for the fields
  537. CBSEImgClr pBSEImg = AcquireABSEImage();
  538. // let the main thread to know that image process is completed
  539. if (pBSEImg == null)
  540. {
  541. log.Error("ImageProcess: can't get no background image.");
  542. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  543. return;
  544. }
  545. curFld.SetBSEImage(pBSEImg);
  546. //BSEData
  547. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample,curFld);
  548. MsgFieldBSE.InitFieldBSEImageMsg();
  549. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  550. log.Info("Acquire BSE image success! Processing image...");
  551. // image process
  552. FieldImageProcess(curFld);
  553. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  554. if (pXRayParam.GetUsingXray() == true)
  555. {
  556. CollectParticlesXrayData(curFld);
  557. }
  558. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  559. curFld.SetIsMeasureComplete(true);
  560. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  561. ClassifyFieldParticles(curFld);
  562. //start db save
  563. StartSaveFileThread(ref curFld);
  564. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  565. }
  566. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  567. {
  568. Thread.Sleep(1000);
  569. log.Warn("db saving!");
  570. }
  571. TheLastWorkOfSampleMeasure();
  572. }
  573. }
  574. public void TheLastWorkOfSampleMeasure()
  575. {
  576. COTSSample theSample = m_Sample;
  577. var pStatus = theSample.GetMsrStatus();
  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(m_Sample);
  582. MsgSmplEnd.InitSampleCompleteMsg();
  583. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  584. string userdB = m_Sample.GetMsrParams().GetSTDName();
  585. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  586. {
  587. try
  588. {
  589. string userdBpath= Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  590. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  591. }
  592. catch(Exception ex)
  593. {
  594. MessageBox.Show("userdB copy failed:" + ex.ToString());
  595. }
  596. }
  597. }
  598. public bool DoMEasureForReMeasure()
  599. {
  600. return true;
  601. }
  602. public virtual void FieldImageProcess(COTSField curFldData)
  603. {
  604. PointF fldCenter = curFldData.GetOTSPosition();
  605. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  606. PointF semPos = new Point();
  607. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  608. curFldData.SetSemPos(semPos);
  609. //first step:remove background of the bse image and compound all the finded particles.
  610. log.Info("Begin to process image and get all particles!");
  611. GetOriginalParticles(ref curFldData);
  612. log.Info("Begin to Calculate the image property of every particle!");
  613. var analysisparts = curFldData.GetListAnalysisParticles();
  614. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  615. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  616. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  617. curFldData.RemoveDuplicateOverlapParticles(pImgProcessParam.GetOverlapParam());
  618. log.Info("Begin to filter particles!");
  619. FilterParticles(ref curFldData);
  620. CalculateParticleAbsolutPos(ref curFldData);
  621. return ;
  622. }
  623. private void FilterParticles(ref COTSField curFldData)
  624. {
  625. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  626. var listXray1 = new List<COTSParticleClr>();
  627. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  628. if (listXray.Count > pXRayParam.GetXrayLimit())
  629. {
  630. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  631. {
  632. listXray1.Add(listXray[i]);
  633. }
  634. }
  635. else
  636. {
  637. listXray1 = listXray;
  638. }
  639. curFldData.SetListAnalysisParticles(listXray1);
  640. log.Info("Analysis particles:" + listXray1.Count);
  641. }
  642. public void GetOriginalParticles(ref COTSField curFldData)
  643. {
  644. // get image process parameter
  645. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  646. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  647. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  648. var pixelsize = m_Sample.CalculatePixelSize();
  649. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  650. {
  651. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  652. foreach (var grayRange in ranges)
  653. {
  654. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  655. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  656. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  657. }
  658. }
  659. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  660. // remove BES image background
  661. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  662. // check if this is an empty image
  663. if (curFldData.NoParticle())
  664. { // empty fields
  665. log.Info("ImageProcess: empty field.");
  666. return;
  667. }
  668. double dPixelSize = m_Sample.CalculatePixelSize();
  669. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  670. return;
  671. }
  672. public virtual void CollectParticlesXrayData(COTSField curFldData)
  673. {
  674. var allParts = curFldData.GetListXrayParticles();
  675. var smallparts = new List<COTSParticleClr>();
  676. var bigparts = new List<COTSParticleClr>();
  677. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  678. foreach (var part in allParts)
  679. {
  680. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  681. if (equalCircleDiameter < quantifyThreshold)
  682. {
  683. smallparts.Add(part);
  684. }
  685. else
  686. {
  687. bigparts.Add(part);
  688. }
  689. }
  690. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  691. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  692. curFldData.CreateXrayList(smallparts);
  693. curFldData.CreateXrayList(bigparts);
  694. // get x-ray parameters
  695. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  696. var workmode = pXRayParam.GetScanMode();
  697. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  698. {
  699. uint nXRayAQTime;
  700. if (bigparts.Count > 0)
  701. {
  702. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  703. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  704. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  705. }
  706. if (smallparts.Count > 0)
  707. {
  708. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  709. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  710. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  711. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  712. }
  713. }
  714. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  715. {
  716. uint nXRayAQTime;
  717. if (bigparts.Count > 0)
  718. {
  719. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  720. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  721. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  722. }
  723. if (smallparts.Count > 0)
  724. {
  725. Thread.Sleep(500);
  726. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  727. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  728. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  729. }
  730. }
  731. return;
  732. }
  733. public virtual void QuantifyParticlesXrayData(COTSField curFldData)
  734. {
  735. var parts = curFldData.GetListXrayParticles();
  736. foreach (var p in parts)
  737. {
  738. m_EDSController.QuantifyXrayByPart(p);
  739. }
  740. }
  741. public void DoHolePreview()
  742. {
  743. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  744. {
  745. //----------memorize the state of sem
  746. var semstate = new SEMStateObject();
  747. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  748. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  749. ISemController sem = m_SemHardwareMgr;
  750. double posX=0, posY=0,posR=0;
  751. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  752. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  753. Point pos = new Point((int)posX, (int)posY);
  754. PointF otsPos = new Point(0, 0);
  755. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  756. {
  757. return;
  758. }
  759. semstate.Pos = otsPos;
  760. semstate.Magnification = dMagnification;
  761. semstate.WorkingDistance = wd;
  762. autoReset.SemState = semstate;
  763. //-------------------------------------
  764. // let the main thread to know that this sample measurement starts
  765. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  766. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  767. // set current time to current time
  768. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  769. // get SEM controller to set magnification and working distance
  770. if (!SetSEMDataMrs(m_HolePreviewSample))
  771. {
  772. log.Error("DoHolePreview: fail to set SEM data.");
  773. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  774. // record end time
  775. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  776. return;
  777. }
  778. // set the BSE scan param
  779. if (!SetHoleBSEParam(m_HolePreviewSample))
  780. {
  781. log.Error("DoHolePreview: fail to set BSE param.");
  782. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  783. // record end time
  784. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  785. return;
  786. }
  787. // calculate field centers
  788. List<System.Drawing.Point> listFieldCenter;
  789. List<System.Drawing.Point> alllistFieldCenter ;
  790. if (!CalculateUnMeasuredHoleImgCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  791. {// failed to calculate field centers
  792. log.Error("DoHolePreview: failed to calculate field centers.");
  793. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  794. // record end time
  795. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  796. return;
  797. }
  798. // go through each field
  799. //int nNewFieldId = 0;
  800. for ( int i = 0; i < listFieldCenter.Count; ++i)
  801. {// check and break if stop button is clicked
  802. if (IsAborted())
  803. {// measure stopped
  804. log.Trace("DoHolePreview: measure thread is stopped.");
  805. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  806. // record end time
  807. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  808. return;
  809. }
  810. // check if sample measurement completes
  811. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  812. int nTotalFieldSize = listFieldCenter.Count;
  813. // get a field center
  814. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  815. // move SEM to the field center
  816. if (!MoveSEMToPoint(poiFieldCentre))
  817. {// failed to move SEM to the position
  818. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  819. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  820. // record end time
  821. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  822. return;
  823. }
  824. // take BSE image for the fields
  825. CBSEImgClr pBSEIamge = AcquireABSEImage();
  826. if (pBSEIamge==null)
  827. {
  828. // failed to acquire a BSE image
  829. log.Error("DoHolePreview: failed to acquire a BSE image.");
  830. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  831. // record end time
  832. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  833. return;
  834. }
  835. //BSEData
  836. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg(m_HolePreviewSample);
  837. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge,poiFieldCentre);
  838. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  839. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  840. {
  841. // measurement failed or stopped
  842. log.Error("DoHolePreview: measurement failed or stopped.");
  843. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  844. // record end time
  845. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  846. return;
  847. }
  848. //save the result to project file
  849. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  850. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  851. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  852. m_listHoleBSEImg.Add(pHoleBSEImg);
  853. }
  854. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  855. //calculate measure time
  856. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  857. // let main thread to know that this sample measurement completes
  858. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg(m_HolePreviewSample);
  859. MsgSmplEnd.InitHolePreSampleEndMsg();
  860. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  861. }
  862. }
  863. // Cumulate field data info
  864. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  865. {
  866. // get measure result items of the sample
  867. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  868. // cumulate field data info
  869. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  870. // cumulate ratio
  871. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  872. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  873. pMsrResults.SetRatio(dRatio);
  874. // go through the particles list
  875. foreach (COTSParticleClr pParticle in listParticles)
  876. {
  877. // create a measure result item
  878. pMsrResults.CumulateMeasureResult(pParticle);
  879. }
  880. m_Sample.SetMsrResults(pMsrResults);
  881. return true;
  882. }
  883. public bool SaveFieldData(COTSField fldData, string filedFileFoler)
  884. {
  885. string strFieldFileFolder = filedFileFoler;
  886. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  887. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  888. int nId = fldData.GetId();
  889. string sFieldId;
  890. sFieldId = nId.ToString();
  891. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  892. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  893. {
  894. log.Error("SaveFieldFiles: save BSE file failed.");
  895. return false;
  896. }
  897. // IncA Data list
  898. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  899. pDBFileMgr.SaveStatusDataToDB();
  900. var fldDB = pDBFileMgr.GetFieldDB();
  901. log.Warn("Start saving particle data.");
  902. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  903. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  904. fldcmds.Add(fldcmd);
  905. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  906. //remomove the invalid particles
  907. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  908. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  909. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  910. var listAnalysisPosXray = new List<CPosXrayClr>();
  911. foreach (var p in fldData.GetListXrayParticles())
  912. {
  913. listAnalysisPosXray.Add(p.GetXray());
  914. }
  915. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  916. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  917. return true;
  918. }
  919. protected void SaveFieldMgrData()
  920. {
  921. while (bSaveThreadWorking)
  922. {
  923. while (fieldQueue.Count() > 0)
  924. {
  925. COTSField f = fieldQueue.Dequeue();
  926. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  927. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  928. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  929. }
  930. if (fieldQueue.Count() == 0)
  931. {
  932. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  933. log.Warn("finished batch saving");
  934. return;
  935. }
  936. }
  937. return;
  938. }
  939. private void SendFieldParticlesInfoToGUI(COTSField curFld,CMsrSampleStatus pStatus)
  940. {
  941. double measuredArea = 0; // this area should be the field area
  942. var a_pBSEImg = curFld.GetBSEImage();
  943. double dPixelSize = curFld.GetPixelSize();
  944. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  945. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  946. log.Info("Send field data to screen!");
  947. pStatus.AddCompletedFieldCenter(curFld.GetOTSPosition());
  948. //Field Data
  949. // record end time
  950. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  951. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  952. MsgFieldEnd.InitFieldDataMsg();
  953. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  954. }
  955. protected void StartSaveFileThread(ref COTSField a_pFieldMgr)
  956. {
  957. fieldQueue.Enqueue(a_pFieldMgr);
  958. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  959. {
  960. if (bSaveThreadWorking == false)
  961. {
  962. bSaveThreadWorking = true;
  963. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  964. m_thread_ptr.IsBackground = true;
  965. m_thread_ptr.Start();
  966. }
  967. }
  968. }
  969. public void ParticleSpecialTreatment(ref COTSField fld)
  970. {
  971. //we adopt such a strategy here:if some particles satisfy the predefined condition then we go through the second collecting with max EDS time,so that we got a more acurate result.
  972. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  973. double edsTime = 0; //to store the EDSTime returned from the engine.
  974. var bigparts = fld.GetListXrayParticles();
  975. var m_ClassifyEngine = new CClassifyEngine();
  976. for (int i = 0; i < bigparts.Count(); i++)
  977. {
  978. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  979. var p = bigparts[i];
  980. // there will be very less particle satisfied the condition ,so we use only one MaxEDS time,though we set MaxEDSTime for every rule separately .Here we store the last one.
  981. //if the particle satisfied the MaxEDS condition then we go through the second colleting.Here we record the particle and the EDSTime and the corresponding xray position.
  982. edsTime = engine.IfNeedMaxEDS(p);
  983. if (edsTime > 0)
  984. {
  985. mapPartXray[p] = i;
  986. }
  987. }
  988. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  989. if (mapPartXray.Count() > 0)
  990. {
  991. foreach (var p in mapPartXray)
  992. {
  993. partsMax.Add(p.Key);
  994. }
  995. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  996. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  997. }
  998. }
  999. public void CalculateParticleAbsolutPos(ref COTSField curFldData)
  1000. {
  1001. double dPixelSize = m_Sample.CalculatePixelSize();
  1002. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1003. foreach (var p in curFldData.GetListAnalysisParticles())
  1004. {
  1005. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1006. PointF semP = new PointF(); ;
  1007. Point semPos = new Point();
  1008. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.GetOTSPosition(), ref semP);
  1009. semPos.X = (int)semP.X;
  1010. semPos.Y = (int)semP.Y;
  1011. p.SetSEMPos(semPos);
  1012. }
  1013. }
  1014. }
  1015. }