using OTSDataType; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using NLog; namespace OTSModelSharp { //used for export and import the parameter public class CSampleParamMgr { //-------------------全局定义--------- protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); // measurement parameters file mark public int MRS_PARAM_FILE_MARK = 'M' + 'E' + 'A' + 'S' + 'U' + 'R' + 'E' + 'P' + 'A' + 'R' + 'A' + 'M'; // measurement parameters file version string public string MRS_PARAM_FILE_VERSION = "1.1.1"; // measurement parameters file public string MESUREMENT_PARAM_FILE_EXT = (".mrp"); public string MESUREMENT_PARAM_FILE_FILTER = ("Measurement Parmeters Files (*.mrp)|*.mrp||"); //------------------定义---------------- // file pathname string m_strPathName; // measurement parameters CSampleParam m_poMsrParams; //----------------public------------------ public CSampleParamMgr() { // initialization Init(); } public void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode) { xInt xMrsParamFileMark = new xInt(); xString xMrsOaramFileVersion = new xString(); Slo slo = new Slo(); slo.Register("MrsParamFileMark", xMrsParamFileMark); slo.Register("MrsParamFileVersion", xMrsOaramFileVersion); slo.Register("MsrParams", m_poMsrParams); if (isStoring) { xMrsParamFileMark.AssignValue(MRS_PARAM_FILE_MARK); xMrsOaramFileVersion.AssignValue(MRS_PARAM_FILE_VERSION); slo.Serialize(true, classDoc, rootNode); } else { slo.Serialize(false, classDoc, rootNode); } } //load/Save public bool Load(string a_strPathName, bool a_bClear) { if (a_bClear) { Init(); } //获取属性配置文件 XmlDocument doc = new XmlDocument(); //载入xml文件 doc.Load(a_strPathName); XmlNode rootNode = doc.SelectSingleNode("XMLData"); Serialize(false, doc, rootNode); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return true; } public bool Load(bool a_bClear) { if (a_bClear) { Init(); } SaveFileDialog folder = new SaveFileDialog(); folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData"; if (folder.ShowDialog() != DialogResult.OK) { return false; } //a_strPathName = folder.InitialDirectory; XmlDocument doc = new XmlDocument(); doc.Load(folder.InitialDirectory); doc.RemoveAll(); XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(xmldecl); XmlElement rootNode = doc.CreateElement("XMLData"); doc.AppendChild(rootNode); Serialize(false, doc, rootNode); try { doc.Save(folder.InitialDirectory); } catch { return false; } m_strPathName = folder.InitialDirectory; return true; } public bool Save(string a_strPathName) { //保存测量项目文件 .prj XmlDocument doc = new XmlDocument(); //添加xml文件头申明 XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(xmldecl); XmlElement rootNode = doc.CreateElement("XMLData"); doc.AppendChild(rootNode); Serialize(true, doc, rootNode); m_strPathName = a_strPathName; try { doc.Save(a_strPathName); } catch { return false; } m_strPathName = a_strPathName; return true; } public bool Save() { SaveFileDialog folder = new SaveFileDialog(); folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData"; if (folder.ShowDialog() != DialogResult.OK) { return false; } XmlDocument doc = new XmlDocument(); doc.Load(folder.InitialDirectory); doc.RemoveAll(); XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(xmldecl); XmlElement rootNode = doc.CreateElement("XMLData"); doc.AppendChild(rootNode); Serialize(true, doc, rootNode); // file pathname m_strPathName = folder.InitialDirectory; try { doc.Save(folder.InitialDirectory); } catch { return false; } return true; } // file pathname public string GetPathName() { return m_strPathName; } public void SetPathName(string a_strPathName) { m_strPathName = a_strPathName; } // measurement parameters file public CSampleParam GetMsrParams() { return m_poMsrParams; } public bool SetMsrParamFile(CSampleParam a_poMsrParams) { m_poMsrParams = a_poMsrParams; return true; } //--------------protected---------------- // initialization protected void Init() { m_poMsrParams = new CSampleParam(); } // duplication public void Duplicate( CSampleParamMgr a_oSource) { // initialization Init(); // copy data over } } }