CSmplMeasure.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using OTSModelSharp.ServiceCenter;
  5. using static OTSDataType.otsdataconst;
  6. using OTSDataType;
  7. using System.Drawing;
  8. using System.Threading;
  9. using OTSCLRINTERFACE;
  10. using OTSMeasureApp._0_OTSModel.OTSDataType;
  11. using System.Windows.Forms;
  12. using System.Data;
  13. using System.IO;
  14. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  15. namespace OTSModelSharp
  16. {
  17. using CHoleBSEImgsList = List<CHoleBSEImg>;
  18. public class CSmplMeasure
  19. {
  20. protected static NLog.Logger log ;
  21. protected bool bSaveThreadWorking;
  22. protected System.Threading.Thread m_thread_ptr;
  23. protected COTSSample m_Sample;
  24. protected COTSSample m_HolePreviewSample;
  25. protected CMeasure m_pMsrThread;
  26. string m_strWorkingFolder;
  27. protected CSmplMsrResult m_pSampleRstFile;
  28. CHoleBSEImgsList m_listHoleBSEImg;
  29. protected ISemController m_SemHardwareMgr;
  30. protected IScanController m_ScanHardwareMgr;
  31. protected IEDSController m_EDSController;
  32. protected Queue<COTSField> fieldQueue=new Queue<COTSField>();
  33. protected IClassifyEngine m_classifyEngine;
  34. 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
  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
  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 CalculateUnMeasuredHoleImgCenters(COTSSample sample, out List<System.Drawing.Point> a_allpieldcenter, out List<System.Drawing.Point> a_listUnMsrFieldCenter)
  185. {
  186. // sample measure parameters
  187. CSampleParam pMsrParam = sample.GetMsrParams();
  188. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  189. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  190. CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  191. CMsrSampleStatus pStatus = sample.GetMsrStatus();
  192. // measured field centers list
  193. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  194. // field centers list manager
  195. CFieldPositionHelper pFieldMgr = new CFieldPositionHelper();
  196. // init field centers list manager
  197. if (!pFieldMgr.Init(sample.GetMsrDomain(), poImageScanParam,0, poSEMDataMsr, listCompletedCenter))
  198. {
  199. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  200. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  201. a_allpieldcenter = new List<Point>();
  202. return false;
  203. }
  204. // get field centers list
  205. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  206. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  207. // ok, return TRUE
  208. return true;
  209. }
  210. protected bool IsAborted()
  211. {
  212. return m_pMsrThread.IsMeasureStopped();
  213. }
  214. protected bool IsPaused()
  215. {
  216. var statu = m_pMsrThread.GetMsrThreadStatus();
  217. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  218. {
  219. return true;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. }
  226. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  227. {
  228. string sStopMode = a_pScanParam.GetStopMode();
  229. int nStopField = a_pScanParam.GetStopParamFields();
  230. // completed fields number
  231. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  232. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  233. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  234. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  235. int nNumParticle = 0;
  236. foreach (var pResult in listMsrResult)
  237. {
  238. if (pResult.GetTypeId() > (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  239. {
  240. nNumParticle += (int)pResult.GetNumber();
  241. }
  242. }
  243. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  244. int nDay = timeSpan.Days;
  245. int nHour = timeSpan.Hours;
  246. int nMin = timeSpan.Minutes;
  247. int nSec = timeSpan.Seconds;
  248. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  249. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  250. int nParticlAim = a_pScanParam.GetStopParamParticles();
  251. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  252. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  253. bool bRet = false;
  254. string[] str = sStopMode.Replace(" ", "").Split('+');
  255. for(int i=0;i< str.Length;i++)
  256. {
  257. switch (int.Parse(str[i].Split(':')[0])-1)
  258. {
  259. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  260. // completed fields number
  261. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  262. {
  263. bRet = true;
  264. }
  265. break;
  266. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  267. if (nCompeltedField >= nStopField)
  268. {
  269. bRet = true;
  270. }
  271. break;
  272. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  273. if (nNumParticle >= nParticlAim)
  274. {
  275. bRet = true;
  276. }
  277. break;
  278. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  279. if (nUsedTime >= nMeasTimeAim)
  280. {
  281. bRet = true;
  282. }
  283. break;
  284. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  285. if (NMeasArea >= NMeasAreaAim)
  286. {
  287. bRet = true;
  288. }
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. return bRet;
  295. }
  296. // move SEM to the point
  297. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  298. {
  299. // get SEM controller
  300. var pSEMController = m_SemHardwareMgr;
  301. PointF a_SEMpt = new Point();
  302. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  303. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  304. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  305. {
  306. return false;
  307. }
  308. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  309. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  310. // move SEM to the position (rotation 0)
  311. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  312. {
  313. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  314. return false;
  315. }
  316. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  317. if (pMsrParam.SlopParam.IsUsingSlopParam)
  318. {
  319. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  320. double originWd = 0;
  321. pSEMController.GetWorkingDistance(ref originWd);
  322. if ((wd - originWd) > 2)
  323. {
  324. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  325. }
  326. else
  327. {
  328. Thread.Sleep(hardWareDelay);
  329. pSEMController.SetWorkingDistance(wd);
  330. }
  331. }
  332. if (hardWareDelay > 0)
  333. {
  334. Thread.Sleep(hardWareDelay);
  335. }
  336. return true;
  337. }
  338. // Acquire a BSE image
  339. CBSEImgClr AcquireABSEImage()
  340. {
  341. // BSE image
  342. CBSEImgClr pBSEImage = null;
  343. // get scan controller
  344. var pScanController = m_ScanHardwareMgr;
  345. pBSEImage = pScanController.AcquireBSEImage();
  346. return pBSEImage;
  347. }
  348. public bool IsLowCounts(COTSParticleClr particle)
  349. {
  350. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  351. if (pXRayParam.GetUsingXray() == true)
  352. {
  353. var thecount = particle.GetXray().GetTotalCount();
  354. var expect = pXRayParam.GetAnalyExpCount();
  355. if (thecount < expect)
  356. {
  357. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  358. particle.SetClassifyId((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  359. particle.SetTypeColor("#000000");
  360. particle.SetTypeName("LowCounts");
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. public virtual void ClassifyFieldParticles(COTSField curFldData)
  367. {
  368. return;
  369. }
  370. private class SEMStateObject
  371. {
  372. private PointF pos;
  373. private double workingDistance;
  374. private CSEMDataGnr semdata;
  375. private COTSSample originalSample;
  376. private double magnification;
  377. public PointF Pos { get => pos; set => pos = value; }
  378. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  379. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  380. public double Magnification { get => magnification; set => magnification = value; }
  381. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  382. }
  383. private class AutoResetSEMControl:IDisposable
  384. {
  385. CSmplMeasure sm;
  386. private SEMStateObject semState=null;
  387. public AutoResetSEMControl(CSmplMeasure s)
  388. {
  389. sm = s;
  390. }
  391. public SEMStateObject SemState
  392. {
  393. get => semState;
  394. set => semState = value;
  395. }
  396. public void Dispose()
  397. {
  398. if (semState != null)
  399. {
  400. sm.SetBSEParam();
  401. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  402. Thread.Sleep(100);
  403. sm.MoveSEMToPoint(semState.Pos);
  404. Thread.Sleep(100);
  405. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  406. }
  407. sm.SetSEMExteralOff();
  408. }
  409. }
  410. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  411. {
  412. double kv = 0, brightness = 0, contrast = 0;
  413. var hw = SemController.GetSEMController();
  414. hw.GetSemHighTension(ref kv);
  415. hw.GetSemBrightness(ref brightness);
  416. hw.GetSemContrast(ref contrast);
  417. SemDataGnr.SetValue(kv, brightness, contrast);
  418. return true;
  419. }
  420. public void DoMeasureForOneSample()
  421. {
  422. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  423. {
  424. // let the main thread to know that this sample measurement starts
  425. var pStatus = m_Sample.GetMsrStatus();
  426. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  427. // set current time to current time
  428. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  429. // let main App know that the sample begin to measure
  430. ST_MSTMsg MsgSmpStart = new ST_MSTMsg(m_Sample);
  431. MsgSmpStart.InitSampleStartMsg();
  432. m_pMsrThread.SendMessageToMeasureGUI(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. for (int i = 0; i < FldDatas.Count; ++i)
  473. {// check and break if stop button is clicked
  474. var curFld = FldDatas[i];
  475. if (curFld.GetIsMeasureComplete())
  476. {
  477. continue;
  478. }
  479. if (IsPaused())
  480. {// measure stopped
  481. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  482. // record end time
  483. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  484. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  485. while (fieldQueue.Count() > 0)
  486. {
  487. Thread.Sleep(100);
  488. }
  489. SetSEMExteralOff();
  490. // update thread measure status class, let the main thread know that this sample measurement stopped
  491. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  492. MsgSmpStop.InitSamplePausedMsg();
  493. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  494. while (IsPaused())
  495. {
  496. Thread.Sleep(300);
  497. }
  498. }
  499. if (IsAborted())
  500. {// measure stopped
  501. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  502. // record end time
  503. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  504. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  505. while (fieldQueue.Count() > 0)
  506. {
  507. Thread.Sleep(100);
  508. }
  509. break;
  510. }
  511. // check if sample measurement completes
  512. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  513. if (IsSampleOver(pScanParam))
  514. {
  515. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  516. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  517. break;
  518. }
  519. // get a field center
  520. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  521. // update thread measure status class, let the main thread know that starts a new field
  522. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample,curFld);
  523. MsgFieldStart.InitFieldStartMsg();
  524. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  525. int fldNo = curFld.GetId();
  526. log.Warn("Current field:" + fldNo.ToString());
  527. // move SEM to the field center
  528. if (!MoveSEMToPoint(poiFieldCentre))
  529. {// failed to move SEM to the position
  530. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  531. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  532. // record end time
  533. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  534. return;
  535. }
  536. log.Info("Begin to Acquire BSE image!");
  537. // take BSE image for the fields
  538. CBSEImgClr pBSEImg = AcquireABSEImage();
  539. // let the main thread to know that image process is completed
  540. if (pBSEImg == null)
  541. {
  542. log.Error("ImageProcess: can't get BSE image.");
  543. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  544. return;
  545. }
  546. curFld.SetBSEImage(pBSEImg);
  547. //BSEData
  548. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample,curFld);
  549. MsgFieldBSE.InitFieldBSEImageMsg();
  550. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  551. log.Info("Acquire BSE image success! Processing image...");
  552. // image process
  553. FieldImageProcess(curFld);
  554. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  555. if (pXRayParam.GetUsingXray() == true)
  556. {
  557. foreach (var p in curFld.GetListAnalysisParticles())
  558. {
  559. p.SetIsXrayParticle(true);
  560. }
  561. CollectParticlesXrayData(curFld);
  562. }
  563. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  564. curFld.SetIsMeasureComplete(true);
  565. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  566. ClassifyFieldParticles(curFld);
  567. //start db save
  568. StartSaveFileThread(ref curFld);
  569. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  570. }
  571. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  572. {
  573. Thread.Sleep(1000);
  574. log.Warn("db saving!");
  575. }
  576. TheLastWorkOfSampleMeasure();
  577. }
  578. }
  579. public void TheLastWorkOfSampleMeasure()
  580. {
  581. COTSSample theSample = m_Sample;
  582. var pStatus = theSample.GetMsrStatus();
  583. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  584. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  585. // let main thread to know that this sample measurement completes
  586. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  587. MsgSmplEnd.InitSampleCompleteMsg();
  588. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  589. string userdB = m_Sample.GetMsrParams().GetSTDName();
  590. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  591. {
  592. try
  593. {
  594. string userdBpath= System.Windows.Forms.Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  595. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  596. }
  597. catch(Exception ex)
  598. {
  599. MessageBox.Show("userdB copy failed:" + ex.ToString());
  600. }
  601. }
  602. }
  603. public bool DoMEasureForReMeasure()
  604. {
  605. return true;
  606. }
  607. public virtual void FieldImageProcess(COTSField curFldData)
  608. {
  609. PointF fldCenter = curFldData.GetOTSPosition();
  610. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  611. PointF semPos = new Point();
  612. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  613. curFldData.SetSemPos(semPos);
  614. //first step:remove background of the bse image and compound all the finded particles.
  615. log.Info("Begin to process image and get all particles!");
  616. GetOriginalParticles(ref curFldData);//according to the ECD scope,filter out the effective particles,remove the noise.
  617. log.Info("Begin to filter particles!");
  618. FilterParticles(ref curFldData);//filter according to the xraylimit
  619. log.Info("Begin to Calculate the image property of every particle!");
  620. var analysisparts = curFldData.GetListAnalysisParticles();
  621. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  622. SelectParticles(ref curFldData);
  623. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  624. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  625. var overlap = pImgProcessParam.GetOverlapParam();
  626. if (overlap > 0)
  627. {
  628. curFldData.RemoveDuplicateOverlapParticles(overlap);
  629. }
  630. CalculateParticleAbsolutPos(ref curFldData);
  631. curFldData.InitParticles(pImgProcessParam);
  632. return ;
  633. }
  634. private void FilterParticles(ref COTSField curFldData)
  635. {
  636. log.Info("filter particle according to xraylimit");
  637. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  638. var listXray1 = new List<COTSParticleClr>();
  639. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  640. if (listXray.Count > pXRayParam.GetXrayLimit())
  641. {
  642. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  643. {
  644. listXray1.Add(listXray[i]);
  645. }
  646. }
  647. else
  648. {
  649. listXray1 = listXray;
  650. }
  651. curFldData.SetListAnalysisParticles(listXray1);
  652. log.Info("Analysis particles:" + listXray1.Count);
  653. }
  654. private void SelectParticles(ref COTSField curFldData)
  655. {
  656. var selconditiondic = m_Sample.GetMsrParams().GetImageProcessParam().GetParticleSelConditionDic();
  657. var listselparts = new List<COTSParticleClr>();
  658. var excludeparts= new List<COTSParticleClr>();
  659. if (selconditiondic.ContainsKey("dmax"))
  660. {
  661. log.Info("Select particles according to dmax");
  662. var rng = selconditiondic["dmax"];
  663. foreach (var p in curFldData.GetListAnalysisParticles())
  664. {
  665. if (p.GetDMAX() < rng.GetStart() || p.GetDMAX() >= rng.GetEnd())
  666. {
  667. excludeparts.Add(p);
  668. }
  669. else
  670. {
  671. log.Info("dmax=" + p.GetDMAX().ToString("F2"));
  672. }
  673. }
  674. }
  675. if (selconditiondic.ContainsKey("dmin"))
  676. {
  677. log.Info("Select particles according to dmin");
  678. var rng = selconditiondic["dmin"];
  679. foreach (var p in curFldData.GetListAnalysisParticles())
  680. {
  681. if (p.GetDMIN() < rng.GetStart() || p.GetDMIN() >= rng.GetEnd())
  682. {
  683. if (!excludeparts.Contains(p))
  684. {
  685. excludeparts.Add(p);
  686. }
  687. else
  688. {
  689. log.Info("dmin=" + p.GetDMIN().ToString("F2"));
  690. }
  691. }
  692. }
  693. }
  694. if (selconditiondic.ContainsKey("orientation"))
  695. {
  696. log.Info("Select particles according to orientation");
  697. var rng = selconditiondic["orientation"];
  698. foreach (var p in curFldData.GetListAnalysisParticles())
  699. {
  700. if (p.GetORIENTATION() < rng.GetStart() || p.GetORIENTATION() >= rng.GetEnd())
  701. {
  702. if (!excludeparts.Contains(p))
  703. {
  704. excludeparts.Add(p);
  705. }
  706. else
  707. {
  708. log.Info("orientation=" + p.GetORIENTATION().ToString("F2"));
  709. }
  710. }
  711. }
  712. }
  713. if (selconditiondic.ContainsKey("aspect"))
  714. {
  715. log.Info("Select particles according to aspect");
  716. var rng = selconditiondic["aspect"];
  717. foreach (var p in curFldData.GetListAnalysisParticles())
  718. {
  719. double aspect = p.GetDMAX() / p.GetDMIN();
  720. if (aspect < rng.GetStart() || aspect >= rng.GetEnd())
  721. {
  722. if (!excludeparts.Contains(p))
  723. {
  724. excludeparts.Add(p);
  725. }
  726. else
  727. {
  728. log.Info("aspect=" + aspect.ToString("F2"));
  729. }
  730. }
  731. }
  732. }
  733. if (selconditiondic.ContainsKey("ferret"))
  734. {
  735. log.Info("Select particles according to ferret");
  736. var rng = selconditiondic["ferret"];
  737. foreach (var p in curFldData.GetListAnalysisParticles())
  738. {
  739. double dferet = p.GetFeretDiameter();
  740. if (dferet < rng.GetStart() || dferet >= rng.GetEnd())
  741. {
  742. if (!excludeparts.Contains(p))
  743. {
  744. excludeparts.Add(p);
  745. }
  746. else
  747. {
  748. log.Info("ferret=" + dferet.ToString("F2"));
  749. }
  750. }
  751. }
  752. }
  753. foreach (var p in curFldData.GetListAnalysisParticles())
  754. {
  755. if (!excludeparts.Contains(p))
  756. {
  757. listselparts.Add(p);
  758. }
  759. }
  760. curFldData.SetListAnalysisParticles(listselparts);
  761. log.Info("Analysis particles:" + listselparts.Count);
  762. }
  763. public void GetOriginalParticles(ref COTSField curFldData)
  764. {
  765. // get image process parameter
  766. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  767. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  768. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  769. var pixelsize = m_Sample.CalculatePixelSize();
  770. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  771. {
  772. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  773. foreach (var grayRange in ranges)
  774. {
  775. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  776. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  777. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  778. }
  779. }
  780. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  781. // remove BES image background
  782. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  783. log.Info("Find all particle num:" +curFldData.GetAllParticles().Count);
  784. return;
  785. }
  786. public virtual void CollectParticlesXrayData(COTSField curFldData)
  787. {
  788. var allParts = curFldData.GetListXrayParticles();
  789. var smallparts = new List<COTSParticleClr>();
  790. var bigparts = new List<COTSParticleClr>();
  791. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  792. foreach (var part in allParts)
  793. {
  794. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  795. if (equalCircleDiameter < quantifyThreshold)
  796. {
  797. smallparts.Add(part);
  798. }
  799. else
  800. {
  801. bigparts.Add(part);
  802. }
  803. }
  804. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  805. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  806. curFldData.CreateXrayList(smallparts);
  807. curFldData.CreateXrayList(bigparts);
  808. // get x-ray parameters
  809. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  810. var workmode = pXRayParam.GetScanMode();
  811. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  812. {
  813. uint nXRayAQTime;
  814. if (bigparts.Count > 0)
  815. {
  816. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  817. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  818. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  819. }
  820. if (smallparts.Count > 0)
  821. {
  822. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  823. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  824. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  825. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  826. }
  827. }
  828. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  829. {
  830. uint nXRayAQTime;
  831. if (bigparts.Count > 0)
  832. {
  833. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  834. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  835. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  836. }
  837. if (smallparts.Count > 0)
  838. {
  839. Thread.Sleep(500);
  840. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  841. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  842. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  843. }
  844. }
  845. return;
  846. }
  847. public virtual void QuantifyParticlesXrayData(COTSField curFldData)
  848. {
  849. var parts = curFldData.GetListXrayParticles();
  850. foreach (var p in parts)
  851. {
  852. m_EDSController.QuantifyXrayByPart(p);
  853. }
  854. }
  855. public void DoHolePreview()
  856. {
  857. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  858. {
  859. //----------memorize the state of sem
  860. var semstate = new SEMStateObject();
  861. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  862. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  863. ISemController sem = m_SemHardwareMgr;
  864. double posX=0, posY=0,posR=0;
  865. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  866. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  867. Point pos = new Point((int)posX, (int)posY);
  868. PointF otsPos = new Point(0, 0);
  869. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  870. {
  871. return;
  872. }
  873. semstate.Pos = otsPos;
  874. semstate.Magnification = dMagnification;
  875. semstate.WorkingDistance = wd;
  876. autoReset.SemState = semstate;
  877. //-------------------------------------
  878. // let the main thread to know that this sample measurement starts
  879. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  880. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  881. // set current time to current time
  882. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  883. // get SEM controller to set magnification and working distance
  884. if (!SetSEMDataMrs(m_HolePreviewSample))
  885. {
  886. log.Error("DoHolePreview: fail to set SEM data.");
  887. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  888. // record end time
  889. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  890. return;
  891. }
  892. // set the BSE scan param
  893. if (!SetHoleBSEParam(m_HolePreviewSample))
  894. {
  895. log.Error("DoHolePreview: fail to set BSE param.");
  896. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  897. // record end time
  898. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  899. return;
  900. }
  901. // calculate field centers
  902. List<System.Drawing.Point> listFieldCenter;
  903. List<System.Drawing.Point> alllistFieldCenter ;
  904. if (!CalculateUnMeasuredHoleImgCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  905. {// failed to calculate field centers
  906. log.Error("DoHolePreview: failed to calculate field centers.");
  907. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  908. // record end time
  909. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  910. return;
  911. }
  912. // go through each field
  913. //int nNewFieldId = 0;
  914. for ( int i = 0; i < listFieldCenter.Count; ++i)
  915. {// check and break if stop button is clicked
  916. if (IsAborted())
  917. {// measure stopped
  918. log.Trace("DoHolePreview: measure thread is stopped.");
  919. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  920. // record end time
  921. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  922. return;
  923. }
  924. // check if sample measurement completes
  925. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  926. int nTotalFieldSize = listFieldCenter.Count;
  927. // get a field center
  928. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  929. // move SEM to the field center
  930. if (!MoveSEMToPoint(poiFieldCentre))
  931. {// failed to move SEM to the position
  932. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  933. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  934. // record end time
  935. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  936. return;
  937. }
  938. // take BSE image for the fields
  939. CBSEImgClr pBSEIamge = AcquireABSEImage();
  940. if (pBSEIamge==null)
  941. {
  942. // failed to acquire a BSE image
  943. log.Error("DoHolePreview: failed to acquire a BSE image.");
  944. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  945. // record end time
  946. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  947. return;
  948. }
  949. //BSEData
  950. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg(m_HolePreviewSample);
  951. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge,poiFieldCentre);
  952. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  953. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  954. {
  955. // measurement failed or stopped
  956. log.Error("DoHolePreview: measurement failed or stopped.");
  957. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  958. // record end time
  959. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  960. return;
  961. }
  962. //save the result to project file
  963. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  964. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  965. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  966. m_listHoleBSEImg.Add(pHoleBSEImg);
  967. }
  968. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  969. //calculate measure time
  970. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  971. // let main thread to know that this sample measurement completes
  972. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg(m_HolePreviewSample);
  973. MsgSmplEnd.InitHolePreSampleEndMsg();
  974. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  975. }
  976. }
  977. // Cumulate field data info
  978. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  979. {
  980. // get measure result items of the sample
  981. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  982. // go through the particles list
  983. foreach (COTSParticleClr pParticle in listParticles)
  984. {
  985. // create a measure result item
  986. pMsrResults.CumulateMeasureResult(pParticle);
  987. }
  988. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  989. // cumulate ratio
  990. double dRatio = pMsrResults.GetTotalParticleArea();
  991. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  992. pMsrResults.SetRatio(dRatio);
  993. m_Sample.SetMsrResults(pMsrResults);
  994. return true;
  995. }
  996. public bool SaveFieldData(COTSField fldData, string filedFileFoler)
  997. {
  998. string strFieldFileFolder = filedFileFoler;
  999. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  1000. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  1001. int nId = fldData.GetId();
  1002. string sFieldId;
  1003. sFieldId = nId.ToString();
  1004. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  1005. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  1006. {
  1007. log.Error("SaveFieldFiles: save BSE file failed.");
  1008. return false;
  1009. }
  1010. // IncA Data list
  1011. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  1012. pDBFileMgr.SaveStatusDataToDB();
  1013. var fldDB = pDBFileMgr.GetFieldDB();
  1014. log.Warn("Start saving particle data.");
  1015. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  1016. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  1017. fldcmds.Add(fldcmd);
  1018. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  1019. //remomove the invalid particles
  1020. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  1021. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  1022. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  1023. var listAnalysisPosXray = new List<CPosXrayClr>();
  1024. foreach (var p in fldData.GetListXrayParticles())
  1025. {
  1026. listAnalysisPosXray.Add(p.GetXray());
  1027. }
  1028. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  1029. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1030. return true;
  1031. }
  1032. protected void SaveFieldMgrData()
  1033. {
  1034. while (bSaveThreadWorking)
  1035. {
  1036. while (fieldQueue.Count() > 0)
  1037. {
  1038. COTSField f = fieldQueue.Dequeue();
  1039. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1040. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  1041. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1042. }
  1043. if (fieldQueue.Count() == 0)
  1044. {
  1045. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1046. log.Warn("finished batch saving");
  1047. return;
  1048. }
  1049. }
  1050. return;
  1051. }
  1052. private void SendFieldParticlesInfoToGUI(COTSField curFld,CMsrSampleStatus pStatus)
  1053. {
  1054. double measuredArea = 0; // this area should be the field area
  1055. var a_pBSEImg = curFld.GetBSEImage();
  1056. double dPixelSize = curFld.GetPixelSize();
  1057. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  1058. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  1059. log.Info("Send field data to screen!");
  1060. pStatus.AddCompletedFieldCenter(curFld.GetOTSPosition());
  1061. //Field Data
  1062. // record end time
  1063. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  1064. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  1065. MsgFieldEnd.InitFieldDataMsg();
  1066. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  1067. }
  1068. protected void StartSaveFileThread(ref COTSField a_pFieldMgr)
  1069. {
  1070. fieldQueue.Enqueue(a_pFieldMgr);
  1071. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1072. {
  1073. if (bSaveThreadWorking == false)
  1074. {
  1075. bSaveThreadWorking = true;
  1076. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1077. m_thread_ptr.IsBackground = true;
  1078. m_thread_ptr.Start();
  1079. }
  1080. }
  1081. }
  1082. public void ParticleSpecialTreatment(ref COTSField fld)
  1083. {
  1084. //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.
  1085. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1086. double edsTime = 0; //to store the EDSTime returned from the engine.
  1087. var bigparts = fld.GetListXrayParticles();
  1088. var m_ClassifyEngine = new CClassifyEngine();
  1089. for (int i = 0; i < bigparts.Count(); i++)
  1090. {
  1091. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  1092. var p = bigparts[i];
  1093. // 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.
  1094. //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.
  1095. edsTime = engine.IfNeedMaxEDS(p);
  1096. if (edsTime > 0)
  1097. {
  1098. mapPartXray[p] = i;
  1099. }
  1100. }
  1101. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1102. if (mapPartXray.Count() > 0)
  1103. {
  1104. foreach (var p in mapPartXray)
  1105. {
  1106. partsMax.Add(p.Key);
  1107. }
  1108. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1109. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  1110. }
  1111. }
  1112. public void CalculateParticleAbsolutPos(ref COTSField curFldData)
  1113. {
  1114. double dPixelSize = m_Sample.CalculatePixelSize();
  1115. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1116. foreach (var p in curFldData.GetListAnalysisParticles())
  1117. {
  1118. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1119. PointF semP = new PointF(); ;
  1120. Point semPos = new Point();
  1121. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.GetOTSPosition(), ref semP);
  1122. semPos.X = (int)semP.X;
  1123. semPos.Y = (int)semP.Y;
  1124. p.SetSEMPos(semPos);
  1125. }
  1126. }
  1127. }
  1128. }