You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

479 lines
22 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
using Esi.Mes.LabelPrinter;
using LabelManager2;
namespace Estsh.Client.StepLibrary
{
public class PrintQA : StepBase
{
//计数器数量
private string _PrdCount = 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<dynamic> dtLabel = null;
private static int terminal_id;
private static CodesoftPrinter m_csPrinter = null;
public static bool PrintSerialNumber(List<dynamic> snData, string templateFile, string txtDataFile, List<dynamic> CustPartNoList, List<dynamic> dtpartPrint, bool iffalse)
{
string LabelTemplatePath = Path.Combine(LabelPath, templateFile);
string LabelTextPath = Path.Combine(LabelPath, "QA" + ".txt");
string LabelFilePathQA = Path.Combine(LabelPath, "QA.Lab");
string LabelTextPathBZ = Path.Combine(LabelPath, "BZ.txt");
string LabelFilePathBZ = Path.Combine(LabelPath, "BZ.Lab");
string LabelFilePathBZTWO = Path.Combine(LabelPath, "BZTWO.Lab");
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["type_desc"].ToString()); PrintContent.Append(TAB); //2
PrintContent.Append(dr["model_name"].ToString()); PrintContent.Append(TAB); //3
PrintContent.Append(dr["part_spec"].ToString()); PrintContent.Append(TAB); //4
PrintContent.Append(dr["serial_number"].ToString()); PrintContent.Append(TAB); //5
PrintContent.Append(dr["create_time"].ToString()); PrintContent.Append(TAB); //6
PrintContent.Append(dr["model_alias"].ToString()); PrintContent.Append(TAB); //7
PrintContent.Append(dr["number_T"].ToString()); PrintContent.Append(TAB);
PrintContent.Append(dr["part_T"].ToString()); PrintContent.Append(TAB);
//盐城改过8
PrintContent.Append(dr["part_no"].ToString()); PrintContent.Append(TAB); //8
PrintContent.Append(dr["car_no"].ToString()); PrintContent.Append(TAB); //9
PrintContent.Append(dr["part_no"].ToString()); PrintContent.Append(TAB); //10
//PrintContent.Append(dr["part_spec2"].ToString()); PrintContent.Append(TAB); //11
//PrintContent.Append(dr["part_location"].ToString()); PrintContent.Append(TAB); //12
#region 湘潭麦格纳特殊处理
try
{
if (dr["part_location"].ToString() != "01" || dr["part_location"].ToString() != "02")
{
PrintContent.Append(CustPartNoList[0]["cust_part_no"].ToString()); PrintContent.Append(TAB); //13
PrintContent.Append(CustPartNoList[1]["cust_part_no"].ToString()); PrintContent.Append(TAB); //14
PrintContent.Append(CustPartNoList[2]["cust_part_no"].ToString()); PrintContent.Append(TAB); //15
}
else
{
PrintContent.Append(dr["cust_part_no"].ToString()); PrintContent.Append(TAB); //13
PrintContent.Append(""); PrintContent.Append(TAB); //14
PrintContent.Append(""); PrintContent.Append(TAB); //15
}
}
catch
{
PrintContent.Append(dr["cust_part_no"].ToString()); PrintContent.Append(TAB); //13
PrintContent.Append(""); PrintContent.Append(TAB); //14
PrintContent.Append(""); PrintContent.Append(TAB); //15
}
#endregion
PrintContent.Append(dr["part_class"].ToString()); PrintContent.Append(TAB); //14
PrintContent.Append(dr["label_type"].ToString()); PrintContent.Append(TAB); //15
PrintContent.Append(dr["part_no_3c"].ToString()); PrintContent.Append(TAB); //16
PrintContent.Append(dr["model_code"].ToString()); PrintContent.Append(TAB); //17
PrintContent.Append(dr["model_alias"].ToString()); PrintContent.Append(TAB); //18
PrintContent.Append(dr["enum_desc"].ToString()); PrintContent.Append(TAB); //19
PrintContent.Append(dr["prod_type"].ToString()); PrintContent.Append(TAB); //20
PrintContent.Append(dr["model_name"].ToString()); PrintContent.Append(TAB); //21
PrintContent.Append(dr["type_name"].ToString()); PrintContent.Append(TAB); //22
PrintContent.Append(Environment.NewLine);
lock (_lock_print_object)
{
WritePrintLog(PrintContent.ToString());
if (iffalse)
{
if (dtpartPrint.Count > 0)
{
string printName = dtpartPrint[0]["print_name"].ToString();
string port = dtpartPrint[0]["print_port"].ToString();
string strFile = LabelFilePathBZTWO;
ApplicationClass lbl = new LabelManager2.ApplicationClass();
LabelManager2.Document doc = lbl.ActiveDocument;
lbl.Documents.Open(strFile, false);//调用设好的lbl标签
doc = lbl.ActiveDocument;
doc.Printer.SwitchTo(printName, port, true);
//codesoft模板中标签变量
doc.Variables.FreeVariables.Item("car_no").Value = dr["car_no"].ToString();
doc.Variables.FreeVariables.Item("create_time").Value = dr["create_time"].ToString();
doc.Variables.FreeVariables.Item("csn").Value = dr["csn"].ToString();
doc.Variables.FreeVariables.Item("enum_desc").Value = dr["enum_desc"].ToString();
doc.Variables.FreeVariables.Item("label_type").Value = dr["label_type"].ToString();
doc.Variables.FreeVariables.Item("model_alias").Value = dr["model_alias"].ToString();
doc.Variables.FreeVariables.Item("model_code").Value = dr["model_code"].ToString();
doc.Variables.FreeVariables.Item("model_name").Value = dr["model_name"].ToString();
doc.Variables.FreeVariables.Item("number_T").Value = dr["number_T"].ToString();
doc.Variables.FreeVariables.Item("part_class").Value = dr["part_class"].ToString();
doc.Variables.FreeVariables.Item("part_no").Value = dr["part_no"].ToString();
doc.Variables.FreeVariables.Item("part_no_3c").Value = dr["part_no_3c"].ToString();
doc.Variables.FreeVariables.Item("part_spec").Value = dr["part_spec"].ToString();
doc.Variables.FreeVariables.Item("part_T").Value = dr["part_T"].ToString();
doc.Variables.FreeVariables.Item("prod_type").Value = dr["prod_type"].ToString();
doc.Variables.FreeVariables.Item("serial_number").Value = dr["serial_number"].ToString();
doc.Variables.FreeVariables.Item("type_desc").Value = dr["type_desc"].ToString();
doc.Variables.FreeVariables.Item("type_name").Value = dr["type_name"].ToString();
doc.PrintLabel(1, 1, 1, 1, 1, strFile);
//标签批量连续打印。FormFeed必须等参数变量输出后才执行输出给打印机。
doc.FormFeed();
lbl.Quit();
doc = null;
}
else
{
//2.使用CodeSoft打印
PrintCodeSoftSerialNumber(dr, LabelFilePathBZ, m_csPrinter, CustPartNoList);
}
}
else
{
PrintCodeSoftSerialNumber(dr, LabelFilePathQA, m_csPrinter, CustPartNoList);
//1.使用BarTender打印
//BarTenderPrinter.WriteFile(LabelTextPath, PrintContent.ToString(), Encoding.UTF8, false);
//BarTenderPrinter.Print(BarTenderPath, LabelFilePath);
//BarTenderPrinter.WriteFile(LabelTextPathBZ, PrintContent.ToString(), Encoding.UTF8, false);
//BarTenderPrinter.Print(BarTenderPath, LabelFilePathBZ);
//2.使用CodeSoft打印
PrintCodeSoftSerialNumber(dr, LabelFilePathBZ, m_csPrinter, CustPartNoList);
}
}
return true;
}
return true;
}
public static bool PrintCodeSoftSerialNumber(DataRow snData, string LabelTextPath, CodesoftPrinter csPrinter,List<dynamic> CustPartNoList)
{
//string LabelTemplatePath = Path.Combine(LabelPath, labelFileName);
//m_csPrinter = csPrinter;
Dictionary<String, String> printData = new Dictionary<string, string>();
printData.Add("car_no", snData["car_no"].ToString());
printData.Add("create_time", snData["create_time"].ToString());
printData.Add("csn", snData["csn"].ToString());
if (snData["part_location"].ToString() != "01" || snData["part_location"].ToString() != "02")
{
printData.Add("cust_part_no1", CustPartNoList[0]["cust_part_no"].ToString());
printData.Add("cust_part_no2", CustPartNoList[1]["cust_part_no"].ToString());
printData.Add("cust_part_no3", CustPartNoList[2]["cust_part_no"].ToString());
}
else
{
printData.Add("cust_part_no1", snData["cust_part_no"].ToString());
printData.Add("cust_part_no2", "");
printData.Add("cust_part_no3", "");
}
printData.Add("enum_desc", snData["enum_desc"].ToString());
printData.Add("label_type", snData["label_type"].ToString());
printData.Add("model_alias", snData["model_alias"].ToString());
printData.Add("model_code", snData["model_code"].ToString());
printData.Add("model_name", snData["model_name"].ToString());
printData.Add("number_T", snData["number_T"].ToString());
printData.Add("part_class", snData["part_class"].ToString());
printData.Add("part_no", snData["part_no"].ToString());
printData.Add("part_no_3c", snData["part_no_3c"].ToString());
printData.Add("part_spec", snData["part_spec"].ToString());
printData.Add("part_T", snData["part_T"].ToString());
printData.Add("prod_type", snData["prod_type"].ToString());
printData.Add("serial_number", snData["serial_number"].ToString());
printData.Add("type_desc", snData["type_desc"].ToString());
printData.Add("type_name", snData["type_name"].ToString());
csPrinter.DoPrint(LabelTextPath, printData, 1);
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)
{
}
}
/// <summary>
/// 输入到打印机
/// </summary>
/// <param name="bartendPath">BarTender.exe 的路径</param>
/// <param name="labelPath">*.btw 文件的路径</param>
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); //等待上述进程执行完毕
process.WaitForExit(100*10); //等待上述进程执行完毕
if (process.HasExited == false) //程序停止响应处理
{
process.Kill();
process.Dispose();
}
}
catch
{
process.Kill();
process.Dispose();
}
}
/// <summary>
/// 获取短路径
/// </summary>
/// <param name="path">长路径</param>
/// <param name="shortPath">短路径</param>
/// <param name="shortPathLength">短路径的长度</param>
/// <returns>执行结果</returns>
[DllImport("kernel32.dll ", CharSet = CharSet.Auto)]
public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength);
/// <summary>
/// 把长路径转化为短路径(兼容 WIN 98 格式)
/// </summary>
/// <param name="longName">长路径</param>
/// <returns>短路径</returns>
public static string ToShortPathName(string longName)
{
StringBuilder shortNameBuffer = new StringBuilder(256);
int bufferSize = shortNameBuffer.Capacity;
int result = GetShortPathName(longName, shortNameBuffer, bufferSize);
return shortNameBuffer.ToString();
}
/// <summary>
/// 执行工步操作
/// </summary>
/// <returns>执行结果</returns>
public override bool Do()
{
//本地计数器
CountRead();
MESPrintSN _app = new MESPrintSN(App);
Action(this, new EventArgs());
string inputSN = Context["serial_number"].ToString();
terminal_id = Convert.ToInt32(Context["terminal_id"].ToString());
bool useJupStep = Context.Contains("useJupStep");
//从临时表g_sn_keydata_temp中转移扭矩数据到g_sn_keydata
//bool moveKeyData = _app.moveKeyData(inputSN);
////跳工步不打印合格证条码
//if (!useJupStep)
//{
if (m_csPrinter == null)
{
try
{
m_csPrinter = new CodesoftPrinter();
}
catch
{
ShowMessage(this, "red|请检查打印软件是否安装!");
return false;
}
}
m_csPrinter = (CodesoftPrinter)Context["m_csPrinter"];
dtLabel = _app.GetPrintSN(inputSN);
//查询该零件是否需要指定打印机
List<dynamic> dtpartPrint = _app.getPartPrintList(terminal_id, dtLabel[0]["part_no"].ToString());
bool iffalse = _app.ifTerminalPrint(terminal_id);
List<dynamic> CustPartNoList = new DataTable();
if (dtLabel.Count > 0)
{
string modelName = dtLabel[0]["model_name"].ToString();
CustPartNoList = _app.getCustPartNoList(modelName);
}
ShowMessage(this, "green|条码正在打印中,条码: " + dtLabel[0]["csn"].ToString());
#region
//string rule = dtLabel[0]["label_type"].ToString();
////解决2SV部分总成的3C零件号在不同配置中是不一样的
//if (rule.ToUpper() == "RULE04")
//{
// string PartNo3c = dtLabel[0]["part_no_3c"].ToString();
// string modelID = dtLabel[0]["model_id"].ToString();
// string partID = dtLabel[0]["part_id"].ToString();
// string ReplacePartNo3c = _app.GetPartNO3c(PartNo3c, modelID, partID);
// dtLabel[0]["part_no_3c"] = ReplacePartNo3c;
//}
////只有一个客户零件号的总成是不需要维护在对应表中的
////(如果查出对应表中的客户零件号则以查出的为准,否则使用总成信息表中维护的客户零件号)
//if (rule.ToUpper() == "RULE03")
//{
// string custPartNo = dtLabel[0]["cust_partno"].ToString();
// string custPartNo2 = dtLabel[0]["cust_part_no"].ToString();
// if (string.IsNullOrEmpty(custPartNo))
// {
// dtLabel[0]["cust_partno"] = custPartNo2;
// }
//}
#endregion
PrintSerialNumber(dtLabel, LabelPath, "", CustPartNoList, dtpartPrint, iffalse);
_app.UpdatePrintCount(inputSN);
//本地计数器
CountAdd();
Context.Remove("useJupStep");
//更新条码状态为下线
_app.updateStatus(inputSN);
string stationType = _app.stationType(Context["terminal_id"].ToString());
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());
return base.Do();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// PrintQA
//
this.Name = "PrintQA";
this.Size = new System.Drawing.Size(223, 129);
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, "计数器更新错误,未知!");
}
}
}
}