Просмотр исходного кода

20251009001 添加注释。log功能添加,PDF生成工具添加

向羽 孟 1 месяц назад
Родитель
Сommit
1f1f50baea

+ 2 - 1
MvvmScaffoldFrame48.DLL/CameraTools/HikCamera.cs

@@ -1,4 +1,5 @@
-using MvCameraControl;
+// 海康相机实例类
+using MvCameraControl;
 using MvvmScaffoldFrame48.Model.HikVisionCamera;
 using System;
 using System.Collections.Generic;

+ 2 - 1
MvvmScaffoldFrame48.DLL/CameraTools/HikVision.cs

@@ -1,4 +1,5 @@
-using MvCameraControl;
+//海康相机操作类
+using MvCameraControl;
 using MvvmScaffoldFrame48.Model.HikVisionCamera;
 using System.Collections.Generic;
 

+ 2 - 1
MvvmScaffoldFrame48.DLL/CommunicationTools/ModbusTcpClient.cs

@@ -1,4 +1,5 @@
-using NModbus;
+//ModbusTCP客户端类
+using NModbus;
 using System;
 using System.Net.Sockets;
 

+ 2 - 1
MvvmScaffoldFrame48.DLL/ConfigTools/XMLReadWrite.cs

@@ -1,4 +1,5 @@
-using System;
+//XML读写类
+using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;

+ 459 - 0
MvvmScaffoldFrame48.DLL/FileTools/PDFGenerate.cs

