123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Linq;
- namespace UIService
- {
- public enum EnumAxisGroupType
- {
- AxisGroupS,
- AxisGroupXy,
- AxisGroupXyz,
- AxisGroupXyzu,
- AxisGroupXyzuvw
- }
- public enum EnumPLCAxisGroupType
- {
- PLCAxisGroupS,
- PLCAxisGroupXy,
- PLCAxisGroupXyz,
- PLCAxisGroupXyzu,
- PLCAxisGroupXyzuvw
- }
- public class MotionCardConfigInfo
- {
- public string CardType { get; set; } = "CardType";
- public string CardNo { get; set; } = "CardNo";
- public string CANCount { get; set; } = "CANCount";
- public string CANBaund { get; set; } = "CANBaund";
- public string FilePath { get; set; } = "FilePath";
- public string Axises { get; set; } = "Axises";
- public string TypeName { get; set; } = "LeadShine";
- }
- public class PLCConfigInfo
- {
- public string PLCModelName { get; set; } = "PLCModelName";
- public string PLCNo { get; set; } = "PLCNo";
- public string CommunicateType { get; set; } = "CommunicateType";
- public string IP { get; set; } = "IP";
- public string Port { get; set; } = "Port";
- public string COM { get; set; } = "COM";
- public string Axises { get; set; } = "Axises";
- }
-
- public class AxisConfigInfo
- {
- public string AxisID { set; get; } = "AxisID";
- public string AxisName { set; get; } = "AxisName";
- public string AxisNo { set; get; } = "AxisNo";
- public string AxisDescribtion { set; get; } = "AxisDescribtion";
- public string CardName { set; get; } = "CardName";
- }
- public class AxisGroupConfigInfo
- {
- public string ID { get; set; } = "ID";
- public string AxisGroupName { get; set; } = "AxisGroupName";
- public string AxisGroupTypeName { get; set; } = "AxisGroupTypeName";
- public List<AxisConfigInfo> ListGroupAxisConfigInfo = new List<AxisConfigInfo>();
-
- }
- public class IOConfigInfo
- {
- public string ID { get; set; } = "ID";
- public string Name { get; set; } = "Name";
- public string CardType { get; set; } = "CardType";
- }
- public class ClsHardConfig
- {
- XmlDocument docXml = new XmlDocument();
- public void LoadXml(string strPath, TreeView treeView)
- {
- if (File.Exists(strPath))
- {
- docXml.Load(strPath); //.TableMananger.xml "TreeXml.xml"
- RecursionTreeControl(docXml.DocumentElement, treeView.Nodes);//将加载完成的XML文件显示在TreeView控件中
- }
-
- }
- /// <summary>
- /// RecursionTreeControl:表示将XML文件的内容显示在TreeView控件中
- /// </summary>
- /// <param name="xmlNode">将要加载的XML文件中的节点元素</param>
- /// <param name="nodes">将要加载的XML文件中的节点集合</param>
- public void RecursionTreeControl(XmlNode xmlNode, TreeNodeCollection nodes)
- {
- foreach (XmlNode node in xmlNode.ChildNodes)//循环遍历当前元素的子元素集合
- {
- TreeNode new_child = new TreeNode();//定义一个TreeNode节点对象
- new_child.Name = node.Attributes["Name"].Value;
- new_child.Text = node.Attributes["Text"].Value;
- nodes.Add(new_child);//向当前TreeNodeCollection集合中添加当前节点
- RecursionTreeControl(node, new_child.Nodes);//调用本方法进行递归
- }
- }
- #region Save
- public void SaveToXml(TreeView treeView,string strPath)
- {
- XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
- XDocument xml = new XDocument(dec);
- XElement root = new XElement("Tree");
- foreach (TreeNode node in treeView.Nodes)
- {
- XElement e = CreateElements(node);
- root.Add(e);
- }
- xml.Add(root);
- xml.Save(strPath);//"TreeXml.xml"
- }
- public XElement CreateElements(TreeNode node)
- {
- XElement root = CreateElement(node);
- foreach (TreeNode n in node.Nodes)
- {
- XElement e = CreateElements(n);
- root.Add(e);
- }
- return root;
- }
- public XElement CreateElement(TreeNode node)
- {
- return new XElement("Node",
- new XAttribute("Name", node.Name),
- new XAttribute("Text", node.Text)
- );
- }
- #endregion
- public List<MotionCardConfigInfo> listMotionCardConfigInfo = new List<MotionCardConfigInfo>();
- public List<AxisConfigInfo> listCardAxisInfos = new List<AxisConfigInfo>();
- public List<AxisGroupConfigInfo> listCardGroupInfo = new List<AxisGroupConfigInfo>();
- public List<IOConfigInfo> listIOConfigInfo = new List<IOConfigInfo>();
- public List<AxisConfigInfo> listPLCAxisInfos = new List<AxisConfigInfo>();
- public List<PLCConfigInfo> listPLCConfigInfos = new List<PLCConfigInfo>();
- public List<AxisGroupConfigInfo> listPLCGroupConfigInfo = new List<AxisGroupConfigInfo>();
- public void ReadXml(string strPath)
- {
- XmlDocument doc = new XmlDocument();
- listCardAxisInfos.Clear();
- listMotionCardConfigInfo.Clear();
- listCardGroupInfo.Clear();
- listIOConfigInfo.Clear();
- listPLCAxisInfos.Clear();
- listPLCConfigInfos.Clear();
- listPLCGroupConfigInfo.Clear();
- doc.Load(strPath);
- XmlNode rootNode = doc.DocumentElement;
- foreach(XmlNode firstNode in rootNode.ChildNodes)//运动控制卡 / PLC /IO
- {
- GetChildNode(firstNode, out string strFirstName, out string strFirstText);
- switch(strFirstName)
- {
- case "运动控制卡":
- foreach (XmlNode secondNode in firstNode.ChildNodes)//LeadShine,APE,轴组
- {
- GetChildNode(secondNode, out string strSecondName, out string strSecondText);
- switch (strSecondName)
- {
- case "LeadShine":
- GetMotionCardAndAxisInfo(secondNode, strSecondName);
- break;
- case "APE":
- GetMotionCardAndAxisInfo(secondNode, strSecondName);
- break;
- case "轴组":
- AddAxisGroupConfigInfo(secondNode, listCardGroupInfo);
- break;
- }
- }
- break;
- case "PLC":
- foreach(XmlNode nodePLCName in firstNode.ChildNodes)//欧姆龙,三菱,轴组
- {
- GetChildNode(nodePLCName, out string strPLCName, out string strPLCText);
- switch(strPLCName)
- {
- case "欧姆龙":
- GetPLCAndAxisInfo(nodePLCName);
- break;
- case "三菱":
- GetPLCAndAxisInfo(nodePLCName);
- break;
- case "轴组":
- AddAxisGroupConfigInfo(nodePLCName, listPLCGroupConfigInfo);
- break;
- }
- }
- break;
- case "IO卡":
- foreach (XmlNode nodeIO in firstNode.ChildNodes)//LeadShine,APE
- {
- GetChildNode(nodeIO, out string strIOName, out string strIOText);
- foreach(XmlNode nodeType in nodeIO)//IOC0640,IOC1280,IOD2424P,
- {
- GetChildNode(nodeType, out string strTypeName, out string strTypeText);
- IOConfigInfo iOConfigInfo = new IOConfigInfo();
- iOConfigInfo.Name = strTypeName;
- iOConfigInfo.CardType = strIOName ;
- iOConfigInfo.ID = nodeType.ChildNodes[0].ChildNodes[0].Attributes["Text"].Value;
- listIOConfigInfo.Add(iOConfigInfo);
- }
- }
- break;
- }
- }
- int a = listMotionCardConfigInfo.Count;
- int b = listCardAxisInfos.Count;
-
- int c = listCardGroupInfo.Count;
- int d = listIOConfigInfo.Count;
- }
- public void GetChildNode(XmlNode xmlNode,out string strName,out string strText)
- {
- strName = xmlNode.Attributes["Name"].Value;
- strText = xmlNode.Attributes["Text"].Value;
- }
- public void AddAxisConfigInfo(XmlNode xmlNode,List<AxisConfigInfo> listAxis)
- {
- foreach (XmlNode chileNode in xmlNode)//Axis[0],Axis[1],Axis[2],Axis[3],
- {
- AxisConfigInfo axisConfigInfo = new AxisConfigInfo();
- foreach (XmlNode detailNode in chileNode)//ID,AxisName,AxisNo,Desc,CardName
- {
- switch (detailNode.Attributes["Name"].Value)
- {
- case "AxisID":
- axisConfigInfo.AxisID = detailNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "AxisName":
- axisConfigInfo.AxisName = detailNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "AxisNo":
- axisConfigInfo.AxisNo = detailNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "AxisDescribtion":
- axisConfigInfo.AxisDescribtion = detailNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "CardName":
- axisConfigInfo.CardName = detailNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- }
- }
- listAxis.Add(axisConfigInfo);
- }
- }
- public void AddAxisGroupConfigInfo(XmlNode nodeGroup, List<AxisGroupConfigInfo> listGroup)
- {
- AxisGroupConfigInfo axisGroupConfigInfo = new AxisGroupConfigInfo();
- foreach (XmlNode groupNode in nodeGroup.ChildNodes)//ID,AxisGroupName,AxisGroupTypeName
- {
- GetChildNode(groupNode, out string strGroupName, out string strGroupText);
- switch (strGroupName)
- {
- case "ID":
- axisGroupConfigInfo.ID = groupNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "AxisGroupName":
- axisGroupConfigInfo.AxisGroupName = groupNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "AxisGroupTypeName":
- XmlNode groupTypeNode = groupNode.ChildNodes[0];//AxisGroupXy,AxisGroupXyz,AxisGroupS,
- GetChildNode(groupTypeNode, out string strTypeName, out string strTypeText);
- axisGroupConfigInfo.AxisGroupTypeName = strTypeName;
- AddAxisConfigInfo(groupTypeNode, axisGroupConfigInfo.ListGroupAxisConfigInfo);
- break;
- }
- }
- listGroup.Add(axisGroupConfigInfo);
- }
- public void GetMotionCardAndAxisInfo(XmlNode secondNode,string strCardTypeName)
- {
- foreach (XmlNode thirdNode in secondNode.ChildNodes)//DMC5400A,DMC5600,DMC5800,DMC5C00
- {
- MotionCardConfigInfo motionCardConfigInfo = new MotionCardConfigInfo();
- GetChildNode(thirdNode, out string strThirdName, out string strThirdText);
- motionCardConfigInfo.CardType = strThirdName;
- motionCardConfigInfo.TypeName = strCardTypeName;
- foreach (XmlNode fourthNode in thirdNode.ChildNodes)//CardNo,CANCount,CANBaund,FilePath,Axises
- {
- GetChildNode(fourthNode, out string strFourthName, out string strFourthText);
- switch (strFourthName)
- {
- case "CardNo":
- motionCardConfigInfo.CardNo = fourthNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "CANCount":
- motionCardConfigInfo.CANCount = fourthNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "CANBaund":
- motionCardConfigInfo.CANBaund = fourthNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "FilePath":
- motionCardConfigInfo.FilePath = fourthNode.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "Axises":
- AddAxisConfigInfo(fourthNode, listCardAxisInfos);
- break;
- }
- }
- listMotionCardConfigInfo.Add(motionCardConfigInfo);
- }
- }
- public void GetPLCAndAxisInfo(XmlNode nodePLCName)
- {
- foreach (XmlNode nodePLCModel in nodePLCName.ChildNodes)//CP,NX,NJ
- {
- PLCConfigInfo plcConfigInfo = new PLCConfigInfo();
- GetChildNode(nodePLCModel, out string strModelName, out string strModelText);
- plcConfigInfo.PLCModelName = strModelName;
- foreach (XmlNode nodePLCConfig in nodePLCModel.ChildNodes)//PLCNo,CommunicateType,IP,Port,COM,Axises
- {
- GetChildNode(nodePLCConfig, out string strConfigName, out string strConfigText);
- switch (strConfigName)
- {
- case "PLCNo":
- plcConfigInfo.PLCNo = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "CommunicateType":
- plcConfigInfo.CommunicateType = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "IP":
- plcConfigInfo.IP = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "Port":
- plcConfigInfo.Port = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "COM":
- plcConfigInfo.COM = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value;
- break;
- case "Axises":
- AddAxisConfigInfo(nodePLCConfig, listPLCAxisInfos);
- break;
- }
- }
- listPLCConfigInfos.Add(plcConfigInfo);
- }
- }
- }
- }
|