2017-04-26 12:24:31 +00:00
|
|
|
#include "GCodeReader.hpp"
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2020-09-09 13:03:51 +00:00
|
|
|
#include <boost/nowide/fstream.hpp>
|
2021-09-21 20:41:55 +00:00
|
|
|
#include <boost/nowide/cstdio.hpp>
|
2017-04-26 12:24:31 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2019-01-17 12:49:10 +00:00
|
|
|
#include <iomanip>
|
2021-09-21 13:30:37 +00:00
|
|
|
#include "Utils.hpp"
|
2017-04-26 12:24:31 +00:00
|
|
|
|
2021-05-10 05:08:38 +00:00
|
|
|
#include "LocalesUtils.hpp"
|
|
|
|
|
2018-01-03 16:29:49 +00:00
|
|
|
#include <Shiny/Shiny.h>
|
2021-09-09 13:25:14 +00:00
|
|
|
#include <fast_float/fast_float.h>
|
2018-01-03 16:29:49 +00:00
|
|
|
|
2017-04-26 12:24:31 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2021-08-09 15:37:25 +00:00
|
|
|
static inline char get_extrusion_axis_char(const GCodeConfig &config)
|
|
|
|
{
|
|
|
|
std::string axis = get_extrusion_axis(config);
|
|
|
|
assert(axis.size() <= 1);
|
|
|
|
// Return 0 for gcfNoExtrusion
|
|
|
|
return axis.empty() ? 0 : axis[0];
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:01:18 +00:00
|
|
|
void GCodeReader::apply_config(const GCodeConfig &config)
|
|
|
|
{
|
|
|
|
m_config = config;
|
2021-08-09 15:37:25 +00:00
|
|
|
m_extrusion_axis = get_extrusion_axis_char(m_config);
|
2017-10-17 14:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCodeReader::apply_config(const DynamicPrintConfig &config)
|
2017-04-26 12:24:31 +00:00
|
|
|
{
|
2017-06-30 15:05:58 +00:00
|
|
|
m_config.apply(config, true);
|
2021-08-09 15:37:25 +00:00
|
|
|
m_extrusion_axis = get_extrusion_axis_char(m_config);
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 14:19:16 +00:00
|
|
|
const char* GCodeReader::parse_line_internal(const char *ptr, const char *end, GCodeLine &gline, std::pair<const char*, const char*> &command)
|
2018-01-03 16:29:49 +00:00
|
|
|
{
|
|
|
|
PROFILE_FUNC();
|
2021-06-04 19:37:49 +00:00
|
|
|
|
|
|
|
assert(is_decimal_separator_point());
|
2017-04-26 12:24:31 +00:00
|
|
|
|
|
|
|
// command and args
|
2018-01-03 16:29:49 +00:00
|
|
|
const char *c = ptr;
|
2017-04-26 12:24:31 +00:00
|
|
|
{
|
2018-01-03 16:29:49 +00:00
|
|
|
PROFILE_BLOCK(command_and_args);
|
|
|
|
// Skip the whitespaces.
|
|
|
|
command.first = skip_whitespaces(c);
|
|
|
|
// Skip the command.
|
|
|
|
c = command.second = skip_word(command.first);
|
|
|
|
// Up to the end of line or comment.
|
|
|
|
while (! is_end_of_gcode_line(*c)) {
|
|
|
|
// Skip whitespaces.
|
|
|
|
c = skip_whitespaces(c);
|
|
|
|
if (is_end_of_gcode_line(*c))
|
|
|
|
break;
|
|
|
|
// Check the name of the axis.
|
2020-03-21 09:09:33 +00:00
|
|
|
Axis axis = NUM_AXES_WITH_UNKNOWN;
|
2018-01-03 16:29:49 +00:00
|
|
|
switch (*c) {
|
|
|
|
case 'X': axis = X; break;
|
|
|
|
case 'Y': axis = Y; break;
|
|
|
|
case 'Z': axis = Z; break;
|
|
|
|
case 'F': axis = F; break;
|
|
|
|
default:
|
2021-08-09 15:37:25 +00:00
|
|
|
if (*c == m_extrusion_axis) {
|
|
|
|
if (m_extrusion_axis != 0)
|
|
|
|
axis = E;
|
|
|
|
} else if (*c >= 'A' && *c <= 'Z')
|
2020-03-21 09:09:33 +00:00
|
|
|
// Unknown axis, but we still want to remember that such a axis was seen.
|
|
|
|
axis = UNKNOWN_AXIS;
|
2018-01-03 16:29:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-21 09:09:33 +00:00
|
|
|
if (axis != NUM_AXES_WITH_UNKNOWN) {
|
2018-01-03 16:29:49 +00:00
|
|
|
// Try to parse the numeric value.
|
2021-09-03 14:19:16 +00:00
|
|
|
double v;
|
2021-09-09 13:25:14 +00:00
|
|
|
auto [pend, ec] = fast_float::from_chars(++ c, end, v);
|
2021-09-03 14:19:16 +00:00
|
|
|
if (pend != c && is_end_of_word(*pend)) {
|
2018-01-03 16:29:49 +00:00
|
|
|
// The axis value has been parsed correctly.
|
2020-03-21 09:09:33 +00:00
|
|
|
if (axis != UNKNOWN_AXIS)
|
|
|
|
gline.m_axis[int(axis)] = float(v);
|
2018-01-03 16:29:49 +00:00
|
|
|
gline.m_mask |= 1 << int(axis);
|
|
|
|
c = pend;
|
|
|
|
} else
|
|
|
|
// Skip the rest of the word.
|
|
|
|
c = skip_word(c);
|
|
|
|
} else
|
|
|
|
// Skip the rest of the word.
|
|
|
|
c = skip_word(c);
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 16:29:49 +00:00
|
|
|
if (gline.has(E) && m_config.use_relative_e_distances)
|
|
|
|
m_position[E] = 0;
|
|
|
|
|
|
|
|
// Skip the rest of the line.
|
|
|
|
for (; ! is_end_of_line(*c); ++ c);
|
|
|
|
|
|
|
|
// Copy the raw string including the comment, without the trailing newlines.
|
|
|
|
if (c > ptr) {
|
|
|
|
PROFILE_BLOCK(copy_raw_string);
|
|
|
|
gline.m_raw.assign(ptr, c);
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
2018-01-03 16:29:49 +00:00
|
|
|
|
|
|
|
// Skip the trailing newlines.
|
|
|
|
if (*c == '\r')
|
|
|
|
++ c;
|
|
|
|
if (*c == '\n')
|
|
|
|
++ c;
|
|
|
|
|
|
|
|
if (m_verbose)
|
|
|
|
std::cout << gline.m_raw << std::endl;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCodeReader::update_coordinates(GCodeLine &gline, std::pair<const char*, const char*> &command)
|
|
|
|
{
|
|
|
|
PROFILE_FUNC();
|
|
|
|
if (*command.first == 'G') {
|
|
|
|
int cmd_len = int(command.second - command.first);
|
|
|
|
if ((cmd_len == 2 && (command.first[1] == '0' || command.first[1] == '1')) ||
|
|
|
|
(cmd_len == 3 && command.first[1] == '9' && command.first[2] == '2')) {
|
|
|
|
for (size_t i = 0; i < NUM_AXES; ++ i)
|
|
|
|
if (gline.has(Axis(i)))
|
2018-09-10 07:11:49 +00:00
|
|
|
m_position[i] = gline.value(Axis(i));
|
2018-01-03 16:29:49 +00:00
|
|
|
}
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 13:30:37 +00:00
|
|
|
template<typename ParseLineCallback, typename LineEndCallback>
|
|
|
|
bool GCodeReader::parse_file_raw_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback)
|
2017-04-26 12:24:31 +00:00
|
|
|
{
|
2021-09-21 13:30:37 +00:00
|
|
|
FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") };
|
|
|
|
|
|
|
|
// Read the input stream 64kB at a time, extract lines and process them.
|
2021-09-03 14:19:16 +00:00
|
|
|
std::vector<char> buffer(65536 * 10, 0);
|
2021-09-21 13:30:37 +00:00
|
|
|
// Line buffer.
|
|
|
|
std::string gcode_line;
|
|
|
|
size_t file_pos = 0;
|
2021-02-18 13:34:40 +00:00
|
|
|
m_parsing = true;
|
2021-09-21 13:30:37 +00:00
|
|
|
for (;;) {
|
|
|
|
size_t cnt_read = ::fread(buffer.data(), 1, buffer.size(), in.f);
|
|
|
|
if (::ferror(in.f))
|
2021-09-07 09:18:07 +00:00
|
|
|
return false;
|
2021-09-21 13:30:37 +00:00
|
|
|
bool eof = cnt_read == 0;
|
2021-09-03 14:19:16 +00:00
|
|
|
auto it = buffer.begin();
|
2021-09-21 13:30:37 +00:00
|
|
|
auto it_bufend = buffer.begin() + cnt_read;
|
|
|
|
while (it != it_bufend || (eof && ! gcode_line.empty())) {
|
|
|
|
// Find end of line.
|
|
|
|
bool eol = false;
|
2021-09-03 14:19:16 +00:00
|
|
|
auto it_end = it;
|
2021-09-21 13:30:37 +00:00
|
|
|
for (; it_end != it_bufend && ! (eol = *it_end == '\r' || *it_end == '\n'); ++ it_end)
|
|
|
|
if (*it_end == '\n')
|
2021-10-18 13:46:13 +00:00
|
|
|
line_end_callback(file_pos + (it_end - buffer.begin()) + 1);
|
2021-09-21 13:30:37 +00:00
|
|
|
// End of line is indicated also if end of file was reached.
|
|
|
|
eol |= eof && it_end == it_bufend;
|
2021-09-03 14:19:16 +00:00
|
|
|
if (eol) {
|
2021-09-21 13:30:37 +00:00
|
|
|
if (gcode_line.empty())
|
|
|
|
parse_line_callback(&(*it), &(*it_end));
|
2021-09-03 14:19:16 +00:00
|
|
|
else {
|
2021-09-21 13:30:37 +00:00
|
|
|
gcode_line.insert(gcode_line.end(), it, it_end);
|
|
|
|
parse_line_callback(gcode_line.c_str(), gcode_line.c_str() + gcode_line.size());
|
|
|
|
gcode_line.clear();
|
2021-09-03 14:19:16 +00:00
|
|
|
}
|
2021-09-21 13:30:37 +00:00
|
|
|
if (! m_parsing)
|
|
|
|
// The callback wishes to exit.
|
|
|
|
return true;
|
2021-09-03 14:19:16 +00:00
|
|
|
} else
|
2021-09-21 13:30:37 +00:00
|
|
|
gcode_line.insert(gcode_line.end(), it, it_end);
|
|
|
|
// Skip EOL.
|
|
|
|
it = it_end;
|
|
|
|
if (it != it_bufend && *it == '\r')
|
|
|
|
++ it;
|
|
|
|
if (it != it_bufend && *it == '\n') {
|
2021-10-18 13:46:13 +00:00
|
|
|
line_end_callback(file_pos + (it - buffer.begin()) + 1);
|
2021-09-21 13:30:37 +00:00
|
|
|
++ it;
|
|
|
|
}
|
2021-09-03 14:19:16 +00:00
|
|
|
}
|
2021-09-21 13:30:37 +00:00
|
|
|
if (eof)
|
|
|
|
break;
|
|
|
|
file_pos += cnt_read;
|
2021-09-03 14:19:16 +00:00
|
|
|
}
|
2021-09-07 09:18:07 +00:00
|
|
|
return true;
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:30:37 +00:00
|
|
|
template<typename ParseLineCallback, typename LineEndCallback>
|
|
|
|
bool GCodeReader::parse_file_internal(const std::string &filename, ParseLineCallback parse_line_callback, LineEndCallback line_end_callback)
|
|
|
|
{
|
|
|
|
GCodeLine gline;
|
|
|
|
return this->parse_file_raw_internal(filename,
|
|
|
|
[this, &gline, parse_line_callback](const char *begin, const char *end) {
|
|
|
|
gline.reset();
|
|
|
|
this->parse_line(begin, end, gline, parse_line_callback);
|
|
|
|
},
|
|
|
|
line_end_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCodeReader::parse_file(const std::string &file, callback_t callback)
|
|
|
|
{
|
|
|
|
return this->parse_file_internal(file, callback, [](size_t){});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCodeReader::parse_file(const std::string &file, callback_t callback, std::vector<size_t> &lines_ends)
|
|
|
|
{
|
|
|
|
lines_ends.clear();
|
|
|
|
return this->parse_file_internal(file, callback, [&lines_ends](size_t file_pos){ lines_ends.emplace_back(file_pos); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCodeReader::parse_file_raw(const std::string &filename, raw_line_callback_t line_callback)
|
|
|
|
{
|
|
|
|
return this->parse_file_raw_internal(filename,
|
|
|
|
[this, line_callback](const char *begin, const char *end) { line_callback(*this, begin, end); },
|
|
|
|
[](size_t){});
|
|
|
|
}
|
|
|
|
|
2018-08-04 15:38:25 +00:00
|
|
|
bool GCodeReader::GCodeLine::has(char axis) const
|
|
|
|
{
|
|
|
|
const char *c = m_raw.c_str();
|
|
|
|
// Skip the whitespaces.
|
|
|
|
c = skip_whitespaces(c);
|
|
|
|
// Skip the command.
|
|
|
|
c = skip_word(c);
|
|
|
|
// Up to the end of line or comment.
|
|
|
|
while (! is_end_of_gcode_line(*c)) {
|
|
|
|
// Skip whitespaces.
|
|
|
|
c = skip_whitespaces(c);
|
|
|
|
if (is_end_of_gcode_line(*c))
|
|
|
|
break;
|
|
|
|
// Check the name of the axis.
|
|
|
|
if (*c == axis)
|
|
|
|
return true;
|
|
|
|
// Skip the rest of the word.
|
|
|
|
c = skip_word(c);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-03 16:29:49 +00:00
|
|
|
bool GCodeReader::GCodeLine::has_value(char axis, float &value) const
|
2017-04-26 12:24:31 +00:00
|
|
|
{
|
2021-06-04 19:37:49 +00:00
|
|
|
assert(is_decimal_separator_point());
|
2018-01-03 16:29:49 +00:00
|
|
|
const char *c = m_raw.c_str();
|
|
|
|
// Skip the whitespaces.
|
|
|
|
c = skip_whitespaces(c);
|
|
|
|
// Skip the command.
|
|
|
|
c = skip_word(c);
|
|
|
|
// Up to the end of line or comment.
|
|
|
|
while (! is_end_of_gcode_line(*c)) {
|
|
|
|
// Skip whitespaces.
|
|
|
|
c = skip_whitespaces(c);
|
|
|
|
if (is_end_of_gcode_line(*c))
|
|
|
|
break;
|
|
|
|
// Check the name of the axis.
|
|
|
|
if (*c == axis) {
|
|
|
|
// Try to parse the numeric value.
|
|
|
|
char *pend = nullptr;
|
|
|
|
double v = strtod(++ c, &pend);
|
|
|
|
if (pend != nullptr && is_end_of_word(*pend)) {
|
|
|
|
// The axis value has been parsed correctly.
|
|
|
|
value = float(v);
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
2018-01-03 16:29:49 +00:00
|
|
|
// Skip the rest of the word.
|
|
|
|
c = skip_word(c);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCodeReader::GCodeLine::set(const GCodeReader &reader, const Axis axis, const float new_value, const int decimal_digits)
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << std::fixed << std::setprecision(decimal_digits) << new_value;
|
|
|
|
|
|
|
|
char match[3] = " X";
|
|
|
|
if (int(axis) < 3)
|
|
|
|
match[1] += int(axis);
|
|
|
|
else if (axis == F)
|
|
|
|
match[1] = 'F';
|
|
|
|
else {
|
|
|
|
assert(axis == E);
|
2021-08-09 15:37:25 +00:00
|
|
|
// Extruder axis is set.
|
|
|
|
assert(reader.extrusion_axis() != 0);
|
2018-01-03 16:29:49 +00:00
|
|
|
match[1] = reader.extrusion_axis();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->has(axis)) {
|
|
|
|
size_t pos = m_raw.find(match)+2;
|
|
|
|
size_t end = m_raw.find(' ', pos+1);
|
|
|
|
m_raw = m_raw.replace(pos, end-pos, ss.str());
|
|
|
|
} else {
|
|
|
|
size_t pos = m_raw.find(' ');
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
m_raw += std::string(match) + ss.str();
|
|
|
|
else
|
|
|
|
m_raw = m_raw.replace(pos, 0, std::string(match) + ss.str());
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
2018-01-03 16:29:49 +00:00
|
|
|
m_axis[axis] = new_value;
|
|
|
|
m_mask |= 1 << int(axis);
|
2017-04-26 12:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|