using UAS_MES_NEW.DataOperate; using LabelManager2; using System.Data; using System.Text; using System.IO; using UAS_MES_NEW.Entity; using System.Text.RegularExpressions; using System.Windows.Forms; namespace UAS_MES_NEW.PublicMethod { class Print { static DataHelper dh = SystemInf.dh; //CodeSoft打印的驱动和文件 static Document doc; ////CodeSoft的打印机 //string CodeSpft_Printer; // static LabelFormatDocument format; static BarTender.Format doc2; public Print() { } public static bool SinglePrint(string iCaller, BarTender.Application lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage) { ErrorMessage = ""; DataTable dt = new DataTable(); if (IfRePrint != "-1") { if (LabelType == "卡通箱标" || LabelType == "彩盒标" || LabelType == "栈板标" || LabelType == "机身标") { dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select"); } else { 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"); } //////如果已经打印过了,则不允许再打印 //if (dt.Rows.Count > 0) //{ // ErrorMessage = SnCode + LabelType + "已打印"; // return false; //} } // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString(); // FileInfo PrintFile = new FileInfo(LabelName); //打开模板路径 //查询模板对应的取值SQL和参数名称 dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select"); //StringBuilder sb = new StringBuilder(); //if (!PrintFile.Exists) //{ // MessageBox.Show("打印文件不存在"); // return false; //} //string filechangetime = PrintFile.LastWriteTime.ToString(); //if (filechangetime != filelastwritetime) //{ // lbl.Dispose(); // lbl = new Engine(); // // BaseUtil.WriteLbl(); // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')"; // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'"); //} doc2 = lbl.Formats.Open(LabelName); if (doc2 == null) { MessageBox.Show("标签文件打开失败"); return false; } //执行全部的SQL for (int i = 0; i < dt.Rows.Count; i++) { string sql = dt.Rows[i]["lp_sql"].ToString(); try { Regex ConnoteA = new Regex("{\\w+}"); foreach (Match mch in ConnoteA.Matches(sql)) { string x = mch.Value.Trim(); sql = sql.Replace(x, "'" + SnCode + "'"); } DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); if (Param.Rows.Count == 0) continue; //查询的结果的参数个数大于1需要给标签的多个参数赋值 if (Param.Rows.Count > 0) { int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count; for (int j = 0; j < LoopTime; j++) { for (int k = 0; k < doc2.NamedSubStrings.Count; k++) { if (j == 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper()) { doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[0][0].ToString()); } //使用SN开头的参数赋值SN1,SN2,SN3等参数 //if (j != 0 & doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j)) //{ // doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString()); //} if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j+1)) { doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, Param.Rows[j][0].ToString()); } } } } } catch (System.Exception ex) { MessageBox.Show("SQL维护不正确" + ex.Message); } } // LogManager.DoLog(sb.ToString()); //保存本次赋值进行打印 // doc2.Printer.SwitchTo(PrinterName); // 同样标签的份数 doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum; // 序列标签数 doc2.PrintSetup.NumberSerializedLabels = 1; doc2.PrintSetup.Printer = PrinterName; doc2.PrintOut(false, false); doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, ""); LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode); //打印完毕 lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges); return true; } 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) { ErrorMessage = ""; DataTable dt = new DataTable(); if (IfRePrint != "-1") { if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标") { dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select"); } else { 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"); } ////如果已经打印过了,则不允许再打印 if (dt.Rows.Count > 0) { ErrorMessage = SnCode + LabelType + "已打印"; return false; } } //打开模板路径 //查询模板对应的取值SQL和参数名称 dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select"); StringBuilder sb = new StringBuilder(); if (doc == null) { MessageBox.Show("打印文件不存在"); return false; } //执行全部的SQL for (int i = 0; i < dt.Rows.Count; i++) { string sql = dt.Rows[i]["lp_sql"].ToString(); try { Regex ConnoteA = new Regex("{\\w+}"); foreach (Match mch in ConnoteA.Matches(sql)) { string x = mch.Value.Trim(); sql = sql.Replace(x, "'" + SnCode + "'"); } DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); if (Param.Rows.Count == 0) continue; //查询的结果的参数个数大于1需要给标签的多个参数赋值 if (Param.Rows.Count > 0) { int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count; for (int j = 0; j < LoopTime; j++) { for (int k = 0; k < doc.Variables.FormVariables.Count; k++) { if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper()) { doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString(); } //使用SN开头的参数赋值SN1,SN2,SN3等参数 if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)) { doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString(); } } } } } catch (System.Exception) { MessageBox.Show("SQL维护不正确"); } } LogManager.DoLog(sb.ToString()); //保存本次赋值进行打印 doc.Printer.SwitchTo(PrinterName); doc.PrintDocument(PrintNum); LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, ""); for (int k = 0; k < doc.Variables.FormVariables.Count; k++) { doc.Variables.FormVariables.Item(k + 1).Value = null; } LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode); return true; } public static bool CodeSoft(string iCaller, ref ApplicationClass lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage) { ErrorMessage = ""; DataTable dt = new DataTable(); if (IfRePrint != "-1") { if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标") { dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select"); } else { dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_makecode='" + MakeCode + "' and lpl_type='" + LabelType + LabelName + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select"); } ////如果已经打印过了,则不允许再打印 //if (dt.Rows.Count > 0) //{ // ErrorMessage = SnCode + LabelType + "已打印"; // return false; //} } string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString(); FileInfo PrintFile = new FileInfo(LabelName); //打开模板路径 //查询模板对应的取值SQL和参数名称 dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "' order by to_number(lp_detno)", "select"); StringBuilder sb = new StringBuilder(); if (!PrintFile.Exists) { MessageBox.Show("打印文件不存在"); return false; } string filechangetime = PrintFile.LastWriteTime.ToString(); if (filechangetime != filelastwritetime) { lbl.Quit(); lbl = new ApplicationClass(); BaseUtil.WriteLbl(); filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')"; dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'"); } doc = lbl.Documents.Open(LabelName, true); if (doc == null) { MessageBox.Show("标签文件打开失败"); return false; } //执行全部的SQL for (int i = 0; i < dt.Rows.Count; i++) { string sql = dt.Rows[i]["lp_sql"].ToString(); try { Regex ConnoteA = new Regex("{\\w+}"); foreach (Match mch in ConnoteA.Matches(sql)) { string x = mch.Value.Trim(); sql = sql.Replace(x, "'" + SnCode + "'"); } DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); if (Param.Rows.Count == 0) continue; //查询的结果的参数个数大于1需要给标签的多个参数赋值 if (Param.Rows.Count > 0) { int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count; for (int j = 0; j < LoopTime; j++) { for (int k = 0; k < doc.Variables.FormVariables.Count; k++) { if (j == 0 & doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper()) { doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[0][0].ToString(); } //使用SN开头的参数赋值SN1,SN2,SN3等参数 if (doc.Variables.FormVariables.Item(k + 1).Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)) { doc.Variables.FormVariables.Item(k + 1).Value = Param.Rows[j][0].ToString(); } } } } } catch (System.Exception) { MessageBox.Show("SQL维护不正确"); } } LogManager.DoLog(sb.ToString()); //保存本次赋值进行打印 doc.Printer.SwitchTo(PrinterName); doc.PrintDocument(PrintNum); LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, ""); for (int k = 0; k < doc.Variables.FormVariables.Count; k++) { doc.Variables.FormVariables.Item(k + 1).Value = null; } LogicHandler.doLabelPrintLog(SnCode, LabelType + LabelName, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode); return true; } // public static bool BarTender(string iCaller, ref Engine lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage) // { // ErrorMessage = ""; // DataTable dt = new DataTable(); // //if (IfRePrint != "-1") // //{ // // if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "栈板标") // // { // // dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "'", "select"); // // } // // else // // { // // 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"); // // } // // ////如果已经打印过了,则不允许再打印 // // if (dt.Rows.Count > 0) // // { // // ErrorMessage = SnCode + LabelType + "已打印"; // // return false; // // } // //} // string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString(); // FileInfo PrintFile = new FileInfo(LabelName); // //打开模板路径 // //查询模板对应的取值SQL和参数名称 // dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_id='" + LaID + "'", "select"); // StringBuilder sb = new StringBuilder(); // if (!PrintFile.Exists) // { // MessageBox.Show("打印文件不存在"); // return false; // } // string filechangetime = PrintFile.LastWriteTime.ToString(); // if (filechangetime != filelastwritetime) // { // lbl.Stop(); // lbl = new Engine(true); // BaseUtil.WriteLbl(); // filechangetime = PrintFile.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); // string update = "la_lastwritetime = to_date((regexp_substr('" + filechangetime + "','\\d+.+\\d+')),'yyyy-mm-dd hh24:mi:ss')"; // dh.UpdateByCondition("label", update, "la_id = '" + LaID + "'"); // } // format = lbl.Documents.Open(LabelName); // if (format == null) // { // MessageBox.Show("标签文件打开失败"); // return false; // } // //执行全部的SQL // for (int i = 0; i < dt.Rows.Count; i++) // { // string sql = dt.Rows[i]["lp_sql"].ToString(); // try // { // Regex ConnoteA = new Regex("{\\w+}"); // foreach (Match mch in ConnoteA.Matches(sql)) // { // string x = mch.Value.Trim(); // sql = sql.Replace(x, "'" + SnCode + "'"); // } // DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); // if (Param.Rows.Count == 0) // continue; // //查询的结果的参数个数大于1需要给标签的多个参数赋值 // if (Param.Rows.Count > 0) // { // int LoopTime = Param.Rows.Count > 200 ? 200 : Param.Rows.Count; // for (int j = 0; j < LoopTime; j++) // { // for (int k = 0; k < format.SubStrings.Count; k++) // { // if (j == 0 & format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper()) // { // format.SubStrings[k].Value = Param.Rows[0][0].ToString(); // } // //使用SN开头的参数赋值SN1,SN2,SN3等参数 // if (format.SubStrings[k].Name.ToUpper() == dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)) // { // format.SubStrings[k].Value = Param.Rows[j][0].ToString(); // } // } // } // } // } // catch (System.Exception) // { // MessageBox.Show("SQL维护不正确"); // } // } // LogManager.DoLog(sb.ToString()); // //保存本次赋值进行打印 // format.PrintSetup.PrinterName = PrinterName; // format.PrintSetup.IdenticalCopiesOfLabel = PrintNum; // format.Print(); // LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, ""); // for (int k = 0; k < format.SubStrings.Count; k++) // { // format.SubStrings[k].Value = null; // } // LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode); // return true; // } } }