MeasureFile.cs 9.8 KB

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