IncAFileMgr.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using OTSDataType;
  2. using OTSModelSharp.DTL;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace OTSModelSharp
  11. {
  12. public class CIncAFileMgr
  13. {
  14. // file pathname
  15. String m_strPathName;
  16. // particle list
  17. List<COTSParticle> m_listParticle = new List<COTSParticle>();
  18. // X-ray list
  19. List<CPosXray> m_listPosXray = new List<CPosXray>();
  20. //database
  21. CIncAFileMgr m_pIncADataDB;
  22. CSQLiteTable cSQLiteTable = new CSQLiteTable();
  23. CMergeParticleDB m_pMergePartDB = new CMergeParticleDB("", null);
  24. CSmallParticleInfoDB m_pSmallPartInfoDB;
  25. CSegmentDB m_pSegmentDB;
  26. IDBBase myDB;
  27. System.Drawing.Point m_FieldPos;
  28. CGenInfoDB m_generalInfoTable;
  29. CMsrSampleStatus msrStatus;
  30. CInformationDB mInformationDB;
  31. CIncADataDB m_IncADataDB;
  32. CSegmentDB m_SegmentDB;
  33. public CIncAFileMgr(String fileName)
  34. {
  35. // initialization
  36. m_strPathName = fileName;
  37. Init();
  38. var mergePartDB = this.GetMergedParticleDB();
  39. mergePartDB.Init();//judge if the table exist,if exist delete firstly,then creat,else creat.
  40. var smallPartDB = this.GetSmallParticleInfoDB();
  41. smallPartDB.Init();
  42. }
  43. // initialization
  44. public void Init()
  45. {
  46. // particle list
  47. m_listParticle.Clear();
  48. // X-ray list
  49. m_listPosXray.Clear();
  50. if (!CreateIncAFile())
  51. {
  52. }
  53. }
  54. public CSegmentDB GetSegmentDB()
  55. {
  56. if (m_pSegmentDB!=null)
  57. {
  58. var datastorePtr = GetDatastore();
  59. if (datastorePtr!=null)
  60. {
  61. m_pSegmentDB = datastorePtr;
  62. }
  63. }
  64. return m_pSegmentDB;
  65. }
  66. public CSegmentDB GetDatastore()
  67. {
  68. return m_pSegmentDB;
  69. }
  70. //Create
  71. public bool CreateIncAFile()
  72. {
  73. // check file name
  74. m_strPathName.Trim();
  75. if (m_strPathName=="")
  76. {
  77. // error, wrong file name
  78. return false;
  79. }
  80. // get database name
  81. String sDatabaseName = m_strPathName;
  82. if (Exists(sDatabaseName))
  83. {
  84. if (!Open(m_strPathName, false))
  85. {
  86. return false;
  87. }
  88. }
  89. else
  90. {
  91. if (!Create(m_strPathName,false))
  92. {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. public bool Open(string a_sFileName, bool a_bForce /*= TRUE*/)
  99. {
  100. var datastorePtr = GetDatastore();
  101. return Open(a_sFileName, a_bForce);
  102. }
  103. // check if the file exists or not
  104. public bool Exists(string a_sPath)
  105. {
  106. return true;
  107. }
  108. public CSmallParticleInfoDB GetSmallParticleInfoDB()
  109. {
  110. if (m_pSmallPartInfoDB!=null)
  111. {
  112. var datastorePtr = GetDatastore();
  113. if (datastorePtr!=null)
  114. {
  115. m_pSmallPartInfoDB = new CSmallParticleInfoDB("", cSQLiteTable);
  116. }
  117. }
  118. return m_pSmallPartInfoDB;
  119. }
  120. //Get DB
  121. public CIncAFileMgr GetIncADB()
  122. {
  123. if (m_pIncADataDB!=null)
  124. {
  125. var datastorePtr = GetDatastore();
  126. if (datastorePtr!=null)
  127. {
  128. m_pIncADataDB = new CIncAFileMgr("");
  129. }
  130. }
  131. return m_pIncADataDB;
  132. }
  133. public CMergeParticleDB GetMergedParticleDB()
  134. {
  135. if (m_pMergePartDB!=null)
  136. {
  137. var datastorePtr = GetDatastore();
  138. if (datastorePtr!=null)
  139. {
  140. m_pMergePartDB = new CMergeParticleDB("", cSQLiteTable);
  141. }
  142. }
  143. return m_pMergePartDB;
  144. }
  145. public bool Create(string a_sFileName, bool a_bOverwrite /*= FALSE*/)
  146. {
  147. var datastorePtr = GetDatastore();
  148. if (Create(a_sFileName, a_bOverwrite))
  149. {
  150. return InitFile();
  151. }
  152. return false;
  153. }
  154. // particle list
  155. public List<COTSParticle> GetParticleList() { return m_listParticle; }
  156. public bool InitFile()
  157. {
  158. var generalInfoTable = GetGeneralInfoDB();
  159. if (generalInfoTable!=null)
  160. {
  161. return false;
  162. }
  163. // return generalInfoTable.Init();
  164. return false;
  165. }
  166. public CGenInfoDB GetGeneralInfoDB()
  167. {
  168. if (m_generalInfoTable!=null)
  169. {
  170. var datastorePtr = GetDatastore();
  171. if (datastorePtr!=null)
  172. {
  173. // m_generalInfoTable.reset(new CGenInfoDB("",datastorePtr));
  174. m_generalInfoTable.GetTableInfo();
  175. }
  176. }
  177. return m_generalInfoTable;
  178. }
  179. public void SetPosXrayList(List<CPosXray> a_listPosXray, bool a_bClear)
  180. {
  181. // clear holes list if necessary
  182. if (a_bClear)
  183. {
  184. m_listPosXray.Clear();
  185. }
  186. // copy the list
  187. foreach (var pPosXray in a_listPosXray)
  188. {
  189. m_listPosXray.Add(pPosXray);
  190. }
  191. }
  192. public void SetParticleList(List<COTSParticle> a_listParticle, bool a_bClear)
  193. {
  194. // clear holes list if necessary
  195. if (a_bClear)
  196. {
  197. m_listParticle.Clear();
  198. }
  199. // copy the list
  200. foreach (var pParticle in a_listParticle)
  201. {
  202. m_listParticle.Add(pParticle);
  203. }
  204. }
  205. public void SetFieldPos(System.Drawing.Point p) { m_FieldPos = p; }
  206. public void SetMsrStatus(CMsrSampleStatus s) { msrStatus = s; }
  207. public bool Save(String a_strPathName/* = _T("")*/)
  208. {
  209. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  210. // check file pathname
  211. a_strPathName.Trim();
  212. if (a_strPathName == "")
  213. {
  214. // file open dialog
  215. OpenFileDialog openFileDialog = new OpenFileDialog();
  216. if (openFileDialog.ShowDialog() != DialogResult.OK)
  217. {
  218. return false;
  219. }
  220. // get file pathname
  221. a_strPathName = openFileDialog.FileName;
  222. }
  223. // file pathname
  224. m_strPathName = a_strPathName;
  225. /*if (!CreateIncAFile())
  226. {
  227. LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
  228. return FALSE;
  229. }*/
  230. if (SaveIncAList())
  231. {
  232. return false;
  233. }
  234. // ok, return TRUE
  235. return true;
  236. }
  237. public bool SaveIncAList()
  238. {
  239. var IncADataDB = GetIncADB();
  240. SqlHelper cSQLiteStore = new SqlHelper(); //CSQLiteStore cSQLiteStore = new CSQLiteStore();
  241. cSQLiteStore.ExecuteNonQuery("PRAGMA synchronous = OFF; "); //cSQLiteStore.CloseSynchronous();
  242. cSQLiteStore.ExecuteNonQuery("begin transaction;"); //cSQLiteStore.BeginTransaction();
  243. m_generalInfoTable = GetGeneralInfoDB();
  244. mInformationDB.UpdateTimeStampRow(m_generalInfoTable.GetTableItemNameTimeEnd(), "");
  245. int sta = Convert.ToInt32(msrStatus);
  246. mInformationDB.UpdateIntegerRow(m_generalInfoTable.GetTableItemNameResultStatus(), sta,"");
  247. foreach (var pParticle in m_listParticle)
  248. {
  249. int nFieldId = pParticle.GetFieldId();
  250. int nXrayId = pParticle.GetAnalysisId();
  251. CPosXray pXrayPtr;
  252. bool IsChanged = false;
  253. for (int itr = 0; itr < m_listPosXray.Count; itr++)
  254. {
  255. if (m_listPosXray[itr].ToString() != "")
  256. {
  257. CPosXray PosXray = m_listPosXray[itr];
  258. m_listPosXray.RemoveAt(itr);
  259. IsChanged = true;
  260. }
  261. }
  262. if (!IsChanged)
  263. {
  264. return false;//m_listPosXray[itr];
  265. }
  266. else
  267. {
  268. pXrayPtr = new CPosXray();
  269. }
  270. //save x-ray data
  271. if (!m_IncADataDB.SaveAIncA(pParticle, pXrayPtr, m_FieldPos))
  272. {
  273. return false;
  274. }
  275. // save segment
  276. var SegmentDB = GetSegmentDB();
  277. if (!m_SegmentDB.SaveFeature(pParticle))
  278. {
  279. return false;
  280. }
  281. }
  282. IncADataDB.GetDatastore();
  283. return true;
  284. }
  285. }
  286. }