Tech ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT - fixed export/import to/from 3mf file of mirrored volumes

This commit is contained in:
enricoturri1966 2021-08-20 11:24:48 +02:00
parent 7e0e552fd2
commit 79e8e8627f
2 changed files with 29 additions and 0 deletions

View File

@ -1839,11 +1839,17 @@ namespace Slic3r {
Transform3d volume_matrix_to_object = Transform3d::Identity(); Transform3d volume_matrix_to_object = Transform3d::Identity();
bool has_transform = false; bool has_transform = false;
#if ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
bool is_left_handed = false;
#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
// extract the volume transformation from the volume's metadata, if present // extract the volume transformation from the volume's metadata, if present
for (const Metadata& metadata : volume_data.metadata) { for (const Metadata& metadata : volume_data.metadata) {
if (metadata.key == MATRIX_KEY) { if (metadata.key == MATRIX_KEY) {
volume_matrix_to_object = Slic3r::Geometry::transform3d_from_string(metadata.value); volume_matrix_to_object = Slic3r::Geometry::transform3d_from_string(metadata.value);
has_transform = ! volume_matrix_to_object.isApprox(Transform3d::Identity(), 1e-10); has_transform = ! volume_matrix_to_object.isApprox(Transform3d::Identity(), 1e-10);
#if ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
is_left_handed = Slic3r::Geometry::Transformation(volume_matrix_to_object).is_left_handed();
#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
break; break;
} }
} }
@ -1875,6 +1881,13 @@ namespace Slic3r {
stl_get_size(&stl); stl_get_size(&stl);
triangle_mesh.repair(); triangle_mesh.repair();
#if ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
// PrusaSlicer older than 2.4.0 saved mirrored volumes with reversed winding of the triangles
// This caused the call to TriangleMesh::repair() to reverse all the facets because the calculated volume was negative
if (is_left_handed && stl.stats.facets_reversed > 0 && stl.stats.facets_reversed == stl.stats.original_num_facets)
stl.stats.facets_reversed = 0;
#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
if (m_version == 0) { if (m_version == 0) {
// if the 3mf was not produced by PrusaSlicer and there is only one instance, // if the 3mf was not produced by PrusaSlicer and there is only one instance,
// bake the transformation into the geometry to allow the reload from disk command // bake the transformation into the geometry to allow the reload from disk command
@ -2499,6 +2512,10 @@ namespace Slic3r {
if (volume == nullptr) if (volume == nullptr)
continue; continue;
#if ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
bool is_left_handed = volume->is_left_handed();
#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
VolumeToOffsetsMap::iterator volume_it = volumes_offsets.find(volume); VolumeToOffsetsMap::iterator volume_it = volumes_offsets.find(volume);
assert(volume_it != volumes_offsets.end()); assert(volume_it != volumes_offsets.end());
@ -2513,6 +2530,15 @@ namespace Slic3r {
{ {
const Vec3i &idx = its.indices[i]; const Vec3i &idx = its.indices[i];
char *ptr = buf; char *ptr = buf;
#if ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
boost::spirit::karma::generate(ptr, boost::spirit::lit(" <") << TRIANGLE_TAG <<
" v1=\"" << boost::spirit::int_ <<
"\" v2=\"" << boost::spirit::int_ <<
"\" v3=\"" << boost::spirit::int_ << "\"",
idx[is_left_handed ? 2 : 0] + volume_it->second.first_vertex_id,
idx[1] + volume_it->second.first_vertex_id,
idx[is_left_handed ? 0 : 2] + volume_it->second.first_vertex_id);
#else
boost::spirit::karma::generate(ptr, boost::spirit::lit(" <") << TRIANGLE_TAG << boost::spirit::karma::generate(ptr, boost::spirit::lit(" <") << TRIANGLE_TAG <<
" v1=\"" << boost::spirit::int_ << " v1=\"" << boost::spirit::int_ <<
"\" v2=\"" << boost::spirit::int_ << "\" v2=\"" << boost::spirit::int_ <<
@ -2520,6 +2546,7 @@ namespace Slic3r {
idx[0] + volume_it->second.first_vertex_id, idx[0] + volume_it->second.first_vertex_id,
idx[1] + volume_it->second.first_vertex_id, idx[1] + volume_it->second.first_vertex_id,
idx[2] + volume_it->second.first_vertex_id); idx[2] + volume_it->second.first_vertex_id);
#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT
*ptr = '\0'; *ptr = '\0';
output_buffer += buf; output_buffer += buf;
} }

View File

@ -47,6 +47,8 @@
#define ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER (1 && ENABLE_2_4_0_ALPHA0) #define ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER (1 && ENABLE_2_4_0_ALPHA0)
// Enable drawing contours, at cut level, for sinking volumes // Enable drawing contours, at cut level, for sinking volumes
#define ENABLE_SINKING_CONTOURS (1 && ENABLE_2_4_0_ALPHA0) #define ENABLE_SINKING_CONTOURS (1 && ENABLE_2_4_0_ALPHA0)
// Enable the fix for exporting and importing to/from 3mf file of mirrored volumes
#define ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT (1 && ENABLE_2_4_0_ALPHA0)
#endif // _prusaslicer_technologies_h_ #endif // _prusaslicer_technologies_h_