Print.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UAS_MES.DataOperate;
  2. using LabelManager2;
  3. using System.Data;
  4. using System.Text;
  5. using System.IO;
  6. using UAS_MES.Entity;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. namespace UAS_MES.PublicMethod
  10. {
  11. class Print
  12. {
  13. static DataHelper dh = new DataHelper();
  14. //CodeSoft打印的驱动和文件
  15. static Document doc;
  16. ////CodeSoft的打印机
  17. //string CodeSpft_Printer;
  18. static FileInfo info;
  19. public Print() { }
  20. public static bool CodeSoft(string iCaller, Document doc, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  21. {
  22. ErrorMessage = "";
  23. DataTable dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select");
  24. //如果已经打印过了,则不允许再打印
  25. if (dt.Rows.Count > 0)
  26. {
  27. ErrorMessage = SnCode + LabelType + "已打印";
  28. return false;
  29. }
  30. //打开模板路径
  31. //查询模板对应的取值SQL和参数名称
  32. dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select");
  33. StringBuilder sb = new StringBuilder();
  34. if (doc == null)
  35. {
  36. MessageBox.Show("打印文件不存在");
  37. return false;
  38. }
  39. //执行全部的SQL
  40. for (int i = 0; i < dt.Rows.Count; i++)
  41. {
  42. string sql = dt.Rows[i]["lp_sql"].ToString();
  43. try
  44. {
  45. Regex ConnoteA = new Regex("{\\w+}");
  46. foreach (Match mch in ConnoteA.Matches(sql))
  47. {
  48. string x = mch.Value.Trim();
  49. sql = sql.Replace(x, "'" + SnCode + "'");
  50. }
  51. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  52. if (Param.Rows.Count == 0)
  53. continue;
  54. //查询的结果的参数个数大于1需要给标签的多个参数赋值
  55. if (Param.Rows.Count > 0)
  56. {
  57. int LoopTime = Param.Rows.Count > 100 ? 100 : Param.Rows.Count;
  58. for (int j = 0; j < LoopTime; j++)
  59. {
  60. for (int k = 0; k < doc.Variables.FormVariables.Count; k++)
  61. {
  62. if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper())
  63. {
  64. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString();
  65. }
  66. //使用SN开头的参数赋值SN1,SN2,SN3等参数
  67. if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1))
  68. {
  69. doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString();
  70. }
  71. }
  72. }
  73. }
  74. ////否则只查询名称相等的参数赋值
  75. //else
  76. //{
  77. // for (int j = 0; j < doc.Variables.FormVariables.Count; j++)
  78. // {
  79. // if (doc.Variables.FormVariables.Item(j + 1).Name == dt.Rows[i]["lp_name"].ToString())
  80. // {
  81. // doc.Variables.FormVariables.Item(j + 1).Value = Param.Rows[0][0].ToString();
  82. // break;
  83. // }
  84. // }
  85. //}
  86. }
  87. catch (System.Exception ex)
  88. {
  89. MessageBox.Show("SQL维护不正确");
  90. }
  91. }
  92. LogManager.DoLog(sb.ToString());
  93. //保存本次赋值进行打印
  94. //doc.Save();
  95. doc.Printer.SwitchTo(PrinterName);
  96. doc.PrintDocument(PrintNum);
  97. LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
  98. LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode);
  99. return true;
  100. }
  101. }
  102. }