SmplMeasureInclution.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.OTSInclution;
  11. using OTSModelSharp.ServiceCenter;
  12. using OTSModelSharp.ServiceInterface;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. class CSmplMeasureInclution : CSmplMeasure
  17. {
  18. public CSmplMeasureInclution(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_pBSEImg)
  26. {
  27. int nNewFieldId;
  28. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  29. // create a field
  30. curFldData = new CFieldDataIncA(a_pBSEImg, m_Sample.CalculatePixelSize());
  31. curFldData.SetId(nNewFieldId);
  32. curFldData.SetPosition(fldCenter);
  33. //first step:remove background of the bse image and compound all the finded particles.
  34. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  35. log.Info("Begin to process image and get all particles!");
  36. GetOriginalParticles();
  37. // second step :filter the finded particles.
  38. log.Info("Begin to filter particles!");
  39. FilterParticles();
  40. if (pXRayParam.GetUsingXray() == (int)OTS_USING_X_RAY.Yes)
  41. {
  42. Thread.Sleep(100);
  43. CollectParticlesXrayData();
  44. Thread.Sleep(100);
  45. }
  46. log.Info("Begin to Calculate the image property of every particle!");
  47. var analysisparts = curFldData.ListAnalysisParticles;
  48. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  49. log.Info("Begin to classify particles! particle num:"+ curFldData.ListAnalysisParticles.Count);
  50. ClassifyParticles(curFldData.ListAnalysisParticles);
  51. // save field files
  52. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  53. StartSaveFileThread(curFldData);
  54. return true; ;
  55. }
  56. // save field data
  57. protected void SaveFieldMgrData()
  58. {
  59. while (bSaveThreadWorking)
  60. {
  61. while (fieldQueue.Count() > 0)
  62. {
  63. COTSFieldData f = fieldQueue.Dequeue();
  64. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  65. SaveFieldFiles(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  66. }
  67. if (fieldQueue.Count() == 0)
  68. {
  69. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  70. return;
  71. }
  72. }
  73. return;
  74. }
  75. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  76. {
  77. fieldQueue.Enqueue(a_pFieldMgr);
  78. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  79. {
  80. if (bSaveThreadWorking == false)
  81. {
  82. bSaveThreadWorking = true;
  83. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  84. m_thread_ptr.Start();//m_thread_ptr->detach();
  85. }
  86. }
  87. }
  88. public void FilterParticles()
  89. {
  90. // remove over sized particles, too small particles and get a analysis particles list
  91. // get pixel size
  92. double dPixelSize = m_Sample.CalculatePixelSize();
  93. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  94. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  95. curFldData.FilterParticles(pImgProcessParam, dPixelSize );
  96. //calculate the real sem position of the particle
  97. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  98. foreach (var p in curFldData.ListAnalysisParticles)
  99. {
  100. var r = (Rectangle)p.GetParticleRect();
  101. Point fldLeftTopOTSPos = new Point(curFldData.PoiPos.X - curFldData.Width / 2, curFldData.PoiPos.Y + curFldData.Height / 2);//OTS coordinate
  102. Point Partcenter = new Point(r.Top + r.Height / 2, r.Left + r.Width / 2);//screen coordinate
  103. Point otsPartCenter = new Point((int)(fldLeftTopOTSPos.X + Partcenter.X * dPixelSize), (int)(fldLeftTopOTSPos.Y + Partcenter.Y * dPixelSize));
  104. Point sempoint = new Point();
  105. pCSEMStageData.ConverOTSToSEMPoint(otsPartCenter, ref sempoint);
  106. p.SetAbsolutPos(sempoint);
  107. }
  108. // make sure the particles list is not empty
  109. if (curFldData.NoAnalysisParticle())
  110. {
  111. log.Info("There's no particles to be analysed!");
  112. //return false;
  113. }
  114. return ;
  115. }
  116. public void CollectParticlesXrayData()
  117. {
  118. // get x-ray parameters
  119. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  120. var listAnalysisparts = curFldData.ListAnalysisParticles;
  121. var pStatus = m_Sample.GetMsrStatus();
  122. ////=============================================
  123. curFldData.CreateXrayList(listAnalysisparts);
  124. var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
  125. // get x-ray list (analysis) by particle features
  126. uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  127. if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  128. {
  129. log.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listAnalysisparts.Count);
  130. m_EDSHardwareMgr.GetXRayByFeatures(listAnalysisparts, nXRayAQTime, true);
  131. }
  132. else
  133. {
  134. log.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listAnalysisparts.Count);
  135. // get x-ray list (analysis) by points
  136. m_EDSHardwareMgr.GetXRayByPoints(listAnalysisparts, nXRayAQTime, true);
  137. }
  138. return ;
  139. }
  140. public override void ClassifyParticles(List<COTSParticleClr> parts)
  141. {
  142. try
  143. {
  144. var quantifyparts = parts;
  145. int nSize = quantifyparts.Count();
  146. // go through all analysis particles
  147. for (int i = 0; i < nSize; ++i)
  148. {
  149. string libname = m_Sample.GetMsrParams().GetSTDName();
  150. ClassifyIncAParticle(quantifyparts[i], libname);
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. log.Info(" classify failed. " + e.Message);
  156. }
  157. }
  158. public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
  159. {
  160. int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
  161. particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
  162. if (m_Sample.IfUsingSysSTD())
  163. {
  164. if (libname != "NoSTDDB")
  165. {
  166. //var m_classifyEngine = new CClassifyEngine();
  167. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  168. engine.Classify(particle);
  169. }
  170. if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
  171. {
  172. IClassifyEngine engine;
  173. engine = m_classifyEngine.GetIncClassifyEngine();
  174. engine.ClassifyIncA(particle, steelTech);
  175. }
  176. }
  177. else
  178. {
  179. if (libname != "NoSTDDB")
  180. {
  181. //var m_classifyEngine = new CClassifyEngine();
  182. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  183. engine.Classify(particle);
  184. }
  185. }
  186. return true;
  187. }
  188. public void GetOriginalParticles()
  189. {
  190. // measure status
  191. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  192. // get image process parameter
  193. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  194. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  195. // remove BES image background
  196. curFldData.RemoveBSEImageBG(pImgProcessParam);
  197. // check if this is an empty image
  198. if (curFldData.NoParticle())
  199. { // empty fields
  200. log.Info("ImageProcess: empty field.");
  201. //return false;
  202. }
  203. return ;
  204. }
  205. public bool SaveFieldFiles(COTSFieldData fldData,string filedFileFoler)
  206. {
  207. //loger.Warn("begin bitmap saving...");
  208. string strFieldFileFolder = filedFileFoler;
  209. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  210. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  211. int nId = fldData.GetId();
  212. string sFieldId;
  213. sFieldId = nId.ToString();
  214. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  215. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  216. {
  217. log.Info("SaveFieldFiles: save BSE file failed.");
  218. return false;
  219. }
  220. // IncA Data list
  221. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  222. pDBFileMgr.SaveStatusDataToDB();
  223. if (!pDBFileMgr.SaveIncADataToDB(curFldData.ListAnalysisParticles, fldData.GetPosition()))
  224. {
  225. log.Info("SaveFieldFiles: save inclusion file failed.");
  226. return false;
  227. }
  228. //save the xray data and element data.
  229. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  230. var m_listAnalysisPosXray = new List<CPosXrayClr>();
  231. foreach (var p in curFldData.ListAnalysisParticles)
  232. {
  233. m_listAnalysisPosXray.Add(p.GetXray());
  234. }
  235. if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  236. {
  237. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  238. return false;
  239. }
  240. return true;
  241. }
  242. }
  243. }