| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | //时间:20200610//作者:郝爽//功能:测量文件using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using FileManager;using System.Xml;using System.IO;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<CutHole> m_listCutHole;        public List<CutHole> 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        /// <summary>        /// 创建XML文件        /// </summary>        /// <returns>0:失败;1:成功;2:文件已存在</returns>        public int CreateXml()        {            if (!File.Exists(this.FileName))            {                if( XmlManager.CreateXmlFile(this.FileName))                {                    return 1;                }                else                {                    return 0;                }            }            else            {                XmlManager.CreateXmlFile(this.FileName);                return 2;            }        }        //XML文件保存        //样品孔存储xml文档        public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)        {            CreateXml();            //定义根结点            Slo<MeasureFile> slo_msf = new Slo<MeasureFile>();            Slo<CutHole> slo_cuthole = new Slo<CutHole>();            Collection<CutHole> cot_cuthole = new Collection<CutHole>();            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_cuthole.Register("MParam", this.MParam);            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<CutHole>();            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.FileName);//载入xml文件                XmlNode root = doc.SelectSingleNode("XMLData");                Serialize(true, doc, root);                doc.Save(this.FileName);            }            // 设置路径为初始默认路径            this.FileName = UNTITLED_FILE_NAME;            // Ok, return TRUE            return true;        }        //打开        public void Open()        {        }        //保存        public bool Save()        {            //如果是新文件            this.FileName.Trim();            if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)            {                return SaveAs();            }            XmlDocument doc = new XmlDocument();            if (Directory.Exists(FileName))            {                doc.Load(this.FileName);//载入xml文件                           }            else            {                return SaveAs(); //当前路径不存在                           }            XmlNode root = doc.SelectSingleNode("XMLData");            Serialize(true, doc, root);            doc.Save(this.FileName);            return true;        }        //另存为        public bool SaveAs()        {            //打开保存文件的对话框            this.FileName.Trim();            if (!string.IsNullOrEmpty(this.FileName))            {                this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);            }            SaveFileDialog sfd = new SaveFileDialog();            sfd.Title = "保存测量文件";            sfd.Filter = "测量文件(*.msf)|*.msf";                        if (Directory.Exists(this.FilePath))            {                sfd.InitialDirectory = this.FilePath;            }            if (sfd.ShowDialog() == DialogResult.OK)            {                this.FileName = sfd.FileName.ToString(); //获得文件路径                FilePath = System.IO.Path.GetDirectoryName(this.FileName);                //创建一个新的文件                XmlDocument doc = new XmlDocument();                if (!File.Exists(this.FileName))                {                    if (CreateXml() > 0)//创建该文件?                    {                        return false;                    }                }                doc.Load(this.FileName);                XmlNode root = doc.SelectSingleNode("XMLData");                Serialize(true, doc, root);                doc.Save(this.FileName);            }            return true;        }        //从文件生成切割孔信息        public bool GetCutHolesFromFile(string a_FilePathName)        {            //弹出打开txt文件的对话矿            a_FilePathName.Trim();            if (string.IsNullOrEmpty(a_FilePathName))            {                //新建一个文件对话框                OpenFileDialog pOpenFileDialog = new OpenFileDialog();                //设置对话框标题                pOpenFileDialog.Title = "打开电镜位置列表文件";                //设置打开文件类型                pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";                //监测文件是否存在                pOpenFileDialog.CheckFileExists = true;                //文件打开后执行以下程序                if (pOpenFileDialog.ShowDialog() == DialogResult.OK)                {                    a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);                             //绝对路径                 }            }            //按行取出txt文件            string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);            //按现有的文件格式生成            string posMode = lines[1];            if (posMode.CompareTo(@"Absolute") != 0)            {                return false;            }            //解析切孔生成带有位置的切孔            //验证数量是否正确            int nNum = Convert.ToInt32(lines[3]);            int nLines = lines.Length;            if (nNum != (lines.Length - 4))            {                return false;            }            //行内标识的个数及位置            string[] titles = lines[2].Split(',');            int numPos = titles.Length;            int nLabelPos = Array.IndexOf(titles, "Label");            int nXPos = Array.IndexOf(titles, "X");            int nYPos = Array.IndexOf(titles, "Y");            int nZPos = Array.IndexOf(titles, "Z");            int nTPos = Array.IndexOf(titles, "T");            int nRPos = Array.IndexOf(titles, "R");            int nMPos = Array.IndexOf(titles, "M");            for (int i = 0; i < nNum; i++)            {                int currentLine = i + 4;                string currentString = lines[currentLine];                string[] CurrentPos = currentString.Split(',');                int nCurrentPosNum = CurrentPos.Length;                //当前行内的标识及位置个数不够                if (nCurrentPosNum != numPos)                {                    return false;                }                //切孔标识                CutHole CHole = new CutHole();                CHole.HoleName = CurrentPos[nLabelPos];                //切孔位置                SemPosition holePos = new SemPosition();                                holePos.X = (float)Convert.ToDouble(CurrentPos[nXPos]);                holePos.Y = (float)Convert.ToDouble(CurrentPos[nYPos]);                holePos.Z = (float)Convert.ToDouble(CurrentPos[nZPos]);                holePos.M = (float)Convert.ToDouble(CurrentPos[nMPos]);                holePos.T = (float)Convert.ToDouble(CurrentPos[nTPos]);                holePos.R = (float)Convert.ToDouble(CurrentPos[nRPos]);                CHole.Position = holePos;                //默认切割点均参与测试                CHole.SWITCH = true;                //更新切孔链表                this.ListCutHole.Add(CHole);            }            return true;                    }                #endregion    }}
 |