From c5b22163c59cc3be3e5a4026eb3b9d6d47a51f39 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Fri, 30 Jun 2023 10:35:40 +0200 Subject: [PATCH] Fix crash when exporting to stl --- src/libslic3r/CSGMesh/CSGMesh.hpp | 33 ++++++++++++++++++++++++++++++- src/libslic3r/SLAPrintSteps.cpp | 29 --------------------------- src/slic3r/GUI/Plater.cpp | 7 +++++-- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/libslic3r/CSGMesh/CSGMesh.hpp b/src/libslic3r/CSGMesh/CSGMesh.hpp index d14ed76595..a9a1811186 100644 --- a/src/libslic3r/CSGMesh/CSGMesh.hpp +++ b/src/libslic3r/CSGMesh/CSGMesh.hpp @@ -1,8 +1,10 @@ #ifndef CSGMESH_HPP #define CSGMESH_HPP +#include "libslic3r/Point.hpp" + #include -#include +#include namespace Slic3r { namespace csg { @@ -81,6 +83,35 @@ struct CSGPart { {} }; +template bool is_all_positive(const Cont &csgmesh) +{ + bool is_all_pos = + std::all_of(csgmesh.begin(), + csgmesh.end(), + [](auto &part) { + return csg::get_operation(part) == csg::CSGType::Union; + }); + + return is_all_pos; +} + +template +indexed_triangle_set csgmesh_merge_positive_parts(const Cont &csgmesh) +{ + indexed_triangle_set m; + for (auto &csgpart : csgmesh) { + auto op = csg::get_operation(csgpart); + const indexed_triangle_set * pmesh = csg::get_mesh(csgpart); + if (pmesh && op == csg::CSGType::Union) { + indexed_triangle_set mcpy = *pmesh; + its_transform(mcpy, csg::get_transform(csgpart), true); + its_merge(m, mcpy); + } + } + + return m; +} + }} // namespace Slic3r::csg #endif // CSGMESH_HPP diff --git a/src/libslic3r/SLAPrintSteps.cpp b/src/libslic3r/SLAPrintSteps.cpp index 33e2a6e20f..412bd1b422 100644 --- a/src/libslic3r/SLAPrintSteps.cpp +++ b/src/libslic3r/SLAPrintSteps.cpp @@ -127,35 +127,6 @@ void SLAPrint::Steps::apply_printer_corrections(SLAPrintObject &po, SliceOrigin } } -template bool is_all_positive(const Cont &csgmesh) -{ - bool is_all_pos = - std::all_of(csgmesh.begin(), - csgmesh.end(), - [](auto &part) { - return csg::get_operation(part) == csg::CSGType::Union; - }); - - return is_all_pos; -} - -template -static indexed_triangle_set csgmesh_merge_positive_parts(const Cont &csgmesh) -{ - indexed_triangle_set m; - for (auto &csgpart : csgmesh) { - auto op = csg::get_operation(csgpart); - const indexed_triangle_set * pmesh = csg::get_mesh(csgpart); - if (pmesh && op == csg::CSGType::Union) { - indexed_triangle_set mcpy = *pmesh; - its_transform(mcpy, csg::get_transform(csgpart), true); - its_merge(m, mcpy); - } - } - - return m; -} - indexed_triangle_set SLAPrint::Steps::generate_preview_vdb( SLAPrintObject &po, SLAPrintObjectStep step) { diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index fcd97cdc15..902eeb6ded 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -6471,9 +6471,12 @@ void Plater::export_stl_obj(bool extended, bool selection_only) csg::model_to_csgmesh(mo, Transform3d::Identity(), std::back_inserter(csgmesh), csg::mpartsPositive | csg::mpartsNegative | csg::mpartsDoSplits); - if (csg::check_csgmesh_booleans(range(csgmesh)) == csgmesh.end()) { + auto csgrange = range(csgmesh); + if (csg::is_all_positive(csgrange)) { + mesh = TriangleMesh{csg::csgmesh_merge_positive_parts(csgrange)}; + } else if (csg::check_csgmesh_booleans(csgrange) == csgrange.end()) { try { - auto cgalm = csg::perform_csgmesh_booleans(range(csgmesh)); + auto cgalm = csg::perform_csgmesh_booleans(csgrange); mesh = MeshBoolean::cgal::cgal_to_triangle_mesh(*cgalm); } catch (...) {} }