MeasureFile.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. using System.Drawing;
  14. namespace MeasureData
  15. {
  16. public class MeasureFile : ISlo
  17. {
  18. public const string UNTITLED_FILE_NAME = "Untitled";
  19. #region 内容
  20. //文件名
  21. private string m_fileName;
  22. public string FileName
  23. {
  24. get { return this.m_fileName; }
  25. set { this.m_fileName = value; }
  26. }
  27. //文件路径
  28. private string m_filepath;
  29. public string FilePath
  30. {
  31. get { return this.m_filepath; }
  32. set { this.m_filepath = value; }
  33. }
  34. //切孔文件路径
  35. private string m_CutHoleFilePath;
  36. public string CutHoleFilePath
  37. {
  38. get { return this.m_CutHoleFilePath; }
  39. set { this.m_CutHoleFilePath = value; }
  40. }
  41. //切孔链表
  42. private List<CutHole> m_listCutHole;
  43. public List<CutHole> ListCutHole
  44. {
  45. get { return this.m_listCutHole; }
  46. set { this.m_listCutHole = value; }
  47. }
  48. //测量参数
  49. private MeasureParam m_measureParam;
  50. public MeasureParam MParam
  51. {
  52. get { return this.m_measureParam; }
  53. set { this.m_measureParam = value; }
  54. }
  55. //是否需要保存文件
  56. private bool m_IsModified;
  57. public bool IsModified
  58. {
  59. get { return this.m_IsModified; }
  60. set { this.m_IsModified = value; }
  61. }
  62. #endregion
  63. /// <summary>
  64. /// 创建XML文件
  65. /// </summary>
  66. /// <returns>0:失败;1:成功;2:文件已存在</returns>
  67. public int CreateXml()
  68. {
  69. if (!File.Exists(this.FileName))
  70. {
  71. if (XmlManager.CreateXmlFile(this.FileName))
  72. {
  73. //创建一个新的
  74. return 1;
  75. }
  76. else
  77. {
  78. //创建失败
  79. return 0;
  80. }
  81. }
  82. else
  83. {
  84. //重新创建
  85. XmlManager.CreateXmlFile(this.FileName);
  86. return 2;
  87. }
  88. }
  89. //XML文件保存
  90. //样品孔存储xml文档
  91. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  92. {
  93. Slo sFile = new Slo();
  94. Slo sParam = new Slo();
  95. Collection<CutHole> sCutHoles = new Collection<CutHole>();
  96. foreach (CutHole hole in ListCutHole)
  97. {
  98. sCutHoles.addItem(hole);
  99. }
  100. xString FileName = new xString();
  101. xString FilePath = new xString();
  102. FileName.AssignValue(this.FileName);
  103. FilePath.AssignValue(this.FilePath);
  104. sFile.Register("FileName", FileName);
  105. sFile.Register("FilePath", FilePath);
  106. sFile.Register("Param", this.MParam);
  107. sFile.Register("ListCutHole", sCutHoles);
  108. if (isStoring)
  109. {
  110. sFile.Serialize(true, xml, rootNode);
  111. }
  112. else
  113. {
  114. sFile.Serialize(false, xml, rootNode);
  115. this.FileName = FileName.value();
  116. this.FilePath = FilePath.value();
  117. List<CutHole> tempCutHoleList = new List<CutHole>();
  118. for (int i = 0; i < sCutHoles.size(); i++)
  119. {
  120. tempCutHoleList.Add(sCutHoles.getItem(i));
  121. }
  122. if (tempCutHoleList.Count == sCutHoles.size())
  123. {
  124. ListCutHole.Clear();
  125. ListCutHole = tempCutHoleList;
  126. }
  127. }
  128. }
  129. //构造函数
  130. public MeasureFile()
  131. {
  132. Init();
  133. }
  134. public void Init()
  135. {
  136. this.ListCutHole = new List<CutHole>();
  137. this.FileName = @"";
  138. this.FilePath = @"";
  139. this.MParam = new MeasureParam();
  140. }
  141. #region 操作
  142. //新建
  143. public bool New()
  144. {
  145. int ret = CreateXml();
  146. if (ret > 0)
  147. {
  148. XmlDocument doc = new XmlDocument();
  149. doc.Load(this.FileName);//载入xml文件
  150. XmlNode root = doc.SelectSingleNode("XMLData");
  151. Serialize(true, doc, root);
  152. doc.Save(this.FileName);
  153. }
  154. // 设置路径为初始默认路径
  155. this.FileName = UNTITLED_FILE_NAME;
  156. this.IsModified = false;
  157. // Ok, return TRUE
  158. return true;
  159. }
  160. //打开
  161. public void Open()
  162. {
  163. XmlDocument doc = new XmlDocument();
  164. doc.Load(this.FileName);//载入xml文件
  165. XmlNode root = doc.SelectSingleNode("XMLData");
  166. Serialize(false, doc, root);
  167. this.IsModified = false;
  168. doc.Save(this.FileName);
  169. }
  170. //保存
  171. public bool Save()
  172. {
  173. //没有修改不需要重新保存文件
  174. if (this.IsModified == false)
  175. {
  176. return true;
  177. }
  178. //如果是新文件
  179. this.FileName.Trim();
  180. if (this.FileName == "Untitled")
  181. {
  182. return true;
  183. }
  184. if (string.Compare(this.FileName, UNTITLED_FILE_NAME) == 0)
  185. {
  186. return SaveAs();
  187. }
  188. XmlDocument doc = new XmlDocument();
  189. if (CreateXml() == 0)//创建该文件?
  190. {
  191. return false;
  192. }
  193. if (File.Exists(FileName))
  194. {
  195. doc.Load(this.FileName);//载入xml文件
  196. }
  197. else
  198. {
  199. return SaveAs(); //当前路径不存在
  200. }
  201. XmlNode root = doc.SelectSingleNode("XMLData");
  202. Serialize(true, doc, root);
  203. doc.Save(this.FileName);
  204. return true;
  205. }
  206. //另存为
  207. public bool SaveAs()
  208. {
  209. //打开保存文件的对话框
  210. this.FileName.Trim();
  211. if (!string.IsNullOrEmpty(this.FileName))
  212. {
  213. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  214. }
  215. SaveFileDialog sfd = new SaveFileDialog();
  216. sfd.Title = "保存测量文件";
  217. sfd.InitialDirectory = @"D:\FIB_AUTO\";
  218. sfd.Filter = "测量文件(*.msf)|*.msf";
  219. //if (Directory.Exists(this.FilePath))
  220. //{
  221. // sfd.InitialDirectory = this.FilePath;
  222. //}
  223. if (sfd.ShowDialog() == DialogResult.OK)
  224. {
  225. this.FileName = sfd.FileName.ToString(); //获得文件路径
  226. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  227. //创建一个新的文件
  228. XmlDocument doc = new XmlDocument();
  229. if (CreateXml() == 0)//创建该文件?
  230. {
  231. return false;
  232. }
  233. doc.Load(this.FileName);
  234. XmlNode root = doc.SelectSingleNode("XMLData");
  235. Serialize(true, doc, root);
  236. doc.Save(this.FileName);
  237. return true;
  238. }
  239. else
  240. {
  241. return false;
  242. }
  243. }
  244. //从文件生成切割孔信息
  245. public bool GetCutHolesFromFile(string a_FilePathName)
  246. {
  247. try
  248. {
  249. //清空原文件中的切孔
  250. this.ListCutHole.Clear();
  251. //弹出打开txt文件的对话矿
  252. a_FilePathName.Trim();
  253. if (string.IsNullOrEmpty(a_FilePathName))
  254. {
  255. //新建一个文件对话框
  256. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  257. //设置对话框标题
  258. pOpenFileDialog.Title = "打开电镜位置列表文件";
  259. //设置打开文件类型
  260. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  261. //文件打开路径
  262. pOpenFileDialog.InitialDirectory = @"\\192.168.1.101\Service";
  263. //监测文件是否存在
  264. pOpenFileDialog.CheckFileExists = true;
  265. //文件打开后执行以下程序
  266. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  267. {
  268. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);
  269. //绝对路径
  270. CutHoleFilePath = a_FilePathName;
  271. }
  272. }
  273. //按行取出txt文件
  274. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  275. LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);
  276. //按现有的文件格式生成
  277. string posMode = lines[1];
  278. if (posMode.CompareTo(@"Absolute") != 0)
  279. {
  280. return false;
  281. }
  282. //解析切孔生成带有位置的切孔
  283. //验证数量是否正确
  284. int nNum = Convert.ToInt32(lines[3]);
  285. int nLines = lines.Length;
  286. if (nNum != (lines.Length - 4))
  287. {
  288. return false;
  289. }
  290. //行内标识的个数及位置
  291. string[] titles = lines[2].Split(',');
  292. int numPos = titles.Length;
  293. int nLabelPos = Array.IndexOf(titles, "Label");
  294. int nXPos = Array.IndexOf(titles, "X");
  295. int nYPos = Array.IndexOf(titles, "Y");
  296. int nZPos = Array.IndexOf(titles, "Z");
  297. int nTPos = Array.IndexOf(titles, "T");
  298. int nRPos = Array.IndexOf(titles, "R");
  299. int nMPos = Array.IndexOf(titles, "M");
  300. int nWDPos = Array.IndexOf(titles, "WD");
  301. for (int i = 0; i < nNum; i++)
  302. {
  303. int currentLine = i + 4;
  304. string currentString = lines[currentLine];
  305. LogManager.AddHardwareLog(currentString, true);
  306. string[] CurrentPos = currentString.Split(',');
  307. int nCurrentPosNum = CurrentPos.Length;
  308. //当前行内的标识及位置个数不够
  309. if (nCurrentPosNum != numPos)
  310. {
  311. return false;
  312. }
  313. //切孔标识
  314. CutHole CHole = new CutHole();
  315. CHole.HoleName = CurrentPos[nLabelPos];
  316. //切孔位置
  317. SemPosition holePos = new SemPosition();
  318. holePos.X = Convert.ToSingle(CurrentPos[nXPos]);
  319. holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);
  320. holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);
  321. holePos.M = Convert.ToSingle(CurrentPos[nMPos]);
  322. holePos.T = Convert.ToSingle(CurrentPos[nTPos]);
  323. holePos.R = Convert.ToSingle(CurrentPos[nRPos]);
  324. holePos.WD = Convert.ToSingle(CurrentPos[nWDPos]);
  325. CHole.Position = holePos;
  326. //默认切割点均参与测试
  327. CHole.SWITCH = true;
  328. //更新切孔链表
  329. this.ListCutHole.Add(CHole);
  330. LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]
  331. + "Y =" + CurrentPos[nYPos]
  332. + "Z =" + CurrentPos[nZPos]
  333. + "M =" + CurrentPos[nTPos]
  334. + "T =" + CurrentPos[nRPos]
  335. + "R =" + CurrentPos[nMPos]
  336. , true);
  337. }
  338. this.IsModified = true;
  339. return true;
  340. }
  341. catch (Exception ex)
  342. {
  343. LogManager.LogError(ex.Message);
  344. return false;
  345. }
  346. }
  347. //从点集合对象中生成切割孔信息
  348. public bool GetCutHolesFromAnalysisPosition(List<PointF> cutHolePointF)
  349. {
  350. //集合中的数量
  351. int nNum = Convert.ToInt32(cutHolePointF.Count);
  352. if (nNum == 0)
  353. {
  354. return false;
  355. }
  356. else
  357. {
  358. List<CutHole> tempCutHoleList = new List<CutHole>();
  359. for (int i = 0; i < nNum; i++)
  360. {
  361. //切孔标识
  362. CutHole CHole = new CutHole();
  363. CHole.HoleName = (i + 1).ToString();
  364. //切孔位置
  365. SemPosition holePos = new SemPosition();
  366. holePos.X = Convert.ToSingle(cutHolePointF[i].X);
  367. holePos.Y = Convert.ToSingle(cutHolePointF[i].Y);
  368. holePos.Z = Convert.ToSingle(0);
  369. holePos.M = Convert.ToSingle(0);
  370. holePos.T = Convert.ToSingle(0);
  371. holePos.R = Convert.ToSingle(0);
  372. CHole.Position = holePos;
  373. //默认切割点均参与测试
  374. CHole.SWITCH = true;
  375. //更新切孔链表
  376. tempCutHoleList.Add(CHole);
  377. LogManager.AddHardwareLog("X =" + cutHolePointF[i].X
  378. + "Y =" + cutHolePointF[i].Y
  379. + "Z =" + 0
  380. + "M =" + 0
  381. + "T =" + 0
  382. + "R =" + 0
  383. , true);
  384. }
  385. if (tempCutHoleList.Count > 0)
  386. {
  387. this.ListCutHole.Clear();
  388. this.ListCutHole = tempCutHoleList;
  389. }
  390. }
  391. return true;
  392. }
  393. #endregion
  394. }
  395. }