//时间:20200610 //作者:郝爽 //功能:测量文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FileManager; using System.Xml; using System.IO; //using System.Windows.Forms.dll; namespace MeasureData { public class MeasureFile:ISlo { public const string UNTITLED_FILE_NAME = "Untitled"; #region 内容 //文件名 private string m_fileName; public string FileName { get { return this.m_fileName; } set { this.m_fileName = value; } } //文件路径 private string m_filepath; public string FilePath { get { return this.m_filepath; } set { this.m_filepath = value; } } //切孔链表 private List m_listCutHole; public List ListCutHole { get { return this.m_listCutHole; } set { this.m_listCutHole = value; } } //测量参数 private MeasureParam m_measureParam; public MeasureParam MParam { get { return this.m_measureParam; } set { this.m_measureParam = value; } } #endregion /// /// 创建XML文件 /// /// 0:失败;1:成功;2:文件已存在 public int CreateXml() { if (!File.Exists(this.FilePath + "\\" + this.FileName)) { if( XmlManager.CreateXmlFile(this.FilePath + "\\" + this.FileName)) { return 1; } else { return 0; } } else { return 2; } } //XML文件保存 //样品孔存储xml文档 public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode) { Slo slo_msf = new Slo(); Slo slo_cuthole = new Slo(); Collection cot_cuthole = new Collection(); for (int i = 0; i < this.ListCutHole.Count; i++) { cot_cuthole.addItem(ListCutHole[i]); } xString FileName = new xString(); xString FilePath = new xString(); FileName.AssignValue(this.FileName); FilePath.AssignValue(this.FilePath); slo_cuthole.Register("FileName", FileName); slo_cuthole.Register("FilePath", FilePath); slo_cuthole.Register("ListCutHole", cot_cuthole); slo_msf.Register("MeasureFile", slo_cuthole); if (isStoring) { slo_msf.Serialize(true, xml, rootNode); } else { slo_msf.Serialize(false, xml, rootNode); this.FileName = FileName.value(); this.FilePath = FilePath.value(); for (int i = 0; i < cot_cuthole.m_vCollection.Count; i++) { this.ListCutHole.Add(cot_cuthole.getItem(i)); } } } //构造函数 public MeasureFile() { Init(); } public void Init() { this.ListCutHole = new List(); this.FileName = @""; this.FilePath = @""; this.MParam = new MeasureParam(); } #region 操作 //新建 public bool New() { //int ret = CreateXml(); //if(ret>0) //{ // XmlDocument doc = new XmlDocument(); // doc.Load(this.FilePath + "\\" + this.FileName);//载入xml文件 // XmlNode root = doc.SelectSingleNode("XMLData"); // Serialize(true, doc, root); // doc.Save(this.FilePath + "\\" + this.FileName); //} // 设置路径为初始默认路径 FilePath = UNTITLED_FILE_NAME; // Ok, return TRUE return true; } //打开 public void Open() { } //保存 public void Save() { //Serialize(); } //另存为 public void SaveAs() { } //从文件生成切割孔信息 public void GetCutHolesFromFile(string a_FilePathName) { //弹出打开txt文件的对话矿 a_FilePathName.Trim(); if (string.IsNullOrEmpty(a_FilePathName)) { ////新建一个文件对话框 //OpenFileDialog pOpenFileDialog = new OpenFileDialog(); ////设置对话框标题 //pOpenFileDialog.Title = "打开shp文件"; ////设置打开文件类型 //pOpenFileDialog.Filter = "Shape文件(*.shp)|*.shp"; ////监测文件是否存在 //pOpenFileDialog.CheckFileExists = true; ////文件打开后执行以下程序 //if (pOpenFileDialog.ShowDialog() == DialogResult.OK) //{ // System.IO.Path.GetFullPath(openFileDialog1.FileName); //绝对路径 // System.IO.Path.GetExtension(openFileDialog1.FileName); //文件扩展名 // System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName);//文件名没有扩展 // 名 // System.IO.Path.GetFileName(openFileDialog1.FileName); //得到文件 // System.IO.Path.GetDirectoryName(openFileDialog1.FileName); //得到路径 //} } //按行取出txt文件 //解析切孔生成带有位置的切孔 //// check file pathname //a_strFilePathName.Trim(); //if (a_strFilePathName.IsEmpty()) //{ // // open file dialog // CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, TEXTFILE_FILTER); // CString strDlgTitle = _T(""); // strDlgTitle.LoadString(IDS_OPEN_STAGE_FILE); // dlg.m_ofn.lpstrTitle = strDlgTitle; // if (dlg.DoModal() != IDOK) // { // // user didn't click OK button, return FALSE // LogTrace(__FILE__, __LINE__, _T("LoadStageFromTextFile: user canceled on file open dialog.")); // return FALSE; // } // else // { // a_strFilePathName = dlg.GetPathName(); // } //} //// load string lines from the file //std::vector listLineStr = COTSHelper::LoadTextFileToCStingList(a_strFilePathName); //// get stage components //// stage name //CString strName = _T(""); //// coordinate system //COORDINATE_SYSTEM_SETTING nCoodrSysSetting = COORDINATE_SYSTEM_SETTING::INVALID; //// boundary //CDomainPtr pBoundary = nullptr; //// STD //CDomainPtr pSTD = nullptr; //// Ferrari //CDomainPtr pFerrari = nullptr; //// Holes list //CHolesList listHoles; //for (auto strLine : listLineStr) //{ // // split the string line with ":" // std::vector listStr = COTSHelper::SplitString(strLine, FILE_TITLE_SPLIT); // // jump over the string if it is invalid // // it should have a title string and value string // if ((int)listStr.size() != TEXTFILE_ITEM_COLUMN_NUMBER) // { // continue; // } // CString strTitle = listStr[0]; // CString strValue = listStr[1]; // // get stage component // for (int i = (int)STAGE_ITEMS::MIN; i <= (int)STAGE_ITEMS::MAX; ++i) // { // // match title? // CString strFileItemTitle; // strFileItemTitle.LoadString(IDS_STAGE_FILE_TITLE_FIRST + i); // if (strTitle.CompareNoCase(strFileItemTitle) == 0) // { // // found a stage item // switch ((STAGE_ITEMS)i) // { // case STAGE_ITEMS::NAME: // { // // jump over if name is set // if (strName.IsEmpty()) // { // // jump over if value string is empty // strValue.Trim(); // if (!strValue.IsEmpty()) // { // strName = strValue; // } // } // } // break; // case STAGE_ITEMS::COORDINATE_SYSTEM: // { // // jump over if coordinate system setting is set // if (nCoodrSysSetting == COORDINATE_SYSTEM_SETTING::INVALID) // { // // jump over if value string is empty // strValue.Trim(); // if (!strValue.IsEmpty()) // { // // string to int // int nValue; // if (COTSHelper::StringToInt(strValue, nValue)) // { // // validation // if (nValue >= (int)COORDINATE_SYSTEM_SETTING::MIN && nValue <= (int)COORDINATE_SYSTEM_SETTING::MAX) // { // nCoodrSysSetting = (COORDINATE_SYSTEM_SETTING)nValue; // } // } // } // } // } // break; // case STAGE_ITEMS::BOUNDARY: // { // // jump over if boundary is set // if (pBoundary == nullptr) // { // // get boundary // CDomainPtr pDomain = GetDomain(strValue); // if (pDomain != nullptr) // { // pBoundary = pDomain; // } // } // } // break; // case STAGE_ITEMS::STD: // { // // jump over if STD is set // if (pSTD == nullptr) // { // // get STD // CDomainPtr pDomain = GetDomain(strValue); // if (pDomain != nullptr) // { // pSTD = pDomain; // } // } // } // break; // case STAGE_ITEMS::FERRARI: // { // // jump over if pFerrari is set // if (pFerrari == nullptr) // { // // get STD // CDomainPtr pDomain = GetDomain(strValue); // if (pDomain != nullptr) // { // pFerrari = pDomain; // } // } // } // break; // case STAGE_ITEMS::HOLE: // { // // try to get a sample hole with the value string // CHolePtr pHole = GetHole(strValue); // if (pHole != nullptr) // { // listHoles.push_back(pHole); // } // } // break; // } // // get out the for loop // break; // } // } //} //// check stage components //// name should be ok //if (pBoundary == nullptr) //{ // return FALSE; //} //if (pBoundary->IsInvalid()) //{ // // boundary is invalid // return FALSE; //} //if (pSTD != nullptr) //{ // // STD has to be inside of the boundary // if (!pBoundary->DomainInDomain(*(pSTD.get()))) // { // // STD is over the boundary // return FALSE; // } //} //else //{ // LogErrorTrace(__FILE__, __LINE__, _T("LoadStageFromTextFile: empty STD domain pointer.")); // return FALSE; //} //if (pFerrari != nullptr) //{ // // Ferrari has to be inside of the boundary // if (!pBoundary->DomainInDomain(*(pFerrari.get()))) // { // // Ferrari is over the boundary // return FALSE; // } // else if (pSTD->IntersectDomain(*(pFerrari.get()))) // { // // Ferrari has common part with STD // return FALSE; // } //} //else //{ // LogErrorTrace(__FILE__, __LINE__, _T("LoadStageFromTextFile: empty Ferrari domain pointer.")); // return FALSE; //} //// hole has to be inside of the boundary and can't have common part with STD and each other //CHolesList listStageHoles; //for (auto pHole : listHoles) //{ // if (!pBoundary->DomainInDomain(*(pHole.get()))) // { // // this hole is over the boundary, jump over // continue; // } // else if (pSTD->IntersectDomain(*(pHole.get()))) // { // // this hole has common part with STD, jump over // continue; // } // else if (pFerrari->IntersectDomain(*(pHole.get()))) // { // // this hole has common part with Ferrari, jump over // continue; // } // BOOL bHasCommonPart = FALSE; // for (auto pStageHole : listStageHoles) // { // if (pStageHole->IntersectDomain(*(pHole.get()))) // { // // this hole has common part with a hole already on the stage // bHasCommonPart = TRUE; // break; // } // } // if (bHasCommonPart) // { // // this hole has common part with a hole already on the stage, jump over // continue; // } // // the hole is ok, add it into stage holes list // listStageHoles.push_back(pHole); //} //if (listStageHoles.size() == 0) //{ // // no hole at all // return FALSE; //} //// the stage is in SEM coordinate system, convert all components to OTS system //if (nCoodrSysSetting == COORDINATE_SYSTEM_SETTING::SEM) //{ // if (!ConverSEMToOTSSystem(pBoundary, pSTD, listStageHoles)) // { // return FALSE; // } //} //// create stage //CStagePtr pStage(new CStage()); //// set stage components //pStage->SetName(strName); //pStage->SetBoundary(pBoundary); //pStage->SetSTD(pSTD); //pStage->SetFerrari(pFerrari); //pStage->SetHoleList(listStageHoles); //// add the stage into stages list //m_listStages.push_back(pStage); //// ok, return TRUE //return TRUE; } #endregion } }