ENABLE_GCODE_VIEWER - Basic framework for new gcode viewer

This commit is contained in:
Enrico Turri 2020-03-02 15:13:23 +01:00
parent ebbebe3727
commit 3b6d334d7b
17 changed files with 173 additions and 48 deletions

View file

@ -698,11 +698,13 @@ std::vector<std::pair<coordf_t, std::vector<GCode::LayerToPrint>>> GCode::collec
return layers_to_print;
}
#if ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
void GCode::do_export(Print* print, const char* path, GCodePreviewData* preview_data, GCodeProcessor::Result* result, ThumbnailsGeneratorCallback thumbnail_cb)
#elif ENABLE_THUMBNAIL_GENERATOR
void GCode::do_export(Print* print, const char* path, GCodePreviewData* preview_data, ThumbnailsGeneratorCallback thumbnail_cb)
#else
void GCode::do_export(Print *print, const char *path, GCodePreviewData *preview_data)
#endif // ENABLE_THUMBNAIL_GENERATOR
#endif // ENABLE_GCODE_VIEWER
{
PROFILE_CLEAR();
@ -761,13 +763,11 @@ void GCode::do_export(Print *print, const char *path, GCodePreviewData *preview_
throw std::runtime_error(msg);
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#if ENABLE_GCODE_VIEWER
m_processor.apply_config(print->config());
m_processor.reset();
m_processor.process_file(path_tmp);
if (result != nullptr)
*result = std::move(m_processor.extract_result());
#endif // ENABLE_GCODE_VIEWER
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
GCodeTimeEstimator::PostProcessData normal_data = m_normal_time_estimator.get_post_process_data();
GCodeTimeEstimator::PostProcessData silent_data = m_silent_time_estimator.get_post_process_data();
@ -905,6 +905,25 @@ namespace DoExport {
analyzer.set_gcode_flavor(config.gcode_flavor);
}
#if ENABLE_GCODE_VIEWER
static void init_gcode_processor(const PrintConfig& config, GCodeProcessor& processor)
{
processor.reset();
processor.apply_config(config);
// send extruder offset data to processor
unsigned int num_extruders = static_cast<unsigned int>(config.nozzle_diameter.values.size());
GCodeProcessor::ExtruderOffsetsMap extruder_offsets;
for (unsigned int id = 0; id < num_extruders; ++id)
{
Vec2d offset = config.extruder_offset.get_at(id);
if (!offset.isApprox(Vec2d::Zero()))
extruder_offsets[id] = offset;
}
processor.set_extruder_offsets(extruder_offsets);
}
#endif // ENABLE_GCODE_VIEWER
static double autospeed_volumetric_limit(const Print &print)
{
// get the minimum cross-section used in the print
@ -1142,6 +1161,9 @@ void GCode::_do_export(Print& print, FILE* file)
// modifies the following:
m_normal_time_estimator, m_silent_time_estimator, m_silent_time_estimator_enabled);
DoExport::init_gcode_analyzer(print.config(), m_analyzer);
#if ENABLE_GCODE_VIEWER
DoExport::init_gcode_processor(print.config(), m_processor);
#endif // ENABLE_GCODE_VIEWER
// resets analyzer's tracking data
m_last_mm3_per_mm = GCodeAnalyzer::Default_mm3_per_mm;

View file

@ -14,11 +14,9 @@
#include "GCode/SpiralVase.hpp"
#include "GCode/ToolOrdering.hpp"
#include "GCode/WipeTower.hpp"
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#if ENABLE_GCODE_VIEWER
#include "GCode/GCodeProcessor.hpp"
#endif // ENABLE_GCODE_VIEWER
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#include "GCodeTimeEstimator.hpp"
#include "EdgeGrid.hpp"
#include "GCode/Analyzer.hpp"
@ -171,11 +169,13 @@ public:
// throws std::runtime_exception on error,
// throws CanceledException through print->throw_if_canceled().
#if ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
void do_export(Print* print, const char* path, GCodePreviewData* preview_data = nullptr, GCodeProcessor::Result* result = nullptr, ThumbnailsGeneratorCallback thumbnail_cb = nullptr);
#elif ENABLE_THUMBNAIL_GENERATOR
void do_export(Print* print, const char* path, GCodePreviewData* preview_data = nullptr, ThumbnailsGeneratorCallback thumbnail_cb = nullptr);
#else
void do_export(Print *print, const char *path, GCodePreviewData *preview_data = nullptr);
#endif // ENABLE_THUMBNAIL_GENERATOR
#endif // ENABLE_GCODE_VIEWER
// Exported for the helper classes (OozePrevention, Wipe) and for the Perl binding for unit tests.
const Vec2d& origin() const { return m_origin; }
@ -388,12 +388,10 @@ private:
// Analyzer
GCodeAnalyzer m_analyzer;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#if ENABLE_GCODE_VIEWER
// Processor
GCodeProcessor m_processor;
#endif // ENABLE_GCODE_VIEWER
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// Write a string into a file.
void _write(FILE* file, const std::string& what) { this->_write(file, what.c_str()); }

View file

@ -8,30 +8,26 @@ static const float MMMIN_TO_MMSEC = 1.0f / 60.0f;
namespace Slic3r {
void GCodeProcessor::apply_config(const PrintConfig& config)
{
m_parser.apply_config(config);
}
void GCodeProcessor::reset()
{
m_units = EUnits::Millimeters;
m_global_positioning_type = EPositioningType::Absolute;
m_e_local_positioning_type = EPositioningType::Absolute;
::memset(m_start_position.data(), 0, sizeof(AxisCoords));
::memset(m_end_position.data(), 0, sizeof(AxisCoords));
::memset(m_origin.data(), 0, sizeof(AxisCoords));
std::fill(m_start_position.begin(), m_start_position.end(), 0.0f);
std::fill(m_end_position.begin(), m_end_position.end(), 0.0f);
std::fill(m_origin.begin(), m_origin.end(), 0.0f);
m_feedrate = 0.0f;
m_extruder_id = 0;
m_moves.clear();
m_result.reset();
}
void GCodeProcessor::process_file(const std::string& filename)
{
MoveStep start_step {};
m_moves.emplace_back(start_step);
MoveVertex start_vertex {};
m_result.moves.emplace_back(start_vertex);
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) { process_gcode_line(line); });
int a = 0;
}
@ -149,9 +145,17 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
else if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f) || (delta_pos[Z] != 0.0f))
move_type = EMoveType::Travel;
// correct position by extruder offset
Vec3d extruder_offset = Vec3d::Zero();
auto it = m_extruder_offsets.find(m_extruder_id);
if (it != m_extruder_offsets.end())
extruder_offset = Vec3d(it->second(0), it->second(1), 0.0);
MoveStep move_step { m_end_position, m_feedrate, move_type };
m_moves.emplace_back(move_step);
MoveVertex vertex;
vertex.position = Vec3d(m_end_position[0], m_end_position[1], m_end_position[2]) + extruder_offset;
vertex.feedrate = m_feedrate;
vertex.type = move_type;
m_result.moves.emplace_back(vertex);
/*
std::cout << "start: ";

View file

@ -33,15 +33,24 @@ namespace Slic3r {
Num_Types
};
struct MoveStep
struct MoveVertex
{
AxisCoords position; // mm
float feedrate; // mm/s
EMoveType type;
Vec3d position{ Vec3d::Zero() }; // mm
float feedrate{ 0.0f }; // mm/s
// type of the move terminating at this vertex
EMoveType type{ EMoveType::Noop };
};
using MoveStepsList = std::vector<MoveStep>;
public:
typedef std::map<unsigned int, Vec2d> ExtruderOffsetsMap;
struct Result
{
std::vector<MoveVertex> moves;
void reset() { moves = std::vector<MoveVertex>(); }
};
private:
GCodeReader m_parser;
EUnits m_units;
@ -53,17 +62,22 @@ namespace Slic3r {
AxisCoords m_origin; // mm
float m_feedrate; // mm/s
unsigned int m_extruder_id;
ExtruderOffsetsMap m_extruder_offsets;
MoveStepsList m_moves;
Result m_result;
public:
GCodeProcessor() { reset(); }
void apply_config(const PrintConfig& config);
void apply_config(const PrintConfig& config) { m_parser.apply_config(config); }
void reset();
void set_extruder_offsets(const ExtruderOffsetsMap& extruder_offsets) { m_extruder_offsets = extruder_offsets; }
const Result& get_result() const { return m_result; }
Result&& extract_result() { return std::move(m_result); }
// Process the gcode contained in the file with the given filename
// Return false if any error occourred
void process_file(const std::string& filename);

View file

@ -1619,11 +1619,13 @@ void Print::process()
// The export_gcode may die for various reasons (fails to process output_filename_format,
// write error into the G-code, cannot execute post-processing scripts).
// It is up to the caller to show an error message.
#if ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
std::string Print::export_gcode(const std::string& path_template, GCodePreviewData* preview_data, GCodeProcessor::Result* result, ThumbnailsGeneratorCallback thumbnail_cb)
#elif ENABLE_THUMBNAIL_GENERATOR
std::string Print::export_gcode(const std::string& path_template, GCodePreviewData* preview_data, ThumbnailsGeneratorCallback thumbnail_cb)
#else
std::string Print::export_gcode(const std::string &path_template, GCodePreviewData *preview_data)
#endif // ENABLE_THUMBNAIL_GENERATOR
#endif // ENABLE_GCODE_VIEWER
{
// output everything to a G-code file
// The following call may die if the output_filename_format template substitution fails.
@ -1640,11 +1642,13 @@ std::string Print::export_gcode(const std::string &path_template, GCodePreviewDa
// The following line may die for multiple reasons.
GCode gcode;
#if ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
gcode.do_export(this, path.c_str(), preview_data, result, thumbnail_cb);
#elif ENABLE_THUMBNAIL_GENERATOR
gcode.do_export(this, path.c_str(), preview_data, thumbnail_cb);
#else
gcode.do_export(this, path.c_str(), preview_data);
#endif // ENABLE_THUMBNAIL_GENERATOR
#endif // ENABLE_GCODE_VIEWER
return path.c_str();
}

View file

@ -14,6 +14,9 @@
#if ENABLE_THUMBNAIL_GENERATOR
#include "GCode/ThumbnailData.hpp"
#endif // ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
#include "GCode/GCodeProcessor.hpp"
#endif // ENABLE_GCODE_VIEWER
#include "libslic3r.h"
@ -364,11 +367,13 @@ public:
void process() override;
// Exports G-code into a file name based on the path_template, returns the file path of the generated G-code file.
// If preview_data is not null, the preview_data is filled in for the G-code visualization (not used by the command line Slic3r).
#if ENABLE_THUMBNAIL_GENERATOR
#if ENABLE_GCODE_VIEWER
std::string export_gcode(const std::string& path_template, GCodePreviewData* preview_data, GCodeProcessor::Result* result, ThumbnailsGeneratorCallback thumbnail_cb = nullptr);
#elif ENABLE_THUMBNAIL_GENERATOR
std::string export_gcode(const std::string& path_template, GCodePreviewData* preview_data, ThumbnailsGeneratorCallback thumbnail_cb = nullptr);
#else
std::string export_gcode(const std::string &path_template, GCodePreviewData *preview_data);
#endif // ENABLE_THUMBNAIL_GENERATOR
#endif // ENABLE_GCODE_VIEWER
// methods for handling state
bool is_step_done(PrintStep step) const { return Inherited::is_step_done(step); }

View file

@ -48,7 +48,6 @@
#define ENABLE_2_2_0_BETA1 1
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//==================
// 2.3.0.alpha1 techs
//==================
@ -56,7 +55,6 @@
// Enable G-Code viewer
#define ENABLE_GCODE_VIEWER (1 && ENABLE_2_3_0_ALPHA1)
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#endif // _technologies_h_