CSmplMeasure.cs 50 KB

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