COTSMsrPrjResultData.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. using OTSCLRINTERFACE;
  2. using OTSDataType;
  3. using OTSModelSharp.ServiceCenter;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using System.Xml;
  11. using static OTSDataType.otsdataconst;
  12. namespace OTSModelSharp
  13. {
  14. public class COTSMsrPrjResultData
  15. {
  16. // SEM stage data
  17. CSEMStageData m_pSEMStageData;
  18. //// stage
  19. CSampleHolder m_pStage;
  20. // general parameter
  21. COTSCommonParam m_CommonParam;
  22. // sample list
  23. List<COTSSample> m_listSamples = new List<COTSSample>();
  24. bool m_bModify;
  25. // path name
  26. String m_strPathName;
  27. // working sample index
  28. int m_nWorkingSampeIndex;
  29. const String UNTITLED_FILE_NAME = "Untitled";
  30. const String SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER = "FIELD_FILES";
  31. // Inclusion file name
  32. const String SMPL_MSR_RESULT_INCLUSION_FILE = "Inclusion.db";//put all the table in one db file so that report can make join query.
  33. private otsdataconst.OTS_SysType_ID systemTypeId;
  34. public OTS_SysType_ID SystemTypeId { get => systemTypeId; set => systemTypeId = value; }
  35. public COTSMsrPrjResultData()
  36. {
  37. Init();
  38. }
  39. // initialization
  40. public void Init()
  41. {
  42. m_listSamples.Clear();
  43. //m_listHoleBSEImg.Clear();
  44. m_bModify = false;
  45. m_strPathName = "";
  46. m_nWorkingSampeIndex = -1;
  47. }
  48. public void SetPathName(String a_strPathName) { m_strPathName = a_strPathName; }
  49. public bool Load(string strPathName)
  50. {
  51. Cleanup();
  52. // check if the file exist
  53. if (!FileExists(strPathName))
  54. {
  55. // shouldn't happen, file does not exist
  56. return false;
  57. }
  58. //获取测量项目文件
  59. XmlDocument doc = new XmlDocument();
  60. //载入xml文件
  61. doc.Load(strPathName);
  62. XmlNode root = doc.SelectSingleNode("XMLData");
  63. Serialize(false, doc, root);
  64. //设置测量项目文件路径
  65. SetPathName(strPathName);
  66. //-------------
  67. foreach (var smpl in m_listSamples)
  68. {
  69. // get path of the pathname
  70. List<COTSField> allMeasuredFlds = new List<COTSField>();
  71. CMsrSampleStatus poMsrStatus = new CMsrSampleStatus();
  72. CMsrResultItems poMsrResults = smpl.GetMsrResults();
  73. double aFldArea = smpl.CalculateAFieldArea();
  74. int w, h;
  75. smpl.GetBSESize(out w, out h);
  76. double pixelsize = smpl.CalculatePixelSize();
  77. var overlap = smpl.GetMsrParams().GetImageProcessParam().GetOverlapParam();
  78. var noParticledataFields = smpl.GetFieldsData();//there's only the field postion info in the xml file,so the initial filed has only postion info.
  79. List<COTSField> allflddata = new List<COTSField>();//contains all the field
  80. if (GetAllFieldsFromDB(ref allMeasuredFlds, ref poMsrStatus,smpl.GetName(),ref poMsrResults, aFldArea, new Size(w, h), pixelsize))
  81. {
  82. //------------complete the infomation that doesn't contain in database-----
  83. foreach (var f in allMeasuredFlds)
  84. {
  85. f.SetPixelSize(pixelsize);
  86. f.ImgWidth = w;
  87. f.ImgHeight = h;
  88. double effectiveWidth = f.ImgWidth * pixelsize - 2 * overlap;
  89. double effectiveHeight = f.ImgHeight * pixelsize - 2 * overlap;
  90. PointF fldPos = f.GetOTSPosition();
  91. Point lt = new Point((int)fldPos.X - (int)effectiveWidth / 2, (int)fldPos.Y - (int)effectiveHeight / 2);
  92. Point rb = new Point((int)fldPos.X + (int)effectiveWidth / 2, (int)fldPos.Y + (int)effectiveHeight / 2);
  93. f.SetOTSRect(new OTSMeasureApp._0_OTSModel.OTSDataType.COTSRect(lt, rb));
  94. f.SetMeasureSequence(f.GetId());
  95. f.SetIsMeasureComplete(true);
  96. }
  97. }
  98. //------------------------------
  99. foreach (var emptyfld in noParticledataFields)
  100. {
  101. bool isMeasured = false;
  102. foreach (var measuredField in allMeasuredFlds)
  103. {
  104. if (measuredField.PositionEquals(emptyfld))
  105. {
  106. allflddata.Add(measuredField);
  107. isMeasured = true;
  108. break;
  109. }
  110. }
  111. if (!isMeasured)
  112. {
  113. emptyfld.SetPixelSize(pixelsize);
  114. emptyfld.ImgWidth = w;
  115. emptyfld.ImgHeight = h;
  116. double effectiveWidth = emptyfld.ImgWidth * pixelsize - 2 * overlap;
  117. double effectiveHeight = emptyfld.ImgHeight * pixelsize - 2 * overlap;
  118. PointF fldPos = emptyfld.GetOTSPosition();
  119. Point lt = new Point((int)fldPos.X - (int)effectiveWidth / 2, (int)fldPos.Y - (int)effectiveHeight / 2);
  120. Point rb = new Point((int)fldPos.X + (int)effectiveWidth / 2, (int)fldPos.Y + (int)effectiveHeight / 2);
  121. emptyfld.SetOTSRect(new OTSMeasureApp._0_OTSModel.OTSDataType.COTSRect(lt, rb));
  122. emptyfld.SetIsMeasureComplete(false);
  123. emptyfld.SetMeasureSequence(emptyfld.GetId());
  124. allflddata.Add(emptyfld);
  125. }
  126. }
  127. smpl.SetFieldsData(allflddata);
  128. // file validation
  129. smpl.SetMsrStatus(poMsrStatus);
  130. smpl.SetMsrResults(poMsrResults);
  131. List<Point> listCenterPoint = new List<Point>();
  132. Size rectSize = new Size();
  133. smpl.InitFieldPosData(out listCenterPoint, out rectSize);//init the underlevel fieldmgr object ,so that we can use it later
  134. }
  135. if (m_listSamples.Count > 0)
  136. {
  137. m_nWorkingSampeIndex = 0;
  138. }
  139. //-----------------------------------------
  140. return true;
  141. }
  142. // gets the name of the folder
  143. public String GetFolderName(String a_strPathName)
  144. {
  145. string str = "";
  146. str = Path.GetDirectoryName(a_strPathName);
  147. return str.ToString();
  148. }
  149. public bool FileExists(string a_sPath)
  150. {
  151. bool b_IsExists = File.Exists(a_sPath);
  152. return b_IsExists;
  153. }
  154. public bool DirectoryExists(string a_sPath)
  155. {
  156. bool b_IsExists = Directory.Exists(a_sPath);
  157. return b_IsExists;
  158. }
  159. // cleanup
  160. public void Cleanup()
  161. {
  162. //m_listHoleBSEImg.Clear();
  163. m_listSamples.Clear();
  164. m_bModify = false;
  165. m_strPathName = "";
  166. m_nWorkingSampeIndex = -1;
  167. }
  168. // set modify flag
  169. public void SetModify(bool a_bModify = true) { m_bModify = a_bModify; }
  170. // file pathname
  171. public String GetPathName() { return m_strPathName; }
  172. public bool GetAllFieldsFromDB(ref List<COTSField> allFlds, ref CMsrSampleStatus status,string sampleName,ref CMsrResultItems rst, double aFieldArea, Size picSize, double pixelSize)
  173. {
  174. String strFilePath = GetFolderName(m_strPathName);
  175. if (strFilePath == "")
  176. {
  177. // file path string is an empty string
  178. return false;
  179. }
  180. // field file sub folder string
  181. String strFieldFileSubFolder = strFilePath + "\\" + sampleName + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER + "\\";
  182. String strIncAFilename = strFieldFileSubFolder + "\\" + SMPL_MSR_RESULT_INCLUSION_FILE;
  183. if (!FileExists(strIncAFilename))
  184. {
  185. return false;
  186. }
  187. List<System.Drawing.PointF> completedFldPositions = new List<System.Drawing.PointF>();
  188. CIncAFileMgr incAFileMgr = new CIncAFileMgr(strIncAFilename);
  189. var m_IncADataDB = incAFileMgr.GetIncADB();
  190. List<COTSField> allFldsWithdata = new List<COTSField>();
  191. m_IncADataDB.GetAllFieldsRecord(pixelSize, ref allFldsWithdata);
  192. var eleChemistryDB = incAFileMgr.GetPosXrayDBMgr().GetElementChemistryDB();
  193. Dictionary<string, CPosXrayClr> mapXrayInfo = new Dictionary<string, CPosXrayClr>();
  194. eleChemistryDB.GetAllMapedXrayInfo(ref mapXrayInfo);
  195. foreach (var f in allFldsWithdata)
  196. {
  197. foreach (var p in f.GetListAnalysisParticles())
  198. {
  199. int curFldId = f.GetId();
  200. int xrayId = p.GetAnalysisId();
  201. string fldvec = "";
  202. fldvec += curFldId.ToString();
  203. fldvec += "_";
  204. fldvec += xrayId.ToString();
  205. if (mapXrayInfo.ContainsKey(fldvec))
  206. {
  207. var xray = mapXrayInfo[fldvec];
  208. p.SetXray(xray);
  209. }
  210. }
  211. }
  212. var xrayDataDBDB = incAFileMgr.GetPosXrayDBMgr().GetXrayDataDB();
  213. Dictionary<string, CPosXrayClr> mapXrayData = new Dictionary<string, CPosXrayClr>();// map structure:[(fieldId,xrayId),CPosXrayPtr],every element of this map represent one unique xraydata.
  214. xrayDataDBDB.GetAllMapedXrayData(ref mapXrayData);
  215. foreach (var f in allFldsWithdata)
  216. {
  217. foreach (var p in f.GetListAnalysisParticles())
  218. {
  219. int curFldId = f.GetId();
  220. int xrayId = p.GetAnalysisId();
  221. string fldvec = "";
  222. fldvec += curFldId.ToString();
  223. fldvec += "_";
  224. fldvec += xrayId.ToString();
  225. if (mapXrayData.ContainsKey(fldvec))
  226. {
  227. var xray = mapXrayData[fldvec];
  228. CPosXrayClr t2 = p.GetXray();
  229. if (t2 == null)
  230. {
  231. t2 = new CPosXrayClr();
  232. p.SetXray(t2);
  233. }
  234. t2.SetXrayData(xray.GetXrayData());
  235. }
  236. }
  237. }
  238. CSegmentDB SegmentDB = incAFileMgr.GetSegmentDB();
  239. double msrFldsArea = 0;
  240. Dictionary<int, List<COTSParticleClr>> mapTypeParticles = new Dictionary<int, List<COTSParticleClr>>();//record typeId relevants particlelist;
  241. List<COTSSegmentClr> AllSegments;
  242. Dictionary<string, List<COTSSegmentClr>> mapSegments = new Dictionary<string, List<COTSSegmentClr>>();
  243. if (SegmentDB.GetAllSegmentsRecord(mapSegments))
  244. {
  245. foreach (var fld in allFldsWithdata)
  246. {
  247. int fldId = fld.GetId();
  248. List<COTSParticleClr> parts = fld.GetListAnalysisParticles();
  249. foreach (var part in parts)
  250. {
  251. string fldvec = "";
  252. fldvec += fldId.ToString();
  253. fldvec += "_";
  254. fldvec += part.GetParticleId().ToString();
  255. if (mapSegments.ContainsKey(fldvec))
  256. {
  257. COTSFeatureClr f = new COTSFeatureClr();
  258. AllSegments = mapSegments[fldvec];
  259. f.SetSegmentsList(AllSegments, true);
  260. part.SetFeature(f);
  261. if (mapTypeParticles.ContainsKey(part.GetType()))
  262. {
  263. mapTypeParticles[part.GetType()].Add(part);
  264. }
  265. else
  266. {
  267. List<COTSParticleClr> ps = new List<COTSParticleClr>();
  268. ps.Add(part);
  269. mapTypeParticles.Add(part.GetType(), ps);
  270. }
  271. }
  272. }
  273. msrFldsArea += aFieldArea;
  274. }
  275. }
  276. var fielddb = incAFileMgr.GetFieldDB();
  277. var fielddata = fielddb.GetTableQueryForDataTable();
  278. foreach (DataRow r in fielddata.Rows)
  279. {
  280. var p = new Point(Convert.ToInt32(r[(int)CFieldTable.ColumnID.N_FIELDPOS_X]), Convert.ToInt32(r[(int)CFieldTable.ColumnID.N_FIELDPOS_Y]));
  281. completedFldPositions.Add(p);
  282. }
  283. status.SetCompletedFieldsCenter(completedFldPositions);
  284. //add the empty field(there's no any particle in the bse image,so the field is empty)
  285. foreach (var p in completedFldPositions)
  286. {
  287. bool haveData = false;
  288. var emptyFld = new COTSField(p, pixelSize);
  289. foreach (var fld in allFldsWithdata)
  290. {
  291. if (fld.PositionEquals(emptyFld))
  292. {
  293. haveData = true;
  294. allFlds.Add(fld);
  295. break;
  296. }
  297. }
  298. if (!haveData)
  299. {
  300. allFlds.Add(emptyFld);
  301. }
  302. }
  303. // get MsrStatus info from DB.
  304. var m_GenInfoDB = incAFileMgr.GetGeneralInfoDB();//DBStoreFile类
  305. String strTimeStart = "";
  306. String strTimeEnd = "";
  307. String strRstStatus = "";
  308. m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameTimeStart(), ref strTimeStart);
  309. m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameTimeEnd(), ref strTimeEnd);
  310. m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameResultStatus(), ref strRstStatus);
  311. DateTime timeStart, timeEnd;
  312. try
  313. {
  314. timeStart = Convert.ToDateTime(ConvertFormatOfDateString(strTimeStart)); //此处为兼容之前得测量得结果可以打开,故在此进行数据格式变换
  315. timeEnd = Convert.ToDateTime(ConvertFormatOfDateString(strTimeEnd));
  316. }
  317. catch
  318. {
  319. timeStart = Convert.ToDateTime(strTimeStart); //此处为兼容之前得测量得结果可以打开,故在此进行数据格式变换
  320. timeEnd = Convert.ToDateTime(strTimeEnd);
  321. }
  322. status.SetStartTime(timeStart);
  323. status.SetEndTime(timeEnd);
  324. status.SetUsedTime(timeEnd - timeStart);
  325. status.SetStatus((OTS_MSR_SAMPLE_STATUS)Convert.ToInt32(strRstStatus));
  326. //get MsrResults data from map.
  327. List<CMsrResultItem> rstItms = new List<CMsrResultItem>();
  328. double allPartArea = 0;
  329. int typeNum;
  330. foreach (var typeParticles in mapTypeParticles)
  331. {
  332. typeNum = 0;
  333. double typeArea = 0;
  334. CMsrResultItem rstItm = new CMsrResultItem();
  335. foreach (var p in typeParticles.Value)
  336. {
  337. typeNum += 1;
  338. typeArea += p.GetActualArea();
  339. }
  340. rstItm.SetTypeId(typeParticles.Key);
  341. rstItm.SetNumber((uint)typeNum);
  342. rstItm.SetArea(Convert.ToDouble(typeArea.ToString("0.00")));
  343. rstItm.SetName(typeParticles.Value[0].GetTypeName());
  344. rstItms.Add(rstItm);
  345. allPartArea += typeArea;
  346. }
  347. rst.SetResultItems(rstItms);
  348. rst.SetMeasuredArea(Convert.ToUInt64(msrFldsArea * 1000000));
  349. rst.SetRatio(allPartArea / (msrFldsArea * 1000000));
  350. return true;
  351. //return false;
  352. }
  353. string ConvertFormatOfDateString(string Time)
  354. {
  355. string[] str1 = Time.Split(' ');
  356. string[] str2 = str1[0].Split('/');
  357. return str2[2] + "/" + str2[1] + "/" + str2[0] + " " + str1[1];
  358. }
  359. public string GetSampleWorkingFolder(string sampleName)
  360. {
  361. var pathname = FileHelper.GetFolderName(GetPathName());
  362. var samplePathName = pathname + "\\" + sampleName + "\\";
  363. return samplePathName;
  364. }
  365. public bool Save()
  366. {
  367. // Save or Save As, if strPathName is not empty, it is exist in the computer, this is a save action
  368. String strPathName = GetPathName();
  369. if (strPathName.CompareTo(UNTITLED_FILE_NAME) == 0)
  370. {
  371. // this is a new file
  372. // return save as result
  373. return SaveAs();
  374. }
  375. // is this a new file?
  376. strPathName.Trim();
  377. //保存测量项目文件 .prj
  378. XmlDocument doc = new XmlDocument();
  379. doc.Load(strPathName);
  380. doc.RemoveAll();
  381. //添加xml文件头申明
  382. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  383. doc.AppendChild(xmldecl);
  384. XmlElement rootNode = doc.CreateElement("XMLData");
  385. doc.AppendChild(rootNode);
  386. Serialize(true, doc, rootNode);
  387. try
  388. {
  389. doc.Save(strPathName);
  390. }
  391. catch
  392. {
  393. return false;
  394. }
  395. // set file modify flag
  396. SetModify(false);
  397. // Ok, return TRUE
  398. return true;
  399. }
  400. public bool SaveAs()
  401. {
  402. // get file name
  403. String strPathName = "";
  404. // file open dialog
  405. SaveFileDialog saveFileDialog = new SaveFileDialog();
  406. saveFileDialog.FileName = m_strPathName.Split('\\')[m_strPathName.Split('\\').Length - 1];
  407. saveFileDialog.Filter = "Directory (*.*)|*.*";
  408. saveFileDialog.AddExtension = false;
  409. if (saveFileDialog.ShowDialog() != DialogResult.OK)
  410. {
  411. return false;
  412. }
  413. // get file pathname
  414. strPathName = saveFileDialog.FileName;
  415. string DirectoryName = Path.GetDirectoryName(strPathName);
  416. try
  417. {
  418. Directory.CreateDirectory(strPathName);
  419. }
  420. catch
  421. {
  422. MessageBox.Show("CreateDirectoryFailed!");
  423. return false;
  424. }
  425. strPathName += "\\" + Path.GetFileNameWithoutExtension(strPathName) + ".prj";
  426. //保存测量项目文件 .prj
  427. XmlDocument doc = new XmlDocument();
  428. //添加xml文件头申明
  429. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  430. doc.AppendChild(xmldecl);
  431. XmlElement rootNode = doc.CreateElement("XMLData");
  432. doc.AppendChild(rootNode);
  433. Serialize(true, doc, rootNode);
  434. try
  435. {
  436. doc.Save(strPathName);
  437. }
  438. catch
  439. {
  440. return false;
  441. }
  442. // set file modify flag
  443. m_bModify = false;
  444. // Set project Path
  445. SetPathName(strPathName);
  446. // Ok, return TRUE
  447. return true;
  448. }
  449. // SEM stage data
  450. public CSEMStageData GetSEMStageData() { return m_pSEMStageData; }
  451. // SEM stage data
  452. public void SetSEMStageData(CSEMStageData a_pCSEMStageData)
  453. {
  454. m_pSEMStageData = a_pCSEMStageData;
  455. }
  456. // stage
  457. public CSampleHolder GetStageHolder() { return m_pStage; }
  458. public void SetStage(CSampleHolder a_pStage)
  459. {
  460. m_pStage = new CSampleHolder(a_pStage);
  461. }
  462. // samples list
  463. public List<COTSSample> GetSampleList() { return m_listSamples; }
  464. public COTSSample GetSampleByIndex(int a_nIndex)
  465. {
  466. // safe check
  467. if (a_nIndex < 0 || a_nIndex >= (int)m_listSamples.Count)
  468. {
  469. // invalid sample id
  470. return null;
  471. }
  472. // get the matching sample
  473. COTSSample pSample = m_listSamples[a_nIndex];
  474. // return the sample
  475. return pSample;
  476. }
  477. public COTSSample GetSampleByName(String a_strSampleName)
  478. {
  479. bool Isfind = false;
  480. COTSSample pOTSSample = null;
  481. for (int itr = 0; itr < m_listSamples.Count; itr++)
  482. {
  483. if (m_listSamples[itr].GetName() == a_strSampleName)
  484. {
  485. pOTSSample = m_listSamples[itr];
  486. Isfind = true;
  487. }
  488. }
  489. if (Isfind)
  490. {
  491. return pOTSSample;
  492. }
  493. return m_listSamples[0];
  494. }
  495. public void AddSample(COTSSample pSample)
  496. {
  497. // add the new sample into the samples list
  498. m_listSamples.Add(pSample);
  499. // set the project file modified.
  500. SetModify();
  501. // set the new as working sample
  502. m_nWorkingSampeIndex = (int)m_listSamples.Count - 1;
  503. }
  504. public bool DeleteSampleByIndex(int a_nIndex)
  505. {
  506. // check the index
  507. if (a_nIndex < 0 || a_nIndex >= (int)m_listSamples.Count)
  508. {
  509. // invalid input sample index
  510. return true;
  511. }
  512. // calculate new working sample index
  513. int nNewWorkingSampeIndex;
  514. if (a_nIndex < m_nWorkingSampeIndex)
  515. {
  516. nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1;
  517. }
  518. else if (a_nIndex > m_nWorkingSampeIndex)
  519. {
  520. nNewWorkingSampeIndex = m_nWorkingSampeIndex;
  521. }
  522. else
  523. {
  524. // deleting working sample.
  525. if (a_nIndex == (int)m_listSamples.Count - 1)
  526. {
  527. // this is the last sample, select the above sample as working sample
  528. // if this is only sample in the list working sample index will be -1;
  529. nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1;
  530. }
  531. else
  532. {
  533. // select next sample as working sample
  534. nNewWorkingSampeIndex = m_nWorkingSampeIndex;
  535. }
  536. }
  537. // delete the sample
  538. for (int i = m_listSamples.Count - 1; i >= 0; i--)
  539. {
  540. if (i == a_nIndex)
  541. {
  542. var sam = m_listSamples[i];
  543. sam.GetMsrStatus().ClearCompletedFieldsInfo();
  544. sam.GetMsrStatus().ClearCompletedFieldsInfo();
  545. sam.ClearFields();
  546. sam.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.UNMEASURED);
  547. m_listSamples.Remove(m_listSamples[i]);
  548. break;
  549. }
  550. }
  551. // the file is modified.
  552. SetModify();
  553. // reset working sample index
  554. m_nWorkingSampeIndex = nNewWorkingSampeIndex;
  555. // ok, return TRUE
  556. return true;
  557. }
  558. public bool DeleteSampleByName(String a_strSampleName)
  559. {
  560. // check input sample name
  561. a_strSampleName.Trim();
  562. if (a_strSampleName == "")
  563. {
  564. // input sample name is empty
  565. return false;
  566. }
  567. // go through sample list
  568. int nIndex = 0;
  569. foreach (var pSample in m_listSamples)
  570. {
  571. // return TRUE if this is not an exclude sample and its name is same with input
  572. String strSampleName = pSample.GetName();
  573. if (strSampleName.CompareTo(a_strSampleName) == 0)
  574. {
  575. // find the sample, return deleting result
  576. return DeleteSampleByIndex(nIndex);
  577. }
  578. ++nIndex;
  579. }
  580. // can't find the sample in the samples list, return FALSE
  581. return false;
  582. }
  583. public COTSSample GetWorkingSample()
  584. {
  585. return GetSampleByIndex(m_nWorkingSampeIndex);
  586. }
  587. public bool SetWorkingSampleByIndex(int a_nIndex)
  588. {
  589. // special treatment
  590. if (a_nIndex == -1 && m_listSamples.Count == 0)
  591. {
  592. m_nWorkingSampeIndex = -1;
  593. return true;
  594. }
  595. // check if the working sample index
  596. if (0 > a_nIndex || a_nIndex >= (int)m_listSamples.Count)
  597. {
  598. // invalid sample index
  599. return false;
  600. }
  601. m_nWorkingSampeIndex = a_nIndex;
  602. return true;
  603. }
  604. public bool SetWorkingSampleByName(String a_pSampleName)
  605. {
  606. // check input sample name
  607. a_pSampleName.Trim();
  608. if (a_pSampleName == "")
  609. {
  610. // input sample name is empty
  611. return false;
  612. }
  613. // go through sample list
  614. int nIndex = 0;
  615. foreach (var pSample in m_listSamples)
  616. {
  617. // return TRUE if this is not an exclude sample and its name is same with input
  618. String strSampleName = pSample.GetName();
  619. if (strSampleName.CompareTo(a_pSampleName) == 0)
  620. {
  621. // find the sample, return deleting result
  622. return SetWorkingSampleByIndex(nIndex);
  623. }
  624. ++nIndex;
  625. }
  626. // failed to find the named sample return FALSE
  627. return false;
  628. }
  629. // get modify flag
  630. public bool IsModified() { return m_bModify; }
  631. // file name
  632. public String GetFileName()
  633. {
  634. // make a copy of file path name
  635. String strPathName = m_strPathName;
  636. // is this a new file?
  637. if (strPathName.CompareTo(UNTITLED_FILE_NAME) == 0)
  638. {
  639. return strPathName;
  640. }
  641. // get file name
  642. String strFileName = GetFileNameWithoutExtension(strPathName);
  643. // return file name
  644. return strFileName;
  645. }
  646. // get file name without extension
  647. public String GetFileNameWithoutExtension(String a_strPathName)
  648. {
  649. string str = "";
  650. str = Path.GetFileNameWithoutExtension(a_strPathName);
  651. return str;
  652. }
  653. // if the new sample name can be used.
  654. public bool IsValidSampleName(String a_sName)
  655. {
  656. foreach (var pSample in m_listSamples)
  657. {
  658. String sSampleName = pSample.GetName();
  659. if (a_sName.Equals(sSampleName))
  660. {
  661. return false;
  662. }
  663. }
  664. return true;
  665. }
  666. static String FindFile(String filename, String path)
  667. {
  668. if (Directory.Exists(path))
  669. {
  670. if (File.Exists(path + filename))
  671. return path + filename;
  672. String[] directorys = Directory.GetDirectories(path);
  673. foreach (String d in directorys)
  674. {
  675. String p = FindFile(filename, d);
  676. if (p != null)
  677. return p;
  678. }
  679. }
  680. return null;
  681. }
  682. // calculate measurement area
  683. public CDomain CalculateMsrArea(CHole a_pHole)
  684. {
  685. CDomain pMsrArea = new CDomain(a_pHole.GetShape(), a_pHole.GetDomainRect());
  686. // measurement area should smaller than the sample hole
  687. RectangleF rectMsrArea = pMsrArea.GetDomainRect();
  688. float nWidth = rectMsrArea.Width;
  689. float nHeight = rectMsrArea.Height;
  690. float nDeflateX = CalculateDeflateValue(nWidth);
  691. float nDeflateY = CalculateDeflateValue(nHeight);
  692. rectMsrArea.Offset(nDeflateX, nDeflateY);
  693. pMsrArea.SetDomainRect(rectMsrArea);
  694. // return measurement area
  695. return pMsrArea;
  696. }
  697. // calculate deflate value
  698. public float CalculateDeflateValue(float a_nWitchOrHeight)
  699. {
  700. const long LENGTH_THRESHOLD = 150000;
  701. const long LENGTH_THRESHOLD_MIN = 10000;
  702. const int EDGE = 1000;
  703. const int EDGE_MIN = 500;
  704. // deflate 1000 if width or height is greater than 15000
  705. if (a_nWitchOrHeight >= LENGTH_THRESHOLD)
  706. {
  707. return EDGE;
  708. }
  709. // deflate 500 if width or height is greater than 15000
  710. else if (a_nWitchOrHeight >= LENGTH_THRESHOLD_MIN)
  711. {
  712. return EDGE_MIN;
  713. }
  714. // otherwise, no deflate
  715. return 0;
  716. }
  717. //合并后新添加的需要的方法
  718. public void SetGenParam(COTSCommonParam GenParam) { m_CommonParam = GenParam; }
  719. public COTSCommonParam GetGenParam() { return m_CommonParam; }
  720. public bool Reclassify(bool IgnoreUserModify)
  721. {
  722. string strFilePath = FileHelper.GetFolderName(m_strPathName);
  723. if (strFilePath == null)
  724. {
  725. return false;
  726. }
  727. foreach (var spl in m_listSamples)
  728. {
  729. //get the parameter MsrParam object
  730. CSampleParam pMsrParam = spl.GetMsrParams();
  731. string stdFileName = pMsrParam.GetSTDName();
  732. if (!stdFileName.Contains(".db"))
  733. {
  734. stdFileName += ".db";
  735. }
  736. string filename = strFilePath + "\\" + spl.GetName() + "\\" + spl.GetName() + ".rst";
  737. XmlDocument doc = new XmlDocument();
  738. doc.Load(filename);
  739. XmlNode root = doc.SelectSingleNode("XMLData");
  740. var members = root.SelectNodes("Member");
  741. foreach (XmlNode member1 in members)
  742. {
  743. if (member1.Attributes["RegName"].Value == "Sample")
  744. {
  745. var paramNode = member1.SelectNodes("Member");
  746. foreach (XmlNode member2 in paramNode)
  747. {
  748. if (member2.Attributes["RegName"].Value == "MsrParams")
  749. {
  750. ((XmlElement)member2).SetAttribute("STDName", stdFileName);
  751. }
  752. }
  753. }
  754. }
  755. doc.Save(filename);//save the new std db name
  756. foreach (var fld in spl.GetFieldsData())
  757. {
  758. if (IgnoreUserModify)
  759. {
  760. foreach (var part in fld.GetListAnalysisParticles())
  761. {
  762. if (part.GetClassifyId() < USR_MODIFY_CLASSIFYID_BASE)
  763. {
  764. part.SetType((int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED);
  765. part.SetClassifyId((int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED);
  766. part.SetTypeName("Not Identified");
  767. part.SetTypeColor("#000000");
  768. }
  769. }
  770. }
  771. else
  772. {
  773. foreach (var part in fld.GetListAnalysisParticles())
  774. {
  775. part.SetType((int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED);
  776. part.SetClassifyId((int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED);
  777. part.SetTypeName("Not Identified");
  778. part.SetTypeColor("#000000");
  779. }
  780. }
  781. }
  782. var analysisparts = new List<COTSParticleClr>();
  783. var m_classifyEngine = new CClassifyEngine();
  784. if (SystemTypeId == OTS_SysType_ID.IncA)
  785. {
  786. //get the steel technology parameter
  787. STEEL_TECHNOLOGY steelTech = pMsrParam.GetSteelTechnology();
  788. if (spl.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.InclutionPlusExpressionParse)
  789. {
  790. if (stdFileName != "NoSTDDB.db")
  791. {
  792. foreach (var fld in spl.GetFieldsData())
  793. {
  794. foreach (var part in fld.GetListAnalysisParticles())
  795. {
  796. IClassifyEngine partEngine = m_classifyEngine.GetExpressionClassifyEngine(stdFileName);
  797. if (IsLowCounts(part))
  798. {
  799. continue;
  800. }
  801. if (!partEngine.ClassifyByExpression(part))
  802. {
  803. NLog.LogManager.GetCurrentClassLogger().Error("1 failed to classify!" + "Particle Engine");
  804. }
  805. if (part.GetType() == (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)
  806. {
  807. IClassifyEngine incAEngine = m_classifyEngine.GetIncClassifyEngine();
  808. if (!incAEngine.ClassifyIncA(part, (int)steelTech))
  809. {
  810. NLog.LogManager.GetCurrentClassLogger().Error("2 failed to classify!" + "IncA Engine");
  811. }
  812. }
  813. }
  814. analysisparts.AddRange(fld.GetListAnalysisParticles());
  815. }
  816. }
  817. }
  818. else if (spl.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.ExpressionParse)
  819. {
  820. if (stdFileName != "NoSTDDB.db")
  821. {
  822. foreach (var fld in spl.GetFieldsData())
  823. {
  824. foreach (var part in fld.GetListAnalysisParticles())
  825. {
  826. if (IsLowCounts(part))
  827. {
  828. continue;
  829. }
  830. IClassifyEngine engine = m_classifyEngine.GetExpressionClassifyEngine(stdFileName);
  831. if (!engine.ClassifyByExpression(part))
  832. {
  833. NLog.LogManager.GetCurrentClassLogger().Error("3 failed to classify!" + "Particle Engine");
  834. }
  835. }
  836. analysisparts.AddRange(fld.GetListAnalysisParticles());
  837. }
  838. }
  839. }
  840. else if (spl.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.InclustionEngine)
  841. {
  842. foreach (var fld in spl.GetFieldsData())
  843. {
  844. foreach (var part in fld.GetListAnalysisParticles())
  845. {
  846. if (IsLowCounts(part))
  847. {
  848. continue;
  849. }
  850. IClassifyEngine incAEngine = m_classifyEngine.GetIncClassifyEngine();
  851. if (!incAEngine.ClassifyIncA(part, (int)steelTech))
  852. {
  853. NLog.LogManager.GetCurrentClassLogger().Error("2 failed to classify!" + "IncA Engine");
  854. }
  855. }
  856. analysisparts.AddRange(fld.GetListAnalysisParticles());
  857. }
  858. }
  859. else if (spl.GetMsrParams().GetEngineType() == OTS_CLASSIFY_ENGINE_TYPE.SpectrumMatch)
  860. {
  861. foreach (var fld in spl.GetFieldsData())
  862. {
  863. foreach (var part in fld.GetListAnalysisParticles())
  864. {
  865. if (IsLowCounts(part))
  866. {
  867. continue;
  868. }
  869. IClassifyEngine spectrumEng = m_classifyEngine.GetSpectrumCompareEngine(stdFileName);
  870. if (!spectrumEng.ClassifyBySpectrum(part))
  871. {
  872. NLog.LogManager.GetCurrentClassLogger().Error("2 failed to classify!" + "IncA Engine");
  873. }
  874. }
  875. analysisparts.AddRange(fld.GetListAnalysisParticles());
  876. }
  877. }
  878. }
  879. else if (SystemTypeId == OTS_SysType_ID.TCCleannessA|| SystemTypeId == OTS_SysType_ID.BatteryCleannessA)
  880. {
  881. if (stdFileName != "NoSTDDB.db")
  882. {
  883. foreach (var fld in spl.GetFieldsData())
  884. {
  885. foreach (var part in fld.GetListAnalysisParticles())
  886. {
  887. if (IsLowCounts(part))
  888. {
  889. continue;
  890. }
  891. IClassifyEngine engine = m_classifyEngine.GetExpressionClassifyEngine(stdFileName);
  892. if (!engine.ClassifyByExpression(part))
  893. {
  894. NLog.LogManager.GetCurrentClassLogger().Error("3 failed to classify!" + "Particle Engine");
  895. }
  896. }
  897. analysisparts.AddRange(fld.GetListAnalysisParticles());
  898. }
  899. }
  900. }
  901. string strFieldFileSubFolder = strFilePath + "\\" + spl.GetName() + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER;
  902. string strIncAFilename = strFieldFileSubFolder + "\\" + SMPL_MSR_RESULT_INCLUSION_FILE;
  903. CIncAFileMgr pIncAFileMgr = new CIncAFileMgr(strIncAFilename);
  904. pIncAFileMgr.UpdateParticleList(analysisparts);
  905. if (stdFileName.ToLower() != "nostddb.db")
  906. {
  907. try
  908. {
  909. string userdBpath = Application.StartupPath + "\\Config\\SysData\\" + stdFileName;
  910. File.Copy(userdBpath, strFilePath + "\\" + spl.GetName() + "\\" + stdFileName, true);
  911. }
  912. catch (Exception ex)
  913. {
  914. MessageBox.Show(stdFileName + " copy failed:" + ex.ToString());
  915. return false;
  916. }
  917. }
  918. }
  919. return true;
  920. }
  921. private bool IsLowCounts(COTSParticleClr particle)
  922. {
  923. COTSXRayParam pXRayParam = this.GetWorkingSample().GetMsrParams().GetXRayParam();
  924. if (pXRayParam.GetUsingXray() == true)
  925. {
  926. var thecount = particle.GetXray().GetTotalCount();
  927. var expect = pXRayParam.GetAnalyExpCount();
  928. if (thecount < expect)
  929. {
  930. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);//4 represent "lowcount"
  931. particle.SetClassifyId((int)OTS_PARTICLE_TYPE.LOW_COUNT);//4 represent "lowcount"
  932. particle.SetTypeColor("#000000");
  933. particle.SetTypeName("LowCounts");
  934. return true;
  935. }
  936. }
  937. return false;
  938. }
  939. public void DeleteWorkingSampleFieldDataByFieldNo(int fieldNo)
  940. {
  941. string strFilePath = FileHelper.GetFolderName(m_strPathName);
  942. string strFieldFileSubFolder = strFilePath + "\\" + GetWorkingSample().GetName() + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER;
  943. string strIncAFilename = strFieldFileSubFolder + "\\" + SMPL_MSR_RESULT_INCLUSION_FILE;
  944. CIncAFileMgr pIncAFileMgr = new CIncAFileMgr(strIncAFilename);
  945. var fldDB = pIncAFileMgr.GetFieldDB();
  946. var incADataDb = pIncAFileMgr.GetIncADB();
  947. var segmentDb = pIncAFileMgr.GetSegmentDB();
  948. var posxrayinfodb = pIncAFileMgr.GetPosXrayDBMgr().GetXrayInfoDB();
  949. var posxraydatadb = pIncAFileMgr.GetPosXrayDBMgr().GetXrayDataDB();
  950. var elechemistrydb = pIncAFileMgr.GetPosXrayDBMgr().GetElementChemistryDB();
  951. fldDB.DeleteDataByFieldNo (fieldNo);
  952. incADataDb.DeleteDataByFieldNo(fieldNo);
  953. segmentDb.DeleteDataByFieldNo(fieldNo);
  954. posxrayinfodb.DeleteDataByFieldNo(fieldNo);
  955. posxraydatadb.DeleteDataByFieldNo(fieldNo);
  956. elechemistrydb.DeleteDataByFieldNo(fieldNo);
  957. }
  958. /// <summary>
  959. /// 设置样品中对象属性 赋值
  960. /// </summary>
  961. /// <param name="pSample"></param>
  962. /// <param name="poMsrParams"></param>
  963. /// <returns></returns>
  964. void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  965. {
  966. // copy data over
  967. if (m_pSEMStageData == null)
  968. {
  969. m_pSEMStageData = new CSEMStageData();
  970. }
  971. if (m_pStage == null)
  972. {
  973. m_pStage = new CSampleHolder();
  974. }
  975. xInt xProjMgrFileMark = new xInt();
  976. xString xProjMgrFileVersion = new xString();
  977. xString xstrPathName = new xString();
  978. xString xSystype = new xString();
  979. //Collection<CHoleBSEImg> xholeBSEImgs = new Collection<CHoleBSEImg>();
  980. Collection<COTSSample> xsamples = new Collection<COTSSample>();
  981. Slo slo = new Slo();
  982. slo.Register("ProjMgrFileMark", xProjMgrFileMark);
  983. slo.Register("ProjMgrFileVersion", xProjMgrFileVersion);
  984. slo.Register("strPathName", xstrPathName);
  985. slo.Register("SysType", xSystype);
  986. slo.Register("SEMStageData", m_pSEMStageData);
  987. slo.Register("Stage", m_pStage);
  988. //slo.Register("HoleBSEImg", xholeBSEImgs);
  989. slo.Register("Samples", xsamples);
  990. if (isStoring)
  991. {
  992. xProjMgrFileMark.AssignValue(0);
  993. xProjMgrFileVersion.AssignValue("");
  994. xSystype.AssignValue(systemTypeId.ToString());
  995. xstrPathName.AssignValue(m_strPathName);
  996. //xholeBSEImgs.Clear();
  997. xsamples.Clear();
  998. foreach (var sample in m_listSamples)
  999. {
  1000. xsamples.addItem(sample);
  1001. }
  1002. slo.Serialize(true, classDoc, rootNode);
  1003. }
  1004. else
  1005. {
  1006. slo.Serialize(false, classDoc, rootNode);
  1007. m_strPathName = xstrPathName.value();
  1008. //m_listHoleBSEImg.Clear();
  1009. m_listSamples.Clear();
  1010. for (int i = 0; i < (int)xsamples.size(); i++)
  1011. {
  1012. m_listSamples.Add(xsamples.getItem(i));
  1013. }
  1014. if (xSystype.value() == "IncA")
  1015. {
  1016. systemTypeId = otsdataconst.OTS_SysType_ID.IncA;
  1017. }
  1018. else if(xSystype.value() == "TCCleannessA")
  1019. {
  1020. systemTypeId = otsdataconst.OTS_SysType_ID.TCCleannessA;
  1021. }
  1022. else if (xSystype.value() == "BatteryCleannessA")
  1023. {
  1024. systemTypeId = otsdataconst.OTS_SysType_ID.BatteryCleannessA;
  1025. }
  1026. }
  1027. }
  1028. }
  1029. }