#include "importhpgl.h"
#include <QFile>
#include <qmath.h>
#include <QDebug>
#include "vectorfont.h"

ImportHPGL::ImportHPGL(QObject *parent) :
    QObject(parent)
{
    m_iDPMM = 40;
    m_iPenNo = 1;
    m_ptCurrentPos = QPoint(0,0);
    m_lineType.bDefault = true;
    m_bPenUp = true;

    m_pMarker = NULL;
    m_dScale = (double)1.0;

    m_chTerminator = 3;
    m_nTerminatorMode = 1;

    m_listXY.clear();

    m_penPen.setColor(QColor(0,0,0));
    m_cutPen.setColor(QColor(0,255,0));
    m_halfCutPen.setColor(QColor(0,0,255));

    m_dTextHeight = 0.375 * 10 * m_iDPMM;//字的高度
    m_dTextWidth = 0.285 * 10 * m_iDPMM;//字的宽度
    m_nLableOrigin = 1;//字符串相对于原点位置
    m_dTextAngle = 0;//指定每行文本输出时相对于设备x轴的角度，其单位为1/10度

    m_dScaleX = 1;
    m_dScaleY = 1;
    m_ptOrigin = QPoint(0,0);
    m_nLength = 0;
    m_bPage = false;
    m_bFontItalic = false;
    m_iFontSize = 0;
    m_strFontName = "";

    m_pFileBuf = NULL;
}

ImportHPGL::~ImportHPGL()
{
}

int ImportHPGL::GetQRVesion(int nCount)
{
    int nVesion = 1;

    if (nCount < 17)
    {
        nVesion  = 1;
    }
    else if ((nCount >= 17) && (nCount < 32))
    {
        nVesion  = 2;
    }
    else if ((nCount >= 32) && (nCount < 53))
    {
        nVesion  = 3;
    }
    else if ((nCount >= 53) && (nCount < 78))
    {
        nVesion  = 4;
    }
    else if ((nCount >= 78) && (nCount < 106))
    {
        nVesion  = 5;
    }
    else if ((nCount >= 106) && (nCount < 134))
    {
        nVesion  = 6;
    }
    else if ((nCount >= 134) && (nCount < 154))
    {
        nVesion  = 7;
    }
    else if ((nCount >= 154) && (nCount < 192))
    {
        nVesion  = 8;
    }
    else if ((nCount >= 192) && (nCount < 230))
    {
        nVesion  = 9;
    }
    else if ((nCount >= 230) && (nCount < 271))
    {
        nVesion  = 10;
    }
    else
    {
        nVesion  = 40;
    }
    return nVesion;
}

void ImportHPGL::PointRotate(QPoint &ptPoint, QPoint ptPointO, double dSinBeta, double dCosBeta)
{
    int tx = ptPoint.x() - ptPointO.x();
    int ty = ptPoint.y() - ptPointO.y();

    int nx = tx*dCosBeta - ty*dSinBeta;
    int ny = tx*dSinBeta + ty*dCosBeta;

    ptPoint.setX(nx);
    ptPoint.setY(ny);
}

void ImportHPGL::IniPara()
{
    m_iPenNo = 1;
    m_ptCurrentPos = QPoint(0,0);
    m_lineType.bDefault = true;
    m_bPenUp = true;

    m_pMarker=NULL;
    m_dScale=(double)1.0;

    m_chTerminator = 3;
    m_nTerminatorMode = 1;

    m_listXY.clear();

    m_dTextHeight = 0.375 * 10 * m_iDPMM;//字的高度
    m_dTextWidth = 0.285 * 10 * m_iDPMM;//字的宽度
    m_nLableOrigin = 1;//字符串相对于原点位置
    m_dTextAngle = 0;//指定每行文本输出时相对于设备x轴的角度，其单位为1/10度

    m_dScaleX = 1;
    m_dScaleY = 1;
    m_ptOrigin = QPoint(0,0);
    m_nLength = 0;
    m_bPage = false;
    m_lineType.bDefault = true;
}

bool ImportHPGL::Read(QString strPathName,Marker *pMarker)
{
    QFile file(strPathName);
    char c;
    bool bOk;

    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() <<"file open failed";
        return false;
    }

    m_pMarker=pMarker;

    m_dScale=(double)m_pMarker->m_iDPMM / m_iDPMM;
    m_listXY.clear();
    m_nFileLength = file.size();
    m_pFileBuf = new char[m_nFileLength];
    file.read(m_pFileBuf,m_nFileLength);
    file.close();
    m_nCharCount = 0;
    bOk=GetChar(&c);
    while (bOk && m_nCharCount < m_nFileLength)
    {
        switch (c)
        {
        case 'S':  //S命令
        case 's':
            bOk=S_Code();
            break;
        case 'I':  //I命令
        case 'i':
            bOk=I_Code();
            break;
        case 'P':  //P命令
        case 'p':
            bOk=P_Code();
            break;
        case 'L':  //L命令
        case 'l':
            bOk=L_Code();
            break;
        case 'D':
        case 'd':
            bOk=D_Code();
            break;
        case 'C':
        case 'c':
            bOk=C_Code();
            break;
        case ';':
        case ' ':
        case 0x0A:
        case 0x0D:
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }

        if (bOk)
        {
            bOk=GetChar(&c);
            if (m_nCharCount == m_nFileLength)  bOk=true;
        }
    }

    AddPolylineToMarker();

    if (m_pFileBuf != NULL)
    {
        delete []m_pFileBuf;
        m_pFileBuf = NULL;
    }

    return bOk;
}

#if(0)
void ImportHPGL::creatPolylinePainterPath()
{
    if(m_pMarker == NULL){return;}

    QPainterPath painterPath;
    QRect rect = m_pMarker->GetRect();
    int minX = rect.left();
    int minY = rect.top();
    int maxY = rect.bottom();

    int nLineCount = m_pMarker->m_listPolyline.size();
    //    m_polyTextPath = QPainterPath();
    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int type = polyLine.m_nDrawingType;
        //        if(polyLine.m_nDrawingType == 3)//文字
        //        {
        //            CRPText text = polyLine.m_text;
        //        }

        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 = ((0 - (polyLine.m_listPoint.at(j).y() - minY))+(maxY-minY))/(double)M_IDPMM*MMPIXELY;
            QPointF point(x,y);

            if(j == 0)
            {
                painterPath.moveTo(point);
            }
            else
            {
                painterPath.lineTo(point);
            }
        }
    }

    m_polylinePainterPath = painterPath;
}
#endif

//读取一个非空格、非回车、非换行的字符
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  *pChar  读取的字符
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个字符
//  false 失败
bool ImportHPGL::GetChar(char *pChar)
{
    char c;
    uint nCount;
    bool bOk;
    bOk=true;

    nCount = ReadChar(&c);
    while ((nCount == 1) && ((c == ' ') || (c == 0x0A) || (c == 0x0D)))
    {
        nCount = ReadChar(&c);
    }

    if (bOk && (nCount == 0))
    {
        bOk=false;
    }

    if (nCount == 1)
    {
        *pChar=c;
        bOk=true;
    }

    return bOk;
}

uint ImportHPGL::ReadChar(char* lpBuf)
{
    uint nCount = 1;

    if (m_nCharCount < m_nFileLength)
    {
        *lpBuf = m_pFileBuf[m_nCharCount];
        m_nCharCount = m_nCharCount + 1;
        nCount = 1;
    }
    else
    {
        nCount = 0;
    }

    return nCount;
}

//判断下一个非空格、非回车、非换行的字符是不是','
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 下一个非空格、非回车、非换行的字符是',', 并且已将','从文件中读出
//  false 下一个非空格、非回车、非换行的字符不是',', 并且该字符没有从文件中读出
bool ImportHPGL::NextCharIsComma()
{
    char cCh1;
    bool bNextCharIsStart;

    bNextCharIsStart=false;

    if (GetChar(&cCh1))
    {
        if (cCh1 == ',')
        {
            bNextCharIsStart=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
        }
    }

    return bNextCharIsStart;
}

