using MECF.Framework.UI.Core.DxfScript; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace MECF.Framework.UI.Core.DxfScript { /// /// 名称:事件类 /// 用途:BoolConditions/StringConditions 逻辑实现 /// public partial class GasDxfDocument { //解析全部事件 public void ParseConditions() { foreach (var node in BoolConditions) { string boolCondition = node.Value; if (boolCondition == null || boolCondition.Equals("")) { continue; } int id = node.Key; GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.BoolCondition = new Script(boolCondition); } } foreach (var node in StringConditions) { string stringCondition = node.Value; if (stringCondition == null || stringCondition.Equals("")) { continue; } int id = node.Key; GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.StringCondition = new Script(stringCondition); } } foreach (var node in ClickConditions) { string clickCondition = node.Value; if (clickCondition == null || clickCondition.Equals("")) { continue; } int id = node.Key; GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.ClickCondition = new Script(clickCondition); } } } //保存conditions的公共函数 private void SaveCondition(XmlDocument xmlDoc, XmlElement nodeBoolConditions, XmlElement nodeStringConditions, XmlElement nodeClickConditions, GasBaseShape shape) { if (shape.Id > 0) { string boolCondition = null; if (BoolConditions.ContainsKey(shape.Id)) { boolCondition = BoolConditions[shape.Id]; } string stringCondition = null; if (StringConditions.ContainsKey(shape.Id)) { stringCondition = StringConditions[shape.Id]; } string clickCondition = null; if (ClickConditions.ContainsKey(shape.Id)) { clickCondition = ClickConditions[shape.Id]; } if (boolCondition != null && !boolCondition.Equals("")) { XmlElement nodeBoolCondition = xmlDoc.CreateElement("BoolCondition"); nodeBoolCondition.SetAttribute("Id", shape.Id.ToString()); nodeBoolCondition.InnerText = boolCondition; nodeBoolConditions.AppendChild(nodeBoolCondition); } if (stringCondition != null && !stringCondition.Equals("")) { XmlElement nodeStringCondition = xmlDoc.CreateElement("StringCondition"); nodeStringCondition.SetAttribute("Id", shape.Id.ToString()); nodeStringCondition.InnerText = stringCondition; nodeStringConditions.AppendChild(nodeStringCondition); } if (clickCondition != null && !clickCondition.Equals("")) { XmlElement nodeClickCondition = xmlDoc.CreateElement("ClickCondition"); nodeClickCondition.SetAttribute("Id", shape.Id.ToString()); nodeClickCondition.InnerText = clickCondition; nodeClickConditions.AppendChild(nodeClickCondition); } } } private void LoadCondition(XmlDocument xmlDoc) { XmlNodeList nodeBoolConditions = xmlDoc.SelectNodes("GasDxfDocument/BoolConditions/BoolCondition"); foreach (XmlElement nodeBoolCondition in nodeBoolConditions) { int id = int.Parse(nodeBoolCondition.GetAttribute("Id")); if (id > 0) { string boolCondition = nodeBoolCondition.InnerText; if (boolCondition != null && !boolCondition.Equals("")) { BoolConditions.Add(id, boolCondition); } GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.BoolCondition = new Script(boolCondition); } } } XmlNodeList nodeStringConditions = xmlDoc.SelectNodes("GasDxfDocument/StringConditions/StringCondition"); foreach (XmlElement nodeStringCondition in nodeStringConditions) { int id = int.Parse(nodeStringCondition.GetAttribute("Id")); if (id > 0) { string stringCondition = nodeStringCondition.InnerText; if (stringCondition != null && !stringCondition.Equals("")) { StringConditions.Add(id, stringCondition); } GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.StringCondition = new Script(stringCondition); } } } XmlNodeList nodeClickConditions = xmlDoc.SelectNodes("GasDxfDocument/ClickConditions/ClickCondition"); foreach (XmlElement nodeClickCondition in nodeClickConditions) { int id = int.Parse(nodeClickCondition.GetAttribute("Id")); if (id > 0) { string clickCondition = nodeClickCondition.InnerText; if (clickCondition != null && !clickCondition.Equals("")) { ClickConditions.Add(id, clickCondition); } GasBaseShape shape = FindShapeById(id); if (shape != null) { shape.ClickCondition = new Script(clickCondition); } } } } /// /// 清除所有事件 /// public void ClearAll() { ClearDxf(); if (BoolConditions != null) { BoolConditions.Clear(); } else { BoolConditions = new Dictionary(); } if (StringConditions != null) { StringConditions.Clear(); } else { StringConditions = new Dictionary(); } if (ClickConditions != null) { ClickConditions.Clear(); } else { ClickConditions = new Dictionary(); } InitScriptContent = ""; InitScriptExpress = null; IsGasFileModified = false; SelectedShape = null; } //把boolcondition和stringConditon的文本内容更新到字典中,并且刷新shape的脚本对象 public void UpdateBoolCondition(GasBaseShape shape, string boolCondition) { shape.BoolCondition = new Script(boolCondition); if (BoolConditions.ContainsKey(shape.Id)) { BoolConditions[shape.Id] = boolCondition; } else { BoolConditions.Add(shape.Id, boolCondition); } } public void UpdateStringCondition(GasBaseShape shape, string stringCondition) { shape.StringCondition = new Script(stringCondition); if (StringConditions.ContainsKey(shape.Id)) { StringConditions[shape.Id] = stringCondition; } else { StringConditions.Add(shape.Id, stringCondition); } } public void UpdateClickCondition(GasBaseShape shape, string clickCondition) { shape.ClickCondition = new Script(clickCondition); if (ClickConditions.ContainsKey(shape.Id)) { ClickConditions[shape.Id] = clickCondition; } else { ClickConditions.Add(shape.Id, clickCondition); } } public void UpdateInitScript(string content) { InitScriptContent = content; InitScriptExpress = new Script(content); } //根据Id找元件 public GasBaseShape FindShapeById(int id) { switch (id / 1000000) { case (int)GasBaseShape.ShapeType.ACUTE: case (int)GasBaseShape.ShapeType.OBTUSE: case (int)GasBaseShape.ShapeType.FUNNEL: case (int)GasBaseShape.ShapeType.HLINE: case (int)GasBaseShape.ShapeType.POLY: case (int)GasBaseShape.ShapeType.VLINE: //线段 GasLine line = Lines.Find(x => x.Id == id); if (line != null) { return line; } else { GasPolyLine polyLine = PolyLines.Find(x => x.Id == id); if (polyLine != null) { return polyLine; } else { GasButton button = Buttons.Find(x => x.Id == id); if (button != null) { return button; } else { GasAnalogControl4Jet analog = Analogs.Find(x => x.Id == id); if (analog != null) { return analog; } else { return null; } } } } break; case (int)GasBaseShape.ShapeType.CIRCLE: //圆 GasCircle circle = Circles.Find(x => x.Id == id); if (circle != null) { return circle; } else { GasAITValve valve = Valves.Find(x => x.Id == id); if (valve != null) { return valve; } else { return null; } } break; case (int)GasBaseShape.ShapeType.TEXT: //文本 GasText text = Texts.Find(x => x.Id == id); if (text != null) { return text; } else { return null; } break; default: return null; } } //根据位置找元件 public GasBaseShape GetShapeByPosition(double x, double y) { foreach (GasBaseShape shape in Analogs) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Valves) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Buttons) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Texts) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Circles) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Funnels) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in Lines) { if (shape.Contains(x, y)) { return shape; } } foreach (GasBaseShape shape in PolyLines) { if (shape.Contains(x, y)) { return shape; } } return null; } } }