CSmplMeasure.cs 55 KB

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