From 11bb5c3117298c33a966c2964456684933a77403 Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Mon, 16 Jul 2018 17:10:37 -0500 Subject: [PATCH] factor out set_extruders() to an STL-style template function. Added set method as well. --- xs/src/libslic3r/GCode.cpp | 16 ---------------- xs/src/libslic3r/GCode.hpp | 27 ++++++++++++++++++++++++++- xs/src/libslic3r/GCodeWriter.cpp | 12 ------------ xs/src/libslic3r/GCodeWriter.hpp | 19 ++++++++++++++++++- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/xs/src/libslic3r/GCode.cpp b/xs/src/libslic3r/GCode.cpp index 0d924dad0..7eeea6bf6 100644 --- a/xs/src/libslic3r/GCode.cpp +++ b/xs/src/libslic3r/GCode.cpp @@ -233,22 +233,6 @@ GCode::apply_print_config(const PrintConfig &print_config) this->config.apply(print_config); } -void -GCode::set_extruders(const std::vector &extruder_ids) -{ - this->writer.set_extruders(extruder_ids); - - // enable wipe path generation if any extruder has wipe enabled - this->wipe.enable = false; - for (std::vector::const_iterator it = extruder_ids.begin(); - it != extruder_ids.end(); ++it) { - if (this->config.wipe.get_at(*it)) { - this->wipe.enable = true; - break; - } - } -} - void GCode::set_origin(const Pointf &pointf) { diff --git a/xs/src/libslic3r/GCode.hpp b/xs/src/libslic3r/GCode.hpp index a001daa00..cec896f64 100644 --- a/xs/src/libslic3r/GCode.hpp +++ b/xs/src/libslic3r/GCode.hpp @@ -12,6 +12,8 @@ #include "PrintConfig.hpp" #include "ConditionalGCode.hpp" #include +#include +#include namespace Slic3r { @@ -98,7 +100,30 @@ class GCode { void set_last_pos(const Point &pos); bool last_pos_defined() const; void apply_print_config(const PrintConfig &print_config); - void set_extruders(const std::vector &extruder_ids); + + /// Template function. + template + void set_extruders(Iter begin, Iter end) { + this->writer.set_extruders(begin, end); + // enable wipe path generation if any extruder has wipe enabled + this->wipe.enable = false; + for (Iter it = begin; it != end; ++it) { + if (this->config.wipe.get_at(*it)) { + this->wipe.enable = true; + break; + } + } + } + template + void set_extruders(const std::vector &extruder_ids) { + this->set_extruders(extruder_ids.cbegin(), extruder_ids.cend()); + } + + template + void set_extruders(const std::set &extruder_ids) { + this->set_extruders(extruder_ids.cbegin(), extruder_ids.cend()); + } + void set_origin(const Pointf &pointf); std::string preamble(); std::string notes(); diff --git a/xs/src/libslic3r/GCodeWriter.cpp b/xs/src/libslic3r/GCodeWriter.cpp index f39cec744..2bae7cbf9 100644 --- a/xs/src/libslic3r/GCodeWriter.cpp +++ b/xs/src/libslic3r/GCodeWriter.cpp @@ -21,18 +21,6 @@ GCodeWriter::apply_print_config(const PrintConfig &print_config) this->_extrusion_axis = this->config.get_extrusion_axis(); } -void -GCodeWriter::set_extruders(const std::vector &extruder_ids) -{ - for (std::vector::const_iterator i = extruder_ids.begin(); i != extruder_ids.end(); ++i) - this->extruders.insert( std::pair(*i, Extruder(*i, &this->config)) ); - - /* we enable support for multiple extruder if any extruder greater than 0 is used - (even if prints only uses that one) since we need to output Tx commands - first extruder has index 0 */ - this->multiple_extruders = (*std::max_element(extruder_ids.begin(), extruder_ids.end())) > 0; -} - std::string GCodeWriter::notes() { diff --git a/xs/src/libslic3r/GCodeWriter.hpp b/xs/src/libslic3r/GCodeWriter.hpp index dd4ae50dc..3e9259524 100644 --- a/xs/src/libslic3r/GCodeWriter.hpp +++ b/xs/src/libslic3r/GCodeWriter.hpp @@ -22,7 +22,24 @@ public: Extruder* extruder() const { return this->_extruder; } std::string extrusion_axis() const { return this->_extrusion_axis; } void apply_print_config(const PrintConfig &print_config); - void set_extruders(const std::vector &extruder_ids); + + + template + void set_extruders(Iter begin, Iter end) { + for (auto i = begin; i != end; ++i) + this->extruders.insert( std::pair(*i, Extruder(*i, &this->config)) ); + + /* we enable support for multiple extruder if any extruder greater than 0 is used + (even if prints only uses that one) since we need to output Tx commands + first extruder has index 0 */ + this->multiple_extruders = (*std::max_element(begin, end)) > 0; + } + + template + void set_extruders(const std::vector &extruder_ids) { this->set_extruders(extruder_ids.cbegin(), extruder_ids.cend()); } + template + void set_extruders(const std::set &extruder_ids) { this->set_extruders(extruder_ids.cbegin(), extruder_ids.cend()); } + /// Write any notes provided by the user as comments in the gcode header. std::string notes();