qDebug() << "do perim: " << runPerimeterFlag;
if (runPerimeterFlag)
{
QPointF point;//映射坐标点
//添加标志位
QString retval = "IN;SP1;PU";
//起始坐标
point = perimeterRect.topLeft();
//遍历坐标点
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
retval += ",";
point = perimeterRect.topRight();
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
retval += ",";
point = perimeterRect.bottomRight();
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
retval += ",";
point = perimeterRect.bottomLeft();
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
//结束标志
retval += ",0,0;SP0;IN;";
qDebug() << "perimeter: " << retval;
_port->write(retval.toStdString().c_str());
_port->flush();
closeSerial();
emit finished();
return;
}
//输出处理
QString ExtPlot::print(QPolygonF hpgl_poly, QGraphicsItemGroup * itemGroup)
{
QString retval = "";
QPointF point;
// Create PU command
point = itemGroup->mapToScene(hpgl_poly.first());
retval += "PU";
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
retval += ";";
// Create PD command
retval += "PD";
for (int idx = 1; idx < hpgl_poly.count(); idx++)
{
point = itemGroup->mapToScene(hpgl_poly.at(idx));
if (point.x() < 0 || point.y() < 0)
{
retval = "OOB"; // Out of Bounds
return retval;
}
retval += QString::number(static_cast<int>(point.x()));
retval += ",";
retval += QString::number(static_cast<int>(point.y()));
if (idx < (hpgl_poly.length()-1))
{
retval += ",";
}
}
retval += ";";
return(retval);
}
//svg hpgl 转换 导入
QString ExtLoadFile::importSvg(QString filePath)
{
QProcess * svgToHpgl = new QProcess(this);
QString program = "python2.7";
QStringList arguments;
QString buffer;
QByteArray data;
arguments << "/usr/share/inkscape/extensions/hpgl_output.py"
<< "--precut=FALSE"
<< "--force=0"
<< "--speed=0"
<< filePath;
svgToHpgl->setProcessChannelMode(QProcess::MergedChannels);
svgToHpgl->start(program, arguments);
while (svgToHpgl->waitForReadyRead())
{
data = data + svgToHpgl->readAll();
}
buffer = QString::fromUtf8(data.data(), data.length());
return buffer;
}
QPersistentModelIndex ExtLoadFile::createHpglFile(file_uid _file)
{
int numRows = hpglModel->rowCount();
if (!hpglModel->insertRows(numRows, 1))
{
qDebug() << "Insert row failed.";
return(QModelIndex());
}
QPersistentModelIndex index = QPersistentModelIndex(hpglModel->index(numRows));
if (!hpglModel->setFileUid(index, _file))
{
qDebug() << "setFileUid failed.";
return(QModelIndex());
}
return(index);
}
// Modifies input string, warning!
bool ExtLoadFile::parseHPGL(const QPersistentModelIndex index, QString * hpgl_text)
{
QPointF tail(0, 0);
hpgl_text->remove('\n');
int numCmds = hpgl_text->count(';');
for (int i = 0; i < numCmds; i++)
{
QPolygonF newItem;
QString cmdText;
QString opcode;
int pen;
cmdText = hpgl_text->section(';', i, i);
qDebug() << "====\n= Processing command: ";
// Get opcode, first two characters
opcode = cmdText.mid(0, 2);
qDebug() << "= " << opcode;
// Parse opcode
if (opcode == "PU")
{
// Pen up - we assume a single line (two points)
int commaCount, newX, newY;
cmdText.remove(0,2);
commaCount = cmdText.count(',');
int i = commaCount - 1;
newX = cmdText.section(',', i, i).toInt();
i++;
newY = cmdText.section(',', i, i).toInt();
tail.setX(newX);
tail.setY(newY);
}
else if (opcode == "PD")
{
// Pen down
cmdText.remove(0,2);
int commaCount = cmdText.count(',');
newItem << tail; // Begin from last PU location
for (int i = 0; i < commaCount; i++)
{
//qDebug() << "processing coord: " << text.at(i) << endl;
int newX = cmdText.section(',', i, i).toInt();
i++;
int newY = cmdText.section(',', i, i).toInt();
// qDebug() << "= Found x: " << newX << " y: " << newY;
newItem << QPointF(newX, newY);
}
emit newPolygon(index, newItem);
}
else if (opcode == "IN")
{
// Begin plotting (automatically handled)
}
else if (opcode == "SP")
{
// Set pen
pen = cmdText.mid(2,1).toInt();
qDebug() << "[" << QString::number(pen) << "]";
}
else if (opcode == "FS" || opcode == "VS")
{
// Real hpgl commands that our USCutter doesn't accept
}
else
{
// Default case, something's wrong
return false;
}
int progressInt = ((double)i/(numCmds-1))*100;
emit progress(progressInt);
}
return true;
}
void ExtLoadFile::statusUpdate(QString _consoleStatus)
{
emit statusUpdate(_consoleStatus, Qt::black);
}