COTSMsrPrjResultData.cs 44 KB

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