Fix:step mesh using boost thread

github: #5304
Change-Id: I4afc5978b00eed20c46a1bf4100c9a0f0328daf8
(cherry picked from commit 151f40ad4dbf871576937f9674ac532c32df27d0)
This commit is contained in:
yongfang.bian 2025-01-13 15:46:24 +08:00 committed by Noisyfox
parent affa9068f0
commit 08eeed9c72
3 changed files with 57 additions and 38 deletions

View file

@ -464,25 +464,30 @@ void Step::clean_mesh_data()
unsigned int Step::get_triangle_num(double linear_defletion, double angle_defletion) unsigned int Step::get_triangle_num(double linear_defletion, double angle_defletion)
{ {
unsigned int tri_num = 0; unsigned int tri_num = 0;
Handle(StepProgressIncdicator) progress = new StepProgressIncdicator(m_stop_mesh); try {
clean_mesh_data(); Handle(StepProgressIncdicator) progress = new StepProgressIncdicator(m_stop_mesh);
IMeshTools_Parameters param; clean_mesh_data();
param.Deflection = linear_defletion; IMeshTools_Parameters param;
param.Angle = angle_defletion; param.Deflection = linear_defletion;
param.InParallel = true; param.Angle = angle_defletion;
for (int i = 0; i < m_name_solids.size(); ++i) { param.InParallel = true;
BRepMesh_IncrementalMesh mesh(m_name_solids[i].solid, param, progress->Start()); for (int i = 0; i < m_name_solids.size(); ++i) {
for (TopExp_Explorer anExpSF(m_name_solids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) { BRepMesh_IncrementalMesh mesh(m_name_solids[i].solid, param, progress->Start());
TopLoc_Location aLoc; for (TopExp_Explorer anExpSF(m_name_solids[i].solid, TopAbs_FACE); anExpSF.More(); anExpSF.Next()) {
Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(anExpSF.Current()), aLoc); TopLoc_Location aLoc;
if (!aTriangulation.IsNull()) { Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation(TopoDS::Face(anExpSF.Current()), aLoc);
tri_num += aTriangulation->NbTriangles(); if (!aTriangulation.IsNull()) {
tri_num += aTriangulation->NbTriangles();
}
}
if (m_stop_mesh.load()) {
return 0;
} }
} }
if (m_stop_mesh.load()) { } catch(Exception e) {
return 0; return 0;
}
} }
return tri_num; return tri_num;
} }

View file

