CSmplMeasure.cs 49 KB

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