From 58473f84eedac0556e11ab54db6f58ac0d1353a3 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 20 Aug 2019 11:33:58 +0200 Subject: [PATCH] Check for existence of gcode toolpaths that can be exported to obj file --- src/slic3r/GUI/3DScene.cpp | 42 +++++++++++++++++++++++++++++------ src/slic3r/GUI/3DScene.hpp | 1 + src/slic3r/GUI/GLCanvas3D.cpp | 5 +++++ src/slic3r/GUI/GLCanvas3D.hpp | 1 + src/slic3r/GUI/MainFrame.cpp | 2 +- src/slic3r/GUI/Plater.cpp | 10 ++++++--- src/slic3r/GUI/Plater.hpp | 3 ++- 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index d5fed6d42..60928c78b 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -853,11 +853,39 @@ std::string GLVolumeCollection::log_memory_info() const return " (GLVolumeCollection RAM: " + format_memsize_MB(this->cpu_memory_used()) + " GPU: " + format_memsize_MB(this->gpu_memory_used()) + " Both: " + format_memsize_MB(this->gpu_memory_used()) + ")"; } +bool can_export_to_obj(const GLVolume& volume) +{ + if (!volume.is_active || !volume.is_extrusion_path) + return false; + + if (volume.indexed_vertex_array.triangle_indices.empty() && (std::min(volume.indexed_vertex_array.triangle_indices_size, volume.tverts_range.second - volume.tverts_range.first) == 0)) + return false; + + if (volume.indexed_vertex_array.quad_indices.empty() && (std::min(volume.indexed_vertex_array.quad_indices_size, volume.qverts_range.second - volume.qverts_range.first) == 0)) + return false; + + return true; +} + +bool GLVolumeCollection::has_toolpaths_to_export() const +{ + for (const GLVolume* volume : this->volumes) + { + if (can_export_to_obj(*volume)) + return true; + } + + return false; +} + void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const { if (filename == nullptr) return; + if (!has_toolpaths_to_export()) + return; + FILE* fp = boost::nowide::fopen(filename, "w"); if (fp == nullptr) { BOOST_LOG_TRIVIAL(error) << "GLVolumeCollection::export_toolpaths_to_obj: Couldn't open " << filename << " for writing"; @@ -868,11 +896,11 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const fprintf(fp, "# Generated by %s based on Slic3r\n\n", SLIC3R_BUILD_ID); unsigned int vertices_count = 0; - unsigned int volume_count = 0; + unsigned int volumes_count = 0; for (const GLVolume* volume : this->volumes) { - if (!volume->is_active || !volume->is_extrusion_path) + if (!can_export_to_obj(*volume)) continue; std::vector vertices_and_normals_interleaved; @@ -925,19 +953,19 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const if (triangle_indices.empty() && quad_indices.empty()) continue; - fprintf(fp, "\n# vertices volume %d\n", volume_count); + fprintf(fp, "\n# vertices volume %d\n", volumes_count); for (unsigned int i = 0; i < vertices_and_normals_interleaved.size(); i += 6) { fprintf(fp, "v %f %f %f\n", vertices_and_normals_interleaved[i + 3], vertices_and_normals_interleaved[i + 4], vertices_and_normals_interleaved[i + 5]); } - fprintf(fp, "\n# normals volume %d\n", volume_count); + fprintf(fp, "\n# normals volume %d\n", volumes_count); for (unsigned int i = 0; i < vertices_and_normals_interleaved.size(); i += 6) { fprintf(fp, "vn %f %f %f\n", vertices_and_normals_interleaved[i + 0], vertices_and_normals_interleaved[i + 1], vertices_and_normals_interleaved[i + 2]); } - fprintf(fp, "\n# triangular facets volume %d\n", volume_count); + fprintf(fp, "\n# triangular facets volume %d\n", volumes_count); for (unsigned int i = 0; i < triangle_indices.size(); i += 3) { int id_v1 = vertices_count + 1 + triangle_indices[i + 0]; @@ -946,7 +974,7 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const fprintf(fp, "f %d//%d %d//%d %d//%d\n", id_v1, id_v1, id_v2, id_v2, id_v3, id_v3); } - fprintf(fp, "\n# quadrangular facets volume %d\n", volume_count); + fprintf(fp, "\n# quadrangular facets volume %d\n", volumes_count); for (unsigned int i = 0; i < quad_indices.size(); i += 4) { int id_v1 = vertices_count + 1 + quad_indices[i + 0]; @@ -956,7 +984,7 @@ void GLVolumeCollection::export_toolpaths_to_obj(const char* filename) const fprintf(fp, "f %d//%d %d//%d %d//%d %d//%d\n", id_v1, id_v1, id_v2, id_v2, id_v3, id_v3, id_v4, id_v4); } - ++volume_count; + ++volumes_count; vertices_count += vertices_and_normals_interleaved.size() / 6; } diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 9c0e50cad..e0603ebc0 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -564,6 +564,7 @@ public: // Return CPU, GPU and total memory log line. std::string log_memory_info() const; + bool has_toolpaths_to_export() const; // Export the geometry of the GLVolumes toolpaths of this collection into the file with the given path, in obj format void export_toolpaths_to_obj(const char* filename) const; diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 2a87efab9..67f30c6e4 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -3403,6 +3403,11 @@ void GLCanvas3D::msw_rescale() m_warning_texture.msw_rescale(*this); } +bool GLCanvas3D::has_toolpaths_to_export() const +{ + return m_volumes.has_toolpaths_to_export(); +} + void GLCanvas3D::export_toolpaths_to_obj(const char* filename) const { m_volumes.export_toolpaths_to_obj(filename); diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 4fd37e9b1..d23e29478 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -644,6 +644,7 @@ public: void get_undoredo_toolbar_additional_tooltip(unsigned int item_id, std::string& text) { return m_undoredo_toolbar.get_additional_tooltip(item_id, text); } void set_undoredo_toolbar_additional_tooltip(unsigned int item_id, const std::string& text) { m_undoredo_toolbar.set_additional_tooltip(item_id, text); } + bool has_toolpaths_to_export() const; void export_toolpaths_to_obj(const char* filename) const; private: diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index b3a87f635..800ac55a9 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -247,7 +247,7 @@ bool MainFrame::can_export_model() const bool MainFrame::can_export_toolpaths() const { - return (m_plater != nullptr) && (m_plater->printer_technology() == ptFFF) && m_plater->is_preview_shown() && m_plater->is_preview_loaded(); + return (m_plater != nullptr) && (m_plater->printer_technology() == ptFFF) && m_plater->is_preview_shown() && m_plater->is_preview_loaded() && m_plater->has_toolpaths_to_export(); } bool MainFrame::can_export_supports() const diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 773557ccf..b804c2479 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -4434,7 +4434,12 @@ void Plater::export_3mf(const boost::filesystem::path& output_path) } } -void Plater::export_toolpaths_to_obj() +bool Plater::has_toolpaths_to_export() const +{ + return p->preview->get_canvas3d()->has_toolpaths_to_export(); +} + +void Plater::export_toolpaths_to_obj() const { if ((printer_technology() != ptFFF) || !is_preview_loaded()) return; @@ -4442,9 +4447,8 @@ void Plater::export_toolpaths_to_obj() wxString path = p->get_export_file(FT_OBJ); if (path.empty()) return; - + wxBusyCursor wait; - p->preview->get_canvas3d()->export_toolpaths_to_obj(into_u8(path).c_str()); } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 40961ae46..0bd01835e 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -183,7 +183,8 @@ public: void export_stl(bool extended = false, bool selection_only = false); void export_amf(); void export_3mf(const boost::filesystem::path& output_path = boost::filesystem::path()); - void export_toolpaths_to_obj(); + bool has_toolpaths_to_export() const; + void export_toolpaths_to_obj() const; void reslice(); void reslice_SLA_supports(const ModelObject &object); void changed_object(int obj_idx);