diff --git a/src/libslic3r/CutUtils.cpp b/src/libslic3r/CutUtils.cpp index 71833e4a45..3796696cdb 100644 --- a/src/libslic3r/CutUtils.cpp +++ b/src/libslic3r/CutUtils.cpp @@ -1,5 +1,6 @@ #include "CutUtils.hpp" +#include "Config.hpp" #include "Geometry.hpp" #include "Point.hpp" #include "TriangleMesh.hpp" @@ -70,7 +71,10 @@ static void add_cut_volume(TriangleMesh& mesh, ModelObject* object, const ModelV vol->config.assign_config(src_volume->config); assert(vol->config.id().valid()); assert(vol->config.id() != src_volume->config.id()); - vol->set_material(src_volume->material_id(), *src_volume->material()); + // The id is all that needs carrying: the cut model carries no material settings, and the id resolves + // against the material of the model the result is added to. Copying the material here would replace + // the source object's own entry, which Model::add_material deletes before copying from it. + vol->set_material_id(src_volume->material_id()); vol->cut_info = src_volume->cut_info; } @@ -437,30 +441,96 @@ static void distribute_modifiers_from_object(ModelObject* from_obj, const int in } } +// The identity that decides how a solid part is sliced and colored: filament, material and the +// per-part process overrides. A part inherits the object's config, so a value is read from the volume +// when it sets it and from the object otherwise: restating the object's value does not split the part. +static std::string part_group_key(const ModelVolume& mv) +{ + // The material id and the serialized values are length-prefixed, so a value containing ';' or '=' + // cannot make two different parts share a key. + std::string key = std::to_string(std::max(1, mv.extruder_id())) + "|"; + key += std::to_string(mv.material_id().size()) + ":" + mv.material_id() + "|"; + const ModelObject& object = *mv.get_object(); + t_config_option_keys opt_keys = object.config.keys(); + for (const std::string& opt_key : mv.config.keys()) + if (!object.config.has(opt_key)) + opt_keys.push_back(opt_key); + std::sort(opt_keys.begin(), opt_keys.end()); + for (const std::string& opt_key : opt_keys) { + if (opt_key == "extruder") + continue; + const std::string value = mv.config.has(opt_key) ? mv.config.opt_serialize(opt_key) : object.config.opt_serialize(opt_key); + key += opt_key + "=" + std::to_string(value.size()) + ":" + value + ";"; + } + return key; +} + static void merge_solid_parts_inside_object(ModelObjectPtrs& objects) { + struct Group + { + std::string key; + TriangleMesh mesh; + const ModelVolume* representative = nullptr; + }; + for (ModelObject* mo : objects) { - TriangleMesh mesh; - // Merge all SolidPart but not Connectors - for (const ModelVolume* mv : mo->volumes) { - if (mv->is_model_part() && !mv->is_cut_connector()) { - TriangleMesh m = mv->mesh(); - m.transform(mv->get_matrix()); - mesh.merge(m); + // Merge all SolidPart but not Connectors, keeping parts with a different slicing identity apart. + std::vector originals; + std::vector groups; + for (ModelVolume* mv : mo->volumes) { + if (!mv->is_model_part() || mv->is_cut_connector()) + continue; + originals.push_back(mv); + const std::string key = part_group_key(*mv); + auto group = std::find_if(groups.begin(), groups.end(), [&key](const Group& g) { return g.key == key; }); + if (group == groups.end()) { + groups.push_back({key, TriangleMesh(), mv}); + group = groups.end() - 1; } + TriangleMesh m = mv->mesh(); + m.transform(mv->get_matrix()); + group->mesh.merge(m); } - if (!mesh.empty()) { - ModelVolume* new_volume = mo->add_volume(mesh); - new_volume->name = mo->name; - // Delete all merged SolidPart but not Connectors - for (int i = int(mo->volumes.size()) - 2; i >= 0; --i) { - const ModelVolume* mv = mo->volumes[i]; - if (mv->is_model_part() && !mv->is_cut_connector()) - mo->delete_volume(i); + + bool created = false; + for (Group& group : groups) { + if (group.mesh.empty()) + continue; + + ModelVolume* new_volume = mo->add_volume(std::move(group.mesh)); + if (!created) { + new_volume->name = mo->name; + } else { + // Number the extra parts as splitting a mesh into parts does; the first part keeps the object's name. + const auto name_taken = [mo](const std::string& n) { + return std::any_of(mo->volumes.begin(), mo->volumes.end(), [&n](const ModelVolume* v) { return v->name == n; }); + }; + std::string name; + int suffix = 2; + do + name = mo->name + "_" + std::to_string(suffix++); + while (name_taken(name)); + new_volume->name = name; } - // Ensuring that volumes start with solid parts for proper slicing - mo->sort_volumes(true); + // Don't copy the config's ID. + new_volume->config.assign_config(group.representative->config); + // Only the material id is carried; the cut model has no material table for its settings. + new_volume->set_material_id(group.representative->material_id()); + created = true; } + + if (!created) + continue; + + // Delete the merged SolidPart but not Connectors, matched by pointer. + for (size_t i = mo->volumes.size(); i > 0; --i) { + const ModelVolume* mv = mo->volumes[i - 1]; + if (std::find(originals.begin(), originals.end(), mv) != originals.end()) + mo->delete_volume(i - 1); + } + // Ensuring that volumes start with solid parts for proper slicing + mo->sort_volumes(true); } } diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 7c6e14638f..ba03c9bc71 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1309,24 +1309,6 @@ bool Model::is_fuzzy_skin_painted() const return std::any_of(this->objects.cbegin(), this->objects.cend(), [](const ModelObject *mo) { return mo->is_fuzzy_skin_painted(); }); } -static void add_cut_volume(TriangleMesh& mesh, ModelObject* object, const ModelVolume* src_volume, const Transform3d& cut_matrix, const std::string& suffix = {}, ModelVolumeType type = ModelVolumeType::MODEL_PART) -{ - if (mesh.empty()) - return; - - mesh.transform(cut_matrix); - ModelVolume* vol = object->add_volume(mesh); - vol->set_type(type); - - vol->name = src_volume->name + suffix; - // Don't copy the config's ID. - vol->config.assign_config(src_volume->config); - assert(vol->config.id().valid()); - assert(vol->config.id() != src_volume->config.id()); - vol->set_material(src_volume->material_id(), *src_volume->material()); - vol->cut_info = src_volume->cut_info; -} - ModelObject::~ModelObject() { this->clear_volumes(); @@ -2125,7 +2107,11 @@ void ModelObject::convert_units(ModelObjectPtrs& new_objects, ConversionType con vol->config.assign_config(volume->config); assert(vol->config.id().valid()); assert(vol->config.id() != volume->config.id()); - vol->set_material(volume->material_id(), *volume->material()); + // The clone is detached while it is rebuilt; point it at the source model so set_material_id() + // can record the entry, and detach it again. + new_object->set_model(this->get_model()); + vol->set_material_id(volume->material_id()); + new_object->set_model(nullptr); vol->source.input_file = volume->source.input_file; vol->source.object_idx = (int)new_objects.size(); vol->source.volume_idx = vol_idx; @@ -2778,13 +2764,6 @@ ModelMaterial* ModelVolume::material() const return this->object->get_model()->get_material(m_material_id); } -void ModelVolume::set_material(t_model_material_id material_id, const ModelMaterial &material) -{ - m_material_id = material_id; - if (! material_id.empty()) - this->object->get_model()->add_material(material_id, material); -} - // Extract the current extruder ID based on this ModelVolume's config and the parent ModelObject's config. int ModelVolume::extruder_id() const { diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 5f7220cd93..c59ec6db75 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -1053,7 +1053,6 @@ public: void set_material_id(t_model_material_id material_id); void reset_extra_facets(); ModelMaterial* material() const; - void set_material(t_model_material_id material_id, const ModelMaterial &material); // Extract the current extruder ID based on this ModelVolume's config and the parent ModelObject's config. // Extruder ID is only valid for FFF. Returns -1 for SLA or if the extruder ID is not applicable (support volumes). int extruder_id() const; diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 40bdb6a2d2..1265b0856b 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -4433,8 +4433,8 @@ PrintRegionConfig region_config_from_model_volume(const PrintRegionConfig &defau // default_or_parent_region_config contains parent PrintRegion config, which already contains ModelVolume's config. } apply_to_print_region_config(config, volume.config.get(), feature_overrides, variant_index); - if (! volume.material_id().empty()) - apply_to_print_region_config(config, volume.material()->config.get(), feature_overrides, variant_index); + if (const ModelMaterial* material = volume.material(); material != nullptr) + apply_to_print_region_config(config, material->config.get(), feature_overrides, variant_index); if (layer_range_config != nullptr) { // Not applicable to modifiers. assert(volume.is_model_part()); diff --git a/tests/libslic3r/CMakeLists.txt b/tests/libslic3r/CMakeLists.txt index 2570e7d9ce..6c878026e8 100644 --- a/tests/libslic3r/CMakeLists.txt +++ b/tests/libslic3r/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(${_TEST_NAME}_tests test_clipper_offset.cpp test_clipper_utils.cpp test_cut_connectors.cpp + test_cut.cpp test_config.cpp test_config_variant_expansion.cpp test_locales_utils.cpp diff --git a/tests/libslic3r/test_cut.cpp b/tests/libslic3r/test_cut.cpp new file mode 100644 index 0000000000..19f7a5b382 --- /dev/null +++ b/tests/libslic3r/test_cut.cpp @@ -0,0 +1,337 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include "libslic3r/Config.hpp" +#include "libslic3r/CutUtils.hpp" +#include "libslic3r/Model.hpp" +#include "libslic3r/Point.hpp" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/TriangleMesh.hpp" +#include "libslic3r/TriangleSelector.hpp" + +using namespace Slic3r; + +namespace { + +// A 25 mm cube split into two 12.5 mm wide halves that butt together on the plane x = 0, +// so a groove cut at the origin crosses both of them. +struct TwoPartBox +{ + ModelObject* object = nullptr; + ModelVolume* part_a = nullptr; + ModelVolume* part_b = nullptr; +}; + +ModelVolume* add_half(ModelObject* object, float min_x) +{ + TriangleMesh cube = make_cube(12.5, 25., 25.); + cube.translate(min_x + 6.25f, -12.5f, -12.5f); + return object->add_volume(std::move(cube)); +} + +TwoPartBox add_two_part_box(Model& model) +{ + TwoPartBox box; + box.object = model.add_object(); + box.part_a = add_half(box.object, -12.5f); + box.part_b = add_half(box.object, 0.f); + box.object->add_instance(); + return box; +} + +ModelObject* add_single_part_box(Model& model) +{ + ModelObject* object = model.add_object(); + TriangleMesh cube = make_cube(25., 25., 25.); + cube.translate(-12.5f, -12.5f, -12.5f); + object->add_volume(std::move(cube)); + object->add_instance(); + return object; +} + +std::vector model_parts(const ModelObject& object) +{ + std::vector parts; + for (const ModelVolume* volume : object.volumes) + if (volume->is_model_part() && !volume->is_cut_connector()) + parts.push_back(volume); + return parts; +} + +// Runs the dovetail cut the cut gizmo runs, with the groove at the origin. +const ModelObjectPtrs& run_dovetail(Cut& cut) +{ + Cut::Groove groove; + groove.depth = 3.f; + groove.width = 2.f; + groove.flaps_angle = 0.5235988f; // 30 degrees + groove.angle = 0.0872665f; // 5 degrees + groove.depth_tolerance = 0.1f; + groove.width_tolerance = 0.1f; + return cut.perform_with_groove(groove, Transform3d::Identity(), 1, 0.f, 0.f); +} + +// Painting is remapped by surface overlap, so facets touching the joint would paint the neighbour as +// well: paint the outer face of the part, away from x = 0. +void paint_away_from_joint(ModelVolume* volume) +{ + const indexed_triangle_set& its = volume->mesh().its; + float min_x = std::numeric_limits::max(); + for (const Vec3f& vertex : its.vertices) + min_x = std::min(min_x, vertex.x()); + + const auto on_outer_face = [min_x](const Vec3f& vertex) { return vertex.x() < min_x + 0.01f; }; + + TriangleSelector selector(volume->mesh()); + int painted = 0; + for (size_t i = 0; i < its.indices.size(); ++i) { + const auto& facet = its.indices[i]; + if (on_outer_face(its.vertices[facet(0)]) && on_outer_face(its.vertices[facet(1)]) && on_outer_face(its.vertices[facet(2)])) { + selector.set_facet(int(i), EnforcerBlockerType::Extruder2); + ++painted; + } + } + REQUIRE(painted > 0); + REQUIRE(volume->mmu_segmentation_facets.set(selector)); +} + +} // namespace + +TEST_CASE("A dovetail cut keeps each part's filament", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.part_b->config.set("extruder", 2); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + std::vector filaments; + std::vector names; + for (const ModelVolume* part : model_parts(*object)) { + filaments.push_back(part->extruder_id()); + names.push_back(part->name); + } + std::sort(filaments.begin(), filaments.end()); + std::sort(names.begin(), names.end()); + REQUIRE(filaments == std::vector{ 1, 2 }); + REQUIRE(names == std::vector{ object->name, object->name + "_2" }); + } +} + +TEST_CASE("A dovetail cut of a single-part object still leaves one part", "[Cut]") +{ + Model model; + ModelObject* object = add_single_part_box(model); + + Cut cut(object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* result : objects) { + const std::vector parts = model_parts(*result); + REQUIRE(parts.size() == 1); + REQUIRE(parts.front()->name == result->name); + } +} + +TEST_CASE("A part that restates the object's filament merges with a part that leaves it implicit", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.object->config.set("extruder", 1); + box.part_b->config.set("extruder", 1); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) + REQUIRE(model_parts(*object).size() == 1); +} + +TEST_CASE("A part that restates the object's infill merges with a part that leaves it implicit", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.object->config.set_key_value("sparse_infill_density", new ConfigOptionPercent(50.)); + box.part_b->config.set_key_value("sparse_infill_density", new ConfigOptionPercent(50.)); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) + REQUIRE(model_parts(*object).size() == 1); +} + +TEST_CASE("A dovetail cut keeps the filament of a single-part object", "[Cut]") +{ + Model model; + ModelObject* object = add_single_part_box(model); + object->volumes.front()->config.set("extruder", 2); + + Cut cut(object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* result : objects) { + const std::vector parts = model_parts(*result); + REQUIRE(parts.size() == 1); + REQUIRE(parts.front()->extruder_id() == 2); + } +} + +TEST_CASE("A dovetail cut keeps modifier volumes", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.part_b->config.set("extruder", 2); + + TriangleMesh cube = make_cube(5., 5., 5.); + cube.translate(-2.5f, -2.5f, -2.5f); + ModelVolume* modifier = box.object->add_volume(std::move(cube)); + modifier->set_type(ModelVolumeType::PARAMETER_MODIFIER); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + std::vector filaments; + size_t others = 0; + for (const ModelVolume* volume : object->volumes) { + if (volume->is_model_part() && !volume->is_cut_connector()) + filaments.push_back(volume->extruder_id()); + else + ++others; + } + std::sort(filaments.begin(), filaments.end()); + REQUIRE(filaments == std::vector{ 1, 2 }); + REQUIRE(others == 1); + } +} + +TEST_CASE("A dovetail cut keeps per-part process settings", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.part_b->config.set_key_value("sparse_infill_density", new ConfigOptionPercent(50.)); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + const std::vector parts = model_parts(*object); + REQUIRE(parts.size() == 2); + std::vector densities; + for (const ModelVolume* part : parts) + if (part->config.has("sparse_infill_density")) + densities.push_back(part->config.opt_serialize("sparse_infill_density")); + REQUIRE(densities.size() == 1); + REQUIRE(densities.front() == "50%"); + } +} + +TEST_CASE("Paint stays on its own part after a dovetail cut", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + paint_away_from_joint(box.part_a); + box.part_b->config.set("extruder", 2); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower | ModelObjectCutAttribute::KeepPaint); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + const std::vector parts = model_parts(*object); + REQUIRE(parts.size() == 2); + std::vector painted_filaments; + for (const ModelVolume* part : parts) + if (part->mmu_segmentation_facets.has_facets(*part, EnforcerBlockerType::Extruder2)) + painted_filaments.push_back(part->extruder_id()); + REQUIRE(painted_filaments.size() == 1); + REQUIRE(painted_filaments.front() == 1); + } +} + +TEST_CASE("A dovetail cut keeps parts with different materials apart", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.part_a->set_material_id("mat_a"); + box.part_b->set_material_id("mat_b"); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + std::vector materials; + for (const ModelVolume* part : model_parts(*object)) { + REQUIRE(part->extruder_id() == 1); + materials.push_back(part->material_id()); + } + std::sort(materials.begin(), materials.end()); + REQUIRE(materials == std::vector{ "mat_a", "mat_b" }); + } +} + +TEST_CASE("A dovetail cut inherits the object's filament", "[Cut]") +{ + Model model; + const TwoPartBox box = add_two_part_box(model); + box.object->config.set("extruder", 3); + + Cut cut(box.object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = run_dovetail(cut); + + REQUIRE(objects.size() == 2); + for (const ModelObject* object : objects) { + const std::vector parts = model_parts(*object); + REQUIRE(parts.size() == 1); + REQUIRE(parts.front()->extruder_id() == 3); + } +} + +TEST_CASE("Cut by contour keeps each part's filament", "[Cut]") +{ + Model model; + ModelObject* object = model.add_object(); + add_half(object, -12.5f); + ModelVolume* right = add_half(object, 0.f); + right->config.set("extruder", 2); + object->add_instance(); + + Cut cut(object, 0, Transform3d::Identity(), + ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower); + const ModelObjectPtrs& objects = + cut.perform_by_contour(object, { { true, false }, { true, false } }, 0); + + REQUIRE(objects.size() == 1); + std::vector filaments; + for (const ModelVolume* part : model_parts(*objects.front())) + filaments.push_back(part->extruder_id()); + std::sort(filaments.begin(), filaments.end()); + REQUIRE(filaments == std::vector{ 1, 2 }); +} diff --git a/tests/libslic3r/test_model.cpp b/tests/libslic3r/test_model.cpp index 88a96ee3b8..0c79bd83ed 100644 --- a/tests/libslic3r/test_model.cpp +++ b/tests/libslic3r/test_model.cpp @@ -507,3 +507,42 @@ TEST_CASE("Unplaced objects scaled to millimetres still merge on a common base", bases.push_back(volume->mesh().transformed_bounding_box(volume->get_matrix()).min.z()); CHECK_THAT(bases.front(), Catch::Matchers::WithinAbs(bases.back(), 1e-3)); } + +TEST_CASE("Converting a part's units keeps its material", "[Model]") +{ + Model model; + ModelObject *object = model.add_object(); + ModelVolume *volume = object->add_volume(make_cube(10, 10, 10), ModelVolumeType::MODEL_PART, false); + volume->set_material_id("mat_a"); + + ModelObjectPtrs converted; + object->convert_units(converted, ConversionType::CONV_FROM_INCH, {}); + + REQUIRE(converted.size() == 1); + REQUIRE(converted.front()->volumes.size() == 1); + CHECK(converted.front()->volumes.front()->material_id() == "mat_a"); + + // convert_units() hands its objects to the caller unowned; give them to a model so they are freed. + Model owner; + owner.objects = std::move(converted); +} + +TEST_CASE("Converting a part's units keeps an id whose material is missing", "[Model]") +{ + Model model; + ModelObject *object = model.add_object(); + ModelVolume *volume = object->add_volume(make_cube(10, 10, 10), ModelVolumeType::MODEL_PART, false); + volume->set_material_id("mat_a"); + model.delete_material("mat_a"); + + ModelObjectPtrs converted; + object->convert_units(converted, ConversionType::CONV_FROM_INCH, {}); + + REQUIRE(converted.size() == 1); + REQUIRE(converted.front()->volumes.size() == 1); + CHECK(converted.front()->volumes.front()->material_id() == "mat_a"); + + // convert_units() hands its objects to the caller unowned; give them to a model so they are freed. + Model owner; + owner.objects = std::move(converted); +}