From ab84da6c56767d9894eea925263966bdefdf0556 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Wed, 1 Sep 2021 11:08:08 +0200 Subject: [PATCH] Fix for #6803 - Illegal character in filename when STL opened direct from CAD app Follow-up to ea4e9b35a3e2084325fbaed4c5bedf310bcdef3e Win32 specific: Substitute slashes to back slashes in file paths when loading model files (STLs, 3MFS ...) --- src/slic3r/GUI/GUI_ObjectList.cpp | 3 +-- src/slic3r/GUI/Plater.cpp | 36 +++++++++++++------------------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index 82f03f51a..737c84083 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -1702,8 +1702,7 @@ void ObjectList::load_shape_object_from_gallery(const wxArrayString& input_files snapshot_label += ", " + wxString::FromUTF8(paths[i].filename().string().c_str()); take_snapshot(snapshot_label); - std::vector res = wxGetApp().plater()->load_files(paths, true, false); - if (!res.empty()) + if (! wxGetApp().plater()->load_files(paths, true, false).empty()) wxGetApp().mainframe->update_title(); } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 743f27578..98eb73c89 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -2221,7 +2221,14 @@ std::vector Plater::priv::load_files(const std::vector& input_ std::vector obj_idxs; for (size_t i = 0; i < input_files.size(); ++i) { +#ifdef _WIN32 + auto path = input_files[i]; + // On Windows, we swap slashes to back slashes, see GH #6803 as read_from_file() does not understand slashes on Windows thus it assignes full path to names of loaded objects. + path.make_preferred(); +#else // _WIN32 + // Don't make a copy on Posix. Slash is a path separator, back slashes are not accepted as a substitute. const auto &path = input_files[i]; +#endif // _WIN32 const auto filename = path.filename(); dlg.Update(static_cast(100.0f * static_cast(i) / static_cast(input_files.size())), _L("Loading file") + ": " + from_path(filename)); dlg.Fit(); @@ -2336,9 +2343,7 @@ std::vector Plater::priv::load_files(const std::vector& input_ else { model = Slic3r::Model::read_from_file(path.string(), nullptr, nullptr, only_if(load_config, Model::LoadAttribute::CheckVersion)); for (auto obj : model.objects) - if (obj->name.empty() || - obj->name.find_first_of("/") != std::string::npos) // When file is imported from Fusion360 the path containes "/" instead of "\\" (see https://github.com/prusa3d/PrusaSlicer/issues/6803) - // But read_from_file doesn't support that direction separator and as a result object name containes full path + if (obj->name.empty()) obj->name = fs::path(obj->input_file).filename().string(); } } catch (const ConfigurationError &e) { @@ -2457,7 +2462,7 @@ std::vector Plater::priv::load_files(const std::vector& input_ } if (load_model) { - wxGetApp().app_config->update_skein_dir(input_files[input_files.size() - 1].parent_path().string()); + wxGetApp().app_config->update_skein_dir(input_files[input_files.size() - 1].parent_path().make_preferred().string()); // XXX: Plater.pm had @loaded_files, but didn't seem to fill them with the filenames... statusbar()->set_status_text(_L("Loaded")); } @@ -4787,10 +4792,7 @@ void Plater::load_project(const wxString& filename) p->reset(); - std::vector input_paths; - input_paths.push_back(into_path(filename)); - - if (! load_files(input_paths).empty()) { + if (! load_files({ into_path(filename) }).empty()) { // At least one file was loaded. p->set_project_filename(filename); reset_project_dirty_initial_presets(); @@ -4807,7 +4809,7 @@ void Plater::add_model(bool imperial_units/* = false*/) std::vector paths; for (const auto &file : input_files) - paths.push_back(into_path(file)); + paths.emplace_back(into_path(file)); wxString snapshot_label; assert(! paths.empty()); @@ -4840,12 +4842,8 @@ void Plater::extract_config_from_project() wxString input_file; wxGetApp().load_project(this, input_file); - if (input_file.empty()) - return; - - std::vector input_paths; - input_paths.push_back(into_path(input_file)); - load_files(input_paths, false, true); + if (! input_file.empty()) + load_files({ into_path(input_file) }, false, true); } void Plater::load_gcode() @@ -5076,15 +5074,11 @@ bool Plater::load_files(const wxArrayString& filenames) } case LoadType::LoadGeometry: { Plater::TakeSnapshot snapshot(this, _L("Import Object")); - std::vector in_paths; - in_paths.emplace_back(*it); - load_files(in_paths, true, false); + load_files({ *it }, true, false); break; } case LoadType::LoadConfig: { - std::vector in_paths; - in_paths.emplace_back(*it); - load_files(in_paths, false, true); + load_files({ *it }, false, true); break; } case LoadType::Unknown : {