From a620d804efbe74162c96ad29d755cdab2b823530 Mon Sep 17 00:00:00 2001 From: Lam Wei Lun Date: Thu, 5 Nov 2026 21:40:41 +0800 Subject: [PATCH] By layer: keep As object list copies in their list order (#17232) By layer: order As object list copies by their list position sort_object_instances_by_model_order() keyed every instance on its object's index when it initialized the order, so all copies of an object tied. print.objects() does not list the copies in list order either: copies with a different rotation live in separate PrintObjects, ordered by transformation, so the ties were broken by that grouping and the copies printed as 2, 4, 6, 1, 3, 5. std::sort reordering the ties made it worse from 17 instances up. Number the instances by their position in the model's object and instance lists instead. No order key repeats, so a plain sort is enough; the renumber after it compacts this print's instances to 1..n. The other intra-layer orders and the by-object path are unaffected. The new test rotates every second copy, which fails on every platform before this change, MSVC included. --- src/libslic3r/GCode.cpp | 35 +++++++++++---------------- tests/fff_print/test_print.cpp | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 25052308c1..bc6b91ab84 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -3293,32 +3293,30 @@ static inline std::vector sort_object_instances_by_max_z(c } #endif -// Produce a vector of PrintObjects in the order of their respective ModelObjects in print.model(). +// Produce a vector of PrintInstances in the order of their ModelInstances in the object and instance lists of print.model(). //BBS: add sort logic for seq-print std::vector sort_object_instances_by_model_order(const Print& print, bool init_order) { - 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; - }; + if (init_order) { + // Number by position in the model's object and instance lists. Copies of one object can be split + // into separate PrintObjects, one per distinct transformation, and print.objects() orders those by + // transformation, so its order is not the list order. + int order = 0; + for (const ModelObject *model_object : print.model().objects) + for (ModelInstance *model_instance : model_object->instances) + model_instance->arrange_order = ++order; + } // Build up map from ModelInstance* to PrintInstance* std::vector> model_instance_to_print_instance; model_instance_to_print_instance.reserve(print.num_object_instances()); for (const PrintObject *print_object : print.objects()) for (const PrintInstance &print_instance : print_object->instances()) - { - if (init_order) - const_cast(print_instance.model_instance)->arrange_order = find_object_index(print.model(), print_object->model_object()); model_instance_to_print_instance.emplace_back(print_instance.model_instance, &print_instance); - } std::sort(model_instance_to_print_instance.begin(), model_instance_to_print_instance.end(), [](auto &l, auto &r) { return l.first->arrange_order < r.first->arrange_order; }); if (init_order) { - // Re-assign the arrange_order so each instance has a unique order number + // Re-assign the arrange_order so each instance of this print has a unique order number. The + // numbering above covers the whole model, including the instances of the other plates. for (int k = 0; k < model_instance_to_print_instance.size(); k++) { const_cast(model_instance_to_print_instance[k].first)->arrange_order = k + 1; } @@ -3326,13 +3324,8 @@ std::vector sort_object_instances_by_model_order(const Pri std::vector instances; instances.reserve(model_instance_to_print_instance.size()); - for (const ModelObject *model_object : print.model().objects) - for (const ModelInstance *model_instance : model_object->instances) { - auto it = std::lower_bound(model_instance_to_print_instance.begin(), model_instance_to_print_instance.end(), std::make_pair(model_instance, nullptr), [](auto &l, auto &r) { return l.first->arrange_order < r.first->arrange_order; }); - if (it != model_instance_to_print_instance.end() && it->first == model_instance) - instances.emplace_back(it->second); - } - std::sort(instances.begin(), instances.end(), [](auto& l, auto& r) { return l->model_instance->arrange_order < r->model_instance->arrange_order; }); + for (const auto &model_instance_print_instance : model_instance_to_print_instance) + instances.emplace_back(model_instance_print_instance.second); return instances; } diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index ddbab9d204..2cc95e9ba5 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -41,6 +41,7 @@ #include #include "libslic3r/Polygon.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/GCode.hpp" #include "libslic3r/GCodeReader.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/Exception.hpp" @@ -1566,6 +1567,49 @@ TEST_CASE("Sequential printing applies an instance's rotation to the part above CHECK(error.find("too tall") != std::string::npos); } +TEST_CASE("By-layer As object list keeps copies of one object in their list order", "[Print]") +{ + // validate() numbers the instances through sort_object_instances_by_model_order(print, true). Copies + // of one object used to all share one order key, and print.objects() does not list them in list order: + // every second copy is rotated here, which splits them into separate PrintObjects. The G-code export + // and the sequence labels print in the order those numbers assign. + std::vector meshes; + meshes.push_back(make_cube(5, 5, 2)); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "print_sequence", "by layer" }, + { "print_order", "as_obj_list" }, + { "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()); + + // validate() numbered the instances it ordered; 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 As object list path of 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]); +} + // A sequential (by-object) print must publish the print-level nozzle group result just // like a by-layer print, so custom g-code can index the per-nozzle placeholder tables // (e.g. nozzle_diameter_at_nozzle_id[]) instead of failing on an empty vector.