COTSProjMgrFile.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  1. using OTSDataType;
  2. using OTSIncAMeasureApp.OTSMeasureControl;
  3. using OTSModelSharp.DTL;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Forms;
  13. using static OTSDataType.otsdataconst;
  14. namespace OTSModelSharp
  15. {
  16. public class COTSProjMgrFile
  17. {
  18. // SEM stage data
  19. CSEMStageData m_pSEMStageData;
  20. // stage
  21. CStage m_pStage;
  22. COTSGeneralParameters m_DefaultParameters;
  23. // sample list
  24. List<COTSSample> m_listSamples;
  25. List<CHoleBSEImg> m_listHoleBSEImg;
  26. bool m_bModify;
  27. // path name
  28. String m_strPathName;
  29. // working sample index
  30. int m_nWorkingSampeIndex;
  31. const String UNTITLED_FILE_NAME = "Untitled";
  32. //以下这两个原来在SmplMsrResultFile中
  33. // fields file sub-directory string
  34. const String SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER = "FIELD_FILES";
  35. // Inclusion file name
  36. const String SMPL_MSR_RESULT_INCLUSION_FILE = "Inclusion.db";//put all the table in one db file so that report can make join query.
  37. // CDBStoreBase m_datastorePtr;
  38. CSQLiteDBFile m_datastore;
  39. IMeasureHardware m_HardwareMgr;
  40. public OTS_MSR_SAMPLE_STATUS m_nStatus;
  41. CIncAFileMgr m_IncAFileMgr = new CIncAFileMgr("");
  42. CIncADataDB m_IncADataDB ;
  43. CSegmentDB m_Segment ;
  44. CSQLiteDB m_generalInfoTable;
  45. public COTSProjMgrFile()
  46. {
  47. Init();
  48. // m_HardwareMgr = new MeasureHardware();
  49. }
  50. // initialization
  51. public void Init()
  52. {
  53. m_pSEMStageData = new CSEMStageData();
  54. m_pStage = new CStage();
  55. m_DefaultParameters = new COTSGeneralParameters();
  56. m_listSamples.Clear();
  57. m_listHoleBSEImg.Clear();
  58. //m_listSysSTDItem.clear();
  59. m_bModify = false;
  60. m_strPathName = "";
  61. m_nWorkingSampeIndex = -1;
  62. }
  63. public void OTSProjMgrFile(COTSProjMgrFile a_oSource)
  64. {
  65. // can't copy itself
  66. if (a_oSource == this)
  67. {
  68. return;
  69. }
  70. // copy data over
  71. Duplicate(a_oSource);
  72. }
  73. // duplication
  74. public void Duplicate( COTSProjMgrFile a_oSource)
  75. {
  76. // initialization
  77. Init();
  78. // copy data over
  79. m_pSEMStageData = new CSEMStageData(a_oSource.m_pSEMStageData);
  80. m_pStage = new CStage(a_oSource.m_pStage);
  81. m_DefaultParameters = new COTSGeneralParameters(a_oSource.m_DefaultParameters);
  82. foreach (var pHoleBSEImg in a_oSource.m_listHoleBSEImg)
  83. {
  84. CHoleBSEImg pHoleBSEImgNew =(new CHoleBSEImg(pHoleBSEImg));
  85. m_listHoleBSEImg.Add(pHoleBSEImgNew);
  86. }
  87. foreach (var pSample in a_oSource.m_listSamples)
  88. {
  89. COTSSample pSampleNew =new COTSSample();
  90. m_listSamples.Add(pSampleNew);
  91. }
  92. m_bModify = a_oSource.m_bModify;
  93. m_strPathName = a_oSource.m_strPathName;
  94. m_nWorkingSampeIndex = a_oSource.m_nWorkingSampeIndex;
  95. }
  96. // file
  97. public bool NewFile()
  98. {
  99. // file invalidation
  100. if (!IsValid())
  101. {
  102. // shouldn't happen, file is invalid
  103. return false;
  104. }
  105. // set file name
  106. SetPathName(UNTITLED_FILE_NAME);
  107. // Ok, return TRUE
  108. return true;
  109. }
  110. public bool IsValid()
  111. {
  112. if (m_pSEMStageData!=null || m_pSEMStageData == null)
  113. {
  114. return false;
  115. }
  116. else if (m_pStage!=null || m_pStage == null)
  117. {
  118. return false;
  119. }
  120. else if (m_DefaultParameters!=null)
  121. {
  122. return false;
  123. }
  124. // Ok, return TRUE
  125. return true;
  126. }
  127. public void SetPathName(String a_strPathName) { m_strPathName = a_strPathName; }
  128. public bool GetNewProjMgrClr() { return false; }
  129. public bool Load()
  130. {
  131. // use dll resource
  132. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  133. // get file name
  134. String strPathName = "";
  135. // check file pathname
  136. strPathName.Trim();
  137. if (strPathName == "")
  138. {
  139. // file open dialog
  140. OpenFileDialog openFileDialog = new OpenFileDialog();
  141. if (openFileDialog.ShowDialog() != DialogResult.OK)
  142. {
  143. return false;
  144. }
  145. // get file pathname
  146. strPathName = openFileDialog.FileName;
  147. }
  148. //Init data
  149. Cleanup();
  150. // check if the file exist
  151. if (!Exists(strPathName))
  152. {
  153. // shouldn't happen, file does not exist
  154. return false;
  155. }
  156. //-------------
  157. foreach (var smpl in m_listSamples)
  158. {
  159. // get path of the pathname
  160. String strFilePath = GetFolderName(strPathName);
  161. if (strFilePath=="")
  162. {
  163. // file path string is an empty string
  164. return false;
  165. }
  166. // field file sub folder string
  167. String strFieldFileSubFolder = strFilePath + "\\" + smpl.GetName() + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER + "\\";
  168. // check if the field file sub folder exists
  169. if (!Exists(strFieldFileSubFolder))
  170. {// field files folder doesn't exist
  171. return false;
  172. }
  173. String strIncAFilename = strFieldFileSubFolder + "\\" + SMPL_MSR_RESULT_INCLUSION_FILE;
  174. CIncAFileMgr pIncAFileMgr = new CIncAFileMgr(strIncAFilename);
  175. List<COTSFieldData> allFlds = new List<COTSFieldData>();
  176. CMsrSampleStatus poMsrStatus = new CMsrSampleStatus();
  177. poMsrStatus.GetStatus();
  178. CMsrResults poMsrResults = smpl.GetMsrResults();
  179. double aFldArea = smpl.CalculateAFieldArea();
  180. if (GetAllFieldsFromDB(ref allFlds, poMsrStatus, poMsrResults, aFldArea))
  181. {
  182. smpl.SetFieldsData(allFlds);
  183. // file validation
  184. if (m_pSEMStageData!= new CSEMStageData() || (m_pSEMStageData) == new CSEMStageData())
  185. {
  186. // invalid SME stage data
  187. return false;
  188. }
  189. if (m_pStage!= new CStage() || (m_pStage) == new CStage())
  190. {
  191. // invalid stage
  192. return false;
  193. }
  194. // set modify flag
  195. SetModify(false);
  196. // set pathname
  197. SetPathName(strPathName);
  198. // set working sample
  199. if (m_listSamples.Count > 0)
  200. {
  201. m_nWorkingSampeIndex = 0;
  202. }
  203. else
  204. {
  205. m_nWorkingSampeIndex = -1;
  206. }
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. //-----------------------------------------
  214. return true;
  215. }
  216. // gets the name of the folder
  217. public String GetFolderName(String a_strPathName)
  218. {
  219. string str = "";
  220. char[] sPath=new char[otsdataconst.MAX_PATH];
  221. str.CopyTo(0,sPath, 0, Convert.ToInt32(a_strPathName));
  222. return sPath.ToString();
  223. }
  224. // check if the file exists or not
  225. public bool Exists(string a_sPath)
  226. {
  227. return true;
  228. }
  229. // cleanup
  230. public void Cleanup()
  231. {
  232. m_listHoleBSEImg.Clear();
  233. m_listSamples.Clear();
  234. m_bModify = false;
  235. m_strPathName = "";
  236. m_nWorkingSampeIndex = -1;
  237. }
  238. // set modify flag
  239. public void SetModify(bool a_bModify = true) { m_bModify = a_bModify; }
  240. // file pathname
  241. public String GetPathName() { return m_strPathName; }
  242. public bool GetAllFieldsFromDB(ref List<COTSFieldData > allFlds, CMsrSampleStatus status, CMsrResults rst, double aFieldArea)
  243. {
  244. String sDatabaseName = GetPathName();
  245. if (Exists(sDatabaseName))
  246. {
  247. if (!Open(m_strPathName, false))
  248. {
  249. return false;
  250. }
  251. }
  252. List<System.Drawing.Point> completedflds = new List<System.Drawing.Point>();
  253. CIncAFileMgr IncADataDBs = m_IncAFileMgr.GetIncADB();
  254. m_IncADataDB.GetAllFieldsRecord(ref allFlds);
  255. CSegmentDB SegmentDB = m_IncAFileMgr.GetSegmentDB();
  256. double msrFldsArea = 0;
  257. List<COTSParticle> mapTypeParticles=new List<COTSParticle>();//record typeId relevants particlelist;
  258. // std::map<std::vector<int>, COTSSegmentsList> AllSegments;
  259. List<COTSSegment> AllSegments = new List<COTSSegment>();
  260. Dictionary<List<int>, List<COTSSegment>> mapSegments = new Dictionary<List<int>, List<COTSSegment>>();
  261. if (SegmentDB.GetAllSegmentsRecord(mapSegments))
  262. {
  263. int nCol;
  264. foreach(var fld in allFlds)
  265. {
  266. int fldId = fld.GetId();
  267. List<COTSParticle> parts = fld.GetParticleList();
  268. foreach(var part in parts)
  269. {
  270. List<int> fldvec = new List<int>();
  271. fldvec.Add(fldId);
  272. fldvec.Add(part.GetTagId());
  273. var itr = AllSegments.Find(
  274. delegate(COTSSegment segment)
  275. {
  276. return segment.Equals(sDatabaseName);
  277. });
  278. if (itr != AllSegments.Last())
  279. {
  280. COTSFeature f = new COTSFeature();
  281. f.SetSegmentsList(AllSegments, true);
  282. part.SetFeature(f);
  283. }
  284. mapTypeParticles[part.GetType()].GetDMax();
  285. }
  286. completedflds.Add(fld.GetPosition());
  287. msrFldsArea += aFieldArea;
  288. }
  289. }
  290. // get MsrStatus info from DB.
  291. var GenDB = GetGeneralInfoDB();//DBStoreFile类
  292. String strTimeStart = "";
  293. String strTimeEnd = "";
  294. String strRstStatus = "";
  295. GenDB.GetDatastore();
  296. GenDB.GetTableQuery(strTimeStart);
  297. GenDB.GetTableQuery(strTimeEnd);
  298. GenDB.GetTableQuery(strRstStatus);
  299. status.SetCompletedFieldsCenter(completedflds);
  300. status.SetCompletedFields(completedflds.Count);
  301. DateTime timeStart, timeEnd;
  302. TimeSpan timeSpan ;
  303. timeStart = GetDateTimeFromString(strTimeStart);
  304. timeEnd = GetDateTimeFromString(strTimeEnd);
  305. status.SetStartTime(timeStart);
  306. status.SetEndTime(timeEnd);
  307. status.SetUsedTime(timeEnd - timeStart);
  308. status.SetStatus(m_nStatus);
  309. //get MsrResults data from map.
  310. List<CMsrResultItem> rstItms = new List<CMsrResultItem>();
  311. double allPartArea = 0;
  312. foreach (var typeParticles in mapTypeParticles)
  313. {
  314. CMsrResultItem rstItm = new CMsrResultItem();
  315. int typeNum = 0;
  316. double typeArea = 0;
  317. foreach (var p in mapTypeParticles)
  318. {
  319. typeNum += 1;
  320. typeArea += p.GetArea();
  321. }
  322. rstItm.SetTypeId(typeParticles.GetType());
  323. rstItm.SetNumber((uint)typeNum);
  324. rstItm.SetArea((uint)typeArea);
  325. rstItms.Add(rstItm);
  326. allPartArea += typeArea;
  327. }
  328. rst.SetResultItems(rstItms);
  329. rst.SetMeasuredArea(Convert.ToUInt32(msrFldsArea * 1000000));
  330. rst.SetRadio(allPartArea / (msrFldsArea * 1000000));
  331. return true;
  332. }
  333. public CSQLiteDB GetGeneralInfoDB()
  334. {
  335. if (m_generalInfoTable!=null)
  336. {
  337. var datastorePtr = GetDatastore();
  338. if (datastorePtr!=null)
  339. {
  340. m_generalInfoTable.GetDatastore();
  341. }
  342. }
  343. return m_generalInfoTable;
  344. }
  345. //public CGenInfoDB GetGeneralInfoDB()
  346. // {
  347. // if (m_generalInfoTable!=null)
  348. // {
  349. // var datastorePtr = GetDatastore();
  350. // if (datastorePtr)
  351. // {
  352. // m_generalInfoTable.reset(new CGenInfoDB(datastorePtr));
  353. // }
  354. // }
  355. // return m_generalInfoTable;
  356. // }
  357. public DateTime GetDateTimeFromString( String a_DateTime)
  358. {
  359. DateTime ole_time = new DateTime();
  360. ole_time.ToLocalTime();
  361. return ole_time;
  362. }
  363. //此方法为OTSSQLiteDLl中的DBStoreFile类
  364. public bool Open(string a_sFileName, bool a_bForce /*= TRUE*/)
  365. {
  366. var datastorePtr = GetDatastore();
  367. return datastorePtr.Open(a_sFileName, a_bForce);
  368. }
  369. public CSQLiteDBFile GetDatastore()
  370. {
  371. if (m_datastore!=null)
  372. {
  373. m_datastore = CreateNewSQLiteStore();
  374. }
  375. return m_datastore;
  376. }
  377. public CSQLiteDBFile CreateNewSQLiteStore()
  378. {
  379. CSQLiteDBFile SQLiteDBFile = new CSQLiteDBFile();
  380. return SQLiteDBFile;
  381. }
  382. public bool Save()
  383. {
  384. // Save or Save As, if strPathName is not empty, it is exist in the computer, this is a save action
  385. String strPathName = GetPathName();
  386. // is this a new file?
  387. strPathName.Trim();
  388. if (strPathName.CompareTo(UNTITLED_FILE_NAME) == 0)
  389. {
  390. // this is a new file
  391. // return save as result
  392. return SaveAs();
  393. }
  394. // set file modify flag
  395. SetModify(false);
  396. // Ok, return TRUE
  397. return true;
  398. }
  399. public bool SaveAs()
  400. {
  401. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  402. // save file dialog
  403. String strFileName = "";
  404. if (m_strPathName!="")
  405. {
  406. strFileName = GetFileName(m_strPathName);
  407. }
  408. // get file name
  409. String strPathName = "";
  410. // check file pathname
  411. strPathName.Trim();
  412. if (strPathName == "")
  413. {
  414. // file open dialog
  415. OpenFileDialog openFileDialog = new OpenFileDialog();
  416. if (openFileDialog.ShowDialog() != DialogResult.OK)
  417. {
  418. return false;
  419. }
  420. // get file pathname
  421. strPathName = openFileDialog.FileName;
  422. }
  423. // set file modify flag
  424. m_bModify = false;
  425. // Set project Path
  426. SetPathName(strPathName);
  427. // Ok, return TRUE
  428. return true;
  429. }
  430. // Gets the name of the file.
  431. public String GetFileName(string a_strPathName)
  432. {
  433. return a_strPathName;
  434. }
  435. // SEM stage data
  436. public CSEMStageData GetSEMStageData() { return m_pSEMStageData; }
  437. // SEM stage data
  438. public void SetSEMStageData(CSEMStageData a_pCSEMStageData)
  439. {
  440. // safety check
  441. if (a_pCSEMStageData!=null)
  442. {
  443. // invalid stage data pointer
  444. return;
  445. }
  446. m_pSEMStageData = new CSEMStageData(a_pCSEMStageData);
  447. }
  448. // stage
  449. public CStage GetStage() { return m_pStage; }
  450. public void SetStage(CStage a_pStage)
  451. {
  452. // safety check
  453. if (a_pStage!=null)
  454. {
  455. // invalid input
  456. return;
  457. }
  458. m_pStage = new CStage(a_pStage);
  459. }
  460. // samples list
  461. public List< COTSSample> GetSampleList() { return m_listSamples; }
  462. // samples list
  463. public bool SetSampleList(List<COTSSample> a_listSample, bool a_bClear/* = TRUE*/)
  464. {
  465. // clear samples list if necessary
  466. if (a_bClear)
  467. {
  468. m_listSamples.Clear();
  469. }
  470. // go through the sample list of the source
  471. foreach (var pSample in a_listSample)
  472. {
  473. // add the new sample into sample list
  474. m_listSamples.Add(pSample);
  475. }
  476. return true;
  477. }
  478. public COTSSample GetSampleByIndex(int a_nIndex)
  479. {
  480. // safe check
  481. if (a_nIndex < 0 || a_nIndex >= (int)m_listSamples.Count)
  482. {
  483. // invalid sample id
  484. return null;
  485. }
  486. // get the matching sample
  487. COTSSample pSample = m_listSamples[a_nIndex];
  488. // return the sample
  489. return pSample;
  490. }
  491. public COTSSample GetSampleByName(String a_strSampleName)
  492. {
  493. bool IsChanged = false;
  494. for (int itr = 0; itr < m_listSamples.Count; itr++)
  495. {
  496. if (m_listSamples[itr].GetName() == a_strSampleName)
  497. {
  498. COTSSample pOTSSample = m_listSamples[itr];
  499. m_listSamples.RemoveAt(itr);
  500. IsChanged = true;
  501. }
  502. }
  503. if (!IsChanged)
  504. {
  505. return m_listSamples[0];
  506. }
  507. return m_listSamples[0];
  508. }
  509. public COTSSample AddSample(String a_strHoleName)
  510. {
  511. // this file is valid, pass the test will make sure all the parameters are ok.
  512. if (!IsValid())
  513. {
  514. // shouldn't be here, invalid file
  515. return null;
  516. }
  517. // get new sample name
  518. String strNewSampleName = GetNewSampleName();
  519. // make sure the new sample name is not an empty string
  520. strNewSampleName.Trim();
  521. // get a suitable sample hole for the new sample
  522. CHole pHole = SelectASmpleHole(a_strHoleName);
  523. // check the sample hole
  524. if (pHole!=null)
  525. {
  526. // failed to get sample hole for the new sample
  527. return null;
  528. }
  529. // measure data parameters containing particle analysis std, image scan parameter, image process parameter and x-ray parameter
  530. CMsrParams poMsrParams = new CMsrParams();
  531. MEMBRANE_TYPE a_nVal = (MEMBRANE_TYPE)m_DefaultParameters.GetMembraneType();
  532. poMsrParams.SetImageScanParam(m_DefaultParameters.GetMsrParam().GetImageScanParam());
  533. poMsrParams.SetImageProcessParam(m_DefaultParameters.GetMsrParam().GetImageProcessParam());
  534. poMsrParams.SetXRayParam(m_DefaultParameters.GetMsrParam().GetXRayParam());
  535. String sSTDName = m_DefaultParameters.GetPartSTDLibFileName();
  536. poMsrParams.SetSTDName(sSTDName);
  537. // measurement area
  538. //CDomainPtr pMsrArea = CalculateMsrArea(pHole);
  539. CDomain pMsrArea = CalculateDefaultArea(pHole);
  540. // create a sample
  541. COTSSample pSample = new COTSSample();
  542. // set sample parameters
  543. pSample.SetName(strNewSampleName);
  544. pSample.SetSampleHoleName(pHole.GetName());
  545. pSample.SetSwitch(m_DefaultParameters.GetMeasurementSwitch());
  546. pSample.SetMsrParams(poMsrParams);
  547. pSample.SetMsrArea(pMsrArea);
  548. pSample.SetMembraneType(a_nVal);
  549. // add the new sample into the samples list
  550. m_listSamples.Add(pSample);
  551. // set the project file modified.
  552. SetModify();
  553. // set the new as working sample
  554. m_nWorkingSampeIndex = (int)m_listSamples.Count - 1;
  555. // return the new sample
  556. return pSample;
  557. }
  558. // select a suitable sample hole for a new sample
  559. public CHole SelectASmpleHole(String a_strHoleName /*= _T("")*/)
  560. {
  561. // get holes list of the stage
  562. List< CHole > listHoles = m_pStage.GetHoleList();
  563. // make sure the holes list is not empty
  564. if (listHoles.Count == 0)
  565. {
  566. // shouldn't happen, stage have no hole.
  567. return null;
  568. }
  569. // check the input hole name
  570. a_strHoleName.Trim();
  571. if (a_strHoleName!="")
  572. {
  573. // try to find matched hole
  574. bool IsChanged = false;
  575. for (int itr = 0; itr < listHoles.Count; itr++)
  576. {
  577. if (listHoles[itr].GetName() == a_strHoleName)
  578. {
  579. CHole pElementChemistry = listHoles[itr];
  580. listHoles.RemoveAt(itr);
  581. IsChanged = true;
  582. }
  583. }
  584. if (!IsChanged)
  585. {
  586. return listHoles[0];
  587. }
  588. }
  589. // can't find a matched hole, then pick the first empty hole
  590. // go through the holes list
  591. foreach (var pHole in listHoles)
  592. {
  593. // has this hole any sample in it?
  594. String strHoleName = pHole.GetName();
  595. bool IsChanged = false;
  596. for (int itr = 0; itr < listHoles.Count; itr++)
  597. {
  598. if (listHoles[itr].GetName() == a_strHoleName)
  599. {
  600. CHole pElementChemistry = listHoles[itr];
  601. listHoles.RemoveAt(itr);
  602. IsChanged = true;
  603. }
  604. }
  605. if (!IsChanged)
  606. {
  607. return listHoles[0];
  608. }
  609. }
  610. // no empty hole, then the first hole will be the one
  611. return listHoles[0];
  612. }
  613. public CDomain CalculateDefaultArea(CHole a_pHole)
  614. {
  615. Rectangle rectHole = a_pHole.GetDomainRect();
  616. System.Drawing.Point ptCenter = rectHole.Location;
  617. DOMAIN_SHAPE nShape = m_DefaultParameters.GetShape();
  618. double dArea = m_DefaultParameters.GetArea() * 1000000;
  619. System.Drawing.Point ptLeftTop = new System.Drawing.Point();
  620. System.Drawing.Size ptRightBottom = new System.Drawing.Size();
  621. int nEdge = 0;
  622. int nRadius = 0;
  623. switch ((int)nShape)
  624. {
  625. case (int)DOMAIN_SHAPE.RECTANGLE:
  626. nEdge = (int)Math.Sqrt(dArea) / 2;
  627. ptLeftTop.X = ptCenter.X - nEdge;
  628. ptLeftTop.Y = ptCenter.Y - nEdge;
  629. ptRightBottom.Width = ptCenter.X + nEdge;
  630. ptRightBottom.Height = ptCenter.Y + nEdge;
  631. break;
  632. case (int)DOMAIN_SHAPE.ROUND:
  633. nRadius = (int)Math.Sqrt(dArea / 3.1415926);
  634. ptLeftTop.X = ptCenter.X - nRadius;
  635. ptLeftTop.Y = ptCenter.Y - nRadius;
  636. ptRightBottom.Width = ptCenter.X + nRadius;
  637. ptRightBottom.Height = ptCenter.Y + nRadius;
  638. break;
  639. }
  640. Rectangle MsrRect = new Rectangle(ptLeftTop, ptRightBottom);
  641. CDomain pMsrArea = new CDomain(nShape, MsrRect);
  642. // return measurement area
  643. return pMsrArea;
  644. }
  645. // get new sample name
  646. public String GetNewSampleName()
  647. {
  648. // new sample name
  649. String strNewSmplName = "";
  650. // safety check
  651. if (m_DefaultParameters!=null)
  652. {
  653. // shouldn't happen, invalid general parameter pointer.
  654. return strNewSmplName;
  655. }
  656. // new sample name base
  657. String strNewSmplNameBase = m_DefaultParameters.GetSampleName() + "%d";
  658. int nIndex = 1;
  659. do
  660. {
  661. // new sample name is new sample name base + number string
  662. string.Format(strNewSmplNameBase, nIndex);
  663. ++nIndex;
  664. }
  665. // make sure that the new sample name is not same with any sample in the samples list
  666. while (SameNameInList(strNewSmplName,0));
  667. // new sample name
  668. return strNewSmplName;
  669. }
  670. public bool SameNameInList(String a_strSampleName, int a_nExclude /*= -1*/)
  671. {
  672. // make sure the input sample name is not empty
  673. a_strSampleName.Trim();
  674. if (a_strSampleName=="")
  675. {
  676. // shouldn't happen, input name is an empty string
  677. return false;
  678. }
  679. // go through sample list
  680. int nIndex = 0;
  681. foreach (var pSample in m_listSamples)
  682. {
  683. // return TRUE if this is not an exclude sample and its name is same with input
  684. String strSampleName = pSample.GetName();
  685. if (nIndex != a_nExclude && strSampleName.CompareTo(a_strSampleName) == 0)
  686. {
  687. // find a same name sample
  688. return true;
  689. }
  690. ++nIndex;
  691. }
  692. // no, same name sample in the same list, return FALSE
  693. return false;
  694. }
  695. public bool DeleteSampleByIndex(int a_nIndex)
  696. {
  697. // check the index
  698. if (a_nIndex < 0 || a_nIndex >= (int)m_listSamples.Count)
  699. {
  700. // invalid input sample index
  701. return true;
  702. }
  703. // calculate new working sample index
  704. int nNewWorkingSampeIndex;
  705. if (a_nIndex < m_nWorkingSampeIndex)
  706. {
  707. nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1;
  708. }
  709. else if (a_nIndex > m_nWorkingSampeIndex)
  710. {
  711. nNewWorkingSampeIndex = m_nWorkingSampeIndex;
  712. }
  713. else
  714. {
  715. // deleting working sample.
  716. if (a_nIndex == (int)m_listSamples.Count - 1)
  717. {
  718. // this is the last sample, select the above sample as working sample
  719. // if this is only sample in the list working sample index will be -1;
  720. nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1;
  721. }
  722. else
  723. {
  724. // select next sample as working sample
  725. nNewWorkingSampeIndex = m_nWorkingSampeIndex;
  726. }
  727. }
  728. // delete the sample
  729. for (int i = 0; i < m_listSamples.Count; i++)
  730. {
  731. m_listSamples.Remove(m_listSamples[i]);
  732. }
  733. // the file is modified.
  734. SetModify();
  735. // reset working sample index
  736. m_nWorkingSampeIndex = nNewWorkingSampeIndex;
  737. // ok, return TRUE
  738. return true;
  739. }
  740. public bool DeleteSampleByName(String a_strSampleName)
  741. {
  742. // check input sample name
  743. a_strSampleName.Trim();
  744. if (a_strSampleName=="")
  745. {
  746. // input sample name is empty
  747. return false;
  748. }
  749. // go through sample list
  750. int nIndex = 0;
  751. foreach (var pSample in m_listSamples)
  752. {
  753. // return TRUE if this is not an exclude sample and its name is same with input
  754. String strSampleName = pSample.GetName();
  755. if (strSampleName.CompareTo(a_strSampleName) == 0)
  756. {
  757. // find the sample, return deleting result
  758. return DeleteSampleByIndex(nIndex);
  759. }
  760. ++nIndex;
  761. }
  762. // can't find the sample in the samples list, return FALSE
  763. return false;
  764. }
  765. public bool ResetSamplesListOrder(List<String> a_listSampleNames)
  766. {
  767. // no sample in the samples list; shouldn't be here
  768. if (m_listSamples.Count == 0)
  769. {
  770. return false;
  771. }
  772. // make sure that input sample names list size is same with the samples list
  773. if (m_listSamples.Count != a_listSampleNames.Count)
  774. {
  775. return false;
  776. }
  777. // new sample list
  778. List<COTSSample> listSamples = new List<COTSSample>();
  779. // go through the sample names list
  780. foreach (var strSampleName in a_listSampleNames)
  781. {
  782. // find the sample
  783. // var itr = find_if(m_listSamples.begin(), m_listSamples.end(), [strSampleName](COTSSamplePtr p) { return p->GetName().CompareNoCase(strSampleName) == 0; });
  784. //if (itr == m_listSamples.end())
  785. //{
  786. // // failed to find the sample
  787. // return false;
  788. //}
  789. //// add the sample into the new list
  790. //listSamples.Add(itr);
  791. bool IsChanged = false;
  792. for (int itr = 0; itr < m_listSamples.Count; itr++)
  793. {
  794. if (m_listSamples[itr].GetName() == strSampleName)
  795. {
  796. COTSSample pElementChemistry = m_listSamples[itr];
  797. m_listSamples.RemoveAt(itr);
  798. IsChanged = true;
  799. }
  800. }
  801. if (!IsChanged)
  802. {
  803. return false;
  804. }
  805. }
  806. // reset the samples list
  807. m_listSamples.Clear();
  808. foreach (var pSample in listSamples)
  809. {
  810. m_listSamples.Add(pSample);
  811. }
  812. // set modify flag
  813. SetModify();
  814. // Ok, return TRUE
  815. return true;
  816. }
  817. public bool InsrtSample(COTSSample a_pSample, int a_nIndex)
  818. {
  819. // input check
  820. if (a_pSample!=null)
  821. {
  822. // invalid input sample pointer
  823. return false;
  824. }
  825. if (m_listSamples.Count == 0 || a_nIndex >= (int)m_listSamples.Count)
  826. {
  827. m_listSamples.Add(a_pSample);
  828. }
  829. else if (a_nIndex <= 0)
  830. {
  831. m_listSamples.Insert(Convert.ToInt32(m_listSamples[a_nIndex]), a_pSample);
  832. }
  833. else
  834. {
  835. m_listSamples.Insert(Convert.ToInt32(m_listSamples[a_nIndex]) + a_nIndex, a_pSample);
  836. }
  837. // Ok, return TRUE
  838. return true;
  839. }
  840. public bool ChangeSamplePosition(int a_nIndexFrom, int a_nIndexTo)
  841. {
  842. // make sure both index are valid
  843. if (a_nIndexFrom < 0 || a_nIndexFrom >= (int)m_listSamples.Count)
  844. {
  845. // invalid from index
  846. return false;
  847. }
  848. if (a_nIndexTo < 0 || a_nIndexTo >= (int)m_listSamples.Count)
  849. {
  850. // invalid to index
  851. return false;
  852. }
  853. // need to do nothing if the two indexes are the same
  854. if (a_nIndexFrom != a_nIndexTo)
  855. {
  856. // move to last?
  857. bool bLast = (a_nIndexTo == ((int)m_listSamples.Count - 1));
  858. // get the sample
  859. COTSSample pSample = m_listSamples[a_nIndexFrom];
  860. // remove it from the samples list
  861. m_listSamples.Remove(pSample);
  862. // put the sample back in
  863. if (bLast)
  864. {
  865. m_listSamples.Add(pSample);
  866. }
  867. else
  868. {
  869. m_listSamples.Insert(Convert.ToInt32(m_listSamples[a_nIndexTo]) + a_nIndexTo, pSample);
  870. }
  871. }
  872. // Ok, return TRUE
  873. return true;
  874. }
  875. public bool MoveSamplePosition(COTSSample a_pTargetSample, COTSSample a_RefpSample, bool a_bBefore /*= TRUE*/)
  876. {
  877. // check input
  878. if (a_pTargetSample!=null)
  879. {
  880. // invalid input target sample pointer
  881. return false;
  882. }
  883. if (a_RefpSample!=null)
  884. {
  885. // invalid input ref sample pointer
  886. return false;
  887. }
  888. bool IsChanged = false;
  889. for (int itr = 0; itr < m_listSamples.Count; itr++)
  890. {
  891. if (m_listSamples[itr].GetName() == a_pTargetSample.GetName())
  892. {
  893. COTSSample pElementChemistry = m_listSamples[itr];
  894. m_listSamples.RemoveAt(itr);
  895. IsChanged = true;
  896. }
  897. }
  898. if (!IsChanged)
  899. {
  900. return false;
  901. }
  902. for (int itr = 0; itr < m_listSamples.Count; itr++)
  903. {
  904. if (m_listSamples[itr].GetName() == a_RefpSample.GetName())
  905. {
  906. COTSSample pElementChemistry = m_listSamples[itr];
  907. m_listSamples.RemoveAt(itr);
  908. IsChanged = true;
  909. }
  910. }
  911. if (!IsChanged)
  912. {
  913. return false;
  914. }
  915. // need to do nothing if the two sample are same
  916. // remove the sample from the samples list
  917. for (int itr = 0; itr < m_listSamples.Count; itr++)
  918. {
  919. if (m_listSamples[itr].GetName() == a_RefpSample.GetName())
  920. {
  921. COTSSample pElementChemistry = m_listSamples[itr];
  922. m_listSamples.RemoveAt(itr);
  923. IsChanged = true;
  924. }
  925. }
  926. if (!IsChanged)
  927. {
  928. return false;
  929. }
  930. // put the sample back in
  931. if (a_bBefore)
  932. {
  933. m_listSamples.Insert(0, a_pTargetSample);
  934. }
  935. else
  936. {
  937. m_listSamples.Insert(1, a_pTargetSample);
  938. }
  939. // Ok, return TRUE
  940. return true;
  941. }
  942. public bool MoveSamplePosition(String a_strTargetSampleName, String a_strRefSampleName, bool a_bBefore /*= TRUE*/)
  943. {
  944. // input check
  945. a_strTargetSampleName.Trim();
  946. if (a_strTargetSampleName=="")
  947. {
  948. // input target sample is an empty string
  949. return false;
  950. }
  951. a_strRefSampleName.Trim();
  952. if (a_strRefSampleName=="")
  953. {
  954. // input ref sample is an empty string
  955. return false;
  956. }
  957. // var itr = std::find_if(m_listSamples.begin(), m_listSamples.end(), [a_strTargetSampleName](COTSSamplePtr p) { return p->GetName().CompareNoCase(a_strTargetSampleName) == 0; });
  958. //if (itr == m_listSamples.end())
  959. //{
  960. // // target sample name is not the name of any sample in the samples list
  961. // return false;
  962. //}
  963. bool IsChanged = false;
  964. for (int itr = 0; itr < m_listSamples.Count; itr++)
  965. {
  966. if (m_listSamples[itr].GetName() == a_strTargetSampleName)
  967. {
  968. COTSSample pElementChemistry = m_listSamples[itr];
  969. m_listSamples.RemoveAt(itr);
  970. IsChanged = true;
  971. }
  972. }
  973. if (!IsChanged)
  974. {
  975. return false;
  976. }
  977. COTSSample pTargetSample = m_listSamples[0];
  978. // itr = std::find_if(m_listSamples.begin(), m_listSamples.end(), [a_strRefSampleName](COTSSamplePtr p) { return p->GetName().CompareNoCase(a_strRefSampleName) == 0; });
  979. //if (itr == m_listSamples.end())
  980. //{
  981. // // ref sample name is not the name of any sample in the samples list
  982. // return false;
  983. //}
  984. for (int itr = 0; itr < m_listSamples.Count; itr++)
  985. {
  986. if (m_listSamples[itr].GetName() == a_strTargetSampleName)
  987. {
  988. COTSSample pElementChemistry = m_listSamples[itr];
  989. m_listSamples.RemoveAt(itr);
  990. IsChanged = true;
  991. }
  992. }
  993. if (!IsChanged)
  994. {
  995. return false;
  996. }
  997. COTSSample pRefSample = pTargetSample;
  998. // need to do nothing if the two name are same
  999. if (a_strTargetSampleName.CompareTo(a_strRefSampleName) != 0)
  1000. {
  1001. return MoveSamplePosition(pTargetSample, pRefSample, a_bBefore);
  1002. }
  1003. // Ok, return TRUE
  1004. return true;
  1005. }
  1006. public COTSSample GetWorkingSample()
  1007. {
  1008. // check if the working sample index
  1009. if (m_nWorkingSampeIndex < 0 || m_nWorkingSampeIndex >= (int)m_listSamples.Count)
  1010. {
  1011. // invalid working sample index
  1012. return null;
  1013. }
  1014. return GetSampleByIndex(m_nWorkingSampeIndex);
  1015. }
  1016. int GetWorkingSampleIndex() { return m_nWorkingSampeIndex; }
  1017. void SetWorkingSampleIndex(int a_nWorkingSampleIndex) { m_nWorkingSampeIndex = a_nWorkingSampleIndex; }
  1018. public bool SetWorkingSampleByIndex(int a_nIndex)
  1019. {
  1020. // special treatment
  1021. if (a_nIndex == -1 && m_listSamples.Count == 0)
  1022. {
  1023. m_nWorkingSampeIndex = -1;
  1024. return true;
  1025. }
  1026. // check if the working sample index
  1027. if (0 > a_nIndex || a_nIndex >= (int)m_listSamples.Count)
  1028. {
  1029. // invalid sample index
  1030. return false;
  1031. }
  1032. m_nWorkingSampeIndex = a_nIndex;
  1033. return true;
  1034. }
  1035. public bool SetWorkingSampleByName(String a_pSampleName)
  1036. {
  1037. // check input sample name
  1038. a_pSampleName.Trim();
  1039. if (a_pSampleName=="")
  1040. {
  1041. // input sample name is empty
  1042. return false;
  1043. }
  1044. // go through sample list
  1045. int nIndex = 0;
  1046. foreach (var pSample in m_listSamples)
  1047. {
  1048. // return TRUE if this is not an exclude sample and its name is same with input
  1049. String strSampleName = pSample.GetName();
  1050. if (strSampleName.CompareTo(a_pSampleName) == 0)
  1051. {
  1052. // find the sample, return deleting result
  1053. return SetWorkingSampleByIndex(nIndex);
  1054. }
  1055. ++nIndex;
  1056. }
  1057. // failed to find the named sample
  1058. // failed to find the named sample return FALSE
  1059. return false;
  1060. }
  1061. public bool DeleteWorkingSample()
  1062. {
  1063. // check if the working sample index
  1064. if (m_nWorkingSampeIndex < 0 || m_nWorkingSampeIndex >= (int)m_listSamples.Count)
  1065. {
  1066. // invalid working sample index
  1067. return false;
  1068. }
  1069. // return the result of DeleteSampleByIndex
  1070. return DeleteSampleByIndex(m_nWorkingSampeIndex);
  1071. }
  1072. // Sample hole BSE image list
  1073. public List< CHoleBSEImg> GetHoleBESImgList() { return m_listHoleBSEImg; }
  1074. // hole BSE images list
  1075. public void SetHoleBESImgList(List<CHoleBSEImg> a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  1076. {
  1077. // clear the hole BSE image list if necessary
  1078. if (a_bClear)
  1079. {
  1080. m_listHoleBSEImg.Clear();
  1081. }
  1082. // go through the input list
  1083. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  1084. {
  1085. // create a hole BSE image copy
  1086. CHoleBSEImg pHoleBSEImgNew=(new CHoleBSEImg(pHoleBSEImg));
  1087. // add the new hole BSE image into HoleBSEImage list
  1088. m_listHoleBSEImg.Add(pHoleBSEImgNew);
  1089. }
  1090. if (m_nWorkingSampeIndex != -1)
  1091. {
  1092. COTSSample pSample = GetWorkingSample();
  1093. }
  1094. }
  1095. // get modify flag
  1096. public bool IsModified() { return m_bModify; }
  1097. // file name
  1098. public String GetFileName()
  1099. {
  1100. // make a copy of file path name
  1101. String strPathName = m_strPathName;
  1102. // is this a new file?
  1103. strPathName.Trim();
  1104. if (strPathName.CompareTo(UNTITLED_FILE_NAME) == 0)
  1105. {
  1106. return strPathName;
  1107. }
  1108. // get file name
  1109. String strFileName = GetFileNameWithoutExtension(strPathName);
  1110. strFileName = GetFileName(strFileName);
  1111. // return file name
  1112. return strFileName;
  1113. }
  1114. // get file name without extension
  1115. public String GetFileNameWithoutExtension(String a_strPathName)
  1116. {
  1117. string str = "";
  1118. Char[] sPath=new Char[otsdataconst.MAX_PATH + 4];
  1119. str.CopyTo(0,sPath, 0, Convert.ToInt32(a_strPathName));
  1120. return a_strPathName;
  1121. }
  1122. // if the new sample name can be used.
  1123. public bool IsValidSampleName(String a_sName)
  1124. {
  1125. int nindex = -1;
  1126. foreach (var pSample in m_listSamples)
  1127. {
  1128. nindex++;
  1129. String sSampleName = pSample.GetName();
  1130. if (a_sName.CompareTo(sSampleName)!=0)
  1131. {
  1132. if (nindex != m_nWorkingSampeIndex)
  1133. {
  1134. return false;
  1135. }
  1136. }
  1137. }
  1138. return true;
  1139. }
  1140. // get std file list, a_nPos = -1, current do not use any STD lib, a_nPos = 0, current use STD lib.
  1141. public bool GetSTDFileList(int a_nPos, List<String> a_listSTDLibName)
  1142. {
  1143. String a_strFolderName = m_DefaultParameters.GetPartSTDLibFolderName();
  1144. if (a_strFolderName == "")
  1145. {
  1146. return false;
  1147. }
  1148. // lib name list
  1149. a_listSTDLibName.Clear();
  1150. // file list
  1151. List<String> listSTDFile = new List<string>();
  1152. listSTDFile.Clear();
  1153. const String STD_LIB_EXT = ".db";
  1154. // get file list
  1155. if (!GetFileNameList(a_strFolderName, STD_LIB_EXT, listSTDFile))
  1156. {
  1157. return false;
  1158. }
  1159. foreach (var file in listSTDFile)
  1160. {
  1161. //CString fileName = file.Left(file.GetLength() - 3);
  1162. String fileName = file;
  1163. a_listSTDLibName.Add(fileName);
  1164. }
  1165. //before the use choose one STD, always should be null.
  1166. a_nPos = -1;
  1167. String sCurrentSTD;
  1168. CMsrParams pCurrentParam;
  1169. COTSSample pSample = GetWorkingSample();
  1170. if (pSample == null)
  1171. {
  1172. }
  1173. else
  1174. {
  1175. pCurrentParam = pSample.GetMsrParams();
  1176. if (pCurrentParam == null)
  1177. {
  1178. }
  1179. //将当前使用的数据库的名字与下拉菜单的名字对应起来
  1180. sCurrentSTD = pCurrentParam.GetSTDName();
  1181. for (int i = 0; i < (int)a_listSTDLibName.Count; i++)
  1182. {
  1183. a_nPos++;
  1184. if (a_listSTDLibName[i].CompareTo(sCurrentSTD) == 0)
  1185. break;
  1186. }
  1187. }
  1188. return true;
  1189. }
  1190. // get file name list in a folder
  1191. public bool GetFileNameList(String a_strFolderName, String a_strFileType, List<String> a_listFileName)
  1192. {
  1193. // get file name
  1194. a_strFolderName += "*";
  1195. a_strFolderName += a_strFileType;
  1196. try
  1197. {
  1198. //find first file
  1199. int FindFileData = 0;
  1200. // int file = FindFirstFile(a_strFolderName, FindFileData);
  1201. string path = a_strFolderName;
  1202. string filename = a_strFileType;
  1203. List<string> lineStringList = new List<string>();//存储所有读取到的文件
  1204. DirectoryInfo[] dateDirArr = new DirectoryInfo(path).GetDirectories(); //取指定路径下的所有目录
  1205. foreach (DirectoryInfo directoryInfo in dateDirArr)
  1206. {
  1207. string fullName = directoryInfo.Name + "\\" + filename;
  1208. if (!File.Exists(fullName))
  1209. {
  1210. continue;//目录下不存在此文件,返回。
  1211. }
  1212. FileInfo file = new FileInfo(fullName);
  1213. StreamReader reader = new StreamReader(file.FullName);
  1214. while (!reader.EndOfStream)//判断是否读取完成
  1215. {
  1216. lineStringList.Add(reader.ReadLine());
  1217. }
  1218. reader.Close();
  1219. }
  1220. int INVALID_HANDLE_VALUE = 0;
  1221. //find other file
  1222. if (FindFileData!= INVALID_HANDLE_VALUE)
  1223. {
  1224. a_listFileName.Add(FindFileData.ToString());
  1225. bool bState = false;
  1226. FindFile(a_strFolderName, FindFileData.ToString());
  1227. while (bState)
  1228. {
  1229. a_listFileName.Add(FindFileData.ToString());
  1230. FindFile(a_strFolderName, FindFileData.ToString());
  1231. }
  1232. }
  1233. }
  1234. catch (Exception pe)
  1235. {
  1236. return false;
  1237. }
  1238. return true;
  1239. }
  1240. static String FindFile(String filename, String path)
  1241. {
  1242. if (Directory.Exists(path))
  1243. {
  1244. if (File.Exists(path + filename))
  1245. return path + filename;
  1246. String[] directorys = Directory.GetDirectories(path);
  1247. foreach (String d in directorys)
  1248. {
  1249. String p = FindFile(filename, d);
  1250. if (p != null)
  1251. return p;
  1252. }
  1253. }
  1254. return null;
  1255. }
  1256. //// change STD
  1257. public bool ChangeSTDFromList(int a_nPos)
  1258. {
  1259. String a_strFolderName = m_DefaultParameters.GetPartSTDLibFolderName();
  1260. if (a_strFolderName == "")
  1261. {
  1262. return false;
  1263. }
  1264. // file list
  1265. List<String> listSTDFile = new List<string>();
  1266. listSTDFile.Clear();
  1267. const String STD_LIB_EXT = ".db";
  1268. // get file list
  1269. if (!GetFileNameList(a_strFolderName, STD_LIB_EXT, listSTDFile))
  1270. {
  1271. return false;
  1272. }
  1273. List<String> listSTDLibName = new List<string>();
  1274. listSTDLibName.Clear();
  1275. foreach (var file in listSTDFile)
  1276. {
  1277. //CString fileName = file.Left(file.GetLength() - 3);
  1278. String fileName = file;
  1279. listSTDLibName.Add(fileName);
  1280. }
  1281. if (a_nPos > (int)listSTDLibName.Count)
  1282. {
  1283. return false;
  1284. }
  1285. String STDName = listSTDLibName[a_nPos];
  1286. // updata STD to working sample
  1287. COTSSample pSample = GetWorkingSample();
  1288. CMsrParams pParam = pSample.GetMsrParams();
  1289. pParam.SetSTDName(STDName);
  1290. pSample.SetMsrParams(pParam);
  1291. return true;
  1292. }
  1293. // get measured sample list
  1294. public bool GetMsredSampleList(List<COTSSample> a_listMsredSample)
  1295. {
  1296. a_listMsredSample.Clear();
  1297. foreach (var pSample in m_listSamples)
  1298. {
  1299. if (pSample.GetMsrResults() == new CMsrResults())
  1300. {
  1301. continue;
  1302. }
  1303. else
  1304. {
  1305. a_listMsredSample.Add(pSample);//the reference count will increament automatically.
  1306. }
  1307. }
  1308. return true;
  1309. }
  1310. // calculate measurement area
  1311. public CDomain CalculateMsrArea(CHole a_pHole)
  1312. {
  1313. CDomain pMsrArea = new CDomain(a_pHole.GetShape(), a_pHole.GetDomainRect());
  1314. // create measurement area
  1315. // reset measurement area
  1316. // measurement area should smaller than the sample hole
  1317. Rectangle rectMsrArea = pMsrArea.GetDomainRect();
  1318. int nWidth = rectMsrArea.Width;
  1319. int nHeight = rectMsrArea.Height;
  1320. int nDeflateX = CalculateDeflateValue(nWidth);
  1321. int nDeflateY = CalculateDeflateValue(nHeight);
  1322. rectMsrArea.Offset(nDeflateX, nDeflateY);
  1323. pMsrArea.SetDomainRect(rectMsrArea);
  1324. // return measurement area
  1325. return pMsrArea;
  1326. }
  1327. // calculate deflate value
  1328. public int CalculateDeflateValue(int a_nWitchOrHeight)
  1329. {
  1330. const long LENGTH_THRESHOLD = 150000;
  1331. const long LENGTH_THRESHOLD_MIN = 10000;
  1332. const int EDGE = 1000;
  1333. const int EDGE_MIN = 500;
  1334. // deflate 1000 if width or height is greater than 15000
  1335. if (a_nWitchOrHeight >= LENGTH_THRESHOLD)
  1336. {
  1337. return EDGE;
  1338. }
  1339. // deflate 500 if width or height is greater than 15000
  1340. else if (a_nWitchOrHeight >= LENGTH_THRESHOLD_MIN)
  1341. {
  1342. return EDGE_MIN;
  1343. }
  1344. // otherwise, no deflate
  1345. return 0;
  1346. }
  1347. // remove background
  1348. public bool RemoveBackGround(CBSEImg a_pImgIn, OTSDataType.COTSImageProcessParam a_pImgProcessParam, CBSEImg a_pImgOut)
  1349. {
  1350. /*COTSFieldMgr fldMgr;
  1351. fldMgr.SetBSEImage(a_pImgIn);
  1352. fldMgr.RemoveBSEImageBG(a_pImgProcessParam);
  1353. a_pImgOut = fldMgr.GetBSEImage();*/
  1354. // Binary
  1355. int nWidth = a_pImgIn.GetWidth();
  1356. int nHeight = a_pImgIn.GetHeight();
  1357. int nImgSize = nWidth * nHeight;
  1358. Byte[] pPixel = new Byte[nImgSize];
  1359. Byte[] pSrcImg = a_pImgIn.GetImageDataPointer();
  1360. Byte[] pTargetImg = new Byte[nImgSize];
  1361. long nBGStart;
  1362. long nBGEnd;
  1363. if (a_pImgProcessParam.GetBGRemoveType() == OTS_BGREMOVE_TYPE.MANUAL)
  1364. {
  1365. nBGStart = a_pImgProcessParam.GetBGGray().GetStart();
  1366. nBGEnd = a_pImgProcessParam.GetBGGray().GetEnd();
  1367. // delete background
  1368. for (uint i = 0; i < nImgSize; i++)
  1369. {
  1370. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1371. {
  1372. pPixel[i] = 0;
  1373. }
  1374. else
  1375. {
  1376. pPixel[i] = 255;
  1377. }
  1378. }
  1379. }
  1380. else
  1381. {
  1382. var range = a_pImgIn.CalBackground(a_pImgProcessParam.GetBGRemoveType());//in this function, the input perameter takes no effect right now,but may be useful in the future.
  1383. nBGStart = range.GetStart();
  1384. nBGEnd = range.GetEnd();
  1385. switch (a_pImgProcessParam.GetAutoBGRemoveType())
  1386. {
  1387. case OTS_AUTOBGREMOVE_TYPE.DOWNWARD:
  1388. for (uint i = 0; i < nImgSize; i++)
  1389. {
  1390. if (pSrcImg[i] <= nBGEnd)
  1391. {
  1392. pPixel[i] = 0;
  1393. }
  1394. else
  1395. {
  1396. pPixel[i] = 255;
  1397. }
  1398. }
  1399. break;
  1400. case OTS_AUTOBGREMOVE_TYPE.UPWARD:
  1401. for (uint i = 0; i < nImgSize; i++)
  1402. {
  1403. if (pSrcImg[i] >= nBGStart)
  1404. {
  1405. pPixel[i] = 0;
  1406. }
  1407. else
  1408. {
  1409. pPixel[i] = 255;
  1410. }
  1411. }
  1412. break;
  1413. case OTS_AUTOBGREMOVE_TYPE.MIDDLE:
  1414. for (uint i = 0; i < nImgSize; i++)
  1415. {
  1416. if (pSrcImg[i] >= nBGStart && pSrcImg[i] <= nBGEnd)
  1417. {
  1418. pPixel[i] = 0;
  1419. }
  1420. else
  1421. {
  1422. pPixel[i] = 255;
  1423. }
  1424. }
  1425. break;
  1426. default:
  1427. break;
  1428. }
  1429. }
  1430. long nPtStart = a_pImgProcessParam.GetParticleGray().GetStart();
  1431. long nPtEnd = a_pImgProcessParam.GetParticleGray().GetEnd();
  1432. // image process
  1433. m_HardwareMgr.BErode3(pPixel.ToString(), pTargetImg.ToString(), 5, nHeight, nWidth);
  1434. m_HardwareMgr.BDilate3(pTargetImg.ToString(), pPixel.ToString(), 5, nHeight, nWidth);
  1435. // show the image
  1436. for (uint i = 0; i < nImgSize; i++)
  1437. {
  1438. uint Pixel = BitConverter.ToUInt32(pPixel,0);
  1439. uint SrcImg = BitConverter.ToUInt32(pSrcImg, 0);
  1440. Pixel = 255 - (Pixel + i);
  1441. if ((Pixel + i) == 0)
  1442. {
  1443. Pixel = SrcImg + i;
  1444. }
  1445. }
  1446. a_pImgOut. SetImageRect(a_pImgIn.GetImageRect());
  1447. a_pImgOut.SetImageData(pPixel);
  1448. return true;
  1449. }
  1450. public COTSGeneralParameters GetDefaultParameters() { return m_DefaultParameters; }
  1451. public void SetDefaultParameters(COTSGeneralParameters val) { m_DefaultParameters = val; }
  1452. //合并后新添加的需要的方法
  1453. public bool SetImageScanParam(COTSGeneralParameters ImageScan) { return false; }
  1454. public COTSImgScanPrm GetImageScanParam() { return null; }
  1455. }
  1456. }