CSmplMeasure.cs 58 KB

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