using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
namespace MECF.Framework.UI.Core.DxfScript
{
///
/// 名称:xml类
/// 用途:自定义对象保存至xml文件信息/根据xml文件信息 转化为自定义对象加载ui界面
///
public partial class GasDxfDocument
{
///
/// 保存为xml文件
///
public void SaveGas()
{
if (Lines == null || Lines.Count == 0) return;
XmlDocument xmlDoc = new XmlDocument();
try
{
XmlElement root = xmlDoc.CreateElement("GasDxfDocument");
xmlDoc.AppendChild(root);
XmlElement nodeLines = xmlDoc.CreateElement("Lines");
XmlElement nodeCircles = xmlDoc.CreateElement("Circles");
XmlElement nodeFunnels = xmlDoc.CreateElement("Funnels");
XmlElement nodeTexts = xmlDoc.CreateElement("Texts");
XmlElement nodePolyLines = xmlDoc.CreateElement("PolyLines");
XmlElement nodeValves = xmlDoc.CreateElement("Valves");
XmlElement nodeButtons = xmlDoc.CreateElement("Buttons");
XmlElement nodeAnalogs = xmlDoc.CreateElement("Analogs");
XmlElement nodeBoolConditions = xmlDoc.CreateElement("BoolConditions");
XmlElement nodeStringConditions = xmlDoc.CreateElement("StringConditions");
XmlElement nodeClickConditions = xmlDoc.CreateElement("ClickConditions");
XmlElement nodeInitScriptContent = xmlDoc.CreateElement("InitScriptContent");
root.AppendChild(nodeLines);
root.AppendChild(nodeCircles);
root.AppendChild(nodeFunnels);
root.AppendChild(nodeTexts);
root.AppendChild(nodePolyLines);
root.AppendChild(nodeValves);
root.AppendChild(nodeButtons);
root.AppendChild(nodeAnalogs);
root.AppendChild(nodeBoolConditions);
root.AppendChild(nodeStringConditions);
root.AppendChild(nodeClickConditions);
root.AppendChild(nodeInitScriptContent);
foreach (GasLine line in Lines)
{
XmlElement nodeLine = xmlDoc.CreateElement("Line");
SaveLineNodeBase(xmlDoc, nodeLine, line);
nodeLines.AppendChild(nodeLine);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, line);
}
foreach (GasCircle circle in Circles)
{
XmlElement nodeCircle = xmlDoc.CreateElement("Circle");
SaveCircleNodeBase(xmlDoc, nodeCircle, circle);
nodeCircles.AppendChild(nodeCircle);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, circle);
}
foreach (GasFunnel funnel in Funnels)
{
XmlElement nodeFunnel = xmlDoc.CreateElement("Funnel");
SaveFunnelNodeBase(xmlDoc, nodeFunnel, funnel);
nodeFunnels.AppendChild(nodeFunnel);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, funnel);
}
foreach (GasText text in Texts)
{
XmlElement nodeText = xmlDoc.CreateElement("Text");
SaveTextNodeBase(xmlDoc, nodeText, text);
nodeTexts.AppendChild(nodeText);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, text);
}
foreach (GasPolyLine polyLine in PolyLines)
{
XmlElement nodePolyLine = xmlDoc.CreateElement("PolyLine");
SavePolyLineNodeBase(xmlDoc, nodePolyLine, polyLine);
nodePolyLines.AppendChild(nodePolyLine);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, polyLine);
}
foreach (GasAITValve valve in Valves)
{
XmlElement nodeValve = xmlDoc.CreateElement("Valve");
SaveValveNodeBase(xmlDoc, nodeValve, valve);
nodeValves.AppendChild(nodeValve);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, valve);
}
foreach (GasButton button in Buttons)
{
XmlElement nodeButton = xmlDoc.CreateElement("Button");
SaveButtonNodeBase(xmlDoc, nodeButton, button);
nodeButtons.AppendChild(nodeButton);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, button);
}
foreach (GasAnalogControl4Jet analog in Analogs)
{
XmlElement nodeAnalog = xmlDoc.CreateElement("Analog");
SaveAnalogNodeBase(xmlDoc, nodeAnalog, analog);
nodeAnalogs.AppendChild(nodeAnalog);
SaveCondition(xmlDoc, nodeBoolConditions, nodeStringConditions, nodeClickConditions, analog);
}
XmlElement nodeDfxSize = xmlDoc.CreateElement("DxfSize");
nodeDfxSize.SetAttribute("DxfWidth", DxfWidth.ToString());
nodeDfxSize.SetAttribute("DxfHeight", DxfHeight.ToString());
root.AppendChild(nodeDfxSize);
xmlDoc.Save(GasFilename);
IsGasFileModified = false;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Alert", MessageBoxButton.OK);
}
}
///
/// 根据xml文件信息加载ui
///
public void LoadGas()
{
if (IsGasFileModified)
{
MessageBox.Show($"Please save the current file first.");
return;
}
XmlDocument xmlDoc = new XmlDocument();
try
{
ClearAll();
xmlDoc.Load(GasFilename);
XmlNodeList nodeLines = xmlDoc.SelectNodes("/GasDxfDocument/Lines/Line");
if (nodeLines != null)
{
foreach (XmlElement node in nodeLines)
{
GasLine line = LoadLineNodeBase(xmlDoc, node);
Lines.Add(line);
}
}
XmlNodeList nodeCircles = xmlDoc.SelectNodes("GasDxfDocument/Circles/Circle");
if (nodeCircles != null)
{
foreach (XmlElement node in nodeCircles)
{
GasCircle circle = LoadCircleNodeBase(xmlDoc, node);
Circles.Add(circle);
}
}
XmlNodeList nodeFunnels = xmlDoc.SelectNodes("GasDxfDocument/Funnels/Funnel");
if (nodeFunnels != null)
{
foreach (XmlElement node in nodeFunnels)
{
GasFunnel funnel = LoadFunnelNodeBase(xmlDoc, node);
Funnels.Add(funnel);
}
}
XmlNodeList nodeTexts = xmlDoc.SelectNodes("GasDxfDocument/Texts/Text");
if (nodeTexts != null)
{
foreach (XmlElement node in nodeTexts)
{
GasText text = LoadTextNodeBase(xmlDoc, node);
Texts.Add(text);
}
}
XmlNodeList nodePolyLines = xmlDoc.SelectNodes("GasDxfDocument/PolyLines/PolyLine");
if (nodePolyLines != null)
{
foreach (XmlElement node in nodePolyLines)
{
GasPolyLine polyLine = LoadPolyLineNodeBase(xmlDoc, node);
PolyLines.Add(polyLine);
}
}
XmlNodeList nodeValves = xmlDoc.SelectNodes("GasDxfDocument/Valves/Valve");
if (nodeValves != null)
{
foreach (XmlElement node in nodeValves)
{
GasAITValve valve = LoadValveNodeBase(xmlDoc, node);
Valves.Add(valve);
}
}
XmlNodeList nodeButtons = xmlDoc.SelectNodes("GasDxfDocument/Buttons/Button");
if (nodeButtons != null)
{
foreach (XmlElement node in nodeButtons)
{
GasButton button = LoadButtonNodeBase(xmlDoc, node);
Buttons.Add(button);
}
}
XmlNodeList nodeAnalogs = xmlDoc.SelectNodes("GasDxfDocument/Analogs/Analog");
if (nodeAnalogs != null)
{
foreach (XmlElement node in nodeAnalogs)
{
GasAnalogControl4Jet analog = LoadAnalogNodeBase(xmlDoc, node);
Analogs.Add(analog);
}
}
XmlNodeList nodeDxfSizes = xmlDoc.SelectNodes("GasDxfDocument/DxfSize");
foreach (XmlElement node in nodeDxfSizes)
{
DxfWidth = double.Parse(node.GetAttribute("DxfWidth"));
DxfHeight = double.Parse(node.GetAttribute("DxfHeight"));
}
LoadCondition(xmlDoc);
IsGasFileModified = false;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Alert", MessageBoxButton.OK);
}
}
#region dxf信息保存至xml/根据xml文件初始化 ui展示
//save line To xml
private void SaveLineNodeBase(XmlDocument xmlDoc, XmlElement nodeLine, GasLine line)
{
nodeLine.SetAttribute("X1", line.X1.ToString());
nodeLine.SetAttribute("Y1", line.Y1.ToString());
nodeLine.SetAttribute("X2", line.X2.ToString());
nodeLine.SetAttribute("Y2", line.Y2.ToString());
nodeLine.SetAttribute("Id", line.Id.ToString());
nodeLine.SetAttribute("Enable", line.Enable.ToString());
}
//Load line xml To Object
private GasLine LoadLineNodeBase(XmlDocument xmlDoc, XmlElement nodeLine)
{
double x1, y1, x2, y2;
bool enable;
int id;
x1 = double.Parse(nodeLine.GetAttribute("X1"));
y1 = double.Parse(nodeLine.GetAttribute("Y1"));
x2 = double.Parse(nodeLine.GetAttribute("X2"));
y2 = double.Parse(nodeLine.GetAttribute("Y2"));
id = int.Parse(nodeLine.GetAttribute("Id"));
enable = bool.Parse(nodeLine.GetAttribute("Enable"));
GasLine line = new GasLine(x1, y1, x2, y2);
line.Id = id;
line.Enable = enable;
return line;
}
//save circle To xml
private void SaveCircleNodeBase(XmlDocument xmlDoc, XmlElement nodeCircle, GasCircle circle)
{
nodeCircle.SetAttribute("X", circle.X.ToString());
nodeCircle.SetAttribute("Y", circle.Y.ToString());
nodeCircle.SetAttribute("R", circle.R.ToString());
nodeCircle.SetAttribute("StartAngle", circle.StartAngle.ToString());
nodeCircle.SetAttribute("EndAngle", circle.EndAngle.ToString());
nodeCircle.SetAttribute("Id", circle.Id.ToString());
nodeCircle.SetAttribute("Enable", circle.Enable.ToString());
}
//Load circle xml To Object
private GasCircle LoadCircleNodeBase(XmlDocument xmlDoc, XmlElement nodeCircle)
{
double x, y, r, startAngle, endAngle;
bool enable;
int id;
x = double.Parse(nodeCircle.GetAttribute("X"));
y = double.Parse(nodeCircle.GetAttribute("Y"));
r = double.Parse(nodeCircle.GetAttribute("R"));
startAngle = double.Parse(nodeCircle.GetAttribute("StartAngle"));
endAngle = double.Parse(nodeCircle.GetAttribute("EndAngle"));
id = int.Parse(nodeCircle.GetAttribute("Id"));
enable = bool.Parse(nodeCircle.GetAttribute("Enable"));
GasCircle circle = new GasCircle(x, y, r, startAngle, endAngle);
circle.Id = id;
circle.Enable = enable;
return circle;
}
//save funnel To xml
private void SaveFunnelNodeBase(XmlDocument xmlDoc, XmlElement nodeFunnel, GasFunnel funnel)
{
nodeFunnel.SetAttribute("X", funnel.X.ToString());
nodeFunnel.SetAttribute("Y", funnel.Y.ToString());
nodeFunnel.SetAttribute("Z", funnel.Z.ToString());
nodeFunnel.SetAttribute("ScaleX", funnel.ScaleX.ToString());
nodeFunnel.SetAttribute("ScaleY", funnel.ScaleY.ToString());
nodeFunnel.SetAttribute("ScaleZ", funnel.ScaleZ.ToString());
nodeFunnel.SetAttribute("Rotation", funnel.Rotation.ToString());
nodeFunnel.SetAttribute("Id", funnel.Id.ToString());
nodeFunnel.SetAttribute("Enable", funnel.Enable.ToString());
XmlElement xmlElement = xmlDoc.CreateElement("GasLines");
nodeFunnel.AppendChild(xmlElement);
foreach (var line in funnel.GasLines)
{
XmlElement lineElement = xmlDoc.CreateElement("GasLine");
SaveLineNodeBase(xmlDoc, lineElement, line);
xmlElement.AppendChild(lineElement);
}
}
//Load funnel xml To Object
private GasFunnel LoadFunnelNodeBase(XmlDocument xmlDoc, XmlElement nodeFunnel)
{
XmlNodeList gasLinesXml = nodeFunnel.SelectNodes("GasLines/GasLine");
if (gasLinesXml == null) return null;
var x = double.Parse(nodeFunnel.GetAttribute("X"));
var y = double.Parse(nodeFunnel.GetAttribute("Y"));
var z = double.Parse(nodeFunnel.GetAttribute("Z"));
var scaleX = double.Parse(nodeFunnel.GetAttribute("ScaleX"));
var scaleY = double.Parse(nodeFunnel.GetAttribute("ScaleY"));
var scaleZ = double.Parse(nodeFunnel.GetAttribute("ScaleZ"));
var rotation = double.Parse(nodeFunnel.GetAttribute("Rotation"));
var id = int.Parse(nodeFunnel.GetAttribute("Id"));
var enable = bool.Parse(nodeFunnel.GetAttribute("Enable"));
GasFunnel gasFunnel = new GasFunnel(x, y, z, scaleX, scaleY, scaleZ, rotation);
gasFunnel.Id = id;
gasFunnel.Enable = enable;
foreach (XmlElement item in gasLinesXml)
{
GasLine line = LoadLineNodeBase(xmlDoc, item);
if (line == null) continue;
gasFunnel.GasLines.Add(line);
}
return gasFunnel;
}
//save text To xml
private void SaveTextNodeBase(XmlDocument xmlDoc, XmlElement nodeText, GasText text)
{
nodeText.SetAttribute("X", text.X.ToString());
nodeText.SetAttribute("Y", text.Y.ToString());
nodeText.SetAttribute("Text", text.Text.ToString());
nodeText.SetAttribute("RectWidth", text.RectWidth.ToString());
nodeText.SetAttribute("RectHeight", text.RectHeight.ToString());
nodeText.SetAttribute("TextHeight", text.TextHeight.ToString());
nodeText.SetAttribute("LineSpacce", text.LineSpace.ToString());
nodeText.SetAttribute("Alignment", text.Alignment.ToString());
nodeText.SetAttribute("FontSize", text.FontSize.ToString());
nodeText.SetAttribute("Id", text.Id.ToString());
nodeText.SetAttribute("Enable", text.Enable.ToString());
}
//Load text xml To Object
private GasText LoadTextNodeBase(XmlDocument xmlDoc, XmlElement nodeText)
{
double x, y;
bool enable;
int id;
string _text;
x = double.Parse(nodeText.GetAttribute("X"));
y = double.Parse(nodeText.GetAttribute("Y"));
double fontSize = double.Parse(nodeText.GetAttribute("FontSize"));
double rectWidth = double.Parse(nodeText.GetAttribute("RectWidth"));
double rectHeight = double.Parse(nodeText.GetAttribute("RectHeight"));
double textHeight = double.Parse(nodeText.GetAttribute("TextHeight"));
int alignment = int.Parse(nodeText.GetAttribute("Alignment"));
double lineSpace = double.Parse(nodeText.GetAttribute("LineSpacce"));
_text = nodeText.GetAttribute("Text");
id = int.Parse(nodeText.GetAttribute("Id"));
enable = bool.Parse(nodeText.GetAttribute("Enable"));
GasText text = new GasText(x, y, _text);
text.Id = id;
text.Enable = enable;
text.RectWidth = rectWidth;
text.RectHeight = rectHeight;
text.TextHeight = textHeight;
text.LineSpace = lineSpace;
text.FontSize = fontSize;
text.Alignment = alignment;
return text;
}
//save polyLine To xml
private void SavePolyLineNodeBase(XmlDocument xmlDoc, XmlElement nodePolyLine, GasPolyLine polyLine)
{
nodePolyLine.SetAttribute("Id", polyLine.Id.ToString());
nodePolyLine.SetAttribute("Enable", polyLine.Enable.ToString());
XmlElement nodeInnerLines = xmlDoc.CreateElement("InnerLines");
nodePolyLine.AppendChild(nodeInnerLines);
foreach (GasLine innerLine in polyLine.InnerLines)
{
XmlElement nodeInnerLine = xmlDoc.CreateElement("InnerLine");
SaveLineNodeBase(xmlDoc, nodeInnerLine, innerLine);
nodeInnerLines.AppendChild(nodeInnerLine);
}
}
//Load polyLine xml To Object
private GasPolyLine LoadPolyLineNodeBase(XmlDocument xmlDoc, XmlElement nodePolyLine)
{
bool enable;
int id;
XmlNodeList nodeInnerLines = nodePolyLine.SelectNodes("InnerLines/InnerLine");
if (nodeInnerLines == null)
{
return null;
}
GasPolyLine polyLine = new GasPolyLine();
id = int.Parse(nodePolyLine.GetAttribute("Id"));
enable = bool.Parse(nodePolyLine.GetAttribute("Enable"));
polyLine.Id = id;
polyLine.Enable = enable;
foreach (XmlElement nodeInnerLine in nodeInnerLines)
{
GasLine line = LoadLineNodeBase(xmlDoc, nodeInnerLine);
if (line != null)
{
polyLine.InnerLines.Add(line);
}
}
return polyLine;
}
//save button To xml
private void SaveButtonNodeBase(XmlDocument xmlDoc, XmlElement nodeButton, GasButton button)
{
nodeButton.SetAttribute("Id", button.Id.ToString());
nodeButton.SetAttribute("Enable", button.Enable.ToString());
XmlElement nodeInnerPolyLine = xmlDoc.CreateElement("InnerPolyLine");
XmlElement nodeInnerText = xmlDoc.CreateElement("InnerText");
SavePolyLineNodeBase(xmlDoc, nodeInnerPolyLine, button.InnerPolyLine);
SaveTextNodeBase(xmlDoc, nodeInnerText, button.InnerText);
nodeButton.AppendChild(nodeInnerPolyLine);
nodeButton.AppendChild(nodeInnerText);
}
//Load button xml To Object
private GasButton LoadButtonNodeBase(XmlDocument xmlDoc, XmlElement nodeButton)
{
bool enable;
int id;
id = int.Parse(nodeButton.GetAttribute("Id"));
enable = bool.Parse(nodeButton.GetAttribute("Enable"));
GasPolyLine polyLine = LoadPolyLineNodeBase(xmlDoc, nodeButton.SelectSingleNode("InnerPolyLine") as XmlElement);
GasText text = LoadTextNodeBase(xmlDoc, nodeButton.SelectSingleNode("InnerText") as XmlElement);
GasButton button = new GasButton(polyLine, text);
button.Id = id;
button.Enable = enable;
return button;
}
//save valve To xml
private void SaveValveNodeBase(XmlDocument xmlDoc, XmlElement nodeValve, GasAITValve valve)
{
nodeValve.SetAttribute("Id", valve.Id.ToString());
nodeValve.SetAttribute("Enable", valve.Enable.ToString());
XmlElement nodeInnerPolyLine = xmlDoc.CreateElement("InnerPolyLine");
XmlElement nodeInnerText = xmlDoc.CreateElement("InnerText");
XmlElement nodeInnerCircle = xmlDoc.CreateElement("InnerCircle");
if (valve.InnerPolyLine != null)
{
SavePolyLineNodeBase(xmlDoc, nodeInnerPolyLine, valve.InnerPolyLine);
nodeValve.AppendChild(nodeInnerPolyLine);
}
SaveCircleNodeBase(xmlDoc, nodeInnerCircle, valve.InnerCircle);
SaveTextNodeBase(xmlDoc, nodeInnerText, valve.InnerText);
nodeValve.AppendChild(nodeInnerText);
nodeValve.AppendChild(nodeInnerCircle);
}
//Load valve xml To Object
private GasAITValve LoadValveNodeBase(XmlDocument xmlDoc, XmlElement nodeValve)
{
bool enable;
int id;
id = int.Parse(nodeValve.GetAttribute("Id"));
enable = bool.Parse(nodeValve.GetAttribute("Enable"));
XmlElement nodePolyLine = nodeValve.SelectSingleNode("InnerPolyLine") as XmlElement;
GasPolyLine polyLine = null;
if (nodePolyLine != null)
{
polyLine = LoadPolyLineNodeBase(xmlDoc, nodePolyLine);
}
GasText text = LoadTextNodeBase(xmlDoc, nodeValve.SelectSingleNode("InnerText") as XmlElement);
GasCircle circle = LoadCircleNodeBase(xmlDoc, nodeValve.SelectSingleNode("InnerCircle") as XmlElement);
if (polyLine == null || polyLine.InnerLines == null || polyLine.InnerLines.Count == 0)
{
polyLine = null;
}
GasAITValve valve = new GasAITValve(circle, text, polyLine);
valve.Id = id;
valve.Enable = enable;
return valve;
}
//save analog To xml
private void SaveAnalogNodeBase(XmlDocument xmlDoc, XmlElement nodeAnalog, GasAnalogControl4Jet analog)
{
nodeAnalog.SetAttribute("Id", analog.Id.ToString());
nodeAnalog.SetAttribute("Enable", analog.Enable.ToString());
XmlElement nodeTopButton = xmlDoc.CreateElement("TopButton");
XmlElement nodeLeftButton = xmlDoc.CreateElement("LeftButton");
XmlElement nodeRightButton = xmlDoc.CreateElement("RightButton");
XmlElement nodeInnerButton = xmlDoc.CreateElement("InnerButton");
SaveButtonNodeBase(xmlDoc, nodeTopButton, analog.TopButton);
SaveButtonNodeBase(xmlDoc, nodeLeftButton, analog.LeftButton);
SaveButtonNodeBase(xmlDoc, nodeRightButton, analog.RightButton);
SaveButtonNodeBase(xmlDoc, nodeInnerButton, analog.InnerButton);
nodeAnalog.AppendChild(nodeTopButton);
nodeAnalog.AppendChild(nodeLeftButton);
nodeAnalog.AppendChild(nodeRightButton);
nodeAnalog.AppendChild(nodeInnerButton);
}
//Load analog xml To Object
private GasAnalogControl4Jet LoadAnalogNodeBase(XmlDocument xmlDoc, XmlElement nodeAnalog)
{
bool enable;
int id;
id = int.Parse(nodeAnalog.GetAttribute("Id"));
enable = bool.Parse(nodeAnalog.GetAttribute("Enable"));
XmlElement nodeTopButton = nodeAnalog.SelectSingleNode("TopButton") as XmlElement;
XmlElement nodeLeftButton = nodeAnalog.SelectSingleNode("LeftButton") as XmlElement;
XmlElement nodeRightButton = nodeAnalog.SelectSingleNode("RightButton") as XmlElement;
XmlElement nodeInnerButton = nodeAnalog.SelectSingleNode("InnerButton") as XmlElement;
GasButton topButton = LoadButtonNodeBase(xmlDoc, nodeTopButton);
GasButton leftButton = LoadButtonNodeBase(xmlDoc, nodeLeftButton);
GasButton rightButton = LoadButtonNodeBase(xmlDoc, nodeRightButton);
GasButton innerButton = LoadButtonNodeBase(xmlDoc, nodeInnerButton);
GasAnalogControl4Jet analog = new GasAnalogControl4Jet(innerButton, topButton, leftButton, rightButton);
analog.Id = id;
analog.Enable = enable;
return analog;
}
#endregion
}
}