SmplMeasureCleanliness.cs 18 KB

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