add gcodewriter tests, correct some build errors

This commit is contained in:
supermerill 2019-04-01 16:10:09 +02:00
parent 16368c9575
commit 0f65a8d815
11 changed files with 102 additions and 69 deletions

View File

@ -432,7 +432,8 @@ bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, con
} }
ConfigOption *opt = this->option(opt_key, true); ConfigOption *opt = this->option(opt_key, true);
assert(opt != nullptr); if (opt == nullptr)
throw new UnknownOptionException(opt_key);
return opt->deserialize(value, append); return opt->deserialize(value, append);
} }

View File

@ -1407,14 +1407,15 @@ private:
class StaticConfig : public virtual ConfigBase class StaticConfig : public virtual ConfigBase
{ {
public: public:
StaticConfig() {}
/// Gets list of config option names for each config option of this->def, which has a static counter-part defined by the derived object /// Gets list of config option names for each config option of this->def, which has a static counter-part defined by the derived object
/// and which could be resolved by this->optptr(key) call. /// and which could be resolved by this->optptr(key) call.
t_config_option_keys keys() const; t_config_option_keys keys() const;
protected:
/// Set all statically defined config options to their defaults defined by this->def(). /// Set all statically defined config options to their defaults defined by this->def().
/// used (only) by tests
void set_defaults(); void set_defaults();
protected:
StaticConfig() {}
}; };
} }

View File

