From 7a799be426d00c70fe181efb454065925f87c34a Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 24 Sep 2020 19:03:09 +0200 Subject: [PATCH] DynamicPrintConfig::normalize() renamed to normalize_fdm(), optimization of Print::apply() --- src/PrusaSlicer.cpp | 6 +++--- src/libslic3r/Print.cpp | 32 ++++++++++++++++++++------------ src/libslic3r/PrintConfig.cpp | 2 +- src/libslic3r/PrintConfig.hpp | 12 +----------- src/libslic3r/PrintObject.cpp | 34 +++++++++++++++++++++++++++++----- src/libslic3r/SLAPrint.cpp | 8 +++----- src/libslic3r/SLAPrint.hpp | 2 +- 7 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/PrusaSlicer.cpp b/src/PrusaSlicer.cpp index a12ad8bb7..d79196cfa 100644 --- a/src/PrusaSlicer.cpp +++ b/src/PrusaSlicer.cpp @@ -93,7 +93,7 @@ int CLI::run(int argc, char **argv) return 1; m_extra_config.apply(m_config, true); - m_extra_config.normalize(); + m_extra_config.normalize_fdm(); PrinterTechnology printer_technology = Slic3r::printer_technology(m_config); @@ -129,7 +129,7 @@ int CLI::run(int argc, char **argv) boost::nowide::cerr << "Error while reading config file: " << ex.what() << std::endl; return 1; } - config.normalize(); + config.normalize_fdm(); PrinterTechnology other_printer_technology = Slic3r::printer_technology(config); if (printer_technology == ptUnknown) { printer_technology = other_printer_technology; @@ -196,7 +196,7 @@ int CLI::run(int argc, char **argv) // (command line options override --load files) m_print_config.apply(m_extra_config, true); // Normalizing after importing the 3MFs / AMFs - m_print_config.normalize(); + m_print_config.normalize_fdm(); // Initialize full print configs for both the FFF and SLA technologies. FullPrintConfig fff_print_config; diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index fb276a7c2..34a3d8b23 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -587,7 +587,7 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ new_full_config.option("print_settings_id", true); new_full_config.option("filament_settings_id", true); new_full_config.option("printer_settings_id", true); - new_full_config.normalize(); + new_full_config.normalize_fdm(); // Find modified keys of the various configs. Resolve overrides extruder retract values by filament profiles. t_config_option_keys print_diff, object_diff, region_diff, full_config_diff; @@ -844,8 +844,8 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ // Only volume IDs, volume types, transformation matrices and their order are checked, configuration and other parameters are NOT checked. bool model_parts_differ = model_volume_list_changed(model_object, model_object_new, ModelVolumeType::MODEL_PART); bool modifiers_differ = model_volume_list_changed(model_object, model_object_new, ModelVolumeType::PARAMETER_MODIFIER); - bool support_blockers_differ = model_volume_list_changed(model_object, model_object_new, ModelVolumeType::SUPPORT_BLOCKER); - bool support_enforcers_differ = model_volume_list_changed(model_object, model_object_new, ModelVolumeType::SUPPORT_ENFORCER); + bool supports_differ = model_volume_list_changed(model_object, model_object_new, ModelVolumeType::SUPPORT_BLOCKER) || + model_volume_list_changed(model_object, model_object_new, ModelVolumeType::SUPPORT_ENFORCER); if (model_parts_differ || modifiers_differ || model_object.origin_translation != model_object_new.origin_translation || ! model_object.layer_height_profile.timestamp_matches(model_object_new.layer_height_profile) || @@ -858,20 +858,21 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ } // Copy content of the ModelObject including its ID, do not change the parent. model_object.assign_copy(model_object_new); - } else if (support_blockers_differ || support_enforcers_differ || model_custom_supports_data_changed(model_object, model_object_new)) { + } else if (supports_differ || model_custom_supports_data_changed(model_object, model_object_new)) { // First stop background processing before shuffling or deleting the ModelVolumes in the ModelObject's list. - this->call_cancel_callback(); - update_apply_status(false); + if (supports_differ) { + this->call_cancel_callback(); + update_apply_status(false); + } // Invalidate just the supports step. auto range = print_object_status.equal_range(PrintObjectStatus(model_object.id())); for (auto it = range.first; it != range.second; ++ it) update_apply_status(it->print_object->invalidate_step(posSupportMaterial)); - if (support_enforcers_differ || support_blockers_differ) { + if (supports_differ) { // Copy just the support volumes. model_volume_list_update_supports(model_object, model_object_new); } - } - if (model_custom_seam_data_changed(model_object, model_object_new)) { + } else if (model_custom_seam_data_changed(model_object, model_object_new)) { update_apply_status(this->invalidate_step(psGCodeExport)); } if (! model_parts_differ && ! modifiers_differ) { @@ -942,13 +943,20 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ old.emplace_back(&(*it)); } // Generate a list of trafos and XY offsets for instances of a ModelObject - PrintObjectConfig config = PrintObject::object_config_from_model_object(m_default_object_config, *model_object, num_extruders); + // Producing the config for PrintObject on demand, caching it at print_object_last. + const PrintObject *print_object_last = nullptr; + auto print_object_apply_config = [this, &print_object_last, model_object, num_extruders](PrintObject* print_object) { + print_object->config_apply(print_object_last ? + print_object_last->config() : + PrintObject::object_config_from_model_object(m_default_object_config, *model_object, num_extruders)); + print_object_last = print_object; + }; std::vector new_print_instances = print_objects_from_model_object(*model_object); if (old.empty()) { // Simple case, just generate new instances. for (PrintObjectTrafoAndInstances &print_instances : new_print_instances) { PrintObject *print_object = new PrintObject(this, model_object, print_instances.trafo, std::move(print_instances.instances)); - print_object->config_apply(config); + print_object_apply_config(print_object); print_objects_new.emplace_back(print_object); // print_object_status.emplace(PrintObjectStatus(print_object, PrintObjectStatus::New)); new_objects = true; @@ -965,7 +973,7 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ if (it_old == old.end() || ! transform3d_equal((*it_old)->trafo, new_instances.trafo)) { // This is a new instance (or a set of instances with the same trafo). Just add it. PrintObject *print_object = new PrintObject(this, model_object, new_instances.trafo, std::move(new_instances.instances)); - print_object->config_apply(config); + print_object_apply_config(print_object); print_objects_new.emplace_back(print_object); // print_object_status.emplace(PrintObjectStatus(print_object, PrintObjectStatus::New)); new_objects = true; diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index a8eb68521..93d9cfbcf 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -3233,7 +3233,7 @@ PrinterTechnology printer_technology(const ConfigBase &cfg) return ptUnknown; } -void DynamicPrintConfig::normalize() +void DynamicPrintConfig::normalize_fdm() { if (this->has("extruder")) { int extruder = this->option("extruder")->getInt(); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index bb4428ab4..52e3bc38c 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -254,7 +254,7 @@ public: // Overrides ConfigBase::def(). Static configuration definition. Any value stored into this ConfigBase shall have its definition here. const ConfigDef* def() const override { return &print_config_def; } - void normalize(); + void normalize_fdm(); void set_num_extruders(unsigned int num_extruders); @@ -269,14 +269,6 @@ public: { PrintConfigDef::handle_legacy(opt_key, value); } }; -template -void normalize_and_apply_config(CONFIG &dst, const DynamicPrintConfig &src) -{ - DynamicPrintConfig src_normalized(src); - src_normalized.normalize(); - dst.apply(src_normalized, true); -} - class StaticPrintConfig : public StaticConfig { public: @@ -1457,8 +1449,6 @@ private: static uint64_t s_last_timestamp; }; -template void normalize_and_apply_config(CONFIG& dst, const ModelConfig& src) { normalize_and_apply_config(dst, src.get()); } - } // namespace Slic3r // Serialization through the Cereal library diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index c0425ada4..7bf5734bb 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -1544,22 +1544,46 @@ static void clamp_exturder_to_default(ConfigOptionInt &opt, size_t num_extruders PrintObjectConfig PrintObject::object_config_from_model_object(const PrintObjectConfig &default_object_config, const ModelObject &object, size_t num_extruders) { PrintObjectConfig config = default_object_config; - normalize_and_apply_config(config, object.config); + { + DynamicPrintConfig src_normalized(object.config.get()); + src_normalized.normalize_fdm(); + config.apply(src_normalized, true); + } // Clamp invalid extruders to the default extruder (with index 1). clamp_exturder_to_default(config.support_material_extruder, num_extruders); clamp_exturder_to_default(config.support_material_interface_extruder, num_extruders); return config; } +static void apply_to_print_region_config(PrintRegionConfig &out, const DynamicPrintConfig &in) +{ + // 1) Copy the "extruder key to infill_extruder and perimeter_extruder. + std::string sextruder = "extruder"; + auto *opt_extruder = in.opt(sextruder); + if (opt_extruder) { + if (opt_extruder->value != 0) { + out.infill_extruder.value = opt_extruder->value; + out.perimeter_extruder.value = opt_extruder->value; + } + } + // 2) Copy the rest of the values. + for (auto it = in.cbegin(); it != in.cend(); ++ it) + if (it->first != sextruder) { + ConfigOption *my_opt = out.option(it->first, false); + if (my_opt) + my_opt->set(it->second.get()); + } +} + PrintRegionConfig PrintObject::region_config_from_model_volume(const PrintRegionConfig &default_region_config, const DynamicPrintConfig *layer_range_config, const ModelVolume &volume, size_t num_extruders) { PrintRegionConfig config = default_region_config; - normalize_and_apply_config(config, volume.get_object()->config); + apply_to_print_region_config(config, volume.get_object()->config.get()); if (layer_range_config != nullptr) - normalize_and_apply_config(config, *layer_range_config); - normalize_and_apply_config(config, volume.config); + apply_to_print_region_config(config, *layer_range_config); + apply_to_print_region_config(config, volume.config.get()); if (! volume.material_id().empty()) - normalize_and_apply_config(config, volume.material()->config); + apply_to_print_region_config(config, volume.material()->config.get()); // Clamp invalid extruders to the default extruder (with index 1). clamp_exturder_to_default(config.infill_extruder, num_extruders); clamp_exturder_to_default(config.perimeter_extruder, num_extruders); diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index 30d1df6c1..a2d2ff620 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -185,7 +185,6 @@ SLAPrint::ApplyStatus SLAPrint::apply(const Model &model, DynamicPrintConfig con config.option("sla_print_settings_id", true); config.option("sla_material_settings_id", true); config.option("printer_settings_id", true); - config.normalize(); // Collect changes to print config. t_config_option_keys print_diff = m_print_config.diff(config); t_config_option_keys printer_diff = m_printer_config.diff(config); @@ -400,7 +399,7 @@ SLAPrint::ApplyStatus SLAPrint::apply(const Model &model, DynamicPrintConfig con model_object.config.assign_config(model_object_new.config); if (! object_diff.empty() || object_config_changed) { SLAPrintObjectConfig new_config = m_default_object_config; - normalize_and_apply_config(new_config, model_object.config); + new_config.apply(model_object.config.get(), true); if (it_print_object_status != print_object_status.end()) { t_config_option_keys diff = it_print_object_status->print_object->config().diff(new_config); if (! diff.empty()) { @@ -464,9 +463,8 @@ SLAPrint::ApplyStatus SLAPrint::apply(const Model &model, DynamicPrintConfig con print_object->set_instances(std::move(new_instances)); - SLAPrintObjectConfig new_config = m_default_object_config; - normalize_and_apply_config(new_config, model_object.config); - print_object->config_apply(new_config, true); + print_object->config_apply(m_default_object_config, true); + print_object->config_apply(model_object.config.get(), true); print_objects_new.emplace_back(print_object); new_objects = true; } diff --git a/src/libslic3r/SLAPrint.hpp b/src/libslic3r/SLAPrint.hpp index 5fbf8346c..5ca1b2b8e 100644 --- a/src/libslic3r/SLAPrint.hpp +++ b/src/libslic3r/SLAPrint.hpp @@ -261,7 +261,7 @@ protected: SLAPrintObject(SLAPrint* print, ModelObject* model_object); ~SLAPrintObject(); - void config_apply(const ConfigBase &other, bool ignore_nonexistent = false) { this->m_config.apply(other, ignore_nonexistent); } + void config_apply(const ConfigBase &other, bool ignore_nonexistent = false) { m_config.apply(other, ignore_nonexistent); } void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) { this->m_config.apply_only(other, keys, ignore_nonexistent); }