Sfoglia il codice sorgente

修改Bartender2016支持

callm 2 settimane fa
parent
commit
d7cc300884

+ 4 - 4
UAS_MES_YTDZ/FunctionCode/Packing/Packing_PackageCollectionWeigh.cs

@@ -25,7 +25,7 @@ namespace UAS_MES_NEW.Packing
         LogStringBuilder sql = new LogStringBuilder();
         AutoSizeFormClass asc = new AutoSizeFormClass();
         Document doc;
-        Engine engine;
+        public BarTender.Application engine;
         Thread thread;
         DataTable Dbfind;
         string ErrorMessage = "";
@@ -66,7 +66,7 @@ namespace UAS_MES_NEW.Packing
         {
             try
             {
-                engine = new Engine(true);
+                engine = new BarTender.Application();
                 BaseUtil.WriteLbl();
             }
             catch (Exception ex)
@@ -227,7 +227,7 @@ namespace UAS_MES_NEW.Packing
                 {
                     dh.ExecuteSql("update package set pa_printcount=pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
                     //doc = lbl.Documents.Open(PrintLabel.Text);
-                    if (Print.BarTender1(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrintList.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), oMakeCode == "" ? pa_makecode.Text : oMakeCode, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
+                    if (Print.BarTender(Tag.ToString(),  engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrintList.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), oMakeCode == "" ? pa_makecode.Text : oMakeCode, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
                     {
                         OperateResult.AppendText(">>开始打印箱号" + pa_outboxcode.Text + "\n", Color.Black);
                     }
@@ -237,7 +237,7 @@ namespace UAS_MES_NEW.Packing
                     }
                     if (PrintSecond.Checked)
                     {
-                        if (Print.BarTender1(Tag.ToString(), ref engine, PrintLabel1.Text, PrintLabel1.SelectedValue.ToString(), PrintList1.Text, pa_outboxcode.Text, int.Parse(PrintNum1.Text), oMakeCode == "" ? pa_makecode.Text : oMakeCode, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
+                        if (Print.BarTender(Tag.ToString(),  engine, PrintLabel1.Text, PrintLabel1.SelectedValue.ToString(), PrintList1.Text, pa_outboxcode.Text, int.Parse(PrintNum1.Text), oMakeCode == "" ? pa_makecode.Text : oMakeCode, pr_code.Text, "卡通箱标", "0", out ErrorMessage))
                         {
                             OperateResult.AppendText(">>开始打印箱号" + pa_outboxcode.Text + "\n", Color.Black);
                         }

+ 227 - 3
UAS_MES_YTDZ/PublicMethod/Print.cs

@@ -3,6 +3,7 @@ using Seagull.BarTender.Print;
 using System;
 using System.Data;
 using System.IO;
+using System.Linq;
 using System.Net;
 using System.Text;
 using System.Text.RegularExpressions;
@@ -132,11 +133,11 @@ namespace UAS_MES_NEW.PublicMethod
             doc2.PrintSetup.NumberSerializedLabels = 1;
             doc2.PrintSetup.Printer = PrinterName;
             doc2.PrintOut(false, false);
-            doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
+            //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);
+            //lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
             return true;
         }
 
@@ -432,7 +433,230 @@ namespace UAS_MES_NEW.PublicMethod
         //        return true;
         //    }
 
-
+        public static bool BarTender(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 (PrintNum == 0)
+            {
+                PrintNum = 1;
+            }
+            if (IfRePrint == "-1")
+            {
+                string printNumber = dh.getFieldDataByCondition("CONFIGS", "DATA", "code='printNumber' and caller='MESSetting'").ToString();
+                if (dh.getRowCount("labelprintlog", "lpl_type='" + LabelType + "' and lpl_value='" + SnCode + "'") > int.Parse(printNumber))
+                {
+                    ErrorMessage = "标签补打超出允许打印次数【" + printNumber + "】";
+                    return false;
+                }
+            }
+            if (IfRePrint != "-1" && LabelType != "机身标")
+            {
+                bool allowPallte = true;
+                if ((SystemInf.dh.getFieldDataByCondition("master", "MA_FUNCTION", "ma_user='" + SystemInf.CurrentDB + "'").ToString() == "万年MES系统(正式)"))
+                {
+                    allowPallte = false;
+                }
+                if (LabelType == "卡通箱标" || LabelType == "大箱标" || (LabelType == "栈板标" && allowPallte) || LabelType == "彩盒标")
+                {
+                    dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "' and lpl_stepcode='" + User.CurrentStepCode + "' and lpl_file='" + LabelName + "'", "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;
+                }
+            }
+            else
+            {
+                if (LabelType == "卡通箱标" || LabelType == "大箱标" || LabelType == "彩盒标")
+                {
+                    dt = (DataTable)dh.ExecuteSql("select lpl_id from labelprintlog where lpl_value='" + SnCode + "' and lpl_type='" + LabelType + "'", "select");
+                    if (dt.Rows.Count == 0)
+                    {
+                        //ErrorMessage = SnCode + LabelType + "未打印,不允许补打";
+                        //return false;
+                    }
+                }
+            }
+            string filelastwritetime = dh.getFieldDataByCondition("label", "la_lastwritetime", "la_id = '" + LaID + "'").ToString();
+            string LA_SOFTTYPE = dh.getFieldDataByCondition("label", "LA_SOFTTYPE", "la_id = '" + LaID + "'").ToString();
+            //如果有附件上传的话
+            string fp_name = "";
+            //下载附件
+            FileInfo PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
+            if (!PrintFile.Exists)
+            {
+                if (LA_SOFTTYPE != "")
+                {
+                    string[] fpid = LA_SOFTTYPE.Split(';');
+                    for (int i = 0; i < fpid.Length; i++)
+                    {
+                        if (fpid[i] != "")
+                        {
+                            DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
+                            if (label.Rows.Count > 0)
+                            {
+                                string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
+                                fp_name = label.Rows[0]["fp_name"].ToString();
+                                WebClient wc = new WebClient();
+                                wc.DownloadFile("http://192.168.1.5:8099/" + fp_path, @"C:\打印标签\" + fp_name);
+                            }
+                        }
+                    }
+                    lbl = new BarTender.Application();
+                }
+            }
+            else
+            {
+                string filechangetime = PrintFile.LastWriteTime.ToString();
+                System.DateTime dateTime1 = System.DateTime.Parse(filechangetime);
+                System.DateTime dateTime2 = System.DateTime.Parse(filelastwritetime);
+                if (dateTime1 < dateTime2)
+                {
+                    if (LA_SOFTTYPE != "")
+                    {
+                        string[] fpid = LA_SOFTTYPE.Split(';');
+                        for (int i = 0; i < fpid.Length; i++)
+                        {
+                            if (fpid[i] != "")
+                            {
+                                DataTable label = (DataTable)dh.ExecuteSql("select FP_PATH, FP_DATE, FP_NAME from FILEPATH where fp_id='" + fpid[i] + "'", "select");
+                                if (label.Rows.Count > 0)
+                                {
+                                    string fp_path = label.Rows[0]["FP_PATH"].ToString().Replace("/app/uas/webapps/", "");
+                                    fp_name = label.Rows[0]["fp_name"].ToString();
+                                    WebClient wc = new WebClient();
+                                    wc.DownloadFile("http://192.168.1.5:8099/" + fp_path, @"C:\打印标签\" + fp_name);
+                                    FileInfo file = new FileInfo(@"C:\打印标签\" + fp_name);
+                                    file.CreationTime = Convert.ToDateTime(filelastwritetime);
+                                }
+                            }
+                        }
+                    }
+                    lbl = new BarTender.Application();
+                    BaseUtil.WriteLbl();
+                }
+            }
+            PrintFile = new FileInfo(@"C:\打印标签\" + LabelName);
+            if (!PrintFile.Exists)
+            {
+                MessageBox.Show("打印文件不存在");
+                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 (!PrintFile.Exists)
+            {
+                MessageBox.Show("打印文件不存在");
+                return false;
+            }
+            try
+            {
+                doc2 = lbl.Formats.Open(@"C:\打印标签\" + LabelName);
+            }
+            catch (System.Exception)
+            {
+                lbl = new BarTender.Application();
+                doc2 = lbl.Formats.Open(@"C:\打印标签\" + LabelName);
+            }
+            if (doc2 == null)
+            {
+                MessageBox.Show("标签文件打开失败");
+                return false;
+            }
+            Console.WriteLine("清空参数开始" + System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:ffffff"));
+            try
+            {
+                for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
+                {
+                    doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, "");
+                }
+            }
+            catch (Exception ex)
+            {
+                LogManager.DoLog(ex.Message + ex.StackTrace);
+            }
+            Console.WriteLine("清空参数结束" + System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:ffffff"));
+            string ParamValue = doc2.NamedSubStrings.GetAll("#", "$").ToUpper();
+            string[] paramname = ParamValue.Split('$');
+            for (int i = 0; i < paramname.Length; i++)
+            {
+                paramname[i] = paramname[i].Split('#')[0];
+            }
+            //执行全部的SQL
+            Console.WriteLine("参数赋值开始" + System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:ffffff"));
+            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++)
+                        {
+                            if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper()))
+                            {
+                                if (doc2.NamedSubStrings.Item(dt.Rows[i]["lp_name"].ToString()).Name != null)
+                                {
+                                    doc2.SetNamedSubStringValue(dt.Rows[i]["lp_name"].ToString(), Param.Rows[0][0].ToString());
+                                }
+                            }
+                            if (paramname.Contains(dt.Rows[i]["lp_name"].ToString().ToUpper() + (j + 1)))
+                            {
+                                if (doc2.NamedSubStrings.Item(dt.Rows[i]["lp_name"].ToString() + (j + 1)).Name != null)
+                                {
+                                    doc2.SetNamedSubStringValue(dt.Rows[i]["lp_name"].ToString() + (j + 1), Param.Rows[j][0].ToString());
+                                }
+                            }
+                            //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());
+                            //    }
+                            //    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 (Exception ex)
+                {
+                    MessageBox.Show("SQL维护不正确" + ex.Message);
+                }
+            }
+            Console.WriteLine("参数赋值结束" + System.DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss:ffffff"));
+            LogManager.DoLog(sb.ToString());
+            //保存本次赋值进行打印a
+            doc2.PrintSetup.IdenticalCopiesOfLabel = PrintNum;
+            // 序列标签数 
+            doc2.PrintSetup.NumberSerializedLabels = 1;
+            doc2.PrintSetup.Printer = PrinterName;
+            doc2.PrintOut(false, false);
+            LogicHandler.DoCommandLog(iCaller, User.UserCode, "", User.UserLineCode, User.UserSourceCode, "打印", "成功打印", SnCode, "");
+            LogicHandler.doLabelPrintLog(SnCode, LabelType, MakeCode, ProdCode, User.UserSourceCode, User.CurrentStepCode, IfRePrint, User.UserCode, PrintFile.Name);
+            return true;
+        }
         public static bool BarTender1(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 = "";