From 79e8e8627f403fc442d831b8a087528b7951c427 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Fri, 20 Aug 2021 11:24:48 +0200 Subject: [PATCH] Tech ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT - fixed export/import to/from 3mf file of mirrored volumes --- src/libslic3r/Format/3mf.cpp | 27 +++++++++++++++++++++++++++ src/libslic3r/Technologies.hpp | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index 2a76f218f..febeacdbe 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -1839,11 +1839,17 @@ namespace Slic3r { Transform3d volume_matrix_to_object = Transform3d::Identity(); 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 for (const Metadata& metadata : volume_data.metadata) { if (metadata.key == MATRIX_KEY) { volume_matrix_to_object = Slic3r::Geometry::transform3d_from_string(metadata.value); 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; } } @@ -1875,6 +1881,13 @@ namespace Slic3r { stl_get_size(&stl); 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 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 @@ -2499,6 +2512,10 @@ namespace Slic3r { if (volume == nullptr) 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); assert(volume_it != volumes_offsets.end()); @@ -2513,6 +2530,15 @@ namespace Slic3r { { const Vec3i &idx = its.indices[i]; 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 << " v1=\"" << boost::spirit::int_ << "\" v2=\"" << boost::spirit::int_ << @@ -2520,6 +2546,7 @@ namespace Slic3r { idx[0] + volume_it->second.first_vertex_id, idx[1] + volume_it->second.first_vertex_id, idx[2] + volume_it->second.first_vertex_id); +#endif // ENABLE_FIX_MIRRORED_VOLUMES_3MF_IMPORT_EXPORT *ptr = '\0'; output_buffer += buf; } diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index f6366c18e..d337fdc2d 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -47,6 +47,8 @@ #define ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER (1 && ENABLE_2_4_0_ALPHA0) // Enable drawing contours, at cut level, for sinking volumes #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_