@ -19,6 +19,7 @@ static int _scale(const int val) { return val * Slic3r::GUI::wxGetApp().em_unit(
static int _ITEM_WIDTH() { return _scale(30); } static int _ITEM_WIDTH() { return _scale(30); }
#define MIN_DIALOG_WIDTH FromDIP(400) #define MIN_DIALOG_WIDTH FromDIP(400)
#define SLIDER_WIDTH FromDIP(200) #define SLIDER_WIDTH FromDIP(200)
#define SLIDER_HEIGHT FromDIP(25)
#define TEXT_CTRL_WIDTH FromDIP(70) #define TEXT_CTRL_WIDTH FromDIP(70)
#define BUTTON_SIZE wxSize(FromDIP(58), FromDIP(24)) #define BUTTON_SIZE wxSize(FromDIP(58), FromDIP(24))
#define BUTTON_BORDER FromDIP(int(400 - 58 * 2) / 8) #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, wxSlider* linear_slider = new wxSlider(this, wxID_ANY,
SLIDER_SCALE(get_linear_defletion()), SLIDER_SCALE(get_linear_defletion()),
1, 100, wxDefaultPosition, 1, 100, wxDefaultPosition,
wxSize(SLIDER_WIDTH, -1), wxSize(SLIDER_WIDTH, SLIDER_HEIGHT),
wxSL_HORIZONTAL); wxSL_HORIZONTAL);
linear_sizer->Add(linear_slider, 0, wxALIGN_RIGHT | wxLEFT, FromDIP(5)); 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, wxSlider* angle_slider = new wxSlider(this, wxID_ANY,
SLIDER_SCALE_10(get_angle_defletion()), SLIDER_SCALE_10(get_angle_defletion()),
1, 100, wxDefaultPosition, 1, 100, wxDefaultPosition,
wxSize(SLIDER_WIDTH, -1), wxSize(SLIDER_WIDTH, SLIDER_HEIGHT),
wxSL_HORIZONTAL); wxSL_HORIZONTAL);
angle_sizer->Add(angle_slider, 0, wxALIGN_RIGHT | wxLEFT, FromDIP(5)); 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); bSizer->Add(bSizer_button, 1, wxEXPAND | wxALL, LEFT_RIGHT_PADING);
this->SetSizer(bSizer); this->SetSizer(bSizer);
update_mesh_number_text(); // update_mesh_number_text();
this->Layout(); this->Layout();
bSizer->Fit(this); bSizer->Fit(this);
@ -322,22 +323,34 @@ StepMeshDialog::StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double line
wxGetApp().UpdateDlgDarkUI(this); wxGetApp().UpdateDlgDarkUI(this);
} }
StepMeshDialog::~StepMeshDialog()
{
stop_task();
}
void StepMeshDialog::on_task_done(wxCommandEvent& event) void StepMeshDialog::on_task_done(wxCommandEvent& event)
{ {
wxString text = event.GetString(); wxString text = event.GetString();
mesh_face_number_text->SetLabel(text); mesh_face_number_text->SetLabel(text);
if (task.valid()) { if(m_task) {
task.get(); if (m_task->joinable()) {
m_task->join();
delete m_task;
m_task = nullptr;
}
} }
} }
void StepMeshDialog::stop_task() void StepMeshDialog::stop_task()
{ {
if (task.valid()) { if(m_task) {
m_file.m_stop_mesh.store(true); 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); m_file.m_stop_mesh.store(false);
std::cout << test << std::endl;
} }
} }
@ -348,18 +361,18 @@ void StepMeshDialog::update_mesh_number_text()
return; return;
wxString newText = wxString::Format(_L("Calculating, please wait...")); wxString newText = wxString::Format(_L("Calculating, please wait..."));
mesh_face_number_text->SetLabel(newText); mesh_face_number_text->SetLabel(newText);
stop_task(); stop_task();
task = std::async(std::launch::async, [&] { if (!m_task) {
unsigned int m_mesh_number = m_file.get_triangle_num(get_linear_defletion(), get_angle_defletion()); m_task = new boost::thread(Slic3r::create_thread([this]() -> void {
if (m_mesh_number != 0) { m_mesh_number = m_file.get_triangle_num(get_linear_defletion(), get_angle_defletion());
wxString number_text = wxString::Format("%d", m_mesh_number); if (m_mesh_number != 0) {
wxCommandEvent event(wxEVT_THREAD_DONE); wxString number_text = wxString::Format("%d", m_mesh_number);
event.SetString(number_text); wxCommandEvent event(wxEVT_THREAD_DONE);
wxPostEvent(this, event); event.SetString(number_text);
m_last_linear = get_linear_defletion(); wxPostEvent(this, event);
m_last_angle = get_angle_defletion(); m_last_linear = get_linear_defletion();
} m_last_angle = get_angle_defletion();
return m_mesh_number; }
}); }));
}
} }

View file

@ -12,6 +12,7 @@ class StepMeshDialog : public Slic3r::GUI::DPIDialog
{ {
public: public:
StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double linear_init, double angle_init); StepMeshDialog(wxWindow* parent, Slic3r::Step& file, double linear_init, double angle_init);
~StepMeshDialog() override;
void on_dpi_changed(const wxRect& suggested_rect) override; void on_dpi_changed(const wxRect& suggested_rect) override;
inline double get_linear_defletion() { inline double get_linear_defletion() {
double value; double value;
@ -44,7 +45,7 @@ private:
double m_last_linear = 0.003; double m_last_linear = 0.003;
double m_last_angle = 0.5; double m_last_angle = 0.5;
unsigned int m_mesh_number = 0; unsigned int m_mesh_number = 0;
std::future<unsigned int> task; boost::thread* m_task {nullptr};
bool validate_number_range(const wxString& value, double min, double max); bool validate_number_range(const wxString& value, double min, double max);
void update_mesh_number_text(); void update_mesh_number_text();
void on_task_done(wxCommandEvent& event); void on_task_done(wxCommandEvent& event);