SmplMeasureInclution.cs 12 KB

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