diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index 495fac2263..ff5c12c33c 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -897,7 +897,7 @@ public: static double nil_value() { return std::numeric_limits::quiet_NaN(); } // A scalar is nil, or all values of a vector are nil. bool is_nil() const override { for (auto v : this->values) if (! std::isnan(v)) return false; return true; } - bool is_nil(size_t idx) const override { return std::isnan(this->values[idx]); } + bool is_nil(size_t idx) const override { return this->values.empty() || std::isnan(idx < this->values.size() ? this->values[idx] : this->values.front()); } virtual void set_at_to_nil(size_t i) override { assert(nullable() && (i < this->values.size())); @@ -1081,7 +1081,7 @@ public: static int nil_value() { return std::numeric_limits::max(); } // A scalar is nil, or all values of a vector are nil. bool is_nil() const override { for (auto v : this->values) if (v != nil_value()) return false; return true; } - bool is_nil(size_t idx) const override { return this->values[idx] == nil_value(); } + bool is_nil(size_t idx) const override { return this->values.empty() || (idx < this->values.size() ? this->values[idx] : this->values.front()) == nil_value(); } virtual void set_at_to_nil(size_t i) override { assert(nullable() && (i < this->values.size())); @@ -1422,7 +1422,7 @@ public: static FloatOrPercent nil_value() { return { std::numeric_limits::quiet_NaN(), false }; } // A scalar is nil, or all values of a vector are nil. bool is_nil() const override { for (auto v : this->values) if (! std::isnan(v.value)) return false; return true; } - bool is_nil(size_t idx) const override { return std::isnan(this->values[idx].value); } + bool is_nil(size_t idx) const override { return this->values.empty() || std::isnan((idx < this->values.size() ? this->values[idx] : this->values.front()).value); } virtual void set_at_to_nil(size_t i) override { assert(nullable() && (i < this->values.size())); @@ -1974,7 +1974,7 @@ public: static unsigned char nil_value() { return std::numeric_limits::max(); } // A scalar is nil, or all values of a vector are nil. bool is_nil() const override { for (auto v : this->values) if (v != nil_value()) return false; return true; } - bool is_nil(size_t idx) const override { return this->values[idx] == nil_value(); } + bool is_nil(size_t idx) const override { return this->values.empty() || (idx < this->values.size() ? this->values[idx] : this->values.front()) == nil_value(); } virtual void set_at_to_nil(size_t i) override { assert(nullable() && (i < this->values.size())); diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 487814a286..717d2e9527 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -2203,6 +2203,14 @@ void GCodeProcessor::PreCoolingInjector::build_extruder_free_blocks(const std::v build_by_extruder_blocks(extruder_usage_blocks); } +// ConfigOptionVector::get_at() for the vectors copied out of the config. +template static T value_at_or_front(const std::vector& v, int i, T fallback) +{ + if (v.empty()) + return fallback; + return i >= 0 && i < (int) v.size() ? v[i] : v.front(); +} + // The core injector. Measures the idle-window duration from move.time[valid_machine_id], bails if it is // below the threshold, and emits pre-cool M104 at the window start and pre-heat M104 (relocated out of // SKIPPABLE blocks) at the back-solved heating-start move. @@ -2214,13 +2222,13 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc }; auto is_pre_cooling_valid = [&nozzle_temps = this->filament_nozzle_temps, &pre_cooling_temps = this->filament_pre_cooling_temps](int idx) -> bool { - if (idx < 0) + if (idx < 0 || idx >= (int) pre_cooling_temps.size() || idx >= (int) nozzle_temps.size()) return false; return pre_cooling_temps[idx] > 0 && pre_cooling_temps[idx] < nozzle_temps[idx]; }; auto get_partial_free_cooling_thres = [&](int idx) -> float { - if (idx < 0) + if (idx < 0 || idx >= (int) filament_nozzle_temps.size() || idx >= (int) filament_pre_cooling_temps.size()) return 30.f; float temp_in_tower = filament_nozzle_temps[idx]; return temp_in_tower - (float) (filament_pre_cooling_temps[idx]); @@ -2319,8 +2327,8 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc return; int extruder_id = get_valid_extruder_id(block.last_nozzle_id); - float ext_heating_rate = heating_rate[extruder_id]; - float ext_cooling_rate = cooling_rate[extruder_id]; + float ext_heating_rate = value_at_or_front(heating_rate, extruder_id, 1.0); + float ext_cooling_rate = value_at_or_front(cooling_rate, extruder_id, 1.0); auto add_M104_lines = [&](int gcode_id, int target_extruder, int target_temp, int target_filament, bool skippable, int next_filament_idx, int next_nozzle_id, TimeProcessor::InsertLineType type, const std::string& comment = std::string()) { auto format_line_M104 = [&](int target_extruder, int target_temp, int target_filament, bool skippable, int next_filament_idx, int next_nozzle_id, const std::string& comment = std::string()) -> std::vector { @@ -2330,7 +2338,7 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc std::string m632_line = "M632 S" + std::to_string(next_filament_idx); if (support_dynamic_nozzle_map) m632_line += " H" + std::to_string(next_nozzle_id); - if (extruder_max_nozzle_count[target_extruder] > 1) + if (value_at_or_front(extruder_max_nozzle_count, target_extruder, 1) > 1) m632_line += " N R"; m632_line += " W\n"; buffer.emplace_back(std::move(m632_line)); @@ -2340,7 +2348,7 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc if (handle_hotend_as_extruder) { M104_line += (" I" + std::to_string(target_filament == -1 ? next_filament_idx : target_filament)); } else if (target_extruder != -1) { - M104_line += (" T" + std::to_string(physical_extruder_map[target_extruder])); + M104_line += (" T" + std::to_string(target_extruder < (int) physical_extruder_map.size() ? physical_extruder_map[target_extruder] : target_extruder)); } M104_line += " S" + std::to_string(target_temp); diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp index 79998a7aa1..9b29ba0b53 100644 --- a/src/libslic3r/GCode/WipeTower.cpp +++ b/src/libslic3r/GCode/WipeTower.cpp @@ -131,6 +131,14 @@ inline float align_floor(float value, float base) return std::floor((value) / base) * base; } +// ConfigOptionVector::get_at() for the vectors copied out of the config. +template static inline T at_or_front(const std::vector &v, size_t i) +{ + if (v.empty()) + return T(0); + return i < v.size() ? v[i] : v.front(); +} + static bool is_valid_gcode(const std::string &gcode) { int str_size = gcode.size(); @@ -2139,7 +2147,6 @@ void WipeTower::set_extruder(size_t idx, const PrintConfig& config) m_filpar[idx].precool_target_temp.second = 0; float nozzle_temp_first_layer = config.nozzle_temperature_initial_layer.is_nil(idx) ? -1.f : float(config.nozzle_temperature_initial_layer.get_at(idx)); float nozzle_temp_other_layer = config.nozzle_temperature.is_nil(idx) ? -1.f : float(config.nozzle_temperature.get_at(idx)); - std::vector hotend_cooling_rates = config.hotend_cooling_rate.values; auto is_need_precooling = [&](bool extruder_change) -> bool { bool res = config.enable_pre_heating.value; @@ -2149,8 +2156,8 @@ void WipeTower::set_extruder(size_t idx, const PrintConfig& config) if (is_need_precooling(true)) { for (int i = 0; i < m_filpar[idx].precool_t.first.size(); i++) { if (config.hotend_cooling_rate.is_nil(i)) continue; - m_filpar[idx].precool_t.first[i] = std::max(0.f, nozzle_temp_other_layer - float(config.filament_pre_cooling_temperature.get_at(idx))) / float(hotend_cooling_rates[i]); - m_filpar[idx].precool_t_first_layer.first[i] = std::max(0.f, nozzle_temp_first_layer -float(config.filament_pre_cooling_temperature.get_at(idx))) /float(hotend_cooling_rates[i]); + m_filpar[idx].precool_t.first[i] = std::max(0.f, nozzle_temp_other_layer - float(config.filament_pre_cooling_temperature.get_at(idx))) / float(config.hotend_cooling_rate.get_at(i)); + m_filpar[idx].precool_t_first_layer.first[i] = std::max(0.f, nozzle_temp_first_layer -float(config.filament_pre_cooling_temperature.get_at(idx))) /float(config.hotend_cooling_rate.get_at(i)); } m_filpar[idx].precool_target_temp.first = config.filament_pre_cooling_temperature.get_at(idx); } @@ -2158,8 +2165,8 @@ void WipeTower::set_extruder(size_t idx, const PrintConfig& config) if (is_need_precooling(false)) { for (int i = 0; i < m_filpar[idx].precool_t.second.size(); i++) { if (config.hotend_cooling_rate.is_nil(i)) continue; - m_filpar[idx].precool_t.second[i] = std::max(0.f, nozzle_temp_other_layer - float(config.filament_pre_cooling_temperature_nc.get_at(idx))) / float(hotend_cooling_rates[i]); - m_filpar[idx].precool_t_first_layer.second[i] = std::max(0.f, nozzle_temp_first_layer -float(config.filament_pre_cooling_temperature_nc.get_at(idx))) /float(hotend_cooling_rates[i]); + m_filpar[idx].precool_t.second[i] = std::max(0.f, nozzle_temp_other_layer - float(config.filament_pre_cooling_temperature_nc.get_at(idx))) / float(config.hotend_cooling_rate.get_at(i)); + m_filpar[idx].precool_t_first_layer.second[i] = std::max(0.f, nozzle_temp_first_layer -float(config.filament_pre_cooling_temperature_nc.get_at(idx))) /float(config.hotend_cooling_rate.get_at(i)); } m_filpar[idx].precool_target_temp.second = config.filament_pre_cooling_temperature_nc.get_at(idx); } @@ -3100,7 +3107,7 @@ WipeTower::WipeTowerInfo::ToolChange WipeTower::set_toolchange(int old_tool, int float length_to_extrude = volume_to_length(wipe_volume, m_perimeter_width, layer_height); float toolchange_gap_width = get_block_gap_width(new_tool,false); float nozzlechange_gap_width = get_block_gap_width(old_tool,true); - float filament_change_length = !is_same_extruder(old_tool, new_tool, layer_id) ? m_filaments_change_length.first[old_tool] : m_filaments_change_length.second[old_tool]; + float filament_change_length = !is_same_extruder(old_tool, new_tool, layer_id) ? at_or_front(m_filaments_change_length.first, old_tool) : at_or_front(m_filaments_change_length.second, old_tool); depth += std::ceil(length_to_extrude / width) * toolchange_gap_width; // depth *= m_extra_spacing; @@ -3165,7 +3172,7 @@ void WipeTower::plan_toolchange(float z_par, float layer_height_par, unsigned in depth += std::ceil(length_to_extrude / width) * m_perimeter_width; //depth *= m_extra_spacing; - float filament_change_length = !is_same_extruder(old_tool, new_tool, layer_id) ? m_filaments_change_length.first[old_tool] : m_filaments_change_length.second[old_tool]; + float filament_change_length = !is_same_extruder(old_tool, new_tool, layer_id) ? at_or_front(m_filaments_change_length.first, old_tool) : at_or_front(m_filaments_change_length.second, old_tool); float nozzle_change_depth = 0; float nozzle_change_length = 0; if (is_need_ramming(old_tool, new_tool, layer_id)) { @@ -3327,8 +3334,8 @@ bool WipeTower::is_petg_filament(int filament_id) const bool WipeTower::is_need_reverse_travel(int filament_id,bool extruder_change) const { if (extruder_change) - return m_filpar[filament_id].ramming_travel_time.first > EPSILON && m_filaments_change_length.first[filament_id]>EPSILON; - return m_filpar[filament_id].ramming_travel_time.second > EPSILON && m_filaments_change_length.second[filament_id] > EPSILON; + return m_filpar[filament_id].ramming_travel_time.first > EPSILON && at_or_front(m_filaments_change_length.first, filament_id) > EPSILON; + return m_filpar[filament_id].ramming_travel_time.second > EPSILON && at_or_front(m_filaments_change_length.second, filament_id) > EPSILON; } // BBS: consider both soluable and support properties @@ -3697,8 +3704,11 @@ WipeTower::NozzleChangeResult WipeTower::ramming(int old_filament_id, int new_fi bool need_change_flow = false; float ramming_length = nozzle_change_line_count * (xr - xl); int extruder_id = get_extruder_id(m_current_tool, m_cur_layer_id); - float precool_t = extruder_change ? m_filpar[m_current_tool].precool_t.first[extruder_id] : m_filpar[m_current_tool].precool_t.second[extruder_id]; - float precool_t_first_layer = extruder_change ? m_filpar[m_current_tool].precool_t_first_layer.first[extruder_id] : + // -1: the filament is not in this layer's nozzle map. + float precool_t = extruder_id < 0 ? 0.f : + extruder_change ? m_filpar[m_current_tool].precool_t.first[extruder_id] : m_filpar[m_current_tool].precool_t.second[extruder_id]; + float precool_t_first_layer = extruder_id < 0 ? 0.f : + extruder_change ? m_filpar[m_current_tool].precool_t_first_layer.first[extruder_id] : m_filpar[m_current_tool].precool_t_first_layer.second[extruder_id]; float per_cooling_max_speed = nozzle_change_speed; if (extruder_change) { @@ -4308,7 +4318,7 @@ void WipeTower::toolchange_wipe_new(WipeTowerWriter &writer, const box_coordinat if (should_cooling_before_object) { int n = (cleaning_box.lu[1] - cleaning_box.ld[1]) / dy; int extruder_id = get_extruder_id(m_current_tool, m_cur_layer_id); - float cooling_time = (m_filpar[m_current_tool].filament_tower_interface_print_temp - m_filpar[m_current_tool].nozzle_temperature) / m_hotend_cooling_rate[extruder_id]; + float cooling_time = (m_filpar[m_current_tool].filament_tower_interface_print_temp - m_filpar[m_current_tool].nozzle_temperature) / at_or_front(m_hotend_cooling_rate, extruder_id); if (n < 2) { float estimate_time = estimate_wipe_time(0); speed_factor = estimate_time > cooling_time? 1: estimate_time / cooling_time; @@ -4485,8 +4495,10 @@ void WipeTower::set_nozzle_last_layer_id() for(int i =0 ; i= 0) m_last_layer_id[get_extruder_id(old_tool, idx)] = idx; - m_last_layer_id[get_extruder_id(new_tool, idx)] = idx; + if (int ex = old_tool >= 0 ? get_extruder_id(old_tool, idx) : -1; ex >= 0 && ex < (int) m_last_layer_id.size()) + m_last_layer_id[ex] = idx; + if (int ex = get_extruder_id(new_tool, idx); ex >= 0 && ex < (int) m_last_layer_id.size()) + m_last_layer_id[ex] = idx; } } } @@ -4957,6 +4969,9 @@ void WipeTower::generate_new(std::vector #include #include +#include +#include #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/GCodeReader.hpp" @@ -2040,4 +2042,81 @@ TEST_CASE("Unused extruder turns off on non-IMEX three-tool print", "[MultiFilam // T1 and T2 print up to 10mm — neither should get S0 before that. CHECK_FALSE(premature_s0_t1); CHECK_FALSE(premature_s0_t2); -} \ No newline at end of file +} + +static DynamicPrintConfig short_change_length_config() { + DynamicPrintConfig config = multifilament_config(2, { + // One value for two filaments: the tower must not read past it. + { "filament_change_length", "10" }, + { "filament_change_length_nc", "10" }, + { "nozzle_diameter", "0.4,0.4" }, + { "printer_extruder_id", "1,2" }, + { "printer_extruder_variant", "Direct Drive Standard,Direct Drive Standard" }, + { "extruder_printable_height", "0,0" }, + { "single_extruder_multi_material", 0 }, + { "filament_map_mode", "Manual" }, + { "filament_map", "1,2" }, + { "enable_prime_tower", 1 }, + { "prime_tower_width", 35 }, + { "wipe_tower_x", "50" }, + { "wipe_tower_y", "50" }, + { "layer_height", "0.3" }, + { "wipe_tower_type", "type1" }, + }); + size_flush_to_nozzles(config); + return config; +} + +// Sum of positive extrusion per NOZZLE_CHANGE block, keyed by the block's direction tag. +static std::map> nozzle_change_e_sums(const std::string& gcode) +{ + std::map> sums; + std::istringstream in(gcode); + std::string dir; + double esum = 0.; + for (std::string line; std::getline(in, line);) { + if (const size_t at = line.find("NOZZLE_CHANGE_START"); at != std::string::npos) { + dir = line.substr(at + std::string("NOZZLE_CHANGE_START").size() + 1); + esum = 0.; + } else if (line.find("NOZZLE_CHANGE_END") != std::string::npos) { + sums[dir].push_back(esum); + dir.clear(); + } else if (!dir.empty() && line.rfind("G1 ", 0) == 0) { + const size_t e = line.find(" E"); + if (e != std::string::npos && line[e + 2] != '-') + esum += std::stod(line.substr(e + 2)); + } + } + return sums; +} + +// Identical filaments ram the same length in both directions. Medians skip the first layer's +// flow-ratio outlier. +TEST_CASE("Nozzle change ramming is symmetric for identical filaments", "[MultiFilament]") +{ + TriangleMesh a = cube(20); + a.translate(80, 80, 0); + TriangleMesh b = cube(20); + b.translate(110, 80, 0); + std::vector meshes; + meshes.push_back(std::move(a)); + meshes.push_back(std::move(b)); + const std::vector> overrides = + { { { "extruder", 1 } }, { { "extruder", 2 } } }; + Print print; + Model model; + init_print(std::move(meshes), print, model, short_change_length_config(), &overrides, /*arrange=*/false); + const std::string gcode = Slic3r::Test::gcode(print); + REQUIRE(gcode.find("; WIPE_TOWER_START") != std::string::npos); + auto sums = nozzle_change_e_sums(gcode); + REQUIRE(sums.size() == 2); + std::vector medians; + for (auto& [dir, e] : sums) { + INFO("direction " << dir); + REQUIRE_FALSE(e.empty()); + std::sort(e.begin(), e.end()); + medians.push_back(e[e.size() / 2]); + CHECK(medians.back() > 0.); + } + CHECK_THAT(medians[0], Catch::Matchers::WithinRel(medians[1], 0.01)); +}