MeasureFile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. //创建一个新的
  59. return 1;
  60. }
  61. else
  62. {
  63. //创建失败
  64. return 0;
  65. }
  66. }
  67. else
  68. {
  69. //重新创建
  70. XmlManager.CreateXmlFile(this.FileName);
  71. return 2;
  72. }
  73. }
  74. //XML文件保存
  75. //样品孔存储xml文档
  76. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  77. {
  78. Slo sFile = new Slo();
  79. Slo sParam = new Slo();
  80. Collection<CutHole> sCutHoles = new Collection<CutHole>();
  81. foreach (CutHole hole in ListCutHole)
  82. {
  83. sCutHoles.addItem(hole);
  84. }
  85. xString FileName = new xString();
  86. xString FilePath = new xString();
  87. FileName.AssignValue(this.FileName);
  88. FilePath.AssignValue(this.FilePath);
  89. sFile.Register("FileName", FileName);
  90. sFile.Register("FilePath", FilePath);
  91. sFile.Register("Param", this.MParam);
  92. sFile.Register("ListCutHole", sCutHoles);
  93. if (isStoring)
  94. {
  95. sFile.Serialize(true, xml, rootNode);
  96. }
  97. else
  98. {
  99. sFile.Serialize(false, xml, rootNode);
  100. this.FileName = FileName.value();
  101. this.FilePath = FilePath.value();
  102. for (int i = 0; i < sCutHoles.size(); i++)
  103. {
  104. ListCutHole.Add(sCutHoles.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. SaveFileDialog saveFileDialog = new SaveFileDialog();
  146. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  147. {
  148. string a = saveFileDialog.FileName;
  149. //获得文件路径
  150. string localFilePath = saveFileDialog.FileName.ToString();
  151. //获取文件名,不带路径
  152. string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
  153. //获取文件路径,不带文件名
  154. string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
  155. this.FilePath = FilePath;
  156. this.FileName = fileNameExt;
  157. }
  158. //如果是新文件
  159. this.FileName.Trim();
  160. if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)
  161. {
  162. return SaveAs();
  163. }
  164. XmlDocument doc = new XmlDocument();
  165. if (CreateXml() == 0)//创建该文件?
  166. {
  167. return false;
  168. }
  169. if (File.Exists(FileName))
  170. {
  171. doc.Load(this.FileName);//载入xml文件
  172. }
  173. else
  174. {
  175. return SaveAs(); //当前路径不存在
  176. }
  177. XmlNode root = doc.SelectSingleNode("XMLData");
  178. Serialize(true, doc, root);
  179. doc.Save(this.FileName);
  180. return true;
  181. }
  182. //另存为
  183. public bool SaveAs()
  184. {
  185. //打开保存文件的对话框
  186. this.FileName.Trim();
  187. if (!string.IsNullOrEmpty(this.FileName))
  188. {
  189. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  190. }
  191. SaveFileDialog sfd = new SaveFileDialog();
  192. sfd.Title = "保存测量文件";
  193. sfd.Filter = "测量文件(*.msf)|*.msf";
  194. if (Directory.Exists(this.FilePath))
  195. {
  196. sfd.InitialDirectory = this.FilePath;
  197. }
  198. if (sfd.ShowDialog() == DialogResult.OK)
  199. {
  200. this.FileName = sfd.FileName.ToString(); //获得文件路径
  201. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  202. //创建一个新的文件
  203. XmlDocument doc = new XmlDocument();
  204. if (CreateXml() == 0)//创建该文件?
  205. {
  206. return false;
  207. }
  208. doc.Load(this.FileName);
  209. XmlNode root = doc.SelectSingleNode("XMLData");
  210. Serialize(true, doc, root);
  211. doc.Save(this.FileName);
  212. }
  213. return true;
  214. }
  215. //从文件生成切割孔信息
  216. public bool GetCutHolesFromFile(string a_FilePathName)
  217. {
  218. try
  219. {
  220. //清空原文件中的切孔
  221. this.ListCutHole.Clear();
  222. //弹出打开txt文件的对话矿
  223. a_FilePathName.Trim();
  224. if (string.IsNullOrEmpty(a_FilePathName))
  225. {
  226. //新建一个文件对话框
  227. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  228. //设置对话框标题
  229. pOpenFileDialog.Title = "打开电镜位置列表文件";
  230. //设置打开文件类型
  231. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  232. //监测文件是否存在
  233. pOpenFileDialog.CheckFileExists = true;
  234. //文件打开后执行以下程序
  235. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  236. {
  237. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  238. FilePath = a_FilePathName.Substring(0, a_FilePathName.LastIndexOf('\\')+1);
  239. FileName = pOpenFileDialog.SafeFileName;
  240. }
  241. }
  242. //按行取出txt文件
  243. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  244. LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);
  245. //按现有的文件格式生成
  246. string posMode = lines[1];
  247. if (posMode.CompareTo(@"Absolute") != 0)
  248. {
  249. return false;
  250. }
  251. //解析切孔生成带有位置的切孔
  252. //验证数量是否正确
  253. int nNum = Convert.ToInt32(lines[3]);
  254. int nLines = lines.Length;
  255. if (nNum != (lines.Length - 4))
  256. {
  257. return false;
  258. }
  259. //行内标识的个数及位置
  260. string[] titles = lines[2].Split(',');
  261. int numPos = titles.Length;
  262. int nLabelPos = Array.IndexOf(titles, "Label");
  263. int nXPos = Array.IndexOf(titles, "X");
  264. int nYPos = Array.IndexOf(titles, "Y");
  265. int nZPos = Array.IndexOf(titles, "Z");
  266. int nTPos = Array.IndexOf(titles, "T");
  267. int nRPos = Array.IndexOf(titles, "R");
  268. int nMPos = Array.IndexOf(titles, "M");
  269. for (int i = 0; i < nNum; i++)
  270. {
  271. int currentLine = i + 4;
  272. string currentString = lines[currentLine];
  273. LogManager.AddHardwareLog(currentString, true);
  274. string[] CurrentPos = currentString.Split(',');
  275. int nCurrentPosNum = CurrentPos.Length;
  276. //当前行内的标识及位置个数不够
  277. if (nCurrentPosNum != numPos)
  278. {
  279. return false;
  280. }
  281. //切孔标识
  282. CutHole CHole = new CutHole();
  283. CHole.HoleName = CurrentPos[nLabelPos];
  284. //切孔位置
  285. SemPosition holePos = new SemPosition();
  286. holePos.X = Convert.ToSingle(CurrentPos[nXPos]);
  287. holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);
  288. holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);
  289. holePos.M = Convert.ToSingle(CurrentPos[nMPos]);
  290. holePos.T = Convert.ToSingle(CurrentPos[nTPos]);
  291. holePos.R = Convert.ToSingle(CurrentPos[nRPos]);
  292. CHole.Position = holePos;
  293. //默认切割点均参与测试
  294. CHole.SWITCH = true;
  295. //更新切孔链表
  296. this.ListCutHole.Add(CHole);
  297. LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]
  298. + "Y =" + CurrentPos[nYPos]
  299. + "Z =" + CurrentPos[nZPos]
  300. + "M =" + CurrentPos[nTPos]
  301. + "T =" + CurrentPos[nRPos]
  302. + "R =" + CurrentPos[nMPos]
  303. , true);
  304. }
  305. return true;
  306. }
  307. catch (Exception ex)
  308. {
  309. LogManager.LogError(ex.Message);
  310. return false;
  311. }
  312. }
  313. #endregion
  314. }
  315. }