using OTSDataType; using OTSModelSharp.DTL; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace OTSModelSharp { public class CIncAFileMgr { // file pathname String m_strPathName; // particle list List m_listParticle = new List(); // X-ray list List m_listPosXray = new List(); //database CIncAFileMgr m_pIncADataDB; CSQLiteTable cSQLiteTable = new CSQLiteTable(); CMergeParticleDB m_pMergePartDB = new CMergeParticleDB("", null); CSmallParticleInfoDB m_pSmallPartInfoDB; CSegmentDB m_pSegmentDB; IDBBase myDB; System.Drawing.Point m_FieldPos; CGenInfoDB m_generalInfoTable; CMsrSampleStatus msrStatus; CInformationDB mInformationDB; CIncADataDB m_IncADataDB; CSegmentDB m_SegmentDB; public CIncAFileMgr(String fileName) { // initialization m_strPathName = fileName; Init(); var mergePartDB = this.GetMergedParticleDB(); mergePartDB.Init();//judge if the table exist,if exist delete firstly,then creat,else creat. var smallPartDB = this.GetSmallParticleInfoDB(); smallPartDB.Init(); } // initialization public void Init() { // particle list m_listParticle.Clear(); // X-ray list m_listPosXray.Clear(); if (!CreateIncAFile()) { } } public CSegmentDB GetSegmentDB() { if (m_pSegmentDB!=null) { var datastorePtr = GetDatastore(); if (datastorePtr!=null) { m_pSegmentDB = datastorePtr; } } return m_pSegmentDB; } public CSegmentDB GetDatastore() { return m_pSegmentDB; } //Create public bool CreateIncAFile() { // check file name m_strPathName.Trim(); if (m_strPathName=="") { // error, wrong file name return false; } // get database name String sDatabaseName = m_strPathName; if (Exists(sDatabaseName)) { if (!Open(m_strPathName, false)) { return false; } } else { if (!Create(m_strPathName,false)) { return false; } } return true; } public bool Open(string a_sFileName, bool a_bForce /*= TRUE*/) { var datastorePtr = GetDatastore(); return Open(a_sFileName, a_bForce); } // check if the file exists or not public bool Exists(string a_sPath) { return true; } public CSmallParticleInfoDB GetSmallParticleInfoDB() { if (m_pSmallPartInfoDB!=null) { var datastorePtr = GetDatastore(); if (datastorePtr!=null) { m_pSmallPartInfoDB = new CSmallParticleInfoDB("", cSQLiteTable); } } return m_pSmallPartInfoDB; } //Get DB public CIncAFileMgr GetIncADB() { if (m_pIncADataDB!=null) { var datastorePtr = GetDatastore(); if (datastorePtr!=null) { m_pIncADataDB = new CIncAFileMgr(""); } } return m_pIncADataDB; } public CMergeParticleDB GetMergedParticleDB() { if (m_pMergePartDB!=null) { var datastorePtr = GetDatastore(); if (datastorePtr!=null) { m_pMergePartDB = new CMergeParticleDB("", cSQLiteTable); } } return m_pMergePartDB; } public bool Create(string a_sFileName, bool a_bOverwrite /*= FALSE*/) { var datastorePtr = GetDatastore(); if (Create(a_sFileName, a_bOverwrite)) { return InitFile(); } return false; } // particle list public List GetParticleList() { return m_listParticle; } public bool InitFile() { var generalInfoTable = GetGeneralInfoDB(); if (generalInfoTable!=null) { return false; } // return generalInfoTable.Init(); return false; } public CGenInfoDB GetGeneralInfoDB() { if (m_generalInfoTable!=null) { var datastorePtr = GetDatastore(); if (datastorePtr!=null) { // m_generalInfoTable.reset(new CGenInfoDB("",datastorePtr)); m_generalInfoTable.GetTableInfo(); } } return m_generalInfoTable; } public void SetPosXrayList(List a_listPosXray, bool a_bClear) { // clear holes list if necessary if (a_bClear) { m_listPosXray.Clear(); } // copy the list foreach (var pPosXray in a_listPosXray) { m_listPosXray.Add(pPosXray); } } public void SetParticleList(List a_listParticle, bool a_bClear) { // clear holes list if necessary if (a_bClear) { m_listParticle.Clear(); } // copy the list foreach (var pParticle in a_listParticle) { m_listParticle.Add(pParticle); } } public void SetFieldPos(System.Drawing.Point p) { m_FieldPos = p; } public void SetMsrStatus(CMsrSampleStatus s) { msrStatus = s; } public bool Save(String a_strPathName/* = _T("")*/) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // check file pathname a_strPathName.Trim(); if (a_strPathName == "") { // file open dialog OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return false; } // get file pathname a_strPathName = openFileDialog.FileName; } // file pathname m_strPathName = a_strPathName; /*if (!CreateIncAFile()) { LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed.")); return FALSE; }*/ if (SaveIncAList()) { return false; } // ok, return TRUE return true; } public bool SaveIncAList() { var IncADataDB = GetIncADB(); SqlHelper cSQLiteStore = new SqlHelper(); //CSQLiteStore cSQLiteStore = new CSQLiteStore(); cSQLiteStore.ExecuteNonQuery("PRAGMA synchronous = OFF; "); //cSQLiteStore.CloseSynchronous(); cSQLiteStore.ExecuteNonQuery("begin transaction;"); //cSQLiteStore.BeginTransaction(); m_generalInfoTable = GetGeneralInfoDB(); mInformationDB.UpdateTimeStampRow(m_generalInfoTable.GetTableItemNameTimeEnd(), ""); int sta = Convert.ToInt32(msrStatus); mInformationDB.UpdateIntegerRow(m_generalInfoTable.GetTableItemNameResultStatus(), sta,""); foreach (var pParticle in m_listParticle) { int nFieldId = pParticle.GetFieldId(); int nXrayId = pParticle.GetAnalysisId(); CPosXray pXrayPtr; bool IsChanged = false; for (int itr = 0; itr < m_listPosXray.Count; itr++) { if (m_listPosXray[itr].ToString() != "") { CPosXray PosXray = m_listPosXray[itr]; m_listPosXray.RemoveAt(itr); IsChanged = true; } } if (!IsChanged) { return false;//m_listPosXray[itr]; } else { pXrayPtr = new CPosXray(); } //save x-ray data if (!m_IncADataDB.SaveAIncA(pParticle, pXrayPtr, m_FieldPos)) { return false; } // save segment var SegmentDB = GetSegmentDB(); if (!m_SegmentDB.SaveFeature(pParticle)) { return false; } } IncADataDB.GetDatastore(); return true; } } }