COTSProjMgrFile.cs 58 KB

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