diff --git a/SparkClient.csproj b/SparkClient.csproj
index 62c3379..76c0cdc 100644
--- a/SparkClient.csproj
+++ b/SparkClient.csproj
@@ -24,6 +24,7 @@
+
diff --git a/SparkClient.sln.DotSettings.user b/SparkClient.sln.DotSettings.user
index 4910ea3..79d18e2 100644
--- a/SparkClient.sln.DotSettings.user
+++ b/SparkClient.sln.DotSettings.user
@@ -12,6 +12,7 @@
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
@@ -31,9 +32,11 @@
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
@@ -50,11 +53,13 @@
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
ForceIncluded
ForceIncluded
diff --git a/ViewModel/Grading/GradingResultVM.cs b/ViewModel/Grading/GradingResultVM.cs
index 986bda2..f3cd760 100644
--- a/ViewModel/Grading/GradingResultVM.cs
+++ b/ViewModel/Grading/GradingResultVM.cs
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.Data;
using System.IO;
+using System.IO.Ports;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Input;
@@ -12,27 +13,16 @@ using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SparkClient.Views.Dialog;
-using NPOI.HPSF;
using Application = System.Windows.Application;
using MessageBox = SparkClient.Views.Dialog.MessageBox;
using System.Windows;
-using SharpDX;
using SparkClient.Views.UserControl.ViewportData.Entity;
using SparkClient.ViewModel.Configuration.SettingsPages;
using SparkClient.Views.UserControl.ViewportData.Helper;
-using System.Diagnostics.Metrics;
using log4net;
-using SparkClient.ViewModel.Configuration;
using System.Drawing.Printing;
using System.Drawing;
-using RectangleF = System.Drawing.RectangleF;
-using FontFamily = System.Drawing.FontFamily;
-using FontStyle = System.Drawing.FontStyle;
-using NPOI.SS.Formula.Functions;
-using System.Windows.Media;
-using Brush = System.Drawing.Brush;
-using Brushes = System.Drawing.Brushes;
-
+using HandyControl.Controls;
namespace SparkClient.ViewModel.Grading;
///
/// 检测结果画面VM
@@ -67,6 +57,10 @@ public class GradingResultVM : BaseViewModel
public ICommand SaveFileCommand { get; }
public ICommand SaveAsCommand { get; }
+ // 热敏标签打印
+ public ICommand ThermalPrintCommand { get; }
+ private SerialPort _serialPort;
+
///
/// 左侧计算结果展示数据
///
@@ -167,6 +161,10 @@ public class GradingResultVM : BaseViewModel
{
SaveAsCommand = new RelayCommand(SaveAs);
SaveFileCommand = new RelayCommand(SaveFile);
+ // 热敏标签打印
+ ThermalPrintCommand = new RelayCommand(ExecuteThermalPrint);
+ // 初始化 SerialPort
+ InitializeSerialPort();
if (result != null)
{
@@ -1672,131 +1670,247 @@ public class GradingResultVM : BaseViewModel
DataBaseHelper.ExecuteNonQuery(sql);
}
#endregion
+
+
+ // 执行热敏打印的方法
+ private void ExecuteThermalPrint(object parameter)
+ {
+ try
+ {
+ // 准备要打印的数据
+ string printData = PreparePrintData();
+
+ // 创建打印文档
+ PrintDocument printDocument = new PrintDocument();
+
+ // 确保 PrintPage 事件处理程序正确实现
+ printDocument.PrintPage += (sender, e) =>
+ {
+ // 设置打印字体和格式
+ Font printFont = new Font("Courier", 10);
+ SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
+ float yPos = 0f;
+ int linesPerPage = 0;
- public void PrintLabel()
+ // 逐行打印数据
+ using (StringReader sr = new StringReader(printData))
+ {
+ string line;
+ while ((line = sr.ReadLine()) != null)
+ {
+ yPos = e.MarginBounds.Top + (linesPerPage * printFont.GetHeight(e.Graphics));
+ e.Graphics.DrawString(line, printFont, drawBrush, e.MarginBounds.Left, yPos);
+ linesPerPage++;
+ }
+ }
+
+ // 表示已完成打印,不需要额外的页面
+ e.HasMorePages = false;
+ };
+
+ // 创建自定义打印预览窗体
+ CustomPrintPreviewForm customPreviewForm = new CustomPrintPreviewForm(printDocument);
+
+ // 显示自定义打印预览窗体
+ DialogResult result = customPreviewForm.ShowDialog();
+
+ if (result == DialogResult.OK)
+ {
+ // 打开串口
+ if (!_serialPort.IsOpen)
+ {
+ _serialPort.Open();
+ }
+
+ // 将数据转换为字节数组
+ byte[] dataToSend = System.Text.Encoding.UTF8.GetBytes(printData);
+
+ // 发送数据到串口
+ _serialPort.Write(dataToSend, 0, dataToSend.Length);
+
+ Logger.Info("数据已发送到热敏打印机。");
+ }
+ }
+ catch (Exception ex)
+ {
+ Logger.Error($"热敏打印发生错误: {ex.Message}");
+ Growl.Error("热敏打印发生错误");
+ }
+ finally
+ {
+ // 关闭串口
+ if (_serialPort.IsOpen)
+ {
+ _serialPort.Close();
+ }
+ }
+ }
+
+ // 自定义打印预览窗体类
+ public class CustomPrintPreviewForm : Form
{
- PrintDocument printDoc = new PrintDocument();
- printDoc.PrintPage += new PrintPageEventHandler(PrintPageHandler);
- printDoc.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 550, 300);
+ private PrintPreviewControl printPreviewControl;
+ private Button confirmButton;
+ private Button systemPrintButton; // 新增的系统打印按钮
+ private PrintDocument printDocument;
+
+ public CustomPrintPreviewForm(PrintDocument document)
+ {
+ this.printDocument = document;
- PrintDialog printDialog = new PrintDialog();
- printDialog.Document = printDoc;
+ // 初始化打印预览控件
+ printPreviewControl = new PrintPreviewControl();
+ printPreviewControl.Dock = DockStyle.Fill;
+ printPreviewControl.Document = printDocument;
- if (printDialog.ShowDialog() == DialogResult.OK)
+ // 初始化确认按钮
+ confirmButton = new Button();
+ confirmButton.Text = "确认打印";
+ confirmButton.Dock = DockStyle.Bottom;
+ confirmButton.Click += ConfirmButton_Click;
+
+ // 初始化系统打印按钮
+ systemPrintButton = new Button();
+ systemPrintButton.Text = "系统打印";
+ systemPrintButton.Dock = DockStyle.Bottom;
+ systemPrintButton.Click += SystemPrintButton_Click;
+
+ // 将控件添加到窗体
+ this.Controls.Add(printPreviewControl);
+ this.Controls.Add(confirmButton);
+ this.Controls.Add(systemPrintButton);
+
+ // 设置窗体属性
+ this.Size = new System.Drawing.Size(800, 600);
+ }
+
+ private void ConfirmButton_Click(object sender, EventArgs e)
{
- printDoc.Print();
+ this.DialogResult = DialogResult.OK;
+ this.Close();
+ }
+
+ private void SystemPrintButton_Click(object sender, EventArgs e)
+ {
+ // 创建一个打印对话框
+ PrintDialog printDialog = new PrintDialog();
+ // 将打印对话框关联到当前的 PrintDocument
+ printDialog.Document = printDocument;
+
+ // 显示打印对话框,如果用户点击了“确定”按钮
+ if (printDialog.ShowDialog() == DialogResult.OK)
+ {
+ // 使用用户在对话框中选择的打印机和设置进行打印
+ printDocument.Print();
+ }
}
}
+ // 准备要打印的数据
- private void PrintPageHandler(object sender, PrintPageEventArgs e)
+ private string PreparePrintData()
{
var result = algorithmResult.measurements;
- int startX = 10;
- int startY = 10;
- var col2X = startX + 90;
- var col3X = col2X + 70;
- // 设置字体
- Font font = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);
-
- // 设置颜色
- Brush brush = Brushes.Black;
-
- // 设置对齐方式
- StringFormat format = new StringFormat();
- format.Alignment = StringAlignment.Center; // 水平居中
- format.LineAlignment = StringAlignment.Center; // 垂直居中
-
- // 绘制文本
- // 第一行
- int X = startX;
- int Y = startY;
- e.Graphics.DrawString(Standard, font, brush, new PointF(X, Y));
- X += 70;
- e.Graphics.DrawString(Shape, font, brush, new PointF(X, Y));
- X += 50;
- e.Graphics.DrawString($"ID:{DiamondCode}", font, brush, new PointF(X, Y));
- X += 100;
- e.Graphics.DrawString($"Weight:{Wight}ct.", font, brush, new PointF(X, Y));
- X += 120;
- Brush backgroundBrush = new SolidBrush(System.Drawing.Color.Black); // 背景色为黄色
- e.Graphics.FillRectangle(backgroundBrush, new RectangleF(X, Y, 200, font.Height));
- e.Graphics.DrawString(CutLevelTotal, font, Brushes.White, new PointF(X, Y));
-
- // 第二行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Diameter", font, brush, new PointF(X, Y));
-
- e.Graphics.DrawString($"{FormatDouble_D(result.DIAMETER)}mm", font, brush, new PointF(col2X, Y));
-
- e.Graphics.DrawString($"({FormatDouble_D(result.M1)}mm-{FormatDouble_D(result.M2)}mm)", font, brush, new PointF(col3X, Y));
- e.Graphics.DrawString($"0.3%", font, brush, new PointF(310, Y));
- e.Graphics.FillRectangle(backgroundBrush, new RectangleF(350, Y, 200, font.Height));
- e.Graphics.DrawString(SymLevelTotal, font, Brushes.White, new PointF(X, Y));
-
- // 第三行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Total Dep.", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_D(result.DIAMETER * result.TOTAL_DEPTH)}mm", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"{FormatDouble_P(result.TOTAL_DEPTH)}%", font, brush, new PointF(X, Y));
- e.Graphics.DrawString($"{DtResults.Where(X=> "TOTAL_DEPTH".Equals(X.TestItemId)).Select(X=>X.CutLevel).FirstOrDefault()}", font, brush, new PointF(480, Y));
- // 第四行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Table", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_D(result.DIAMETER * result.TABLE)}mm", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"{FormatDouble_P(result.TABLE)}%", font, brush, new PointF(X, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "TABLE".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(480, Y));
- // 第五行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Crown", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_A(result.CROWN_ANGLE)}°", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"({FormatDouble_A(result.CROWN_ANGLE_MIN)}°-{FormatDouble_A(result.CROWN_ANGLE_MAX)}°)", font, brush, new PointF(X, Y));
- X += 120;
- e.Graphics.DrawString($"({FormatDouble_A(result.CROWN_HEIGHT)}%)", font, brush, new PointF(X, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "CROWN_ANGLE".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(410, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "CROWN_HEIGHT".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(480, Y));
- // 第六行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Pavil.", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_A(result.PAV_ANGLE)}°", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"({FormatDouble_A(result.PAV_ANGLE_MIN)}°-{FormatDouble_A(result.PAV_ANGLE_MAX)}°)", font, brush, new PointF(X, Y));
- X += 120;
- e.Graphics.DrawString($"({FormatDouble_A(result.PAV_DEPTH)}%)", font, brush, new PointF(X, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "PAV_ANGLE".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(410, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "PAV_DEPTH".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(480, Y));
- // 第七行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Culet", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_P(result.CULET)}%", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"Twist", font, brush, new PointF(X, Y));
- X += 60;
- e.Graphics.DrawString($"({FormatDouble_A(result.TWIST_MIN)}°-{FormatDouble_A(result.TWIST_MAX)}°)", font, brush, new PointF(X, Y));
- // 第八行
- X = startX;
- Y += font.Height;
- e.Graphics.DrawString("Girdle 16M", font, brush, new PointF(X, Y));
- X = col2X;
- e.Graphics.DrawString($"{FormatDouble_D(result.GIRDLE_VALLEY)}", font, brush, new PointF(X, Y));
- X = col3X;
- e.Graphics.DrawString($"({FormatDouble_P(result.GIRDLE_VALLEY_MIN)}%-{FormatDouble_P(result.GIRDLE_VALLEY_MAX)}%)", font, brush, new PointF(X, Y));
- e.Graphics.DrawString($"{DtResults.Where(X => "GIRDLE".Equals(X.TestItemId)).Select(X => X.CutLevel).FirstOrDefault()}", font, brush, new PointF(480, Y));
+ // 定义每个字段的目标宽度,确保每个字段有足够的空间
+ int paramWidth = 20; // 参数列宽度
+ int avgWidth = 15; // 平均值列宽度
+ int rangeWidth = 50; // 区间列宽度
+ int cutGradeWidth = 15; // 切工等级列宽度
+ int symGradeWidth = 15; // 对称性等级列宽度
+
+ // 格式化数值,确保数值位数一致
+ string formatNumber = "{0,8:F2}"; // 数值占8个字符宽度,保留两位小数
+
+ // 使用固定宽度填充每个字段,确保整齐
+ string data = $"{string.Format(formatNumber, "机构:IGI").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, "形状:圆形").PadRight(avgWidth)}" +
+ $"{string.Format(formatNumber, $"编码:{DiamondCode}").PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, $"重量: {Wight}ct.").PadRight(paramWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "参数").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, "平均值").PadRight(avgWidth)}" +
+ $"{string.Format(formatNumber, "区间(Min/Max)").PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "切工等级").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "对称性等级").PadRight(symGradeWidth)}" + "\n";
+
+ // 使用 PadRight 对齐每列,确保每一列内容在宽度内对齐
+ data += $"{string.Format(formatNumber, "直径(mm)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.DIAMETER).PadRight(avgWidth)}" +
+ $"{string.Format($"({formatNumber}-{formatNumber})", result.M1, result.M2).PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "完美").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "很好").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "全深比(%)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.TOTAL_DEPTH * 100).PadRight(avgWidth)}" +
+ $"{string.Format(formatNumber, "-").PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, CutLevelTotal).PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "-").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "台宽比(%)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.TABLE * 100).PadRight(avgWidth)}" +
+ $"{string.Format($"({formatNumber}-{formatNumber})", result.TABLE * 100 - 0.7, result.TABLE * 100 + 0.3).PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "极好").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "-").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "冠角(°)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.CROWN_ANGLE).PadRight(avgWidth)}" +
+ $"{string.Format($"({formatNumber}-{formatNumber})", result.CROWN_ANGLE_MIN, result.CROWN_ANGLE_MAX).PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "一般 - 差").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "好").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "冠高比(%)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.CROWN_HEIGHT).PadRight(avgWidth)}" +
+ $"{string.Format(formatNumber, "-").PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "完美").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "很好").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "波谷腰厚(%)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.GIRDLE_VALLEY).PadRight(avgWidth)}" +
+ $"{string.Format($"({formatNumber}-{formatNumber})", result.GIRDLE_VALLEY_MIN, result.GIRDLE_VALLEY_MAX).PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "极好").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "-").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "亭深比(%)").PadRight(paramWidth)}" +
+ $"{string.Format(formatNumber, result.PAV_DEPTH).PadRight(avgWidth)}" +
+ $"{string.Format($"({formatNumber}-{formatNumber})", result.PAV_DEPTH - 0.3, result.PAV_DEPTH + 0.3).PadRight(rangeWidth)}" +
+ $"{string.Format(formatNumber, "极好").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, "极好").PadRight(symGradeWidth)}" + "\n";
+
+ data += $"{string.Format(formatNumber, "综合切工等级: 一般 - 差").PadRight(paramWidth + avgWidth + rangeWidth)}" +
+ $"{string.Format(formatNumber, "综合对称等级: 好").PadRight(cutGradeWidth)}" +
+ $"{string.Format(formatNumber, $"签发日期: {DateTime.Now:yyyy/M/d}").PadRight(symGradeWidth)}" + "\n";
+
+ return data;
+ }
+
+ private void InitializeSerialPort()
+ {
+ try
+ {
+ string _serialPortName = "COM1"; // 根据实际情况设置串口号
+ int _baudRate = 9600; // 根据实际情况设置波特率
+
+ _serialPort = new SerialPort(_serialPortName, _baudRate);
+ _serialPort.ReadTimeout = 500;
+ _serialPort.WriteTimeout = 500;
+ }
+ catch (UnauthorizedAccessException ex)
+ {
+ Logger.Error($"无法访问串口: {ex.Message}");
+ Growl.Error("热敏打印发生错误");
+ }
+ catch (IOException ex)
+ {
+ Logger.Error($"串口操作失败: {ex.Message}");
+ Growl.Error("热敏打印发生错误");
+ }
+ catch (ArgumentOutOfRangeException ex)
+ {
+ Logger.Error($"参数超出范围: {ex.Message}");
+ Growl.Error("热敏打印发生错误");
+ }
}
+
#region 钻石操作相关
//暂略
//部分代码(直接操作控件)需要在xaml.cs里边写
diff --git a/Views/Grading/GradingResult.xaml b/Views/Grading/GradingResult.xaml
index 12dc145..cd66d0c 100644
--- a/Views/Grading/GradingResult.xaml
+++ b/Views/Grading/GradingResult.xaml
@@ -139,6 +139,26 @@
Margin="10 0 0 0" FontSize="16" Width="80" BorderBrush="Black" BorderThickness="0" Height="30"/>
+
+