ClsHardConfig.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. namespace UIService
  7. {
  8. public enum EnumAxisGroupType
  9. {
  10. AxisGroupS,
  11. AxisGroupXy,
  12. AxisGroupXyz,
  13. AxisGroupXyzu,
  14. AxisGroupXyzuvw
  15. }
  16. public enum EnumPLCAxisGroupType
  17. {
  18. PLCAxisGroupS,
  19. PLCAxisGroupXy,
  20. PLCAxisGroupXyz,
  21. PLCAxisGroupXyzu,
  22. PLCAxisGroupXyzuvw
  23. }
  24. public class MotionCardConfigInfo
  25. {
  26. public string CardType { get; set; } = "CardType";
  27. public string CardNo { get; set; } = "CardNo";
  28. public string CANCount { get; set; } = "CANCount";
  29. public string CANBaund { get; set; } = "CANBaund";
  30. public string FilePath { get; set; } = "FilePath";
  31. public string Axises { get; set; } = "Axises";
  32. public string TypeName { get; set; } = "LeadShine";
  33. }
  34. public class PLCConfigInfo
  35. {
  36. public string PLCModelName { get; set; } = "PLCModelName";
  37. public string PLCNo { get; set; } = "PLCNo";
  38. public string CommunicateType { get; set; } = "CommunicateType";
  39. public string IP { get; set; } = "IP";
  40. public string Port { get; set; } = "Port";
  41. public string COM { get; set; } = "COM";
  42. public string Axises { get; set; } = "Axises";
  43. }
  44. public class AxisConfigInfo
  45. {
  46. public string AxisID { set; get; } = "AxisID";
  47. public string AxisName { set; get; } = "AxisName";
  48. public string AxisNo { set; get; } = "AxisNo";
  49. public string AxisDescribtion { set; get; } = "AxisDescribtion";
  50. public string CardName { set; get; } = "CardName";
  51. }
  52. public class AxisGroupConfigInfo
  53. {
  54. public string ID { get; set; } = "ID";
  55. public string AxisGroupName { get; set; } = "AxisGroupName";
  56. public string AxisGroupTypeName { get; set; } = "AxisGroupTypeName";
  57. public List<AxisConfigInfo> ListGroupAxisConfigInfo = new List<AxisConfigInfo>();
  58. }
  59. public class IOConfigInfo
  60. {
  61. public string ID { get; set; } = "ID";
  62. public string Name { get; set; } = "Name";
  63. public string CardType { get; set; } = "CardType";
  64. }
  65. public class ClsHardConfig
  66. {
  67. XmlDocument docXml = new XmlDocument();
  68. public void LoadXml(string strPath, TreeView treeView)
  69. {
  70. if (File.Exists(strPath))
  71. {
  72. docXml.Load(strPath); //.TableMananger.xml "TreeXml.xml"
  73. RecursionTreeControl(docXml.DocumentElement, treeView.Nodes);//将加载完成的XML文件显示在TreeView控件中
  74. }
  75. }
  76. /// <summary>
  77. /// RecursionTreeControl:表示将XML文件的内容显示在TreeView控件中
  78. /// </summary>
  79. /// <param name="xmlNode">将要加载的XML文件中的节点元素</param>
  80. /// <param name="nodes">将要加载的XML文件中的节点集合</param>
  81. public void RecursionTreeControl(XmlNode xmlNode, TreeNodeCollection nodes)
  82. {
  83. foreach (XmlNode node in xmlNode.ChildNodes)//循环遍历当前元素的子元素集合
  84. {
  85. TreeNode new_child = new TreeNode();//定义一个TreeNode节点对象
  86. new_child.Name = node.Attributes["Name"].Value;
  87. new_child.Text = node.Attributes["Text"].Value;
  88. nodes.Add(new_child);//向当前TreeNodeCollection集合中添加当前节点
  89. RecursionTreeControl(node, new_child.Nodes);//调用本方法进行递归
  90. }
  91. }
  92. #region Save
  93. public void SaveToXml(TreeView treeView,string strPath)
  94. {
  95. XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
  96. XDocument xml = new XDocument(dec);
  97. XElement root = new XElement("Tree");
  98. foreach (TreeNode node in treeView.Nodes)
  99. {
  100. XElement e = CreateElements(node);
  101. root.Add(e);
  102. }
  103. xml.Add(root);
  104. xml.Save(strPath);//"TreeXml.xml"
  105. }
  106. public XElement CreateElements(TreeNode node)
  107. {
  108. XElement root = CreateElement(node);
  109. foreach (TreeNode n in node.Nodes)
  110. {
  111. XElement e = CreateElements(n);
  112. root.Add(e);
  113. }
  114. return root;
  115. }
  116. public XElement CreateElement(TreeNode node)
  117. {
  118. return new XElement("Node",
  119. new XAttribute("Name", node.Name),
  120. new XAttribute("Text", node.Text)
  121. );
  122. }
  123. #endregion
  124. public List<MotionCardConfigInfo> listMotionCardConfigInfo = new List<MotionCardConfigInfo>();
  125. public List<AxisConfigInfo> listCardAxisInfos = new List<AxisConfigInfo>();
  126. public List<AxisGroupConfigInfo> listCardGroupInfo = new List<AxisGroupConfigInfo>();
  127. public List<IOConfigInfo> listIOConfigInfo = new List<IOConfigInfo>();
  128. public List<AxisConfigInfo> listPLCAxisInfos = new List<AxisConfigInfo>();
  129. public List<PLCConfigInfo> listPLCConfigInfos = new List<PLCConfigInfo>();
  130. public List<AxisGroupConfigInfo> listPLCGroupConfigInfo = new List<AxisGroupConfigInfo>();
  131. public void ReadXml(string strPath)
  132. {
  133. XmlDocument doc = new XmlDocument();
  134. listCardAxisInfos.Clear();
  135. listMotionCardConfigInfo.Clear();
  136. listCardGroupInfo.Clear();
  137. listIOConfigInfo.Clear();
  138. listPLCAxisInfos.Clear();
  139. listPLCConfigInfos.Clear();
  140. listPLCGroupConfigInfo.Clear();
  141. doc.Load(strPath);
  142. XmlNode rootNode = doc.DocumentElement;
  143. foreach(XmlNode firstNode in rootNode.ChildNodes)//运动控制卡 / PLC /IO
  144. {
  145. GetChildNode(firstNode, out string strFirstName, out string strFirstText);
  146. switch(strFirstName)
  147. {
  148. case "运动控制卡":
  149. foreach (XmlNode secondNode in firstNode.ChildNodes)//LeadShine,APE,轴组
  150. {
  151. GetChildNode(secondNode, out string strSecondName, out string strSecondText);
  152. switch (strSecondName)
  153. {
  154. case "LeadShine":
  155. GetMotionCardAndAxisInfo(secondNode, strSecondName);
  156. break;
  157. case "APE":
  158. GetMotionCardAndAxisInfo(secondNode, strSecondName);
  159. break;
  160. case "轴组":
  161. AddAxisGroupConfigInfo(secondNode, listCardGroupInfo);
  162. break;
  163. }
  164. }
  165. break;
  166. case "PLC":
  167. foreach(XmlNode nodePLCName in firstNode.ChildNodes)//欧姆龙,三菱,轴组
  168. {
  169. GetChildNode(nodePLCName, out string strPLCName, out string strPLCText);
  170. switch(strPLCName)
  171. {
  172. case "欧姆龙":
  173. GetPLCAndAxisInfo(nodePLCName);
  174. break;
  175. case "三菱":
  176. GetPLCAndAxisInfo(nodePLCName);
  177. break;
  178. case "轴组":
  179. AddAxisGroupConfigInfo(nodePLCName, listPLCGroupConfigInfo);
  180. break;
  181. }
  182. }
  183. break;
  184. case "IO卡":
  185. foreach (XmlNode nodeIO in firstNode.ChildNodes)//LeadShine,APE
  186. {
  187. GetChildNode(nodeIO, out string strIOName, out string strIOText);
  188. foreach(XmlNode nodeType in nodeIO)//IOC0640,IOC1280,IOD2424P,
  189. {
  190. GetChildNode(nodeType, out string strTypeName, out string strTypeText);
  191. IOConfigInfo iOConfigInfo = new IOConfigInfo();
  192. iOConfigInfo.Name = strTypeName;
  193. iOConfigInfo.CardType = strIOName ;
  194. iOConfigInfo.ID = nodeType.ChildNodes[0].ChildNodes[0].Attributes["Text"].Value;
  195. listIOConfigInfo.Add(iOConfigInfo);
  196. }
  197. }
  198. break;
  199. }
  200. }
  201. int a = listMotionCardConfigInfo.Count;
  202. int b = listCardAxisInfos.Count;
  203. int c = listCardGroupInfo.Count;
  204. int d = listIOConfigInfo.Count;
  205. }
  206. public void GetChildNode(XmlNode xmlNode,out string strName,out string strText)
  207. {
  208. strName = xmlNode.Attributes["Name"].Value;
  209. strText = xmlNode.Attributes["Text"].Value;
  210. }
  211. public void AddAxisConfigInfo(XmlNode xmlNode,List<AxisConfigInfo> listAxis)
  212. {
  213. foreach (XmlNode chileNode in xmlNode)//Axis[0],Axis[1],Axis[2],Axis[3],
  214. {
  215. AxisConfigInfo axisConfigInfo = new AxisConfigInfo();
  216. foreach (XmlNode detailNode in chileNode)//ID,AxisName,AxisNo,Desc,CardName
  217. {
  218. switch (detailNode.Attributes["Name"].Value)
  219. {
  220. case "AxisID":
  221. axisConfigInfo.AxisID = detailNode.ChildNodes[0].Attributes["Text"].Value;
  222. break;
  223. case "AxisName":
  224. axisConfigInfo.AxisName = detailNode.ChildNodes[0].Attributes["Text"].Value;
  225. break;
  226. case "AxisNo":
  227. axisConfigInfo.AxisNo = detailNode.ChildNodes[0].Attributes["Text"].Value;
  228. break;
  229. case "AxisDescribtion":
  230. axisConfigInfo.AxisDescribtion = detailNode.ChildNodes[0].Attributes["Text"].Value;
  231. break;
  232. case "CardName":
  233. axisConfigInfo.CardName = detailNode.ChildNodes[0].Attributes["Text"].Value;
  234. break;
  235. }
  236. }
  237. listAxis.Add(axisConfigInfo);
  238. }
  239. }
  240. public void AddAxisGroupConfigInfo(XmlNode nodeGroup, List<AxisGroupConfigInfo> listGroup)
  241. {
  242. AxisGroupConfigInfo axisGroupConfigInfo = new AxisGroupConfigInfo();
  243. foreach (XmlNode groupNode in nodeGroup.ChildNodes)//ID,AxisGroupName,AxisGroupTypeName
  244. {
  245. GetChildNode(groupNode, out string strGroupName, out string strGroupText);
  246. switch (strGroupName)
  247. {
  248. case "ID":
  249. axisGroupConfigInfo.ID = groupNode.ChildNodes[0].Attributes["Text"].Value;
  250. break;
  251. case "AxisGroupName":
  252. axisGroupConfigInfo.AxisGroupName = groupNode.ChildNodes[0].Attributes["Text"].Value;
  253. break;
  254. case "AxisGroupTypeName":
  255. XmlNode groupTypeNode = groupNode.ChildNodes[0];//AxisGroupXy,AxisGroupXyz,AxisGroupS,
  256. GetChildNode(groupTypeNode, out string strTypeName, out string strTypeText);
  257. axisGroupConfigInfo.AxisGroupTypeName = strTypeName;
  258. AddAxisConfigInfo(groupTypeNode, axisGroupConfigInfo.ListGroupAxisConfigInfo);
  259. break;
  260. }
  261. }
  262. listGroup.Add(axisGroupConfigInfo);
  263. }
  264. public void GetMotionCardAndAxisInfo(XmlNode secondNode,string strCardTypeName)
  265. {
  266. foreach (XmlNode thirdNode in secondNode.ChildNodes)//DMC5400A,DMC5600,DMC5800,DMC5C00
  267. {
  268. MotionCardConfigInfo motionCardConfigInfo = new MotionCardConfigInfo();
  269. GetChildNode(thirdNode, out string strThirdName, out string strThirdText);
  270. motionCardConfigInfo.CardType = strThirdName;
  271. motionCardConfigInfo.TypeName = strCardTypeName;
  272. foreach (XmlNode fourthNode in thirdNode.ChildNodes)//CardNo,CANCount,CANBaund,FilePath,Axises
  273. {
  274. GetChildNode(fourthNode, out string strFourthName, out string strFourthText);
  275. switch (strFourthName)
  276. {
  277. case "CardNo":
  278. motionCardConfigInfo.CardNo = fourthNode.ChildNodes[0].Attributes["Text"].Value;
  279. break;
  280. case "CANCount":
  281. motionCardConfigInfo.CANCount = fourthNode.ChildNodes[0].Attributes["Text"].Value;
  282. break;
  283. case "CANBaund":
  284. motionCardConfigInfo.CANBaund = fourthNode.ChildNodes[0].Attributes["Text"].Value;
  285. break;
  286. case "FilePath":
  287. motionCardConfigInfo.FilePath = fourthNode.ChildNodes[0].Attributes["Text"].Value;
  288. break;
  289. case "Axises":
  290. AddAxisConfigInfo(fourthNode, listCardAxisInfos);
  291. break;
  292. }
  293. }
  294. listMotionCardConfigInfo.Add(motionCardConfigInfo);
  295. }
  296. }
  297. public void GetPLCAndAxisInfo(XmlNode nodePLCName)
  298. {
  299. foreach (XmlNode nodePLCModel in nodePLCName.ChildNodes)//CP,NX,NJ
  300. {
  301. PLCConfigInfo plcConfigInfo = new PLCConfigInfo();
  302. GetChildNode(nodePLCModel, out string strModelName, out string strModelText);
  303. plcConfigInfo.PLCModelName = strModelName;
  304. foreach (XmlNode nodePLCConfig in nodePLCModel.ChildNodes)//PLCNo,CommunicateType,IP,Port,COM,Axises
  305. {
  306. GetChildNode(nodePLCConfig, out string strConfigName, out string strConfigText);
  307. switch (strConfigName)
  308. {
  309. case "PLCNo":
  310. plcConfigInfo.PLCNo = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
  311. break;
  312. case "CommunicateType":
  313. plcConfigInfo.CommunicateType = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
  314. break;
  315. case "IP":
  316. plcConfigInfo.IP = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
  317. break;
  318. case "Port":
  319. plcConfigInfo.Port = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
  320. break;
  321. case "COM":
  322. plcConfigInfo.COM = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
  323. break;
  324. case "Axises":
  325. AddAxisConfigInfo(nodePLCConfig, listPLCAxisInfos);
  326. break;
  327. }
  328. }
  329. listPLCConfigInfos.Add(plcConfigInfo);
  330. }
  331. }
  332. }
  333. }