597 lines
20 KiB
C++
597 lines
20 KiB
C++
#include "drawdata.h"
|
||
|
||
CreatPictureStr creatPictureByData(Marker marker,int paperWidth,int butSpace,int rightSpace,int penWidth)
|
||
{
|
||
QPainterPath painterPath;
|
||
CBitmapInfo bitmapInfo;
|
||
QRectF rect = marker.GetRect();
|
||
int minX = rect.left();
|
||
int maxY = rect.bottom();
|
||
int rectSize = 20;//20毫米-对接符矩形框大小
|
||
int blockSpace = 50;//50毫米-块与块之间的间距
|
||
|
||
int nLineCount = marker.m_listPolyline.size();
|
||
for(int i = 0; i < nLineCount; i++)
|
||
{
|
||
CRPPolyline polyLine = marker.m_listPolyline.at(i);
|
||
int type = polyLine.m_nDrawingType;
|
||
// if(polyLine.m_nDrawingType == 3)//文字
|
||
// {
|
||
// CRPText text = polyLine->m_text;
|
||
// }
|
||
|
||
if(type == 0)//直线
|
||
{
|
||
int nPointCount = polyLine.m_listPoint.size();
|
||
for(int j = 0; j < nPointCount; j++)
|
||
{
|
||
double x = (polyLine.m_listPoint.at(j).x() - minX)/(double)M_IDPMM*MMPIXELY;
|
||
double y = (maxY - polyLine.m_listPoint.at(j).y())/(double)M_IDPMM*MMPIXELY;
|
||
QPointF point(x,y);
|
||
|
||
if(j == 0)
|
||
{
|
||
painterPath.moveTo(point);
|
||
}
|
||
else
|
||
{
|
||
painterPath.lineTo(point);
|
||
}
|
||
}
|
||
}
|
||
else if(type == 1)//位图
|
||
{
|
||
bitmapInfo = polyLine.m_bitmapInfo;
|
||
|
||
int lux = bitmapInfo.m_ptAbPostLU.x();
|
||
int luy = bitmapInfo.m_ptAbPostLU.y();
|
||
int nlux = (lux - minX)/M_IDPMM*MMPIXELY;
|
||
int nluy = (maxY - luy)/M_IDPMM*MMPIXELY;
|
||
bitmapInfo.m_ptAbPostLU.setX(nlux);
|
||
bitmapInfo.m_ptAbPostLU.setY(nluy);
|
||
|
||
int rdx = bitmapInfo.m_ptAbPostRD.x();
|
||
int rdy = bitmapInfo.m_ptAbPostRD.y();
|
||
int nrdx = (rdx - minX)/M_IDPMM*MMPIXELY;
|
||
int nrdy = (maxY - rdy)/M_IDPMM*MMPIXELY;
|
||
bitmapInfo.m_ptAbPostRD.setX(nrdx);
|
||
bitmapInfo.m_ptAbPostRD.setY(nrdy);
|
||
}
|
||
}
|
||
|
||
if(painterPath.isEmpty())
|
||
{
|
||
qDebug()<<"painterPath.isEmpty";
|
||
}
|
||
|
||
CreatPictureStr picStr;
|
||
QPen pen;
|
||
pen.setWidth(1);//设置笔号,像素为1,用于显示的pic
|
||
pen.setColor(QColor(Qt::black));
|
||
QPainter painter;
|
||
|
||
//当纸张宽度不够打印时需要对接打印
|
||
int width = rect.width()/M_IDPMM;
|
||
int length = rect.height()/M_IDPMM;
|
||
|
||
if(rightSpace != 0)
|
||
{
|
||
if(paperWidth >= length)
|
||
{
|
||
paperWidth = length - rightSpace;
|
||
}
|
||
}
|
||
|
||
double oft = paperWidth*MMPIXELY;
|
||
if(width <= rectSize)
|
||
{
|
||
rectSize = width / 2;
|
||
blockSpace = rectSize / 2;
|
||
}
|
||
int square = rectSize*MMPIXELY;
|
||
|
||
if(paperWidth < length)
|
||
{
|
||
int seNum = length / paperWidth;
|
||
if(length % paperWidth != 0)
|
||
{
|
||
seNum += 1;
|
||
}
|
||
|
||
butSpace += rectSize;
|
||
int num = width / butSpace;
|
||
double space = (double)width / (double)num;
|
||
for(int m = 0; m < seNum-1; m++)
|
||
{
|
||
for(int i = 0; i < num; i++)
|
||
{
|
||
//绘制对接符,5mm的正方形,里面是交叉线X
|
||
//添加正方形path
|
||
int x = i*space * MMPIXELY;
|
||
int y = oft + m * oft - square / 2;
|
||
painterPath.moveTo(x,y);
|
||
QRectF rect(x,y,square,square);
|
||
painterPath.addRect(rect);
|
||
|
||
//添加交叉线(X)
|
||
QPointF diagonalStart(rect.left(), rect.top());
|
||
QPointF diagonalEnd(rect.right(), rect.bottom());
|
||
painterPath.moveTo(diagonalStart);
|
||
painterPath.lineTo(diagonalEnd);
|
||
// 另一条对角线
|
||
QPointF otherDiagonalStart(rect.right(), rect.top());
|
||
QPointF otherDiagonalEnd(rect.left(), rect.bottom());
|
||
painterPath.moveTo(otherDiagonalStart);
|
||
painterPath.lineTo(otherDiagonalEnd);
|
||
}
|
||
}
|
||
QPainterPath pathSegments[seNum];
|
||
splitPaintPath(painterPath,oft,seNum,pathSegments);
|
||
|
||
int startx,starty,targetWidth,targetHeight,blockSize,bmpNum;
|
||
if(bitmapInfo.m_iBytes > 0)//有位图
|
||
{
|
||
startx = bitmapInfo.m_ptAbPostLU.x();
|
||
starty = bitmapInfo.m_ptAbPostLU.y();
|
||
targetWidth = abs(bitmapInfo.m_ptAbPostRD.x() - bitmapInfo.m_ptAbPostLU.x());
|
||
targetHeight = abs(bitmapInfo.m_ptAbPostRD.y() - bitmapInfo.m_ptAbPostLU.y());
|
||
blockSize = paperWidth * MMPIXELY;
|
||
|
||
if(targetHeight > blockSize)
|
||
{
|
||
bmpNum = targetHeight / blockSize;
|
||
if(targetHeight % blockSize != 0)
|
||
{
|
||
bmpNum += 1;
|
||
}
|
||
}
|
||
}
|
||
|
||
painter.begin(&picStr.showPic);
|
||
painter.setPen(pen);
|
||
int oftH = 0;
|
||
int oftW = 0;
|
||
for(int i = 0; i < seNum; i++)
|
||
{
|
||
if(i != 0)
|
||
{
|
||
oftH = 0 - pathSegments[i].boundingRect().height();
|
||
oftW = pathSegments[i].boundingRect().width() + blockSpace * MMPIXELY;
|
||
}
|
||
painter.translate(oftW, oftH);
|
||
painter.drawPath(pathSegments[i]);
|
||
|
||
int boftH = i*(0 - (starty + oftH));
|
||
if(targetHeight > blockSize)
|
||
{
|
||
int bmpH = bitmapInfo.m_pBitmap.height() / bmpNum;
|
||
|
||
int bmpOft = i*bmpH;
|
||
painter.drawPixmap(startx,boftH,targetWidth,blockSize,bitmapInfo.m_pBitmap,
|
||
startx,bmpOft, bitmapInfo.m_pBitmap.width(), bmpH);
|
||
}
|
||
else
|
||
{
|
||
if(startx <= abs(oftW) && starty <= abs(oftH))
|
||
{
|
||
painter.drawPixmap(startx,boftH,targetWidth,targetHeight,bitmapInfo.m_pBitmap,
|
||
startx,starty, bitmapInfo.m_pBitmap.width(), bitmapInfo.m_pBitmap.height());
|
||
}
|
||
}
|
||
}
|
||
painter.end();
|
||
|
||
painter.begin(&picStr.sendPic);
|
||
pen.setWidth(penWidth);
|
||
painter.setPen(pen);
|
||
oftH = 0;
|
||
oftW = 0;
|
||
for(int i = 0; i < seNum; i++)
|
||
{
|
||
if(i != 0)
|
||
{
|
||
oftH = 0 - pathSegments[i].boundingRect().height();
|
||
oftW = pathSegments[i].boundingRect().width() + blockSpace * MMPIXELY;
|
||
}
|
||
painter.translate(oftW, oftH);
|
||
painter.drawPath(pathSegments[i]);
|
||
|
||
int boftH = i*(0 - (starty + oftH));
|
||
if(targetHeight > blockSize)
|
||
{
|
||
int bmpH = bitmapInfo.m_pBitmap.height() / bmpNum;
|
||
|
||
int bmpOft = i*bmpH;
|
||
painter.drawPixmap(startx,boftH,targetWidth,blockSize,bitmapInfo.m_pBitmap,
|
||
startx,bmpOft, bitmapInfo.m_pBitmap.width(), bmpH);
|
||
}
|
||
else
|
||
{
|
||
if(startx <= abs(oftW) && starty <= abs(oftH))
|
||
{
|
||
painter.drawPixmap(startx,boftH,targetWidth,targetHeight,bitmapInfo.m_pBitmap,
|
||
startx,starty, bitmapInfo.m_pBitmap.width(), bitmapInfo.m_pBitmap.height());
|
||
}
|
||
}
|
||
}
|
||
painter.end();
|
||
return picStr;
|
||
}
|
||
|
||
//将路径画在picture上
|
||
painter.begin(&picStr.showPic);
|
||
painter.setPen(pen);
|
||
painter.drawPath(painterPath);
|
||
if(bitmapInfo.m_iBytes > 0)//有位图
|
||
{
|
||
int startx = bitmapInfo.m_ptAbPostLU.x();
|
||
int starty = bitmapInfo.m_ptAbPostLU.y();
|
||
//bitmapInfo.m_pBitmap.save("D:\\1.bmp");
|
||
int targetWidth = abs(bitmapInfo.m_ptAbPostRD.x() - bitmapInfo.m_ptAbPostLU.x());
|
||
int targetHeight = abs(bitmapInfo.m_ptAbPostRD.y() - bitmapInfo.m_ptAbPostLU.y());
|
||
painter.drawPixmap(startx,starty,targetWidth,targetHeight,bitmapInfo.m_pBitmap,
|
||
startx,starty, bitmapInfo.m_pBitmap.width(), bitmapInfo.m_pBitmap.height());
|
||
// painter.drawPixmap(bitmapInfo.m_ptAbPostLU.x(),bitmapInfo.m_ptAbPostLU.y(),bitmapInfo.m_pBitmap);
|
||
}
|
||
painter.end();
|
||
|
||
painter.begin(&picStr.sendPic);
|
||
pen.setWidth(penWidth);
|
||
painter.setPen(pen);//设置笔号,像素为penWidth,用于发送的pic
|
||
painter.drawPath(painterPath);
|
||
if(bitmapInfo.m_iBytes > 0)//有位图
|
||
{
|
||
int startx = bitmapInfo.m_ptAbPostLU.x();
|
||
int starty = bitmapInfo.m_ptAbPostLU.y();
|
||
//bitmapInfo.m_pBitmap.save("D:\\1.bmp");
|
||
int targetWidth = abs(bitmapInfo.m_ptAbPostRD.x() - bitmapInfo.m_ptAbPostLU.x());
|
||
int targetHeight = abs(bitmapInfo.m_ptAbPostRD.y() - bitmapInfo.m_ptAbPostLU.y());
|
||
painter.drawPixmap(startx,starty,targetWidth,targetHeight,bitmapInfo.m_pBitmap,
|
||
startx,starty, bitmapInfo.m_pBitmap.width(), bitmapInfo.m_pBitmap.height());
|
||
// painter.drawPixmap(bitmapInfo.m_ptAbPostLU.x(),bitmapInfo.m_ptAbPostLU.y(),bitmapInfo.m_pBitmap);
|
||
}
|
||
painter.end();
|
||
|
||
return picStr;
|
||
}
|
||
|
||
CreatPictureStr creatPictureByBmp(QPixmap pixmap,int paperWidth,int butSpace,int rightSpace,int penWidth)
|
||
{
|
||
//将图片画在picture上
|
||
CreatPictureStr picStr;
|
||
QPen pen;
|
||
pen.setWidth(1);//设置笔号
|
||
pen.setColor(QColor(Qt::black));
|
||
int height = pixmap.height() / MMPIXELY;
|
||
|
||
if(rightSpace != 0)
|
||
{
|
||
if(paperWidth >= height)
|
||
{
|
||
paperWidth = height - rightSpace;
|
||
}
|
||
}
|
||
|
||
QPainter painter;
|
||
painter.begin(&picStr.showPic);
|
||
painter.setPen(pen);
|
||
if(height > paperWidth)
|
||
{
|
||
//功能未实现
|
||
if(butSpace == 0)//为了去掉警告
|
||
{
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
painter.drawPixmap(0,0,pixmap);
|
||
}
|
||
painter.end();
|
||
|
||
pen.setWidth(penWidth);//设置笔号
|
||
pen.setColor(QColor(Qt::black));
|
||
painter.begin(&picStr.sendPic);
|
||
painter.setPen(pen);
|
||
if(height > paperWidth)
|
||
{
|
||
//功能未实现
|
||
}
|
||
else
|
||
{
|
||
painter.drawPixmap(0,0,pixmap);
|
||
}
|
||
painter.end();
|
||
|
||
return picStr;
|
||
}
|
||
|
||
void creatMarkerDat(McFilesInfo &curFilesInfo, QString printDir, QString filePath, int fileIdx, double angle)
|
||
{
|
||
QFileInfo fileInfo(filePath);
|
||
curFilesInfo.m_marker.Initialize();
|
||
|
||
if(fileInfo.suffix().toUpper() == "DXF")
|
||
{
|
||
curFilesInfo.m_fileType = TYPE_FILE;
|
||
DxfHelper dxfHelper;
|
||
if(dxfHelper.generateDxf(filePath))
|
||
{
|
||
QList <CRPPolyline> polylineList;
|
||
CRPPolyline polyline;
|
||
QVector<QVector<QPointF>> dxfPaths = dxfHelper.getDxfPaths(); // 获取转换出来的dxf所有类型图形的点集
|
||
//创建mark数据
|
||
for (int j = 0; j < dxfPaths.size(); j++)
|
||
{
|
||
polyline.m_listPoint.clear();
|
||
for(int i = 0; i < dxfPaths[j].length(); i++)
|
||
{
|
||
QPointF point = dxfPaths[j].at(i);
|
||
polyline.m_listPoint.append(point);
|
||
}
|
||
polylineList.append(polyline);
|
||
}
|
||
curFilesInfo.m_marker.m_listPolyline = polylineList;
|
||
curFilesInfo.m_fileRect = curFilesInfo.m_marker.GetRect();
|
||
}
|
||
}
|
||
else if(fileInfo.suffix().toUpper() == "PLT")
|
||
{
|
||
ImportHPGL importHPGL;
|
||
curFilesInfo.m_fileType = TYPE_FILE;
|
||
|
||
//判断是否为加密文件
|
||
if (importHPGL.IsSecretFile(filePath) == true)
|
||
{
|
||
QString strSecretFile;
|
||
if(fileIdx == -1)
|
||
{
|
||
// 文件路径
|
||
QDir apppath(qApp->applicationDirPath());
|
||
strSecretFile = apppath.path() + apppath.separator() + "Secret.plt";
|
||
}
|
||
else
|
||
{
|
||
// 文件路径
|
||
strSecretFile = printDir + QString::number(fileIdx+1) ;
|
||
|
||
QDir fileDir(strSecretFile);//对应每台机器的每个文件打印目录
|
||
if(!fileDir.exists())
|
||
{
|
||
fileDir.mkdir(strSecretFile);
|
||
}
|
||
strSecretFile = strSecretFile + fileDir.separator() + "Secret.plt";
|
||
}
|
||
//qDebug()<<"strSecretFile"<<strSecretFile;
|
||
|
||
importHPGL.BitMapDtat(filePath,strSecretFile);
|
||
importHPGL.IniPara();
|
||
importHPGL.setRotateAngle(angle);
|
||
importHPGL.ReadSecretFile(strSecretFile,&curFilesInfo.m_marker);
|
||
//qDebug()<<"importHPGL.IsSecretFile";
|
||
}
|
||
else
|
||
{
|
||
importHPGL.IniPara();
|
||
importHPGL.setRotateAngle(angle);
|
||
importHPGL.Read(filePath,&curFilesInfo.m_marker);
|
||
}
|
||
curFilesInfo.m_fileRect = curFilesInfo.m_marker.GetRect();
|
||
}
|
||
else
|
||
{
|
||
curFilesInfo.m_fileType = TYPE_IMAGE;
|
||
QBitmap pixmap;
|
||
pixmap.load(filePath);
|
||
QRect rect;
|
||
rect.setRect(0,0,pixmap.width()/MMPIXELY*M_IDPMM,pixmap.height()/MMPIXELY*M_IDPMM);
|
||
|
||
curFilesInfo.m_pixmap = pixmap;
|
||
curFilesInfo.m_fileRect = rect;
|
||
}
|
||
|
||
curFilesInfo.m_filePath = filePath;
|
||
curFilesInfo.m_fileName = fileInfo.fileName();
|
||
curFilesInfo.m_printNum = 1;
|
||
curFilesInfo.m_startPoint = 0;
|
||
curFilesInfo.m_printState = Waitting;
|
||
curFilesInfo.m_curPrintBlock = 0;//当前打印块数
|
||
curFilesInfo.m_printedBlockNum = 0;//已打印块数
|
||
curFilesInfo.m_selectBlockNum = 0;//当前选择块数
|
||
curFilesInfo.m_creatDataFlag = 1;
|
||
}
|
||
|
||
void splitPaintPath(QPainterPath originalPath,double segmentHeight,int segmenNum,QPainterPath *pathSegments)
|
||
{
|
||
QPainterPath::Element element = originalPath.elementAt(0);
|
||
QPointF firstPoint(element.x, element.y);
|
||
qreal currentY = 0;
|
||
pathSegments[0].moveTo(firstPoint);
|
||
|
||
for (int i = 1; i < originalPath.elementCount(); i++)
|
||
{
|
||
QPainterPath::Element element = originalPath.elementAt(i);
|
||
QPointF curPoint(element.x, element.y);
|
||
QPointF prePoint(originalPath.elementAt(i-1).x, originalPath.elementAt(i-1).y);
|
||
|
||
QPointF prePot = prePoint;
|
||
int addFlag = 0;
|
||
for(int j = 0; j < segmenNum; j++)
|
||
{
|
||
currentY = segmentHeight + segmentHeight * j;
|
||
//在此块内
|
||
if(curPoint.y() <= currentY && curPoint.y() >= (currentY - segmentHeight) &&
|
||
prePoint.y() <= currentY && prePoint.y() >= (currentY - segmentHeight))
|
||
{
|
||
if (element.type == QPainterPath::MoveToElement)
|
||
{
|
||
pathSegments[j].moveTo(prePot);
|
||
pathSegments[j].moveTo(curPoint);
|
||
}
|
||
if (element.type == QPainterPath::LineToElement)
|
||
{
|
||
pathSegments[j].moveTo(prePot);
|
||
pathSegments[j].lineTo(curPoint);
|
||
}
|
||
addFlag = 1;
|
||
break;
|
||
}
|
||
}
|
||
|
||
//未添加时继续判断
|
||
if(addFlag == 0)
|
||
{
|
||
//跨块,要判断跨了几块,求出交点
|
||
//判断prePoint在哪个块
|
||
int preIdx = 0;
|
||
int curIdx = 0;
|
||
int flag = 0;
|
||
for(int m = 0; m < segmenNum; m++)
|
||
{
|
||
qreal height = segmentHeight + segmentHeight * m;
|
||
//在此块内
|
||
if(prePoint.y() <= height && prePoint.y() >= (height - segmentHeight))
|
||
{
|
||
preIdx = m;
|
||
flag |= 1;
|
||
}
|
||
//在此块内
|
||
if(curPoint.y() <= height && curPoint.y() >= (height - segmentHeight))
|
||
{
|
||
curIdx = m;
|
||
flag |= 2;
|
||
}
|
||
if(flag == 3)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
//从上到下
|
||
if(preIdx < curIdx)
|
||
{
|
||
for(int p = preIdx; p < curIdx+1; p++)
|
||
{
|
||
currentY = segmentHeight + segmentHeight * p;
|
||
|
||
QPointF intersection;
|
||
if (lineIntersection(prePoint, curPoint, currentY, &intersection))
|
||
{
|
||
if (element.type == QPainterPath::MoveToElement)
|
||
{
|
||
pathSegments[p].moveTo(prePot);
|
||
pathSegments[p].moveTo(intersection);
|
||
if(curPoint.y() < currentY + segmentHeight)
|
||
{
|
||
if(p+1 < segmenNum)
|
||
{
|
||
pathSegments[p+1].moveTo(intersection);
|
||
pathSegments[p+1].moveTo(curPoint);
|
||
}
|
||
}
|
||
}
|
||
if (element.type == QPainterPath::LineToElement)
|
||
{
|
||
pathSegments[p].moveTo(prePot);
|
||
pathSegments[p].lineTo(intersection);
|
||
if(curPoint.y() < currentY + segmentHeight)
|
||
{
|
||
if(p+1 < segmenNum)
|
||
{
|
||
pathSegments[p+1].moveTo(intersection);
|
||
pathSegments[p+1].lineTo(curPoint);
|
||
}
|
||
}
|
||
}
|
||
prePot = intersection;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//从下到上
|
||
for(int p = preIdx+1; p > curIdx; p--)
|
||
{
|
||
currentY = segmentHeight * p - segmentHeight;
|
||
|
||
QPointF intersection;
|
||
if (lineIntersection(curPoint, prePoint, currentY, &intersection))
|
||
{
|
||
if (element.type == QPainterPath::MoveToElement)
|
||
{
|
||
pathSegments[p-1].moveTo(prePot);
|
||
pathSegments[p-1].moveTo(intersection);
|
||
if(curPoint.y() > currentY - segmentHeight)
|
||
{
|
||
if(p-1-1 >= 0)
|
||
{
|
||
pathSegments[p-1-1].moveTo(intersection);
|
||
pathSegments[p-1-1].moveTo(curPoint);
|
||
}
|
||
}
|
||
}
|
||
if (element.type == QPainterPath::LineToElement)
|
||
{
|
||
pathSegments[p-1].moveTo(prePot);
|
||
pathSegments[p-1].lineTo(intersection);
|
||
if(curPoint.y() > currentY - segmentHeight)
|
||
{
|
||
if(p-1-1 >= 0)
|
||
{
|
||
pathSegments[p-1-1].moveTo(intersection);
|
||
pathSegments[p-1-1].lineTo(curPoint);
|
||
}
|
||
}
|
||
}
|
||
prePot = intersection;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 求一条线段与y轴的坐标
|
||
//返回值:
|
||
//-1: 没有交点
|
||
// 0: 线段与Y轴重合
|
||
// 1: 有一个交点(x)
|
||
bool lineIntersection(QPointF p1, QPointF p2, double y, QPointF* intersection)
|
||
{
|
||
//-1是为了避免double乘除时产生的误差
|
||
if (((p1.y() < (y - 1)) && (p2.y() < (y - 1))) ||
|
||
((p1.y() > y) && (p2.y() > y)) ||
|
||
0)
|
||
{// 没有交点
|
||
return false;
|
||
}
|
||
else if ((p1.y() == y) && (p2.y() == y))
|
||
{// 线段与Y重合,返回false,因为这种情况已经被添加到块中
|
||
return false;
|
||
}
|
||
// else if (p1.y() == y)
|
||
// {// p1与Y重合
|
||
// intersection->setX(p1.x());
|
||
// intersection->setY(y);
|
||
// return true;
|
||
// }
|
||
// else if (p2.y() == y)
|
||
// {// p2与Y重合
|
||
// intersection->setX(p2.x());
|
||
// intersection->setY(y);
|
||
// return true;
|
||
// }
|
||
else
|
||
{// 有一个交点
|
||
double dx1 = p2.x() - p1.x();
|
||
double dy1 = p2.y() - p1.y();
|
||
double dy = y - p1.y();
|
||
|
||
double x = (dx1/dy1) * dy + p1.x();
|
||
intersection->setX(x);
|
||
intersection->setY(y);
|
||
return true;
|
||
}
|
||
return true;
|
||
}
|