COTSMsrPrjResultData.cs 45 KB

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