CSmplMeasure.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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_EDSHardwareMgr;
  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_EDSHardwareMgr = 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_PARTCLE_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. sm.MoveSEMToPoint(semState.Pos);
  391. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  392. }
  393. sm.SetSEMExteralOff();
  394. }
  395. }
  396. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  397. {
  398. double kv = 0, brightness = 0, contrast = 0;
  399. var hw = SemController.GetSEMController();
  400. hw.GetSemHighTension(ref kv);
  401. hw.GetSemBrightness(ref brightness);
  402. hw.GetSemContrast(ref contrast);
  403. SemDataGnr.SetValue(kv, brightness, contrast);
  404. return true;
  405. }
  406. public void DoMeasureForOneSample()
  407. {
  408. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  409. {
  410. // let the main thread to know that this sample measurement starts
  411. var pStatus = m_Sample.GetMsrStatus();
  412. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  413. // set current time to current time
  414. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  415. // let main App know that the sample begin to measure
  416. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  417. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  418. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  419. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  420. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  421. log.Info(m_Sample.GetName() + " Measurement started!");
  422. // get SEM controller to set magnification and working distance
  423. if (!SetSEMDataMrs(m_Sample))
  424. {
  425. log.Error("DoMeasure: fail to set SEM data.");
  426. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  427. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  428. return;
  429. }
  430. // set the BSE scan param
  431. if (!SetBSEParam())
  432. {
  433. log.Error("DoMeasure: fail to set BSE param.");
  434. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  435. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  436. return;
  437. }
  438. var pSEMDataGnr = new CSEMDataGnr();
  439. log.Info("Get Kv, Brightness and Contrast!");
  440. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  441. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  442. // record SEM data
  443. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  444. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  445. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  446. // record stage
  447. CStage pStage = pProjMgrFile.GetStage();
  448. m_pSampleRstFile.SetSEMStage(pStage);
  449. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  450. log.Info("Create result file!");
  451. if (!m_pSampleRstFile.CreateResultFiles())
  452. {// failed to call measure result file Save method
  453. log.Error("DoMeasure: failed to call measure result file Save method.");
  454. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  455. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  456. return;
  457. }
  458. //------
  459. var FldDatas = m_Sample.GetFieldsData();
  460. if (m_EDSHardwareMgr.GetEDSType()==EDSTYPE.BRUKER && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  461. {
  462. List<string> KeyNameList = new List<string>();
  463. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  464. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  465. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  466. m_EDSHardwareMgr.SetFilterKeyEleNames( KeyNameList);//will decide if do the quantification according to this keyelelist.
  467. }
  468. for (int i = 0; i < FldDatas.Count; ++i)
  469. {// check and break if stop button is clicked
  470. var curFld = FldDatas[i];
  471. if (curFld.GetIsMeasureComplete())
  472. {
  473. continue;
  474. }
  475. if (IsPaused())
  476. {// measure stopped
  477. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  478. // record end time
  479. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  480. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  481. while (fieldQueue.Count() > 0)
  482. {
  483. Thread.Sleep(100);
  484. }
  485. SetSEMExteralOff();
  486. // update thread measure status class, let the main thread know that this sample measurement stopped
  487. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  488. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  489. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  490. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.PAUSED;
  491. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime.Now.ToShortDateString();
  492. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  493. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStop);
  494. while (IsPaused())
  495. {
  496. Thread.Sleep(300);
  497. }
  498. }
  499. if (IsAborted())
  500. {// measure stopped
  501. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  502. // record end time
  503. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  504. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  505. while (fieldQueue.Count() > 0)
  506. {
  507. Thread.Sleep(100);
  508. }
  509. break;
  510. }
  511. // check if sample measurement completes
  512. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  513. if (IsSampleOver(pScanParam))
  514. {
  515. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  516. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  517. break;
  518. }
  519. // get a field center
  520. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  521. // update thread measure status class, let the main thread know that starts a new field
  522. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  523. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  524. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  525. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = new Point((int)poiFieldCentre.X,(int)poiFieldCentre.Y);
  526. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  527. int fldNo = curFld.GetId();
  528. log.Warn("Current field:" + fldNo.ToString());
  529. // move SEM to the field center
  530. if (!MoveSEMToPoint(poiFieldCentre))
  531. {// failed to move SEM to the position
  532. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  533. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  534. // record end time
  535. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  536. return;
  537. }
  538. log.Info("Begin to Acquire BSE image!");
  539. // take BSE image for the fields
  540. CBSEImgClr pBSEImg = AcquireABSEImage();
  541. // let the main thread to know that image process is completed
  542. if (pBSEImg == null)
  543. {
  544. log.Error("ImageProcess: can't get no background image.");
  545. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  546. return;
  547. }
  548. //BSEData
  549. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  550. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  551. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  552. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = (int)poiFieldCentre.X;
  553. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = (int)poiFieldCentre.Y;
  554. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  555. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  556. byte[] pImgData = pBSEImg.GetImageDataPtr();
  557. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  558. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  559. log.Info("Acquire BSE image success! Processing image...");
  560. // image process
  561. var rst = FieldImageProcess(curFld, pBSEImg);
  562. if (rst == true)
  563. {
  564. curFld.SetIsMeasureComplete(true);
  565. }
  566. else
  567. {
  568. return;
  569. }
  570. //start db save
  571. StartSaveFileThread(ref curFld);
  572. double measuredArea = 0; // this area should be the field area
  573. var a_pBSEImg = curFld.GetBSEImage();
  574. double dPixelSize = curFld.GetPixelSize();
  575. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  576. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  577. log.Info("Send field data to screen!");
  578. pStatus.AddCompletedFieldCenter(poiFieldCentre);
  579. //Field Data
  580. // record end time
  581. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  582. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  583. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  584. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  585. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  586. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = m_Sample.GetFieldsData().Count;
  587. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFld.GetListAnalysisParticles().Count;
  588. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  589. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  590. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  591. }
  592. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  593. {
  594. Thread.Sleep(1000);
  595. log.Warn("db saving!");
  596. }
  597. TheLastWorkOfSampleMeasure();
  598. }
  599. }
  600. public void TheLastWorkOfSampleMeasure()
  601. {
  602. COTSSample theSample = m_Sample;
  603. var pStatus = theSample.GetMsrStatus();
  604. //merging particles
  605. log.Info("Merging big particles which are crossing the field edge!");
  606. CImageHandler imgpro = new CImageHandler();
  607. int scanfldsize = theSample.GetSEMDataMsr().GetScanFieldSize();
  608. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  609. double pixelSize = theSample.CalculatePixelSize();
  610. imgpro.MergeBigBoundaryParticles(theSample.GetFieldsData(), pixelSize, scanfldsize, theSample.GetResolutionSize(), ref mergedParticles);
  611. CalculateMergedPartProperty(mergedParticles, pixelSize);
  612. ClassifyMergedParticles(mergedParticles);
  613. log.Info("begin merged particle data db saving...");
  614. SaveMergedParticles(mergedParticles);
  615. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  616. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  617. // let main thread to know that this sample measurement completes
  618. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  619. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  620. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  621. MsgSmplEnd.STMSampleStu.cSampleName = theSample.GetName();
  622. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  623. }
  624. public bool DoMEasureForReMeasure()
  625. {
  626. return true;
  627. }
  628. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  629. {
  630. var m_ImagePro = new CImageHandler();
  631. foreach (COTSParticleClr part in mergedParticles)
  632. {
  633. m_ImagePro.CalParticleImageProp(part, pixelSize);
  634. }
  635. }
  636. public virtual bool FieldImageProcess(COTSFieldData curFldData, CBSEImgClr a_pBSEImg)
  637. {
  638. PointF fldCenter = curFldData.OTSPos;
  639. curFldData.SetBSEImage(a_pBSEImg);
  640. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  641. PointF semPos = new Point();
  642. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  643. curFldData.SetSemPos(semPos);
  644. //first step:remove background of the bse image and compound all the finded particles.
  645. log.Info("Begin to process image and get all particles!");
  646. GetOriginalParticles(ref curFldData);
  647. // second step :filter the finded particles.
  648. log.Info("Begin to filter particles!");
  649. FilterParticles(ref curFldData);
  650. CalculateParticleAbsolutPos(ref curFldData);
  651. log.Info("Begin to Calculate the image property of every particle!");
  652. var analysisparts = curFldData.GetListAnalysisParticles();
  653. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  654. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  655. if (pXRayParam.GetUsingXray() == true)
  656. {
  657. log.Info("Begin to collect particle's xray data!");
  658. //Thread.Sleep(100);
  659. CollectParticlesXrayData(curFldData);
  660. //Thread.Sleep(100);
  661. }
  662. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  663. ClassifyFieldParticles(curFldData);
  664. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  665. return true;
  666. }
  667. public void GetOriginalParticles(ref COTSFieldData curFldData)
  668. {
  669. // measure status
  670. //CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  671. // get image process parameter
  672. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  673. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  674. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  675. var pixelsize = m_Sample.CalculatePixelSize();
  676. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  677. {
  678. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  679. foreach (var grayRange in ranges)
  680. {
  681. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  682. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  683. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  684. }
  685. }
  686. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  687. // remove BES image background
  688. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  689. // check if this is an empty image
  690. if (curFldData.NoParticle())
  691. { // empty fields
  692. log.Info("ImageProcess: empty field.");
  693. }
  694. return;
  695. }
  696. public void FilterParticles(ref COTSFieldData curFldData)
  697. {
  698. if (curFldData.NoAnalysisParticle())
  699. {
  700. log.Warn("There's no analysis particles!");
  701. return;
  702. }
  703. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  704. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  705. double dPixelSize = m_Sample.CalculatePixelSize();
  706. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  707. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  708. var listXray1 = new List<COTSParticleClr>();
  709. var pXRayParam = pMsrParam.GetXRayParam();
  710. if (listXray.Count > pXRayParam.GetXrayLimit())
  711. {
  712. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  713. {
  714. listXray1.Add(listXray[i]);
  715. }
  716. }
  717. else
  718. {
  719. listXray1 = listXray;
  720. }
  721. curFldData.SetListAnalysisParticles(listXray1);
  722. log.Info("Analysis particles:"+ listXray1.Count);
  723. //2) according to the quantify threshold size value separate the analysis particles into two group :the bigparticles and the smallparticles.
  724. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  725. var smallparts = curFldData.ListSmallParticles;
  726. var bigparts = curFldData.ListBigParticles;
  727. foreach (var part in listXray1)
  728. {
  729. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  730. if (equalCircleDiameter < quantifyThreshold)
  731. {
  732. smallparts.Add(part);
  733. }
  734. else
  735. {
  736. bigparts.Add(part);
  737. }
  738. }
  739. curFldData.ListSmallParticles = smallparts;
  740. curFldData.ListBigParticles = bigparts;
  741. //fld.SmallParticlePercentage=percentage;
  742. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  743. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  744. return;
  745. }
  746. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  747. {
  748. // get x-ray parameters
  749. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  750. // calculate search x-ray acquire time
  751. uint nXRayAQTime;
  752. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  753. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  754. foreach (var p in curFldData.ListSmallParticles)
  755. {
  756. if (p.IsXrayParticle())
  757. {
  758. smallparts.Add(p);
  759. }
  760. }
  761. foreach (var p in curFldData.ListBigParticles)
  762. {
  763. if (p.IsXrayParticle())
  764. {
  765. bigparts.Add(p);
  766. }
  767. }
  768. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  769. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  770. if (bigparts.Count > 0)
  771. {
  772. var workmode = pXRayParam.GetScanMode();
  773. // get x-ray list (analysis) by particle features
  774. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  775. {
  776. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  777. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  778. }
  779. else if(workmode==OTS_X_RAY_SCAN_MODE.PointMode)
  780. {
  781. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  782. m_EDSHardwareMgr.GetXRayByParts(bigparts, nXRayAQTime, true);
  783. }
  784. }
  785. if (smallparts.Count > 0)
  786. {
  787. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  788. // get x-ray list (analysis) by points
  789. m_EDSHardwareMgr.GetXRayByParts(smallparts, nXRayAQTime, true);
  790. }
  791. return;
  792. }
  793. public void DoHolePreview()
  794. {
  795. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  796. {
  797. //----------memorize the state of sem
  798. var semstate = new SEMStateObject();
  799. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  800. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  801. ISemController sem = m_SemHardwareMgr;
  802. double posX=0, posY=0,posR=0;
  803. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  804. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  805. Point pos = new Point((int)posX, (int)posY);
  806. PointF otsPos = new Point(0, 0);
  807. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  808. {
  809. return;
  810. }
  811. semstate.Pos = otsPos;
  812. semstate.Magnification = dMagnification;
  813. semstate.WorkingDistance = wd;
  814. autoReset.SemState = semstate;
  815. //-------------------------------------
  816. // let the main thread to know that this sample measurement starts
  817. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  818. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  819. // set current time to current time
  820. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  821. // let main App know that the sample begin to measure
  822. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  823. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  824. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  825. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  826. // get SEM controller to set magnification and working distance
  827. if (!SetSEMDataMrs(m_HolePreviewSample))
  828. {
  829. log.Error("DoHolePreview: fail to set SEM data.");
  830. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  831. // record end time
  832. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  833. return;
  834. }
  835. // set the BSE scan param
  836. if (!SetHoleBSEParam(m_HolePreviewSample))
  837. {
  838. log.Error("DoHolePreview: fail to set BSE param.");
  839. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  840. // record end time
  841. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  842. return;
  843. }
  844. // calculate field centers
  845. List<System.Drawing.Point> listFieldCenter;
  846. List<System.Drawing.Point> alllistFieldCenter ;
  847. if (!CalculateUnMeasuredFieldsCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  848. {// failed to calculate field centers
  849. log.Error("DoHolePreview: failed to calculate field centers.");
  850. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  851. // record end time
  852. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  853. return;
  854. }
  855. // go through each field
  856. int nNewFieldId = 0;
  857. for ( int i = 0; i < listFieldCenter.Count; ++i)
  858. {// check and break if stop button is clicked
  859. if (IsAborted())
  860. {// measure stopped
  861. log.Trace("DoHolePreview: measure thread is stopped.");
  862. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  863. // record end time
  864. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  865. return;
  866. }
  867. // check if sample measurement completes
  868. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  869. int nTotalFieldSize = listFieldCenter.Count;
  870. // get a field center
  871. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  872. // update thread measure status class, let the main thread know that starts a new field
  873. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  874. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  875. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  876. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  877. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  878. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  879. // move SEM to the field center
  880. if (!MoveSEMToPoint(poiFieldCentre))
  881. {// failed to move SEM to the position
  882. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  883. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  884. // record end time
  885. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  886. return;
  887. }
  888. // take BSE image for the fields
  889. CBSEImgClr pBSEIamge = AcquireABSEImage();
  890. if (pBSEIamge==null)
  891. {
  892. // failed to acquire a BSE image
  893. log.Error("DoHolePreview: failed to acquire a BSE image.");
  894. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  895. // record end time
  896. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  897. return;
  898. }
  899. //BSEData
  900. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  901. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  902. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  903. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  904. MsgFieldBSE.STMSampleRetData.BSEData.fieldId = i;
  905. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  906. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  907. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  908. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  909. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  910. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  911. {
  912. // measurement failed or stopped
  913. log.Error("DoHolePreview: measurement failed or stopped.");
  914. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  915. // record end time
  916. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  917. return;
  918. }
  919. //save the result to project file
  920. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  921. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  922. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  923. m_listHoleBSEImg.Add(pHoleBSEImg);
  924. // prepare for the next
  925. ++nNewFieldId;
  926. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  927. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  928. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  929. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  930. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  931. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  932. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  933. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  934. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  935. }
  936. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  937. //calculate measure time
  938. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  939. // let main thread to know that this sample measurement completes
  940. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  941. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  942. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  943. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  944. }
  945. }
  946. // Cumulate field data info
  947. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  948. {
  949. // get measure result items of the sample
  950. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  951. // cumulate field data info
  952. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  953. // cumulate ratio
  954. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  955. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  956. pMsrResults.SetRatio(dRatio);
  957. // go through the particles list
  958. foreach (COTSParticleClr pParticle in listParticles)
  959. {
  960. // create a measure result item
  961. pMsrResults.CumulateMeasureResult(pParticle);
  962. }
  963. // ok, return TRUE
  964. return true;
  965. }
  966. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  967. {
  968. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  969. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  970. foreach (COTSParticleClr part in mergedParts)
  971. {
  972. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  973. }
  974. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  975. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  976. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  977. foreach (COTSParticleClr part in mergedParts)
  978. {
  979. ches.Add(part.GetXray());
  980. }
  981. xraydb.SaveElementChemistriesList(ches);
  982. return true;
  983. }
  984. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  985. {
  986. string strFieldFileFolder = filedFileFoler;
  987. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  988. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  989. int nId = fldData.GetId();
  990. string sFieldId;
  991. sFieldId = nId.ToString();
  992. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  993. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  994. {
  995. log.Error("SaveFieldFiles: save BSE file failed.");
  996. return false;
  997. }
  998. // IncA Data list
  999. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  1000. pDBFileMgr.SaveStatusDataToDB();
  1001. var fldDB = pDBFileMgr.GetFieldDB();
  1002. log.Warn("Start saving particle data.");
  1003. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  1004. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  1005. fldcmds.Add(fldcmd);
  1006. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  1007. //remomove the invalid particles
  1008. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  1009. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  1010. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  1011. var listAnalysisPosXray = new List<CPosXrayClr>();
  1012. foreach (var p in fldData.GetListXrayParticles())
  1013. {
  1014. listAnalysisPosXray.Add(p.GetXray());
  1015. }
  1016. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  1017. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1018. return true;
  1019. }
  1020. protected void SaveFieldMgrData()
  1021. {
  1022. while (bSaveThreadWorking)
  1023. {
  1024. while (fieldQueue.Count() > 0)
  1025. {
  1026. COTSFieldData f = fieldQueue.Dequeue();
  1027. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1028. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1029. }
  1030. if (fieldQueue.Count() == 0)
  1031. {
  1032. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1033. log.Warn("finished batch saving");
  1034. return;
  1035. }
  1036. }
  1037. return;
  1038. }
  1039. protected void StartSaveFileThread(ref COTSFieldData a_pFieldMgr)
  1040. {
  1041. fieldQueue.Enqueue(a_pFieldMgr);
  1042. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1043. {
  1044. if (bSaveThreadWorking == false)
  1045. {
  1046. bSaveThreadWorking = true;
  1047. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1048. m_thread_ptr.IsBackground = true;
  1049. m_thread_ptr.Start();
  1050. }
  1051. }
  1052. }
  1053. public void ParticleSpecialTreatment(ref COTSFieldData fld)
  1054. {
  1055. //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.
  1056. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1057. double edsTime = 0; //to store the EDSTime returned from the engine.
  1058. var bigparts = fld.GetListXrayParticles();
  1059. var m_ClassifyEngine = new CClassifyEngine();
  1060. for (int i = 0; i < bigparts.Count(); i++)
  1061. {
  1062. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  1063. var p = bigparts[i];
  1064. // 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.
  1065. //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.
  1066. edsTime = engine.IfNeedMaxEDS(p);
  1067. if (edsTime > 0)
  1068. {
  1069. mapPartXray[p] = i;
  1070. }
  1071. }
  1072. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1073. if (mapPartXray.Count() > 0)
  1074. {
  1075. foreach (var p in mapPartXray)
  1076. {
  1077. partsMax.Add(p.Key);
  1078. }
  1079. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1080. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  1081. }
  1082. }
  1083. public void CalculateParticleAbsolutPos(ref COTSFieldData curFldData)
  1084. {
  1085. double dPixelSize = m_Sample.CalculatePixelSize();
  1086. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1087. foreach (var p in curFldData.GetListAnalysisParticles())
  1088. {
  1089. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1090. PointF semP = new PointF(); ;
  1091. Point semPos = new Point();
  1092. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.OTSPos, ref semP);
  1093. semPos.X = (int)semP.X;
  1094. semPos.Y = (int)semP.Y;
  1095. p.SetAbsolutPos(semPos);
  1096. }
  1097. }
  1098. }
  1099. }