using Core.Mes.Client.Comm.Server;
using Core.Mes.Client.Comm.Tool;
using CoreFS.CA06;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
namespace Core.StlMes.Client.Qcm
{
///
/// 公共方法类
///
public class QcmBaseCommon
{
///
/// 过滤主表中没有但从表有的记录
///
/// 主表
/// 从表
/// 关联主键
public static DataTable FilterTable(DataTable dt1, DataTable dt2, string condition)
{
if (dt1 != null && dt1.Rows.Count > 0 && dt2 != null)
{
dt2.BeginInit();
for (int i = 0; i < dt2.Rows.Count; i++)
{
string pic = dt2.Rows[i][condition].ToString();
DataRow[] dr = dt1.Select(condition + "='" + pic + "'");
if (dr == null || dr.Length == 0)
dt2.Rows[i].Delete();
}
dt2.EndInit();
dt2.AcceptChanges();
return dt2;
}
return null;
}
///
/// 初始化上限符号
///
/// 下拉框
public static void InitSignMax(UltraComboEditor uce)
{
DataTable dt = new DataTable();
dt.Columns.Add("STDMAX_SIGN");
DataRow dr1 = dt.NewRow();
dr1["STDMAX_SIGN"] = "<";
DataRow dr2 = dt.NewRow();
dr2["STDMAX_SIGN"] = "<=";
DataRow dr3 = dt.NewRow();
dr3["STDMAX_SIGN"] = "";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
uce.DataSource = dt;
uce.DisplayMember = "STDMAX_SIGN";
}
///
/// 初始化下限符号
///
/// 下拉框
public static void InitSignMin(UltraComboEditor uce)
{
DataTable dt = new DataTable();
dt.Columns.Add("STDMIN_SIGN");
DataRow dr1 = dt.NewRow();
dr1["STDMIN_SIGN"] = ">";
DataRow dr2 = dt.NewRow();
dr2["STDMIN_SIGN"] = ">=";
DataRow dr3 = dt.NewRow();
dr3["STDMIN_SIGN"] = "=";
DataRow dr4 = dt.NewRow();
dr4["STDMIN_SIGN"] = "";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
dt.Rows.Add(dr3);
dt.Rows.Add(dr4);
uce.DataSource = dt;
uce.DisplayMember = "STDMIN_SIGN";
}
///
/// 让ultraGrid 不可以编辑(排除选择按钮)
///
public static void SetUltraGridNoEdit(UltraGrid ultraGrid1)
{
for (int i = 0; i < ultraGrid1.Rows.Count; i++)
{
UltraGridRow ugr = ultraGrid1.Rows[i];
for (int j = 0; j < ugr.Cells.Count; j++)
{
if (!ugr.Cells[j].Column.Key.Equals("CHC"))
ugr.Cells[j].Activation = Activation.ActivateOnly;
}
if (ugr.HasChild())
{
for (int j = 0; j < ugr.ChildBands[0].Rows.Count; j++)
{
for (int k = 0; k < ugr.ChildBands[0].Rows[j].Cells.Count - 1; k++)
{
if (!ugr.ChildBands[0].Rows[j].Cells[k].Column.Key.Equals("CHC"))
ugr.ChildBands[0].Rows[j].Cells[k].Activation = Activation.ActivateOnly;
}
}
}
}
}
///
/// 颜色区别
///
/// 数据集
/// 列
/// 属性
public static void DistinguishColor(UltraGrid ultraGrid, string columnName, string state)
{
UltraGridRow row = null;
UltraGridRow rowChild = null;
for (int i = 0; i < ultraGrid.Rows.Count; i++)
{
row = ultraGrid.Rows[i];
if (!row.Cells[columnName].Value.ToString().Equals(state))
{
row.Appearance.ForeColor = Color.Red;
}
if (row.HasChild())
{
for (int j = 0; j < row.ChildBands[0].Rows.Count; j++)
{
rowChild = row.ChildBands[0].Rows[j];
if (!rowChild.Cells[columnName].Value.ToString().Equals(state))
rowChild.Appearance.ForeColor = Color.Red;
}
}
}
}
///
/// 颜色区别
///
/// 数据集
/// 列
/// 属性
public static void SetGridColor(UltraGrid ultraGrid, string columnName, string state)
{
UltraGridRow row = null;
UltraGridRow rowChild = null;
for (int i = 0; i < ultraGrid.Rows.Count; i++)
{
row = ultraGrid.Rows[i];
if (row.Cells[columnName].Value.ToString().Equals(state))
{
row.Appearance.ForeColor = Color.Red;
}
if (row.HasChild())
{
for (int j = 0; j < row.ChildBands[0].Rows.Count; j++)
{
rowChild = row.ChildBands[0].Rows[j];
if (rowChild.Cells[columnName].Value.ToString().Equals(state))
rowChild.Appearance.ForeColor = Color.Red;
}
}
}
}
///
/// 初始化下拉框
///
/// 方法名
/// OB对象
/// 下拉框
/// 显示值
/// 是否需要空白行
public static void InitDropUltraComEditor(UltraComboEditor uce, string methodID, string showValue, OpeBase ob, Boolean isNull)
{
DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
QcmBaseCommon.InitDropList(uce, dt, showValue, isNull);
ClsBaseInfo.SetComboItemHeight(uce);
}
///
/// 初始下拉框
///
public static void InitDropList(UltraComboEditor uce, DataTable dt, string showValue, Boolean isNull)
{
if (dt != null && dt.Rows.Count > 0)
{
DataTable dtNext = new DataTable();
dtNext.Columns.Add("ID");
dtNext.Columns.Add("NAME");
if (isNull)
{
DataRow dr = dtNext.NewRow();
dr["ID"] = "";
dr["NAME"] = "";
dtNext.Rows.Add(dr);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dtNext.NewRow();
string str = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (dt.Columns[j].Caption.Equals(showValue))
dr["NAME"] = dt.Rows[i][j].ToString();
else
{
str = str + "@" + dt.Rows[i][j].ToString();
}
}
dr["ID"] = str.Substring(1);
dtNext.Rows.Add(dr);
}
uce.DataSource = dtNext;
uce.DisplayMember = "NAME";
uce.ValueMember = "ID";
}
}
///
/// 初始化下拉框
///
/// 下拉框
/// 方法名
/// 显示值
/// 隐藏值
/// 是否有空行
/// OB对象
public static void InitDrop(UltraComboEditor uce, string methodID, string showName, string value, Boolean isNull, OpeBase ob)
{
DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
if (dt != null && dt.Rows.Count > 0 && isNull)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
dr[i] = "";
}
dt.Rows.InsertAt(dr, 0);
}
uce.DataSource = dt;
uce.DisplayMember = showName;
uce.ValueMember = value;
ClsBaseInfo.SetComboItemHeight(uce);
}
///
/// 展开
///
///
public static void Expanl(UltraGrid ultraGrid1, ref Dictionary expanl)
{
if (expanl.Count == 0)
{
if (ultraGrid1.Rows.Count > 0)
ultraGrid1.DisplayLayout.Rows[0].Activate();
}
else
{
Dictionary.KeyCollection keycol = expanl.Keys;
{
foreach (int key in keycol)
{
if (key < ultraGrid1.Rows.Count)
ultraGrid1.Rows[key].Activate();
if (expanl[key] > -1)
{
if (key < ultraGrid1.Rows.Count)
ultraGrid1.Rows[key].ExpandAll();
}
}
}
}
expanl.Clear();
}
///
/// 检测公式的合法性(针对表达式是"外径","壁厚","径壁比"的四则运算)
///
/// 输入的公式
/// 0代表合法,-1代表不合法
public static int TestFormula(string str)
{
string formula = str.Replace("壁厚", "A"); //壁厚替换为A
formula = formula.Replace("外径", "B"); //外径替换为B
formula = formula.Replace("径壁比", "C"); //径壁比替换为C
formula = formula.Replace("Axc", "D"); //试样面积替换为D
formula = formula.Replace("(", "("); //中文括号替换为英文括号
formula = formula.Replace(")", ")"); //中文括号替换为英文括号
//由于下面的正则表达式有缺陷,这里手动判断括号的数量是否匹配(但不匹配是否成对)
int left = 0;
int right = 0;
for (int i = 0; i < formula.Length; i++)
{
if (formula[i] == '(')
left += 1;
if (formula[i] == ')')
right += 1;
}
if (left != right)
{
return -1;
}
char[] ch = { '(', ')', '+', '-', '*', '/', ' ', '^' }; //切割获取项目,+-*/空格 括号
string[] element = formula.Split(ch, StringSplitOptions.RemoveEmptyEntries); //按ch数组切割,并去掉空格字符串
for (int i = 0; i < element.Length; i++) //判断输入的是否是"外径","壁厚","径壁比"和数字
{
if (element[i] != "A" && element[i] != "B" && element[i] != "C" && element[i] != "D" && !StringUtil.IsInt(element[i]))
{
return -1;
}
}
//输入规则是否符合四则运算规则。有缺陷,"(A+B" "A+B)"下面的正则表达式会认为这两种情况合法
//string reg = @"^(\(*[A-C0-9]+(.[A-C0-9]+)*\)*(\+|-|/|\*))+[A-C0-9]+(.[A-C0-9]+)*\)*$";
string reg = @"^(\(*[A-D0-9]+(.[A-D0-9]+)*\)*(\+|-|/|\*))+[A-D0-9]+(.[A-D0-9]+)*\)*$";
if (Regex.IsMatch(formula, reg))
{
return 0;
}
else
{
return -1;
}
}
///
/// ULTRAGRID不可编辑
///
///
public static void SetNoEdit(UltraGrid ug)
{
if (ug == null || ug.Rows.Count <= 0)
{
return;
}
foreach (UltraGridColumn ugc in ug.DisplayLayout.Bands[0].Columns)
{
if (!ugc.Key.Equals("CHC"))
ugc.CellActivation = Activation.ActivateOnly;
}
}
///
/// 检验公式是否合理
///
/// 公式字符串
/// 合理返回True,不合理返回False
public static bool CheckElement(String formula, OpeBase ob)
{
//例:(0.21-C)*0.05+1.2>1.65?1.65:(0.21-C)*0.05+1.2 或 (0.21-C)*0.05+0.60
//替换输入法的差异
string str = formula.Replace("?", "?");
str.Replace(":", ":");
str.Replace(">=", ">");
str.Replace("<=", ">");
str.Replace("<", ">");
str.Replace("=", ">");
str.Replace(" ", "");
str.Replace("(", "(");
str.Replace(")", ")");
ArrayList list = CheckFirst(str, ob);
if (list == null)
{
return false;
}
string reStr = formula;
for (int i = 0; i < list.Count; i++)
{
reStr = reStr.Replace(list[i].ToString(), "1");
}
decimal? result;
result = reStr.CompileFormula();
if (result == null)
{
return false;
}
//MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
//sc.Language = "JavaScript";
// try
// {
// double a =double.Parse( sc.Eval(reStr).ToString());
// }
// catch
// {
// return false;
// }
return true;
}
public static ArrayList CheckFirst(string str, OpeBase ob)
{
//为化学公式去除+-*/
string[] strSp = str.Split('?', ':', '>', '+', '-', '/', '*', ')', '(');
ArrayList elements = new ArrayList();
for (int i = 0; i < strSp.Length; i++)
{
//判断是否为数字,如果不是纯数字,则表示是化学元素,添加到ArrayList中,传递到后台。
if (!StringUtil.IsNumber(strSp[i]))
{
if (!elements.Contains(strSp[i]))
{
elements.Add(strSp[i]);
}
}
}
DataTable dt = ServerHelper.GetData("com.steering.pss.qcm.ComBaseQuery.checkElement", new Object[] { elements }, ob);
if (dt != null && dt.Rows.Count > 0)
{
int i = Convert.ToInt32(dt.Rows[0][0].ToString());
if (i != elements.Count)
{
return null;
}
}
return elements;
}
///
/// 理化标准界面的公式验证
///
/// 输入的公式字符串
/// 是否合法
public static bool CheckCompositeFormula(string formula)
{
if (formula.Equals(""))
{
return false;
}
//if ()
if (!formula.Contains("D") && !formula.Contains("t") && !formula.Contains("Axc") && !formula.Contains("C")
&& !formula.Contains("Rm"))
{
return false;
}
string str = formula.Replace("D", "1");
str = str.Replace("t", "1");
str = str.Replace("Axc", "1");
str = str.Replace("Rm", "1");
str = str.Replace("C", "1");
decimal? result;
result = str.CompileFormula();
if (result == null)
{
return false;
}
//MSScriptControl控件只能在32位系统运行,64位系统不能使用。故使用了上面扩展到string类的方法。
//MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
//sc.Language = "JavaScript";
//try
//{
// double a = double.Parse(sc.Eval(str).ToString());
//}
//catch
//{
// return false;
//}
return true;
}
///
/// 公差界面的公式验证
///
/// 输入的公式
/// 是否合法
public static bool CheckToleranceFormula(string formula)
{
if (formula.Equals(""))
{
return false;
}
if (!formula.Contains("D") && !formula.Contains("t") && !formula.Contains("L"))
{
return false;
}
string str = formula.Replace("D", "1.0");
str = str.Replace("t", "1.0");
str = str.Replace("L", "1.0");
decimal? result;
result = str.CompileFormula();
if (result == null)
{
return false;
}
return true;
}
}
}