@@ -0,0 +1,459 @@
+using iTextSharp.text;
+using iTextSharp.text.pdf;
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace MvvmScaffoldFrame48.DLL.FileTools
+{
+    public class PDFGenerate
+    {
+        #region 成员变量
+        /// <summary>
+        /// 是否保存标志量
+        /// </summary>
+        public bool IsSave = false;
+
+        /// <summary>
+        /// 字体位置
+        /// </summary>
+        string fontPath = @"C:\Windows\Fonts\MSYH.ttc,0";  // 微软雅黑
+        #endregion
+
+        #region 实例
+        /// <summary>
+        /// 文档对象
+        /// </summary>
+        private Document document = new Document(PageSize.A4);
+
+        /// <summary>
+        /// 字体实例
+        /// </summary>
+        BaseFont baseFont = null;
+        #endregion
+
+        #region 构造函数
+
+        /// <summary>
+        /// 构造函数
+        /// </summary>
+        /// <param name="outputPath">PDF生成位置</param>
+        public PDFGenerate(string outputPath)
+        {
+            PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create));
+            document.Open();
+            baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
+        }
+        #endregion
+
+        #region 公共方法
+
+        /// <summary>
+        /// 插入文本
+        /// </summary>
+        /// <param name="StrText">插入的文本</param>
+        public void InsertText(string StrText)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, 12);
+            Paragraph TextValue = new Paragraph(StrText, FontValue);
+            TextValue.Alignment = Element.ALIGN_CENTER;
+            document.Add(TextValue);
+        }
+
+        /// <summary>
+        /// 插入文本
+        /// </summary>
+        /// <param name="StrText">插入的文本</param>
+        /// <param name="FontSize">字体的字号</param>
+        public void InsertText(string StrText, int FontSize)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, FontSize);
+            Paragraph TextValue = new Paragraph(StrText, FontValue);
+            TextValue.Alignment = Element.ALIGN_CENTER;
+            document.Add(TextValue);
+        }
+
+        /// <summary>
+        /// 插入文本
+        /// </summary>
+        /// <param name="StrText">插入的文本</param>
+        /// <param name="FontSize">字体的字号</param>
+        /// <param name="Alignment">对齐方式</param>
+        public void InsertText(string StrText, int FontSize, int Alignment)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, FontSize);
+            Paragraph TextValue = new Paragraph(StrText, FontValue);
+            if (Alignment == 1)
+            {
+                TextValue.Alignment = Element.ALIGN_LEFT;
+            }
+            else if (Alignment == 2)
+            {
+                TextValue.Alignment = Element.ALIGN_RIGHT;
+            }
+            else if (Alignment == 3)
+            {
+                TextValue.Alignment = Element.ALIGN_CENTER;
+            }
+            else
+            {
+                Console.WriteLine("未定义对齐方式");
+            }
+            document.Add(TextValue);
+        }
+
+        /// <summary>
+        /// 插入图片
+        /// </summary>
+        /// <param name="imagePath">图片路径</param>
+        public void InsertImage(string imagePath)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, 12);
+            // 插入图片
+            try
+            {
+                Image img = Image.GetInstance(imagePath);
+                img.ScaleToFit(500, 300);
+                img.Alignment = Element.ALIGN_CENTER;
+                document.Add(img);
+            }
+            catch (Exception ex)
+            {
+                document.Add(new Paragraph("图片加载失败: " + ex.Message));
+            }
+        }
+
+        /// <summary>
+        /// 插入图片
+        /// </summary>
+        /// <param name="imagePath">图片路径</param>
+        public void InsertImage(byte[] imageBytes)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, 12);
+            // 插入图片
+            try
+            {
+                Image img = Image.GetInstance(imageBytes);
+                img.ScaleToFit(500, 300);
+                img.Alignment = Element.ALIGN_CENTER;
+                document.Add(img);
+            }
+            catch (Exception ex)
+            {
+                document.Add(new Paragraph("图片加载失败: " + ex.Message));
+            }
+        }
+
+        /// <summary>
+        /// 插入图片
+        /// </summary>
+        /// <param name="imagePath">图片路径</param>
+        /// <param name="width">图片宽度</param>
+        /// <param name="height">图片高度</param>
+        public void InsertImage(string imagePath, float width, float height)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, 12);
+            // 插入图片
+            try
+            {
+                Image img = Image.GetInstance(imagePath);
+                img.ScaleToFit(width, height);
+                img.Alignment = Element.ALIGN_CENTER;
+                document.Add(img);
+            }
+            catch (Exception ex)
+            {
+                document.Add(new Paragraph("图片加载失败: " + ex.Message));
+            }
+        }
+
+        /// <summary>
+        /// 插入图片
+        /// </summary>
+        /// <param name="imagePath">图片路径</param>
+        /// <param name="width">图片宽度</param>
+        /// <param name="height">图片高度</param>
+        public void InsertImage(byte[] imageBytes, float width, float height)
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            Font FontValue = new Font(baseFont, 12);
+            // 插入图片
+            try
+            {
+                Image img = Image.GetInstance(imageBytes);
+                img.ScaleToFit(width, height);
+                img.Alignment = Element.ALIGN_CENTER;
+                document.Add(img);
+            }
+            catch (Exception ex)
+            {
+                document.Add(new Paragraph("图片加载失败: " + ex.Message));
+            }
+        }
+
+        /// <summary>
+        /// 插入表格
+        /// </summary>
+        /// <param name="dataList">数据列表</param>
+        /// <param name="tableName">表名</param>
+        public void InsertTable<T>(List<T> dataList, string tableName = "表头") where T : class
+        {
+            if (IsSave)
+            {
+                return;
+            }
+
+            if (dataList == null || dataList.Count == 0)
+            {
+                return; // 如果数据为空则直接返回
+            }
+
+            Font FontValue = new Font(baseFont, 12);
+
+            // 使用反射获取类的属性作为列
+            var properties = typeof(T).GetProperties();
+            PdfPTable table = new PdfPTable(properties.Length);
+            table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
+
+            // 添加表名作为表头(跨所有列)
+            PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
+            cell.Colspan = properties.Length;
+            cell.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(cell);
+
+            // 添加属性名作为列标题
+            foreach (var prop in properties)
+            {
+                PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
+                headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(headerCell);
+            }
+
+            // 添加数据行
+            foreach (var data in dataList)
+            {
+                foreach (var prop in properties)
+                {
+                    var value = prop.GetValue(data)?.ToString() ?? "";
+                    table.AddCell(new Phrase(value, FontValue));
+                }
+            }
+
+            // 将表格添加到文档
+            document.Add(table);
+        }
+
+        /// <summary>
+        /// 插入表格
+        /// </summary>
+        /// <param name="dataList">数据列表</param>
+        /// <param name="tableName">表名</param>
+        public void InsertTable<T>(List<T> dataList, List<string> Colspan, string tableName = "表头") where T : class
+        {
+            if (IsSave)
+            {
+                return;
+            }
+
+            if (dataList == null || dataList.Count == 0)
+            {
+                return; // 如果数据为空则直接返回
+            }
+
+            Font FontValue = new Font(baseFont, 12);
+
+            // 使用反射获取类的属性作为列
+            var properties = typeof(T).GetProperties();
+            if (properties.Length != Colspan.Count) { return; }
+            PdfPTable table = new PdfPTable(Colspan.Count);
+            table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
+
+            // 添加表名作为表头(跨所有列)
+            PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
+            cell.Colspan = Colspan.Count;
+            cell.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(cell);
+
+            // 添加属性名作为列标题
+            foreach (var prop in Colspan)
+            {
+                PdfPCell headerCell = new PdfPCell(new Phrase(prop, FontValue));
+                headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(headerCell);
+            }
+
+            // 添加数据行
+            foreach (var data in dataList)
+            {
+                foreach (var prop in properties)
+                {
+                    var value = prop.GetValue(data)?.ToString() ?? "";
+                    table.AddCell(new Phrase(value, FontValue));
+                }
+            }
+
+            // 将表格添加到文档
+            document.Add(table);
+        }
+
+        /// <summary>
+        /// 插入表格
+        /// </summary>
+        /// <param name="dataList">数据列表</param>
+        /// <param name="tableName">表名</param>
+        public void InsertTable<T>(T data, string tableName = "表头") where T : class
+        {
+            if (IsSave)
+            {
+                return;
+            }
+
+            if (data == null)
+            {
+                return; // 如果数据为空则直接返回
+            }
+
+            Font FontValue = new Font(baseFont, 12);
+
+            // 使用反射获取类的属性作为列
+            var properties = typeof(T).GetProperties();
+            PdfPTable table = new PdfPTable(2);
+            table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
+
+            // 添加表名作为表头(跨所有列)
+            PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
+            cell.Colspan = 2;
+            cell.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(cell);
+
+            PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
+            headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(headerCell1);
+            PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
+            headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(headerCell2);
+
+            //// 添加属性名作为列标题
+            foreach (var prop in properties)
+            {
+                PdfPCell headerCell = new PdfPCell(new Phrase(prop.Name, FontValue));
+                headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(headerCell);
+
+                var value = prop.GetValue(data)?.ToString() ?? "";
+                PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
+                RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(RowValueCell);
+            }
+
+            // 将表格添加到文档
+            document.Add(table);
+        }
+
+        /// <summary>
+        /// 插入表格
+        /// </summary>
+        /// <param name="dataList">数据列表</param>
+        /// <param name="tableName">表名</param>
+        public bool InsertTable<T>(T data, List<string> Rowspan, string tableName = "表头") where T : class
+        {
+            bool Result = false;
+            if (IsSave)
+            {
+                return false;
+            }
+
+            if (data == null)
+            {
+                return false; // 如果数据为空则直接返回
+            }
+
+            Font FontValue = new Font(baseFont, 12);
+
+            // 使用反射获取类的属性作为列
+            var properties = typeof(T).GetProperties();
+            PdfPTable table = new PdfPTable(2);
+            table.WidthPercentage = 100; // 表格宽度为页面宽度的100%
+
+            // 添加表名作为表头(跨所有列)
+            PdfPCell cell = new PdfPCell(new Phrase(tableName, FontValue));
+            cell.Colspan = 2;
+            cell.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(cell);
+
+            PdfPCell headerCell1 = new PdfPCell(new Phrase("参数名", FontValue));
+            headerCell1.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(headerCell1);
+            PdfPCell headerCell2 = new PdfPCell(new Phrase("参数值", FontValue));
+            headerCell2.HorizontalAlignment = Element.ALIGN_CENTER;
+            table.AddCell(headerCell2);
+
+            if (Rowspan.Count != properties.Length) { return false; }
+            for (int i = 0; i < properties.Length; i++)
+            {
+                PdfPCell RowNameCell = new PdfPCell(new Phrase(Rowspan[i], FontValue));
+                RowNameCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(RowNameCell);
+
+                var value = properties[i].GetValue(data)?.ToString() ?? "";
+                PdfPCell RowValueCell = new PdfPCell(new Phrase(value, FontValue));
+                RowValueCell.HorizontalAlignment = Element.ALIGN_CENTER;
+                table.AddCell(RowValueCell);
+            }
+
+            // 将表格添加到文档
+            document.Add(table);
+            Result = true;
+            return Result;
+        }
+
+        /// <summary>
+        /// 插入空白行
+        /// </summary>
+        public void InsertNewLine()
+        {
+            if (IsSave)
+            {
+                return;
+            }
+            document.Add(Chunk.NEWLINE);
+        }
+
+        /// <summary>
+        /// 保存PDF文件
+        /// </summary>
+        public void SavePDF()
+        {
+            document.Close();
+            IsSave = true;
+        }
+        #endregion
+    }
+}

