CSmplMeasure.cs 50 KB

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