SmplMeasureInclution.cs 13 KB

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