CSmplMeasure.cs 56 KB

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