QcmBaseCommon.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. using Core.Mes.Client.Comm.Server;
  2. using Core.Mes.Client.Comm.Tool;
  3. using CoreFS.CA06;
  4. using Infragistics.Win.UltraWinEditors;
  5. using Infragistics.Win.UltraWinGrid;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Text.RegularExpressions;
  12. namespace Core.StlMes.Client.Qcm
  13. {
  14. /// <summary>
  15. /// 公共方法类
  16. /// </summary>
  17. public class QcmBaseCommon
  18. {
  19. /// <summary>
  20. /// 过滤主表中没有但从表有的记录
  21. /// </summary>
  22. /// <param name="dt1">主表</param>
  23. /// <param name="dt2">从表</param>
  24. /// <param name="condition">关联主键</param>
  25. public static DataTable FilterTable(DataTable dt1, DataTable dt2, string condition)
  26. {
  27. if (dt1 != null && dt1.Rows.Count > 0 && dt2 != null)
  28. {
  29. dt2.BeginInit();
  30. for (int i = 0; i < dt2.Rows.Count; i++)
  31. {
  32. string pic = dt2.Rows[i][condition].ToString();
  33. DataRow[] dr = dt1.Select(condition + "='" + pic + "'");
  34. if (dr == null || dr.Length == 0)
  35. dt2.Rows[i].Delete();
  36. }
  37. dt2.EndInit();
  38. dt2.AcceptChanges();
  39. return dt2;
  40. }
  41. return null;
  42. }
  43. /// <summary>
  44. /// 初始化上限符号
  45. /// </summary>
  46. /// <param name="uce">下拉框</param>
  47. public static void InitSignMax(UltraComboEditor uce)
  48. {
  49. DataTable dt = new DataTable();
  50. dt.Columns.Add("STDMAX_SIGN");
  51. DataRow dr1 = dt.NewRow();
  52. dr1["STDMAX_SIGN"] = "<";
  53. DataRow dr2 = dt.NewRow();
  54. dr2["STDMAX_SIGN"] = "<=";
  55. DataRow dr3 = dt.NewRow();
  56. dr3["STDMAX_SIGN"] = "";
  57. dt.Rows.Add(dr1);
  58. dt.Rows.Add(dr2);
  59. dt.Rows.Add(dr3);
  60. uce.DataSource = dt;
  61. uce.DisplayMember = "STDMAX_SIGN";
  62. }
  63. /// <summary>
  64. /// 初始化下限符号
  65. /// </summary>
  66. /// <param name="uce">下拉框</param>
  67. public static void InitSignMin(UltraComboEditor uce)
  68. {
  69. DataTable dt = new DataTable();
  70. dt.Columns.Add("STDMIN_SIGN");
  71. DataRow dr1 = dt.NewRow();
  72. dr1["STDMIN_SIGN"] = ">";
  73. DataRow dr2 = dt.NewRow();
  74. dr2["STDMIN_SIGN"] = ">=";
  75. DataRow dr3 = dt.NewRow();
  76. dr3["STDMIN_SIGN"] = "=";
  77. DataRow dr4 = dt.NewRow();
  78. dr4["STDMIN_SIGN"] = "";
  79. dt.Rows.Add(dr1);
  80. dt.Rows.Add(dr2);
  81. dt.Rows.Add(dr3);
  82. dt.Rows.Add(dr4);
  83. uce.DataSource = dt;
  84. uce.DisplayMember = "STDMIN_SIGN";
  85. }
  86. /// <summary>
  87. /// 让ultraGrid 不可以编辑(排除选择按钮)
  88. /// </summary>
  89. public static void SetUltraGridNoEdit(UltraGrid ultraGrid1)
  90. {
  91. for (int i = 0; i < ultraGrid1.Rows.Count; i++)
  92. {
  93. UltraGridRow ugr = ultraGrid1.Rows[i];
  94. for (int j = 0; j < ugr.Cells.Count; j++)
  95. {
  96. if (!ugr.Cells[j].Column.Key.Equals("CHC"))
  97. ugr.Cells[j].Activation = Activation.ActivateOnly;
  98. }
  99. if (ugr.HasChild())
  100. {
  101. for (int j = 0; j < ugr.ChildBands[0].Rows.Count; j++)
  102. {
  103. for (int k = 0; k < ugr.ChildBands[0].Rows[j].Cells.Count - 1; k++)
  104. {
  105. if (!ugr.ChildBands[0].Rows[j].Cells[k].Column.Key.Equals("CHC"))
  106. ugr.ChildBands[0].Rows[j].Cells[k].Activation = Activation.ActivateOnly;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 颜色区别
  114. /// </summary>
  115. /// <param name="ultraGrid">数据集</param>
  116. /// <param name="columnName">列</param>
  117. /// <param name="columnName">属性</param>
  118. public static void DistinguishColor(UltraGrid ultraGrid, string columnName, string state)
  119. {
  120. UltraGridRow row = null;
  121. UltraGridRow rowChild = null;
  122. for (int i = 0; i < ultraGrid.Rows.Count; i++)
  123. {
  124. row = ultraGrid.Rows[i];
  125. if (!row.Cells[columnName].Value.ToString().Equals(state))
  126. {
  127. row.Appearance.ForeColor = Color.Red;
  128. }
  129. if (row.HasChild())
  130. {
  131. for (int j = 0; j < row.ChildBands[0].Rows.Count; j++)
  132. {
  133. rowChild = row.ChildBands[0].Rows[j];
  134. if (!rowChild.Cells[columnName].Value.ToString().Equals(state))
  135. rowChild.Appearance.ForeColor = Color.Red;
  136. }
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 颜色区别
  142. /// </summary>
  143. /// <param name="ultraGrid">数据集</param>
  144. /// <param name="columnName">列</param>
  145. /// <param name="columnName">属性</param>
  146. public static void SetGridColor(UltraGrid ultraGrid, string columnName, string state)
  147. {
  148. UltraGridRow row = null;
  149. UltraGridRow rowChild = null;
  150. for (int i = 0; i < ultraGrid.Rows.Count; i++)
  151. {
  152. row = ultraGrid.Rows[i];
  153. if (row.Cells[columnName].Value.ToString().Equals(state))
  154. {
  155. row.Appearance.ForeColor = Color.Red;
  156. }
  157. if (row.HasChild())
  158. {
  159. for (int j = 0; j < row.ChildBands[0].Rows.Count; j++)
  160. {
  161. rowChild = row.ChildBands[0].Rows[j];
  162. if (rowChild.Cells[columnName].Value.ToString().Equals(state))
  163. rowChild.Appearance.ForeColor = Color.Red;
  164. }
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// 初始化下拉框
  170. /// </summary>
  171. /// <param name="methodID">方法名</param>
  172. /// <param name="ob">OB对象</param>
  173. /// <param name="uce">下拉框</param>
  174. /// <param name="showValue">显示值</param>
  175. /// <param name="isNull">是否需要空白行</param>
  176. public static void InitDropUltraComEditor(UltraComboEditor uce, string methodID, string showValue, OpeBase ob, Boolean isNull)
  177. {
  178. DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
  179. QcmBaseCommon.InitDropList(uce, dt, showValue, isNull);
  180. ClsBaseInfo.SetComboItemHeight(uce);
  181. }
  182. /// <summary>
  183. /// 初始下拉框
  184. /// </summary>
  185. public static void InitDropList(UltraComboEditor uce, DataTable dt, string showValue, Boolean isNull)
  186. {
  187. if (dt != null && dt.Rows.Count > 0)
  188. {
  189. DataTable dtNext = new DataTable();
  190. dtNext.Columns.Add("ID");
  191. dtNext.Columns.Add("NAME");
  192. if (isNull)
  193. {
  194. DataRow dr = dtNext.NewRow();
  195. dr["ID"] = "";
  196. dr["NAME"] = "";
  197. dtNext.Rows.Add(dr);
  198. }
  199. for (int i = 0; i < dt.Rows.Count; i++)
  200. {
  201. DataRow dr = dtNext.NewRow();
  202. string str = "";
  203. for (int j = 0; j < dt.Columns.Count; j++)
  204. {
  205. if (dt.Columns[j].Caption.Equals(showValue))
  206. dr["NAME"] = dt.Rows[i][j].ToString();
  207. else
  208. {
  209. str = str + "@" + dt.Rows[i][j].ToString();
  210. }
  211. }
  212. dr["ID"] = str.Substring(1);
  213. dtNext.Rows.Add(dr);
  214. }
  215. uce.DataSource = dtNext;
  216. uce.DisplayMember = "NAME";
  217. uce.ValueMember = "ID";
  218. }
  219. }
  220. /// <summary>
  221. /// 初始化下拉框
  222. /// </summary>
  223. /// <param name="uce">下拉框</param>
  224. /// <param name="methodID">方法名</param>
  225. /// <param name="showName">显示值</param>
  226. /// <param name="value">隐藏值</param>
  227. /// <param name="isNull">是否有空行</param>
  228. /// <param name="ob">OB对象</param>
  229. public static void InitDrop(UltraComboEditor uce, string methodID, string showName, string value, Boolean isNull, OpeBase ob)
  230. {
  231. DataTable dt = ServerHelper.GetData(methodID, new Object[] { }, ob);
  232. if (dt != null && dt.Rows.Count > 0 && isNull)
  233. {
  234. DataRow dr = dt.NewRow();
  235. for (int i = 0; i < dt.Columns.Count; i++)
  236. {
  237. dr[i] = "";
  238. }
  239. dt.Rows.InsertAt(dr, 0);
  240. }
  241. uce.DataSource = dt;
  242. uce.DisplayMember = showName;
  243. uce.ValueMember = value;
  244. ClsBaseInfo.SetComboItemHeight(uce);
  245. }
  246. /// <summary>
  247. /// 展开
  248. /// </summary>
  249. /// <param name="expanl"></param>
  250. public static void Expanl(UltraGrid ultraGrid1, ref Dictionary<int, int> expanl)
  251. {
  252. if (expanl.Count == 0)
  253. {
  254. if (ultraGrid1.Rows.Count > 0)
  255. ultraGrid1.DisplayLayout.Rows[0].Activate();
  256. }
  257. else
  258. {
  259. Dictionary<int, int>.KeyCollection keycol = expanl.Keys;
  260. {
  261. foreach (int key in keycol)
  262. {
  263. if (key < ultraGrid1.Rows.Count)
  264. ultraGrid1.Rows[key].Activate();
  265. if (expanl[key] > -1)
  266. {
  267. if (key < ultraGrid1.Rows.Count)
  268. ultraGrid1.Rows[key].ExpandAll();
  269. }
  270. }
  271. }
  272. }
  273. expanl.Clear();
  274. }
  275. /// <summary>
  276. /// 检测公式的合法性(针对表达式是"外径","壁厚","径壁比"的四则运算)
  277. /// </summary>
  278. /// <param name="str">输入的公式</param>
  279. /// <returns>0代表合法,-1代表不合法</returns>
  280. public static int TestFormula(string str)
  281. {
  282. string formula = str.Replace("壁厚", "A"); //壁厚替换为A
  283. formula = formula.Replace("外径", "B"); //外径替换为B
  284. formula = formula.Replace("径壁比", "C"); //径壁比替换为C
  285. formula = formula.Replace("Axc", "D"); //试样面积替换为D
  286. formula = formula.Replace("(", "("); //中文括号替换为英文括号
  287. formula = formula.Replace(")", ")"); //中文括号替换为英文括号
  288. //由于下面的正则表达式有缺陷,这里手动判断括号的数量是否匹配(但不匹配是否成对)
  289. int left = 0;
  290. int right = 0;
  291. for (int i = 0; i < formula.Length; i++)
  292. {
  293. if (formula[i] == '(')
  294. left += 1;
  295. if (formula[i] == ')')
  296. right += 1;
  297. }
  298. if (left != right)
  299. {
  300. return -1;
  301. }
  302. char[] ch = { '(', ')', '+', '-', '*', '/', ' ', '^' }; //切割获取项目,+-*/空格 括号
  303. string[] element = formula.Split(ch, StringSplitOptions.RemoveEmptyEntries); //按ch数组切割,并去掉空格字符串
  304. for (int i = 0; i < element.Length; i++) //判断输入的是否是"外径","壁厚","径壁比"和数字
  305. {
  306. if (element[i] != "A" && element[i] != "B" && element[i] != "C" && element[i] != "D" && !StringUtil.IsInt(element[i]))
  307. {
  308. return -1;
  309. }
  310. }
  311. //输入规则是否符合四则运算规则。有缺陷,"(A+B" "A+B)"下面的正则表达式会认为这两种情况合法
  312. //string reg = @"^(\(*[A-C0-9]+(.[A-C0-9]+)*\)*(\+|-|/|\*))+[A-C0-9]+(.[A-C0-9]+)*\)*$";
  313. string reg = @"^(\(*[A-D0-9]+(.[A-D0-9]+)*\)*(\+|-|/|\*))+[A-D0-9]+(.[A-D0-9]+)*\)*$";
  314. if (Regex.IsMatch(formula, reg))
  315. {
  316. return 0;
  317. }
  318. else
  319. {
  320. return -1;
  321. }
  322. }
  323. /// <summary>
  324. /// ULTRAGRID不可编辑
  325. /// </summary>
  326. /// <param name="ug"></param>
  327. public static void SetNoEdit(UltraGrid ug)
  328. {
  329. if (ug == null || ug.Rows.Count <= 0)
  330. {
  331. return;
  332. }
  333. foreach (UltraGridColumn ugc in ug.DisplayLayout.Bands[0].Columns)
  334. {
  335. if (!ugc.Key.Equals("CHC"))
  336. ugc.CellActivation = Activation.ActivateOnly;
  337. }
  338. }
  339. /// <summary>
  340. /// 检验公式是否合理
  341. /// </summary>
  342. /// <param name="formula">公式字符串</param>
  343. /// <returns>合理返回True,不合理返回False</returns>
  344. public static bool CheckElement(String formula, OpeBase ob)
  345. {
  346. //例:(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
  347. //替换输入法的差异
  348. string str = formula.Replace("?", "?");
  349. str.Replace(":", ":");
  350. str.Replace(">=", ">");
  351. str.Replace("<=", ">");
  352. str.Replace("<", ">");
  353. str.Replace("=", ">");
  354. str.Replace(" ", "");
  355. str.Replace("(", "(");
  356. str.Replace(")", ")");
  357. ArrayList list = CheckFirst(str, ob);
  358. if (list == null)
  359. {
  360. return false;
  361. }
  362. string reStr = formula;
  363. for (int i = 0; i < list.Count; i++)
  364. {
  365. reStr = reStr.Replace(list[i].ToString(), "1");
  366. }
  367. decimal? result;
  368. result = reStr.CompileFormula();
  369. if (result == null)
  370. {
  371. return false;
  372. }
  373. //MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
  374. //sc.Language = "JavaScript";
  375. // try
  376. // {
  377. // double a =double.Parse( sc.Eval(reStr).ToString());
  378. // }
  379. // catch
  380. // {
  381. // return false;
  382. // }
  383. return true;
  384. }
  385. public static ArrayList CheckFirst(string str, OpeBase ob)
  386. {
  387. //为化学公式去除+-*/
  388. string[] strSp = str.Split('?', ':', '>', '+', '-', '/', '*', ')', '(');
  389. ArrayList elements = new ArrayList();
  390. for (int i = 0; i < strSp.Length; i++)
  391. {
  392. //判断是否为数字,如果不是纯数字,则表示是化学元素,添加到ArrayList中,传递到后台。
  393. if (!StringUtil.IsNumber(strSp[i]))
  394. {
  395. if (!elements.Contains(strSp[i]))
  396. {
  397. elements.Add(strSp[i]);
  398. }
  399. }
  400. }
  401. DataTable dt = ServerHelper.GetData("com.steering.pss.qcm.ComBaseQuery.checkElement", new Object[] { elements }, ob);
  402. if (dt != null && dt.Rows.Count > 0)
  403. {
  404. int i = Convert.ToInt32(dt.Rows[0][0].ToString());
  405. if (i != elements.Count)
  406. {
  407. return null;
  408. }
  409. }
  410. return elements;
  411. }
  412. /// <summary>
  413. /// 理化标准界面的公式验证
  414. /// </summary>
  415. /// <param name="formula">输入的公式字符串</param>
  416. /// <returns>是否合法</returns>
  417. public static bool CheckCompositeFormula(string formula)
  418. {
  419. if (formula.Equals(""))
  420. {
  421. return false;
  422. }
  423. //if ()
  424. if (!formula.Contains("D") && !formula.Contains("t") && !formula.Contains("Axc") && !formula.Contains("C")
  425. && !formula.Contains("Rm"))
  426. {
  427. return false;
  428. }
  429. string str = formula.Replace("D", "1");
  430. str = str.Replace("t", "1");
  431. str = str.Replace("Axc", "1");
  432. str = str.Replace("Rm", "1");
  433. str = str.Replace("C", "1");
  434. decimal? result;
  435. result = str.CompileFormula();
  436. if (result == null)
  437. {
  438. return false;
  439. }
  440. //MSScriptControl控件只能在32位系统运行,64位系统不能使用。故使用了上面扩展到string类的方法。
  441. //MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControlClass();
  442. //sc.Language = "JavaScript";
  443. //try
  444. //{
  445. // double a = double.Parse(sc.Eval(str).ToString());
  446. //}
  447. //catch
  448. //{
  449. // return false;
  450. //}
  451. return true;
  452. }
  453. /// <summary>
  454. /// 公差界面的公式验证
  455. /// </summary>
  456. /// <param name="formula">输入的公式</param>
  457. /// <returns>是否合法</returns>
  458. public static bool CheckToleranceFormula(string formula)
  459. {
  460. if (formula.Equals(""))
  461. {
  462. return false;
  463. }
  464. if (!formula.Contains("D") && !formula.Contains("t") && !formula.Contains("L"))
  465. {
  466. return false;
  467. }
  468. string str = formula.Replace("D", "1.0");
  469. str = str.Replace("t", "1.0");
  470. str = str.Replace("L", "1.0");
  471. decimal? result;
  472. result = str.CompileFormula();
  473. if (result == null)
  474. {
  475. return false;
  476. }
  477. return true;
  478. }
  479. }
  480. }