mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-28 08:09:55 -05:00
* Cut: keep per-part filament colors after dovetail cut The dovetail path merged every model part of each half into one volume built from a raw mesh, dropping per-part extruder and material overrides, so the cut result fell back to the object-level extruder (T1). Merge by effective slicing identity (extruder, material, per-part config) instead, one merged volume per group, copying the representative's config and material. Connectors and modifiers are never merged or deleted, and single-group results keep today's single volume named after the object. Fixes #17187 * Fix missing includes * Cut: number extra parts and fix null material lookups Name the extra merged parts Object_2, Object_3, ... as splitting a mesh into parts does, instead of Object_T<extruder>, which misleads when the parts differ by material or by a per-part setting, and whose fallback suffix was not re-checked for uniqueness. Move the merged mesh into the new volume instead of copying it. add_cut_volume() dereferenced the source volume's material, but the cut model has no material table, so cutting a part that has a material id crashed before the merge could carry that id. Fall back to the material id alone, and let PrintObject skip a material's region settings when the material is missing rather than dereferencing null. * Cut: don't replace the source material while copying it add_cut_volume() carried the source volume's material with set_material(), and Model::add_material(id, other) deletes the existing material before copying from other. The source volume and the target object share the cut model, so the second copy of the same volume (the "_A" and "_B" halves of a groove cut) resolved that same material and replaced it with a copy of itself after deleting it, reading freed memory. A part with a material id segfaulted the cut before the merge could carry that id. Copy the material's settings only when the target model has no material of that id yet; otherwise just set the id and keep the existing one. * Cut: carry a part's material id and merge by effective settings The cut model holds no material settings, so add_cut_volume() only needs the id: copying the material there could only replace the source object's own entry, which Model::add_material() deletes before copying from it. Drop the unused duplicate of add_cut_volume() in Model.cpp as well. Group parts by the settings they slice with rather than by the keys they set explicitly: a part that restates a value the object already sets now stays with a part that leaves it implicit, instead of coming back as Object_2. * Model: keep a part's material when converting units ModelObject::convert_units() rebuilt a clone detached from its model and then called set_material() on it: the source volume's material was read without a check, and set_material() reached add_material() through the clone's null model. Any part with a material id crashed the conversion. Copy the id, pointing the clone at the source model for the call so the entry is recorded and its settings resolve after the conversion. set_material() has no callers left, so remove it. * Model tests: free the converted objects through a model ModelObject's destructor is private, so the tests cannot delete the objects convert_units() hands back. Give them to a Model, which owns model objects and frees them.
865 lines
37 KiB
C++
865 lines
37 KiB
C++
|
||
#include "CutUtils.hpp"
|
||
#include "Config.hpp"
|
||
#include "Geometry.hpp"
|
||
#include "Point.hpp"
|
||
#include "TriangleMesh.hpp"
|
||
#include "libslic3r.h"
|
||
#include "Model.hpp"
|
||
#include "TriangleMeshSlicer.hpp"
|
||
#include "TriangleSelector.hpp"
|
||
#include "ObjectID.hpp"
|
||
|
||
#include <algorithm>
|
||
#include <boost/log/trivial.hpp>
|
||
#include <cassert>
|
||
#include <string>
|
||
#include <vector>
|
||
#include <utility>
|
||
#include <cstddef>
|
||
#include <optional>
|
||
#include <math.h>
|
||
#include <cmath>
|
||
#include "BoundingBox.hpp"
|
||
|
||
namespace Slic3r {
|
||
|
||
using namespace Geometry;
|
||
|
||
static void apply_tolerance(ModelVolume* vol)
|
||
{
|
||
ModelVolume::CutInfo& cut_info = vol->cut_info;
|
||
|
||
assert(cut_info.is_connector);
|
||
if (!cut_info.is_processed)
|
||
return;
|
||
|
||
// The connector mesh is a unit shape, so the scaling factor is its size.
|
||
Vec3d sf = vol->get_scaling_factor();
|
||
|
||
// make a "hole" wider
|
||
sf[X] += double(cut_info.radius_tolerance);
|
||
sf[Y] += double(cut_info.radius_tolerance);
|
||
|
||
// make a "hole" deeper
|
||
sf[Z] += double(cut_info.height_tolerance);
|
||
|
||
// A dowel is centered on the cut plane and stays there, so each of its holes gets half the extra depth.
|
||
// A plug or snap sits on one side of the plane: its hole grows by the tolerance away from the cut face
|
||
// and by the overlap through it, so its center moves by half the difference.
|
||
if (cut_info.connector_type == CutConnectorType::Plug ||
|
||
cut_info.connector_type == CutConnectorType::Snap) {
|
||
sf[Z] += CONNECTOR_HOLE_OVERLAP;
|
||
const double z_offset = 0.5 * (static_cast<double>(cut_info.height_tolerance) - CONNECTOR_HOLE_OVERLAP);
|
||
vol->set_offset(vol->get_offset() + rotation_transform(vol->get_rotation()) * (z_offset * Vec3d::UnitZ()));
|
||
}
|
||
|
||
vol->set_scaling_factor(sf);
|
||
}
|
||
|
||
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());
|
||
// 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;
|
||
}
|
||
|
||
// Cuts the volume at cut_z above the cut plane; the result stays in the cut plane's coordinates.
|
||
static void process_volume_cut( const ModelVolume* volume, const Transform3d& instance_matrix, const Transform3d& cut_matrix,
|
||
ModelObjectCutAttributes attributes, TriangleMesh& upper_mesh, TriangleMesh& lower_mesh,
|
||
float cut_z = 0.f)
|
||
{
|
||
const auto volume_matrix = volume->get_matrix();
|
||
|
||
const Transformation cut_transformation = Transformation(cut_matrix);
|
||
const Transform3d invert_cut_matrix = cut_transformation.get_rotation_matrix().inverse() * translation_transform(-1 * cut_transformation.get_offset());
|
||
|
||
// Transform the mesh by the combined transformation matrix.
|
||
// Flip the triangles in case the composite transformation is left handed.
|
||
TriangleMesh mesh(volume->mesh());
|
||
mesh.transform(invert_cut_matrix * instance_matrix * volume_matrix, true);
|
||
|
||
indexed_triangle_set upper_its, lower_its;
|
||
cut_mesh(mesh.its, cut_z, &upper_its, &lower_its);
|
||
if (attributes.has(ModelObjectCutAttribute::KeepUpper))
|
||
upper_mesh = TriangleMesh(upper_its);
|
||
if (attributes.has(ModelObjectCutAttribute::KeepLower))
|
||
lower_mesh = TriangleMesh(lower_its);
|
||
}
|
||
|
||
static void process_connector_cut( ModelVolume* volume, const Transform3d& instance_matrix, const Transform3d& cut_matrix,
|
||
ModelObjectCutAttributes attributes, ModelObject* upper, ModelObject* lower,
|
||
std::vector<ModelObject*>& dowels)
|
||
{
|
||
assert(volume->cut_info.is_connector);
|
||
volume->cut_info.set_processed();
|
||
|
||
const auto volume_matrix = volume->get_matrix();
|
||
|
||
// ! Don't apply instance transformation for the conntectors.
|
||
// This transformation is already there
|
||
if (volume->cut_info.connector_type != CutConnectorType::Dowel) {
|
||
if (attributes.has(ModelObjectCutAttribute::KeepUpper)) {
|
||
ModelVolume* vol = nullptr;
|
||
if (volume->cut_info.connector_type == CutConnectorType::Snap) {
|
||
TriangleMesh mesh = TriangleMesh(its_make_cylinder(1.0, 1.0, PI / 180.));
|
||
|
||
vol = upper->add_volume(std::move(mesh));
|
||
vol->set_transformation(volume->get_transformation());
|
||
vol->set_type(ModelVolumeType::NEGATIVE_VOLUME);
|
||
|
||
vol->cut_info = volume->cut_info;
|
||
vol->name = volume->name;
|
||
}
|
||
else
|
||
vol = upper->add_volume(*volume);
|
||
|
||
vol->set_transformation(volume_matrix);
|
||
apply_tolerance(vol);
|
||
}
|
||
if (attributes.has(ModelObjectCutAttribute::KeepLower)) {
|
||
ModelVolume* vol = lower->add_volume(*volume);
|
||
vol->set_transformation(volume_matrix);
|
||
// for lower part change type of connector from NEGATIVE_VOLUME to MODEL_PART if this connector is a plug
|
||
vol->set_type(ModelVolumeType::MODEL_PART);
|
||
}
|
||
}
|
||
else {
|
||
if (attributes.has(ModelObjectCutAttribute::CreateDowels)) {
|
||
ModelObject* dowel{ nullptr };
|
||
// Clone the object to duplicate instances, materials etc.
|
||
volume->get_object()->clone_for_cut(&dowel);
|
||
|
||
// add one more solid part same as connector if this connector is a dowel
|
||
ModelVolume* vol = dowel->add_volume(*volume);
|
||
vol->set_type(ModelVolumeType::MODEL_PART);
|
||
|
||
// But discard rotation and Z-offset for this volume
|
||
vol->set_rotation(Vec3d::Zero());
|
||
vol->set_offset(Z, 0.0);
|
||
|
||
dowels.push_back(dowel);
|
||
}
|
||
|
||
// Cut the dowel
|
||
apply_tolerance(volume);
|
||
|
||
// Cut each hole just past the plane, so it breaks through its cut face without getting shorter.
|
||
const float overlap = static_cast<float>(CONNECTOR_HOLE_OVERLAP);
|
||
TriangleMesh upper_mesh, lower_mesh, discarded;
|
||
process_volume_cut(volume, Transform3d::Identity(), cut_matrix, attributes, upper_mesh, discarded, -overlap);
|
||
process_volume_cut(volume, Transform3d::Identity(), cut_matrix, attributes, discarded, lower_mesh, overlap);
|
||
|
||
// Add cut parts to the related objects
|
||
add_cut_volume(upper_mesh, upper, volume, cut_matrix, "_A", volume->type());
|
||
add_cut_volume(lower_mesh, lower, volume, cut_matrix, "_B", volume->type());
|
||
}
|
||
}
|
||
|
||
static void process_modifier_cut(ModelVolume* volume, const Transform3d& instance_matrix, const Transform3d& inverse_cut_matrix,
|
||
ModelObjectCutAttributes attributes, ModelObject* upper, ModelObject* lower)
|
||
{
|
||
const auto volume_matrix = instance_matrix * volume->get_matrix();
|
||
|
||
// Modifiers are not cut, but we still need to add the instance transformation
|
||
// to the modifier volume transformation to preserve their shape properly.
|
||
volume->set_transformation(Transformation(volume_matrix));
|
||
|
||
if (attributes.has(ModelObjectCutAttribute::KeepAsParts)) {
|
||
upper->add_volume(*volume);
|
||
return;
|
||
}
|
||
|
||
// Some logic for the negative volumes/connectors. Add only needed modifiers
|
||
auto bb = volume->mesh().transformed_bounding_box(inverse_cut_matrix * volume_matrix);
|
||
bool is_crossed_by_cut = bb.min[Z] <= 0 && bb.max[Z] >= 0;
|
||
if (attributes.has(ModelObjectCutAttribute::KeepUpper) && (bb.min[Z] >= 0 || is_crossed_by_cut))
|
||
upper->add_volume(*volume);
|
||
if (attributes.has(ModelObjectCutAttribute::KeepLower) && (bb.max[Z] <= 0 || is_crossed_by_cut))
|
||
lower->add_volume(*volume);
|
||
}
|
||
|
||
static void process_solid_part_cut(const ModelVolume* volume, const Transform3d& instance_matrix, const Transform3d& cut_matrix,
|
||
ModelObjectCutAttributes attributes, ModelObject* upper, ModelObject* lower)
|
||
{
|
||
// Perform cut
|
||
TriangleMesh upper_mesh, lower_mesh;
|
||
process_volume_cut(volume, instance_matrix, cut_matrix, attributes, upper_mesh, lower_mesh);
|
||
|
||
// Add required cut parts to the objects
|
||
|
||
if (attributes.has(ModelObjectCutAttribute::KeepAsParts)) {
|
||
add_cut_volume(upper_mesh, upper, volume, cut_matrix, "_A");
|
||
if (!lower_mesh.empty()) {
|
||
add_cut_volume(lower_mesh, upper, volume, cut_matrix, "_B");
|
||
upper->volumes.back()->cut_info.is_from_upper = false;
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (attributes.has(ModelObjectCutAttribute::KeepUpper))
|
||
add_cut_volume(upper_mesh, upper, volume, cut_matrix);
|
||
|
||
if (attributes.has(ModelObjectCutAttribute::KeepLower) && !lower_mesh.empty())
|
||
add_cut_volume(lower_mesh, lower, volume, cut_matrix);
|
||
}
|
||
|
||
static void reset_instance_transformation(ModelObject* object, size_t src_instance_idx,
|
||
const Transform3d& cut_matrix = Transform3d::Identity(),
|
||
bool place_on_cut = false, bool flip = false)
|
||
{
|
||
// Reset instance transformation except offset and Z-rotation
|
||
|
||
for (size_t i = 0; i < object->instances.size(); ++i) {
|
||
auto& obj_instance = object->instances[i];
|
||
const double rot_z = obj_instance->get_rotation().z();
|
||
|
||
Transformation inst_trafo = Transformation(obj_instance->get_transformation().get_matrix_no_scaling_factor());
|
||
// add respect to mirroring
|
||
if (obj_instance->is_left_handed())
|
||
inst_trafo = inst_trafo * Transformation(scale_transform(Vec3d(-1, 1, 1)));
|
||
|
||
obj_instance->set_transformation(inst_trafo);
|
||
|
||
Vec3d rotation = Vec3d::Zero();
|
||
if (!flip && !place_on_cut) {
|
||
if ( i != src_instance_idx)
|
||
rotation[Z] = rot_z;
|
||
}
|
||
else {
|
||
Transform3d rotation_matrix = Transform3d::Identity();
|
||
if (flip)
|
||
rotation_matrix = rotation_transform(PI * Vec3d::UnitX());
|
||
|
||
if (place_on_cut)
|
||
rotation_matrix = rotation_matrix * Transformation(cut_matrix).get_rotation_matrix().inverse();
|
||
|
||
if (i != src_instance_idx)
|
||
rotation_matrix = rotation_transform(rot_z * Vec3d::UnitZ()) * rotation_matrix;
|
||
|
||
rotation = Transformation(rotation_matrix).get_rotation();
|
||
}
|
||
|
||
obj_instance->set_rotation(rotation);
|
||
}
|
||
}
|
||
|
||
|
||
Cut::Cut(const ModelObject* object, int instance, const Transform3d& cut_matrix,
|
||
ModelObjectCutAttributes attributes/*= ModelObjectCutAttribute::KeepUpper | ModelObjectCutAttribute::KeepLower | ModelObjectCutAttribute::KeepAsParts*/)
|
||
: m_instance(instance), m_cut_matrix(cut_matrix), m_attributes(attributes)
|
||
{
|
||
m_model = Model();
|
||
if (object)
|
||
m_model.add_object(*object);
|
||
}
|
||
|
||
void Cut::post_process(ModelObject* object, ModelObjectPtrs& cut_object_ptrs, bool keep, bool place_on_cut, bool flip)
|
||
{
|
||
if (!object) return;
|
||
|
||
if (keep && !object->volumes.empty()) {
|
||
reset_instance_transformation(object, m_instance, m_cut_matrix, place_on_cut, flip);
|
||
cut_object_ptrs.push_back(object);
|
||
}
|
||
else
|
||
m_model.objects.push_back(object); // will be deleted in m_model.clear_objects();
|
||
}
|
||
|
||
void Cut::post_process(ModelObject* upper, ModelObject* lower, ModelObjectPtrs& cut_object_ptrs)
|
||
{
|
||
post_process(upper, cut_object_ptrs,
|
||
m_attributes.has(ModelObjectCutAttribute::KeepUpper),
|
||
m_attributes.has(ModelObjectCutAttribute::PlaceOnCutUpper),
|
||
m_attributes.has(ModelObjectCutAttribute::FlipUpper));
|
||
|
||
post_process(lower, cut_object_ptrs,
|
||
m_attributes.has(ModelObjectCutAttribute::KeepLower),
|
||
m_attributes.has(ModelObjectCutAttribute::PlaceOnCutLower),
|
||
m_attributes.has(ModelObjectCutAttribute::PlaceOnCutLower) || m_attributes.has(ModelObjectCutAttribute::FlipLower));
|
||
}
|
||
|
||
|
||
void Cut::finalize(const ModelObjectPtrs& objects, const std::vector<std::optional<TriangleSelector::SavedPainting>>& saved_paintings)
|
||
{
|
||
// Paint volumes
|
||
for (const auto& saved_painting : saved_paintings) {
|
||
if (saved_painting) {
|
||
for (const auto object : objects) {
|
||
for (const auto volume : object->volumes) {
|
||
if (volume->is_model_part() && !volume->is_cut_connector()) {
|
||
volume->restore_painting(saved_painting, true);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//clear model from temporary objects
|
||
m_model.clear_objects();
|
||
|
||
// add to model result objects
|
||
m_model.objects = objects;
|
||
}
|
||
|
||
|
||
const ModelObjectPtrs& Cut::perform_with_plane()
|
||
{
|
||
if (!m_attributes.has(ModelObjectCutAttribute::KeepUpper) && !m_attributes.has(ModelObjectCutAttribute::KeepLower)) {
|
||
m_model.clear_objects();
|
||
return m_model.objects;
|
||
}
|
||
|
||
ModelObject* mo = m_model.objects.front();
|
||
|
||
BOOST_LOG_TRIVIAL(trace) << "ModelObject::cut - start";
|
||
|
||
// Clone the object to duplicate instances, materials etc.
|
||
ModelObject* upper{ nullptr };
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepUpper))
|
||
mo->clone_for_cut(&upper);
|
||
|
||
ModelObject* lower{ nullptr };
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepLower) && !m_attributes.has(ModelObjectCutAttribute::KeepAsParts))
|
||
mo->clone_for_cut(&lower);
|
||
|
||
std::vector<ModelObject*> dowels;
|
||
|
||
// Because transformations are going to be applied to meshes directly,
|
||
// we reset transformation of all instances and volumes,
|
||
// except for translation and Z-rotation on instances, which are preserved
|
||
// in the transformation matrix and not applied to the mesh transform.
|
||
|
||
const auto instance_matrix = mo->instances[m_instance]->get_transformation().get_matrix_no_offset();
|
||
const Transformation cut_transformation = Transformation(m_cut_matrix);
|
||
const Transform3d inverse_cut_matrix = cut_transformation.get_rotation_matrix().inverse() * translation_transform(-1. * cut_transformation.get_offset());
|
||
|
||
std::vector<std::optional<TriangleSelector::SavedPainting>> saved_paintings;
|
||
for (ModelVolume* volume : mo->volumes) {
|
||
// Save painting data before reset_extra_facets() discards it.
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepPaint)) {
|
||
saved_paintings.emplace_back(volume->save_painting());
|
||
if (saved_paintings.back()) {
|
||
// Transform mesh to cut space (same transform as process_volume_cut applies)
|
||
saved_paintings.back()->mesh.transform(instance_matrix * volume->get_matrix(), true);
|
||
}
|
||
}
|
||
|
||
volume->reset_extra_facets();
|
||
|
||
if (!volume->is_model_part()) {
|
||
if (volume->cut_info.is_processed)
|
||
process_modifier_cut(volume, instance_matrix, inverse_cut_matrix, m_attributes, upper, lower);
|
||
else
|
||
process_connector_cut(volume, instance_matrix, m_cut_matrix, m_attributes, upper, lower, dowels);
|
||
}
|
||
else if (!volume->mesh().empty())
|
||
process_solid_part_cut(volume, instance_matrix, m_cut_matrix, m_attributes, upper, lower);
|
||
}
|
||
|
||
// Post-process cut parts
|
||
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepAsParts) && upper->volumes.empty()) {
|
||
m_model = Model();
|
||
m_model.objects.push_back(upper);
|
||
return m_model.objects;
|
||
}
|
||
|
||
ModelObjectPtrs cut_object_ptrs;
|
||
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepAsParts) && !upper->volumes.empty()) {
|
||
reset_instance_transformation(upper, m_instance, m_cut_matrix);
|
||
cut_object_ptrs.push_back(upper);
|
||
}
|
||
else {
|
||
// Delete all modifiers which are not intersecting with solid parts bounding box
|
||
auto delete_extra_modifiers = [this](ModelObject* mo) {
|
||
if (!mo) return;
|
||
const BoundingBoxf3 obj_bb = mo->instance_bounding_box(m_instance);
|
||
const Transform3d inst_matrix = mo->instances[m_instance]->get_transformation().get_matrix();
|
||
|
||
for (int i = int(mo->volumes.size()) - 1; i >= 0; --i)
|
||
if (const ModelVolume* vol = mo->volumes[i];
|
||
!vol->is_model_part() && !vol->is_cut_connector()) {
|
||
auto bb = vol->mesh().transformed_bounding_box(inst_matrix * vol->get_matrix());
|
||
if (!obj_bb.intersects(bb))
|
||
mo->delete_volume(i);
|
||
}
|
||
};
|
||
|
||
post_process(upper, lower, cut_object_ptrs);
|
||
delete_extra_modifiers(upper);
|
||
delete_extra_modifiers(lower);
|
||
|
||
if (m_attributes.has(ModelObjectCutAttribute::CreateDowels) && !dowels.empty()) {
|
||
for (auto dowel : dowels) {
|
||
reset_instance_transformation(dowel, m_instance);
|
||
dowel->name += "-Dowel-" + dowel->volumes[0]->name;
|
||
cut_object_ptrs.push_back(dowel);
|
||
}
|
||
}
|
||
}
|
||
|
||
finalize(cut_object_ptrs, saved_paintings);
|
||
|
||
BOOST_LOG_TRIVIAL(trace) << "ModelObject::cut - end";
|
||
|
||
return m_model.objects;
|
||
}
|
||
|
||
static void distribute_modifiers_from_object(ModelObject* from_obj, const int instance_idx, ModelObject* to_obj1, ModelObject* to_obj2)
|
||
{
|
||
auto obj1_bb = to_obj1 ? to_obj1->instance_bounding_box(instance_idx) : BoundingBoxf3();
|
||
auto obj2_bb = to_obj2 ? to_obj2->instance_bounding_box(instance_idx) : BoundingBoxf3();
|
||
const Transform3d inst_matrix = from_obj->instances[instance_idx]->get_transformation().get_matrix();
|
||
|
||
for (ModelVolume* vol : from_obj->volumes)
|
||
if (!vol->is_model_part()) {
|
||
// Don't add modifiers which are processed connectors
|
||
if (vol->cut_info.is_connector && !vol->cut_info.is_processed)
|
||
continue;
|
||
auto bb = vol->mesh().transformed_bounding_box(inst_matrix * vol->get_matrix());
|
||
// Don't add modifiers which are not intersecting with solid parts
|
||
if (obj1_bb.intersects(bb))
|
||
to_obj1->add_volume(*vol);
|
||
if (obj2_bb.intersects(bb))
|
||
to_obj2->add_volume(*vol);
|
||
}
|
||
}
|
||
|
||
// 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) {
|
||
// Merge all SolidPart but not Connectors, keeping parts with a different slicing identity apart.
|
||
std::vector<ModelVolume*> originals;
|
||
std::vector<Group> 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);
|
||
}
|
||
|
||
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;
|
||
}
|
||
// 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);
|
||
}
|
||
}
|
||
|
||
|
||
const ModelObjectPtrs& Cut::perform_by_contour(const ModelObject* src_object, std::vector<Part> parts, int dowels_count)
|
||
{
|
||
ModelObject* cut_mo = m_model.objects.front();
|
||
|
||
// Clone the object to duplicate instances, materials etc.
|
||
ModelObject* upper{ nullptr };
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepUpper)) cut_mo->clone_for_cut(&upper);
|
||
ModelObject* lower{ nullptr };
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepLower)) cut_mo->clone_for_cut(&lower);
|
||
|
||
if (upper && lower) {
|
||
upper->name = upper->name + "_A";
|
||
lower->name = lower->name + "_B";
|
||
}
|
||
|
||
// Save painting data so we later can remap it.
|
||
std::vector<std::optional<TriangleSelector::SavedPainting>> saved_paintings;
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepPaint)) {
|
||
const auto instance_matrix = src_object->instances[m_instance]->get_transformation().get_matrix_no_offset();
|
||
for (const auto volume : src_object->volumes) {
|
||
saved_paintings.emplace_back(volume->save_painting());
|
||
if (saved_paintings.back()) {
|
||
// Transform mesh to cut space (same transform as process_volume_cut applies)
|
||
saved_paintings.back()->mesh.transform(instance_matrix * volume->get_matrix(), true);
|
||
}
|
||
}
|
||
}
|
||
|
||
const size_t cut_parts_cnt = parts.size();
|
||
bool has_modifiers = false;
|
||
|
||
// Distribute SolidParts to the Upper/Lower object
|
||
for (size_t id = 0; id < cut_parts_cnt; ++id) {
|
||
if (parts[id].is_modifier)
|
||
has_modifiers = true; // modifiers will be added later to the related parts
|
||
else if (ModelObject* obj = (parts[id].selected ? upper : lower))
|
||
obj->add_volume(*(cut_mo->volumes[id]));
|
||
}
|
||
|
||
if (has_modifiers) {
|
||
// Distribute Modifiers to the Upper/Lower object
|
||
distribute_modifiers_from_object(cut_mo, m_instance, upper, lower);
|
||
}
|
||
|
||
ModelObjectPtrs cut_object_ptrs;
|
||
|
||
ModelVolumePtrs& volumes = cut_mo->volumes;
|
||
if (volumes.size() == cut_parts_cnt) {
|
||
// Means that object is cut without connectors
|
||
|
||
// Just add Upper and Lower objects to cut_object_ptrs
|
||
post_process(upper, lower, cut_object_ptrs);
|
||
|
||
// Now merge all model parts together:
|
||
merge_solid_parts_inside_object(cut_object_ptrs);
|
||
|
||
// replace initial objects in model with cut object
|
||
finalize(cut_object_ptrs, saved_paintings);
|
||
}
|
||
else if (volumes.size() > cut_parts_cnt) {
|
||
// Means that object is cut with connectors
|
||
|
||
// All volumes are distributed to Upper / Lower object,
|
||
// So we don’t need them anymore
|
||
for (size_t id = 0; id < cut_parts_cnt; id++)
|
||
delete* (volumes.begin() + id);
|
||
volumes.erase(volumes.begin(), volumes.begin() + cut_parts_cnt);
|
||
|
||
// Perform cut just to get connectors
|
||
Cut cut(cut_mo, m_instance, m_cut_matrix, m_attributes);
|
||
const ModelObjectPtrs& cut_connectors_obj = cut.perform_with_plane();
|
||
assert(dowels_count > 0 ? cut_connectors_obj.size() >= 3 : cut_connectors_obj.size() == 2);
|
||
|
||
// Connectors from upper object
|
||
for (const ModelVolume* volume : cut_connectors_obj[0]->volumes)
|
||
upper->add_volume(*volume, volume->type());
|
||
|
||
// Connectors from lower object
|
||
for (const ModelVolume* volume : cut_connectors_obj[1]->volumes)
|
||
lower->add_volume(*volume, volume->type());
|
||
|
||
// Add Upper and Lower objects to cut_object_ptrs
|
||
post_process(upper, lower, cut_object_ptrs);
|
||
|
||
// Now merge all model parts together:
|
||
merge_solid_parts_inside_object(cut_object_ptrs);
|
||
|
||
// replace initial objects in model with cut object
|
||
finalize(cut_object_ptrs, saved_paintings);
|
||
|
||
// Add Dowel-connectors as separate objects to model
|
||
if (cut_connectors_obj.size() >= 3)
|
||
for (size_t id = 2; id < cut_connectors_obj.size(); id++)
|
||
m_model.add_object(*cut_connectors_obj[id]);
|
||
}
|
||
|
||
return m_model.objects;
|
||
}
|
||
|
||
|
||
const ModelObjectPtrs& Cut::perform_with_groove(const Groove& groove,
|
||
const Transform3d& rotation_m,
|
||
const int groove_count,
|
||
const float groove_gap,
|
||
const float m_radius,
|
||
bool keep_as_parts /* = false*/)
|
||
{
|
||
ModelObject* cut_mo = m_model.objects.front();
|
||
|
||
// Clone the object to duplicate instances, materials etc.
|
||
ModelObject* upper{ nullptr };
|
||
cut_mo->clone_for_cut(&upper);
|
||
ModelObject* lower{ nullptr };
|
||
cut_mo->clone_for_cut(&lower);
|
||
|
||
if (upper && lower) {
|
||
upper->name = upper->name + "_A";
|
||
lower->name = lower->name + "_B";
|
||
}
|
||
|
||
// Save painting data so we later can remap it.
|
||
std::vector<std::optional<TriangleSelector::SavedPainting>> saved_paintings;
|
||
if (m_attributes.has(ModelObjectCutAttribute::KeepPaint)) {
|
||
const auto instance_matrix = cut_mo->instances[m_instance]->get_transformation().get_matrix_no_offset();
|
||
for (const auto volume : cut_mo->volumes) {
|
||
saved_paintings.emplace_back(volume->save_painting());
|
||
if (saved_paintings.back()) {
|
||
// Transform mesh to cut space (same transform as process_volume_cut applies)
|
||
saved_paintings.back()->mesh.transform(instance_matrix * volume->get_matrix(), true);
|
||
}
|
||
}
|
||
}
|
||
|
||
const double groove_half_depth = 0.5 * double(groove.depth);
|
||
|
||
Model tmp_model_for_cut = Model();
|
||
|
||
Model tmp_model = Model();
|
||
tmp_model.add_object(*cut_mo);
|
||
ModelObject* tmp_object = tmp_model.objects.front();
|
||
|
||
auto add_volumes_from_cut = [](ModelObject* object, const ModelObjectCutAttribute attribute, const Model& tmp_model_for_cut) {
|
||
const auto& volumes = tmp_model_for_cut.objects.front()->volumes;
|
||
for (const ModelVolume* volume : volumes)
|
||
if (volume->is_model_part()) {
|
||
if ((attribute == ModelObjectCutAttribute::KeepUpper && volume->is_from_upper()) ||
|
||
(attribute != ModelObjectCutAttribute::KeepUpper && !volume->is_from_upper())) {
|
||
ModelVolume* new_vol = object->add_volume(*volume);
|
||
new_vol->reset_from_upper();
|
||
}
|
||
}
|
||
};
|
||
|
||
auto cut = [this, add_volumes_from_cut]
|
||
(ModelObject* object, const Transform3d& cut_matrix, const ModelObjectCutAttribute add_volumes_attribute, Model& tmp_model_for_cut) {
|
||
Cut cut(object, m_instance, cut_matrix);
|
||
|
||
tmp_model_for_cut = Model();
|
||
tmp_model_for_cut.add_object(*cut.perform_with_plane().front());
|
||
assert(!tmp_model_for_cut.objects.empty());
|
||
|
||
object->clear_volumes();
|
||
add_volumes_from_cut(object, add_volumes_attribute, tmp_model_for_cut);
|
||
reset_instance_transformation(object, m_instance);
|
||
};
|
||
|
||
// cut by upper plane (+Z)
|
||
{
|
||
const Transform3d cut_matrix_upper = translation_transform(rotation_m * (groove_half_depth * Vec3d::UnitZ())) * m_cut_matrix;
|
||
|
||
cut(tmp_object, cut_matrix_upper, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
add_volumes_from_cut(upper, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
}
|
||
|
||
// cut by lower plane (-Z)
|
||
{
|
||
const Transform3d cut_matrix_lower = translation_transform(rotation_m * (-groove_half_depth * Vec3d::UnitZ())) * m_cut_matrix;
|
||
|
||
cut(tmp_object, cut_matrix_lower, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
add_volumes_from_cut(lower, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
}
|
||
|
||
// Compute same slot outer width used in preview plane
|
||
const float groove_width = calculate_groove_width(groove, m_radius);
|
||
|
||
ModelObject* groove_object{nullptr};
|
||
|
||
// multiple cuts
|
||
for (int i = 0; i < groove_count; i++) {
|
||
bool is_first_groove = i == 0;
|
||
bool is_last_groove = i == groove_count - 1;
|
||
|
||
// Calculate the x-axis offset for this dovetail
|
||
float groove_offset_factor_start = -.5 * ((groove_count - 1));
|
||
float groove_offset_factor = groove_offset_factor_start + i;
|
||
|
||
float offset_x = groove_offset_factor * (groove_gap + groove_width);
|
||
|
||
|
||
tmp_object->clone_for_cut(&groove_object);
|
||
for (ModelVolume* volume : tmp_object->volumes) {
|
||
ModelVolume* new_vol = groove_object->add_volume(*volume);
|
||
new_vol->reset_from_upper();
|
||
}
|
||
|
||
// isolate area of current groove
|
||
if (!is_first_groove) {
|
||
float left_cut_position = (-groove_gap / 2.f) - (groove_width / 2.f) + offset_x;
|
||
|
||
const Transform3d cut_matrix_left = translation_transform(rotation_m * (left_cut_position * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, M_PI / 2.0, 0));
|
||
|
||
cut(groove_object, cut_matrix_left, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
}
|
||
if (!is_last_groove) {
|
||
float right_cut_position = (groove_gap / 2.f) + (groove_width / 2.f) + offset_x;
|
||
|
||
const Transform3d cut_matrix_right = translation_transform(rotation_m * (right_cut_position * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, M_PI / 2.0, 0));
|
||
cut(groove_object, cut_matrix_right, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
}
|
||
|
||
const Transform3d groove_translation = translation_transform(rotation_m * (offset_x * Vec3d::UnitX()));
|
||
// cut middle part with 2 angles and add parts to related upper/lower objects
|
||
const double h_side_shift = 0.5 * double(groove.width + groove.depth / tan(groove.flaps_angle));
|
||
|
||
// cut by angle1 plane
|
||
{
|
||
const Transform3d cut_matrix_angle1 = groove_translation * translation_transform(rotation_m * (-h_side_shift * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, -groove.flaps_angle, -groove.angle));
|
||
|
||
cut(groove_object, cut_matrix_angle1, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
add_volumes_from_cut(lower, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
}
|
||
|
||
// cut by angle2 plane
|
||
{
|
||
const Transform3d cut_matrix_angle2 = groove_translation * translation_transform(rotation_m * (h_side_shift * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, groove.flaps_angle, groove.angle));
|
||
|
||
cut(groove_object, cut_matrix_angle2, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
add_volumes_from_cut(lower, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
}
|
||
|
||
// apply tolerance to the middle part
|
||
{
|
||
const double h_groove_shift_tolerance = groove_half_depth - (double)groove.depth_tolerance;
|
||
|
||
const Transform3d cut_matrix_lower_tolerance = groove_translation * translation_transform(rotation_m * (-h_groove_shift_tolerance * Vec3d::UnitZ())) *
|
||
m_cut_matrix;
|
||
cut(groove_object, cut_matrix_lower_tolerance, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
|
||
const double h_side_shift_tolerance = h_side_shift - 0.5 * double(groove.width_tolerance);
|
||
|
||
const Transform3d cut_matrix_angle1_tolerance = groove_translation * translation_transform(rotation_m * (-h_side_shift_tolerance * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, -groove.flaps_angle, -groove.angle));
|
||
cut(groove_object, cut_matrix_angle1_tolerance, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
|
||
const Transform3d cut_matrix_angle2_tolerance = groove_translation * translation_transform(rotation_m * (h_side_shift_tolerance * Vec3d::UnitX())) *
|
||
m_cut_matrix * rotation_transform(Vec3d(0, groove.flaps_angle, groove.angle));
|
||
cut(groove_object, cut_matrix_angle2_tolerance, ModelObjectCutAttribute::KeepUpper, tmp_model_for_cut);
|
||
}
|
||
|
||
add_volumes_from_cut(upper, ModelObjectCutAttribute::KeepLower, tmp_model_for_cut);
|
||
|
||
groove_object->clear_volumes();
|
||
}
|
||
|
||
ModelObjectPtrs cut_object_ptrs;
|
||
|
||
if (keep_as_parts) {
|
||
// add volumes from lower object to the upper, but mark them as a lower
|
||
const auto& volumes = lower->volumes;
|
||
for (const ModelVolume* volume : volumes) {
|
||
ModelVolume* new_vol = upper->add_volume(*volume);
|
||
new_vol->cut_info.is_from_upper = false;
|
||
}
|
||
|
||
// add modifiers
|
||
for (const ModelVolume* volume : cut_mo->volumes)
|
||
if (!volume->is_model_part())
|
||
upper->add_volume(*volume);
|
||
|
||
cut_object_ptrs.push_back(upper);
|
||
|
||
// add lower object to the cut_object_ptrs just to correct delete it from the Model destructor and avoid memory leaks
|
||
cut_object_ptrs.push_back(lower);
|
||
}
|
||
else {
|
||
// add modifiers if object has any
|
||
for (const ModelVolume* volume : cut_mo->volumes)
|
||
if (!volume->is_model_part()) {
|
||
distribute_modifiers_from_object(cut_mo, m_instance, upper, lower);
|
||
break;
|
||
}
|
||
|
||
assert(!upper->volumes.empty() && !lower->volumes.empty());
|
||
|
||
// Add Upper and Lower parts to cut_object_ptrs
|
||
|
||
post_process(upper, lower, cut_object_ptrs);
|
||
|
||
// Now merge all model parts together:
|
||
merge_solid_parts_inside_object(cut_object_ptrs);
|
||
}
|
||
|
||
finalize(cut_object_ptrs, saved_paintings);
|
||
|
||
return m_model.objects;
|
||
}
|
||
|
||
float Cut::calculate_groove_width (const Cut::Groove& groove, const float m_radius)
|
||
{
|
||
// Compute same slot outer width used in preview plane
|
||
const double flap_width = is_approx(groove.flaps_angle, 0.f) ? groove.depth : groove.depth / sin(groove.flaps_angle);
|
||
const double total_flap_width = 2.0 * flap_width * cos(groove.flaps_angle);
|
||
const double slot_neck_half_width = 0.5f * (groove.width);
|
||
const double slot_mouth_half_width = 0.5 * (groove.width + total_flap_width);
|
||
const double plane_half_height = 0.5f* (1.5f * (1.5f *m_radius));
|
||
const double flap_taper_offset = plane_half_height * tan(groove.angle);
|
||
const double slot_outer_x_max = std::max(slot_mouth_half_width + flap_taper_offset, slot_neck_half_width + flap_taper_offset);
|
||
|
||
return float(2.0 * slot_outer_x_max);
|
||
}
|
||
|
||
} // namespace Slic3r
|
||
|