From 0f15db8b83fcfc8edd1b5a44eed48c026c250c2d Mon Sep 17 00:00:00 2001 From: Eldenroot Date: Sun, 8 Nov 2026 02:09:08 +0100 Subject: [PATCH] Fix objects printing in the wrong place when identical objects share sliced layers (#17183) `is_print_object_the_same` now also compares `center_offset()`. Two objects with the same transform can have different offsets, because the offset comes from each object's first instance. Before, the object that took over the other's layers printed shifted. --- src/libslic3r/Print.cpp | 4 +++ tests/fff_print/test_print.cpp | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index b9b5daad2f..92704cee7c 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -3449,6 +3449,10 @@ void Print::process(long long *time_cost_with_cache, bool use_cache) auto is_print_object_the_same = [](const PrintObject* object1, const PrintObject* object2) -> bool{ if (object1->trafo().matrix() != object2->trafo().matrix()) return false; + // Layers are stored relative to center_offset(), which comes from the first instance of each ModelObject, so it + // can differ between objects with the same trafo. Sharing the layers would then print the object in the wrong place. + if (object1->center_offset() != object2->center_offset()) + return false; const ModelObject* model_obj1 = object1->model_object(); const ModelObject* model_obj2 = object2->model_object(); if (model_obj1->volumes.size() != model_obj2->volumes.size()) diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index ab2a9d802c..23c0ccc973 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -327,6 +327,55 @@ TEST_CASE("Objects share slices only when their height ranges are the same", "[P } } +TEST_CASE("Objects whose center offsets differ do not share slices", "[Print][Regression]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "layer_height", 0.2 }, + { "initial_layer_print_height", 0.2 }, + }); + + // A triangular prism: the center of its bounding box does not turn with the prism unless it is turned by + // a multiple of 90 degrees. + const std::vector vertices{{0, 0, 0}, {30, 0, 0}, {0, 10, 0}, {0, 0, 10}, {30, 0, 10}, {0, 10, 10}}; + const std::vector faces{{0, 2, 1}, {3, 4, 5}, {0, 1, 4}, {0, 4, 3}, {1, 2, 5}, {1, 5, 4}, {2, 0, 3}, {2, 3, 5}}; + + Print print; + Model model; + init_print({TriangleMesh(vertices, faces)}, print, model, config); + ModelObject *object_a = model.objects.front(); + // add_object() gives the copy extruder 1, and objects only share slices when their configs match. + object_a->config.set_key_value("extruder", new ConfigOptionInt(1)); + const Vec3d origin = object_a->instances.front()->get_offset(); + ModelObject *object_b = model.add_object(*object_a); // shares object_a's mesh + + // The first instance of object_b is turned, the second one is as straight as object_a's. + ModelInstance *turned = object_b->instances.front(); + turned->set_offset(origin + Vec3d(60., 0., 0.)); + turned->set_rotation(Vec3d(0., 0., PI / 3.)); + ModelInstance *straight = object_b->add_instance(); + straight->set_offset(origin + Vec3d(120., 0., 0.)); + + print.apply(model, config); + print.process(); + + const PrintObject *print_a = nullptr; + const PrintObject *print_straight = nullptr; + for (const PrintObject *object : print.objects()) + if (object->model_object()->id() == object_a->id()) + print_a = object; + REQUIRE(print_a != nullptr); + for (const PrintObject *object : print.objects()) + if (object->model_object()->id() == object_b->id() && object->trafo().matrix() == print_a->trafo().matrix()) + print_straight = object; + REQUIRE(print_straight != nullptr); + + // What makes the objects differ, otherwise the checks below would not test anything. + REQUIRE(print_a->center_offset() != print_straight->center_offset()); + CHECK(print_a->get_shared_object() == nullptr); + CHECK(print_straight->get_shared_object() == nullptr); +} + // --------------------------------------------------------------------------- // Print::validate() warning collection //