PlanComm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Runtime.Serialization;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using System.Xml;
  14. using System.Data.SqlClient;
  15. using System.Diagnostics;
  16. using System.Reflection; //MISSIN
  17. using Infragistics.Win.UltraWinGrid;
  18. using Infragistics.Win;
  19. using Infragistics.Excel;
  20. using Infragistics.Win.UltraWinDataSource;
  21. using Infragistics.Win.UltraWinEditors;
  22. using Core.Mes.Client.Comm.Server;
  23. using Core.Mes.Client.Comm.Tool;
  24. using Core.StlMes.Client.Mcp.Control;
  25. namespace Core.StlMes.Client.PlnSaleOrd
  26. {
  27. public class PlanComm
  28. {
  29. public static double PI = 3.1415926;
  30. /// <summary>
  31. /// 管米单重计算(t/m)
  32. /// </summary>
  33. /// <param name="outdiameter">外径(mm)</param>
  34. /// <param name="aimwallthick">壁厚(mm)</param>
  35. /// <returns></returns>
  36. public static double WeightOfMi(double outdiameter, double aimwallthick)
  37. {
  38. double weightOfmi = 0.02466 * aimwallthick * (outdiameter - aimwallthick) / 1000;
  39. return weightOfmi;
  40. }
  41. /// <summary>
  42. /// 管坯米单重(t/m)
  43. /// </summary>
  44. /// <param name="outdiameter">管坯断面(mm)</param>
  45. /// <returns></returns>
  46. public static double GpweightOfmi(double outdiameter)
  47. {
  48. return (7.8 * PI / 4 * outdiameter * outdiameter) / 1000 / 1000;
  49. }
  50. /// <summary>
  51. /// 单位英尺长度转米
  52. /// </summary>
  53. /// <param name="lenth">英尺长度</param>
  54. /// <returns>米长度</returns>
  55. public static double FootoMi(double lenth)
  56. {
  57. double converate = 0.3048; //换算率
  58. return Math.Round((lenth * converate), 3);
  59. }
  60. /// <summary>
  61. /// 单位英磅转吨
  62. /// </summary>
  63. /// <param name="weight"></param>
  64. /// <returns></returns>
  65. public static double PoundtoTon(double weight)
  66. {
  67. double converate = 0.45392 / 1000; //换算率
  68. return Math.Round((weight * converate), 3);
  69. }
  70. /// <summary>
  71. /// 设置grid单元格可编辑
  72. /// </summary>
  73. /// <param name="band">grid表格</param>
  74. /// <param name="strs">可编辑的列头名称数组</param>
  75. public static void setGridActivation(UltraGridBand band, params string[] strs)
  76. {
  77. foreach (UltraGridColumn column in band.Columns)
  78. {
  79. if (!strs.Contains(column.Key))
  80. {
  81. column.CellActivation = Activation.ActivateOnly;
  82. }
  83. else
  84. {
  85. column.CellActivation = Activation.AllowEdit;
  86. column.CellAppearance.BackColor = Color.FromArgb(255, 255, 128);
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// 表格下拉框,可以有ValueList来提供,可以选择的样式
  92. /// </summary>
  93. /// <param name="table"></param>
  94. /// <param name="strKey"></param>
  95. /// <param name="strText"></param>
  96. /// <returns></returns>
  97. public static ValueList GeneralValuelist(ref System.Data.DataTable table, string strKey, string strText)
  98. {
  99. if (table == null || !table.Columns.Contains(strKey) || !table.Columns.Contains(strText))
  100. {
  101. return null;
  102. }
  103. ArrayList alist = new ArrayList();
  104. ValueList vlist = new ValueList();
  105. for (int i = 0; i < table.Rows.Count; i++)
  106. {
  107. try
  108. {
  109. if (!alist.Contains(table.Rows[i][strKey]))
  110. {
  111. alist.Add(table.Rows[i][strKey]);
  112. vlist.ValueListItems.Add(table.Rows[i][strKey], Convert.ToString(table.Rows[i][strText]));
  113. }
  114. }
  115. catch { }
  116. }
  117. return vlist;
  118. }
  119. /// <summary>
  120. /// 自定义对象深拷贝
  121. /// </summary>
  122. /// <typeparam name="T">类名</typeparam>
  123. /// <param name="RealObject">源对象</param>
  124. /// <returns></returns>
  125. public static T Clone<T>(T RealObject)
  126. {
  127. using (Stream objectStream = new MemoryStream())
  128. {
  129. //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
  130. IFormatter formatter = new BinaryFormatter();
  131. formatter.Serialize(objectStream, RealObject);
  132. objectStream.Seek(0, SeekOrigin.Begin);
  133. return (T)formatter.Deserialize(objectStream);
  134. }
  135. }
  136. /// <summary>
  137. /// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
  138. /// </summary>
  139. /// <param name="ugrid">UltraGrid</param>
  140. /// <param name="e"></param>
  141. /// <param name="strs">可以进行列名称</param>
  142. public static void setGridCopyActColumn(UltraGrid ugrid, KeyEventArgs e, params string[] strs)
  143. {
  144. if (e.Control && e.KeyCode == Keys.D)
  145. {
  146. if (ugrid != null && ugrid.ActiveRow != null)
  147. {
  148. UltraGridRow ugr = ugrid.ActiveRow;
  149. foreach (string colName in strs)
  150. {
  151. if (ugrid.ActiveCell.Column.Key.Equals(colName))
  152. {
  153. if (ugr.Cells[colName].Activation != Activation.AllowEdit)
  154. {
  155. return;
  156. }
  157. ugrid.UpdateData();
  158. ArrayList list = new ArrayList();
  159. IQueryable<UltraGridRow> checkRows = ugrid.Rows.AsQueryable().Where(" CHECK = 'True' ");
  160. if (checkRows.Count() == 0)
  161. {
  162. return;
  163. }
  164. foreach (UltraGridRow uRow in checkRows)
  165. {
  166. if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
  167. {
  168. uRow.Cells[colName].Value = ugr.Cells[colName].Value;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
  178. /// </summary>
  179. /// <param name="ugrid">UltraGrid</param>
  180. /// <param name="key">选择列名(例如"CHC")</param>
  181. /// <param name="e"></param>
  182. /// <param name="strs">可以进行列名称</param>
  183. public static void setGridCopyActColumn(UltraGrid ugrid, string key, KeyEventArgs e, params string[] strs)
  184. {
  185. if (e.Control && e.KeyCode == Keys.D)
  186. {
  187. if (ugrid != null && ugrid.ActiveRow != null)
  188. {
  189. UltraGridRow ugr = ugrid.ActiveRow;
  190. foreach (string colName in strs)
  191. {
  192. if (ugrid.ActiveCell.Column.Key.Equals(colName))
  193. {
  194. if (ugr.Cells[colName].Activation != Activation.AllowEdit)
  195. {
  196. return;
  197. }
  198. ugrid.UpdateData();
  199. ArrayList list = new ArrayList();
  200. IQueryable<UltraGridRow> checkRows = ugrid.Rows.AsQueryable().Where(key + " = 'True'");
  201. if (checkRows.Count() == 0)
  202. {
  203. return;
  204. }
  205. foreach (UltraGridRow uRow in checkRows)
  206. {
  207. if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
  208. {
  209. uRow.Cells[colName].Value = ugr.Cells[colName].Value;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 返回table的column列中是否存在str
  219. /// </summary>
  220. /// <param name="dt">datable</param>
  221. /// <param name="column">列头名</param>
  222. /// <param name="str">匹配字符串</param>
  223. /// <returns></returns>
  224. public static bool isInDataTable(System.Data.DataTable dt, string column, string str)
  225. {
  226. bool isInTable = false;
  227. if (dt == null || column.Equals(""))
  228. {
  229. return isInTable;
  230. }
  231. if (!dt.Columns.Contains(column))
  232. {
  233. return isInTable;
  234. }
  235. foreach(DataRow dr in dt.Rows)
  236. {
  237. if (dr[column].ToString().Equals(str))
  238. {
  239. isInTable = true;
  240. }
  241. }
  242. return isInTable;
  243. }
  244. /// <summary>
  245. /// 复制DataTable到UltraDataBand
  246. /// </summary>
  247. /// <param name="src">源数据</param>
  248. /// <param name="dest">UltraDataBand</param>
  249. /// <param name="ClearExists">是否清除原数据</param>
  250. public static void CopyDataToDataSource(System.Data.DataTable src, UltraDataBand dest, bool ClearExists)
  251. {
  252. if (src == null || dest == null)
  253. {
  254. return;
  255. }
  256. if (ClearExists)
  257. {
  258. dest.DataSource.Rows.Clear();
  259. }
  260. for (int i = 0; i < src.Rows.Count; i++)
  261. {
  262. DataRow CurRow = src.Rows[i];
  263. object[] cellValues = new object[dest.Columns.Count];
  264. for (int j = 0; j < dest.Columns.Count; j++)
  265. {
  266. if (src.Columns.Contains(dest.Columns[j].Key))
  267. {
  268. cellValues[j] = CurRow[j];
  269. }
  270. else
  271. {
  272. cellValues[j] = null;
  273. }
  274. }
  275. dest.DataSource.Rows.Add(cellValues);
  276. }
  277. }
  278. /// <summary>
  279. /// 设置Grid列是数字类型,显示样式
  280. /// </summary>
  281. /// <param name="band">UltraGridBand</param>
  282. /// <param name="maxDigit">数字最大位数(如:99999.99,最大位数5位)</param>
  283. /// <param name="digit">保留小数位数(如:99999.99,保留2位小数)</param>
  284. /// <param name="strs">数字单元列</param>
  285. public static void setGridDigitalCol(UltraGridBand band, int maxDigit, int digit, params string[] strs)
  286. {
  287. if (band == null)
  288. {
  289. return;
  290. }
  291. string maskInput = "-";
  292. if (maxDigit > 0)
  293. {
  294. string str = "";
  295. for (int i = 0; i < maxDigit; i++)
  296. {
  297. if (i != 0 && (i % 3) == 0)
  298. {
  299. str = "," + str;
  300. }
  301. str = "n" + str;
  302. }
  303. maskInput = maskInput + str;
  304. if (digit > 0)
  305. {
  306. maskInput += ".";
  307. for (int i = 0; i < digit; i++)
  308. {
  309. maskInput += "n";
  310. }
  311. }
  312. foreach (UltraGridColumn column in band.Columns)
  313. {
  314. if (strs.Contains(column.Key))
  315. {
  316. column.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;
  317. column.MaskInput = maskInput;
  318. column.CellAppearance.TextHAlign = HAlign.Right;
  319. if (digit > 0)
  320. {
  321. column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Double;
  322. }
  323. else
  324. {
  325. column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Integer;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. /// <summary>
  332. /// Grid列添加求和统计,设置数字显示样式
  333. /// </summary>
  334. /// <param name="band">UltraGridBand</param>
  335. /// <param name="digit">保留小数位数(如:99999.99,保留2位小数)</param>
  336. /// <param name="strs">添加统计的列头名</param>
  337. public static void setGridSummarySet(UltraGrid grid, int digit, params string[] strs)
  338. {
  339. grid.DisplayLayout.Override.AllowRowSummaries = Infragistics.Win.UltraWinGrid.AllowRowSummaries.Default;
  340. grid.DisplayLayout.Override.AllowRowSummaries = Infragistics.Win.UltraWinGrid.AllowRowSummaries.BasedOnDataType;
  341. for (int i = 0; i < strs.Count(); i++)
  342. {
  343. string columnKey = strs[i];
  344. UltraGridBand band = grid.DisplayLayout.Bands[0];
  345. if (band == null)
  346. {
  347. return;
  348. }
  349. if (!band.Columns.Exists(columnKey))
  350. {
  351. continue;
  352. }
  353. if (band.Summaries.Exists(columnKey))
  354. {
  355. continue;
  356. }
  357. SummarySettings sumColumn = band.Summaries.Add(columnKey, SummaryType.Sum,
  358. band.Columns[columnKey], SummaryPosition.UseSummaryPositionColumn);
  359. sumColumn.DisplayFormat = "{0:N" + digit + "}";
  360. sumColumn.Appearance.TextHAlign = HAlign.Right;
  361. }
  362. grid.DisplayLayout.Override.SummaryFooterCaptionVisible = Infragistics.Win.DefaultableBoolean.False;
  363. }
  364. /// <summary>
  365. /// 等待窗口
  366. /// </summary>
  367. public static void WaitFromOpen(Cursor cursor)
  368. {
  369. cursor = Cursors.WaitCursor; //控制鼠标的样式为等待
  370. if (Constant.WaitingForm == null)
  371. {
  372. Constant.WaitingForm = new WaitingForm();
  373. }
  374. Constant.WaitingForm.ShowToUser = true;
  375. Constant.WaitingForm.Show();
  376. Constant.WaitingForm.Update();
  377. }
  378. /// <summary>
  379. /// 关闭等待
  380. /// </summary>
  381. public static void WaitFromColse(Cursor cursor)
  382. {
  383. cursor = Cursors.Default;
  384. Constant.WaitingForm.ShowToUser = false;
  385. Constant.WaitingForm.Close();
  386. Constant.WaitingForm = null;
  387. }
  388. /// <summary>
  389. //byWenCheng 设置grid的band的列名和描述(列名和描述来自数据库的列名和描述)
  390. /// </summary>
  391. /// <param name="band">需要设置的grid的band</param>
  392. /// <param name="tablename">与band对应的表名</param>
  393. /// <param name="ob">OpenBase对象</param>
  394. public static void SetColandCaption(Infragistics.Win.UltraWinGrid.UltraGridBand band, string tablename,CoreFS.CA06.OpeBase ob)
  395. {
  396. if (band == null)
  397. {
  398. return;
  399. }
  400. System.Data.DataTable dt = GetDataBySql(string.Format(@"select column_name,comments from user_col_comments where table_name = '{0}'", tablename),ob);
  401. if (dt.Rows.Count > 0)
  402. {
  403. foreach (DataRow row in dt.Rows)
  404. {
  405. band.Columns.Add(row[0].ToString(), row[1].ToString());
  406. }
  407. }
  408. }
  409. /// <summary>
  410. ///byWenCheng 执行sql语句
  411. /// </summary>
  412. /// <param name="sqlString"></param>
  413. /// <param name="_ob"></param>
  414. /// <returns></returns>
  415. public static System.Data.DataTable GetDataBySql(string sqlString,CoreFS.CA06.OpeBase _ob)
  416. {
  417. return Core.Mes.Client.Comm.Server.ServerHelper.GetData("com.steering.pss.plnsaleord.ordAmCal.OrderDemo.getDataBySql", new Object[] { sqlString }, _ob);
  418. }
  419. /// <summary>
  420. ///byWenCheng 设置某band的所有列不可编辑,除了ignoreColums里包含的列
  421. /// </summary>
  422. /// <param name="band">需要设置的band</param>
  423. /// <param name="ignoreColums">需要忽略的列</param>
  424. public static void SetGridColNoEdit(Infragistics.Win.UltraWinGrid.UltraGridBand band, string[] ignoreColums)
  425. {
  426. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn col in band.Columns)
  427. {
  428. if (ignoreColums.Length > 0 && !string.IsNullOrEmpty(ignoreColums.FirstOrDefault(c => c == col.Key)))
  429. {
  430. col.CellAppearance.BackColor = System.Drawing.Color.Yellow;
  431. continue;
  432. }
  433. col.CellActivation = Infragistics.Win.UltraWinGrid.Activation.NoEdit;
  434. }
  435. }
  436. /// <summary>
  437. /// byWenCheng,复制数据到数据表,暂未测试速率,目前发现在查询数据时比较耗时,2000条数据2-3s
  438. /// </summary>
  439. /// <param name="srcDt"></param>
  440. /// <param name="desDt"></param>
  441. /// <param name="ClearExsit"></param>
  442. public static void CopyDatabyDatatable(System.Data.DataTable srcDt,System.Data.DataTable desDt, bool ClearExsit)
  443. {
  444. if (srcDt.Rows.Count <= 0)
  445. {
  446. return;
  447. }
  448. if (ClearExsit)
  449. {
  450. desDt.Rows.Clear();
  451. }
  452. foreach (DataRow row in srcDt.Rows)
  453. {
  454. desDt.LoadDataRow(row.ItemArray, false);
  455. }
  456. }
  457. /// <summary>
  458. ///byWenCheng 设置列宽根据内容自适应方法1
  459. /// </summary>
  460. /// <param name="band">需要设置的band</param>
  461. public static void SetColumnAutoFitSize1(UltraGridBand band)
  462. {
  463. if (band != null)
  464. {
  465. band.PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
  466. }
  467. }
  468. /// <summary>
  469. ///byWenCheng 设置列宽根据内容自适应方法2
  470. /// </summary>
  471. /// <param name="band">需要设置的band</param>
  472. public static void SetColumnAutoFitSize2(UltraGridBand band)
  473. {
  474. if (band != null)
  475. {
  476. foreach (UltraGridColumn col in band.Columns)
  477. {
  478. col.PerformAutoResize();
  479. }
  480. }
  481. }
  482. /// <summary>
  483. ///byWenCheng 设置行高根据内容自适应
  484. /// </summary>
  485. /// <param name="grid"></param>
  486. public static void SetRowAutoFitSize(UltraGrid grid)
  487. {
  488. if(grid != null)
  489. {
  490. grid.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
  491. }
  492. }
  493. /// <summary>
  494. ///byWenCheng 获取grid的激活的行,如果该行有对应的父行,则取其父行。和selected选择的行有区别的是,selectedrow是可以有多行,activedrow只能是一行,而且一般是最后选择的那一行
  495. /// </summary>
  496. /// <param name="grid"></param>
  497. /// <returns></returns>
  498. public static UltraGridRow GetActiveRow(UltraGrid grid)
  499. {
  500. if (grid == null || grid.ActiveRow == null)
  501. {
  502. return null;
  503. }
  504. UltraGridRow ugr = grid.ActiveRow;
  505. if (ugr.HasParent())
  506. {
  507. ugr = ugr.ParentRow;
  508. }
  509. return ugr;
  510. }
  511. /// <summary>
  512. /// byWencheng,获取小数点后指定位数的double值
  513. /// </summary>
  514. /// <param name="dsrc">源double</param>
  515. /// <param name="nBit">指定小数点后的位数</param>
  516. /// <returns>结果值</returns>
  517. public static double GetSpecDecimalBit(double dsrc,int nBit)
  518. {
  519. return Convert.ToDouble(dsrc.ToString(string.Format("f{0}",nBit)));
  520. }
  521. }
  522. }