diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 1b84b2463..864cce52c 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1445,7 +1445,6 @@ void GCode::_do_export(Print &print, FILE *file) print.throw_if_canceled(); m_cooling_buffer->set_current_extruder(initial_extruder_id); - m_writer.toolchange(initial_extruder_id); // Emit machine envelope limits for the Marlin firmware. this->print_machine_envelope(file, print); @@ -1453,7 +1452,7 @@ void GCode::_do_export(Print &print, FILE *file) // Disable fan. if ( print.config().disable_fan_first_layers.get_at(initial_extruder_id) && config().gcode_flavor != gcfKlipper) - _write(file, m_writer.set_fan(0, true)); + _write(file, m_writer.set_fan(0, true, initial_extruder_id)); // Let the start-up script prime the 1st printing tool. m_placeholder_parser.set("initial_tool", initial_extruder_id); diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index a87635a2c..bb3bcd8a1 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -31,6 +31,44 @@ void GCodeWriter::apply_print_region_config(const PrintRegionConfig& print_regio config_region = &print_region_config; } +std::vector GCodeWriter::extruder_ids() const { + std::vector out; + out.reserve(m_extruders.size()); + for (const Extruder& e : m_extruders) + out.push_back(e.id()); + return out; +} + +std::vector GCodeWriter::mill_ids() const { + std::vector out; + out.reserve(m_millers.size()); + for (const Tool& e : m_millers) + out.push_back(e.id()); + return out; +} + +uint16_t GCodeWriter::first_mill() const { + if (m_millers.empty()) { + uint16_t max = 0; + for (const Extruder& e : m_extruders) + max = std::max(max, e.id()); + max++; + return (uint16_t)max; + } else return m_millers.front().id(); +} +bool GCodeWriter::tool_is_extruder() const { + return m_tool->id() < first_mill(); +} +const Tool* GCodeWriter::get_tool(uint16_t id) const{ + for (const Extruder& e : m_extruders) + if (id == e.id()) + return &e; + for (const Tool& e : m_millers) + if (id == e.id()) + return &e; + return nullptr; +} + void GCodeWriter::set_extruders(std::vector extruder_ids) { std::sort(extruder_ids.begin(), extruder_ids.end()); @@ -89,9 +127,13 @@ std::string GCodeWriter::postamble() const std::string GCodeWriter::set_temperature(const unsigned int temperature, bool wait, int tool) { + //use m_tool if tool isn't set + if (tool < 0 && m_tool != nullptr) + tool = m_tool->id(); + //add offset int16_t temp_w_offset = int16_t(temperature); - temp_w_offset += int16_t(m_tool->temp_offset()); + temp_w_offset += int16_t(get_tool(tool)->temp_offset()); temp_w_offset = std::max(int16_t(0), std::min(int16_t(2000), temp_w_offset)); if (m_last_temperature_with_offset == temp_w_offset) @@ -175,13 +217,15 @@ std::string GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait return gcode.str(); } -std::string GCodeWriter::set_fan(const unsigned int speed, bool dont_save) +std::string GCodeWriter::set_fan(const unsigned int speed, bool dont_save, uint16_t default_tool) { std::ostringstream gcode; + const Tool *tool = m_tool == nullptr ? get_tool(default_tool) : m_tool; //add fan_offset int16_t fan_speed = int16_t(speed); - fan_speed += int8_t(m_tool->fan_offset()); + if (tool != nullptr) + fan_speed += int8_t(tool->fan_offset()); fan_speed = std::max(int16_t(0), std::min(int16_t(100), fan_speed)); //test if it's useful to write it diff --git a/src/libslic3r/GCodeWriter.hpp b/src/libslic3r/GCodeWriter.hpp index 87aced07f..0d8c7b42c 100644 --- a/src/libslic3r/GCodeWriter.hpp +++ b/src/libslic3r/GCodeWriter.hpp @@ -34,41 +34,21 @@ public: // Extruders are expected to be sorted in an increasing order. void set_extruders(std::vector extruder_ids); const std::vector& extruders() const { return m_extruders; } - std::vector extruder_ids() const { - std::vector out; - out.reserve(m_extruders.size()); - for (const Extruder& e : m_extruders) - out.push_back(e.id()); - return out; - } + std::vector extruder_ids() const; void set_mills(std::vector extruder_ids); const std::vector& mills() const { return m_millers; } - std::vector mill_ids() const { - std::vector out; - out.reserve(m_millers.size()); - for (const Tool& e : m_millers) - out.push_back(e.id()); - return out; - } + std::vector mill_ids() const; //give the first mill id or an id after the last extruder. Can be used to see if an id is an extruder or a mill - uint16_t first_mill() const { - if (m_millers.empty()) { - uint16_t max = 0; - for (const Extruder& e : m_extruders) - max = std::max(max, e.id()); - max++; - return (uint16_t)max; - }else return m_millers.front().id(); - } - bool tool_is_extruder() const { - return m_tool->id() < first_mill(); - } + uint16_t first_mill() const; + bool tool_is_extruder() const; + const Tool* get_tool(uint16_t id) const; std::string preamble(); std::string postamble() const; std::string set_temperature(unsigned int temperature, bool wait = false, int tool = -1); std::string set_bed_temperature(unsigned int temperature, bool wait = false); unsigned int get_fan() { return m_last_fan_speed; } - std::string set_fan(unsigned int speed, bool dont_save = false); + /// set fan at speed. Save it as current fan speed if !dont_save, and use tool default_tool if the internal m_tool is null (no toolchange done yet). + std::string set_fan(unsigned int speed, bool dont_save = false, uint16_t default_tool = 0); void set_acceleration(unsigned int acceleration); std::string write_acceleration(); std::string reset_e(bool force = false);