//取一个整数
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  iValue  取得的整数
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个整数iValue
//  false 失败
bool ImportHPGL::GetIntegerData(int &iValue)
{
    QString string1;
    char c;
    bool bOk,bNum;

    bNum=false;

    bOk=GetChar(&c);
    if (bOk && (c == '-'))
    {
        string1=string1 + c;
        bOk=GetChar(&c);
    }
    while (bOk)
    {
        if (('0' <= c) && (c <= '9'))
        {
            string1=string1 + c;
            bNum=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
            break;
        }

        bOk=GetChar(&c);
    }

    if (!bOk)
    {
        if (m_nCharCount == m_nFileLength)
            bOk=true;
    }
    if (!bNum)
        bOk=false;

    if (bOk)
        iValue=string1.toInt();

    return bOk;
}

//取一个双精度浮点数
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  dValue  取得的双精度浮点数
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功取得一个双精度浮点数dValue
//  false 失败
bool ImportHPGL::GetDoubleData(double &dValue)
{
    QString string1;
    char c;
    bool bOk,bNum;

    bNum=false;

    bOk=GetChar(&c);
    if (bOk && (c == '-'))
    {
        string1=string1 + c;
        bOk=GetChar(&c);
    }
    while (bOk)
    {
        if ((('0' <= c) && (c <= '9')) || c == '.')
        {
            string1=string1 + c;
            bNum=true;
        }
        else
        {
            m_nCharCount = m_nCharCount - 1;
            break;
        }

        bOk=GetChar(&c);
    }

    if (!bOk)
    {
        if (m_nCharCount == m_nFileLength)
            bOk=true;
    }

    if (!bNum)
        bOk=false;

    if (bOk)
        dValue=string1.toDouble();

    return bOk;
}

//取坐标值
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  ptPoint  坐标
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功
//  false 失败
bool ImportHPGL::GetCoordinate(QPoint &ptPoint)
{
    bool bOk;
    int iX,iY;
    double x,y;
    QString strInfo;

    ptPoint=m_ptCurrentPos;
    //bOk=GetIntegerData(x);
    bOk=GetDoubleData(x);//为了兼容Gemini文件，Gemini坐标不是整数，是小数
    if (bOk)
    {
        iX=(double)(x+m_nLength) * m_dScale * m_dScaleX;
        ptPoint.setX(iX);
    }

    if (bOk)
    {
        bOk=SearchChar(',' ,strInfo);
        if (bOk)
        {
            //bOk=GetIntegerData(y);
            bOk=GetDoubleData(y);//为了兼容Gemini文件，Gemini坐标不是整数，是小数
            if (bOk)
            {
                iY=(double)y * m_dScale * m_dScaleY;
                ptPoint.setY(iY);
            }

        }
    }

    return bOk;
}

//获得线的类型
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  lineType  线型
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功
//  false 失败
bool ImportHPGL::GetLineType(LineType &lineType)
{
    bool bOk;
    int nLineType, nPatternLength, nMode;
    QString strInfo;
    lineType  = m_lineType;
    bOk=GetIntegerData(nLineType);
    if (bOk)
    {
        lineType.bDefault = false;
        lineType.nLinetype = nLineType;
        bOk=SearchChar( ',' ,strInfo);
        if (bOk)
        {
            bOk=GetIntegerData(nPatternLength);
            if (bOk)
            {
                lineType.nPatternLength = nPatternLength;
            }

            bOk=SearchChar( ';' ,strInfo);
            if (bOk)
            {
                bOk=GetIntegerData(nMode);
                if (bOk)
                {
                    lineType.nMode = nMode;
                }
            }
            else
            {
                lineType.nMode = 0;
            }
        }
        else
        {
            lineType.nPatternLength = 4;
        }
    }
    else
    {
        lineType.bDefault = true;
    }

    return bOk;
}

//查找一个指定的字符
//输入参数:
//  pFile 切割数据文件
//  cFind 指定的字符
//输出参数:
//  bEndOfFile =true 已到文件尾
//  strInfo 从开始位置到指定字符之前的内容(不包括最后的指定字符)
//返回值:
//  true 成功取得一个字符
//  false 失败
bool ImportHPGL::SearchChar(char cFind,QString &strInfo)
{
    char c;
    uint nCount;
    bool bOk;

    bOk=true;
    strInfo.clear();


    nCount = ReadChar(&c);
    while ((nCount == 1) && (c != cFind))
    {
        strInfo = strInfo + c;
        nCount = ReadChar(&c);
    }


    if (bOk && (nCount == 0))
    {
        bOk=false;
    }

    if (nCount == 1)
    {
        bOk=true;
    }

    return bOk;
}

//将文件的读取位置移到下一个英文字母(26个英文字母)
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 成功将文件的读取位置移到下一个英文字母
//  false 失败
bool ImportHPGL::MoveToNextEnglishChar()
{
    char c;
    uint nCount;
    bool bOk, bFind;

    bOk=true;

    bFind = false;
    nCount = ReadChar(&c);
    while ((nCount == 1) && (!bFind))
    {
        if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z')))
        {
            m_nCharCount = m_nCharCount - 1;
            bFind = true;
        }
        else
        {
            nCount = ReadChar(&c);
        }
    }

    return bOk;
}

//将m_listXY加入到m_pMarker中,并将m_listXY清空
void ImportHPGL::AddPolylineToMarker()
{
    //长度<0.2mm为钻孔
    CRPPolyline RPPolyline1;
    QPoint pt1,pt2;
    CDrill drill;
    bool bIsdirll = false;

    if (m_listXY.size() >= 2)
    {
        if (m_listXY.size() == 2 && m_iPenNo == 3)
        {
            pt1 = m_listXY.at(0);
            pt2 = m_listXY.at(1);
            if (((pt1.x() - pt2.x())*(pt1.x() - pt2.x())+(pt1.y() - pt2.y())*(pt1.y() - pt2.y()))<=64)
            {
                bIsdirll = true;
            }
        }
        if (bIsdirll)
        {
            drill.m_pt = pt1;
            drill.m_nDrillType = 0;
            drill.m_nAngle = 0;
            RPPolyline1.m_nDrawingType = 5;
            RPPolyline1.m_drill = drill;
            RPPolyline1.m_nPenNo = drill.m_nDrillType;
        }
        else
        {
            RPPolyline1.m_nPenNo = m_iPenNo;
            RPPolyline1.m_bPgEnd = m_bPage;
            RPPolyline1.m_lineType = m_lineType;
            RPPolyline1.m_listPoint.clear();
            RPPolyline1.m_listPoint.append(m_listXY);
        }
        m_pMarker->m_listPolyline.append(RPPolyline1);
    }
    else
    {
        if (m_iPenNo == 20 && m_listXY.size() > 0)
        {
            drill.m_pt = m_listXY.at(0);
            drill.m_nDrillType = 0;
            drill.m_nAngle = 0;
            RPPolyline1.m_nDrawingType = 5;
            RPPolyline1.m_drill = drill;
            RPPolyline1.m_nPenNo = drill.m_nDrillType;
            m_pMarker->m_listPolyline.append(RPPolyline1);
        }
    }
    m_listXY.clear();
}

void ImportHPGL::AddPoint2listXY(QPoint ptPoint)
{
    if ((m_listXY.size() == 0) && (m_ptCurrentPos != ptPoint))
    {
        m_listXY.append(m_ptCurrentPos);
    }

    m_listXY.append(ptPoint);
}

void ImportHPGL::AddTextPointToLine(bool bUp,QPoint ptPoint)
{
    if (bUp)
    {
        AddPolylineToMarker();
        m_ptCurrentPos = ptPoint;
        m_bPenUp = true;
    }
    else
    {
        AddPoint2listXY(ptPoint);
        m_ptCurrentPos = ptPoint;
        m_bPenUp = false;
    }
}

