diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index deb2ed0f9f..fa85557b6e 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -3515,11 +3515,11 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato // object's empty lead-in), so they must not be counted here either or the layer // count in the file disagrees with its layer changes. const bool belt = print.config().belt_printer.value; - if (print.config().print_sequence == PrintSequence::ByObject) { - // Add each of the object's layers separately. - for (auto object : print.objects()) { - std::vector zs; - zs.reserve(object->layers().size() + object->support_layers().size()); + // Elsewhere process_layer() skips a layer its tool ordering gives no extruder, such as a tip too thin to print. + const auto count_printed_layers = [belt](const auto &objects, const ToolOrdering &tool_ordering) { + std::vector zs; + for (const PrintObject *object : objects) { + zs.reserve(zs.size() + object->layers().size() + object->support_layers().size()); for (auto layer : object->layers()) if (! belt || belt_object_layer_prints_something(*object, *layer)) zs.push_back(layer->print_z); @@ -3529,45 +3529,24 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato // Belt brim apron bands each get their own change_layer() call. for (const BeltBrimBand &band : object->belt_brim_prologue()) zs.push_back(band.print_z); - if (zs.empty()) - continue; - std::sort(zs.begin(), zs.end()); - //BBS: merge numerically very close Z values. - auto end_it = std::unique(zs.begin(), zs.end()); - unsigned int temp_layer_count = (unsigned int)(end_it - zs.begin()); - for (auto it = zs.begin(); it != end_it - 1; it++) { - if (abs(*it - *(it + 1)) < EPSILON) - temp_layer_count--; - } - m_layer_count += (unsigned int)(object->instances().size() * temp_layer_count); } - } else { - // Print all objects with the same print_z together. - std::vector zs; - for (auto object : print.objects()) { - zs.reserve(zs.size() + object->layers().size() + object->support_layers().size()); - for (auto layer : object->layers()) - if (! belt || belt_object_layer_prints_something(*object, *layer)) - zs.push_back(layer->print_z); - for (auto layer : object->support_layers()) - if (! belt || layer->has_extrusions()) - zs.push_back(layer->print_z); - // See the ByObject branch: apron bands are real printed layers. - for (const BeltBrimBand &band : object->belt_brim_prologue()) - zs.push_back(band.print_z); + if (! belt) + zs.erase(std::remove_if(zs.begin(), zs.end(), [&tool_ordering](coordf_t z) { + return tool_ordering.empty() || tool_ordering.tools_for_layer(z).extruders.empty(); + }), + zs.end()); + if (zs.empty()) + return 0u; + std::sort(zs.begin(), zs.end()); + //BBS: merge numerically very close Z values. + auto end_it = std::unique(zs.begin(), zs.end()); + unsigned int layer_count = (unsigned int)(end_it - zs.begin()); + for (auto it = zs.begin(); it != end_it - 1; it++) { + if (abs(*it - *(it + 1)) < EPSILON) + layer_count--; } - if (!zs.empty()) - { - std::sort(zs.begin(), zs.end()); - //BBS: merge numerically very close Z values. - auto end_it = std::unique(zs.begin(), zs.end()); - m_layer_count = (unsigned int)(end_it - zs.begin()); - for (auto it = zs.begin(); it != end_it - 1; it++) { - if (abs(*it - *(it + 1)) < EPSILON) - m_layer_count--; - } - } - } + return layer_count; + }; print.throw_if_canceled(); m_enable_cooling_markers = true; @@ -3643,6 +3622,8 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato tool_ordering = ToolOrdering(*(*print_object_instance_sequential_active)->print_object, initial_extruder_id); tool_ordering.sort_and_build_data(*(*print_object_instance_sequential_active)->print_object,initial_extruder_id); } + // Each instance prints the object's layers separately. + m_layer_count += count_printed_layers(std::vector{(*print_object_instance_sequential_active)->print_object}, tool_ordering); float temp_max_additional_fan = tool_ordering.cal_max_additional_fan(print.config()); if(temp_max_additional_fan > max_additional_fan ) max_additional_fan = temp_max_additional_fan; @@ -3669,6 +3650,8 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato // If the tool ordering has been pre-calculated by Print class for wipe tower already, reuse it. tool_ordering = print.tool_ordering(); tool_ordering.assign_custom_gcodes(print); + // All objects with the same print_z print together. + m_layer_count = count_printed_layers(print.objects(), tool_ordering); float temp_max_additional_fan = tool_ordering.cal_max_additional_fan(print.config()); if(temp_max_additional_fan > max_additional_fan ) max_additional_fan = temp_max_additional_fan; diff --git a/tests/fff_print/test_gcode.cpp b/tests/fff_print/test_gcode.cpp index 53f3ecc4eb..9f838329b3 100644 --- a/tests/fff_print/test_gcode.cpp +++ b/tests/fff_print/test_gcode.cpp @@ -1043,3 +1043,43 @@ TEST_CASE("Resonance avoidance keeps outer walls within their volumetric speed l CHECK_THAT(speed, Catch::Matchers::WithinAbs(expected_speed, 0.5)); } } + +TEST_CASE("The layer count leaves out layers that print nothing", "[GCode][Regression]") +{ + const std::string sequence = GENERATE(as{}, "by layer", "by object"); + CAPTURE(sequence); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({{"print_sequence", sequence}, + {"layer_height", 0.2}, + {"initial_layer_print_height", 0.2}, + {"wall_generator", "classic"}, + {"detect_thin_wall", false}, + {"extruder_clearance_radius", 20}, + {"machine_start_gcode", ";TOTAL_LAYERS=[total_layer_count]\n"}}); + Print print; + Model model; + // The top layer of each 10 mm cone slices to a circle 0.2 mm wide, too narrow for a wall, so it prints nothing. + Slic3r::Test::init_print({make_cone(10., 10.), make_cone(10., 10.)}, print, model, config, nullptr, false); + model.objects[0]->instances.front()->set_offset(Vec3d(60., 100., 0.)); + model.objects[1]->instances.front()->set_offset(Vec3d(140., 100., 0.)); + print.apply(model, config); + const std::string gcode = Slic3r::Test::gcode(print); + + long layer_changes = 0; + long placeholder = -1; + long trailer = -1; + std::istringstream lines(gcode); + for (std::string line; std::getline(lines, line);) { + if (line.rfind(";LAYER_CHANGE", 0) == 0) + ++layer_changes; + else if (line.rfind(";TOTAL_LAYERS=", 0) == 0) + placeholder = std::stol(line.substr(14)); + else if (line.rfind("; total layers count = ", 0) == 0) + trailer = std::stol(line.substr(23)); + } + const long object_layers = long(print.objects().front()->layer_count()) * (sequence == "by object" ? 2 : 1); + REQUIRE(layer_changes > 0); + REQUIRE(layer_changes < object_layers); + CHECK(placeholder == layer_changes); + CHECK(trailer == layer_changes); +}