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