QPoint ImportHPGL::GetTextOrigin(QPoint pt,QString strText)
{
    int nLen;
    double dTextAngle;
    double dLength;
    int iX;
    int iY;
    nLen = strText.size();
    dTextAngle = m_dTextAngle/180.0*PI ;
    switch(m_nLableOrigin)
    {
    case 1:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 2:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 4:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 5:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 6:
        iX=pt.x()-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 7:
        iX=pt.x()-(m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 8:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 9:
        iX=pt.x()-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 11:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)+m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI ;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()+dLength*qCos(dTextAngle);
        iY=pt.y()+dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 12:
        iX=pt.x()+(0.25*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()+(0.25*m_dTextWidth)*qSin(dTextAngle);
        iX=iX-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(0.5*m_dTextHeight)*qCos(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 13:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)-m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()+dLength*qCos(dTextAngle);
        iY=pt.y()-dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 14:
        iX=pt.x()-(1.25*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(1.25*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 15:
        iX=pt.x()-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 16:
        iX=pt.x()+(0.25*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()-(0.25*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(0.5*nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(0.5*nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 17:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)-m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=qSqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()-dLength*qCos(dTextAngle);
        iY=pt.y()+dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(m_dTextHeight)*qSin(dTextAngle);
        iY=iY+(m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 18:
        iX=pt.x()-(0.25*m_dTextWidth)*qCos(dTextAngle);
        iY=pt.y()-(0.25*m_dTextWidth)*qSin(dTextAngle);
        iX=iX-(0.5*m_dTextHeight)*qSin(dTextAngle);
        iY=pt.y()+(0.5*m_dTextHeight)*qCos(dTextAngle);
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    case 19:
        dTextAngle=qAtan2(0.25*m_dTextHeight,0.25*m_dTextWidth)+m_dTextAngle;
        dTextAngle=dTextAngle/180.0*PI;
        dLength=sqrt((0.25*m_dTextHeight)*(0.25*m_dTextHeight)+(0.25*m_dTextWidth)*(0.25*m_dTextWidth));
        iX=pt.x()-dLength*qCos(dTextAngle);
        iY=pt.y()-dLength*qSin(dTextAngle);
        dTextAngle=m_dTextAngle/180.0*PI;
        iX=iX-(nLen*m_dTextWidth)*qCos(dTextAngle);
        iY=iY-(nLen*m_dTextWidth)*qSin(dTextAngle);
        pt.setX(iX);
        pt.setY(iY);
        break;
    }
    return pt;
}

double ImportHPGL::GetTextAngle(double dX,double dY)
{
    double dAngle = 0;

    if ((dX == 0) && (dY > 0))
    {
        dAngle = 90;
    }
    else if ((dX == 0) && (dY < 0))
    {
        dAngle = -90;
    }
    else if ((dX > 0) && (dY == 0))
    {
        dAngle = 0;
    }
    else if ((dX < 0) && (dY == 0))
    {
        dAngle = 180;
    }
    else if ((dX!=0) && (dY!=0))
    {
        dAngle=qAtan2(dY,dX)*180/PI;
    }

    double tmp = (dAngle - (int)dAngle);
    if(qAbs((int)(tmp*10)) >= 5)
    {
        double angle = dAngle < 0 ? dAngle += -1 : dAngle+=1;
        dAngle = angle;
    }
    dAngle = (int)dAngle;

    return dAngle;
}

void ImportHPGL::SetScale()
{
    double dScaleX,dScaleY;
    switch(m_sc.nType)
    {
    case 0://异步
        m_dScaleX=(double)(m_ptP2.x()-m_ptP1.x())/(m_sc.dXMax-m_sc.dXMin);
        m_dScaleY=(double)(m_ptP2.y()-m_ptP1.y())/(m_sc.dYMax-m_sc.dYMin);
        m_ptOrigin.setX(m_ptP1.x()-m_sc.dXMin*m_dScaleX);
        m_ptOrigin.setY(m_ptP1.y()-m_sc.dYMin*m_dScaleY);
        break;
    case 1://同步
        dScaleX=(double)(m_ptP2.x()-m_ptP1.x())/(m_sc.dXMax-m_sc.dXMin);
        dScaleY=(double)(m_ptP2.y()-m_ptP1.y())/(m_sc.dYMax-m_sc.dYMin);
        if(dScaleX>dScaleY) //x>y
        {
            m_dScaleX=dScaleY;
            m_dScaleY=dScaleY;
            m_ptOrigin.setY(m_ptP1.y());
            m_ptOrigin.setX(((m_ptP2.x()-m_ptP1.x())-(m_sc.dXMax-m_sc.dXMin)*dScaleY)*m_sc.dLeft/100.0);
        }
        else //x<y
        {
            m_dScaleX=dScaleX;
            m_dScaleY=dScaleX;
            m_ptOrigin.setX(m_ptP1.x());
            m_ptOrigin.setY(((m_ptP2.y()-m_ptP1.y())-(m_sc.dYMax-m_sc.dYMin)*dScaleX)*m_sc.dBottom/100.0);
        }
        break;
    case 2://点因子
        m_dScaleX=m_sc.dXMax;
        m_dScaleY=m_sc.dYMax;
        m_ptOrigin.setX(m_ptP1.x()-m_sc.dXMin*m_dScaleX);
        m_ptOrigin.setY(m_ptP1.y()-m_sc.dYMin*m_dScaleY);
        break;
    }
}

bool ImportHPGL::IsSecretFile(QString strPatnName)
{
    QFile file(strPatnName);
    if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }
    int nLength = file.size();
    if (nLength >= 22)
    {
        char cFileBuf[23];
        file.read(&cFileBuf[0],22);
        if((cFileBuf[0] == 0x66) && (cFileBuf[1] == 0x53) &&(strncmp(&cFileBuf[3],"Richpeace plot file",19) == 0))
        {
            file.close();
            return true;
        }
        else
        {
            file.close();
            return false;
        }
    }
    return false;
}

int ImportHPGL::BitMapDtat(QString strFileName, QString strSecretFile)
{
    int i,j,k,m,n,nRemainChar;
    char cFileBuf[1025];
    char cSecretBuf[256];
    char errRe;
    QFile file(strFileName);
    int nLength;
    BOOL bError = FALSE;

    m=j=0;
    nRemainChar = 0;
    errRe=0;
    QFile hFile(strSecretFile);

    if (!hFile.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        return 0;
    }

    if(!file.open(QIODevice::ReadOnly))
    {
        return 0;
    }

    nLength = file.size();
    file.read(&cFileBuf[0],22);
    i = 22;

    for(;i<nLength;)
    {
        k = 0;
        if (m < j)
        {
            while(k<(j-m))
            {
                cFileBuf[k] = cFileBuf[m+k];
                k++;
            }
            j = 1024 - k;
        }
        else
            j = 1024;
        if (nRemainChar > 0)
        {
            n = 252 - nRemainChar + 2;
            if (j > nRemainChar)
            {
                m = 254;
                j -= nRemainChar;
                i += nRemainChar;
            }
            else
            {
                m = n + j;
                i += j;
                j = 0;
            }
            if ((n < m) && ((nRemainChar & 0x01) != 0))
            {
                nRemainChar -= (m - n);
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
            }
            else
                nRemainChar -= (m - n);
            for (;n<m;)
            {
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
            }
        }

        while (j > 0)
        {
            if (nLength == i)
            {
                break;
            }
            if ((nLength - i) < 256)
            {
                bError = TRUE;
                errRe = 1;
                break;
            }
            file.read(cSecretBuf,256);
            i += 256;
            cSecretBuf[0] ^= cSecretBuf[254];
            cSecretBuf[1] ^= cSecretBuf[255];
            cSecretBuf[0] ^= 0xac;
            cSecretBuf[1] ^= 0xe3;
            if (j < 252)
            {
                m = j + 2;
                nRemainChar = 252 - j;
                j = 0;
                i -= nRemainChar;
            }
            else
            {
                m = 254;
                j -= 252;
                nRemainChar = 0;
            }
            if ((m & 0x01) != 0)
            {
                m--;
                for (n=2;n<m;)
                {
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
                }
                cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
            }
            else
            {
                for (n=2;n<m;)
                {
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[0];
                    cFileBuf[k++] = cSecretBuf[n++] ^ cSecretBuf[1];
                }
            }
        }
        j = k;
        k = 0;
        m = j;
        hFile.write(cFileBuf, j);
        if (bError)
            break;
    }

    file.close();
    hFile.close();
    if(errRe == 0)
        return TRUE;
    else
        return FALSE;

    return 0;
}

bool ImportHPGL::ReadSecretFile(QString stePathName, Marker *pMarker)
{
    QFile file(stePathName);
    unsigned char c;
    unsigned char *cPin;
    int nParameter;
    int nSecretFileLength;
    unsigned char * pSecretFileBuf = NULL;
    QString strTemp;
    int i = 0;//加密指针pSecretFileBuf索引
    VectorFont vectorFont;
    QPoint ptPoint;
    int	 bitPost[8];
    int  bitDispStyle[2];
    int  bitGDIInfo[4];
    CBitmapInfo bitmapInfo;
    BYTE *pBmpData;
    CRPPolyline RPPolyline;
    CCode code;
    QRcode *pCode;
    double dSin,dCos;
    int nPixelsDot;
    CNotch notch;
    CDrill drill;
    PlotBitmap plotBitmap;
    int pointx,pointy;

    int notchPenNO = 5;
    int iFontNameLenth = 0;
    int notchOffsetX,notchOffsetY;
    notchOffsetX = notchOffsetY = 0;

    IniPara();

    if (!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    m_pMarker = pMarker;
    m_dScale = (double)m_pMarker->m_iDPMM / 40;
    m_listXY.clear();
    nSecretFileLength = file.size();
    pSecretFileBuf = new unsigned char[nSecretFileLength];
    if(pSecretFileBuf != NULL)
    {
        file.read((char*)pSecretFileBuf,nSecretFileLength);
    }
    file.close();

    while (i < nSecretFileLength)
    {
        c = pSecretFileBuf[i++];
        switch (c)
        {
        case DEFCMD_IN://0x8a://IN
            break;
        case DEFCMD_PG://0x97://PG
            m_nLength = m_pMarker->GetRect().right();
            break;
        case DEFCMD_SP://0x7c:
        {
            AddPolylineToMarker();
            unsigned char cTemp;
            cTemp = pSecretFileBuf[i++];
            m_iPenNo = cTemp;
        }
            break;
        case DEFCMD_LT://0xe6:
        {
            c = pSecretFileBuf[i++];
            c ^= 0xce;
            if (c != 0)
            {
                if (c == 1)
                    c = 3;
                else if (c == 2)
                    c = 1;
                else
                    c = 8;
            }
        }
            break;
        case DEFCMD_SI://0x5d: 字体宽×高
        {
            cPin = (unsigned char *)&m_dTextWidth;
            for(int nCount=7; nCount>=0; nCount--)
            {
                *(cPin+nCount) = pSecretFileBuf[i++];
            }
            m_dTextWidth = m_dTextWidth * 10 * M_IDPMM;

            cPin = (unsigned char *)&m_dTextHeight;
            for(int nCount=7; nCount>=0; nCount--)
            {
                *(cPin+nCount) = pSecretFileBuf[i++];
            }
            m_dTextHeight = m_dTextHeight * 10 * M_IDPMM;
        }
            break;
        case DEFCMD_LB://0x4d:
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&nParameter;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            m_ptCurrentPos.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            m_ptCurrentPos.setY(pointy);

            strTemp.clear();
            QByteArray tempArr;
            tempArr.clear();
            for(int nCount=0; nCount<nParameter; nCount++)
            {
                tempArr.append(pSecretFileBuf[i++]);
            }

            //防止中文乱码
            QTextCodec *gbkCodec = QTextCodec::codecForName("GBK");
            strTemp = gbkCodec->toUnicode(tempArr);

            m_ptCurrentPos = GetTextOrigin(m_ptCurrentPos,strTemp);
            vectorFont.m_dFontHeight = m_dTextHeight;
            vectorFont.m_dFontAngle = m_dTextAngle;
            vectorFont.TextOutString(m_ptCurrentPos.x(),m_ptCurrentPos.y(),(const char*)strTemp.data(),strTemp.length());
        }
            break;
        case DEFCMD_DI://0x5B:
        {
            cPin = (unsigned char *)&m_dTextAngle;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            m_bFontItalic = pSecretFileBuf[i++];

            cPin = (unsigned char *)&m_iFontSize;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
        }
            break;
        case DEFCMD_FN://0x4B:
        {
            m_strFontName = "";
            cPin = (unsigned char *)&iFontNameLenth;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            for (int a = 0; a < iFontNameLenth; a++)
            {
                char chTemp = pSecretFileBuf[i++];
                m_strFontName = m_strFontName + chTemp;
            }
        }
            break;
        case DEFCMD_DRILL://0xD2:冲孔
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            drill.m_pt.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            drill.m_pt.setY(pointy);

            cPin = (unsigned char *)&drill.m_nDrillType;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&drill.m_nAngle;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            RPPolyline.m_nDrawingType = 5;
            RPPolyline.m_drill = drill;
            RPPolyline.m_nPenNo = drill.m_nDrillType;
            m_pMarker->m_listPolyline.append(RPPolyline);
        }
            break;
        case DEFCMD_NOTCH://0xD3:剪口
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptStart.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptStart.setY(pointy);

            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptEnd.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            notch.m_ptEnd.setY(pointy);

            cPin = (unsigned char *)&notch.m_nWidth;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&notch.m_nNotchType;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            notch.CovertToOutputByOffset(notchOffsetX,notchOffsetY);
            RPPolyline.m_nDrawingType = 6;
            RPPolyline.m_notch = notch;
            RPPolyline.m_nPenNo = notchPenNO;
            m_pMarker->m_listPolyline.append(RPPolyline);
        }
            break;
        case DEFCMD_PU://0xb5:
        case DEFCMD_LPU://0x5e:
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setY(pointy);

            m_ptCurrentPos = ptPoint;
            m_bPenUp = true;
        }
            break;
        case DEFCMD_PD://0x3d:
        case DEFCMD_LPD://0x3b:
        {
            cPin = (unsigned char *)&pointx;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setX(pointx);

            cPin = (unsigned char *)&pointy;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];
            ptPoint.setY(pointy);

            AddPoint2listXY(ptPoint);
            m_ptCurrentPos = ptPoint;
        }
            break;
        case DEFCMD_NULL://0x00:空指令
            AddPolylineToMarker();
            break;
        case DEFCMD_BT://0x9a:
        {
            AddPolylineToMarker();

            //位图的左上角在整个图中的X位置
            //位图的左上角在整个图中的Y位置
            //位图的左下角在整个图中的X位置
            //位图的左下角在整个图中的Y位置
            //位图的右上角在整个图中的X位置
            //位图的右上角在整个图中的Y位置
            //位图的右下角在整个图中的X位置
            //位图的右下角在整个图中的Y位置
            for(int nCount=0; nCount<8; nCount++)
            {
                cPin = (unsigned char *)&nParameter;
                *(cPin+3) = pSecretFileBuf[i++];
                *(cPin+2) = pSecretFileBuf[i++];
                *(cPin+1) = pSecretFileBuf[i++];
                *cPin = pSecretFileBuf[i++];
                bitPost[nCount] = nParameter;
            }

            //为1时则为透明显示; 为0则为非透明显示
            //背景颜色(在透明显示时背景颜色的点将不被显示)
            for(int nCount=0; nCount<2; nCount++)
            {
                cPin = (unsigned char *)&nParameter;
                *(cPin+3) = pSecretFileBuf[i++];
                *(cPin+2) = pSecretFileBuf[i++];
                *(cPin+1) = pSecretFileBuf[i++];
                *cPin = pSecretFileBuf[i++];
                bitDispStyle[nCount] = nParameter;
            }

            //GDI+中BitmapData的Width,单位：像素
            //GDI+中BitmapData的Height,单位：像素
            //GDI+中BitmapData的Stride(每行的字节数)
            //GDI+中BitmapData的PixelFormat
            for(int nCount=0; nCount<4; nCount++)
            {
                cPin = (unsigned char *)&nParameter;
                *(cPin+3) = pSecretFileBuf[i++];
                *(cPin+2) = pSecretFileBuf[i++];
                *(cPin+1) = pSecretFileBuf[i++];
                *cPin = pSecretFileBuf[i++];
                bitGDIInfo[nCount] = nParameter;
            }

            //int压缩后位图数据大小(字节数) V1.01开始有此数据
            cPin = (unsigned char *)&nParameter;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            bitmapInfo.m_ptPostLU.setX(bitPost[0]);
            bitmapInfo.m_ptPostLU.setY(bitPost[1]);
            bitmapInfo.m_ptPostLD.setX(bitPost[2]);
            bitmapInfo.m_ptPostLD.setY(bitPost[3]);
            bitmapInfo.m_ptPostRU.setX(bitPost[4]);
            bitmapInfo.m_ptPostRU.setY(bitPost[5]);
            bitmapInfo.m_ptPostRD.setX(bitPost[6]);
            bitmapInfo.m_ptPostRD.setY(bitPost[7]);

            bitmapInfo.m_ptAbPostLU.setX(bitPost[0] + m_ptOrigin.x() + m_nLength);
            bitmapInfo.m_ptAbPostLU.setY(bitPost[1] + m_ptOrigin.y());
            bitmapInfo.m_ptAbPostLD.setX(bitPost[2] + m_ptOrigin.x() + m_nLength);
            bitmapInfo.m_ptAbPostLD.setY(bitPost[3] + m_ptOrigin.y());
            bitmapInfo.m_ptAbPostRU.setX(bitPost[4] + m_ptOrigin.x() + m_nLength);
            bitmapInfo.m_ptAbPostRU.setY(bitPost[5] + m_ptOrigin.y());
            bitmapInfo.m_ptAbPostRD.setX(bitPost[6] + m_ptOrigin.x() + m_nLength);
            bitmapInfo.m_ptAbPostRD.setY(bitPost[7] + m_ptOrigin.y());

            bitmapInfo.m_iTransparent = bitDispStyle[0];
            bitmapInfo.m_iBKColor = bitDispStyle[1];

            bitmapInfo.m_iWidth = bitGDIInfo[0];
            bitmapInfo.m_iHeight = bitGDIInfo[1];
            bitmapInfo.m_iStride = bitGDIInfo[2];
            bitmapInfo.m_iPixelFormat = bitGDIInfo[3];

            bitmapInfo.m_iBytes = nParameter;

            QByteArray unBmpArr;
            unBmpArr.clear();
            pBmpData = new BYTE[bitmapInfo.m_iBytes];
            for(int nCount=0; nCount<bitmapInfo.m_iBytes; nCount++)
            {
                pBmpData[nCount] = pSecretFileBuf[i++];
                unBmpArr.append(pBmpData[nCount]);
            }
            QByteArray pUnCompressData;

            //解压缩
            // 文件路径
            QDir apppath(qApp->applicationDirPath());
            QString zipPath = apppath.path();
            QString zipFile = zipPath + apppath.separator() + "bfile.zip";
            QFile file(zipFile);
            if (!file.open(QIODevice::WriteOnly))
            {
                return false;
            }
            file.write(unBmpArr);
            file.close();

            QZipReader reader(zipFile);
            reader.extractAll(zipPath);

            QZipReader::FileInfo fileInfo = reader.entryInfoAt(0);//文件夹名称
            pUnCompressData =  reader.fileData(fileInfo.filePath);
            reader.close();

            //            bitmapInfo.m_pBitmap = plotBitmap.Create1BPPBitmap(bitmapInfo.m_iTransparent,bitmapInfo.m_iBKColor,bitmapInfo.m_iWidth,bitmapInfo.m_iHeight,
            //                                                               bitmapInfo.m_iStride,bitmapInfo.m_iPixelFormat, pUnCompressData.data());

            bitmapInfo.m_pBitmap = plotBitmap.Create1BPPBitmap(bitmapInfo.m_iWidth,bitmapInfo.m_iHeight, (unsigned char*)pUnCompressData.data());

            delete pBmpData;
            QFile::remove(zipFile);
            QString reStr = zipPath + apppath.separator() + fileInfo.filePath;
            QFile::remove(reStr);

            RPPolyline.m_nDrawingType = 1;
            RPPolyline.m_bitmapInfo = bitmapInfo;
            m_pMarker->m_listPolyline.append(RPPolyline);
        }
            break;
        case DEFCMD_CODE://0xE7:
        {
            AddPolylineToMarker();
            cPin = (unsigned char *)&code.m_nLX;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nLY;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nSizeX;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nSizeY;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nAngle;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nType;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            cPin = (unsigned char *)&code.m_nCount;
            *(cPin+3) = pSecretFileBuf[i++];
            *(cPin+2) = pSecretFileBuf[i++];
            *(cPin+1) = pSecretFileBuf[i++];
            *cPin = pSecretFileBuf[i++];

            code.m_strCode.clear();

            QByteArray codeArr;
            codeArr.clear();
            for(int nCount=0; nCount<code.m_nCount; nCount++)
            {
                codeArr.append(pSecretFileBuf[i++]);
            }

            //防止中文乱码
            QTextCodec *gbkCodec = QTextCodec::codecForName("GBK");
            code.m_strCode = gbkCodec->toUnicode(codeArr);

            if (code.m_nType == 0)
            {
            }
            else if (code.m_nType == 1)
            {
                int nVesion = GetQRVesion(code.m_nCount);
                pCode = QRcode_encodeString(code.m_strCode.toStdString().c_str(),nVesion,QR_ECLEVEL_L,QR_MODE_8,1);
                if (pCode != NULL)
                {
                    int scale = 1;

                    int qrCode_Width = pCode->width > 0 ? pCode->width : 1;
                    int width = scale * qrCode_Width;
                    int height = scale * qrCode_Width;

                    QImage image(width, height, QImage::Format_ARGB32);

                    QPainter painter(&image);
                    QColor background(Qt::white);
                    painter.setBrush(background);
                    painter.setPen(Qt::NoPen);
                    painter.drawRect(0, 0, width, height);
                    QColor foreground(Qt::black);
                    painter.setBrush(foreground);
                    for(int y = 0; y < qrCode_Width; ++y)
                    {
                        for(int x = 0; x < qrCode_Width; ++x)
                        {
                            unsigned char character = pCode->data[y * qrCode_Width + x];
                            if(character & 0x01)
                            {
                                QRect rect(x * scale, y * scale, scale, scale);
                                painter.drawRects(&rect, 1);
                            }
                        }
                    }

                    QBitmap qrPixmap = QBitmap::fromImage(image);
                    //qrPixmap.save("D:\\1.bmp");
                    bitmapInfo.m_pBitmap = qrPixmap;

                    if (code.m_nSizeY <= code.m_nSizeX)
                    {
                        nPixelsDot = (int)((double)code.m_nSizeY / (double)pCode->width + 0.5);
                    }
                    else
                    {
                        nPixelsDot = (int)((double)code.m_nSizeX / (double)pCode->width + 0.5);
                    }

                    int nStride = WIDTHBYTES(pCode->width * nPixelsDot);

                    bitmapInfo.m_ptPostLU.setX(code.m_nLX);
                    bitmapInfo.m_ptPostLU.setY(code.m_nLY);
                    bitmapInfo.m_ptPostLD.setX(code.m_nLX);
                    bitmapInfo.m_ptPostLD.setY(code.m_nLY - code.m_nSizeY);
                    bitmapInfo.m_ptPostRU.setX(code.m_nLX + code.m_nSizeX);
                    bitmapInfo.m_ptPostRU.setY(code.m_nLY);
                    bitmapInfo.m_ptPostRD.setX(code.m_nLX + code.m_nSizeX);
                    bitmapInfo.m_ptPostRD.setY(code.m_nLY - code.m_nSizeY);

                    dSin = sin(CONST_PI/180.0*code.m_nAngle/100.0);
                    dCos = cos(CONST_PI/180.0*code.m_nAngle/100.0);

                    PointRotate(bitmapInfo.m_ptPostLU,bitmapInfo.m_ptPostLU,dSin,dCos);
                    PointRotate(bitmapInfo.m_ptPostLD,bitmapInfo.m_ptPostLU,dSin,dCos);
                    PointRotate(bitmapInfo.m_ptPostRU,bitmapInfo.m_ptPostLU,dSin,dCos);
                    PointRotate(bitmapInfo.m_ptPostRD,bitmapInfo.m_ptPostLU,dSin,dCos);

                    bitmapInfo.m_ptAbPostLU.setX(bitmapInfo.m_ptPostLU.x() + m_ptOrigin.x() + m_nLength);
                    bitmapInfo.m_ptAbPostLU.setY(bitmapInfo.m_ptPostLU.y() + m_ptOrigin.y());
                    bitmapInfo.m_ptAbPostLD.setX(bitmapInfo.m_ptPostLD.x() + m_ptOrigin.x() + m_nLength);
                    bitmapInfo.m_ptAbPostLD.setY(bitmapInfo.m_ptPostLD.y() + m_ptOrigin.y());
                    bitmapInfo.m_ptAbPostRU.setX(bitmapInfo.m_ptPostRU.x() + m_ptOrigin.x() + m_nLength);
                    bitmapInfo.m_ptAbPostRU.setY(bitmapInfo.m_ptPostRU.y() + m_ptOrigin.y());
                    bitmapInfo.m_ptAbPostRD.setX(bitmapInfo.m_ptPostRD.x() + m_ptOrigin.x() + m_nLength);
                    bitmapInfo.m_ptAbPostRD.setY(bitmapInfo.m_ptPostRD.y() + m_ptOrigin.y());

                    bitmapInfo.m_iTransparent = 0;
                    bitmapInfo.m_iBKColor = qRgb(255,255,255);

                    bitmapInfo.m_iWidth = pCode->width;
                    bitmapInfo.m_iHeight = pCode->width;
                    bitmapInfo.m_iStride = nStride;
                    bitmapInfo.m_iPixelFormat = PixelFormat1bppIndexed;

                    bitmapInfo.m_iBytes = pCode->width*nStride;

                    RPPolyline.m_nDrawingType = 1;
                    RPPolyline.m_bitmapInfo = bitmapInfo;
                    m_pMarker->m_listPolyline.append(RPPolyline);

                    QRcode_free(pCode);
                }

#if(0)
                if (code.m_nSizeY <= code.m_nSizeX)
                {
                    nPixelsDot = (int)((double)code.m_nSizeY / (double)pCode->width + 0.5);
                }
                else
                {
                    nPixelsDot = (int)((double)code.m_nSizeX / (double)pCode->width + 0.5);
                }

                int nStride = WIDTHBYTES(pCode->width * nPixelsDot);

                BYTE* pData = new BYTE[pCode->width*nPixelsDot*nStride];

                for (int y = 0; y < pCode->width; y++)
                {
                    for (int x = 0; x < pCode->width; x++)
                    {
                        int nByte = x / 8;
                        int nBit  = x % 8;

                        if ((pCode->data[y*pCode->width + x] & 0x01))
                        {
                            //SetUncharBit(pData[y*nStride+nByte],7-nBit,0);
                            for (nIndexRow = 0; nIndexRow < nPixelsDot; nIndexRow++)
                            {
                                for (nIndexCol = 0; nIndexCol < nPixelsDot; nIndexCol++)
                                {
                                    nByte = (x * nPixelsDot + nIndexCol) / 8;
                                    nBit = (x * nPixelsDot + nIndexCol) % 8;
                                    plotBitmap.SetUncharBit(pData[(y*nPixelsDot +nIndexRow)*nStride + nByte], 7 - nBit, 0);
                                }
                            }
                        }
                        else
                        {
                            //SetUncharBit(pData[y*nStride+nByte],7-nBit,1);
                            for (nIndexRow = 0; nIndexRow < nPixelsDot; nIndexRow++)
                            {
                                for (nIndexCol = 0; nIndexCol < nPixelsDot; nIndexCol++)
                                {
                                    nByte = (x * nPixelsDot + nIndexCol) / 8;
                                    nBit = (x * nPixelsDot + nIndexCol) % 8;
                                    plotBitmap.SetUncharBit(pData[(y * nPixelsDot + nIndexRow)*nStride + nByte], 7 - nBit, 1);
                                }
                            }
                        }
                    }
                }

                bitmapInfo.m_ptPostLU.setX(code.m_nLX);
                bitmapInfo.m_ptPostLU.setY(code.m_nLY);
                bitmapInfo.m_ptPostLD.setX(code.m_nLX);
                bitmapInfo.m_ptPostLD.setY(code.m_nLY - code.m_nSizeY);
                bitmapInfo.m_ptPostRU.setX(code.m_nLX + code.m_nSizeX);
                bitmapInfo.m_ptPostRU.setY(code.m_nLY);
                bitmapInfo.m_ptPostRD.setX(code.m_nLX + code.m_nSizeX);
                bitmapInfo.m_ptPostRD.setY(code.m_nLY - code.m_nSizeY);

                dSin = sin(CONST_PI/180.0*code.m_nAngle/100.0);
                dCos = cos(CONST_PI/180.0*code.m_nAngle/100.0);

                PointRotate(bitmapInfo.m_ptPostLU,bitmapInfo.m_ptPostLU,dSin,dCos);
                PointRotate(bitmapInfo.m_ptPostLD,bitmapInfo.m_ptPostLU,dSin,dCos);
                PointRotate(bitmapInfo.m_ptPostRU,bitmapInfo.m_ptPostLU,dSin,dCos);
                PointRotate(bitmapInfo.m_ptPostRD,bitmapInfo.m_ptPostLU,dSin,dCos);

                bitmapInfo.m_ptAbPostLU.setX(bitmapInfo.m_ptPostLU.x() + m_ptOrigin.x() + m_nLength);
                bitmapInfo.m_ptAbPostLU.setY(bitmapInfo.m_ptPostLU.y() + m_ptOrigin.y());
                bitmapInfo.m_ptAbPostLD.setX(bitmapInfo.m_ptPostLD.x() + m_ptOrigin.x() + m_nLength);
                bitmapInfo.m_ptAbPostLD.setY(bitmapInfo.m_ptPostLD.y() + m_ptOrigin.y());
                bitmapInfo.m_ptAbPostRU.setX(bitmapInfo.m_ptPostRU.x() + m_ptOrigin.x() + m_nLength);
                bitmapInfo.m_ptAbPostRU.setY(bitmapInfo.m_ptPostRU.y() + m_ptOrigin.y());
                bitmapInfo.m_ptAbPostRD.setX(bitmapInfo.m_ptPostRD.x() + m_ptOrigin.x() + m_nLength);
                bitmapInfo.m_ptAbPostRD.setY(bitmapInfo.m_ptPostRD.y() + m_ptOrigin.y());

                bitmapInfo.m_iTransparent = 0;
                bitmapInfo.m_iBKColor = qRgb(255,255,255);

                bitmapInfo.m_iWidth = pCode->width;
                bitmapInfo.m_iHeight = pCode->width;
                bitmapInfo.m_iStride = nStride;
                bitmapInfo.m_iPixelFormat = PixelFormat1bppIndexed;

                bitmapInfo.m_iBytes = pCode->width*nStride;

                //                    bitmapInfo.m_pBitmap = plotBitmap.Create1BPPBitmap(bitmapInfo.m_iTransparent,bitmapInfo.m_iBKColor,bitmapInfo.m_iWidth*nPixelsDot,bitmapInfo.m_iHeight*nPixelsDot,
                //                                                            bitmapInfo.m_iStride,bitmapInfo.m_iPixelFormat, (char*)pData);

                bitmapInfo.m_pBitmap = plotBitmap.Create1BPPBitmap(bitmapInfo.m_iWidth*nPixelsDot,bitmapInfo.m_iHeight*nPixelsDot,pData);

                RPPolyline.m_nDrawingType = 1;
                RPPolyline.m_bitmapInfo = bitmapInfo;
                m_pMarker->m_listPolyline.append(RPPolyline);//*/

                /*code.m_pBitmap = Create1BPPBitmap(0,RGB(255,255,255),pCode->width,pCode->width,nStride,PixelFormat1bppIndexed,pData);
                    WritreBMP("D:\\345.bmp", pCode->width, pCode->width, nStride, PixelFormat1bppIndexed, pData);
                    RPPolyline.m_nDrawingType = 4;
                    RPPolyline.m_code = code;
                    m_pMarker->m_listPolyline.AddTail(RPPolyline);*/

                QRcode_free(pCode);
                delete []pData;
            }
#endif
            }
        }
        break;
        default:
            qDebug()<<tr("Encrypted file parsing error!");//加密文件解析错误！
            break;
    }
}
    return true;
}

bool ImportHPGL::C_Code()
{
    char cCh1;
    bool bOk;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'I':
        case 'i':
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

//分析S指令
//输入参数:
//  pFile 切割数据文件
//输出参数:
//  bEndOfFile =true 已到文件尾
//返回值:
//  true 正确的G指令
//  false 不正确的G指令
bool ImportHPGL::S_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)  {
        switch (cCh1)  {
        case 'P':
        case 'p':
            AddPolylineToMarker();
            GetIntegerData(m_iPenNo);
            break;
        case 'I':
        case 'i':
            bOk=GetDoubleData(m_dTextWidth);
            m_dTextWidth = m_dTextWidth * 10 * m_iDPMM;
            bOk=SearchChar( ',' ,strInfo);
            bOk=GetDoubleData(m_dTextHeight);
            m_dTextHeight = m_dTextHeight * 10 * m_iDPMM;
            break;
        case 'C':
        case 'c':
            SC_Code();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::SC_Code()
{
    bool bOk;
    QString strInfo;
    double dXMin,dXMax,dYMin,dYMax;
    int nType;

    bOk = GetDoubleData(dXMin);
    bOk=SearchChar(',' ,strInfo);
    bOk = GetDoubleData(dXMax);
    bOk=SearchChar(',' ,strInfo);
    bOk = GetDoubleData(dYMin);
    bOk=SearchChar(',',strInfo);
    bOk = GetDoubleData(dYMax);
    if (NextCharIsComma())
    {
        bOk = GetIntegerData(nType);
        if (NextCharIsComma())
        {
            bOk = GetDoubleData(m_sc.dLeft);
            bOk=SearchChar( ',',strInfo);
            bOk = GetDoubleData(m_sc.dBottom);
        }
    }
    else
    {
        nType = 0;
    }
    if (bOk)
    {
        m_sc.dXMin = dXMin;
        m_sc.dXMax = dXMax;
        m_sc.dYMin = dYMin;
        m_sc.dYMax = dYMax;
        m_sc.nType = nType;
        SetScale();
    }
    bOk = MoveToNextEnglishChar();
    return bOk;
}

bool ImportHPGL::I_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'N':
        case 'n':
            break;
        case 'P':
        case 'p':
            int nP1X,nP1Y,nP2X,nP2Y;
            bOk = GetIntegerData(nP1X);
            if (bOk)
            {
                bOk=SearchChar( ',' , strInfo);
                bOk = GetIntegerData(nP1Y);
                bOk=SearchChar(',' ,strInfo);
                bOk = GetIntegerData(nP2X);
                bOk=SearchChar(',' ,strInfo);
                bOk = GetIntegerData(nP2Y);
            }
            if (bOk)
            {
                m_ptP1.setX(nP1X);
                m_ptP1.setY(nP1Y);
                m_ptP2.setX(nP2X);
                m_ptP2.setY(nP2Y);
            }
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::PU_Code()
{
    bool bOk;
    QPoint ptPoint;

    AddPolylineToMarker();
    bOk = GetCoordinate(ptPoint);
    if (bOk)
    {
        m_ptCurrentPos = ptPoint;
    }

    m_bPenUp = true;

    bOk=true;
    return bOk;
}

bool ImportHPGL::PD_Code()
{
    bool bOk;
    QPoint ptPoint;

    bOk = GetCoordinate(ptPoint);
    if (bOk)
    {
        AddPoint2listXY(ptPoint);
        m_ptCurrentPos = ptPoint;
    }
    while (bOk)
    {
        if (NextCharIsComma())
        {
            bOk = GetCoordinate(ptPoint);
            if (bOk)
            {
                AddPoint2listXY(ptPoint);
                m_ptCurrentPos = ptPoint;
            }
        }
        else
        {
            break;
        }
    }
    m_bPenUp = false;

    bOk=true;
    return bOk;
}

bool ImportHPGL::P_Code()
{
    bool bOk;
    char c;

    if (GetChar(&c))
    {
        switch (c)
        {
        case 'U':
        case 'u':
            PU_Code();
            break;
        case 'D':
        case 'd':
            PD_Code();
            break;
        case 'A':
        case 'a':
            if (m_bPenUp)
            {
                PU_Code();
            }
            else
            {
                PD_Code();
            }
            break;
        case 'G':
        case 'g':
            m_bPage = true;
            AddPolylineToMarker();
            m_nLength = m_pMarker->GetRect().right();
            //qDebug()<< "length:" << m_nLength;
            //qDebug()<< "line count:" << m_pMarker->m_listPolyline.size();
            m_bPage = false;
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    bOk=true;
    return bOk;
}

bool ImportHPGL::L_Code()
{
    char cCh1;
    bool bOk;
    QString strText;
    VectorFont vectorFont;
    CRPPolyline crppolylineTemp;

    bOk=GetChar(&cCh1);
    if (bOk)
    {
        switch (cCh1)
        {
        case 'T':
        case 't':
            AddPolylineToMarker();
            GetLineType(m_lineType);
            bOk = MoveToNextEnglishChar();
            break;
        case 'B':
        case 'b':
            AddPolylineToMarker();
            bOk = SearchChar(m_chTerminator,strText);
            m_ptCurrentPos = GetTextOrigin(m_ptCurrentPos,strText);

            crppolylineTemp.m_nDrawingType = 3;
            crppolylineTemp.m_text.m_strText = strText;
            crppolylineTemp.m_text.m_ptTextPos = m_ptCurrentPos;
            crppolylineTemp.m_text.m_dTextAngle = m_dTextAngle;
            crppolylineTemp.m_text.m_nHeight = m_dTextHeight;
            crppolylineTemp.m_text.m_nWidth  = m_dTextWidth;
            crppolylineTemp.m_text.m_ptPostLU = m_ptCurrentPos;
            crppolylineTemp.m_text.m_ptPostLD = QPoint(m_ptCurrentPos.x(),m_ptCurrentPos.y()+m_dTextHeight);
            crppolylineTemp.m_text.m_ptPostRU = QPoint(m_ptCurrentPos.x()+m_dTextWidth*strText.length(),m_ptCurrentPos.y());
            crppolylineTemp.m_text.m_ptPostRD = QPoint(m_ptCurrentPos.x()+m_dTextWidth*strText.length(),m_ptCurrentPos.y()+m_dTextHeight);

            //用线段方法绘制
#ifdef Q_OS_WIN
            vectorFont.m_dFontHeight = m_dTextHeight;
            vectorFont.m_dFontAngle = m_dTextAngle;
            vectorFont.TextOutString(m_ptCurrentPos.x(),m_ptCurrentPos.y(),strText.toStdString().c_str(),strText.size());
#endif

            //qDebug()<<crppolylineTemp.m_text.m_strText;
            //m_pMarker->m_listPolyline.append(crppolylineTemp);//用文本方法绘制

            break;
        case 'O':
        case 'o':
            bOk=GetIntegerData(m_nLableOrigin);
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}


bool ImportHPGL::D_Code()
{
    char cCh1;
    bool bOk;
    QString strInfo;

    bOk=GetChar(&cCh1);
    if (bOk)  {
        switch (cCh1)  {
        case 'F':
        case 'f':
            break;
        case 'I':
        case 'i':
            double dX,dY;

            bOk = GetDoubleData(dX);
            if (bOk)
            {
                bOk = SearchChar(',' ,strInfo);
                bOk = GetDoubleData(dY);
            }
            else
            {
                dX = 1;
                dY = 0;
            }
            m_dTextAngle = GetTextAngle(dX,dY);
            bOk = MoveToNextEnglishChar();
            break;
        case 'T':
        case 't':
            char chTerminator;
            int nTerminatorMode;
            bOk = GetChar(&chTerminator);
            if (bOk)
            {
                m_chTerminator = chTerminator;
                bOk = GetIntegerData(nTerminatorMode);
                if (bOk)
                {
                    m_nTerminatorMode = nTerminatorMode;
                }
            }
            bOk = MoveToNextEnglishChar();
            break;
        default:
            bOk = MoveToNextEnglishChar();
            break;
        }
    }

    return bOk;
}

bool ImportHPGL::Write(QString strPathName,Marker *pMarker)
{
    QFile *writeFile = new QFile(strPathName);
    writeFile->open(QIODevice::ReadWrite | QFile::Truncate);
    // 如果文件没有被占用可以打开
    // 创建stream
    QTextStream txtOutput(writeFile);

    QString strHPGL = "IN;DF;SP0;PU0,0;";
    //int nLength = 0;
    int i,j;
    int iLineCount,iPointCount;

    QString strTemp;
    int nOldPenNo = 0;

    bool bSetLT = false;

    iLineCount = pMarker->m_listPolyline.size();
    for (i = 0; i < iLineCount; i++)
    {
        CRPPolyline polyLine = pMarker->m_listPolyline.at(i);

        if (polyLine.m_lineType.bDefault == false)
        {
            bSetLT = true;
            strTemp = QString("LT%1,%2,%3;").arg(polyLine.m_lineType.nLinetype).arg(polyLine.m_lineType.nPatternLength).arg(1);
        }
        else
        {
            if (bSetLT)
                strTemp = "LT;";
        }
        strHPGL = strHPGL + strTemp;

        if (polyLine.m_nDrawingType == 0)
        {
            iPointCount = polyLine.m_listPoint.size();
            //选择画笔 1为笔绘 3为半透切割 其它为切割
            if (nOldPenNo != polyLine.m_nPenNo)
            {
                if (polyLine.m_nPenNo == 1)
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                else if (polyLine.m_nPenNo == 3)
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                else
                {
                    strTemp = QString("SP%1;").arg(polyLine.m_nPenNo);
                    nOldPenNo = polyLine.m_nPenNo;
                }
                strHPGL = strHPGL + strTemp;
            }


            for (j = 0; j < iPointCount; j++)
            {

                QPoint pt = polyLine.m_listPoint.at(j);

                if (j == 0)
                {
                    strTemp = QString("PU%1,%2;").arg(pt.x()).arg(pt.y());

                }
                else if(j == 1)
                {
                    if (j + 1 == iPointCount)
                    {
                        strTemp = QString("PD%1,%2;").arg(pt.x()).arg(pt.y());

                    }
                    else
                    {
                        strTemp = QString("PD%1,%2,").arg(pt.x()).arg(pt.y());

                    }
                }
                else
                {
                    if (j + 1 == iPointCount)
                    {

                        strTemp = QString("%1,%2;").arg(pt.x()).arg(pt.y());
                    }
                    else
                    {
                        strTemp = QString("%1,%2,").arg(pt.x()).arg(pt.y());

                    }
                }
                strHPGL = strHPGL + strTemp;
            }
        }
        else if (polyLine.m_nDrawingType == 5)
        {
            strTemp = "SP20;";
            strHPGL = strHPGL + strTemp;
            strTemp = QString("PU%1,%2;").arg(polyLine.m_drill.m_pt.x()).arg(polyLine.m_drill.m_pt.y());
            strHPGL = strHPGL + strTemp;
            strTemp = QString("PD%1,%2;").arg(polyLine.m_drill.m_pt.x()).arg(polyLine.m_drill.m_pt.y());
            strHPGL = strHPGL + strTemp;
            nOldPenNo = 20;
        }

        // 在stream追加数据，并换行
        txtOutput << strHPGL << endl;
        strHPGL = "";

    }
    strHPGL = strHPGL + "SP0;";
    //nLength = strHPGL.size();

    // 在stream追加数据，并换行
    txtOutput << strHPGL << endl;
    strHPGL = "";
    writeFile->close();
    return true;
}

#if(0)
QPainterPath ImportHPGL::GetPolylinePainterPath()
{
    return m_polylinePainterPath;
}
#endif

#if(0)
int ImportHPGL::CreatePreviewImage(QString saveName,QImage *pImg, int saveflag)
{
    QImage * pImage = NULL;
    int width, height;
    width = height = 0;

    if (pImg == NULL)
    {
        width = (m_maxX - m_minX) / M_IDPMM + 2*PREVIEW_SIDE;
        height = (m_maxY - m_minY) / M_IDPMM+ 2*PREVIEW_SIDE;
        pImage = new QImage(width, height, QImage::Format_ARGB32); //
    }
    else
    {
        pImage = pImg;
    }

    width  = pImage->width();
    height = pImage->height();
    if (width < PREVIEW_SIDE*2 || height < PREVIEW_SIDE*2)
    {
        if (pImage != NULL)
        {
            delete pImage;
        }
        return -1;
    }

    QPainter painter(pImage);
    QColor backcolor(255, 255, 255, 255);
    QColor pencolor(0, 0, 0, 255);

    // 背景
    QPen pen;
    pen.setWidth(1);
    pen.setColor(backcolor);
    painter.setPen(pen);
    painter.setBrush(backcolor);
    painter.drawRect(0, 0, width, height);

    // 图形显示区域
    int dpminx = PREVIEW_SIDE;
    int dpmaxx = width - PREVIEW_SIDE;
    int dpminy = PREVIEW_SIDE;
    int dpmaxy = height - PREVIEW_SIDE;

    // 计算缩放系数
    double factor, temp;
    factor = (double)(abs(m_maxX - m_minX)) / (dpmaxx - dpminx);      // 按x计算的缩放系数
    temp =   (double)(abs(m_maxY - m_minY)) / (dpmaxy - dpminy);      // 按轮廓计算，最小能够放下重复次数个图形
    if (temp >= factor)     // 使用较大的缩放系数
    {
        factor = temp;
    }

    // 计算显示参数，按照图形的实际位置显示（数据坐标零点对应图形中心）
    int dpx = (int)((dpminx+dpmaxx)/2 - ((m_maxX+m_minX)/factor)/2);
    int dpy = (int)((dpminy+dpmaxy)/2 - ((m_maxY+m_minY)/factor)/2);

    int curx, cury, prex, prey;
    curx = cury = prex = prey = 0;

    curx = dpx;
    cury = (height) - dpy;

    pen.setColor(pencolor);
    painter.setPen(pen);

    int nLineCount = m_pMarker->m_listPolyline.size();
    for(int i = 0; i < nLineCount; i++)
    {
        CRPPolyline polyLine = m_pMarker->m_listPolyline.at(i);
        int nPointCount = polyLine.m_listPoint.size();
        for(int j = 0; j < nPointCount; j++)
        {
            prex = curx;
            prey = cury;

            curx = polyLine.m_listPoint.at(j).x() / factor+ dpx;
            cury = height - (polyLine.m_listPoint.at(j).y() / factor + dpy);

            if(j == 0)
            {
                continue;
            }

            painter.drawLine(prex, prey, curx, cury);
        }
    }

    // 保存成文件
    if (saveflag != 0)
    {
        saveName += ".preview.bmp";
        pImage->save(saveName, "bmp");
    }

    if (pImage != NULL)
    {
        pImage = NULL;
        delete pImage;
    }

    return 0;
}
#endif
