diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index ab58a43aa..2d4780d0e 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -740,7 +740,11 @@ ConfigSubstitutions ConfigBase::load(const boost::property_tree::ptree &tree, Fo } // Load the config keys from the given string. -static inline size_t load_from_gcode_string_legacy(ConfigBase &config, const char *str, ConfigSubstitutionContext &substitutions) +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT +size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions) +#else +static inline size_t load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions) +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT { if (str == nullptr) return 0; diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index 6439e4632..dd121c90a 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -2015,6 +2015,10 @@ public: // Set all the nullable values to nils. void null_nullables(); +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + static size_t load_from_gcode_string_legacy(ConfigBase& config, const char* str, ConfigSubstitutionContext& substitutions); +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + private: // Set a configuration value from a string. bool set_deserialize_raw(const t_config_option_key& opt_key_src, const std::string& value, ConfigSubstitutionContext& substitutions, bool append); diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 55c41f8fe..ea46cd3a5 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -746,6 +746,9 @@ const std::vector> GCodeProces { EProducer::PrusaSlicer, "generated by PrusaSlicer" }, { EProducer::Slic3rPE, "generated by Slic3r Prusa Edition" }, { EProducer::Slic3r, "generated by Slic3r" }, +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + { EProducer::SuperSlicer, "generated by SuperSlicer" }, +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT { EProducer::Cura, "Cura_SteamEngine" }, { EProducer::Simplify3D, "G-Code generated by Simplify3D(R)" }, { EProducer::CraftWare, "CraftWare" }, @@ -1227,6 +1230,10 @@ void GCodeProcessor::process_file(const std::string& filename, std::function> GCodeProcessor::get_roles_time(Prin return ret; } +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT +ConfigSubstitutions load_from_superslicer_gcode_file(const std::string& filename, DynamicPrintConfig& config, ForwardCompatibilitySubstitutionRule compatibility_rule) +{ + // for reference, see: ConfigBase::load_from_gcode_file() + + boost::nowide::ifstream ifs(filename); + + auto header_end_pos = ifs.tellg(); + ConfigSubstitutionContext substitutions_ctxt(compatibility_rule); + size_t key_value_pairs = 0; + + ifs.seekg(0, ifs.end); + auto file_length = ifs.tellg(); + auto data_length = std::min(65535, file_length - header_end_pos); + ifs.seekg(file_length - data_length, ifs.beg); + std::vector data(size_t(data_length) + 1, 0); + ifs.read(data.data(), data_length); + ifs.close(); + key_value_pairs = ConfigBase::load_from_gcode_string_legacy(config, data.data(), substitutions_ctxt); + + if (key_value_pairs < 80) + throw Slic3r::RuntimeError(format("Suspiciously low number of configuration values extracted from %1%: %2%", filename, key_value_pairs)); + + return std::move(substitutions_ctxt.substitutions); +} + +void GCodeProcessor::apply_config_superslicer(const std::string& filename) +{ + DynamicPrintConfig config; + config.apply(FullPrintConfig::defaults()); + load_from_superslicer_gcode_file(filename, config, ForwardCompatibilitySubstitutionRule::EnableSilent); + apply_config(config); +} +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + std::vector GCodeProcessor::get_layers_time(PrintEstimatedStatistics::ETimeMode mode) const { return (mode < PrintEstimatedStatistics::ETimeMode::Count) ? @@ -1849,6 +1891,9 @@ bool GCodeProcessor::process_producers_tags(const std::string_view comment) { case EProducer::Slic3rPE: case EProducer::Slic3r: +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + case EProducer::SuperSlicer: +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT case EProducer::PrusaSlicer: { return process_prusaslicer_tags(comment); } case EProducer::Cura: { return process_cura_tags(comment); } case EProducer::Simplify3D: { return process_simplify3d_tags(comment); } diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index 0639567ea..fce888233 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -543,6 +543,9 @@ namespace Slic3r { PrusaSlicer, Slic3rPE, Slic3r, +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + SuperSlicer, +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT Cura, Simplify3D, CraftWare, @@ -599,6 +602,9 @@ namespace Slic3r { private: void apply_config(const DynamicPrintConfig& config); void apply_config_simplify3d(const std::string& filename); +#if ENABLE_FIX_SUPERSLICER_GCODE_IMPORT + void apply_config_superslicer(const std::string& filename); +#endif // ENABLE_FIX_SUPERSLICER_GCODE_IMPORT void process_gcode_line(const GCodeReader::GCodeLine& line, bool producers_enabled); // Process tags embedded into comments diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index e32f3cde7..350f6aedc 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -64,4 +64,13 @@ #define ENABLE_FIX_SEAMS_SYNCH (1 && ENABLE_2_4_0_ALPHA2) +//==================== +// 2.4.0.alpha3 techs +//==================== +#define ENABLE_2_4_0_ALPHA3 1 + +// Enable fixing loading of gcode files generated with SuperSlicer in GCodeViewer +#define ENABLE_FIX_SUPERSLICER_GCODE_IMPORT (1 && ENABLE_2_4_0_ALPHA3) + + #endif // _prusaslicer_technologies_h_