CSmplMeasure.cs 50 KB

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