-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcodeparser.cpp
249 lines (210 loc) · 10.5 KB
/
gcodeparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <QDebug>
#include <QFile>
#include <QPalette>
#include <QFileDialog>
#include <QMessageBox>
#include <gradientcolor.h>
static QVariant findECoordinate(QString currentLine){
QStringList splitLine = currentLine.split(" ", QString::SkipEmptyParts);
QStringListIterator itr(splitLine);
while(itr.hasNext()) {
QString cur = itr.next();
//Detect comment
if (cur.startsWith(";"))
break;
if (cur.startsWith('E')){
qDebug() << "Found an E coordinate: " + (cur.midRef(1,(cur.length()-2))).toString();
return QVariant(cur.midRef(1, (cur.length()-2)).toFloat()); //This has no error detection
}
}
return QVariant(QString());
}
static int findToolChange(QString currentLine){
return (currentLine.midRef(1,1).toInt()); //TODO: This has no error detection
}
//WRITE GCODE FOR DIAMOND HOTEND
static void writeGcode(int gradientStartLayer, int gradientEndLayer,
bool fancyRetraction,
QPalette startPal, QPalette endPal,
QTextStream *reader, QTextStream *writer){
//Get the current Colors of the two gradient buttons
QColor startColor = startPal.color(QPalette::Button);
QColor endColor = endPal.color(QPalette::Button);
//Next, calculate the number of layers we have to work with
int activeLayers = gradientEndLayer - gradientStartLayer;
//Now get the CMY componants of the corresponding colors
int startWeight = startColor.cyan();
int endWeight = endColor.cyan();
gradientColor cyan(startWeight, endWeight, activeLayers, 0);
startWeight = startColor.magenta();
endWeight = endColor.magenta();
gradientColor yellow(startWeight, endWeight, activeLayers, 1);
startWeight = startColor.yellow();
endWeight = endColor.yellow();
gradientColor magenta(startWeight, endWeight, activeLayers, 2);
QString currentLine;
QVariant newECoordinateVariant;
float prevECoordinate = 0.0;
bool currentlyRetracting = false;
int previousTool = 0;
//Now these are the variables we'll increment in the loop through the input file
int nextActiveLayer = gradientStartLayer;
// qDebug() << "fNextActiveLayer:" + QString::number(fNextActiveLayer) + " , nextActiveLayer: " + QString::number(nextActiveLayer) + " , fNextActivePercent: " + QString::number(fNextActivePercent) + " , nextActivePercent: " + QString::number(nextActivePercent);
// qDebug() << "ascending: " + QString::number(ascending) + ", percentDelta: " + QString::number(percentDelta) + ", layerDelta: " + QString::number(layerDelta) + ", activeLayers: " + QString::number(activeLayers) + ", activePercents: " + QString::number(activePercents);
while (!reader->atEnd())
{
//Read next line, transcribe it
currentLine = reader->readLine();
//Handle Fancy Retraction:
if (fancyRetraction){
QStringList splitLine = currentLine.split(" ", QString::SkipEmptyParts);
QString command = splitLine[0];
if (command == "G1" || command == "G0")
newECoordinateVariant = findECoordinate(currentLine);
else if (command == "G92")
prevECoordinate = 0.0;
else if (command.startsWith('T'))
previousTool = findToolChange(currentLine); //Not sure about this interaction
// qDebug() << "Command: " + command + ", prevECoordinate: " + QString::number(prevECoordinate) + ", newECoordinate: " + newECoordinateVariant.toString();
if (!newECoordinateVariant.isNull()){
float newECoordinate = newECoordinateVariant.toFloat();
if (currentlyRetracting){
if (newECoordinate > prevECoordinate){
// *writer << "//STOP RETRACTION \n";
*writer << (currentLine + "\n");
*writer << ("T" + QString::number(previousTool) + "\n");
currentlyRetracting = false;
prevECoordinate = newECoordinate;
}
else
*writer << currentLine + "\n";
}
else{
if (newECoordinate < prevECoordinate){
// *writer << "//START RETRACTION \n";
*writer << "T15 \n";
*writer << (currentLine + "\n");
currentlyRetracting = true;
prevECoordinate = newECoordinate;
}
else
*writer << currentLine + "\n";
}
}
else
*writer << currentLine + "\n";
}
else {
*writer << currentLine + "\n";
}
//If this is a layer we need to add a gradient command to, add it
if ((currentLine.contains("; layer " + QString::number(nextActiveLayer)) && (nextActiveLayer < gradientEndLayer)))
{
// //Adjust the searching variables to find the next active layer
nextActiveLayer += 1;
*writer << cyan.printAndIncrement();
*writer << yellow.printAndIncrement();
*writer << magenta.printAndIncrement();
*writer << "M164 S0\n";
// qDebug() << "fNextActiveLayer:" + QString::number(fNextActiveLayer) + " , nextActiveLayer: " + QString::number(nextActiveLayer) + " , fNextActivePercent: " + QString::number(fNextActivePercent) + " , nextActivePercent: " + QString::number(nextActivePercent);
}
}
QMessageBox::information(0,"done","All Done!");
}
//WRITE GCODE FOR BUILDER DUAL
static void writeGcode(int gradientStartLayer, int gradientEndLayer,
bool fancyRetraction,
int gradientStartPercent, int gradientEndPercent,
QTextStream *reader, QTextStream *writer){
QString currentLine;
//Default Layer and Percent deltas
float percentDelta = 1.0;
float layerDelta = 1.0;
//First, check to see if we're increasing gradient percent
int ascending = 1;
if (gradientStartPercent > gradientEndPercent)
ascending = -1;
//Also, lets keep track of how many percents we're changing (absolute value)
int activePercents = abs(gradientEndPercent - gradientStartPercent);
//Next, calculate the number of layers we have to work with
int activeLayers = gradientEndLayer - gradientStartLayer;
//See if we're moving more than one percent at a time, or more than one layer at a time
if (activeLayers < activePercents)
percentDelta = (float)activePercents/(float)activeLayers;
else
layerDelta = (float)activeLayers/(float)activePercents;
float fNextActiveLayer = gradientStartLayer;
int nextActiveLayer = gradientStartLayer;
float fNextActivePercent = gradientStartPercent;
int nextActivePercent = gradientStartPercent;
QVariant newECoordinateVariant;
float prevECoordinate = 0.0;
bool currentlyRetracting = false;
int previousTool = 0;
while (!reader->atEnd())
{
//Read next line, transcribe it
currentLine = reader->readLine();
//Handle Fancy Retraction:
if (fancyRetraction){
QStringList splitLine = currentLine.split(" ", QString::SkipEmptyParts);
QString command = splitLine[0];
if (command == "G1" || command == "G0")
newECoordinateVariant = findECoordinate(currentLine);
else if (command == "G92")
prevECoordinate = 0.0;
else if (command.startsWith('T'))
previousTool = findToolChange(currentLine); //Not sure about this interaction
qDebug() << "Command: " + command + ", prevECoordinate: " + QString::number(prevECoordinate) + ", newECoordinate: " + newECoordinateVariant.toString();
if (!newECoordinateVariant.isNull()){
float newECoordinate = newECoordinateVariant.toFloat();
if (currentlyRetracting){
if (newECoordinate > prevECoordinate){
*writer << "//STOP RETRACTION \n";
*writer << (currentLine + "\n");
*writer << ("G93 R" + QString::number(nextActivePercent-percentDelta) + "\n");
currentlyRetracting = false;
prevECoordinate = newECoordinate;
}
else
*writer << currentLine + "\n";
}
else{
if (newECoordinate < prevECoordinate){
*writer << "//START RETRACTION \n";
*writer << "G93 R50 \n";
*writer << (currentLine + "\n");
currentlyRetracting = true;
prevECoordinate = newECoordinate;
}
else
*writer << currentLine + "\n";
}
}
else
*writer << currentLine + "\n";
}
else {
*writer << currentLine + "\n";
}
//If this is a layer we need to add a gradient command to, add it
if ((currentLine.contains("; layer " + QString::number(nextActiveLayer)) && (nextActiveLayer < gradientEndLayer)))
{
if (!currentlyRetracting)
*writer << "G93 R" + QString::number(nextActivePercent) + "\n";
//Adjust the searching variables to find the next active layer
fNextActiveLayer += layerDelta;
nextActiveLayer = qRound(fNextActiveLayer);
fNextActivePercent += (percentDelta * ascending);
nextActivePercent = qRound(fNextActivePercent);
// qDebug() << "fNextActiveLayer:" + QString::number(fNextActiveLayer) + " , nextActiveLayer: " + QString::number(nextActiveLayer) + " , fNextActivePercent: " + QString::number(fNextActivePercent) + " , nextActivePercent: " + QString::number(nextActivePercent);
}
}
QMessageBox::information(0,"done","All Done!");
}
static int calculateGradientShifts(int start, int end, int startPercent, int endPercent)
{
int range = (end - start)/abs(endPercent - startPercent);
// qDebug() << (QString::number(end) + " - " + QString::number(start) + ") / abs (" + QString::number(endPercent) + " - " + QString::number(startPercent) + " = " + QString::number(range));
return range;
}