using Estsh.Client.Base; using System.Data; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace Estsh.Client.StepLibrary { public class PrintPartSN : StepBase { private StepLibraryAPP _app = null; 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 bool PrintSerialNumber(List snData, string templateFile, string txtDataFile) { string LabelTemplatePath = Path.Combine(LabelPath, templateFile); string LabelTextPath = Path.Combine(LabelPath, txtDataFile + ".txt"); string LabelFilePath = Path.Combine(LabelPath, txtDataFile + ".BTW"); if (!httpClient.CheckDataValid(snData)) return false; StringBuilder PrintContent = new StringBuilder(); foreach (DataRow dr in snData) { PrintContent.Append(dr["csn"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["car_no"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["vendor_part_no"].ToString()); PrintContent.Append(TAB); string subboard_qty = ""; for (int i = 1; i <= (8 - dr["subboard_qty"].ToString().Length); i++) { subboard_qty = subboard_qty + "0"; } subboard_qty = "NO:" + subboard_qty + dr["subboard_qty"].ToString(); PrintContent.Append(subboard_qty); PrintContent.Append(TAB); PrintContent.Append(dr["part_no"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["part_spec"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["part_spec2"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["part_location"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["cust_part_no"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["part_no_3c"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["model_code"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["model_desc"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["model_alias"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["enum_desc"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["serial_number"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["prod_type"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["model_name"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["type_name"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["number_T"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["part_T"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["ymd"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["hms"].ToString()); PrintContent.Append(TAB); PrintContent.Append(dr["ymd2"].ToString()); PrintContent.Append(TAB); PrintContent.Append(Environment.NewLine); lock (_lock_print_object) { WritePrintLog(PrintContent.ToString()); BarTenderPrinter.WriteFile(LabelTextPath, PrintContent.ToString(), Encoding.UTF8, false); BarTenderPrinter.Print(BarTenderPath, LabelFilePath); } 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(); } private string printIP = string.Empty; /// /// 执行工步操作 /// /// 执行结果 public override bool Do() { _app = new StepLibraryAPP(httpClient); Action(this, new EventArgs()); string SN = Context["serial_number"].ToString(); dtLabel = _app.GetPrintSN(SN); ShowMessage(this, "green|条码正在打印中,条码: " + SN); PrintSerialNumber(dtLabel, LabelPath, "PartSN"); //更新总成打印流水号 _app.UpdatePrdQty(SN); _app.UpdatePrintCount(SN); Complate(this, new EventArgs()); return base.Do(); } private void InitializeComponent() { this.SuspendLayout(); // // PrintPartSN // this.Name = "PrintPartSN"; this.ResumeLayout(false); } } }