CSmplMeasure.cs 49 KB

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