SmplMeasureCleanliness.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using OTSCOMMONCLR;
  9. using OTSDataType;
  10. using OTSModelSharp.Measure.OTSCleanliness;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSModelSharp.ServiceInterface;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. class CSmplMeasureCleanliness : CSmplMeasure
  17. {
  18. public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
  19. {
  20. SetWorkingFolder(a_strWorkingFolder);
  21. SetSample(a_pSample);
  22. m_classifyEngine = new CClassifyEngine();
  23. }
  24. // field image process
  25. public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
  26. {
  27. int nNewFieldId;
  28. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  29. //first step:remove background of the bse image and compound all the finded particles.
  30. curFldData = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
  31. CFieldDataClean curFldDataCln =( CFieldDataClean) curFldData;
  32. curFldData.SetId(nNewFieldId);
  33. curFldData.SetPosition(fldCenter);
  34. GetOriginalParticles();
  35. // second step :filter the finded particles.
  36. FilterParticles((CFieldDataClean)curFldData);
  37. CalculateParticlePos();
  38. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  39. //collect xray data.
  40. if (pXRayParam.GetUsingXray() == (int)OTS_USING_X_RAY.Yes)
  41. {
  42. Thread.Sleep(100);
  43. CollectParticlesXrayData((CFieldDataClean)curFldData);
  44. Thread.Sleep(100);
  45. }
  46. //special treatment.
  47. //ParticleSpecialTreatment();
  48. log.Info("Begin to Calculate the image property of every particle!");
  49. var analysisparts = curFldDataCln.ListBigParticles;
  50. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  51. ClassifyFieldParticles();
  52. // save field files
  53. m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  54. StartSaveFileThread(curFldData);
  55. return true;
  56. }
  57. public void CalculateParticlePos()
  58. {
  59. double dPixelSize = m_Sample.CalculatePixelSize();
  60. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  61. foreach (var p in curFldData.ListAnalysisParticles)
  62. {
  63. Point fldOtsPos = new Point(curFldData.PoiPos.X, curFldData.PoiPos.Y);
  64. Point semPos = new Point();
  65. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  66. p.SetAbsolutPos(semPos);
  67. }
  68. }
  69. // save field data
  70. protected void SaveFieldMgrData()
  71. {
  72. while (bSaveThreadWorking)
  73. {
  74. while (fieldQueue.Count() > 0)
  75. {
  76. CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
  77. double pixelSize = m_Sample.CalculatePixelSize();
  78. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  79. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  80. }
  81. if (fieldQueue.Count() == 0)
  82. {
  83. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  84. return;
  85. }
  86. }
  87. return;
  88. }
  89. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  90. {
  91. fieldQueue.Enqueue(a_pFieldMgr);
  92. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  93. {
  94. if (bSaveThreadWorking == false)
  95. {
  96. bSaveThreadWorking = true;
  97. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  98. m_thread_ptr.Start();//m_thread_ptr->detach();
  99. }
  100. }
  101. }
  102. public void FilterParticles(CFieldDataClean fld)
  103. {
  104. // 1)remove over sized particles, too small particles and get a analysis particles list
  105. double dPixelSize = m_Sample.CalculatePixelSize();
  106. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  107. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  108. curFldData.FilterParticles(pImgProcessParam, dPixelSize);
  109. if (curFldData.NoAnalysisParticle())
  110. {
  111. log.Warn("There's no analysis particles!");
  112. }
  113. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  114. int quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetQuantifyMinSize();
  115. var smallparts = fld.ListSmallParticles;
  116. var bigparts = fld.ListBigParticles;
  117. foreach (var part in curFldData.ListAnalysisParticles)
  118. {
  119. if (part.GetArea() < quantifyThreshold)
  120. {
  121. smallparts.Add(part);
  122. }
  123. else
  124. {
  125. bigparts.Add(part);
  126. }
  127. }
  128. fld.ListSmallParticles = smallparts;
  129. fld.ListBigParticles = bigparts;
  130. //fld.SmallParticlePercentage=percentage;
  131. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  132. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  133. return ;
  134. }
  135. public void CollectParticlesXrayData(CFieldDataClean fld)
  136. {
  137. // get x-ray parameters
  138. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  139. // calculate search x-ray acquire time
  140. uint nXRayAQTime;
  141. var smallparts = fld.ListSmallParticles;
  142. var bigparts = fld.ListBigParticles;
  143. var pStatus = m_Sample.GetMsrStatus();
  144. //// particle x-ray analysis
  145. ////=============================================
  146. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  147. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  148. // get x-ray list (analysis) by particle features
  149. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  150. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  151. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  152. // get x-ray list (analysis) by points
  153. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  154. return ;
  155. }
  156. public void ParticleSpecialTreatment(CFieldDataClean fld)
  157. {
  158. //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.
  159. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  160. double edsTime = 0; //to store the EDSTime returned from the engine.
  161. var bigparts = fld.ListBigParticles;
  162. var m_ClassifyEngine = new CClassifyEngine();
  163. for (int i = 0; i < bigparts.Count(); i++)
  164. {
  165. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  166. var p = bigparts[i];
  167. // 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.
  168. //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.
  169. edsTime = engine.IfNeedMaxEDS(p);
  170. if (edsTime > 0)
  171. {
  172. mapPartXray[p] = i;
  173. }
  174. }
  175. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  176. if (mapPartXray.Count() > 0)
  177. {
  178. foreach (var p in mapPartXray)
  179. {
  180. partsMax.Add(p.Key);
  181. }
  182. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  183. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  184. }
  185. }
  186. public override void ClassifyFieldParticles()
  187. {
  188. try
  189. {
  190. CFieldDataClean curFldDataCln = (CFieldDataClean)curFldData;
  191. string libname = m_Sample.GetMsrParams().GetSTDName();
  192. if (libname != "NoSTDDB")
  193. {
  194. log.Info("Begin to classify big particles!Using " + libname);
  195. var bigparts = curFldDataCln.ListBigParticles;
  196. ClassifyQuantifyParticles(bigparts,libname);
  197. var smallParts = curFldDataCln.ListSmallParticles;
  198. ClassifySmallParticles(smallParts, libname);
  199. }
  200. }
  201. catch (Exception e)
  202. {
  203. log.Info("calcu the particle image property or classify failed. "+e.Message);
  204. }
  205. }
  206. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  207. {
  208. try
  209. {
  210. try
  211. {
  212. string libname = m_Sample.GetMsrParams().GetSTDName();
  213. if (libname != "NoSTDDB")
  214. {
  215. log.Info("Begin to classify big particles!Using " + libname);
  216. var bigparts = mergedParts;
  217. ClassifyQuantifyParticles(bigparts, libname);
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. log.Info("calcu the particle image property or classify failed. " + e.Message);
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. log.Info(" classify failed. " + e.Message);
  228. }
  229. }
  230. public void SaveFieldParticlesData()
  231. {
  232. StartSaveFileThread(curFldData);
  233. }
  234. public void GetOriginalParticles()
  235. {
  236. // measure status
  237. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  238. // get image process parameter
  239. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  240. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  241. // remove BES image background
  242. curFldData.RemoveBSEImageBG(pImgProcessParam);
  243. // check if this is an empty image
  244. if (curFldData.NoParticle())
  245. { // empty fields
  246. log.Info("ImageProcess: empty field.");
  247. //return false;
  248. }
  249. return ;
  250. }
  251. public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
  252. {
  253. int nSize = (int)a_listAnalysisParticles.Count();
  254. // go through all analysis particles
  255. for (int i = 0; i < nSize; ++i)
  256. {
  257. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  258. //CPosXrayClr pXray = a_listAnalysisParticles[i].GetXray();
  259. // CLEEngine engine = GetParticleEngine(libname.GetBuffer());
  260. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  261. if (!engine.Classify(pParticle))
  262. {
  263. // invalid x-ray pointer.
  264. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  265. return false;
  266. }
  267. }
  268. // ok, return TRUE
  269. return true;
  270. }
  271. public bool ClassifySmallParticles(List<COTSParticleClr> smallparts, string libname)
  272. {
  273. List<COTSParticleClr> a_listAnalysisParticles = smallparts;
  274. int nSize = (int)a_listAnalysisParticles.Count();
  275. // go through all analysis particles
  276. for (int i = 0; i < nSize; ++i)
  277. {
  278. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  279. IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
  280. if (!engine.Classify(pParticle))
  281. {
  282. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. public bool SaveFieldFiles(CFieldDataClean f, string filedFileFoler)
  289. {
  290. string strFieldFileFolder = filedFileFoler;
  291. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  292. pBSEImgFileMgr.SetBSEImg(f.GetBSEImage());
  293. int nId = f.GetId();
  294. string sFieldId;
  295. sFieldId = nId.ToString();
  296. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  297. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  298. {
  299. log.Info("SaveFieldFiles: save BSE file failed.");
  300. return false;
  301. }
  302. // IncA Data list
  303. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  304. //pDBFileMgr.BeginTransaction();
  305. pDBFileMgr.SaveStatusDataToDB();
  306. if (!pDBFileMgr.SaveIncADataToDB(f.ListAnalysisParticles, f.GetPosition()))
  307. {
  308. log.Info("SaveFieldFiles: save inclusion file failed.");
  309. return false;
  310. }
  311. //save the xray data and element data.
  312. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  313. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  314. foreach (var p in f. ListAnalysisParticles)
  315. {
  316. m_listAnalysisPosXray.Add(p.GetXray());
  317. }
  318. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  319. {
  320. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  321. return false;
  322. }
  323. //save the small particle info
  324. CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
  325. Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
  326. foreach (COTSParticleClr smallP in f.ListSmallParticles)
  327. {
  328. if (mapSmallParts.ContainsKey((int)smallP.GetType()))
  329. {
  330. mapSmallParts[(int)smallP.GetType()].Add(smallP);
  331. }
  332. else
  333. {
  334. var plist = new List<COTSParticleClr>();
  335. plist.Add(smallP);
  336. mapSmallParts.Add((int)smallP.GetType(), plist);
  337. }
  338. }
  339. List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
  340. foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
  341. {
  342. CSmallParticleInfo partInfo = new CSmallParticleInfo();
  343. partInfo.FieldId = particles.Value[0].GetFieldId();
  344. partInfo.AveGray = particles.Value[0].GetAveGray();
  345. partInfo.Quantity = (int)(particles.Value.Count / f.SmallParticlePercentage);
  346. partInfo.TypeId = particles.Key;
  347. partInfo.TypeColor = particles.Value[0].GetTypeColor();
  348. partInfo.TypeName = particles.Value[0].GetTypeName();
  349. double area = 0;
  350. foreach (var p in particles.Value)
  351. {
  352. area += p.GetArea();
  353. }
  354. //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  355. partInfo.Area =(int) area ;
  356. smallparts.Add(partInfo);
  357. }
  358. foreach (CSmallParticleInfo smallp in smallparts)
  359. {
  360. smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  361. }
  362. //pDBFileMgr.CommitTransaction();
  363. return true;
  364. }
  365. }
  366. }