By object: keep rotated copies of an object in their list order (#17286)

* By object: order rotated copies of an object by their list position

Copies of one object that differ in rotation get one PrintObject each, and
print.objects() lists those groups by transformation. sequential_print_
clearance_valid() keyed the instances by their object's index, which all
copies share, so std::stable_sort kept the transformation grouping and every
second copy on a rotated plate printed out of place.

Key each instance by its position in the model's object and instance lists
instead. No key repeats, so the sort no longer depends on being stable, and
arrange_order is then the list order the G-code export and the sequence
labels already print in.

Fixes #17260.

* Keep the sequential printed-copy tests in step with the list order

The new by-object test rotated a cube centred on its corner, which moved the
footprint by half the cube and closed the 12 mm grid gap under the 2 mm
clearance distance, so validation rejected the plate. Centre the mesh on the
object origin so the rotation turns the footprint in place.

Sequential clearance warned the copy that print.objects() listed first, which
for a plate of rotated copies was the rotated one. Copies now print in object
list order, so print validation warns the first copy in the list; expect that
copy from the preview as well, and drop the now incorrect header comment.
This commit is contained in:
Lam Wei Lun authored and GitHub committed 2026-11-06 21:30:38 +08:00
1 parent 2ec1a0732f
commit 719fdcf4dd
4 files changed
+83 -23

No files matched your search

+29 -17
View File
@@ -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<int, int> 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<size_t> 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<size_t> 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]");
}