MeasureFile.cs 14 KB

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