From e253204f8d898658b7941609fa2827d03367870d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E9=AD=94=E4=BB=99?= <13068499+willowhh@user.noreply.gitee.com> Date: Thu, 15 May 2025 11:22:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8D=B0=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/PrintControlViewModel.cs | 15 +++++- .../ViewModels/ScanControlViewModel.cs | 2 + .../BartenderHelper/BartenderPrintHelper.cs | 51 ++++++++++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintControlViewModel.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintControlViewModel.cs index 5d72459..b04aa1a 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintControlViewModel.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintControlViewModel.cs @@ -7,6 +7,7 @@ using RIZO_Application.Core; using RIZO_Application.Core.Mvvm; using RIZO_Helper.Tools; using RIZO_Application.Infrastructure.Model; +using System.Linq; namespace RIZO_Application.Modules.ModuleName.ViewModels { @@ -79,6 +80,10 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels { { "10", printDto.PartNumber }, { "11", "02S" }, + {"13",null }, + {"14",null }, + {"15",null }, + {"16",null }, { "10000", printDto.WorkOrder }, { "10002", printDto.Team }, { "10003", printDto.Sort.ToString() }, @@ -86,13 +91,19 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels { "10005", printDto.Sort.ToString() }, { "10006", printDto.BatchCode }, { "10007", printDto.PackageNum.ToString() }, - { "10011", printDto.LabelType.ToString() } + { "10011", printDto.LabelType.ToString() }, + {"10013",null }, }; + var subString = _printHelper.GetNamedSubStrings(templatePath:printDto.Path).Result; + + var Intersect = printParameters.Where(x => subString.Contains(x.Key)).ToDictionary( + x=>x.Key,x=>x.Value + ); // 执行打印 bool printSuccess = _printHelper.PrintLabel( templatePath: printDto.Path, - subStringValues: printParameters, + subStringValues: Intersect, copies: 1); if (printSuccess) diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/ScanControlViewModel.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/ScanControlViewModel.cs index fb12ead..3bff303 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/ScanControlViewModel.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/ScanControlViewModel.cs @@ -9,6 +9,7 @@ using RIZO_Application.Infrastructure.Model; using RIZO_Helper.Tools; using System.Windows; using System.IO.Ports; +using System.Windows.Documents; namespace RIZO_Application.Modules.ModuleName.ViewModels { @@ -109,6 +110,7 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels IsConnected = false; // 发生错误时断开连接 } + // 定义事件处理方法 private void HandleMessage(object sender, string labelCode) { diff --git a/RIZO_Application/RIZO_Application.Infrastructure/Util/BartenderHelper/BartenderPrintHelper.cs b/RIZO_Application/RIZO_Application.Infrastructure/Util/BartenderHelper/BartenderPrintHelper.cs index b83b2e2..a1562c5 100644 --- a/RIZO_Application/RIZO_Application.Infrastructure/Util/BartenderHelper/BartenderPrintHelper.cs +++ b/RIZO_Application/RIZO_Application.Infrastructure/Util/BartenderHelper/BartenderPrintHelper.cs @@ -1,9 +1,11 @@ -using System; +using Microsoft.SqlServer.Server; +using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; namespace linesider_screen_tool { @@ -12,7 +14,7 @@ namespace linesider_screen_tool private const string BarTenderProgId = "BarTender.Application"; private object? _btApp; // 声明为可空类型 private int _disposedValue; - + public BartenderPrintHelper() { // 延迟初始化,在首次使用时创建 Bartender 应用实例 @@ -38,6 +40,51 @@ namespace linesider_screen_tool }, templatePath); } + + public Task> GetNamedSubStrings(string templatePath) + { + dynamic format =null; + dynamic btApp = null; + try + { + btApp = Activator.CreateInstance(Type.GetTypeFromProgID(BarTenderProgId)); + format = btApp.Formats.Open( + templatePath, + false, + null + ); + format.PrintSetup.IdenticalCopiesOfLabel = 1; + format.PrintSetup.NumberSerializedLabels = 1; + List subStrings = new List(); + dynamic namedSubStrings = format.NamedSubStrings; + + foreach (var iteam in namedSubStrings) + { + subStrings.Add(iteam.Name); + } + + return Task.FromResult(subStrings); + } + catch (Exception ex) + { + + Console.WriteLine($"发生错误: {ex.Message}"); + return null; + } + finally + { + // 清理资源 + if (format != null) + { + Marshal.ReleaseComObject(format); + } + if (btApp != null) + { + btApp.Quit(0); + Marshal.ReleaseComObject(_btApp); + } + } + } /// /// 批量打印标签(高性能实现) ///