qt---plt格式处理

     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);
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值