首次提交:本地项目同步到Gitea
This commit is contained in:
62
LibShapes/Core/Print/AbstractPrintItemFactory.cs
Normal file
62
LibShapes/Core/Print/AbstractPrintItemFactory.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
public abstract class AbstractPrintItemFactory : IPrintItemFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 用这个来保存PrintItem
|
||||
/// </summary>
|
||||
protected Dictionary<string, PrintItem> dict_printItem = new Dictionary<string, PrintItem>();
|
||||
|
||||
|
||||
public PrintItem GetPrintItem(string name)
|
||||
{
|
||||
if (dict_printItem.ContainsKey(name))
|
||||
{
|
||||
return dict_printItem[name];
|
||||
}
|
||||
return null;
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除,受到保护的,外部不能随便调用的。
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
protected void deletePrintItem(string name)
|
||||
{
|
||||
dict_printItem.Remove(name);
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外部不能随便的增加
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="printItem"></param>
|
||||
protected void registerPrintItem(string name, PrintItem printItem)
|
||||
{
|
||||
if (! dict_printItem.ContainsKey(name))
|
||||
{
|
||||
// 没有才添加
|
||||
dict_printItem[name] = printItem;
|
||||
}
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public string registerPrintItem(PrintItem printItem)
|
||||
{
|
||||
string id = getNoRepeatId(); // 先取得一个不重复的id
|
||||
registerPrintItem(id, printItem); // 注册
|
||||
return id; // 返回这个id
|
||||
}
|
||||
|
||||
public abstract string getNoRepeatId();
|
||||
|
||||
}
|
||||
}
|
||||
21
LibShapes/Core/Print/IPrintItemFactory.cs
Normal file
21
LibShapes/Core/Print/IPrintItemFactory.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
/// <summary>
|
||||
/// 这个是取得PrintItem
|
||||
/// </summary>
|
||||
interface IPrintItemFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据名字取得PrintItem
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
PrintItem GetPrintItem(string name);
|
||||
|
||||
}
|
||||
}
|
||||
19
LibShapes/Core/Print/IPrintManager.cs
Normal file
19
LibShapes/Core/Print/IPrintManager.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
/// <summary>
|
||||
/// 打印管理者
|
||||
/// </summary>
|
||||
interface IPrintManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加一个新的打印
|
||||
/// </summary>
|
||||
/// <param name="printItem"></param>
|
||||
void addPrintItem(PrintItem printItem);
|
||||
}
|
||||
}
|
||||
55
LibShapes/Core/Print/PrintItem.cs
Normal file
55
LibShapes/Core/Print/PrintItem.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
/**
|
||||
* 外部往里边传入这个变量,
|
||||
* 内部把这个拆分成一张纸,比如一张纸上有10个模板,那么就10个变量集合,不用打印数量,打印机了。
|
||||
*
|
||||
* */
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
/// <summary>
|
||||
/// 打印项目
|
||||
/// </summary>
|
||||
public class PrintItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 打印机名称
|
||||
/// </summary>
|
||||
public string PrinterName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 图形
|
||||
/// </summary>
|
||||
public Shapes Shapes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 变量的集合
|
||||
/// </summary>
|
||||
public List<Dictionary<string, string>> Valss { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 打印数量
|
||||
/// </summary>
|
||||
public List<int> PrintCounts { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否充满打印,
|
||||
/// </summary>
|
||||
public bool isFullPrint { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public PrintItem()
|
||||
{
|
||||
// 初始化变量
|
||||
Valss = new List<Dictionary<string, string>>();
|
||||
PrintCounts = new List<int>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
239
LibShapes/Core/Print/PrintManagerImpl.cs
Normal file
239
LibShapes/Core/Print/PrintManagerImpl.cs
Normal file
@@ -0,0 +1,239 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Printing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
/// <summary>
|
||||
/// 打印管理者的实现
|
||||
/// </summary>
|
||||
public class PrintManagerImpl : AbstractPrintItemFactory, IPrintManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 打印的时候需要的id,区分不同的打印的。
|
||||
/// </summary>
|
||||
private int _id = 0;
|
||||
|
||||
public void addPrintItem(PrintItem printItem)
|
||||
{
|
||||
// 这里收到一个打印的部分,然后需要拆分,
|
||||
// 思路是,首先区分是否充满,充满的话,就是按照整数去打印,而不充满的,是要填充的。
|
||||
if (printItem.Valss.Count != printItem.PrintCounts.Count) throw new SizeNotEqual();
|
||||
// 判断是否是充满打印,由具体的函数去负责处理。
|
||||
if (printItem.isFullPrint)
|
||||
{
|
||||
addPrintItemFullPrint(printItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
addPrintItemNotFullPrint(printItem);
|
||||
|
||||
}
|
||||
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不是充满的打印。
|
||||
/// </summary>
|
||||
/// <param name="printItem"></param>
|
||||
protected void addPrintItemNotFullPrint(PrintItem printItem)
|
||||
{
|
||||
// 如下的提取出来是方便减少代码的。
|
||||
int rows = printItem.Shapes.Paper.Rows;
|
||||
int cols = printItem.Shapes.Paper.Cols;
|
||||
// 一个临时的打印变量。
|
||||
PrintItem printItem_tmp = new PrintItem();
|
||||
printItem_tmp.PrinterName = printItem.PrinterName;
|
||||
printItem_tmp.Shapes = printItem.Shapes;
|
||||
// 循环条件是还有要打印的。
|
||||
while (printItem.Valss.Count > 0)
|
||||
{
|
||||
// 判断前面的是否为空,为空表示下边的这个可以一次性打印几张。
|
||||
if (printItem_tmp.Valss.Count == 0)
|
||||
{
|
||||
// 判断是否判断是否大于一张
|
||||
if (printItem.PrintCounts.First() >= rows* cols)
|
||||
{
|
||||
|
||||
// 填充指定数量的变量
|
||||
for (int i = 0; i < rows*cols; i++)
|
||||
{
|
||||
printItem_tmp.Valss.Add(printItem.Valss.First());
|
||||
}
|
||||
int num = printItem.PrintCounts.First() / (rows * cols);// 取整
|
||||
printItem.PrintCounts[0] -= num * rows * cols; // 减去指定的数量
|
||||
sendToPrinter(printItem_tmp, num); // 发送给打印机
|
||||
printItem_tmp = new PrintItem(); // 重新
|
||||
printItem_tmp.PrinterName = printItem.PrinterName;
|
||||
printItem_tmp.Shapes = printItem.Shapes;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 这里表示不够一页啊
|
||||
for (int i = 0; i < printItem.PrintCounts.First(); i++)
|
||||
{
|
||||
printItem_tmp.Valss.Add(printItem.Valss.First());
|
||||
}
|
||||
// 添加上去
|
||||
printItem.PrintCounts[0] = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// 添加指定的多个。这里表示这次打印的数量是1张。
|
||||
// 这里判断是否够一张
|
||||
if (printItem.PrintCounts.First() + printItem_tmp.Valss.Count >= rows * cols)
|
||||
{
|
||||
// 看看还却多少。
|
||||
int i_max = rows * cols - printItem_tmp.Valss.Count;
|
||||
for (int i = 0; i < i_max; i++)
|
||||
{
|
||||
printItem.Valss.Add(printItem.Valss.First());
|
||||
}
|
||||
// 减去指定的多少。
|
||||
printItem.PrintCounts[0] -= i_max; // 减去指定的数量
|
||||
sendToPrinter(printItem_tmp, 1); // 发送给打印机
|
||||
printItem_tmp = new PrintItem(); // 重新
|
||||
printItem_tmp.PrinterName = printItem.PrinterName;
|
||||
printItem_tmp.Shapes = printItem.Shapes;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < printItem.PrintCounts.First(); i++)
|
||||
{
|
||||
printItem_tmp.Valss.Add(printItem.Valss.First());
|
||||
}
|
||||
printItem.PrintCounts[0] = 0;// 不够减,看看下一个把。
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 这里删除空白的
|
||||
while (printItem.PrintCounts.Count > 0 && printItem.PrintCounts.First() == 0)
|
||||
{
|
||||
// 删除第一个。
|
||||
printItem.Valss.RemoveAt(0);
|
||||
printItem.PrintCounts.RemoveAt(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 如果还有数据,就打印吧
|
||||
if (printItem_tmp.Valss.Count != 0)
|
||||
{
|
||||
sendToPrinter(printItem_tmp, 1); // 发送给打印机
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 充满的打印。
|
||||
/// </summary>
|
||||
/// <param name="printItem"></param>
|
||||
protected void addPrintItemFullPrint(PrintItem printItem)
|
||||
{
|
||||
// 如下的提取出来是方便减少代码的。
|
||||
int rows = printItem.Shapes.Paper.Rows;
|
||||
int cols = printItem.Shapes.Paper.Cols;
|
||||
// 每一个都是充满打印。
|
||||
for (int i = 0; i < printItem.Valss.Count; i++)
|
||||
{
|
||||
PrintItem printItem_tmp = new PrintItem(); //
|
||||
printItem_tmp.PrinterName = printItem.PrinterName; // 打印机
|
||||
printItem_tmp.Shapes = printItem.Shapes;
|
||||
for (int j = 0; j < rows * cols; j++)
|
||||
{
|
||||
printItem_tmp.Valss.Add(printItem.Valss[i]); // 添加多次就是啦。
|
||||
}
|
||||
// 这里计算打印的数量
|
||||
int num = (int)((printItem.PrintCounts[i] + 0.5) / (rows + cols));
|
||||
// 发送给打印机
|
||||
sendToPrinter(printItem_tmp, num);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送给打印机
|
||||
/// </summary>
|
||||
/// <param name="printItem"></param>
|
||||
/// <param name="printnum"></param>
|
||||
protected void sendToPrinter(PrintItem printItem, int printnum)
|
||||
{
|
||||
// 这里真正的发给打印机
|
||||
PrintDocument printDocument = new PrintDocument();
|
||||
printDocument.PrinterSettings.PrinterName = printItem.PrinterName; // 设置打印机
|
||||
printDocument.PrinterSettings.Copies = (short)printnum; // 设置打印数量
|
||||
printDocument.PrintController = new StandardPrintController(); //
|
||||
printDocument.OriginAtMargins = false; // 如果图形起始于页面边距,则为 true;如果图形原点位于该页可打印区域的左上角,则为 false。 默认值为 false。
|
||||
printDocument.DefaultPageSettings.PaperSize = new PaperSize(
|
||||
"custom",
|
||||
(int)(printItem.Shapes.Paper.PaperWidth / 10 / 2.54 * 100),
|
||||
(int)(printItem.Shapes.Paper.PaperHeight / 10 / 2.54 * 100)
|
||||
);
|
||||
//printDocument.DefaultPageSettings.Landscape = printItem.Shapes.Paper.Landscape; // 横向打印
|
||||
|
||||
// 然后设置打印的id
|
||||
string id = registerPrintItem(printItem);
|
||||
printDocument.DocumentName = id; // 这个id跟printItem是存在一一对应关系的
|
||||
printDocument.PrintPage += PrintDocument_PrintPage; // 打印事件处理
|
||||
printDocument.Print(); // 打印。
|
||||
printDocument.Dispose();
|
||||
|
||||
}
|
||||
|
||||
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
|
||||
{
|
||||
// 这里真正的打印,首先取得打印的数据
|
||||
PrintDocument printDocument = sender as PrintDocument;
|
||||
// 这里取得打印的数据了
|
||||
PrintItem printItem = GetPrintItem(printDocument.DocumentName);
|
||||
// 先提取出数据
|
||||
int rows = printItem.Shapes.Paper.Rows;
|
||||
int cols = printItem.Shapes.Paper.Cols;
|
||||
float top = printItem.Shapes.Paper.Top;
|
||||
float left = printItem.Shapes.Paper.Left;
|
||||
float modelWidth = printItem.Shapes.Paper.ModelWidth;
|
||||
float modelHeight = printItem.Shapes.Paper.ModelHeight;
|
||||
float hor = printItem.Shapes.Paper.HorizontalIntervalDistance;// 模板的水平间隔
|
||||
float ver = printItem.Shapes.Paper.VerticalIntervalDistance; // 模板的垂直间隔
|
||||
var valsss = printItem.Valss; // 变量集合。
|
||||
var shapes = printItem.Shapes; // 形状
|
||||
// 循环打印
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols; j++)
|
||||
{
|
||||
// 更改变量
|
||||
// 这里要判断一下是否超过索引
|
||||
if (i * rows + j >= printItem.Valss.Count) break;
|
||||
shapes.Vars = valsss[i * rows + j];
|
||||
// 然后计算偏移
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.Translate(
|
||||
left + j * (modelWidth + hor),
|
||||
top + i * (modelHeight + ver)
|
||||
);
|
||||
// 绘图
|
||||
shapes.Draw(e.Graphics, matrix, false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override string getNoRepeatId()
|
||||
{
|
||||
return (++_id).ToString();// 假设打印的部分很小,足够了把。
|
||||
//throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
14
LibShapes/Core/Print/SizeNotEqual.cs
Normal file
14
LibShapes/Core/Print/SizeNotEqual.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Io.Github.Kerwinxu.LibShapes.Core.Print
|
||||
{
|
||||
/// <summary>
|
||||
/// 尺寸不相等的异常
|
||||
/// </summary>
|
||||
public class SizeNotEqual :Exception
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user