diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index de5a132dbc..fae1cf399f 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -904,7 +904,9 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print const PrintInstance *print_instance; BoundingBox bounding_box; Polygon hull_polygon; - int object_index; + // Place of the instance in print.model()'s object and instance lists. Copies of one object get + // one PrintObject per distinct transformation, so print.objects() does not list them in order. + std::pair model_position; double height; // Whether the instance reaches above extruder_clearance_height_to_rod, and the Y extent of it // the rod can hit while a later instance prints (see above_rod_y_range). @@ -965,13 +967,24 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print } return any; }; - auto find_object_index = [](const Model& model, const ModelObject* obj) { - for (int index = 0; index < model.objects.size(); index++) - { - if (model.objects[index] == obj) - return index; - } - return -1; + // Place of an instance in print.model()'s object and instance lists, which is the order the user + // sees in the object list. print.objects() groups the copies of one object by transformation, so + // it cannot be used for this. + auto model_position_of = [](const Model &model, const ModelInstance *instance) { + const ModelObject *object = instance->get_object(); + int object_index = -1; + for (int index = 0; index < int(model.objects.size()); index++) + if (model.objects[index] == object) { + object_index = index; + break; + } + int instance_index = -1; + for (int index = 0; index < int(object->instances.size()); index++) + if (object->instances[index] == instance) { + instance_index = index; + break; + } + return std::make_pair(object_index, instance_index); }; auto [object_skirt_offset, _] = print.object_skirt_offset(); @@ -1056,7 +1069,7 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print } struct print_instance_info print_info {&instance, convex_hull.bounding_box(), convex_hull}; print_info.height = instance.print_object->height(); - print_info.object_index = find_object_index(print.model(), print_object->model_object()); + print_info.model_position = model_position_of(print.model(), instance.model_instance); print_info.has_above_rod = above_rod_y_range(instance, print_info.rod_min_y, print_info.rod_max_y); const BoundingBox footprint = print_info.bounding_box.inflated(-scale_(0.5 * print.config().extruder_clearance_radius.value + object_skirt_offset)); if (print_info.has_above_rod && !skirt_reaches_rod) { @@ -1098,16 +1111,15 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print { const size_t n = print_instance_with_bounding_box.size(); - auto by_object_index = [&](size_t l, size_t r) { - return print_instance_with_bounding_box[l].object_index < print_instance_with_bounding_box[r].object_index; - }; - + // Order by place in the model's object and instance lists. Copies of one object share an object + // index, and print.objects() lists them grouped by transformation, so the instance index is what + // keeps them in their list order. No key repeats, so the sort does not depend on being stable. std::vector fallback_order(n); for (size_t i = 0; i < n; ++i) fallback_order[i] = i; - std::stable_sort(fallback_order.begin(), fallback_order.end(), by_object_index); + std::sort(fallback_order.begin(), fallback_order.end(), [&](size_t l, size_t r) { + return print_instance_with_bounding_box[l].model_position < print_instance_with_bounding_box[r].model_position; + }); - // Every instance of an object shares its object_index, so order by place in the list instead, - // which tells instances apart and keeps copies of one object in their list order. std::vector list_position(n); for (size_t k = 0; k < n; ++k) list_position[fallback_order[k]] = k; @@ -1225,7 +1237,7 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print for (auto &inst : print_instance_with_bounding_box) BOOST_LOG_TRIVIAL(debug) << "after sequential-print ordering, print_instance " << inst.print_instance->model_instance->get_object()->name - << ", object_index: " << inst.object_index + << ", model_position: " << inst.model_position.first << "/" << inst.model_position.second << ", height: " << inst.height << (solved ? " [reordered for clearance]" : " [object-list order]"); } diff --git a/src/slic3r/GUI/SequentialPrintClearance.hpp b/src/slic3r/GUI/SequentialPrintClearance.hpp index 0f5d79be9c..d41f2579d2 100644 --- a/src/slic3r/GUI/SequentialPrintClearance.hpp +++ b/src/slic3r/GUI/SequentialPrintClearance.hpp @@ -20,7 +20,7 @@ struct SequentialClearanceInstance ObjectID instance_id; }; -// PrintApply may group rotated copies in a different order from ModelObject::instances. +// Print validation may order the instances of an object differently from ModelObject::instances. // Reorder one object's instances only when validation has assigned all of them an order. inline void sort_sequential_clearance_instances( std::vector::iterator first, std::vector::iterator last, diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index 2cc95e9ba5..18c26c41ea 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -1294,6 +1294,52 @@ TEST_CASE("Sequential printing keeps copies of one object in their list order", [](const ModelInstance *l, const ModelInstance *r) { return l->arrange_order < r->arrange_order; })); } +TEST_CASE("Sequential printing keeps rotated copies of one object in their list order", "[Print]") +{ + // Copies of one object that differ in rotation get one PrintObject each, and print.objects() lists + // those groups by transformation. Ordering by the object index alone therefore follows that + // grouping instead of the object list, and every second copy here prints out of place. + // The mesh is centred on the object origin, so the rotation turns the footprint in place instead + // of moving it, and the grid spacing below stays clear of the clearance radius. + TriangleMesh cube = make_cube(5, 5, 2); + cube.translate(-2.5f, -2.5f, 0.f); + std::vector meshes; + meshes.push_back(std::move(cube)); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "print_sequence", "by object" }, + { "skirt_loops", 0 }, + { "layer_change_gcode", "G92 E0\n" } // validate() relative-E reset + }); + Print print; + Model model; + Test::init_print(std::move(meshes), print, model, config, nullptr, /*arrange=*/false, /*instances=*/200); + const ModelInstancePtrs &instances = model.objects.front()->instances; + for (size_t k = 0; k < instances.size(); ++k) { + instances[k]->set_offset(Vec3d(10. + 12. * double(k % 15), 10. + 12. * double(k / 15), instances[k]->get_offset().z())); + if (k % 2 != 0) + instances[k]->set_rotation(Vec3d(0., 0., PI / 2)); + } + print.apply(model, config); + + std::vector warnings; + const StringObjectException error = print.validate(&warnings); + INFO(error.string); + REQUIRE(error.string.empty()); + CHECK(find_order_warning(warnings) == nullptr); + + // validate() numbered the instances it ordered; the copies must be numbered in list order. + const ModelInstancePtrs &copies = print.model().objects.front()->instances; + for (size_t k = 0; k < copies.size(); ++k) + REQUIRE(copies[k]->arrange_order == int(k + 1)); + + // The G-code export orders the instances from those numbers. + const std::vector ordered = sort_object_instances_by_model_order(print); + REQUIRE(ordered.size() == copies.size()); + for (size_t k = 0; k < copies.size(); ++k) + REQUIRE(ordered[k]->model_instance == copies[k]); +} + TEST_CASE("Sequential printing still lets objects below the rod use the rod height when the lid is lower", "[Print]") { // Some stock profiles put the lid below the rod, such as the Creality Ender-3 V3 KE (lid 34 mm, diff --git a/tests/slic3rutils/test_sequential_clearance.cpp b/tests/slic3rutils/test_sequential_clearance.cpp index eebf1f704e..8fd3134bd6 100644 --- a/tests/slic3rutils/test_sequential_clearance.cpp +++ b/tests/slic3rutils/test_sequential_clearance.cpp @@ -83,7 +83,7 @@ TEST_CASE("Sequential clearance allows the last object up to printable height", CHECK_THAT(warnings.front().second, WithinAbs(200., 1e-6)); } -TEST_CASE("Sequential clearance warns the same rotated copy as print validation", "[SequentialClearance][Regression]") +TEST_CASE("Sequential clearance warns the same copy as print validation", "[SequentialClearance][Regression]") { constexpr double rod_height = 10.; constexpr double lid_height = 30.; @@ -116,15 +116,17 @@ TEST_CASE("Sequential clearance warns the same rotated copy as print validation" Slic3r::Polygon hull = object->convex_hull_2d(object->instances[i]->get_matrix()); instances.push_back({object->get_instance_max_z(i), hull.bounding_box(), hull, object->instances[i]->id()}); } - const Slic3r::Polygon expected_warning = instances[1].hull_polygon; + // Copies print in object-list order, so the first copy is the one held to the rod height: the later + // copy shares its Y band. Print validation warns about that copy, and the clearance preview must + // warn about the same one. REQUIRE(validation_warnings.size() == 1); - CHECK(validation_warnings.front().first.bounding_box().contains(instances[1].bounding_box.center())); - CHECK_FALSE(validation_warnings.front().first.bounding_box().contains(instances[0].bounding_box.center())); + CHECK(validation_warnings.front().first.bounding_box().contains(instances[0].bounding_box.center())); + CHECK_FALSE(validation_warnings.front().first.bounding_box().contains(instances[1].bounding_box.center())); sort_sequential_clearance_instances(instances.begin(), instances.end(), print_order); const auto warnings = sequential_clearance_height_polygons(instances, printable_height, lid_height, rod_height); REQUIRE(warnings.size() == 1); - CHECK(warnings.front().first == expected_warning); + CHECK(warnings.front().first == instances[0].hull_polygon); CHECK_THAT(warnings.front().second, WithinAbs(rod_height, 1e-6)); }