using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ICSharpCode.Core; using Mesnac.Basic; namespace Mesnac.Gui.Edit.Dialog { public partial class FrmVar : Form { private string msg = String.Empty; AccidenceAnalyzer express; public FrmVar() { InitializeComponent(); express = new AccidenceAnalyzer(); express.OnAccidenceAnalysis += new AccidenceAnalyzer.AccidenceAnalysis(express_OnAccidenceAnalysis); express.OnCallBack += new AccidenceAnalyzer.CallBack(express_OnCallBack); button2.Enabled = false; button3.Enabled = false; this.InitUIMethod(); } /// /// 初始化界面元素 /// public void InitUIMethod() { this.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Text")); this.label1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_label1")); this.label2.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_label2")); this.label3.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_label3")); this.label4.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_label4")); this.button1.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_button1")); this.button2.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_button2")); this.button3.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK")); this.button4.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel")); this.tabPageAll.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_tabPageAll")); this.tabPageFilter.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_tabPageFilter")); this.listViewFunction.Columns[0].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewFunction_columnHeader1")); this.listViewFunction.Columns[1].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewFunction_columnHeader2")); this.listViewVar.Columns[0].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewVar_columnName")); this.listViewVar.Columns[1].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewVar_columnGroup")); this.listViewVar.Columns[2].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewVar_columnDevice")); this.listViewVar.Columns[3].Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_listViewVar_columnComment")); } //函数传到这个回调函数里 static object express_OnCallBack(string funcName, object[] param, ref bool isOk) { isOk = true; string fun = funcName.ToUpper(); if (fun.Equals("SIN")) { return Math.Sin(Convert.ToDouble(param[0])); } else if (fun.Equals("COS")) { return Math.Cos(Convert.ToDouble(param[0])); } else if (fun.Equals("LOG10")) { return Math.Log10(Convert.ToDouble(param[0])); } else if (fun.Equals("EXP")) { return Math.Exp(Convert.ToDouble(param[0])); } else if (fun.Equals("ASIN")) { return Math.Asin(Convert.ToDouble(param[0])); } else if (fun.Equals("ACOS")) { return Math.Acos(Convert.ToDouble(param[0])); } else if (fun.Equals("ABS")) { return Math.Abs(Convert.ToDecimal(param[0])); } else if (fun.Equals("ATAN")) { return Math.Atan(Convert.ToDouble(param[0])); } else if (fun.Equals("CEILING")) { return Math.Ceiling(Convert.ToDouble(param[0])); } else if (fun.Equals("COSH")) { return Math.Cosh(Convert.ToDouble(param[0])); } else if (fun.Equals("FLOOR")) { return Math.Floor(Convert.ToDouble(param[0])); } else if (fun.Equals("SINH")) { return Math.Sinh(Convert.ToDouble(param[0])); } else if (fun.Equals("SQRT")) { return Math.Sqrt(Convert.ToDouble(param[0])); } else if (fun.Equals("TAN")) { return Math.Tan(Convert.ToDouble(param[0])); } else if (fun.Equals("TANH")) { return Math.Tanh(Convert.ToDouble(param[0])); } else if (fun.Equals("TRUNCATE")) { return Math.Truncate(Convert.ToDouble(param[0])); } isOk = false; return 0; } //变量传到这个回调函数里 static bool express_OnAccidenceAnalysis(ref Operand opd) { ////变量名是value值 if (opd.Type == OperandType.STRING) { Dictionary allEquips = Mesnac.Equips.Factory.Instance.AllEquips; foreach (string key in allEquips.Keys) { Dictionary allGroups = allEquips[key].Group; foreach (string key2 in allGroups.Keys) { Dictionary datas = allGroups[key2].Data; foreach (string key3 in datas.Keys) { if (datas[key3].KeyName.Equals(opd.Value.ToString())) { opd.Type = OperandType.NUMBER; opd.Value = datas[key3].Value; return true; } } } } opd.Type = OperandType.NUMBER; opd.Value = 0; return false; } else { return true; } } private void button2_Click(object sender, EventArgs e) { bool bok = express.Parse(textBox1.Text); if (bok) { //bool ok = false; //object d = express.Evaluate(ref ok); bool ok = true; if (ok) { errorProvider1.SetError((Control)textBox1, ""); //MessageBox.Show(this, "OK!", "信息提示"); button2.Enabled = false; } else { errorProvider1.SetIconAlignment((Control)textBox1, ErrorIconAlignment.MiddleRight); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Msg1")); //有错误,请检查输入表达式或变量名! errorProvider1.SetError((Control)textBox1, this.msg); } } else { errorProvider1.SetIconAlignment((Control)textBox1, ErrorIconAlignment.MiddleRight); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Msg1")); //有错误,请检查输入表达式或变量名! errorProvider1.SetError((Control)textBox1, this.msg); } } public string VarExpress { get { return this.textBox1.Text; } set { this.textBox1.Text = value; } } private void button3_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; } private void button4_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } private void addfun() { int pos = 0; listViewFunction.Items.Clear(); ListViewItem item = new ListViewItem(); item = listViewFunction.Items.Add("Sin"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的正弦值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Sin")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Cos"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的余弦值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Cos")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Log10"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定数字以10为底的对数"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Log10")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Exp"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回e的指定次幂"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Exp")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Asin"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回正弦值为指定数字的角度"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Asin")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Acos"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回余弦值为指定数字的角度"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Acos")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Abs"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定数字的绝对值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Abs")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Atan"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回正切值为指定数字的角度"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Atan")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Ceiling"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回大于或等于该指定双精度浮点数的最小整数"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Ceiling")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Cosh"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的双曲余弦值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Cosh")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Floor"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回小于或等于该指定双精度浮点数的最大整数"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Floor")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Sinh"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的双曲正弦值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Sinh")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Sqrt"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定数字的平方根"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Sqrt")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Tan"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的正切值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Tan")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Tanh"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("返回指定角度的双曲正切值"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Tanh")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("Truncate"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("计算指定双精度浮点数的整数部分"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_Truncate")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("+、-、*、/、%、()"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("算术运算:加、减、乘、除、求余,左右括号"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_SSYSF")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add(">=,>,<,<=,="); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("比较运算:大于等于、大于、小于、小于等于、等于"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_BJYSF")); item.SubItems.Add(this.msg); item = new ListViewItem(); item = listViewFunction.Items.Add("&、|、!、,"); listViewFunction.Items[pos++].ImageIndex = 0; //item.SubItems.Add("逻辑运算:与、或、非、逗号运算"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_LJYSF")); item.SubItems.Add(this.msg); item = new ListViewItem(); //item = listViewFunction.Items.Add("注意:"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_NoteTitle")); item = listViewFunction.Items.Add(this.msg); listViewFunction.Items[pos++].ImageIndex = 1; //item.SubItems.Add("函数内只能是常数或者变量,不能是带变量的表达式,函数参考请参考C#函数语法"); this.msg = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_FrmVar_Fun_NoteContent")); item.SubItems.Add(this.msg); listViewFunction.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); } private void FrmPlcVar_Load(object sender, EventArgs e) { addfun(); this.treeView1.Nodes.Clear(); TreeNode firstLevelNode = null; ListViewItem li = new ListViewItem(); Dictionary allEquips2 = Mesnac.Equips.Factory.Instance.AllConfigEquip; foreach (string key in allEquips2.Keys) { firstLevelNode = new TreeNode(); firstLevelNode.Text = key; Dictionary allGroups2 = allEquips2[key].Group; TreeNode secondLevelNode = null; foreach (string key2 in allGroups2.Keys) { secondLevelNode = new TreeNode(); secondLevelNode.Text = key2; Dictionary datas2 = allGroups2[key2].Data; TreeNode threeLevelNode = null; foreach (string key3 in datas2.Keys) { li.SubItems.Clear(); Mesnac.Equips.BaseInfo.Data data = datas2[key3]; threeLevelNode = new TreeNode(); threeLevelNode.Text = data.KeyName; threeLevelNode.ToolTipText = data.Remark; li.Text = threeLevelNode.Text; li.SubItems.Add(secondLevelNode.Text); li.SubItems.Add(firstLevelNode.Text); listViewVar.Items.Add((ListViewItem)li.Clone()); secondLevelNode.Nodes.Add(threeLevelNode); } firstLevelNode.Nodes.Add(secondLevelNode); } this.treeView1.Nodes.Add(firstLevelNode); } this.treeView1.ExpandAll(); this.tabControlRight.SelectedIndex = 1; // } private void listViewVar_SelectedIndexChanged(object sender, EventArgs e) { } private void listViewVar_MouseDoubleClick(object sender, MouseEventArgs e) { if (listViewVar.SelectedItems.Count == 1) { string name = listViewVar.SelectedItems[0].SubItems[0].Text; VarExpress = name; } } public Color FillColor { get { return label3.BackColor; } set { label3.BackColor = value; } } private bool _b = false; public void SetFillColorVisible(bool b) { _b = b; label3.Enabled = _b; button1.Enabled = _b; label3.Visible = _b; button1.Visible = _b; label2.Visible = _b; } private void button1_Click(object sender, EventArgs e) { ColorDialog dlg = new ColorDialog(); dlg.Color = label3.BackColor; if (dlg.ShowDialog(this) == DialogResult.OK) { label3.BackColor = dlg.Color; } } private void textBox1_TextChanged(object sender, EventArgs e) { button3.Enabled = true; button2.Enabled = true; } /// /// 过滤处理 /// /// /// private void txtFilter_TextChanged(object sender, EventArgs e) { string filterName = this.txtFilter.Text.Trim(); //要过滤的变量名 this.treeView1.Nodes.Clear(); TreeNode firstLevelNode = null; Dictionary allEquips2 = Mesnac.Equips.Factory.Instance.AllConfigEquip; foreach (string key in allEquips2.Keys) { firstLevelNode = new TreeNode(); firstLevelNode.Text = key; firstLevelNode.ToolTipText = key; Dictionary allGroups2 = allEquips2[key].Group; TreeNode secondLevelNode = null; foreach (string key2 in allGroups2.Keys) { secondLevelNode = new TreeNode(); secondLevelNode.Text = key2; secondLevelNode.ToolTipText = key2; Dictionary datas2 = allGroups2[key2].Data; TreeNode threeLevelNode = null; foreach (string key3 in datas2.Keys) { Mesnac.Equips.BaseInfo.Data data = datas2[key3]; if (String.IsNullOrWhiteSpace(filterName) || data.KeyName.ToLower().Contains(filterName.ToLower())) { threeLevelNode = new TreeNode(); threeLevelNode.Text = data.KeyName; threeLevelNode.ToolTipText = data.Remark; secondLevelNode.Nodes.Add(threeLevelNode); } } firstLevelNode.Nodes.Add(secondLevelNode); } this.treeView1.Nodes.Add(firstLevelNode); } this.treeView1.ExpandAll(); } private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { //判断是否为变量级别 if (e.Node.Level == 2) { string name = e.Node.Text; VarExpress = name; } } } }