123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "Exception.hpp"
- #include "PrintBase.hpp"
- #include <boost/filesystem.hpp>
- #include <boost/lexical_cast.hpp>
- #include <regex>
- #include "I18N.hpp"
- #define L(s) Slic3r::I18N::translate(s)
- namespace Slic3r
- {
- size_t PrintStateBase::g_last_timestamp = 0;
- void PrintBase::update_object_placeholders(DynamicConfig &config, const std::string &default_ext) const
- {
-
- std::string input_file;
- std::vector<std::string> v_scale;
- for (const ModelObject *model_object : m_model.objects) {
- ModelInstance *printable = nullptr;
- for (ModelInstance *model_instance : model_object->instances)
- if (model_instance->is_printable()) {
- printable = model_instance;
- break;
- }
- if (printable) {
-
- v_scale.push_back("x:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(X) * 100) +
- "% y:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Y) * 100) +
- "% z:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Z) * 100) + "%");
- if (input_file.empty())
- input_file = model_object->name.empty() ? model_object->input_file : model_object->name;
- }
- }
-
- config.set_key_value("scale", new ConfigOptionStrings(v_scale));
- if (! input_file.empty()) {
-
- const std::string input_filename = boost::filesystem::path(input_file).filename().string();
- const std::string input_filename_base = input_filename.substr(0, input_filename.find_last_of("."));
- config.set_key_value("input_filename", new ConfigOptionString(input_filename_base + default_ext));
- config.set_key_value("input_filename_base", new ConfigOptionString(input_filename_base));
- }
- }
- std::string PrintBase::output_filename(const std::string &format, const std::string &default_ext, const std::string &filename_base, const DynamicConfig *config_override) const
- {
- DynamicConfig cfg;
- if (config_override != nullptr)
- cfg = *config_override;
- PlaceholderParser::update_timestamp(cfg);
- this->update_object_placeholders(cfg, default_ext);
- if (! filename_base.empty()) {
- cfg.set_key_value("input_filename", new ConfigOptionString(filename_base + default_ext));
- cfg.set_key_value("input_filename_base", new ConfigOptionString(filename_base));
- }
- try {
- uint16_t extruder_initial = config_override->option("initial_extruder") != nullptr ? config_override->option("initial_extruder")->getInt() : 0;
- boost::filesystem::path filepath = format.empty() ?
- cfg.opt_string("input_filename_base") + default_ext :
- this->placeholder_parser().process(format, extruder_initial, &cfg);
-
- std::string forbidden_base;
- if (const ConfigOptionString* opt = this->placeholder_parser().external_config()->option<ConfigOptionString>("gcode_filename_illegal_char")) {
- forbidden_base = opt->value;
- }
- if (!forbidden_base.empty()) {
- const std::string filename_init = filepath.stem().string();
- std::string filename = filename_init;
- std::string extension = filepath.extension().string();
-
- std::regex placehoder = std::regex("\\{[a-z_]+\\}");
- std::smatch matches;
- std::string::const_iterator searchStart(filename_init.cbegin());
- while (std::regex_search(searchStart, filename_init.cend(), matches, placehoder)) {
- for (int i = 0; i < matches.size(); i++) {
- filename.replace(matches.position(i), matches.length(i), matches.length(i), '_');
- }
- searchStart = matches.suffix().first;
- }
-
- bool regexp_used = false;
- if (forbidden_base.front() == '(' || forbidden_base.front() == '[') {
- try {
- filename = std::regex_replace(filename, std::regex(forbidden_base), "_");
- regexp_used = true;
- }catch(std::exception){}
- }
- if (!regexp_used) {
- for(int i=0; i< forbidden_base.size(); i++)
- std::replace(filename.begin(), filename.end(), forbidden_base.at(i), '_');
- }
-
- searchStart = (filename_init.cbegin());
- while (std::regex_search(searchStart, filename_init.cend(), matches, placehoder)) {
- for (int i = 0; i < matches.size(); i++) {
- filename.replace(matches.position(i), matches.length(i), matches.str());
- }
- searchStart = matches.suffix().first;
- }
-
- filepath = filename + extension;
- }
- if (filepath.extension().empty())
- filepath = boost::filesystem::change_extension(filepath, default_ext);
- return filepath.string();
- } catch (std::runtime_error &err) {
- throw Slic3r::PlaceholderParserError(L("Failed processing of the output_filename_format template.") + "\n" + err.what());
- }
- }
- std::string PrintBase::output_filepath(const std::string &path, const std::string &filename_base) const
- {
-
- if (path.empty())
-
- return (boost::filesystem::path(m_model.propose_export_file_name_and_path()).parent_path() / this->output_filename(filename_base)).make_preferred().string();
-
-
- boost::filesystem::path p(path);
- if (boost::filesystem::is_directory(p))
- return (p / this->output_filename(filename_base)).make_preferred().string();
-
-
- return path;
- }
- void PrintBase::status_update_warnings(ObjectID object_id, int step, PrintStateBase::WarningLevel , const std::string &message)
- {
- if (this->m_status_callback)
- m_status_callback(SlicingStatus(*this, step));
- else if (! message.empty())
- printf("%s warning: %s\n", (object_id == this->id()) ? "print" : "print object", message.c_str());
- }
- std::mutex& PrintObjectBase::state_mutex(PrintBase *print)
- {
- return print->state_mutex();
- }
- std::function<void()> PrintObjectBase::cancel_callback(PrintBase *print)
- {
- return print->cancel_callback();
- }
- void PrintObjectBase::status_update_warnings(PrintBase *print, int step, PrintStateBase::WarningLevel warning_level, const std::string &message)
- {
- print->status_update_warnings(this->id(), step, warning_level, message);
- }
- }
|