From 08eeed9c72cad9748b986cda508774ec6bb9b55f Mon Sep 17 00:00:00 2001 From: "yongfang.bian" Date: Mon, 13 Jan 2025 15:46:24 +0800 Subject: [PATCH] Fix:step mesh using boost thread github: #5304 Change-Id: I4afc5978b00eed20c46a1bf4100c9a0f0328daf8 (cherry picked from commit 151f40ad4dbf871576937f9674ac532c32df27d0) --- src/libslic3r/Format/STEP.cpp | 37 ++++++++++++--------- src/slic3r/GUI/StepMeshDialog.cpp | 55 +++++++++++++++++++------------ src/slic3r/GUI/StepMeshDialog.hpp | 3 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/src/libslic3r/Format/STEP.cpp b/src/libslic3r/Format/STEP.cpp index 9b7988c247..5105d26776 100644 --- a/src/libslic3r/Format/STEP.cpp +++ b/src/libslic3r/Format/STEP.cpp @@ -464,25 +464,30 @@ void Step::clean_mesh_data() unsigned int Step::get_triangle_num(double linear_defletion, double angle_defletion) { unsigned int tri_num = 0; - Handle(StepProgressIncdicator) progress = new StepProgressIncdicator(m_stop_mesh); - clean_mesh_data(); - IMeshTools_Parameters param; - param.Deflection = linear_defletion; - param.Angle = angle_defletion; - param.InParallel = true; - for (int i = 0; i < m_name_solids.size(); ++i) { - BRepMesh_IncrementalMesh mesh(m_name_solids[i].solid, param, progress->Start()); - for (TopExp_Explorer anExpSF(m_name_solids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) { - TopLoc_Location aLoc; - Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(anExpSF.Current()), aLoc); - if (!aTriangulation.IsNull()) { - tri_num += aTriangulation->NbTriangles(); + try { + Handle(StepProgressIncdicator) progress = new StepProgressIncdicator(m_stop_mesh); + clean_mesh_data(); + IMeshTools_Parameters param; + param.Deflection = linear_defletion; + param.Angle = angle_defletion; + param.InParallel = true; + for (int i = 0; i < m_name_solids.size(); ++i) { + BRepMesh_IncrementalMesh mesh(m_name_solids[i].solid, param, progress->Start()); + for (TopExp_Explorer anExpSF(m_name_solids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) { + TopLoc_Location aLoc; + Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(anExpSF.Current()), aLoc); + if (!aTriangulation.IsNull()) { + tri_num += aTriangulation->NbTriangles(); + } + } + if (m_stop_mesh.load()) { + return 0; } } - if (m_stop_mesh.load()) { - return 0; - } + } catch(Exception e) { + return 0; } + return tri_num; } diff --git a/src/slic3r/GUI/StepMeshDialog.cpp b/src/slic3r/GUI/StepMeshDialog.cpp index c29151d7a0..65a36c4597 100644 --- a/src/slic3r/GUI/StepMeshDialog.cpp +++ b/src/slic3r/GUI/StepMeshDialog.cpp @@ -19,6 +19,7 @@ static int _scale(const int val) { return val * Slic3r::GUI::wxGetApp().em_unit( static int _ITEM_WIDTH() { return _scale(30); } #define MIN_DIALOG_WIDTH FromDIP(400) #define SLIDER_WIDTH FromDIP(200) +#define SLIDER_HEIGHT FromDIP(25) #define TEXT_CTRL_WIDTH FromDIP(70) #define BUTTON_SIZE wxSize(FromDIP(58), FromDIP(24)) #define BUTTON_BORDER FromDIP(int(400 - 58 * 2) / 8) @@ -139,7 +140,7 @@ StepMeshDialog::StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double line wxSlider* linear_slider = new wxSlider(this, wxID_ANY, SLIDER_SCALE(get_linear_defletion()), 1, 100, wxDefaultPosition, - wxSize(SLIDER_WIDTH, -1), + wxSize(SLIDER_WIDTH, SLIDER_HEIGHT), wxSL_HORIZONTAL); linear_sizer->Add(linear_slider, 0, wxALIGN_RIGHT | wxLEFT, FromDIP(5)); @@ -191,7 +192,7 @@ StepMeshDialog::StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double line wxSlider* angle_slider = new wxSlider(this, wxID_ANY, SLIDER_SCALE_10(get_angle_defletion()), 1, 100, wxDefaultPosition, - wxSize(SLIDER_WIDTH, -1), + wxSize(SLIDER_WIDTH, SLIDER_HEIGHT), wxSL_HORIZONTAL); angle_sizer->Add(angle_slider, 0, wxALIGN_RIGHT | wxLEFT, FromDIP(5)); @@ -303,7 +304,7 @@ StepMeshDialog::StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double line bSizer->Add(bSizer_button, 1, wxEXPAND | wxALL, LEFT_RIGHT_PADING); this->SetSizer(bSizer); - update_mesh_number_text(); + // update_mesh_number_text(); this->Layout(); bSizer->Fit(this); @@ -322,22 +323,34 @@ StepMeshDialog::StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double line wxGetApp().UpdateDlgDarkUI(this); } +StepMeshDialog::~StepMeshDialog() +{ + stop_task(); +} + void StepMeshDialog::on_task_done(wxCommandEvent& event) { wxString text = event.GetString(); mesh_face_number_text->SetLabel(text); - if (task.valid()) { - task.get(); + if(m_task) { + if (m_task->joinable()) { + m_task->join(); + delete m_task; + m_task = nullptr; + } } } void StepMeshDialog::stop_task() { - if (task.valid()) { + if(m_task) { m_file.m_stop_mesh.store(true); - unsigned int test = task.get(); + if (m_task->joinable()) { + m_task->join(); + delete m_task; + m_task = nullptr; + } m_file.m_stop_mesh.store(false); - std::cout << test << std::endl; } } @@ -348,18 +361,18 @@ void StepMeshDialog::update_mesh_number_text() return; wxString newText = wxString::Format(_L("Calculating, please wait...")); mesh_face_number_text->SetLabel(newText); - stop_task(); - task = std::async(std::launch::async, [&] { - unsigned int m_mesh_number = m_file.get_triangle_num(get_linear_defletion(), get_angle_defletion()); - if (m_mesh_number != 0) { - wxString number_text = wxString::Format("%d", m_mesh_number); - wxCommandEvent event(wxEVT_THREAD_DONE); - event.SetString(number_text); - wxPostEvent(this, event); - m_last_linear = get_linear_defletion(); - m_last_angle = get_angle_defletion(); - } - return m_mesh_number; - }); + if (!m_task) { + m_task = new boost::thread(Slic3r::create_thread([this]() -> void { + m_mesh_number = m_file.get_triangle_num(get_linear_defletion(), get_angle_defletion()); + if (m_mesh_number != 0) { + wxString number_text = wxString::Format("%d", m_mesh_number); + wxCommandEvent event(wxEVT_THREAD_DONE); + event.SetString(number_text); + wxPostEvent(this, event); + m_last_linear = get_linear_defletion(); + m_last_angle = get_angle_defletion(); + } + })); + } } \ No newline at end of file diff --git a/src/slic3r/GUI/StepMeshDialog.hpp b/src/slic3r/GUI/StepMeshDialog.hpp index 1f3804b448..4ea70ae6e7 100644 --- a/src/slic3r/GUI/StepMeshDialog.hpp +++ b/src/slic3r/GUI/StepMeshDialog.hpp @@ -12,6 +12,7 @@ class StepMeshDialog : public Slic3r::GUI::DPIDialog { public: StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double linear_init, double angle_init); + ~StepMeshDialog() override; void on_dpi_changed(const wxRect& suggested_rect) override; inline double get_linear_defletion() { double value; @@ -44,7 +45,7 @@ private: double m_last_linear = 0.003; double m_last_angle = 0.5; unsigned int m_mesh_number = 0; - std::future task; + boost::thread* m_task {nullptr}; bool validate_number_range(const wxString& value, double min, double max); void update_mesh_number_text(); void on_task_done(wxCommandEvent& event);