COTSMsrPrjResultData.cs 45 KB

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