using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Data; using System.Collections; using NPOIReport; using System.Diagnostics; using System.Runtime.InteropServices; using Estsh.Client.Base; using System.Windows.Forms; using Com.Estsh.MES.App; using ApServerProvider; using MainProgram; using MainProgram.BLL; using ZPLPrinter; namespace Estsh.Client.StepLibrary { public class YYPrintQA : StepBase { //计数器数量 private string _PrdCount = string.Empty; private string Bcode = string.Empty; private string BcodeText = string.Empty; private static string BarTenderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Bartend\bartend.exe"); private static string LabelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\LabelFile"); private static object _lock_print_object = new object(); private const char TAB = '\t'; private const string NEW_LINE = "\r\n"; private List dtLabel = null; public static bool PrintSerialNumber(List snData, string templateFile, string Bcode, string txtBcode) { string LabelTemplatePath = Path.Combine(LabelPath, templateFile); string LabelFilePath = Path.Combine(LabelPath, Bcode); string LabelTextPath = Path.Combine(LabelPath, txtBcode); if (!CommonListData.CheckDataValid(snData)) return false; StringBuilder PrintContent = new StringBuilder(); foreach (DataRow dr in snData) { PrintContent.Append(dr["csn"].ToString()); PrintContent.Append(TAB); //1 PrintContent.Append(dr["dates"].ToString()); PrintContent.Append(TAB); //2 PrintContent.Append(dr["part_no_3c"].ToString()); PrintContent.Append(TAB); //3 PrintContent.Append(dr["cust_part_no"].ToString()); PrintContent.Append(TAB); //4 PrintContent.Append(dr["part_spec"].ToString()); PrintContent.Append(TAB); //5 PrintContent.Append(dr["part_spec2"].ToString()); PrintContent.Append(TAB); //5 PrintContent.Append(dr["part_no"].ToString()); PrintContent.Append(TAB); //6 PrintContent.Append(dr["order_seq"].ToString()); PrintContent.Append(TAB); //7 PrintContent.Append(dr["car_no"].ToString()); PrintContent.Append(TAB); //8 PrintContent.Append(dr["part_location"].ToString()); PrintContent.Append(TAB); //9 PrintContent.Append(dr["seq"].ToString()); PrintContent.Append(TAB); //10 PrintContent.Append(dr["label_type"].ToString()); PrintContent.Append(TAB); //11 PrintContent.Append(dr["model_code"].ToString()); PrintContent.Append(TAB); //13 PrintContent.Append(dr["enum_alias"].ToString()); PrintContent.Append(TAB); //14 PrintContent.Append(dr["model_name"].ToString()); PrintContent.Append(TAB); //25 PrintContent.Append(dr["type_name"].ToString()); PrintContent.Append(TAB); //26 PrintContent.Append(Environment.NewLine); lock (_lock_print_object) { WritePrintLog(PrintContent.ToString()); BarTenderPrinter.WriteFile(LabelTextPath, PrintContent.ToString(), Encoding.UTF8, false); BarTenderPrinter.Print(BarTenderPath, LabelFilePath); //BarTenderPrinter.WriteFile(LabelTextPath, null, Encoding.UTF8, false); } return true; } return true; } private static void WritePrintLog(string msgText) { string logPath = AppDomain.CurrentDomain.BaseDirectory + @"\\PrintLog"; try { if (!System.IO.Directory.Exists(logPath)) { System.IO.Directory.CreateDirectory(logPath); } string txtMsgPath = logPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; string timeStamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss-->"); using (StreamWriter sw = new StreamWriter(txtMsgPath, true)) { sw.WriteLine(timeStamp + msgText.ToString()); sw.Flush(); sw.Close(); } } catch (System.Exception ex) { } } /// /// 输入到打印机 /// /// BarTender.exe 的路径 /// *.btw 文件的路径 public static void Print(string bartendPath, string labelPath) { Process process = new Process(); process.StartInfo = new ProcessStartInfo(ToShortPathName(bartendPath), @" /F=" + ToShortPathName(labelPath) + " /p /x"); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.RedirectStandardOutput = true; try { process.Start(); process.WaitForExit(100 * 1000); //等待上述进程执行完毕 if (process.HasExited == false) //程序停止响应处理 { process.Kill(); process.Dispose(); } } catch { process.Kill(); process.Dispose(); } } /// /// 获取短路径 /// /// 长路径 /// 短路径 /// 短路径的长度 /// 执行结果 [DllImport("kernel32.dll ", CharSet = CharSet.Auto)] public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength); /// /// 把长路径转化为短路径(兼容 WIN 98 格式) /// /// 长路径 /// 短路径 public static string ToShortPathName(string longName) { StringBuilder shortNameBuffer = new StringBuilder(256); int bufferSize = shortNameBuffer.Capacity; int result = GetShortPathName(longName, shortNameBuffer, bufferSize); return shortNameBuffer.ToString(); } /// /// 执行工步操作 /// /// 执行结果 public override bool Do() { try { //本地计数器 CountRead(); MESPrintSN _app = new MESPrintSN(App); Action(this, new EventArgs()); string inputSN = Context["serial_number"].ToString(); bool useJupStep = Context.Contains("useJupStep"); ////从临时表(g_sn_keydata_temp)中转移扭矩数据到(g_sn_keydata) //bool moveKeyData = _app.moveKeyData(inputSN); ////跳工步不打印合格证条码 //if (!useJupStep) //{ ShowMessage(this, "green|条码正在打印中,条码: " + inputSN); dtLabel = _app.GetPrintSN(inputSN); string part_location = dtLabel[0]["part_location"].ToString(); string typeName = dtLabel[0]["type_name"].ToString(); //if (part_location == "08" || part_location == "09" || part_location == "10" || part_location == "11" || part_location=="12"|| part_location=="13"|| part_location=="14") //{ // Bcode = "YYLKQA.BTW"; // BcodeText = "YYLKQA.TXT"; //} //else //{ // if (typeName == "19D") // { // Bcode = "YYLKQA.BTW"; // BcodeText = "YYLKQA.TXT"; // } // else // { // Bcode = "YYJLQA.BTW"; // BcodeText = "YYJLQA.TXT"; // } //} if (typeName == "SX12") { Bcode = "YYJLQA.BTW"; BcodeText = "YYJLQA.TXT"; } else if (typeName == "MPC") { Bcode = "YYMPCQA.BTW"; BcodeText = "YYMPCQA.TXT"; } else { Bcode = "YYLKQA.BTW"; BcodeText = "YYLKQA.TXT"; } PrintSerialNumber(dtLabel, LabelPath, Bcode, BcodeText); _app.UpdatePrintCount(inputSN); //本地计数器 CountAdd(); Context.Remove("useJupStep"); string stationType = _app.stationType(Context["terminal_id"].ToString()); string pdlineID = _app.pdLineID(Context["terminal_id"].ToString()); if (stationType == "B" || stationType == "A") { _app.updateOnLineStatus(Context["serial_number"].ToString(), pdlineID); } if (stationType == "C") { //更新条码状态为下线 _app.updateStatus(inputSN); string tres = _app.ProdTrans(0, Context["serial_number"].ToString(), Convert.ToInt32(Context["terminal_id"].ToString()), 0, stationType); } //try //{ // string tres = _app.ProdTrans(0, Context["serial_number"].ToString(), Convert.ToInt32(Context["terminal_id"].ToString()), 0, stationType); // if (tres.ToUpper() != "OK") // { // ShowMessage(this, "red|" + tres + ""); // return false; // } //} //catch (Exception ex) //{ // using (StreamWriter sw = new StreamWriter("ProdTrans-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true)) // { // sw.WriteLine(string.Format("{0} {1} {2}", "[" // + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // + "] ProdTrans", Context["serial_number"].ToString(), ex)); // sw.Flush(); // sw.Close(); // } //} Complate(this, new EventArgs()); } catch (Exception ex) { } return base.Do(); } private void InitializeComponent() { this.SuspendLayout(); this.Name = "PrintQA"; this.Size = new System.Drawing.Size(367, 224); this.ResumeLayout(false); } private void CountAdd() { string value = _PrdCount; int count = 0; try { count = Convert.ToInt32(value); count++; _PrdCount = count.ToString(); } catch { _PrdCount = "1"; } try { StreamWriter sw = new StreamWriter("InPdlineCount.txt"); sw.Write(count.ToString()); sw.Flush(); sw.Close(); } catch { ShowMessage(this, "计数器更新错误,未知!"); } } private void CountRead() { try { if (File.Exists("InPdlineCount.txt")) { FileStream fs = new FileStream(@"InPdlineCount.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string content = sr.ReadToEnd(); //一次性读取全部数据 _PrdCount = content; fs.Close(); sr.Close(); } else { _PrdCount = "0"; } } catch { _PrdCount = "0"; ShowMessage(this, "计数器更新错误,未知!"); } } } }