Frontend to the famous GRBL.

outputdeviceqpicture.cpp 9.0KB

    /* Part of x2grbl * * Copyright Johann Wilhelm <johann.wilhelm@9mal6.de> 2015 * * see Readme.md for detailed license and usage information! */ #include "outputdeviceqpicture.h" #include <QDebug> #include <limits> using namespace std; #include <QVector2D> #include <QSettings> OutputDeviceQPicture::OutputDeviceQPicture(QObject *parent) : GCodeOutputDevice(parent) { Painter.begin(&Picture); x=0; y=0; z=0; ToolDiameter=1; minX=numeric_limits<double>::max(); minY=numeric_limits<double>::max(); minZ=numeric_limits<double>::max(); maxX=numeric_limits<double>::lowest(); maxY=numeric_limits<double>::lowest(); maxZ=numeric_limits<double>::lowest(); } bool OutputDeviceQPicture::supported(GCode &Cmd) { if (Cmd.Code==CodeType_MCode) { switch(Cmd.Cmd) { case 2: //End Program case 3: //Turn Spin Clockwise case 5: //Stop Spindle case 6: //Set Tool return true; break; default: return false; } } if (Cmd.Code==CodeType_GCode) { switch(Cmd.Cmd) { case 0: //rapid case 1: //straight line case 2: //helical motion CW case 3: //helical motion CCW case 4: //dwell case 21: //mm-mode case 90: //absolute-mode return true; break; default: return false; } } return false; } bool OutputDeviceQPicture::Run(GCode &Cmd) { if (!supported(Cmd)) return false; if (Cmd.Code==CodeType_MCode) { switch(Cmd.Cmd) { case 2: //End Program case 3: //Turn Spin Clockwise case 5: //Stop Spindle return true; case 6: { if (Cmd.Parameters.contains(QString("T"))) { int Tool=Cmd.Parameters[QString("T")].toInt(); if (Mills.contains(Tool)) ToolDiameter=(double)Mills[Tool]/10.0; else if (Drills.contains(Tool)) ToolDiameter=(double)Drills[Tool]/10.0; else qDebug()<<"OutputDeviceQPicture Tool not found!\n"; } else { qDebug()<<"OutputDeviceQPicture Tool not specified!\n"; } } break; default: return false; } } double nextX=x; double nextY=y; double nextZ=z; bool ok=true; if (Cmd.Code==CodeType_GCode) { switch(Cmd.Cmd) { case 0: //rapid case 1: { //straight line if (Cmd.Parameters.contains(QString("X"))) nextX=Cmd.Parameters[QString("X")].toDouble(); if (Cmd.Parameters.contains(QString("Y"))) nextY=Cmd.Parameters[QString("Y")].toDouble(); if (Cmd.Parameters.contains(QString("Z"))) nextZ=Cmd.Parameters[QString("Z")].toDouble(); if (nextZ<0 /*&& nextZ>-0.2*/) { Painter.setBrush(QBrush(Qt::black, Qt::SolidPattern)); Painter.setPen(QPen(Painter.brush(), ToolDiameter, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); Painter.drawLine(QPointF(x,y), QPointF(nextX,nextY)); } } break; case 2: case 3: { if (Cmd.Parameters.contains(QString("X"))) nextX=Cmd.Parameters[QString("X")].toDouble(); if (Cmd.Parameters.contains(QString("Y"))) nextY=Cmd.Parameters[QString("Y")].toDouble(); if (Cmd.Parameters.contains(QString("Z"))) nextZ=Cmd.Parameters[QString("Z")].toDouble(); double r; if (Cmd.Parameters.contains(QString("r"))) nextZ=Cmd.Parameters[QString("r")].toDouble(); QRectF rect; //bounding-rect double alpha; //start double beta; //end if (Cmd.Parameters.contains(QString("I")) || Cmd.Parameters.contains(QString("J"))) { double i=0; double j=0; if (Cmd.Parameters.contains(QString("I"))) i=Cmd.Parameters[QString("I")].toDouble(); if (Cmd.Parameters.contains(QString("J"))) j=Cmd.Parameters[QString("J")].toDouble(); double r=sqrt(j*j+i*i); qDebug()<<"Circle r="<<r<<"\n"; QVector2D Center=QVector2D(x+i, y+j); QVector2D A=QVector2D(x,y)-Center; QVector2D B=QVector2D(nextX,nextY)-Center; alpha=acos(A.x()/r)/M_PI*180; if (A.y()<0) alpha*=-1; if (fabs(A.x()/r)>1) alpha=0; beta=acos(B.x()/r)/M_PI*180; if (B.y()<0) beta*=-1; if (fabs(B.x()/r)>1) beta=0; if (!Cmd.Parameters.contains(QString("X")) && !Cmd.Parameters.contains(QString("Y"))) { beta=360; alpha=0; } rect=QRectF(Center.x()-r, Center.y()-r, 2.0*r, 2.0*r); } else { qDebug()<<"AAAH\n"; } if (nextZ<0 /*&& nextZ>-0.2*/) { Painter.setPen(QPen(Painter.brush(), ToolDiameter, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); Painter.setBrush(QBrush()); // Painter.drawRect(rect); // Painter.drawEllipse(rect); // Painter.drawArc(rect, alpha*16, (fabs(alpha)+beta)*16); if (Cmd.Cmd==3) { double delta=0; if (beta<0) beta+=360; delta=beta-alpha; Painter.drawArc(rect, beta*16, delta*16); } if (Cmd.Cmd==2) { double delta=0; if (alpha<0) beta+=360; delta=alpha-beta; Painter.drawArc(rect, alpha*16, delta*16); } Painter.setBrush(QBrush(Qt::black, Qt::SolidPattern)); // Painter.drawLine(QPointF(x,y), QPointF(nextX,nextY)); } } break; case 4: //dwell case 21: //mm-mode case 90: //absolute-mode break; default: ok=false; } } if (!ok) return false; x=nextX; y=nextY; z=nextZ; if (x<minX) minX=x; if (y<minY) minY=y; if (z<minZ) minZ=z; if (x>maxX) maxX=x; if (y>maxY) maxY=y; if (z>maxZ) maxZ=z; return true; } QString OutputDeviceQPicture::name() { return QString("QPicture"); } QPicture OutputDeviceQPicture::picture() { Painter.end(); return Picture; } void OutputDeviceQPicture::Reset() { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QString("2.5d Grbl-Commander")); settings.beginGroup("MillingBits"); QStringList Keys=settings.allKeys(); for (int i=0;i<Keys.count();i++) { if (Keys[i].contains(QString("BitDiameter"))) { QString BitNumber=Keys[i]; while (BitNumber.count() && !BitNumber[0].isDigit()) { BitNumber.remove(0,1); } int BitDiameter=settings.value(Keys[i]).toInt(); Mills[BitNumber.toInt()]=BitDiameter; } } settings.endGroup(); settings.beginGroup("DrillingBits"); Keys=settings.allKeys(); for (int i=0;i<Keys.count();i++) { if (Keys[i].contains(QString("BitDiameter"))) { QString BitNumber=Keys[i]; while (BitNumber.count() && !BitNumber[0].isDigit()) { BitNumber.remove(0,1); } int BitDiameter=settings.value(Keys[i]).toInt(); Drills[BitNumber.toInt()+1000]=BitDiameter; } } settings.endGroup(); }