CSmplMeasure.cs 56 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. CMeasureParam pMsrParam = sample.GetMsrParams();
  192. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  193. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  194. CSEMDataMsr poSEMDataMsr = m_Sample.GetSEMDataMsr();
  195. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  196. //CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  197. //CMsrSampleStatus pStatus = sample.GetMsrStatus();
  198. // measured field centers list
  199. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  200. // field centers list manager
  201. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  202. // init field centers list manager
  203. if (!pFieldMgr.Init(sample.GetMsrDomain(), poImageScanParam, pImgProcParam, poSEMDataMsr, listCompletedCenter))
  204. {
  205. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  206. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  207. a_allpieldcenter = new List<Point>();
  208. return false;
  209. }
  210. // get field centers list
  211. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  212. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  213. // ok, return TRUE
  214. return true;
  215. }
  216. protected bool IsAborted()
  217. {
  218. return m_pMsrThread.IsMeasureStopped();
  219. }
  220. protected bool IsPaused()
  221. {
  222. //Debug.Assert(m_pMsrThread == null);
  223. var statu = m_pMsrThread.GetMsrThreadStatus();
  224. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  225. {
  226. return true;
  227. }
  228. else
  229. {
  230. return false;
  231. }
  232. }
  233. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  234. {
  235. string sStopMode = a_pScanParam.GetStopMode();
  236. int nStopField = a_pScanParam.GetStopParamFields();
  237. // completed fields number
  238. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  239. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  240. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  241. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  242. int nNumParticle = 0;
  243. foreach (var pResult in listMsrResult)
  244. {
  245. if (pResult.GetTypeId() > (int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  246. {
  247. nNumParticle += (int)pResult.GetNumber();
  248. }
  249. }
  250. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  251. int nDay = timeSpan.Days;
  252. int nHour = timeSpan.Hours;
  253. int nMin = timeSpan.Minutes;
  254. int nSec = timeSpan.Seconds;
  255. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  256. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  257. int nParticlAim = a_pScanParam.GetStopParamParticles();
  258. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  259. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  260. bool bRet = false;
  261. string[] str = sStopMode.Replace(" ", "").Split('+');
  262. for(int i=0;i< str.Length;i++)
  263. {
  264. switch (int.Parse(str[i].Split(':')[0])-1)
  265. {
  266. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  267. // completed fields number
  268. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  269. {
  270. bRet = true;
  271. }
  272. break;
  273. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  274. if (nCompeltedField >= nStopField)
  275. {
  276. bRet = true;
  277. }
  278. break;
  279. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  280. if (nNumParticle >= nParticlAim)
  281. {
  282. bRet = true;
  283. }
  284. break;
  285. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  286. if (nUsedTime >= nMeasTimeAim)
  287. {
  288. bRet = true;
  289. }
  290. break;
  291. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  292. if (NMeasArea >= NMeasAreaAim)
  293. {
  294. bRet = true;
  295. }
  296. break;
  297. default:
  298. break;
  299. }
  300. }
  301. return bRet;
  302. }
  303. // move SEM to the point
  304. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  305. {
  306. // get SEM controller
  307. var pSEMController = m_SemHardwareMgr;
  308. PointF a_SEMpt = new Point();
  309. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  310. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  311. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  312. {
  313. return false;
  314. }
  315. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  316. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  317. // move SEM to the position (rotation 0)
  318. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  319. {
  320. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  321. return false;
  322. }
  323. CMeasureParam pMsrParam = m_Sample.GetMsrParams();
  324. if (pMsrParam.SlopParam.IsUsingSlopParam)
  325. {
  326. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  327. double originWd = 0;
  328. pSEMController.GetWorkingDistance(ref originWd);
  329. if ((wd - originWd) > 2)
  330. {
  331. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  332. }
  333. else
  334. {
  335. Thread.Sleep(hardWareDelay);
  336. pSEMController.SetWorkingDistance(wd);
  337. }
  338. }
  339. Thread.Sleep(hardWareDelay);
  340. return true;
  341. }
  342. // Acquire a BSE image
  343. CBSEImgClr AcquireABSEImage()
  344. {
  345. // BSE image
  346. CBSEImgClr pBSEImage = null;
  347. // get scan controller
  348. var pScanController = m_ScanHardwareMgr;
  349. pBSEImage = pScanController.AcquireBSEImage();
  350. return pBSEImage;
  351. }
  352. public virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  353. {
  354. return;
  355. }
  356. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  357. {
  358. return;
  359. }
  360. private class SEMStateObject
  361. {
  362. private PointF pos;
  363. private double workingDistance;
  364. private CSEMDataGnr semdata;
  365. private COTSSample originalSample;
  366. private double magnification;
  367. public PointF Pos { get => pos; set => pos = value; }
  368. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  369. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  370. public double Magnification { get => magnification; set => magnification = value; }
  371. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  372. }
  373. private class AutoResetSEMControl:IDisposable
  374. {
  375. CSmplMeasure sm;
  376. private SEMStateObject semState=null;
  377. public AutoResetSEMControl(CSmplMeasure s)
  378. {
  379. sm = s;
  380. }
  381. public SEMStateObject SemState
  382. {
  383. get => semState;
  384. set => semState = value;
  385. }
  386. public void Dispose()
  387. {
  388. if (semState != null)
  389. {
  390. sm.SetBSEParam();
  391. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  392. sm.MoveSEMToPoint(semState.Pos);
  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_EDSHardwareMgr.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_EDSHardwareMgr.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. //start db save
  573. StartSaveFileThread(ref curFld);
  574. double measuredArea = 0; // this area should be the field area
  575. var a_pBSEImg = curFld.GetBSEImage();
  576. double dPixelSize = curFld.GetPixelSize();
  577. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  578. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  579. log.Info("Send field data to screen!");
  580. pStatus.AddCompletedFieldCenter(poiFieldCentre);
  581. //Field Data
  582. // record end time
  583. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  584. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  585. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  586. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  587. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  588. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = m_Sample.GetFieldsData().Count;
  589. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFld.GetListAnalysisParticles().Count;
  590. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  591. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  592. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  593. }
  594. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  595. {
  596. Thread.Sleep(1000);
  597. log.Warn("db saving!");
  598. }
  599. TheLastWorkOfSampleMeasure();
  600. }
  601. }
  602. public void TheLastWorkOfSampleMeasure()
  603. {
  604. COTSSample theSample = m_Sample;
  605. var pStatus = theSample.GetMsrStatus();
  606. //merging particles
  607. log.Info("Merging big particles which are crossing the field edge!");
  608. CImageHandler imgpro = new CImageHandler();
  609. int scanfldsize = theSample.GetSEMDataMsr().GetScanFieldSize();
  610. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  611. double pixelSize = theSample.CalculatePixelSize();
  612. imgpro.MergeBigBoundaryParticles(theSample.GetFieldsData(), pixelSize, scanfldsize, theSample.GetResolutionSize(), ref mergedParticles);
  613. CalculateMergedPartProperty(mergedParticles, pixelSize);
  614. ClassifyMergedParticles(mergedParticles);
  615. log.Info("begin merged particle data db saving...");
  616. SaveMergedParticles(mergedParticles);
  617. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  618. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  619. // let main thread to know that this sample measurement completes
  620. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  621. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  622. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  623. MsgSmplEnd.STMSampleStu.cSampleName = theSample.GetName();
  624. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  625. }
  626. public bool DoMEasureForReMeasure()
  627. {
  628. return true;
  629. }
  630. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  631. {
  632. var m_ImagePro = new CImageHandler();
  633. foreach (COTSParticleClr part in mergedParticles)
  634. {
  635. m_ImagePro.CalParticleImageProp(part, pixelSize);
  636. }
  637. }
  638. public virtual bool FieldImageProcess(COTSFieldData curFldData, CBSEImgClr a_pBSEImg)
  639. {
  640. PointF fldCenter = curFldData.OTSPos;
  641. curFldData.SetBSEImage(a_pBSEImg);
  642. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  643. PointF semPos = new Point();
  644. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  645. curFldData.SetSemPos(semPos);
  646. //first step:remove background of the bse image and compound all the finded particles.
  647. log.Info("Begin to process image and get all particles!");
  648. GetOriginalParticles(ref curFldData);
  649. // second step :filter the finded particles.
  650. log.Info("Begin to filter particles!");
  651. FilterParticles(ref curFldData);
  652. CalculateParticleAbsolutPos(ref curFldData);
  653. log.Info("Begin to Calculate the image property of every particle!");
  654. var analysisparts = curFldData.GetListAnalysisParticles();
  655. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  656. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  657. if (pXRayParam.GetUsingXray() == true)
  658. {
  659. log.Info("Begin to collect particle's xray data!");
  660. //Thread.Sleep(100);
  661. CollectParticlesXrayData(curFldData);
  662. //Thread.Sleep(100);
  663. }
  664. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  665. ClassifyFieldParticles(curFldData);
  666. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  667. return true;
  668. }
  669. public void GetOriginalParticles(ref COTSFieldData curFldData)
  670. {
  671. // measure status
  672. //CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  673. // get image process parameter
  674. CMeasureParam pMsrParam = m_Sample.GetMsrParams();
  675. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  676. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  677. var pixelsize = m_Sample.CalculatePixelSize();
  678. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  679. {
  680. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  681. foreach (var grayRange in ranges)
  682. {
  683. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  684. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  685. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  686. }
  687. }
  688. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  689. // remove BES image background
  690. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  691. // check if this is an empty image
  692. if (curFldData.NoParticle())
  693. { // empty fields
  694. log.Info("ImageProcess: empty field.");
  695. }
  696. return;
  697. }
  698. public void FilterParticles(ref COTSFieldData curFldData)
  699. {
  700. if (curFldData.NoAnalysisParticle())
  701. {
  702. log.Warn("There's no analysis particles!");
  703. return;
  704. }
  705. CMeasureParam pMsrParam = m_Sample.GetMsrParams();
  706. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  707. double dPixelSize = m_Sample.CalculatePixelSize();
  708. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  709. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  710. var listXray1 = new List<COTSParticleClr>();
  711. var pXRayParam = pMsrParam.GetXRayParam();
  712. if (listXray.Count > pXRayParam.GetXrayLimit())
  713. {
  714. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  715. {
  716. listXray1.Add(listXray[i]);
  717. }
  718. }
  719. else
  720. {
  721. listXray1 = listXray;
  722. }
  723. curFldData.SetListAnalysisParticles(listXray1);
  724. log.Info("Analysis particles:"+ listXray1.Count);
  725. //2) according to the quantify threshold size value separate the analysis particles into two group :the bigparticles and the smallparticles.
  726. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  727. var smallparts = curFldData.ListSmallParticles;
  728. var bigparts = curFldData.ListBigParticles;
  729. foreach (var part in listXray1)
  730. {
  731. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  732. if (equalCircleDiameter < quantifyThreshold)
  733. {
  734. smallparts.Add(part);
  735. }
  736. else
  737. {
  738. bigparts.Add(part);
  739. }
  740. }
  741. curFldData.ListSmallParticles = smallparts;
  742. curFldData.ListBigParticles = bigparts;
  743. //fld.SmallParticlePercentage=percentage;
  744. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  745. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  746. return;
  747. }
  748. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  749. {
  750. // get x-ray parameters
  751. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  752. // calculate search x-ray acquire time
  753. uint nXRayAQTime;
  754. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  755. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  756. foreach (var p in curFldData.ListSmallParticles)
  757. {
  758. if (p.IsXrayParticle())
  759. {
  760. smallparts.Add(p);
  761. }
  762. }
  763. foreach (var p in curFldData.ListBigParticles)
  764. {
  765. if (p.IsXrayParticle())
  766. {
  767. bigparts.Add(p);
  768. }
  769. }
  770. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  771. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  772. if (bigparts.Count > 0)
  773. {
  774. var workmode = pXRayParam.GetScanMode();
  775. // get x-ray list (analysis) by particle features
  776. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  777. {
  778. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  779. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  780. }
  781. else if(workmode==OTS_X_RAY_SCAN_MODE.PointMode)
  782. {
  783. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  784. m_EDSHardwareMgr.GetXRayByParts(bigparts, nXRayAQTime, true);
  785. }
  786. }
  787. if (smallparts.Count > 0)
  788. {
  789. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  790. // get x-ray list (analysis) by points
  791. m_EDSHardwareMgr.GetXRayByParts(smallparts, nXRayAQTime, true);
  792. }
  793. return;
  794. }
  795. public void DoHolePreview()
  796. {
  797. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  798. {
  799. //----------memorize the state of sem
  800. var semstate = new SEMStateObject();
  801. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  802. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  803. ISemController sem = m_SemHardwareMgr;
  804. double posX=0, posY=0,posR=0;
  805. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  806. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  807. Point pos = new Point((int)posX, (int)posY);
  808. PointF otsPos = new Point(0, 0);
  809. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  810. {
  811. return;
  812. }
  813. semstate.Pos = otsPos;
  814. semstate.Magnification = dMagnification;
  815. semstate.WorkingDistance = wd;
  816. autoReset.SemState = semstate;
  817. //-------------------------------------
  818. // let the main thread to know that this sample measurement starts
  819. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  820. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  821. // set current time to current time
  822. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  823. // let main App know that the sample begin to measure
  824. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  825. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  826. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  827. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  828. // get SEM controller to set magnification and working distance
  829. if (!SetSEMDataMrs(m_HolePreviewSample))
  830. {
  831. log.Error("DoHolePreview: fail to set SEM data.");
  832. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  833. // record end time
  834. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  835. return;
  836. }
  837. // set the BSE scan param
  838. if (!SetHoleBSEParam(m_HolePreviewSample))
  839. {
  840. log.Error("DoHolePreview: fail to set BSE param.");
  841. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  842. // record end time
  843. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  844. return;
  845. }
  846. // calculate field centers
  847. List<System.Drawing.Point> listFieldCenter;
  848. List<System.Drawing.Point> alllistFieldCenter ;
  849. if (!CalculateUnMeasuredFieldsCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  850. {// failed to calculate field centers
  851. log.Error("DoHolePreview: failed to calculate field centers.");
  852. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  853. // record end time
  854. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  855. return;
  856. }
  857. // go through each field
  858. int nNewFieldId = 0;
  859. for ( int i = 0; i < listFieldCenter.Count; ++i)
  860. {// check and break if stop button is clicked
  861. if (IsAborted())
  862. {// measure stopped
  863. log.Trace("DoHolePreview: measure thread is stopped.");
  864. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  865. // record end time
  866. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  867. return;
  868. }
  869. // check if sample measurement completes
  870. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  871. int nTotalFieldSize = listFieldCenter.Count;
  872. // get a field center
  873. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  874. // update thread measure status class, let the main thread know that starts a new field
  875. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  876. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  877. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  878. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  879. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  880. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  881. // move SEM to the field center
  882. if (!MoveSEMToPoint(poiFieldCentre))
  883. {// failed to move SEM to the position
  884. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  885. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  886. // record end time
  887. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  888. return;
  889. }
  890. // take BSE image for the fields
  891. CBSEImgClr pBSEIamge = AcquireABSEImage();
  892. if (pBSEIamge==null)
  893. {
  894. // failed to acquire a BSE image
  895. log.Error("DoHolePreview: failed to acquire a BSE image.");
  896. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  897. // record end time
  898. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  899. return;
  900. }
  901. //BSEData
  902. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  903. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  904. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  905. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  906. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  907. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  908. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  909. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  910. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  911. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  912. {
  913. // measurement failed or stopped
  914. log.Error("DoHolePreview: measurement failed or stopped.");
  915. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  916. // record end time
  917. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  918. return;
  919. }
  920. //save the result to project file
  921. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  922. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  923. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  924. m_listHoleBSEImg.Add(pHoleBSEImg);
  925. // prepare for the next
  926. ++nNewFieldId;
  927. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  928. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  929. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  930. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  931. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  932. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  933. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  934. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  935. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  936. }
  937. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  938. //calculate measure time
  939. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  940. // let main thread to know that this sample measurement completes
  941. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  942. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  943. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  944. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  945. }
  946. }
  947. // Cumulate field data info
  948. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  949. {
  950. // get measure result items of the sample
  951. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  952. // cumulate field data info
  953. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  954. // cumulate ratio
  955. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  956. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  957. pMsrResults.SetRatio(dRatio);
  958. // go through the particles list
  959. foreach (COTSParticleClr pParticle in listParticles)
  960. {
  961. // create a measure result item
  962. pMsrResults.CumulateMeasureResult(pParticle);
  963. }
  964. // ok, return TRUE
  965. return true;
  966. }
  967. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  968. {
  969. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  970. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  971. foreach (COTSParticleClr part in mergedParts)
  972. {
  973. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  974. }
  975. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  976. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  977. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  978. foreach (COTSParticleClr part in mergedParts)
  979. {
  980. ches.Add(part.GetXray());
  981. }
  982. xraydb.SaveElementChemistriesList(ches);
  983. return true;
  984. }
  985. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  986. {
  987. string strFieldFileFolder = filedFileFoler;
  988. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  989. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  990. int nId = fldData.GetId();
  991. string sFieldId;
  992. sFieldId = nId.ToString();
  993. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  994. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  995. {
  996. log.Error("SaveFieldFiles: save BSE file failed.");
  997. return false;
  998. }
  999. // IncA Data list
  1000. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  1001. pDBFileMgr.SaveStatusDataToDB();
  1002. var fldDB = pDBFileMgr.GetFieldDB();
  1003. log.Warn("Start saving particle data.");
  1004. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  1005. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  1006. fldcmds.Add(fldcmd);
  1007. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  1008. //remomove the invalid particles
  1009. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  1010. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  1011. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  1012. var listAnalysisPosXray = new List<CPosXrayClr>();
  1013. foreach (var p in fldData.GetListXrayParticles())
  1014. {
  1015. listAnalysisPosXray.Add(p.GetXray());
  1016. }
  1017. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  1018. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1019. return true;
  1020. }
  1021. protected void SaveFieldMgrData()
  1022. {
  1023. while (bSaveThreadWorking)
  1024. {
  1025. while (fieldQueue.Count() > 0)
  1026. {
  1027. COTSFieldData f = fieldQueue.Dequeue();
  1028. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1029. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1030. }
  1031. if (fieldQueue.Count() == 0)
  1032. {
  1033. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1034. log.Warn("finished batch saving");
  1035. return;
  1036. }
  1037. }
  1038. return;
  1039. }
  1040. protected void StartSaveFileThread(ref COTSFieldData a_pFieldMgr)
  1041. {
  1042. fieldQueue.Enqueue(a_pFieldMgr);
  1043. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1044. {
  1045. if (bSaveThreadWorking == false)
  1046. {
  1047. bSaveThreadWorking = true;
  1048. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1049. m_thread_ptr.IsBackground = true;
  1050. m_thread_ptr.Start();
  1051. }
  1052. }
  1053. }
  1054. public void ParticleSpecialTreatment(ref COTSFieldData fld)
  1055. {
  1056. //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.
  1057. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  1058. double edsTime = 0; //to store the EDSTime returned from the engine.
  1059. var bigparts = fld.GetListXrayParticles();
  1060. var m_ClassifyEngine = new CClassifyEngine();
  1061. for (int i = 0; i < bigparts.Count(); i++)
  1062. {
  1063. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  1064. var p = bigparts[i];
  1065. // 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.
  1066. //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.
  1067. edsTime = engine.IfNeedMaxEDS(p);
  1068. if (edsTime > 0)
  1069. {
  1070. mapPartXray[p] = i;
  1071. }
  1072. }
  1073. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1074. if (mapPartXray.Count() > 0)
  1075. {
  1076. foreach (var p in mapPartXray)
  1077. {
  1078. partsMax.Add(p.Key);
  1079. }
  1080. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1081. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  1082. }
  1083. }
  1084. public void CalculateParticleAbsolutPos(ref COTSFieldData curFldData)
  1085. {
  1086. double dPixelSize = m_Sample.CalculatePixelSize();
  1087. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1088. foreach (var p in curFldData.GetListAnalysisParticles())
  1089. {
  1090. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1091. PointF semP = new PointF(); ;
  1092. Point semPos = new Point();
  1093. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.OTSPos, ref semP);
  1094. semPos.X = (int)semP.X;
  1095. semPos.Y = (int)semP.Y;
  1096. p.SetAbsolutPos(semPos);
  1097. }
  1098. }
  1099. }
  1100. }