CSmplMeasure.cs 53 KB

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