diff --git a/src/GUI/Plater.cpp b/src/GUI/Plater.cpp index 301917f46..84f93a6a0 100644 --- a/src/GUI/Plater.cpp +++ b/src/GUI/Plater.cpp @@ -483,7 +483,7 @@ void Plater::arrange() { GetFrame()->statusbar->SetStatusText(_("Nothing to arrange.")); return; } - bool success {this->model->arrange_objects(this->config->min_object_distance(), &bb)}; + bool success {this->model->arrange_objects(this->config->config().min_object_distance(), &bb)}; if (success) { GetFrame()->statusbar->SetStatusText(_("Objects were arranged.")); diff --git a/src/GUI/Plater/Plate2D.cpp b/src/GUI/Plater/Plate2D.cpp index e36f0d3c8..2aa46d200 100644 --- a/src/GUI/Plater/Plate2D.cpp +++ b/src/GUI/Plater/Plate2D.cpp @@ -386,7 +386,7 @@ void Plate2D::update_bed_size() { const auto& canvas_h {canvas_size.GetHeight()}; if (canvas_w == 0) return; // Abort early if we haven't drawn canvas yet. - this->bed_polygon = Slic3r::Polygon(scale(dynamic_cast(config->optptr("bed_shape"))->values)); + this->bed_polygon = Slic3r::Polygon(scale(config->get("bed_shape").values)); const auto& polygon = bed_polygon; diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index c91f27440..6b24bbed7 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -26,7 +26,7 @@ Config::new_from_defaults(t_config_option_keys init) for (auto& opt_key : init) { if (print_config_def.has(opt_key)) { const std::string value { print_config_def.get(opt_key)->default_value->serialize() }; - my_config->set_deserialize(opt_key, value); + my_config->_config.set_deserialize(opt_key, value); } } @@ -52,7 +52,7 @@ bool Config::validate() { // general validation - for (auto k : this->keys()) { + for (auto k : this->_config.keys()) { if (print_config_def.options.count(k) == 0) continue; // skip over keys that aren't in the master list const auto& opt {print_config_def.options.at(k)}; if (opt.cli == "" || std::regex_search(opt.cli, _match_info, _cli_pattern) == false) continue; @@ -95,24 +95,24 @@ Config::set(const t_config_option_key& opt_key, const std::string& value) switch (def.type) { case coInt: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setInt(std::stoi(value)); } break; case coInts: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; if (!ptr->deserialize(value, true) ) { throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } } break; case coFloat: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setFloat(std::stod(value)); } break; case coFloatOrPercent: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; const size_t perc = value.find("%"); ptr->percent = (perc != std::string::npos); if (ptr->percent) @@ -122,14 +122,14 @@ Config::set(const t_config_option_key& opt_key, const std::string& value) } break; case coFloats: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; if (!ptr->deserialize(value, true) ) { throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } } break; case coString: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; if (!ptr->deserialize(value) ) { throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } @@ -152,32 +152,32 @@ Config::set(const t_config_option_key& opt_key, const int value) switch (def.type) { case coInt: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setInt(value); } break; case coInts: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->deserialize(std::to_string(value), true); } break; case coFloat: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setFloat(value); } break; case coFloatOrPercent: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setFloat(value); } break; case coFloats: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->deserialize(std::to_string(value), true); } break; case coString: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; if (!ptr->deserialize(std::to_string(value)) ) { throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } @@ -199,32 +199,32 @@ Config::set(const t_config_option_key& opt_key, const double value) switch (def.type) { case coInt: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setInt(std::round(value)); } break; case coInts: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->deserialize(std::to_string(std::round(value)), true); } break; case coFloat: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setFloat(value); } break; case coFloatOrPercent: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->setFloat(value); } break; case coFloats: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; ptr->deserialize(std::to_string(value), true); } break; case coString: { - auto* ptr {dynamic_cast(this->optptr(opt_key, true))}; + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; if (!ptr->deserialize(std::to_string(value)) ) { throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } @@ -283,7 +283,7 @@ is_valid_float(const std::string& type, const ConfigOptionDef& opt, const std::s return result; } - +Config::Config() : _config(DynamicPrintConfig()) {}; } // namespace Slic3r diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 2f4f24447..f115d940e 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -34,7 +34,7 @@ public: class Config; using config_ptr = std::shared_ptr; -class Config : public DynamicPrintConfig { +class Config { public: /// Factory method to construct a Config with all default values loaded. @@ -63,14 +63,29 @@ public: template T& get(const t_config_option_key& opt_key, bool create=false) { if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option.")); - return *(dynamic_cast(this->optptr(opt_key, create))); + return *(dynamic_cast(this->_config.optptr(opt_key, create))); } + + double getFloat(const t_config_option_key& opt_key, bool create=false) { + if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option.")); + return (dynamic_cast(this->_config.optptr(opt_key, create)))->getFloat(); + } + + int getInt(const t_config_option_key& opt_key, bool create=false) { + if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option.")); + return (dynamic_cast(this->_config.optptr(opt_key, create)))->getInt(); + } + std::string getString(const t_config_option_key& opt_key, bool create=false) { + if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option.")); + return (dynamic_cast(this->_config.optptr(opt_key, create)))->getString(); + } + /// Template function to dynamic cast and leave it in pointer form. template T* get_ptr(const t_config_option_key& opt_key, bool create=false) { if (print_config_def.options.count(opt_key) == 0) throw InvalidOptionType(opt_key + std::string(" is an invalid option.")); - return dynamic_cast(this->optptr(opt_key, create)); + return dynamic_cast(this->_config.optptr(opt_key, create)); } /// Function to parse value from a string to whatever opt_key is. @@ -90,9 +105,23 @@ public: /// It will throw InvalidConfigOption exceptions on failure. bool validate(); + const DynamicPrintConfig& config() const { return _config; } + bool empty() const { return _config.empty(); } + + /// Pass-through of apply() + void apply(const config_ptr& other) { _config.apply(other->config()); } + void apply(const Slic3r::Config& other) { _config.apply(other.config()); } + + + Config(); + private: std::regex _cli_pattern {"=(.+)$"}; std::smatch _match_info {}; + + + /// Underlying configuration store. + DynamicPrintConfig _config {}; }; bool is_valid_int(const std::string& type, const ConfigOptionDef& opt, const std::string& ser_value);