Files
OrcaSlicer-bambulab/src/libslic3r/GCode/GCodeProcessor.hpp
T

223 lines
6.4 KiB
C++
Raw Normal View History

2020-02-14 08:31:31 +01:00
#ifndef slic3r_GCodeProcessor_hpp_
#define slic3r_GCodeProcessor_hpp_
#if ENABLE_GCODE_VIEWER
#include "libslic3r/GCodeReader.hpp"
#include "libslic3r/Point.hpp"
#include "libslic3r/ExtrusionEntity.hpp"
2020-02-14 08:31:31 +01:00
2020-04-02 13:19:42 +02:00
#include <array>
2020-04-06 11:53:15 +02:00
#include <vector>
2020-04-02 13:19:42 +02:00
2020-02-14 08:31:31 +01:00
namespace Slic3r {
class GCodeProcessor
{
2020-04-02 12:03:18 +02:00
public:
static const std::string Extrusion_Role_Tag;
static const std::string Width_Tag;
static const std::string Height_Tag;
2020-04-03 10:15:46 +02:00
static const std::string Mm3_Per_Mm_Tag;
2020-04-06 11:53:15 +02:00
static const std::string Color_Change_Tag;
static const std::string Pause_Print_Tag;
static const std::string Custom_Code_Tag;
2020-04-02 12:03:18 +02:00
private:
2020-02-14 08:31:31 +01:00
using AxisCoords = std::array<float, 4>;
2020-04-06 17:24:11 +02:00
using ExtrudersColor = std::vector<unsigned char>;
2020-02-14 08:31:31 +01:00
enum class EUnits : unsigned char
{
Millimeters,
Inches
};
enum class EPositioningType : unsigned char
{
Absolute,
Relative
};
2020-04-06 08:55:48 +02:00
struct CachedPosition
{
AxisCoords position; // mm
float feedrate; // mm/s
2020-04-06 11:53:15 +02:00
void reset();
};
struct CpColor
{
2020-04-06 17:24:11 +02:00
unsigned char counter;
unsigned char current;
2020-04-06 11:53:15 +02:00
void reset();
2020-04-06 08:55:48 +02:00
};
2020-04-02 12:03:18 +02:00
public:
enum class EMoveType : unsigned char
{
Noop,
Retract,
Unretract,
Tool_change,
Color_change,
Pause_Print,
Custom_GCode,
Travel,
Extrude,
Count
};
struct MoveVertex
2020-02-14 08:31:31 +01:00
{
EMoveType type{ EMoveType::Noop };
2020-04-02 12:03:18 +02:00
ExtrusionRole extrusion_role{ erNone };
2020-04-06 17:24:11 +02:00
unsigned char extruder_id{ 0 };
unsigned char cp_color_id{ 0 };
2020-04-02 12:03:18 +02:00
Vec3f position{ Vec3f::Zero() }; // mm
2020-04-24 08:46:31 +02:00
float delta_extruder{ 0.0f }; // mm
2020-04-02 12:03:18 +02:00
float feedrate{ 0.0f }; // mm/s
float width{ 0.0f }; // mm
float height{ 0.0f }; // mm
2020-04-03 10:15:46 +02:00
float mm3_per_mm{ 0.0f };
float fan_speed{ 0.0f }; // percentage
float time{ 0.0f }; // s
2020-02-14 08:31:31 +01:00
float volumetric_rate() const { return feedrate * mm3_per_mm; }
2020-04-02 12:03:18 +02:00
std::string to_string() const
{
std::string str = std::to_string((int)type);
str += ", " + std::to_string((int)extrusion_role);
str += ", " + Slic3r::to_string((Vec3d)position.cast<double>());
str += ", " + std::to_string(extruder_id);
2020-04-06 11:53:15 +02:00
str += ", " + std::to_string(cp_color_id);
2020-04-02 12:03:18 +02:00
str += ", " + std::to_string(feedrate);
str += ", " + std::to_string(width);
str += ", " + std::to_string(height);
2020-04-03 10:15:46 +02:00
str += ", " + std::to_string(mm3_per_mm);
str += ", " + std::to_string(fan_speed);
2020-04-02 12:03:18 +02:00
return str;
}
};
2020-02-14 08:31:31 +01:00
struct Result
{
unsigned int id;
std::vector<MoveVertex> moves;
void reset() { moves = std::vector<MoveVertex>(); }
};
private:
2020-02-14 08:31:31 +01:00
GCodeReader m_parser;
EUnits m_units;
EPositioningType m_global_positioning_type;
EPositioningType m_e_local_positioning_type;
2020-04-02 12:03:18 +02:00
std::vector<Vec3f> m_extruder_offsets;
2020-04-06 08:55:48 +02:00
GCodeFlavor m_flavor;
2020-02-14 08:31:31 +01:00
AxisCoords m_start_position; // mm
AxisCoords m_end_position; // mm
AxisCoords m_origin; // mm
2020-04-06 08:55:48 +02:00
CachedPosition m_cached_position;
2020-02-14 08:31:31 +01:00
2020-04-03 10:15:46 +02:00
float m_feedrate; // mm/s
float m_width; // mm
float m_height; // mm
float m_mm3_per_mm;
float m_fan_speed; // percentage
2020-04-02 12:03:18 +02:00
ExtrusionRole m_extrusion_role;
2020-04-06 17:24:11 +02:00
unsigned char m_extruder_id;
2020-04-06 11:53:15 +02:00
ExtrudersColor m_extruders_color;
CpColor m_cp_color;
2020-02-14 08:31:31 +01:00
Result m_result;
static unsigned int s_result_id;
2020-02-14 08:31:31 +01:00
public:
GCodeProcessor() { reset(); }
2020-04-02 12:03:18 +02:00
void apply_config(const PrintConfig& config);
2020-02-14 08:31:31 +01:00
void reset();
const Result& get_result() const { return m_result; }
Result&& extract_result() { return std::move(m_result); }
2020-02-14 08:31:31 +01:00
// Process the gcode contained in the file with the given filename
void process_file(const std::string& filename);
private:
void process_gcode_line(const GCodeReader::GCodeLine& line);
// Process tags embedded into comments
2020-04-02 12:03:18 +02:00
void process_tags(const std::string& comment);
2020-02-14 08:31:31 +01:00
// Move
void process_G0(const GCodeReader::GCodeLine& line);
2020-02-14 08:31:31 +01:00
void process_G1(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
2020-04-03 10:15:46 +02:00
// Retract
void process_G10(const GCodeReader::GCodeLine& line);
// Unretract
void process_G11(const GCodeReader::GCodeLine& line);
// Firmware controlled Retract
void process_G22(const GCodeReader::GCodeLine& line);
// Firmware controlled Unretract
void process_G23(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Set to Absolute Positioning
2020-04-03 10:15:46 +02:00
void process_G90(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Set to Relative Positioning
2020-04-03 10:15:46 +02:00
void process_G91(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Set Position
2020-04-03 10:15:46 +02:00
void process_G92(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Set extruder to absolute mode
2020-04-03 10:15:46 +02:00
void process_M82(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Set extruder to relative mode
2020-04-03 10:15:46 +02:00
void process_M83(const GCodeReader::GCodeLine& line);
// Set fan speed
void process_M106(const GCodeReader::GCodeLine& line);
// Disable fan
void process_M107(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
2020-04-06 08:55:48 +02:00
// Set tool (Sailfish)
void process_M108(const GCodeReader::GCodeLine& line);
// Recall stored home offsets
void process_M132(const GCodeReader::GCodeLine& line);
// Set tool (MakerWare)
void process_M135(const GCodeReader::GCodeLine& line);
// Repetier: Store x, y and z position
void process_M401(const GCodeReader::GCodeLine& line);
// Repetier: Go to stored position
void process_M402(const GCodeReader::GCodeLine& line);
2020-04-02 12:03:18 +02:00
// Processes T line (Select Tool)
2020-04-03 10:15:46 +02:00
void process_T(const GCodeReader::GCodeLine& line);
2020-04-06 08:55:48 +02:00
void process_T(const std::string& command);
2020-04-02 12:03:18 +02:00
void store_move_vertex(EMoveType type);
};
2020-02-14 08:31:31 +01:00
} /* namespace Slic3r */
#endif // ENABLE_GCODE_VIEWER
#endif /* slic3r_GCodeProcessor_hpp_ */