2017-04-26 12:24:31 +00:00
|
|
|
#ifndef slic3r_GCodeReader_hpp_
|
|
|
|
#define slic3r_GCodeReader_hpp_
|
|
|
|
|
|
|
|
#include "libslic3r.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include "PrintConfig.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
|
|
|
class GCodeReader {
|
|
|
|
public:
|
|
|
|
class GCodeLine {
|
|
|
|
public:
|
|
|
|
GCodeReader* reader;
|
|
|
|
std::string raw;
|
|
|
|
std::string cmd;
|
|
|
|
std::string comment;
|
|
|
|
std::map<char,std::string> args;
|
|
|
|
|
|
|
|
GCodeLine(GCodeReader* _reader) : reader(_reader) {};
|
|
|
|
|
2017-06-30 15:05:58 +00:00
|
|
|
bool has(char arg) const { return this->args.count(arg) > 0; };
|
2017-05-18 14:53:19 +00:00
|
|
|
float get_float(char arg) const { return float(atof(this->args.at(arg).c_str())); };
|
|
|
|
float new_X() const { return this->has('X') ? float(atof(this->args.at('X').c_str())) : this->reader->X; };
|
|
|
|
float new_Y() const { return this->has('Y') ? float(atof(this->args.at('Y').c_str())) : this->reader->Y; };
|
|
|
|
float new_Z() const { return this->has('Z') ? float(atof(this->args.at('Z').c_str())) : this->reader->Z; };
|
|
|
|
float new_E() const { return this->has('E') ? float(atof(this->args.at('E').c_str())) : this->reader->E; };
|
|
|
|
float new_F() const { return this->has('F') ? float(atof(this->args.at('F').c_str())) : this->reader->F; };
|
2017-04-26 12:24:31 +00:00
|
|
|
float dist_X() const { return this->new_X() - this->reader->X; };
|
|
|
|
float dist_Y() const { return this->new_Y() - this->reader->Y; };
|
|
|
|
float dist_Z() const { return this->new_Z() - this->reader->Z; };
|
|
|
|
float dist_E() const { return this->new_E() - this->reader->E; };
|
|
|
|
float dist_XY() const {
|
|
|
|
float x = this->dist_X();
|
|
|
|
float y = this->dist_Y();
|
|
|
|
return sqrt(x*x + y*y);
|
|
|
|
};
|
2017-06-30 15:05:58 +00:00
|
|
|
bool extruding() const { return this->cmd == "G1" && this->dist_E() > 0; };
|
2017-04-26 12:24:31 +00:00
|
|
|
bool retracting() const { return this->cmd == "G1" && this->dist_E() < 0; };
|
2017-06-30 15:05:58 +00:00
|
|
|
bool travel() const { return this->cmd == "G1" && ! this->has('E'); };
|
2017-04-26 12:24:31 +00:00
|
|
|
void set(char arg, std::string value);
|
|
|
|
};
|
|
|
|
typedef std::function<void(GCodeReader&, const GCodeLine&)> callback_t;
|
|
|
|
|
|
|
|
float X, Y, Z, E, F;
|
|
|
|
bool verbose;
|
|
|
|
callback_t callback;
|
|
|
|
|
2017-06-30 15:05:58 +00:00
|
|
|
GCodeReader() : X(0), Y(0), Z(0), E(0), F(0), verbose(false), m_extrusion_axis('E') {};
|
2017-10-17 14:01:18 +00:00
|
|
|
void apply_config(const GCodeConfig &config);
|
|
|
|
void apply_config(const DynamicPrintConfig &config);
|
2017-04-26 12:24:31 +00:00
|
|
|
void parse(const std::string &gcode, callback_t callback);
|
|
|
|
void parse_line(std::string line, callback_t callback);
|
|
|
|
void parse_file(const std::string &file, callback_t callback);
|
|
|
|
|
|
|
|
private:
|
2017-06-30 15:05:58 +00:00
|
|
|
GCodeConfig m_config;
|
|
|
|
char m_extrusion_axis;
|
2017-04-26 12:24:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace Slic3r */
|
|
|
|
|
|
|
|
#endif /* slic3r_GCodeReader_hpp_ */
|