| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 | //时间: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;using System.Drawing;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 string m_CutHoleFilePath;        public string CutHoleFilePath        {            get { return this.m_CutHoleFilePath; }            set { this.m_CutHoleFilePath = 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)        {            Slo sFile = new Slo();            Slo sParam = new Slo();            Collection<CutHole> sCutHoles = new Collection<CutHole>();            foreach (CutHole hole in ListCutHole)            {                sCutHoles.addItem(hole);            }                                    xString FileName = new xString();            xString FilePath = new xString();            FileName.AssignValue(this.FileName);            FilePath.AssignValue(this.FilePath);            sFile.Register("FileName", FileName);            sFile.Register("FilePath", FilePath);            sFile.Register("Param", this.MParam);            sFile.Register("ListCutHole", sCutHoles);                        if (isStoring)            {                sFile.Serialize(true, xml, rootNode);                            }            else            {                sFile.Serialize(false, xml, rootNode);                this.FileName = FileName.value();                this.FilePath = FilePath.value();                List<CutHole> tempCutHoleList = new List<CutHole>();                for (int i = 0; i < sCutHoles.size(); i++)                {                    tempCutHoleList.Add(sCutHoles.getItem(i));                }                if (tempCutHoleList.Count == sCutHoles.size())                {                    ListCutHole.Clear();                    ListCutHole = tempCutHoleList;                }            }        }               //构造函数        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()        {            XmlDocument doc = new XmlDocument();            doc.Load(this.FileName);//载入xml文件            XmlNode root = doc.SelectSingleNode("XMLData");            Serialize(false, doc, root);            doc.Save(this.FileName);        }        //保存        public bool Save()        {            //如果是新文件            this.FileName.Trim();            if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)            {                return SaveAs();            }            XmlDocument doc = new XmlDocument();            if (CreateXml() == 0)//创建该文件?            {                return false;            }            if (File.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 (CreateXml() == 0)//创建该文件?                {                    return false;                }                doc.Load(this.FileName);                XmlNode root = doc.SelectSingleNode("XMLData");                Serialize(true, doc, root);                doc.Save(this.FileName);                return true;            }            else            {                return false;            }        }        //从文件生成切割孔信息        public bool GetCutHolesFromFile(string a_FilePathName)        {            try            {                //清空原文件中的切孔                this.ListCutHole.Clear();                //弹出打开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);                                                     //绝对路径                        CutHoleFilePath = a_FilePathName;                    }                }                //按行取出txt文件                string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);                LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);                //按现有的文件格式生成                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];                    LogManager.AddHardwareLog(currentString, true);                    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 = Convert.ToSingle(CurrentPos[nXPos]);                    holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);                    holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);                    holePos.M = Convert.ToSingle(CurrentPos[nMPos]);                    holePos.T = Convert.ToSingle(CurrentPos[nTPos]);                    holePos.R = Convert.ToSingle(CurrentPos[nRPos]);                    CHole.Position = holePos;                    //默认切割点均参与测试                    CHole.SWITCH = true;                    //更新切孔链表                    this.ListCutHole.Add(CHole);                    LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]                        + "Y =" + CurrentPos[nYPos]                        + "Z =" + CurrentPos[nZPos]                        + "M =" + CurrentPos[nTPos]                        + "T =" + CurrentPos[nRPos]                        + "R =" + CurrentPos[nMPos]                        , true);                }                return true;            }            catch (Exception ex)            {                LogManager.LogError(ex.Message);                return false;            }                 }        //从点集合对象中生成切割孔信息        public bool GetCutHolesFromAnalysisPosition(List<PointF> cutHolePointF)        {            //集合中的数量            int nNum = Convert.ToInt32(cutHolePointF.Count);            if (nNum == 0)            {                return false;            }            else            {                List<CutHole> tempCutHoleList = new List<CutHole>();                for (int i = 0; i < nNum; i++)                {                    //切孔标识                    CutHole CHole = new CutHole();                    CHole.HoleName = (i + 1).ToString();                    //切孔位置                    SemPosition holePos = new SemPosition();                    holePos.X = Convert.ToSingle(cutHolePointF[i].X);                    holePos.Y = Convert.ToSingle(cutHolePointF[i].Y);                    holePos.Z = Convert.ToSingle(0);                    holePos.M = Convert.ToSingle(0);                    holePos.T = Convert.ToSingle(0);                    holePos.R = Convert.ToSingle(0);                    CHole.Position = holePos;                    //默认切割点均参与测试                    CHole.SWITCH = true;                    //更新切孔链表                    tempCutHoleList.Add(CHole);                    LogManager.AddHardwareLog("X =" + cutHolePointF[i].X                        + "Y =" + cutHolePointF[i].Y                        + "Z =" + 0                        + "M =" + 0                        + "T =" + 0                        + "R =" + 0                        , true);                }                if (tempCutHoleList.Count > 0)                {                    this.ListCutHole.Clear();                    this.ListCutHole = tempCutHoleList;                }            }            return true;        }        #endregion    }}
 |