CSmplMeasure.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. using System.Windows.Forms;
  17. namespace OTSModelSharp
  18. {
  19. using CHoleBSEImgsList = List<CHoleBSEImg>;
  20. public class CSmplMeasure
  21. {
  22. protected static NLog.Logger log ;
  23. protected bool bSaveThreadWorking;
  24. protected System.Threading.Thread m_thread_ptr;
  25. protected COTSSample m_Sample;
  26. protected COTSSample m_HolePreviewSample;
  27. protected CMeasure m_pMsrThread;
  28. string m_strWorkingFolder;
  29. protected CSmplMsrResult m_pSampleRstFile;
  30. CHoleBSEImgsList m_listHoleBSEImg;
  31. protected SemController m_SemHardwareMgr;
  32. protected ScanController m_ScanHardwareMgr;
  33. protected EDSController m_EDSHardwareMgr;
  34. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  35. protected IClassifyEngine m_classifyEngine;
  36. public CSmplMeasure()
  37. {
  38. log = NLog.LogManager.GetCurrentClassLogger();
  39. m_pSampleRstFile = new CSmplMsrResult();
  40. m_SemHardwareMgr = SemController.GetSEMController();
  41. m_ScanHardwareMgr = ScanController.GetScanController();
  42. m_EDSHardwareMgr = EDSController.GetEDSController();
  43. m_listHoleBSEImg = new CHoleBSEImgsList();
  44. }
  45. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  46. {
  47. log = NLog.LogManager.GetCurrentClassLogger();
  48. m_strWorkingFolder = a_strWorkingFolder;
  49. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);
  50. m_SemHardwareMgr = SemController.GetSEMController();
  51. m_ScanHardwareMgr = ScanController.GetScanController();
  52. m_EDSHardwareMgr = EDSController.GetEDSController();
  53. m_listHoleBSEImg = new CHoleBSEImgsList();
  54. m_Sample = a_pSample;
  55. m_EDSHardwareMgr.SetAnalyExpCount(m_Sample.GetMsrParams().GetXRayParam().GetAnalyExpCount());
  56. }
  57. public void SetSample(COTSSample a_pSample)
  58. {
  59. m_Sample = a_pSample;
  60. m_pSampleRstFile.SetSample(a_pSample);
  61. }
  62. public COTSSample GetSample() { return m_Sample; }
  63. public void SetHolePreviewSample(COTSSample a_pSample)
  64. {
  65. m_HolePreviewSample = a_pSample;
  66. }
  67. public COTSSample GetHolePreviewSample() { return m_HolePreviewSample; }
  68. public void SetMsrThread(CMeasure mt)
  69. {
  70. m_pMsrThread = mt;
  71. }
  72. bool SetSEMDataMrs(COTSSample sample)
  73. {
  74. var pSEMDataMsr = sample.GetSEMDataMsr();
  75. double dMag = pSEMDataMsr.GetMagnification();
  76. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  77. var pSEMCtrl = m_pMsrThread.GetSEMController();
  78. pSEMCtrl.SetMagnification(dMag);
  79. pSEMCtrl.SetWorkingDistance(dWorkDis);
  80. return true;
  81. }
  82. bool SetSEMExteralOn()
  83. {
  84. var pSEMCtrl = m_SemHardwareMgr;
  85. log.Warn("Set Scan Exteral on!");
  86. pSEMCtrl.SetScanExternal(true);
  87. return true;
  88. }
  89. // set SEM external off
  90. bool SetSEMExteralOff()
  91. {
  92. var pSEMCtrlPtr = m_SemHardwareMgr;
  93. log.Warn("Set SEM Exteral Off!");
  94. pSEMCtrlPtr.SetScanExternal(false);
  95. return true;
  96. }
  97. bool SetBSEParam()
  98. {
  99. // get SEM controller
  100. var pSEMController = m_SemHardwareMgr;
  101. // get scan controller
  102. var pScanController = m_ScanHardwareMgr;
  103. // scan parameters
  104. var pMsrParam = m_Sample.GetMsrParams();
  105. var pImgScanParam = pMsrParam.GetImageScanParam();
  106. // get image size
  107. var nImageSizeId = pImgScanParam.GetImageResulotion();
  108. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  109. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  110. // get dwell time
  111. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  112. // convert dwell time to bruker dwell time (8, 16, 32)
  113. int nBrukerDwellTimeId=3;
  114. switch (nDwellTime)
  115. {
  116. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  117. nBrukerDwellTimeId = 3;
  118. break;
  119. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  120. nBrukerDwellTimeId = 4;
  121. break;
  122. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  123. nBrukerDwellTimeId = 5;
  124. break;
  125. default:
  126. nBrukerDwellTimeId = 3;
  127. break;
  128. }
  129. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  130. if (!pScanController.Init())
  131. {
  132. log.Error("SetBSEParam: failed to get scan control.");
  133. return false;
  134. }
  135. // set dwell time
  136. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  137. {
  138. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  139. return false;
  140. }
  141. // set image size
  142. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  143. {
  144. // failed to set dwell time
  145. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  146. return false;
  147. }
  148. return true;
  149. }
  150. bool SetHoleBSEParam(COTSSample sample)
  151. {
  152. // get SEM controller
  153. var pSEMController = m_SemHardwareMgr;
  154. // get scan controller
  155. var pScanController = m_ScanHardwareMgr;
  156. // scan parameters
  157. var pMsrParam = sample.GetMsrParams();
  158. var pImgScanParam = pMsrParam.GetImageScanParam();
  159. // get image size
  160. var nImageSizeId = pImgScanParam.GetImageResulotion();
  161. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  162. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  163. // get SEM controller
  164. //if (!pSEMController.IsConnected())
  165. //{
  166. // if (!pSEMController.Connect())
  167. // {
  168. // log.Error("SetBSEParam: can't connect SEM.");
  169. // return false;
  170. // }
  171. //}
  172. // get dwell time
  173. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  174. // convert dwell time to bruker dwell time (6, 16, 32)
  175. 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.
  176. if (!pScanController.Init())
  177. {
  178. log.Error("SetBSEParam: failed to get scan control.");
  179. return false;
  180. }
  181. // set dwell time
  182. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  183. {
  184. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  185. return false;
  186. }
  187. // set image size
  188. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  189. {
  190. // failed to set dwell time
  191. log.Error("SetBSEParam: failed to set ImageSize");
  192. return false;
  193. }
  194. return true;
  195. }
  196. public void SetWorkingFolder(String a_strWorkingFolder)
  197. {
  198. // add "\\" at the string end if it is not "\\"
  199. if (a_strWorkingFolder.PadRight(1)!="\\")
  200. {
  201. a_strWorkingFolder += "\\";
  202. }
  203. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  204. }
  205. bool CalculateUnMeasuredFieldsCenters(COTSSample sample, out List<System.Drawing.Point> a_allpieldcenter, out List<System.Drawing.Point> a_listUnMsrFieldCenter)
  206. {
  207. // sample measure parameters
  208. CSampleParam pMsrParam = sample.GetMsrParams();
  209. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  210. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  211. CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  212. CMsrSampleStatus pStatus = sample.GetMsrStatus();
  213. // measured field centers list
  214. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  215. // field centers list manager
  216. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  217. // init field centers list manager
  218. if (!pFieldMgr.Init(sample.GetMsrArea(), pImgProcParam, poImageScanParam, poSEMDataMsr, listCompletedCenter))
  219. {
  220. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  221. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  222. a_allpieldcenter = new List<Point>();
  223. return false;
  224. }
  225. // get field centers list
  226. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  227. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  228. // ok, return TRUE
  229. return true;
  230. }
  231. protected bool IsAborted()
  232. {
  233. return m_pMsrThread.IsMeasureStopped();
  234. }
  235. protected bool IsPaused()
  236. {
  237. //Debug.Assert(m_pMsrThread == null);
  238. var statu = m_pMsrThread.GetMsrThreadStatus();
  239. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  240. {
  241. return true;
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. }
  248. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  249. {
  250. string sStopMode = a_pScanParam.GetStopMode();
  251. int nStopField = a_pScanParam.GetStopParamFields();
  252. // completed fields number
  253. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  254. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  255. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  256. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  257. int nNumParticle = 0;
  258. foreach (var pResult in listMsrResult)
  259. {
  260. if (pResult.GetTypeId() > (int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  261. {
  262. nNumParticle += (int)pResult.GetNumber();
  263. }
  264. }
  265. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  266. int nDay = timeSpan.Days;
  267. int nHour = timeSpan.Hours;
  268. int nMin = timeSpan.Minutes;
  269. int nSec = timeSpan.Seconds;
  270. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  271. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  272. int nParticlAim = a_pScanParam.GetStopParamParticles();
  273. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  274. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  275. bool bRet = false;
  276. string[] str = sStopMode.Replace(" ", "").Split('+');
  277. for(int i=0;i< str.Length;i++)
  278. {
  279. switch (int.Parse(str[i].Split(':')[0])-1)
  280. {
  281. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  282. // completed fields number
  283. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  284. {
  285. bRet = true;
  286. }
  287. break;
  288. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  289. if (nCompeltedField >= nStopField)
  290. {
  291. bRet = true;
  292. }
  293. break;
  294. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  295. if (nNumParticle >= nParticlAim)
  296. {
  297. bRet = true;
  298. }
  299. break;
  300. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  301. if (nUsedTime >= nMeasTimeAim)
  302. {
  303. bRet = true;
  304. }
  305. break;
  306. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  307. if (NMeasArea >= NMeasAreaAim)
  308. {
  309. bRet = true;
  310. }
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. return bRet;
  317. }
  318. // move SEM to the point
  319. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  320. {
  321. // get SEM controller
  322. var pSEMController = m_SemHardwareMgr;
  323. PointF a_SEMpt = new Point();
  324. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  325. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  326. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  327. {
  328. return false;
  329. }
  330. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  331. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  332. // move SEM to the position (rotation 0)
  333. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  334. {
  335. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  336. return false;
  337. }
  338. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  339. if (pMsrParam.SlopParam.IsUsingSlopParam)
  340. {
  341. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  342. double originWd = 0;
  343. pSEMController.GetWorkingDistance(ref originWd);
  344. if ((wd - originWd) > 2)
  345. {
  346. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  347. }
  348. else
  349. {
  350. Thread.Sleep(hardWareDelay);
  351. pSEMController.SetWorkingDistance(wd);
  352. }
  353. }
  354. Thread.Sleep(hardWareDelay);
  355. return true;
  356. }
  357. // Acquire a BSE image
  358. CBSEImgClr AcquireABSEImage()
  359. {
  360. // BSE image
  361. CBSEImgClr pBSEImage = null;
  362. // get scan controller
  363. var pScanController = m_ScanHardwareMgr;
  364. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  365. return pBSEImage;
  366. }
  367. public virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  368. {
  369. return;
  370. }
  371. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  372. {
  373. return;
  374. }
  375. private class SEMStateObject
  376. {
  377. private PointF pos;
  378. private double workingDistance;
  379. private CSEMDataGnr semdata;
  380. private COTSSample originalSample;
  381. private double magnification;
  382. public PointF Pos { get => pos; set => pos = value; }
  383. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  384. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  385. public double Magnification { get => magnification; set => magnification = value; }
  386. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  387. }
  388. private class AutoResetSEMControl:IDisposable
  389. {
  390. CSmplMeasure sm;
  391. private SEMStateObject semState=null;
  392. public AutoResetSEMControl(CSmplMeasure s)
  393. {
  394. sm = s;
  395. }
  396. public SEMStateObject SemState
  397. {
  398. get => semState;
  399. set => semState = value;
  400. }
  401. public void Dispose()
  402. {
  403. if (semState != null)
  404. {
  405. sm.SetBSEParam();
  406. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  407. sm.MoveSEMToPoint(semState.Pos);
  408. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  409. }
  410. sm.SetSEMExteralOff();
  411. }
  412. }
  413. public void DoMeasureForOneSample()
  414. {
  415. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  416. {
  417. // let the main thread to know that this sample measurement starts
  418. var pStatus = m_Sample.GetMsrStatus();
  419. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  420. // set current time to current time
  421. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  422. // let main App know that the sample begin to measure
  423. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  424. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  425. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  426. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  427. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  428. log.Info(m_Sample.GetName() + " Measurement started!");
  429. // get SEM controller to set magnification and working distance
  430. if (!SetSEMDataMrs(m_Sample))
  431. {
  432. log.Error("DoMeasure: fail to set SEM data.");
  433. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  434. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  435. return;
  436. }
  437. // set the BSE scan param
  438. if (!SetBSEParam())
  439. {
  440. log.Error("DoMeasure: fail to set BSE param.");
  441. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  442. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  443. return;
  444. }
  445. var pSEMDataGnr = new CSEMDataGnr();
  446. log.Info("Get Kv, Brightness and Contrast!");
  447. m_SemHardwareMgr.GetSEMDataGnrFromHw(ref pSEMDataGnr);
  448. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  449. // record SEM data
  450. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  451. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  452. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  453. // record stage
  454. CStage pStage = pProjMgrFile.GetStage();
  455. m_pSampleRstFile.SetSEMStage(pStage);
  456. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  457. log.Info("Create result file!");
  458. if (!m_pSampleRstFile.CreateResultFiles())
  459. {// failed to call measure result file Save method
  460. log.Error("DoMeasure: failed to call measure result file Save method.");
  461. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  462. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  463. return;
  464. }
  465. //------
  466. var FldDatas = m_Sample.GetFieldsData();
  467. List<string> KeyNameList = new List<string>();
  468. if (m_EDSHardwareMgr.eds.GetEDSName() == "EDS Bruker" && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  469. {
  470. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  471. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  472. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  473. }
  474. for (int i = 0; i < FldDatas.Count; ++i)
  475. {// check and break if stop button is clicked
  476. var curFld = FldDatas[i];
  477. if (curFld.GetIsMeasureComplete())
  478. {
  479. continue;
  480. }
  481. if (IsPaused())
  482. {// measure stopped
  483. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  484. // record end time
  485. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  486. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  487. while (fieldQueue.Count() > 0)
  488. {
  489. Thread.Sleep(100);
  490. }
  491. SetSEMExteralOff();
  492. // update thread measure status class, let the main thread know that this sample measurement stopped
  493. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  494. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  495. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  496. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.PAUSED;
  497. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime.Now.ToShortDateString();
  498. MsgSmpStop.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.PAUSED;
  499. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStop);
  500. while (IsPaused())
  501. {
  502. Thread.Sleep(300);
  503. }
  504. }
  505. if (IsAborted())
  506. {// measure stopped
  507. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  508. // record end time
  509. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  510. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  511. while (fieldQueue.Count() > 0)
  512. {
  513. Thread.Sleep(100);
  514. }
  515. break;
  516. }
  517. // check if sample measurement completes
  518. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  519. if (IsSampleOver(pScanParam))
  520. {
  521. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  522. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  523. break;
  524. }
  525. // get a field center
  526. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  527. // update thread measure status class, let the main thread know that starts a new field
  528. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  529. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  530. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  531. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = new Point((int)poiFieldCentre.X,(int)poiFieldCentre.Y);
  532. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  533. int fldNo = curFld.GetId();
  534. log.Warn("Current field:" + fldNo.ToString());
  535. // move SEM to the field center
  536. if (!MoveSEMToPoint(poiFieldCentre))
  537. {// failed to move SEM to the position
  538. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  539. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  540. // record end time
  541. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  542. return;
  543. }
  544. log.Info("Begin to Acquire BSE image!");
  545. // take BSE image for the fields
  546. CBSEImgClr pBSEImg = AcquireABSEImage();
  547. // let the main thread to know that image process is completed
  548. if (pBSEImg == null)
  549. {
  550. log.Error("ImageProcess: can't get no background image.");
  551. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  552. return;
  553. }
  554. curFld.strKeyName = KeyNameList;
  555. //BSEData
  556. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  557. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  558. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  559. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = (int)poiFieldCentre.X;
  560. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = (int)poiFieldCentre.Y;
  561. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  562. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  563. byte[] pImgData = pBSEImg.GetImageDataPtr();
  564. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  565. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  566. // get a new field id
  567. log.Info("Acquire BSE image success! Processing image...");
  568. // image process
  569. var rst = FieldImageProcess(curFld, pBSEImg);
  570. if (rst == true)
  571. {
  572. // add the field into the field
  573. curFld.SetIsMeasureComplete(true);
  574. }
  575. double measuredArea = 0; // this area should be the field area
  576. var a_pBSEImg = curFld.GetBSEImage();
  577. double dPixelSize = curFld.GetPixelSize();
  578. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  579. if (!CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea))
  580. { // failed to call SaveFieldFile method
  581. log.Error("ImageProcess: call CumulateFieldData method.");
  582. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  583. return;
  584. }
  585. log.Info("Send field data to screen!");
  586. pStatus.AddCompletedFieldCenter(poiFieldCentre);
  587. //Field Data
  588. // record end time
  589. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  590. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  591. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  592. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  593. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  594. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = m_Sample.GetFieldsData().Count;
  595. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFld.GetListAnalysisParticles().Count;
  596. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  597. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  598. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  599. }
  600. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  601. {
  602. Thread.Sleep(1000);
  603. log.Warn("db saving!");
  604. }
  605. TheLastWorkOfSampleMeasure();
  606. }
  607. }
  608. public void TheLastWorkOfSampleMeasure()
  609. {
  610. COTSSample theSample = m_Sample;
  611. var pStatus = theSample.GetMsrStatus();
  612. //merging particles
  613. log.Info("Merging big particles which are crossing the field edge!");
  614. CImageHandler imgpro = new CImageHandler();
  615. int scanfldsize = theSample.GetSEMDataMsr().GetScanFieldSize();
  616. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  617. double pixelSize = theSample.CalculatePixelSize();
  618. //Repeated particle treatment
  619. if (theSample.GetMsrParams().GetImageProcessParam().GetPartCombin())
  620. {
  621. log.Info("begin repeated particle treatment...");
  622. if (imgpro.RepeatedParticleTreatment(theSample.GetFieldsData(), theSample, m_pSampleRstFile.m_strdbPathName))
  623. {
  624. log.Info("success...");
  625. }
  626. else
  627. {
  628. log.Info("faild...");
  629. }
  630. }
  631. imgpro.MergeBigBoundaryParticles(theSample.GetFieldsData(), pixelSize, scanfldsize, theSample.GetResolutionSize(), ref mergedParticles);
  632. CalculateMergedPartProperty(mergedParticles, pixelSize);
  633. ClassifyMergedParticles(mergedParticles);
  634. log.Info("begin merged particle data db saving...");
  635. SaveMergedParticles(mergedParticles);
  636. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  637. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  638. // let main thread to know that this sample measurement completes
  639. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  640. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  641. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  642. MsgSmplEnd.STMSampleStu.cSampleName = theSample.GetName();
  643. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  644. }
  645. public bool DoMEasureForReMeasure()
  646. {
  647. return true;
  648. }
  649. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  650. {
  651. var m_ImagePro = new CImageHandler();
  652. foreach (COTSParticleClr part in mergedParticles)
  653. {
  654. m_ImagePro.CalParticleImageProp(part, pixelSize);
  655. }
  656. }
  657. public bool FieldImageProcess(COTSFieldData curFldData, CBSEImgClr a_pBSEImg)
  658. {
  659. PointF fldCenter = curFldData.OTSPos;
  660. curFldData.SetBSEImage(a_pBSEImg);
  661. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  662. PointF semPos = new Point();
  663. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  664. curFldData.SetSemPos(semPos);
  665. //first step:remove background of the bse image and compound all the finded particles.
  666. log.Info("Begin to process image and get all particles!");
  667. GetOriginalParticles(ref curFldData);
  668. // second step :filter the finded particles.
  669. log.Info("Begin to filter particles!");
  670. FilterParticles(ref curFldData);
  671. CalculateParticleAbsolutPos(ref curFldData);
  672. log.Info("Begin to Calculate the image property of every particle!");
  673. var analysisparts = curFldData.GetListAnalysisParticles();
  674. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  675. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  676. if (pXRayParam.GetUsingXray() == true)
  677. {
  678. log.Info("Begin to collect particle's xray data!");
  679. Thread.Sleep(100);
  680. m_EDSHardwareMgr.SetResolution(a_pBSEImg.GetWidth(), a_pBSEImg.GetHeight());
  681. CollectParticlesXrayData(curFldData);
  682. Thread.Sleep(100);
  683. }
  684. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  685. ClassifyFieldParticles(curFldData);
  686. // save field files
  687. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  688. StartSaveFileThread(ref curFldData);
  689. return true;
  690. }
  691. public void GetOriginalParticles(ref COTSFieldData curFldData)
  692. {
  693. // measure status
  694. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  695. // get image process parameter
  696. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  697. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  698. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  699. var pixelsize = m_Sample.CalculatePixelSize();
  700. if (specialPartsparam.GetIsToRun())
  701. {
  702. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  703. foreach (var grayRange in ranges)
  704. {
  705. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  706. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  707. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  708. }
  709. }
  710. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  711. // remove BES image background
  712. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  713. // check if this is an empty image
  714. if (curFldData.NoParticle())
  715. { // empty fields
  716. log.Info("ImageProcess: empty field.");
  717. }
  718. return;
  719. }
  720. public void FilterParticles(ref COTSFieldData curFldData)
  721. {
  722. if (curFldData.NoAnalysisParticle())
  723. {
  724. log.Warn("There's no analysis particles!");
  725. return;
  726. }
  727. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  728. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  729. double dPixelSize = m_Sample.CalculatePixelSize();
  730. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  731. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  732. var listXray1 = new List<COTSParticleClr>();
  733. var pXRayParam = pMsrParam.GetXRayParam();
  734. if (listXray.Count > pXRayParam.GetXrayLimit())
  735. {
  736. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  737. {
  738. listXray1.Add(listXray[i]);
  739. }
  740. }
  741. else
  742. {
  743. listXray1 = listXray;
  744. }
  745. curFldData.SetListAnalysisParticles(listXray1);
  746. log.Info("Analysis particles:"+ listXray1.Count);
  747. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  748. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  749. var smallparts = curFldData.ListSmallParticles;
  750. var bigparts = curFldData.ListBigParticles;
  751. foreach (var part in listXray1)
  752. {
  753. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  754. if (equalCircleDiameter < quantifyThreshold)
  755. {
  756. smallparts.Add(part);
  757. }
  758. else
  759. {
  760. bigparts.Add(part);
  761. }
  762. }
  763. curFldData.ListSmallParticles = smallparts;
  764. curFldData.ListBigParticles = bigparts;
  765. //fld.SmallParticlePercentage=percentage;
  766. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  767. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  768. return;
  769. }
  770. public void CollectParticlesXrayData(COTSFieldData curFldData)
  771. {
  772. // get x-ray parameters
  773. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  774. // calculate search x-ray acquire time
  775. uint nXRayAQTime;
  776. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  777. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  778. foreach (var p in curFldData.ListSmallParticles)
  779. {
  780. if (p.IsXrayParticle())
  781. {
  782. smallparts.Add(p);
  783. }
  784. }
  785. foreach (var p in curFldData.ListBigParticles)
  786. {
  787. if (p.IsXrayParticle())
  788. {
  789. bigparts.Add(p);
  790. }
  791. }
  792. m_EDSHardwareMgr.nameList = curFldData.strKeyName;
  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. }