From 0f65a8d81588815507a64ab8dbc9ef388afd9957 Mon Sep 17 00:00:00 2001 From: supermerill Date: Mon, 1 Apr 2019 16:10:09 +0200 Subject: [PATCH] add gcodewriter tests, correct some build errors --- src/libslic3r/Config.cpp | 3 +- src/libslic3r/Config.hpp | 5 +- src/libslic3r/GCodeWriter.cpp | 51 ++++++++++--------- src/libslic3r/PrintConfig.hpp | 10 ++-- src/test/CMakeLists.txt | 1 + src/test/GUI/test_cli.cpp | 3 +- .../test_gcodewriter/config_lift_unlift.ini | 45 ++++++++++++---- src/test/libslic3r/test_gcodewriter.cpp | 10 ++-- src/test/test_data.cpp | 24 ++++++--- src/test/test_options.hpp | 14 ----- src/test/test_options.hpp.in | 5 +- 11 files changed, 102 insertions(+), 69 deletions(-) delete mode 100644 src/test/test_options.hpp diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index fd1e9e08c..968bcd474 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -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); - assert(opt != nullptr); + if (opt == nullptr) + throw new UnknownOptionException(opt_key); return opt->deserialize(value, append); } diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index bf768159d..3505b7ef4 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -1407,14 +1407,15 @@ private: class StaticConfig : public virtual ConfigBase { 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 /// and which could be resolved by this->optptr(key) call. t_config_option_keys keys() const; -protected: /// Set all statically defined config options to their defaults defined by this->def(). + /// used (only) by tests void set_defaults(); +protected: + StaticConfig() {} }; } diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index e1c2d3888..f3d774b09 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -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) { - m_pos(0) = point(0); - m_pos(1) = point(1); + m_pos.x() = point.x(); + m_pos.y() = point.y(); std::ostringstream gcode; - gcode << "G1 X" << XYZF_NUM(point(0)) - << " Y" << XYZF_NUM(point(1)) + gcode << "G1 X" << XYZF_NUM(point.x()) + << " Y" << XYZF_NUM(point.y()) << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); COMMENT(comment); 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 adjust the nominal Z by reducing the lift amount that will be used for unlift. */ - if (!this->will_move_z(point(2))) { - double nominal_z = m_pos(2) - m_lifted; - m_lifted = m_lifted - (point(2) - nominal_z); + if (!this->will_move_z(point.z())) { + double nominal_z = m_pos.z() - m_lifted; + m_lifted = m_lifted - (point.z() - nominal_z); 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; std::ostringstream gcode; - gcode << "G1 X" << XYZF_NUM(point(0)) - << " Y" << XYZF_NUM(point(1)) - << " Z" << XYZF_NUM(point(2)) + gcode << "G1 X" << XYZF_NUM(point.x()) + << " Y" << XYZF_NUM(point.y()) + << " Z" << XYZF_NUM(point.z()) << " F" << XYZF_NUM(this->config.travel_speed.value * 60.0); COMMENT(comment); 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 reducing the lift amount that will be used for unlift. */ 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); 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) { - m_pos(2) = z; + m_pos.z() = z; std::ostringstream gcode; 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 we don't perform an actual Z move. */ if (m_lifted > 0) { - double nominal_z = m_pos(2) - m_lifted; - if (z >= nominal_z && z <= m_pos(2)) + double nominal_z = m_pos.z() - m_lifted; + if (z >= nominal_z && z <= m_pos.z()) return false; } 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) { - m_pos(0) = point(0); - m_pos(1) = point(1); + m_pos.x() = point.x(); + m_pos.y() = point.y(); m_extruder->extrude(dE); std::ostringstream gcode; - gcode << "G1 X" << XYZF_NUM(point(0)) - << " Y" << XYZF_NUM(point(1)) + gcode << "G1 X" << XYZF_NUM(point.x()) + << " Y" << XYZF_NUM(point.y()) << " " << m_extrusion_axis << E_NUM(m_extruder->E()); COMMENT(comment); 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) { - m_pos(0) = point(0); - m_pos(1) = point(1); + m_pos.x() = point.x(); + m_pos.y() = point.y(); m_lifted = 0; 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 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()); } - 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; - 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 ""; } @@ -501,7 +504,7 @@ std::string GCodeWriter::unlift() { std::string gcode; 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; } return gcode; diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 5b3b4cfef..802d9bcb8 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -727,13 +727,13 @@ public: ConfigOptionString extrusion_axis; ConfigOptionFloats extrusion_multiplier; ConfigOptionFloat fan_speedup_time; - ConfigOptionFloats filament_diameter; - ConfigOptionFloats filament_density; - ConfigOptionStrings filament_type; - ConfigOptionBools filament_soluble; ConfigOptionFloats filament_cost; + ConfigOptionFloats filament_density; + ConfigOptionFloats filament_diameter; + ConfigOptionBools filament_soluble; ConfigOptionFloats filament_max_volumetric_speed; ConfigOptionFloats filament_max_wipe_tower_speed; + ConfigOptionStrings filament_type; ConfigOptionFloats filament_loading_speed; ConfigOptionFloats filament_loading_speed_start; ConfigOptionFloats filament_load_time; @@ -767,8 +767,8 @@ public: ConfigOptionFloats retract_restart_extra; ConfigOptionFloats retract_restart_extra_toolchange; ConfigOptionFloats retract_speed; - ConfigOptionString start_gcode; ConfigOptionStrings start_filament_gcode; + ConfigOptionString start_gcode; ConfigOptionBool single_extruder_multi_material; ConfigOptionBool single_extruder_multi_material_priming; ConfigOptionString toolchange_gcode; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index b334da67c..888632a6d 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -13,6 +13,7 @@ set(SLIC3R_TEST_SOURCES # libslic3r/test_config.cpp # toredo libslic3r/test_fill.cpp libslic3r/test_flow.cpp + libslic3r/test_gcodewriter.cpp ) if (NOT TARGET Catch) diff --git a/src/test/GUI/test_cli.cpp b/src/test/GUI/test_cli.cpp index bd112753a..7f70cb5ed 100644 --- a/src/test/GUI/test_cli.cpp +++ b/src/test/GUI/test_cli.cpp @@ -2,12 +2,13 @@ #include #include -#include "../test_options.hpp" +#include "../test_data.hpp" #include "../../slic3r.hpp" #include "../../libslic3r/GCodeReader.hpp" using namespace Slic3r; +using namespace Slic3r::Test; using namespace std::string_literals; bool file_exists(const std::string& name, const std::string& ext) { diff --git a/src/test/inputs/test_gcodewriter/config_lift_unlift.ini b/src/test/inputs/test_gcodewriter/config_lift_unlift.ini index 3e811b663..389bc6c23 100644 --- a/src/test/inputs/test_gcodewriter/config_lift_unlift.ini +++ b/src/test/inputs/test_gcodewriter/config_lift_unlift.ini @@ -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 extrusion_axis = E extrusion_multiplier = 1 +fan_speedup_time = 0 filament_cost = 0 filament_density = 0 filament_diameter = 3 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_flavor = reprap -label_printed_objects = 0 +gcode_label_objects = false layer_gcode = max_print_speed = 80 max_volumetric_speed = 0 -notes = -pressure_advance = 0 -printer_notes = +retract_before_wipe = 0 retract_length = 2 retract_length_toolchange = 10 retract_lift = 1.5 retract_lift_above = 0 retract_lift_below = 0 +retract_lift_not_last_layer = false retract_restart_extra = 0 retract_restart_extra_toolchange = 0 retract_speed = 40 start_filament_gcode = "; Filament gcode\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 = travel_speed = 130 -use_firmware_retraction = 0 -use_relative_e_distances = 0 -use_set_and_wait_bed = 0 -use_set_and_wait_extruder = 0 -use_volumetric_e = 0 +use_firmware_retraction = false +use_relative_e_distances = false +use_volumetric_e = false +# cooling_tube_retraction; +# 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; diff --git a/src/test/libslic3r/test_gcodewriter.cpp b/src/test/libslic3r/test_gcodewriter.cpp index 1273a5bc1..457549786 100644 --- a/src/test/libslic3r/test_gcodewriter.cpp +++ b/src/test/libslic3r/test_gcodewriter.cpp @@ -1,16 +1,18 @@ #include #include -#include "GCodeWriter.hpp" -#include "test_options.hpp" +#include "../../libslic3r/GCodeWriter.hpp" +#include "../test_options.hpp" +//#include "../test_data.hpp" // get access to init_print, etc using namespace Slic3r; 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]") { GIVEN("A config from a file and a single extruder.") { GCodeWriter writer; - auto& config {writer.config}; + GCodeConfig& config {writer.config}; config.set_defaults(); 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") { GIVEN("A config from a file and a single extruder.") { GCodeWriter writer; - auto& config {writer.config}; + GCodeConfig& config {writer.config}; config.set_defaults(); config.load(std::string(testfile_dir) + "test_gcodewriter/config_lift_unlift.ini"s); diff --git a/src/test/test_data.cpp b/src/test/test_data.cpp index 72becdf41..a0941a8e1 100644 --- a/src/test/test_data.cpp +++ b/src/test/test_data.cpp @@ -1,9 +1,9 @@ #include "test_data.hpp" -#include "../../libslic3r/TriangleMesh.hpp" -#include "../../libslic3r/GCodeReader.hpp" -#include "../../libslic3r/Config.hpp" -#include "../../libslic3r/Print.hpp" -#include "../../libslic3r/Point.hpp" +#include "../libslic3r/TriangleMesh.hpp" +#include "../libslic3r/GCodeReader.hpp" +#include "../libslic3r/Config.hpp" +#include "../libslic3r/Print.hpp" +#include "../libslic3r/Point.hpp" #include #include #include @@ -292,7 +292,19 @@ void clean_file(const std::string& name, const std::string& ext, bool glob) { filename.append("."); 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) { diff --git a/src/test/test_options.hpp b/src/test/test_options.hpp deleted file mode 100644 index 50b818f65..000000000 --- a/src/test/test_options.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef TEST_OPTIONS_HPP -#include - -/// 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 diff --git a/src/test/test_options.hpp.in b/src/test/test_options.hpp.in index dd43939dd..f0f4210a9 100644 --- a/src/test/test_options.hpp.in +++ b/src/test/test_options.hpp.in @@ -1,10 +1,11 @@ #ifndef TEST_OPTIONS_HPP +#define TEST_OPTIONS_HPP #include /// 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; result.append(testfile_dir); result.append(filename);