CSmplMeasure.cs 50 KB

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