+ 200 - 0
MvvmScaffoldFrame48.DLL/LogTools/TxtLog.cs

@@ -0,0 +1,200 @@
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Text;
+
+namespace MvvmScaffoldFrame48.DLL.LogTools
+{
+    public class TxtLog
+    {
+        #region 变量
+        static public int min = 0;
+        static public int max = 10;
+        #endregion
+
+        #region 实例
+        static public StringBuilder _log = new StringBuilder();
+        static TxtGenerate fio;
+        #endregion
+
+        #region 构造方法
+        static TxtLog()
+        {
+            fio = new TxtGenerate();
+            fio.OpenWriteFile("log.txt");
+        }
+        #endregion
+
+        #region 静态方法
+        static public void error(object str)
+        {
+            log("Erorr:" + str, 10);
+        }
+        /// <summary>
+        /// log写入
+        /// </summary>
+        /// <param name="str"></param>
+        static public void log(object str)
+        {
+            log(str, 0);
+        }
+        static public void log(string str)
+        {
+            log(str, 0);
+        }
+        /// <summary>
+        /// log等级定制
+        /// </summary>
+        /// <param name="p"></param>
+        /// <param name="level"></param>
+        public static void log(object p, int level)
+        {
+            log(p.ToString(), level);
+        }
+
+        static public void log(string str, int level)
+        {
+            if (level < fio.LogLevel)
+            {
+                return;
+            }
+            if (str == null)
+            {
+                str = "null";
+            }
+            fio.WriteLine(DateTime.Now.ToString("O") + "->" + str + "\r\n");
+        }
+        /// <summary>
+        /// 修改Log的限制等级0日志最多,10最少
+        /// </summary>
+        /// <param name="Loglevel"></param>
+        public static void ChangeLogLevel(int Loglevel)
+        {
+            fio.LogLevel = Loglevel;
+        }
+
+        public static void LogSurPlusMemory()
+        {
+            PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");
+            var ramAvailable = ramCounter.NextValue();
+            string ramAvaiableStr = string.Format("{0} MB", ramAvailable);
+            log($"剩余物理内存:{ramAvaiableStr}");
+        }
+        #endregion
+
+    }
+
+    public class TxtGenerate
+    {
+        #region 变量
+        private string LogFloderPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
+        private double LogFileSize = 128 * Math.Pow(1024, 3);
+        public int LogLevel = 5;
+        #endregion
+
+        #region 实例
+        private FileStream fsr;
+        private FileStream fsw;
+        private StreamWriter sw;
+        private StreamReader sr;
+        #endregion
+
+        #region 共有方法
+        /// <summary>
+        /// 创建用于读取文件行的文件流和StreamWriter对象
+        /// </summary>
+        /// <param name="file"></param>
+        public void OpenReadFile(string file)
+        {
+            if (!File.Exists(file))
+                File.Create(file).Close();
+            fsr = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
+            sr = new StreamReader(fsr);
+        }
+
+        /// <summary>
+        /// 关闭读文件流
+        /// </summary>
+        public void CloseReadFile()
+        {
+            if (fsr != null)
+                fsr.Close();
+        }
+
+        /// <summary>
+        /// 创建用于向文件中追加行的文件流和StreamWriter对象
+        /// </summary>
+        /// <param name="file"></param>
+        public void OpenWriteFile(string file)
+        {
+            int i = 1;
+            while (true)
+            {
+                if (!Directory.Exists(LogFloderPath))//如果不存在就创建file文件夹
+                {
+                    Directory.CreateDirectory(LogFloderPath);
+                }
+                if (!File.Exists(LogFloderPath + file))  // 如果文件不存在,先创建这个文件
+                    File.Create(LogFloderPath + file).Close();
+                // 以追加模式打开这个文件
+                fsw = new FileStream(LogFloderPath + file, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
+                if (fsw.Length > LogFileSize)
+                {
+                    file = "log" + i + ".txt";
+                    i++;
+                    continue;
+                }
+                break;
+            }
+            // 根据创建的FileStream对象来创建StreamWriter对象
+            sw = new StreamWriter(fsw);
+        }
+
+        /// <summary>
+        /// 关闭写文件流
+        /// </summary>
+        public void CloseWriteFile()
+        {
+            if (fsw != null)
+                fsw.Close();
+        }
+
+        /// <summary>
+        /// 从文件中读取一行
+        /// </summary>
+        /// <returns></returns>
+        public string ReadLine()
+        {
+            if (sr.EndOfStream)  // 如果文件流指针已经指向文件尾部,返回null
+                return null;
+            return sr.ReadLine();
+        }
+
+        /// <summary>
+        /// 向文件中追加一行字符串
+        /// </summary>
+        /// <param name="s"></param>
+        public void WriteLine(string s)
+        {
+            if (fsw.Length > LogFileSize)
+            {
+                OpenWriteFile("log.txt");
+            }
+            lock (sw)
+            {
+                sw.WriteLine(s);
+                sw.Flush(); // 刷新写入缓冲区,使这一行对于读文件流可见
+            }
+        }
+
+        /// <summary>
+        /// 用于判断文件流指针是否位于文件尾部
+        /// </summary>
+        /// <returns></returns>
+        public bool IsEof()
+        {
+            return sr.EndOfStream;
+        }
+        #endregion
+    }
+}

+ 5 - 0
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -53,6 +53,8 @@
     <Compile Include="CameraTools\HikVision.cs" />
     <Compile Include="CommunicationTools\ModbusTcpClient.cs" />
     <Compile Include="ConfigTools\XMLReadWrite.cs" />
+    <Compile Include="FileTools\PDFGenerate.cs" />
+    <Compile Include="LogTools\TxtLog.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UserManager.cs" />
   </ItemGroup>
@@ -67,6 +69,9 @@
     <Content Include="DLL\MvFGCtrlC.Net.dll" />
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="iTextSharp">
+      <Version>5.5.13.4</Version>
+    </PackageReference>
     <PackageReference Include="NModbus">
       <Version>3.0.81</Version>
     </PackageReference>

+ 10 - 0
MvvmScaffoldFrame48.MODEL/HikVisionCamera/CameraImageSizeCModel.cs

@@ -6,9 +6,19 @@ using System.Threading.Tasks;
 
 namespace MvvmScaffoldFrame48.Model.HikVisionCamera
 {
+    /// <summary>
+    /// 摄像头图片大小
+    /// </summary>
     public class CameraImageSizeCModel
     {
+        /// <summary>
+        /// 图像宽
+        /// </summary>
         public int Width { get; set; }
+
+        /// <summary>
+        /// 图像高
+        /// </summary>
         public int Height { get; set; }
     }
 }

+ 6 - 0
MvvmScaffoldFrame48.MODEL/HikVisionCamera/CameraInfoModel.cs

@@ -11,8 +11,14 @@ namespace MvvmScaffoldFrame48.Model.HikVisionCamera
     /// </summary>
     public class CameraInfoModel
     {
+        /// <summary>
+        /// 设备名称
+        /// </summary>
         public string DeviceName { get; set; }
 
+        /// <summary>
+        /// 设备序列号
+        /// </summary>
         public string DeviceSN { get; set; }
     }
 }

+ 2 - 1
MvvmScaffoldFrame48.VIEWMODEL/BaseViewModel.cs

@@ -1,4 +1,5 @@
-using System;
+// ViewModel数据同步基类
+using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;

+ 2 - 7
MvvmScaffoldFrame48.VIEWMODEL/MainViewModel.cs

@@ -1,12 +1,7 @@
-using MvvmScaffoldFrame48.DLL;
+// 演示类
+using MvvmScaffoldFrame48.DLL;
 using MvvmScaffoldFrame48.Model;
-using System;
-using System.Collections.Generic;
 using System.Collections.ObjectModel;
-using System.ComponentModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using System.Windows.Input;
 
 namespace MvvmScaffoldFrame48.ViewModel

+ 2 - 1
MvvmScaffoldFrame48.VIEWMODEL/RelayComand.cs

@@ -1,4 +1,5 @@
-using System;
+// 事件传递类,用于传递界面的事件
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;