PrintBase.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "Exception.hpp"
  2. #include "PrintBase.hpp"
  3. #include <boost/filesystem.hpp>
  4. #include <boost/lexical_cast.hpp>
  5. #include <regex>
  6. #include "I18N.hpp"
  7. //! macro used to mark string used at localization,
  8. //! return same string
  9. #define L(s) Slic3r::I18N::translate(s)
  10. namespace Slic3r
  11. {
  12. size_t PrintStateBase::g_last_timestamp = 0;
  13. // Update "scale", "input_filename", "input_filename_base" placeholders from the current m_objects.
  14. void PrintBase::update_object_placeholders(DynamicConfig &config, const std::string &default_ext) const
  15. {
  16. // get the first input file name
  17. std::string input_file;
  18. std::vector<std::string> v_scale;
  19. for (const ModelObject *model_object : m_model.objects) {
  20. ModelInstance *printable = nullptr;
  21. for (ModelInstance *model_instance : model_object->instances)
  22. if (model_instance->is_printable()) {
  23. printable = model_instance;
  24. break;
  25. }
  26. if (printable) {
  27. // CHECK_ME -> Is the following correct ?
  28. v_scale.push_back("x:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(X) * 100) +
  29. "% y:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Y) * 100) +
  30. "% z:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Z) * 100) + "%");
  31. if (input_file.empty())
  32. input_file = model_object->name.empty() ? model_object->input_file : model_object->name;
  33. }
  34. }
  35. config.set_key_value("scale", new ConfigOptionStrings(v_scale));
  36. if (! input_file.empty()) {
  37. // get basename with and without suffix
  38. const std::string input_filename = boost::filesystem::path(input_file).filename().string();
  39. const std::string input_filename_base = input_filename.substr(0, input_filename.find_last_of("."));
  40. config.set_key_value("input_filename", new ConfigOptionString(input_filename_base + default_ext));
  41. config.set_key_value("input_filename_base", new ConfigOptionString(input_filename_base));
  42. }
  43. }
  44. // Generate an output file name based on the format template, default extension, and template parameters
  45. // (timestamps, object placeholders derived from the model, current placeholder prameters, print statistics - config_override)
  46. std::string PrintBase::output_filename(const std::string &format, const std::string &default_ext, const std::string &filename_base, const DynamicConfig *config_override) const
  47. {
  48. DynamicConfig cfg;
  49. if (config_override != nullptr)
  50. cfg = *config_override;
  51. PlaceholderParser::update_timestamp(cfg);
  52. this->update_object_placeholders(cfg, default_ext);
  53. if (! filename_base.empty()) {
  54. cfg.set_key_value("input_filename", new ConfigOptionString(filename_base + default_ext));
  55. cfg.set_key_value("input_filename_base", new ConfigOptionString(filename_base));
  56. }
  57. try {
  58. uint16_t extruder_initial = config_override->option("initial_extruder") != nullptr ? config_override->option("initial_extruder")->getInt() : 0;
  59. boost::filesystem::path filepath = format.empty() ?
  60. cfg.opt_string("input_filename_base") + default_ext :
  61. this->placeholder_parser().process(format, extruder_initial, &cfg);
  62. //remove unwanted characters
  63. std::string forbidden_base;
  64. if (const ConfigOptionString* opt = this->placeholder_parser().external_config()->option<ConfigOptionString>("gcode_filename_illegal_char")) {
  65. forbidden_base = opt->value;
  66. }
  67. if (!forbidden_base.empty()) {
  68. const std::string filename_init = filepath.stem().string();
  69. std::string filename = filename_init;
  70. std::string extension = filepath.extension().string();
  71. //remove {print_time} and things like that that may be computed after, and re-put them inside it after the replace.
  72. std::regex placehoder = std::regex("\\{[a-z_]+\\}");
  73. std::smatch matches;
  74. std::string::const_iterator searchStart(filename_init.cbegin());
  75. while (std::regex_search(searchStart, filename_init.cend(), matches, placehoder)) {
  76. for (int i = 0; i < matches.size(); i++) {
  77. filename.replace(matches.position(i), matches.length(i), matches.length(i), '_');
  78. }
  79. searchStart = matches.suffix().first;
  80. }
  81. //remove unwanted characters from the cleaned string
  82. bool regexp_used = false;
  83. if (forbidden_base.front() == '(' || forbidden_base.front() == '[') {
  84. try {
  85. filename = std::regex_replace(filename, std::regex(forbidden_base), "_");
  86. regexp_used = true;
  87. }catch(std::exception){}
  88. }
  89. if (!regexp_used) {
  90. for(int i=0; i< forbidden_base.size(); i++)
  91. std::replace(filename.begin(), filename.end(), forbidden_base.at(i), '_');
  92. }
  93. //re-put {print_time} and things like that
  94. searchStart = (filename_init.cbegin());
  95. while (std::regex_search(searchStart, filename_init.cend(), matches, placehoder)) {
  96. for (int i = 0; i < matches.size(); i++) {
  97. filename.replace(matches.position(i), matches.length(i), matches.str());
  98. }
  99. searchStart = matches.suffix().first;
  100. }
  101. // set the path var
  102. filepath = filename + extension;
  103. }
  104. if (filepath.extension().empty())
  105. filepath = boost::filesystem::change_extension(filepath, default_ext);
  106. return filepath.string();
  107. } catch (std::runtime_error &err) {
  108. throw Slic3r::PlaceholderParserError(L("Failed processing of the output_filename_format template.") + "\n" + err.what());
  109. }
  110. }
  111. std::string PrintBase::output_filepath(const std::string &path, const std::string &filename_base) const
  112. {
  113. // if we were supplied no path, generate an automatic one based on our first object's input file
  114. if (path.empty())
  115. // get the first input file name
  116. return (boost::filesystem::path(m_model.propose_export_file_name_and_path()).parent_path() / this->output_filename(filename_base)).make_preferred().string();
  117. // if we were supplied a directory, use it and append our automatically generated filename
  118. boost::filesystem::path p(path);
  119. if (boost::filesystem::is_directory(p))
  120. return (p / this->output_filename(filename_base)).make_preferred().string();
  121. // if we were supplied a file which is not a directory, use it
  122. return path;
  123. }
  124. void PrintBase::status_update_warnings(ObjectID object_id, int step, PrintStateBase::WarningLevel /* warning_level */, const std::string &message)
  125. {
  126. if (this->m_status_callback)
  127. m_status_callback(SlicingStatus(*this, step));
  128. else if (! message.empty())
  129. printf("%s warning: %s\n", (object_id == this->id()) ? "print" : "print object", message.c_str());
  130. }
  131. std::mutex& PrintObjectBase::state_mutex(PrintBase *print)
  132. {
  133. return print->state_mutex();
  134. }
  135. std::function<void()> PrintObjectBase::cancel_callback(PrintBase *print)
  136. {
  137. return print->cancel_callback();
  138. }
  139. void PrintObjectBase::status_update_warnings(PrintBase *print, int step, PrintStateBase::WarningLevel warning_level, const std::string &message)
  140. {
  141. print->status_update_warnings(this->id(), step, warning_level, message);
  142. }
  143. } // namespace Slic3r