CSmplMeasure.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 (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 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.SetTypeColor("#000000");
  359. particle.SetTypeName("LowCounts");
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. public virtual void ClassifyFieldParticles(COTSField curFldData)
  366. {
  367. return;
  368. }
  369. private class SEMStateObject
  370. {
  371. private PointF pos;
  372. private double workingDistance;
  373. private CSEMDataGnr semdata;
  374. private COTSSample originalSample;
  375. private double magnification;
  376. public PointF Pos { get => pos; set => pos = value; }
  377. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  378. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  379. public double Magnification { get => magnification; set => magnification = value; }
  380. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  381. }
  382. private class AutoResetSEMControl:IDisposable
  383. {
  384. CSmplMeasure sm;
  385. private SEMStateObject semState=null;
  386. public AutoResetSEMControl(CSmplMeasure s)
  387. {
  388. sm = s;
  389. }
  390. public SEMStateObject SemState
  391. {
  392. get => semState;
  393. set => semState = value;
  394. }
  395. public void Dispose()
  396. {
  397. if (semState != null)
  398. {
  399. sm.SetBSEParam();
  400. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  401. Thread.Sleep(100);
  402. sm.MoveSEMToPoint(semState.Pos);
  403. Thread.Sleep(100);
  404. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  405. }
  406. sm.SetSEMExteralOff();
  407. }
  408. }
  409. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  410. {
  411. double kv = 0, brightness = 0, contrast = 0;
  412. var hw = SemController.GetSEMController();
  413. hw.GetSemHighTension(ref kv);
  414. hw.GetSemBrightness(ref brightness);
  415. hw.GetSemContrast(ref contrast);
  416. SemDataGnr.SetValue(kv, brightness, contrast);
  417. return true;
  418. }
  419. public void DoMeasureForOneSample()
  420. {
  421. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  422. {
  423. // let the main thread to know that this sample measurement starts
  424. var pStatus = m_Sample.GetMsrStatus();
  425. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  426. // set current time to current time
  427. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  428. // let main App know that the sample begin to measure
  429. ST_MSTMsg MsgSmpStart = new ST_MSTMsg(m_Sample);
  430. MsgSmpStart.InitSampleStartMsg();
  431. m_pMsrThread.SendMessageToMeasureGUI(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. for (int i = 0; i < FldDatas.Count; ++i)
  472. {// check and break if stop button is clicked
  473. var curFld = FldDatas[i];
  474. if (curFld.GetIsMeasureComplete())
  475. {
  476. continue;
  477. }
  478. if (IsPaused())
  479. {// measure stopped
  480. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  481. // record end time
  482. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  483. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  484. while (fieldQueue.Count() > 0)
  485. {
  486. Thread.Sleep(100);
  487. }
  488. SetSEMExteralOff();
  489. // update thread measure status class, let the main thread know that this sample measurement stopped
  490. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  491. MsgSmpStop.InitSamplePausedMsg();
  492. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  493. while (IsPaused())
  494. {
  495. Thread.Sleep(300);
  496. }
  497. }
  498. if (IsAborted())
  499. {// measure stopped
  500. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  501. // record end time
  502. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  503. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  504. while (fieldQueue.Count() > 0)
  505. {
  506. Thread.Sleep(100);
  507. }
  508. break;
  509. }
  510. // check if sample measurement completes
  511. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  512. if (IsSampleOver(pScanParam))
  513. {
  514. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  515. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  516. break;
  517. }
  518. // get a field center
  519. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  520. // update thread measure status class, let the main thread know that starts a new field
  521. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample,curFld);
  522. MsgFieldStart.InitFieldStartMsg();
  523. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  524. int fldNo = curFld.GetId();
  525. log.Warn("Current field:" + fldNo.ToString());
  526. // move SEM to the field center
  527. if (!MoveSEMToPoint(poiFieldCentre))
  528. {// failed to move SEM to the position
  529. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  530. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  531. // record end time
  532. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  533. return;
  534. }
  535. log.Info("Begin to Acquire BSE image!");
  536. // take BSE image for the fields
  537. CBSEImgClr pBSEImg = AcquireABSEImage();
  538. // let the main thread to know that image process is completed
  539. if (pBSEImg == null)
  540. {
  541. log.Error("ImageProcess: can't get no background image.");
  542. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  543. return;
  544. }
  545. curFld.SetBSEImage(pBSEImg);
  546. //BSEData
  547. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample,curFld);
  548. MsgFieldBSE.InitFieldBSEImageMsg();
  549. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  550. log.Info("Acquire BSE image success! Processing image...");
  551. // image process
  552. FieldImageProcess(curFld);
  553. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  554. if (pXRayParam.GetUsingXray() == true)
  555. {
  556. foreach (var p in curFld.GetListAnalysisParticles())
  557. {
  558. p.SetIsXrayParticle(true);
  559. }
  560. CollectParticlesXrayData(curFld);
  561. }
  562. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  563. curFld.SetIsMeasureComplete(true);
  564. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  565. ClassifyFieldParticles(curFld);
  566. //start db save
  567. StartSaveFileThread(ref curFld);
  568. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  569. }
  570. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  571. {
  572. Thread.Sleep(1000);
  573. log.Warn("db saving!");
  574. }
  575. TheLastWorkOfSampleMeasure();
  576. }
  577. }
  578. public void TheLastWorkOfSampleMeasure()
  579. {
  580. COTSSample theSample = m_Sample;
  581. var pStatus = theSample.GetMsrStatus();
  582. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  583. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  584. // let main thread to know that this sample measurement completes
  585. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  586. MsgSmplEnd.InitSampleCompleteMsg();
  587. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  588. string userdB = m_Sample.GetMsrParams().GetSTDName();
  589. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  590. {
  591. try
  592. {
  593. string userdBpath= System.Windows.Forms.Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  594. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  595. }
  596. catch(Exception ex)
  597. {
  598. MessageBox.Show("userdB copy failed:" + ex.ToString());
  599. }
  600. }
  601. }
  602. public bool DoMEasureForReMeasure()
  603. {
  604. return true;
  605. }
  606. public virtual void FieldImageProcess(COTSField curFldData)
  607. {
  608. PointF fldCenter = curFldData.GetOTSPosition();
  609. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  610. PointF semPos = new Point();
  611. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  612. curFldData.SetSemPos(semPos);
  613. //first step:remove background of the bse image and compound all the finded particles.
  614. log.Info("Begin to process image and get all particles!");
  615. GetOriginalParticles(ref curFldData);
  616. log.Info("Begin to filter particles!");
  617. FilterParticles(ref curFldData);//filter according to the xraylimit
  618. log.Info("Begin to Calculate the image property of every particle!");
  619. var analysisparts = curFldData.GetListAnalysisParticles();
  620. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  621. SelectParticles(ref curFldData);
  622. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  623. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  624. var overlap = pImgProcessParam.GetOverlapParam();
  625. if (overlap > 0)
  626. {
  627. curFldData.RemoveDuplicateOverlapParticles(overlap);
  628. }
  629. CalculateParticleAbsolutPos(ref curFldData);
  630. curFldData.InitParticles(pImgProcessParam);
  631. return ;
  632. }
  633. private void FilterParticles(ref COTSField curFldData)
  634. {
  635. log.Info("filter particle according to xraylimit");
  636. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  637. var listXray1 = new List<COTSParticleClr>();
  638. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  639. if (listXray.Count > pXRayParam.GetXrayLimit())
  640. {
  641. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  642. {
  643. listXray1.Add(listXray[i]);
  644. }
  645. }
  646. else
  647. {
  648. listXray1 = listXray;
  649. }
  650. curFldData.SetListAnalysisParticles(listXray1);
  651. log.Info("Analysis particles:" + listXray1.Count);
  652. }
  653. private void SelectParticles(ref COTSField curFldData)
  654. {
  655. var selconditiondic = m_Sample.GetMsrParams().GetImageProcessParam().GetParticleSelConditionDic();
  656. var listselparts = new List<COTSParticleClr>();
  657. var excludeparts= new List<COTSParticleClr>();
  658. if (selconditiondic.ContainsKey("dmax"))
  659. {
  660. log.Info("Select particles according to dmax");
  661. var rng = selconditiondic["dmax"];
  662. foreach (var p in curFldData.GetListAnalysisParticles())
  663. {
  664. if (p.GetDMAX() < rng.GetStart() || p.GetDMAX() >= rng.GetEnd())
  665. {
  666. excludeparts.Add(p);
  667. }
  668. else
  669. {
  670. log.Info("dmax=" + p.GetDMAX().ToString("F2"));
  671. }
  672. }
  673. }
  674. if (selconditiondic.ContainsKey("dmin"))
  675. {
  676. log.Info("Select particles according to dmin");
  677. var rng = selconditiondic["dmin"];
  678. foreach (var p in curFldData.GetListAnalysisParticles())
  679. {
  680. if (p.GetDMIN() < rng.GetStart() || p.GetDMIN() >= rng.GetEnd())
  681. {
  682. if (!excludeparts.Contains(p))
  683. {
  684. excludeparts.Add(p);
  685. }
  686. else
  687. {
  688. log.Info("dmin=" + p.GetDMIN().ToString("F2"));
  689. }
  690. }
  691. }
  692. }
  693. if (selconditiondic.ContainsKey("orientation"))
  694. {
  695. log.Info("Select particles according to orientation");
  696. var rng = selconditiondic["orientation"];
  697. foreach (var p in curFldData.GetListAnalysisParticles())
  698. {
  699. if (p.GetORIENTATION() < rng.GetStart() || p.GetORIENTATION() >= rng.GetEnd())
  700. {
  701. if (!excludeparts.Contains(p))
  702. {
  703. excludeparts.Add(p);
  704. }
  705. else
  706. {
  707. log.Info("orientation=" + p.GetORIENTATION().ToString("F2"));
  708. }
  709. }
  710. }
  711. }
  712. if (selconditiondic.ContainsKey("aspect"))
  713. {
  714. log.Info("Select particles according to aspect");
  715. var rng = selconditiondic["aspect"];
  716. foreach (var p in curFldData.GetListAnalysisParticles())
  717. {
  718. double aspect = p.GetDMAX() / p.GetDMIN();
  719. if (aspect < rng.GetStart() || aspect >= rng.GetEnd())
  720. {
  721. if (!excludeparts.Contains(p))
  722. {
  723. excludeparts.Add(p);
  724. }
  725. else
  726. {
  727. log.Info("aspect=" + aspect.ToString("F2"));
  728. }
  729. }
  730. }
  731. }
  732. foreach (var p in curFldData.GetListAnalysisParticles())
  733. {
  734. if (!excludeparts.Contains(p))
  735. {
  736. listselparts.Add(p);
  737. }
  738. }
  739. curFldData.SetListAnalysisParticles(listselparts);
  740. log.Info("Analysis particles:" + listselparts.Count);
  741. }
  742. public void GetOriginalParticles(ref COTSField curFldData)
  743. {
  744. // get image process parameter
  745. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  746. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  747. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  748. var pixelsize = m_Sample.CalculatePixelSize();
  749. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  750. {
  751. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  752. foreach (var grayRange in ranges)
  753. {
  754. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  755. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  756. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  757. }
  758. }
  759. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  760. // remove BES image background
  761. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  762. log.Info("Find all particle num:" +curFldData.GetAllParticles().Count);
  763. return;
  764. }
  765. public virtual void CollectParticlesXrayData(COTSField curFldData)
  766. {
  767. var allParts = curFldData.GetListXrayParticles();
  768. var smallparts = new List<COTSParticleClr>();
  769. var bigparts = new List<COTSParticleClr>();
  770. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  771. foreach (var part in allParts)
  772. {
  773. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  774. if (equalCircleDiameter < quantifyThreshold)
  775. {
  776. smallparts.Add(part);
  777. }
  778. else
  779. {
  780. bigparts.Add(part);
  781. }
  782. }
  783. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  784. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  785. curFldData.CreateXrayList(smallparts);
  786. curFldData.CreateXrayList(bigparts);
  787. // get x-ray parameters
  788. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  789. var workmode = pXRayParam.GetScanMode();
  790. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  791. {
  792. uint nXRayAQTime;
  793. if (bigparts.Count > 0)
  794. {
  795. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  796. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  797. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  798. }
  799. if (smallparts.Count > 0)
  800. {
  801. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  802. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  803. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  804. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  805. }
  806. }
  807. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  808. {
  809. uint nXRayAQTime;
  810. if (bigparts.Count > 0)
  811. {
  812. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  813. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  814. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  815. }
  816. if (smallparts.Count > 0)
  817. {
  818. Thread.Sleep(500);
  819. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  820. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  821. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  822. }
  823. }
  824. return;
  825. }
  826. public virtual void QuantifyParticlesXrayData(COTSField curFldData)
  827. {
  828. var parts = curFldData.GetListXrayParticles();
  829. foreach (var p in parts)
  830. {
  831. m_EDSController.QuantifyXrayByPart(p);
  832. }
  833. }
  834. public void DoHolePreview()
  835. {
  836. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  837. {
  838. //----------memorize the state of sem
  839. var semstate = new SEMStateObject();
  840. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  841. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  842. ISemController sem = m_SemHardwareMgr;
  843. double posX=0, posY=0,posR=0;
  844. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  845. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  846. Point pos = new Point((int)posX, (int)posY);
  847. PointF otsPos = new Point(0, 0);
  848. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  849. {
  850. return;
  851. }
  852. semstate.Pos = otsPos;
  853. semstate.Magnification = dMagnification;
  854. semstate.WorkingDistance = wd;
  855. autoReset.SemState = semstate;
  856. //-------------------------------------
  857. // let the main thread to know that this sample measurement starts
  858. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  859. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  860. // set current time to current time
  861. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  862. // get SEM controller to set magnification and working distance
  863. if (!SetSEMDataMrs(m_HolePreviewSample))
  864. {
  865. log.Error("DoHolePreview: fail to set SEM data.");
  866. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  867. // record end time
  868. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  869. return;
  870. }
  871. // set the BSE scan param
  872. if (!SetHoleBSEParam(m_HolePreviewSample))
  873. {
  874. log.Error("DoHolePreview: fail to set BSE param.");
  875. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  876. // record end time
  877. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  878. return;
  879. }
  880. // calculate field centers
  881. List<System.Drawing.Point> listFieldCenter;
  882. List<System.Drawing.Point> alllistFieldCenter ;
  883. if (!CalculateUnMeasuredHoleImgCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  884. {// failed to calculate field centers
  885. log.Error("DoHolePreview: failed to calculate field centers.");
  886. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  887. // record end time
  888. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  889. return;
  890. }
  891. // go through each field
  892. //int nNewFieldId = 0;
  893. for ( int i = 0; i < listFieldCenter.Count; ++i)
  894. {// check and break if stop button is clicked
  895. if (IsAborted())
  896. {// measure stopped
  897. log.Trace("DoHolePreview: measure thread is stopped.");
  898. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  899. // record end time
  900. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  901. return;
  902. }
  903. // check if sample measurement completes
  904. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  905. int nTotalFieldSize = listFieldCenter.Count;
  906. // get a field center
  907. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  908. // move SEM to the field center
  909. if (!MoveSEMToPoint(poiFieldCentre))
  910. {// failed to move SEM to the position
  911. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  912. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  913. // record end time
  914. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  915. return;
  916. }
  917. // take BSE image for the fields
  918. CBSEImgClr pBSEIamge = AcquireABSEImage();
  919. if (pBSEIamge==null)
  920. {
  921. // failed to acquire a BSE image
  922. log.Error("DoHolePreview: failed to acquire a BSE image.");
  923. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  924. // record end time
  925. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  926. return;
  927. }
  928. //BSEData
  929. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg(m_HolePreviewSample);
  930. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge,poiFieldCentre);
  931. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  932. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  933. {
  934. // measurement failed or stopped
  935. log.Error("DoHolePreview: measurement failed or stopped.");
  936. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  937. // record end time
  938. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  939. return;
  940. }
  941. //save the result to project file
  942. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  943. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  944. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  945. m_listHoleBSEImg.Add(pHoleBSEImg);
  946. }
  947. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  948. //calculate measure time
  949. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  950. // let main thread to know that this sample measurement completes
  951. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg(m_HolePreviewSample);
  952. MsgSmplEnd.InitHolePreSampleEndMsg();
  953. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  954. }
  955. }
  956. // Cumulate field data info
  957. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  958. {
  959. // get measure result items of the sample
  960. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  961. // cumulate field data info
  962. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  963. // cumulate ratio
  964. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  965. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  966. pMsrResults.SetRatio(dRatio);
  967. // go through the particles list
  968. foreach (COTSParticleClr pParticle in listParticles)
  969. {
  970. // create a measure result item
  971. pMsrResults.CumulateMeasureResult(pParticle);
  972. }
  973. m_Sample.SetMsrResults(pMsrResults);
  974. return true;
  975. }
  976. public bool SaveFieldData(COTSField fldData, string filedFileFoler)
  977. {
  978. string strFieldFileFolder = filedFileFoler;
  979. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  980. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  981. int nId = fldData.GetId();
  982. string sFieldId;
  983. sFieldId = nId.ToString();
  984. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  985. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  986. {
  987. log.Error("SaveFieldFiles: save BSE file failed.");
  988. return false;
  989. }
  990. // IncA Data list
  991. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  992. pDBFileMgr.SaveStatusDataToDB();
  993. var fldDB = pDBFileMgr.GetFieldDB();
  994. log.Warn("Start saving particle data.");
  995. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  996. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  997. fldcmds.Add(fldcmd);
  998. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  999. //remomove the invalid particles
  1000. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  1001. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  1002. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  1003. var listAnalysisPosXray = new List<CPosXrayClr>();
  1004. foreach (var p in fldData.GetListXrayParticles())
  1005. {
  1006. listAnalysisPosXray.Add(p.GetXray());
  1007. }
  1008. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  1009. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1010. return true;
  1011. }
  1012. protected void SaveFieldMgrData()
  1013. {
  1014. while (bSaveThreadWorking)
  1015. {
  1016. while (fieldQueue.Count() > 0)
  1017. {
  1018. COTSField f = fieldQueue.Dequeue();
  1019. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1020. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  1021. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1022. }
  1023. if (fieldQueue.Count() == 0)
  1024. {
  1025. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1026. log.Warn("finished batch saving");
  1027. return;
  1028. }
  1029. }
  1030. return;
  1031. }
  1032. private void SendFieldParticlesInfoToGUI(COTSField curFld,CMsrSampleStatus pStatus)
  1033. {
  1034. double measuredArea = 0; // this area should be the field area
  1035. var a_pBSEImg = curFld.GetBSEImage();
  1036. double dPixelSize = curFld.GetPixelSize();
  1037. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  1038. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  1039. log.Info("Send field data to screen!");
  1040. pStatus.AddCompletedFieldCenter(curFld.GetOTSPosition());
  1041. //Field Data
  1042. // record end time
  1043. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  1044. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  1045. MsgFieldEnd.InitFieldDataMsg();
  1046. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  1047. }
  1048. protected void StartSaveFileThread(ref COTSField a_pFieldMgr)
  1049. {
  1050. fieldQueue.Enqueue(a_pFieldMgr);
  1051. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1052. {
  1053. if (bSaveThreadWorking == false)
  1054. {
  1055. bSaveThreadWorking = true;
  1056. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1057. m_thread_ptr.IsBackground = true;
  1058. m_thread_ptr.Start();
  1059. }
  1060. }
  1061. }
  1062. public void ParticleSpecialTreatment(ref COTSField fld)
  1063. {
  1064. //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.
  1065. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1066. double edsTime = 0; //to store the EDSTime returned from the engine.
  1067. var bigparts = fld.GetListXrayParticles();
  1068. var m_ClassifyEngine = new CClassifyEngine();
  1069. for (int i = 0; i < bigparts.Count(); i++)
  1070. {
  1071. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  1072. var p = bigparts[i];
  1073. // 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.
  1074. //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.
  1075. edsTime = engine.IfNeedMaxEDS(p);
  1076. if (edsTime > 0)
  1077. {
  1078. mapPartXray[p] = i;
  1079. }
  1080. }
  1081. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1082. if (mapPartXray.Count() > 0)
  1083. {
  1084. foreach (var p in mapPartXray)
  1085. {
  1086. partsMax.Add(p.Key);
  1087. }
  1088. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1089. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  1090. }
  1091. }
  1092. public void CalculateParticleAbsolutPos(ref COTSField curFldData)
  1093. {
  1094. double dPixelSize = m_Sample.CalculatePixelSize();
  1095. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1096. foreach (var p in curFldData.GetListAnalysisParticles())
  1097. {
  1098. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1099. PointF semP = new PointF(); ;
  1100. Point semPos = new Point();
  1101. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.GetOTSPosition(), ref semP);
  1102. semPos.X = (int)semP.X;
  1103. semPos.Y = (int)semP.Y;
  1104. p.SetSEMPos(semPos);
  1105. }
  1106. }
  1107. }
  1108. }