CSampleParamMgr.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using OTSDataType;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. using NLog;
  11. namespace OTSModelSharp
  12. {
  13. //used for export and import the parameter
  14. public class CSampleParamMgr
  15. {
  16. //-------------------全局定义---------
  17. protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  18. // measurement parameters file mark
  19. public int MRS_PARAM_FILE_MARK = 'M' + 'E' + 'A' + 'S' + 'U' + 'R' + 'E' + 'P' + 'A' + 'R' + 'A' + 'M';
  20. // measurement parameters file version string
  21. public string MRS_PARAM_FILE_VERSION = "1.1.1";
  22. // measurement parameters file
  23. public string MESUREMENT_PARAM_FILE_EXT = (".mrp");
  24. public string MESUREMENT_PARAM_FILE_FILTER = ("Measurement Parmeters Files (*.mrp)|*.mrp||");
  25. //------------------定义----------------
  26. // file pathname
  27. string m_strPathName;
  28. // measurement parameters
  29. CSampleParam m_poMsrParams;
  30. //----------------public------------------
  31. public CSampleParamMgr()
  32. {
  33. // initialization
  34. Init();
  35. }
  36. public void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  37. {
  38. xInt xMrsParamFileMark = new xInt();
  39. xString xMrsOaramFileVersion = new xString();
  40. Slo slo = new Slo();
  41. slo.Register("MrsParamFileMark", xMrsParamFileMark);
  42. slo.Register("MrsParamFileVersion", xMrsOaramFileVersion);
  43. slo.Register("MsrParams", m_poMsrParams);
  44. if (isStoring)
  45. {
  46. xMrsParamFileMark.AssignValue(MRS_PARAM_FILE_MARK);
  47. xMrsOaramFileVersion.AssignValue(MRS_PARAM_FILE_VERSION);
  48. slo.Serialize(true, classDoc, rootNode);
  49. }
  50. else
  51. {
  52. slo.Serialize(false, classDoc, rootNode);
  53. }
  54. }
  55. //load/Save
  56. public bool Load(string a_strPathName, bool a_bClear)
  57. {
  58. if (a_bClear)
  59. {
  60. Init();
  61. }
  62. //获取属性配置文件
  63. XmlDocument doc = new XmlDocument();
  64. //载入xml文件
  65. doc.Load(a_strPathName);
  66. XmlNode rootNode = doc.SelectSingleNode("XMLData");
  67. Serialize(false, doc, rootNode);
  68. // file pathname
  69. m_strPathName = a_strPathName;
  70. // ok, return TRUE
  71. return true;
  72. }
  73. public bool Load(bool a_bClear)
  74. {
  75. if (a_bClear)
  76. {
  77. Init();
  78. }
  79. SaveFileDialog folder = new SaveFileDialog();
  80. folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData";
  81. if (folder.ShowDialog() != DialogResult.OK)
  82. {
  83. return false;
  84. }
  85. //a_strPathName = folder.InitialDirectory;
  86. XmlDocument doc = new XmlDocument();
  87. doc.Load(folder.InitialDirectory);
  88. doc.RemoveAll();
  89. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  90. doc.AppendChild(xmldecl);
  91. XmlElement rootNode = doc.CreateElement("XMLData");
  92. doc.AppendChild(rootNode);
  93. Serialize(false, doc, rootNode);
  94. try
  95. {
  96. doc.Save(folder.InitialDirectory);
  97. }
  98. catch
  99. {
  100. return false;
  101. }
  102. m_strPathName = folder.InitialDirectory;
  103. return true;
  104. }
  105. public bool Save(string a_strPathName)
  106. {
  107. //保存测量项目文件 .prj
  108. XmlDocument doc = new XmlDocument();
  109. //添加xml文件头申明
  110. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  111. doc.AppendChild(xmldecl);
  112. XmlElement rootNode = doc.CreateElement("XMLData");
  113. doc.AppendChild(rootNode);
  114. Serialize(true, doc, rootNode);
  115. m_strPathName = a_strPathName;
  116. try
  117. {
  118. doc.Save(a_strPathName);
  119. }
  120. catch
  121. {
  122. return false;
  123. }
  124. m_strPathName = a_strPathName;
  125. return true;
  126. }
  127. public bool Save()
  128. {
  129. SaveFileDialog folder = new SaveFileDialog();
  130. folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData";
  131. if (folder.ShowDialog() != DialogResult.OK)
  132. {
  133. return false;
  134. }
  135. XmlDocument doc = new XmlDocument();
  136. doc.Load(folder.InitialDirectory);
  137. doc.RemoveAll();
  138. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  139. doc.AppendChild(xmldecl);
  140. XmlElement rootNode = doc.CreateElement("XMLData");
  141. doc.AppendChild(rootNode);
  142. Serialize(true, doc, rootNode);
  143. // file pathname
  144. m_strPathName = folder.InitialDirectory;
  145. try
  146. {
  147. doc.Save(folder.InitialDirectory);
  148. }
  149. catch
  150. {
  151. return false;
  152. }
  153. return true;
  154. }
  155. // file pathname
  156. public string GetPathName()
  157. {
  158. return m_strPathName;
  159. }
  160. public void SetPathName(string a_strPathName)
  161. {
  162. m_strPathName = a_strPathName;
  163. }
  164. // measurement parameters file
  165. public CSampleParam GetMsrParams()
  166. {
  167. return m_poMsrParams;
  168. }
  169. public bool SetMsrParamFile(CSampleParam a_poMsrParams)
  170. {
  171. m_poMsrParams = a_poMsrParams;
  172. return true;
  173. }
  174. //--------------protected----------------
  175. // initialization
  176. protected void Init()
  177. {
  178. m_poMsrParams = new CSampleParam();
  179. }
  180. // duplication
  181. public void Duplicate( CSampleParamMgr a_oSource)
  182. {
  183. // initialization
  184. Init();
  185. // copy data over
  186. }
  187. }
  188. }