MeasureFile.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //时间:20200610
  2. //作者:郝爽
  3. //功能:测量文件
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using FileManager;
  11. using System.Xml;
  12. using System.IO;
  13. namespace MeasureData
  14. {
  15. public class MeasureFile:ISlo
  16. {
  17. public const string UNTITLED_FILE_NAME = "Untitled";
  18. #region 内容
  19. //文件名
  20. private string m_fileName;
  21. public string FileName
  22. {
  23. get { return this.m_fileName; }
  24. set { this.m_fileName = value; }
  25. }
  26. //文件路径
  27. private string m_filepath;
  28. public string FilePath
  29. {
  30. get { return this.m_filepath; }
  31. set { this.m_filepath = value; }
  32. }
  33. //切孔链表
  34. private List<CutHole> m_listCutHole;
  35. public List<CutHole> ListCutHole
  36. {
  37. get { return this.m_listCutHole; }
  38. set { this.m_listCutHole = value; }
  39. }
  40. //测量参数
  41. private MeasureParam m_measureParam;
  42. public MeasureParam MParam
  43. {
  44. get { return this.m_measureParam; }
  45. set { this.m_measureParam = value; }
  46. }
  47. #endregion
  48. /// <summary>
  49. /// 创建XML文件
  50. /// </summary>
  51. /// <returns>0:失败;1:成功;2:文件已存在</returns>
  52. public int CreateXml()
  53. {
  54. if (!File.Exists(this.FileName))
  55. {
  56. if( XmlManager.CreateXmlFile(this.FileName))
  57. {
  58. return 1;
  59. }
  60. else
  61. {
  62. return 0;
  63. }
  64. }
  65. else
  66. {
  67. XmlManager.CreateXmlFile(this.FileName);
  68. return 2;
  69. }
  70. }
  71. //XML文件保存
  72. //样品孔存储xml文档
  73. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  74. {
  75. CreateXml();
  76. //定义根结点
  77. Slo<MeasureFile> slo_msf = new Slo<MeasureFile>();
  78. Slo<CutHole> slo_cuthole = new Slo<CutHole>();
  79. Collection<CutHole> cot_cuthole = new Collection<CutHole>();
  80. for (int i = 0; i < this.ListCutHole.Count; i++)
  81. {
  82. cot_cuthole.addItem(ListCutHole[i]);
  83. }
  84. xString FileName = new xString();
  85. xString FilePath = new xString();
  86. FileName.AssignValue(this.FileName);
  87. FilePath.AssignValue(this.FilePath);
  88. slo_cuthole.Register("FileName", FileName);
  89. slo_cuthole.Register("FilePath", FilePath);
  90. slo_cuthole.Register("ListCutHole", cot_cuthole);
  91. slo_cuthole.Register("MParam", this.MParam);
  92. slo_msf.Register("MeasureFile", slo_cuthole);
  93. if (isStoring)
  94. {
  95. slo_msf.Serialize(true, xml, rootNode);
  96. }
  97. else
  98. {
  99. slo_msf.Serialize(false, xml, rootNode);
  100. this.FileName = FileName.value();
  101. this.FilePath = FilePath.value();
  102. for (int i = 0; i < cot_cuthole.m_vCollection.Count; i++)
  103. {
  104. this.ListCutHole.Add(cot_cuthole.getItem(i));
  105. }
  106. }
  107. }
  108. //构造函数
  109. public MeasureFile()
  110. {
  111. Init();
  112. }
  113. public void Init()
  114. {
  115. this.ListCutHole = new List<CutHole>();
  116. this.FileName = @"";
  117. this.FilePath = @"";
  118. this.MParam = new MeasureParam();
  119. }
  120. #region 操作
  121. //新建
  122. public bool New()
  123. {
  124. int ret = CreateXml();
  125. if (ret > 0)
  126. {
  127. XmlDocument doc = new XmlDocument();
  128. doc.Load(this.FileName);//载入xml文件
  129. XmlNode root = doc.SelectSingleNode("XMLData");
  130. Serialize(true, doc, root);
  131. doc.Save(this.FileName);
  132. }
  133. // 设置路径为初始默认路径
  134. this.FileName = UNTITLED_FILE_NAME;
  135. // Ok, return TRUE
  136. return true;
  137. }
  138. //打开
  139. public void Open()
  140. {
  141. }
  142. //保存
  143. public bool Save()
  144. {
  145. //如果是新文件
  146. this.FileName.Trim();
  147. if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)
  148. {
  149. return SaveAs();
  150. }
  151. XmlDocument doc = new XmlDocument();
  152. if (Directory.Exists(FileName))
  153. {
  154. doc.Load(this.FileName);//载入xml文件
  155. }
  156. else
  157. {
  158. return SaveAs(); //当前路径不存在
  159. }
  160. XmlNode root = doc.SelectSingleNode("XMLData");
  161. Serialize(true, doc, root);
  162. doc.Save(this.FileName);
  163. return true;
  164. }
  165. //另存为
  166. public bool SaveAs()
  167. {
  168. //打开保存文件的对话框
  169. this.FileName.Trim();
  170. if (!string.IsNullOrEmpty(this.FileName))
  171. {
  172. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  173. }
  174. SaveFileDialog sfd = new SaveFileDialog();
  175. sfd.Title = "保存测量文件";
  176. sfd.Filter = "测量文件(*.msf)|*.msf";
  177. if (Directory.Exists(this.FilePath))
  178. {
  179. sfd.InitialDirectory = this.FilePath;
  180. }
  181. if (sfd.ShowDialog() == DialogResult.OK)
  182. {
  183. this.FileName = sfd.FileName.ToString(); //获得文件路径
  184. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  185. //创建一个新的文件
  186. XmlDocument doc = new XmlDocument();
  187. if (!File.Exists(this.FileName))
  188. {
  189. if (CreateXml() > 0)//创建该文件?
  190. {
  191. return false;
  192. }
  193. }
  194. doc.Load(this.FileName);
  195. XmlNode root = doc.SelectSingleNode("XMLData");
  196. Serialize(true, doc, root);
  197. doc.Save(this.FileName);
  198. }
  199. return true;
  200. }
  201. //从文件生成切割孔信息
  202. public bool GetCutHolesFromFile(string a_FilePathName)
  203. {
  204. //弹出打开txt文件的对话矿
  205. a_FilePathName.Trim();
  206. if (string.IsNullOrEmpty(a_FilePathName))
  207. {
  208. //新建一个文件对话框
  209. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  210. //设置对话框标题
  211. pOpenFileDialog.Title = "打开电镜位置列表文件";
  212. //设置打开文件类型
  213. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  214. //监测文件是否存在
  215. pOpenFileDialog.CheckFileExists = true;
  216. //文件打开后执行以下程序
  217. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  218. {
  219. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  220. }
  221. }
  222. //按行取出txt文件
  223. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  224. //按现有的文件格式生成
  225. string posMode = lines[1];
  226. if (posMode.CompareTo(@"Absolute") != 0)
  227. {
  228. return false;
  229. }
  230. //解析切孔生成带有位置的切孔
  231. //验证数量是否正确
  232. int nNum = Convert.ToInt32(lines[3]);
  233. int nLines = lines.Length;
  234. if (nNum != (lines.Length - 4))
  235. {
  236. return false;
  237. }
  238. //行内标识的个数及位置
  239. string[] titles = lines[2].Split(',');
  240. int numPos = titles.Length;
  241. int nLabelPos = Array.IndexOf(titles, "Label");
  242. int nXPos = Array.IndexOf(titles, "X");
  243. int nYPos = Array.IndexOf(titles, "Y");
  244. int nZPos = Array.IndexOf(titles, "Z");
  245. int nTPos = Array.IndexOf(titles, "T");
  246. int nRPos = Array.IndexOf(titles, "R");
  247. int nMPos = Array.IndexOf(titles, "M");
  248. for (int i = 0; i < nNum; i++)
  249. {
  250. int currentLine = i + 4;
  251. string currentString = lines[currentLine];
  252. string[] CurrentPos = currentString.Split(',');
  253. int nCurrentPosNum = CurrentPos.Length;
  254. //当前行内的标识及位置个数不够
  255. if (nCurrentPosNum != numPos)
  256. {
  257. return false;
  258. }
  259. //切孔标识
  260. CutHole CHole = new CutHole();
  261. CHole.HoleName = CurrentPos[nLabelPos];
  262. //切孔位置
  263. SemPosition holePos = new SemPosition();
  264. holePos.X = (float)Convert.ToDouble(CurrentPos[nXPos]);
  265. holePos.Y = (float)Convert.ToDouble(CurrentPos[nYPos]);
  266. holePos.Z = (float)Convert.ToDouble(CurrentPos[nZPos]);
  267. holePos.M = (float)Convert.ToDouble(CurrentPos[nMPos]);
  268. holePos.T = (float)Convert.ToDouble(CurrentPos[nTPos]);
  269. holePos.R = (float)Convert.ToDouble(CurrentPos[nRPos]);
  270. CHole.Position = holePos;
  271. //默认切割点均参与测试
  272. CHole.SWITCH = true;
  273. //更新切孔链表
  274. this.ListCutHole.Add(CHole);
  275. }
  276. return true;
  277. }
  278. #endregion
  279. }
  280. }