MeasureFile.cs 13 KB

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