@ -278,12 +278,12 @@ std::string GCodeWriter::set_speed(double F, const std::string &comment, const s
std::string GCodeWriter::travel_to_xy(const Vec2d &point, const std::string &comment) std::string GCodeWriter::travel_to_xy(const Vec2d &point, const std::string &comment)
{ {
m_pos(0) = point(0); m_pos.x() = point.x();
m_pos(1) = point(1); m_pos.y() = point.y();
std::ostringstream gcode; std::ostringstream gcode;
gcode << "G1 X" << XYZF_NUM(point(0)) gcode << "G1 X" << XYZF_NUM(point.x())
<< " Y" << XYZF_NUM(point(1)) << " Y" << XYZF_NUM(point.y())
<< " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0);
COMMENT(comment); COMMENT(comment);
gcode << "\n"; gcode << "\n";
@ -296,9 +296,9 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
don't perform the Z move but we only move in the XY plane and don't perform the Z move but we only move in the XY plane and
adjust the nominal Z by reducing the lift amount that will be adjust the nominal Z by reducing the lift amount that will be
used for unlift. */ used for unlift. */
if (!this->will_move_z(point(2))) { if (!this->will_move_z(point.z())) {
double nominal_z = m_pos(2) - m_lifted; double nominal_z = m_pos.z() - m_lifted;
m_lifted = m_lifted - (point(2) - nominal_z); m_lifted = m_lifted - (point.z() - nominal_z);
return this->travel_to_xy(to_2d(point)); return this->travel_to_xy(to_2d(point));
} }
@ -308,9 +308,9 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
m_pos = point; m_pos = point;
std::ostringstream gcode; std::ostringstream gcode;
gcode << "G1 X" << XYZF_NUM(point(0)) gcode << "G1 X" << XYZF_NUM(point.x())
<< " Y" << XYZF_NUM(point(1)) << " Y" << XYZF_NUM(point.y())
<< " Z" << XYZF_NUM(point(2)) << " Z" << XYZF_NUM(point.z())
<< " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0);
COMMENT(comment); COMMENT(comment);
gcode << "\n"; gcode << "\n";
@ -323,7 +323,7 @@ std::string GCodeWriter::travel_to_z(double z, const std::string &comment)
we don't perform the move but we only adjust the nominal Z by we don't perform the move but we only adjust the nominal Z by
reducing the lift amount that will be used for unlift. */ reducing the lift amount that will be used for unlift. */
if (!this->will_move_z(z)) { if (!this->will_move_z(z)) {
double nominal_z = m_pos(2) - m_lifted; double nominal_z = m_pos.z() - m_lifted;
m_lifted = m_lifted - (z - nominal_z); m_lifted = m_lifted - (z - nominal_z);
return ""; return "";
} }
@ -336,7 +336,7 @@ std::string GCodeWriter::travel_to_z(double z, const std::string &comment)
std::string GCodeWriter::_travel_to_z(double z, const std::string &comment) std::string GCodeWriter::_travel_to_z(double z, const std::string &comment)
{ {
m_pos(2) = z; m_pos.z() = z;
std::ostringstream gcode; std::ostringstream gcode;
gcode << "G1 Z" << XYZF_NUM(z) gcode << "G1 Z" << XYZF_NUM(z)
@ -351,8 +351,8 @@ bool GCodeWriter::will_move_z(double z) const
/* If target Z is lower than current Z but higher than nominal Z /* If target Z is lower than current Z but higher than nominal Z
we don't perform an actual Z move. */ we don't perform an actual Z move. */
if (m_lifted > 0) { if (m_lifted > 0) {
double nominal_z = m_pos(2) - m_lifted; double nominal_z = m_pos.z() - m_lifted;
if (z >= nominal_z && z <= m_pos(2)) if (z >= nominal_z && z <= m_pos.z())
return false; return false;
} }
return true; return true;
@ -360,13 +360,13 @@ bool GCodeWriter::will_move_z(double z) const
std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std::string &comment) std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std::string &comment)
{ {
m_pos(0) = point(0); m_pos.x() = point.x();
m_pos(1) = point(1); m_pos.y() = point.y();
m_extruder->extrude(dE); m_extruder->extrude(dE);
std::ostringstream gcode; std::ostringstream gcode;
gcode << "G1 X" << XYZF_NUM(point(0)) gcode << "G1 X" << XYZF_NUM(point.x())
<< " Y" << XYZF_NUM(point(1)) << " Y" << XYZF_NUM(point.y())
<< " " << m_extrusion_axis << E_NUM(m_extruder->E()); << " " << m_extrusion_axis << E_NUM(m_extruder->E());
COMMENT(comment); COMMENT(comment);
gcode << "\n"; gcode << "\n";
@ -375,8 +375,8 @@ std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std:
std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std::string &comment) std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std::string &comment)
{ {
m_pos(0) = point(0); m_pos.x() = point.x();
m_pos(1) = point(1); m_pos.y() = point.y();
m_lifted = 0; m_lifted = 0;
m_extruder->extrude(dE); m_extruder->extrude(dE);
@ -487,12 +487,15 @@ std::string GCodeWriter::lift()
{ {
double above = this->config.retract_lift_above.get_at(m_extruder->id()); double above = this->config.retract_lift_above.get_at(m_extruder->id());
double below = this->config.retract_lift_below.get_at(m_extruder->id()); double below = this->config.retract_lift_below.get_at(m_extruder->id());
if (m_pos(2) >= above && (below == 0 || m_pos(2) <= below)) if (m_pos.z() >= above && (below == 0 || m_pos.z() <= below))
target_lift = this->config.retract_lift.get_at(m_extruder->id()); target_lift = this->config.retract_lift.get_at(m_extruder->id());
} }
if (m_lifted == 0 && target_lift > 0) { // compare against epsilon because travel_to_z() does math on it
// and subtracting layer_height from retract_lift might not give
// exactly zero
if (std::abs(m_lifted) < EPSILON && target_lift > 0) {
m_lifted = target_lift; m_lifted = target_lift;
return this->_travel_to_z(m_pos(2) + target_lift, "lift Z"); return this->_travel_to_z(m_pos.z() + target_lift, "lift Z");
} }
return ""; return "";
} }
@ -501,7 +504,7 @@ std::string GCodeWriter::unlift()
{ {
std::string gcode; std::string gcode;
if (m_lifted > 0) { if (m_lifted > 0) {
gcode += this->_travel_to_z(m_pos(2) - m_lifted, "restore layer Z"); gcode += this->_travel_to_z(m_pos.z() - m_lifted, "restore layer Z");
m_lifted = 0; m_lifted = 0;
} }
return gcode; return gcode;

View File

@ -727,13 +727,13 @@ public:
ConfigOptionString extrusion_axis; ConfigOptionString extrusion_axis;
ConfigOptionFloats extrusion_multiplier; ConfigOptionFloats extrusion_multiplier;
ConfigOptionFloat fan_speedup_time; ConfigOptionFloat fan_speedup_time;
ConfigOptionFloats filament_diameter;
ConfigOptionFloats filament_density;
ConfigOptionStrings filament_type;
ConfigOptionBools filament_soluble;
ConfigOptionFloats filament_cost; ConfigOptionFloats filament_cost;
ConfigOptionFloats filament_density;
ConfigOptionFloats filament_diameter;
ConfigOptionBools filament_soluble;
ConfigOptionFloats filament_max_volumetric_speed; ConfigOptionFloats filament_max_volumetric_speed;
ConfigOptionFloats filament_max_wipe_tower_speed; ConfigOptionFloats filament_max_wipe_tower_speed;
ConfigOptionStrings filament_type;
ConfigOptionFloats filament_loading_speed; ConfigOptionFloats filament_loading_speed;
ConfigOptionFloats filament_loading_speed_start; ConfigOptionFloats filament_loading_speed_start;
ConfigOptionFloats filament_load_time; ConfigOptionFloats filament_load_time;
@ -767,8 +767,8 @@ public:
ConfigOptionFloats retract_restart_extra; ConfigOptionFloats retract_restart_extra;
ConfigOptionFloats retract_restart_extra_toolchange; ConfigOptionFloats retract_restart_extra_toolchange;
ConfigOptionFloats retract_speed; ConfigOptionFloats retract_speed;
ConfigOptionString start_gcode;
ConfigOptionStrings start_filament_gcode; ConfigOptionStrings start_filament_gcode;
ConfigOptionString start_gcode;
ConfigOptionBool single_extruder_multi_material; ConfigOptionBool single_extruder_multi_material;
ConfigOptionBool single_extruder_multi_material_priming; ConfigOptionBool single_extruder_multi_material_priming;
ConfigOptionString toolchange_gcode; ConfigOptionString toolchange_gcode;

View File

@ -13,6 +13,7 @@ set(SLIC3R_TEST_SOURCES
# libslic3r/test_config.cpp # toredo # libslic3r/test_config.cpp # toredo
libslic3r/test_fill.cpp libslic3r/test_fill.cpp
libslic3r/test_flow.cpp libslic3r/test_flow.cpp
libslic3r/test_gcodewriter.cpp
) )
if (NOT TARGET Catch) if (NOT TARGET Catch)

View File

@ -2,12 +2,13 @@
#include <fstream> #include <fstream>
#include <cstdio> #include <cstdio>
#include "../test_options.hpp" #include "../test_data.hpp"
#include "../../slic3r.hpp" #include "../../slic3r.hpp"
#include "../../libslic3r/GCodeReader.hpp" #include "../../libslic3r/GCodeReader.hpp"
using namespace Slic3r; using namespace Slic3r;
using namespace Slic3r::Test;
using namespace std::string_literals; using namespace std::string_literals;
bool file_exists(const std::string& name, const std::string& ext) { bool file_exists(const std::string& name, const std::string& ext) {

View File

@ -5,34 +5,59 @@ end_filament_gcode = "; Filament-specific end gcode \n;END gcode for filament\n"
end_gcode = M104 S0 ; turn off temperature\nG28 X0 ; home X axis\nM84 ; disable motors\n end_gcode = M104 S0 ; turn off temperature\nG28 X0 ; home X axis\nM84 ; disable motors\n
extrusion_axis = E extrusion_axis = E
extrusion_multiplier = 1 extrusion_multiplier = 1
fan_speedup_time = 0
filament_cost = 0 filament_cost = 0
filament_density = 0 filament_density = 0
filament_diameter = 3 filament_diameter = 3
filament_max_volumetric_speed = 0 filament_max_volumetric_speed = 0
filament_notes = "" filament_max_wipe_tower_speed = 0
# filament_type
# filament_loading_speed
# filament_loading_speed_start
# filament_load_time
# filament_unloading_speed
# filament_unloading_speed_start
# filament_toolchange_delay
# filament_unload_time
# filament_cooling_moves
# filament_cooling_initial_speed
# filament_minimal_purge_on_wipe_tower
# filament_wipe_advanced_pigment
# filament_cooling_final_speed
# filament_ramming_parameters
gcode_comments = 0 gcode_comments = 0
gcode_flavor = reprap gcode_flavor = reprap
label_printed_objects = 0 gcode_label_objects = false
layer_gcode = layer_gcode =
max_print_speed = 80 max_print_speed = 80
max_volumetric_speed = 0 max_volumetric_speed = 0
notes = retract_before_wipe = 0
pressure_advance = 0
printer_notes =
retract_length = 2 retract_length = 2
retract_length_toolchange = 10 retract_length_toolchange = 10
retract_lift = 1.5 retract_lift = 1.5
retract_lift_above = 0 retract_lift_above = 0
retract_lift_below = 0 retract_lift_below = 0
retract_lift_not_last_layer = false
retract_restart_extra = 0 retract_restart_extra = 0
retract_restart_extra_toolchange = 0 retract_restart_extra_toolchange = 0
retract_speed = 40 retract_speed = 40
start_filament_gcode = "; Filament gcode\n" start_filament_gcode = "; Filament gcode\n"
start_gcode = G28 ; home all axes\nG1 Z5 F5000 ; lift nozzle\n start_gcode = G28 ; home all axes\nG1 Z5 F5000 ; lift nozzle\n
#single_extruder_multi_material = false
#single_extruder_multi_material_priming = false
toolchange_gcode = toolchange_gcode =
travel_speed = 130 travel_speed = 130
use_firmware_retraction = 0 use_firmware_retraction = false
use_relative_e_distances = 0 use_relative_e_distances = false
use_set_and_wait_bed = 0 use_volumetric_e = false
use_set_and_wait_extruder = 0 # cooling_tube_retraction;
use_volumetric_e = 0 # cooling_tube_length;
# high_current_on_filament_swap;
# parking_pos_retraction;
# remaining_times;
# silent_mode;
# extra_loading_move;
# wipe_advanced;
# wipe_advanced_nozzle_melted_volume;
# wipe_advanced_multiplier;
# wipe_advanced_algo;

View File

@ -1,16 +1,18 @@
#include <catch.hpp> #include <catch.hpp>
#include <memory> #include <memory>
#include "GCodeWriter.hpp" #include "../../libslic3r/GCodeWriter.hpp"
#include "test_options.hpp" #include "../test_options.hpp"
//#include "../test_data.hpp" // get access to init_print, etc
using namespace Slic3r; using namespace Slic3r;
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
// can't understand what the test want to test: here we are overflowing the double capacity to break the lift logic...
SCENARIO("lift() and unlift() behavior with large values of Z", "[!shouldfail]") { SCENARIO("lift() and unlift() behavior with large values of Z", "[!shouldfail]") {
GIVEN("A config from a file and a single extruder.") { GIVEN("A config from a file and a single extruder.") {
GCodeWriter writer; GCodeWriter writer;
auto& config {writer.config}; GCodeConfig& config {writer.config};
config.set_defaults(); config.set_defaults();
config.load(std::string(testfile_dir) + "test_gcodewriter/config_lift_unlift.ini"s); config.load(std::string(testfile_dir) + "test_gcodewriter/config_lift_unlift.ini"s);
@ -40,7 +42,7 @@ SCENARIO("lift() and unlift() behavior with large values of Z", "[!shouldfail]")
SCENARIO("lift() is not ignored after unlift() at normal values of Z") { SCENARIO("lift() is not ignored after unlift() at normal values of Z") {
GIVEN("A config from a file and a single extruder.") { GIVEN("A config from a file and a single extruder.") {
GCodeWriter writer; GCodeWriter writer;
auto& config {writer.config}; GCodeConfig& config {writer.config};
config.set_defaults(); config.set_defaults();
config.load(std::string(testfile_dir) + "test_gcodewriter/config_lift_unlift.ini"s); config.load(std::string(testfile_dir) + "test_gcodewriter/config_lift_unlift.ini"s);

View File

@ -1,9 +1,9 @@
#include "test_data.hpp" #include "test_data.hpp"
#include "../../libslic3r/TriangleMesh.hpp" #include "../libslic3r/TriangleMesh.hpp"
#include "../../libslic3r/GCodeReader.hpp" #include "../libslic3r/GCodeReader.hpp"
#include "../../libslic3r/Config.hpp" #include "../libslic3r/Config.hpp"
#include "../../libslic3r/Print.hpp" #include "../libslic3r/Print.hpp"
#include "../../libslic3r/Point.hpp" #include "../libslic3r/Point.hpp"
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <fstream> #include <fstream>
@ -292,7 +292,19 @@ void clean_file(const std::string& name, const std::string& ext, bool glob) {
filename.append("."); filename.append(".");
filename.append(ext); filename.append(ext);
std::remove(testfile(filename).c_str()); bool file_exist = false;
{
std::ifstream f(testfile(filename));
file_exist = f.good();
f.close();
}
if (file_exist) std::remove(testfile(filename).c_str());
else {
std::ifstream f(filename);
file_exist = f.good();
f.close();
if (file_exist) std::remove(filename.c_str());
}
} }
Slic3r::Model model(const std::string& model_name, TriangleMesh&& _mesh) { Slic3r::Model model(const std::string& model_name, TriangleMesh&& _mesh) {

View File

@ -1,14 +0,0 @@
#ifndef TEST_OPTIONS_HPP
#include <string>
/// Directory path, passed in from the outside, for the path to the test inputs dir.
constexpr auto* testfile_dir {"C:/local/Slic3rcpp/src/test/inputs/"};
inline std::string testfile(std::string filename) {
std::string result;
result.append(testfile_dir);
result.append(filename);
return result;
}
#endif // TEST_OPTIONS_HPP

View File

@ -1,10 +1,11 @@
#ifndef TEST_OPTIONS_HPP #ifndef TEST_OPTIONS_HPP
#define TEST_OPTIONS_HPP
#include <string> #include <string>
/// Directory path, passed in from the outside, for the path to the test inputs dir. /// Directory path, passed in from the outside, for the path to the test inputs dir.
constexpr auto* testfile_dir {"@TESTFILE_DIR@"}; constexpr char* testfile_dir {"@TESTFILE_DIR@"};
inline std::string testfile(std::string filename) { inline std::string testfile(const std::string &filename) {
std::string result; std::string result;
result.append(testfile_dir); result.append(testfile_dir);
result.append(filename); result.append(filename);