CSmplMeasure.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. foreach (var p in curFld.GetListAnalysisParticles())
  557. {
  558. p.SetIsXrayParticle(true);
  559. }
  560. CollectParticlesXrayData(curFld);
  561. }
  562. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  563. curFld.SetIsMeasureComplete(true);
  564. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  565. ClassifyFieldParticles(curFld);
  566. //start db save
  567. StartSaveFileThread(ref curFld);
  568. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  569. }
  570. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  571. {
  572. Thread.Sleep(1000);
  573. log.Warn("db saving!");
  574. }
  575. TheLastWorkOfSampleMeasure();
  576. }
  577. }
  578. public void TheLastWorkOfSampleMeasure()
  579. {
  580. COTSSample theSample = m_Sample;
  581. var pStatus = theSample.GetMsrStatus();
  582. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  583. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  584. // let main thread to know that this sample measurement completes
  585. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  586. MsgSmplEnd.InitSampleCompleteMsg();
  587. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  588. string userdB = m_Sample.GetMsrParams().GetSTDName();
  589. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  590. {
  591. try
  592. {
  593. string userdBpath= Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  594. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  595. }
  596. catch(Exception ex)
  597. {
  598. MessageBox.Show("userdB copy failed:" + ex.ToString());
  599. }
  600. }
  601. }
  602. public bool DoMEasureForReMeasure()
  603. {
  604. return true;
  605. }
  606. public virtual void FieldImageProcess(COTSField curFldData)
  607. {
  608. PointF fldCenter = curFldData.GetOTSPosition();
  609. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  610. PointF semPos = new Point();
  611. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  612. curFldData.SetSemPos(semPos);
  613. //first step:remove background of the bse image and compound all the finded particles.
  614. log.Info("Begin to process image and get all particles!");
  615. GetOriginalParticles(ref curFldData);
  616. log.Info("Begin to filter particles!");
  617. FilterParticles(ref curFldData);//filter according to the xraylimit
  618. log.Info("Begin to Calculate the image property of every particle!");
  619. var analysisparts = curFldData.GetListAnalysisParticles();
  620. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  621. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  622. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  623. var overlap = pImgProcessParam.GetOverlapParam();
  624. if (overlap > 0)
  625. {
  626. curFldData.RemoveDuplicateOverlapParticles(overlap);
  627. }
  628. CalculateParticleAbsolutPos(ref curFldData);
  629. curFldData.InitParticles(pImgProcessParam);
  630. return ;
  631. }
  632. private void FilterParticles(ref COTSField curFldData)
  633. {
  634. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  635. var listXray1 = new List<COTSParticleClr>();
  636. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  637. if (listXray.Count > pXRayParam.GetXrayLimit())
  638. {
  639. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  640. {
  641. listXray1.Add(listXray[i]);
  642. }
  643. }
  644. else
  645. {
  646. listXray1 = listXray;
  647. }
  648. curFldData.SetListAnalysisParticles(listXray1);
  649. log.Info("Analysis particles:" + listXray1.Count);
  650. }
  651. public void GetOriginalParticles(ref COTSField curFldData)
  652. {
  653. // get image process parameter
  654. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  655. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  656. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  657. var pixelsize = m_Sample.CalculatePixelSize();
  658. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  659. {
  660. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  661. foreach (var grayRange in ranges)
  662. {
  663. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  664. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  665. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  666. }
  667. }
  668. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  669. // remove BES image background
  670. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  671. log.Info("Find all particle num:" +curFldData.GetAllParticles().Count);
  672. return;
  673. }
  674. public virtual void CollectParticlesXrayData(COTSField curFldData)
  675. {
  676. var allParts = curFldData.GetListXrayParticles();
  677. var smallparts = new List<COTSParticleClr>();
  678. var bigparts = new List<COTSParticleClr>();
  679. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  680. foreach (var part in allParts)
  681. {
  682. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  683. if (equalCircleDiameter < quantifyThreshold)
  684. {
  685. smallparts.Add(part);
  686. }
  687. else
  688. {
  689. bigparts.Add(part);
  690. }
  691. }
  692. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  693. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  694. curFldData.CreateXrayList(smallparts);
  695. curFldData.CreateXrayList(bigparts);
  696. // get x-ray parameters
  697. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  698. var workmode = pXRayParam.GetScanMode();
  699. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  700. {
  701. uint nXRayAQTime;
  702. if (bigparts.Count > 0)
  703. {
  704. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  705. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  706. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  707. }
  708. if (smallparts.Count > 0)
  709. {
  710. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  711. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  712. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  713. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  714. }
  715. }
  716. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  717. {
  718. uint nXRayAQTime;
  719. if (bigparts.Count > 0)
  720. {
  721. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  722. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  723. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  724. }
  725. if (smallparts.Count > 0)
  726. {
  727. Thread.Sleep(500);
  728. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  729. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  730. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  731. }
  732. }
  733. return;
  734. }
  735. public virtual void QuantifyParticlesXrayData(COTSField curFldData)
  736. {
  737. var parts = curFldData.GetListXrayParticles();
  738. foreach (var p in parts)
  739. {
  740. m_EDSController.QuantifyXrayByPart(p);
  741. }
  742. }
  743. public void DoHolePreview()
  744. {
  745. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  746. {
  747. //----------memorize the state of sem
  748. var semstate = new SEMStateObject();
  749. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  750. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  751. ISemController sem = m_SemHardwareMgr;
  752. double posX=0, posY=0,posR=0;
  753. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  754. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  755. Point pos = new Point((int)posX, (int)posY);
  756. PointF otsPos = new Point(0, 0);
  757. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  758. {
  759. return;
  760. }
  761. semstate.Pos = otsPos;
  762. semstate.Magnification = dMagnification;
  763. semstate.WorkingDistance = wd;
  764. autoReset.SemState = semstate;
  765. //-------------------------------------
  766. // let the main thread to know that this sample measurement starts
  767. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  768. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  769. // set current time to current time
  770. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  771. // get SEM controller to set magnification and working distance
  772. if (!SetSEMDataMrs(m_HolePreviewSample))
  773. {
  774. log.Error("DoHolePreview: fail to set SEM data.");
  775. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  776. // record end time
  777. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  778. return;
  779. }
  780. // set the BSE scan param
  781. if (!SetHoleBSEParam(m_HolePreviewSample))
  782. {
  783. log.Error("DoHolePreview: fail to set BSE param.");
  784. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  785. // record end time
  786. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  787. return;
  788. }
  789. // calculate field centers
  790. List<System.Drawing.Point> listFieldCenter;
  791. List<System.Drawing.Point> alllistFieldCenter ;
  792. if (!CalculateUnMeasuredHoleImgCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  793. {// failed to calculate field centers
  794. log.Error("DoHolePreview: failed to calculate field centers.");
  795. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  796. // record end time
  797. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  798. return;
  799. }
  800. // go through each field
  801. //int nNewFieldId = 0;
  802. for ( int i = 0; i < listFieldCenter.Count; ++i)
  803. {// check and break if stop button is clicked
  804. if (IsAborted())
  805. {// measure stopped
  806. log.Trace("DoHolePreview: measure thread is stopped.");
  807. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  808. // record end time
  809. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  810. return;
  811. }
  812. // check if sample measurement completes
  813. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  814. int nTotalFieldSize = listFieldCenter.Count;
  815. // get a field center
  816. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  817. // move SEM to the field center
  818. if (!MoveSEMToPoint(poiFieldCentre))
  819. {// failed to move SEM to the position
  820. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  821. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  822. // record end time
  823. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  824. return;
  825. }
  826. // take BSE image for the fields
  827. CBSEImgClr pBSEIamge = AcquireABSEImage();
  828. if (pBSEIamge==null)
  829. {
  830. // failed to acquire a BSE image
  831. log.Error("DoHolePreview: failed to acquire a BSE image.");
  832. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  833. // record end time
  834. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  835. return;
  836. }
  837. //BSEData
  838. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg(m_HolePreviewSample);
  839. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge,poiFieldCentre);
  840. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  841. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  842. {
  843. // measurement failed or stopped
  844. log.Error("DoHolePreview: measurement failed or stopped.");
  845. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  846. // record end time
  847. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  848. return;
  849. }
  850. //save the result to project file
  851. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  852. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  853. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  854. m_listHoleBSEImg.Add(pHoleBSEImg);
  855. }
  856. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  857. //calculate measure time
  858. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  859. // let main thread to know that this sample measurement completes
  860. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg(m_HolePreviewSample);
  861. MsgSmplEnd.InitHolePreSampleEndMsg();
  862. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  863. }
  864. }
  865. // Cumulate field data info
  866. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  867. {
  868. // get measure result items of the sample
  869. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  870. // cumulate field data info
  871. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  872. // cumulate ratio
  873. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  874. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  875. pMsrResults.SetRatio(dRatio);
  876. // go through the particles list
  877. foreach (COTSParticleClr pParticle in listParticles)
  878. {
  879. // create a measure result item
  880. pMsrResults.CumulateMeasureResult(pParticle);
  881. }
  882. m_Sample.SetMsrResults(pMsrResults);
  883. return true;
  884. }
  885. public bool SaveFieldData(COTSField fldData, string filedFileFoler)
  886. {
  887. string strFieldFileFolder = filedFileFoler;
  888. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  889. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  890. int nId = fldData.GetId();
  891. string sFieldId;
  892. sFieldId = nId.ToString();
  893. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  894. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  895. {
  896. log.Error("SaveFieldFiles: save BSE file failed.");
  897. return false;
  898. }
  899. // IncA Data list
  900. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  901. pDBFileMgr.SaveStatusDataToDB();
  902. var fldDB = pDBFileMgr.GetFieldDB();
  903. log.Warn("Start saving particle data.");
  904. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  905. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  906. fldcmds.Add(fldcmd);
  907. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  908. //remomove the invalid particles
  909. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  910. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  911. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  912. var listAnalysisPosXray = new List<CPosXrayClr>();
  913. foreach (var p in fldData.GetListXrayParticles())
  914. {
  915. listAnalysisPosXray.Add(p.GetXray());
  916. }
  917. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  918. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  919. return true;
  920. }
  921. protected void SaveFieldMgrData()
  922. {
  923. while (bSaveThreadWorking)
  924. {
  925. while (fieldQueue.Count() > 0)
  926. {
  927. COTSField f = fieldQueue.Dequeue();
  928. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  929. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  930. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  931. }
  932. if (fieldQueue.Count() == 0)
  933. {
  934. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  935. log.Warn("finished batch saving");
  936. return;
  937. }
  938. }
  939. return;
  940. }
  941. private void SendFieldParticlesInfoToGUI(COTSField curFld,CMsrSampleStatus pStatus)
  942. {
  943. double measuredArea = 0; // this area should be the field area
  944. var a_pBSEImg = curFld.GetBSEImage();
  945. double dPixelSize = curFld.GetPixelSize();
  946. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  947. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  948. log.Info("Send field data to screen!");
  949. pStatus.AddCompletedFieldCenter(curFld.GetOTSPosition());
  950. //Field Data
  951. // record end time
  952. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  953. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  954. MsgFieldEnd.InitFieldDataMsg();
  955. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  956. }
  957. protected void StartSaveFileThread(ref COTSField a_pFieldMgr)
  958. {
  959. fieldQueue.Enqueue(a_pFieldMgr);
  960. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  961. {
  962. if (bSaveThreadWorking == false)
  963. {
  964. bSaveThreadWorking = true;
  965. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  966. m_thread_ptr.IsBackground = true;
  967. m_thread_ptr.Start();
  968. }
  969. }
  970. }
  971. public void ParticleSpecialTreatment(ref COTSField fld)
  972. {
  973. //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.
  974. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  975. double edsTime = 0; //to store the EDSTime returned from the engine.
  976. var bigparts = fld.GetListXrayParticles();
  977. var m_ClassifyEngine = new CClassifyEngine();
  978. for (int i = 0; i < bigparts.Count(); i++)
  979. {
  980. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  981. var p = bigparts[i];
  982. // 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.
  983. //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.
  984. edsTime = engine.IfNeedMaxEDS(p);
  985. if (edsTime > 0)
  986. {
  987. mapPartXray[p] = i;
  988. }
  989. }
  990. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  991. if (mapPartXray.Count() > 0)
  992. {
  993. foreach (var p in mapPartXray)
  994. {
  995. partsMax.Add(p.Key);
  996. }
  997. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  998. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  999. }
  1000. }
  1001. public void CalculateParticleAbsolutPos(ref COTSField curFldData)
  1002. {
  1003. double dPixelSize = m_Sample.CalculatePixelSize();
  1004. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1005. foreach (var p in curFldData.GetListAnalysisParticles())
  1006. {
  1007. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1008. PointF semP = new PointF(); ;
  1009. Point semPos = new Point();
  1010. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.GetOTSPosition(), ref semP);
  1011. semPos.X = (int)semP.X;
  1012. semPos.Y = (int)semP.Y;
  1013. p.SetSEMPos(semPos);
  1014. }
  1015. }
  1016. }
  1017. }