mirror of
https://github.com/QIDITECH/QIDISlicer.git
synced 2026-02-01 16:38:43 +03:00
PRUSA 2.7.0
This commit is contained in:
@@ -17,12 +17,14 @@
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/nowide/cenv.hpp>
|
||||
#include <boost/nowide/cstdio.hpp>
|
||||
#include <boost/nowide/iostream.hpp>
|
||||
#include <boost/nowide/fstream.hpp>
|
||||
#include <boost/property_tree/ini_parser.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <string.h>
|
||||
|
||||
#include <LibBGCode/binarize/binarize.hpp>
|
||||
//FIXME for GCodeFlavor and gcfMarlin (for forward-compatibility conversion)
|
||||
// This is not nice, likely it would be better to pass the ConfigSubstitutionContext to handle_legacy().
|
||||
#include "PrintConfig.hpp"
|
||||
@@ -311,12 +313,9 @@ void ConfigDef::finalize()
|
||||
if (def.type == coEnum) {
|
||||
assert(def.enum_def);
|
||||
assert(def.enum_def->is_valid_closed_enum());
|
||||
assert(def.gui_type != ConfigOptionDef::GUIType::i_enum_open &&
|
||||
def.gui_type != ConfigOptionDef::GUIType::f_enum_open &&
|
||||
def.gui_type != ConfigOptionDef::GUIType::select_open);
|
||||
assert(! def.is_gui_type_enum_open());
|
||||
def.enum_def->finalize_closed_enum();
|
||||
} else if (def.gui_type == ConfigOptionDef::GUIType::i_enum_open || def.gui_type == ConfigOptionDef::GUIType::f_enum_open ||
|
||||
def.gui_type == ConfigOptionDef::GUIType::select_open) {
|
||||
} else if (def.is_gui_type_enum_open()) {
|
||||
assert(def.enum_def);
|
||||
assert(def.enum_def->is_valid_open_enum());
|
||||
assert(def.gui_type != ConfigOptionDef::GUIType::i_enum_open || def.type == coInt || def.type == coInts);
|
||||
@@ -720,11 +719,37 @@ void ConfigBase::setenv_() const
|
||||
}
|
||||
}
|
||||
|
||||
ConfigSubstitutions ConfigBase::load(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
ConfigSubstitutions ConfigBase::load(const std::string& filename, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
{
|
||||
return is_gcode_file(file) ?
|
||||
this->load_from_gcode_file(file, compatibility_rule) :
|
||||
this->load_from_ini(file, compatibility_rule);
|
||||
enum class EFileType
|
||||
{
|
||||
Ini,
|
||||
AsciiGCode,
|
||||
BinaryGCode
|
||||
};
|
||||
|
||||
EFileType file_type;
|
||||
|
||||
if (is_gcode_file(filename)) {
|
||||
FILE* file = boost::nowide::fopen(filename.c_str(), "rb");
|
||||
if (file == nullptr)
|
||||
throw Slic3r::RuntimeError(format("Error opening file %1%", filename));
|
||||
|
||||
std::vector<std::byte> cs_buffer(65536);
|
||||
using namespace bgcode::core;
|
||||
file_type = (is_valid_binary_gcode(*file, true, cs_buffer.data(), cs_buffer.size()) == EResult::Success) ? EFileType::BinaryGCode : EFileType::AsciiGCode;
|
||||
fclose(file);
|
||||
}
|
||||
else
|
||||
file_type = EFileType::Ini;
|
||||
|
||||
switch (file_type)
|
||||
{
|
||||
case EFileType::Ini: { return this->load_from_ini(filename, compatibility_rule); }
|
||||
case EFileType::AsciiGCode: { return this->load_from_gcode_file(filename, compatibility_rule);}
|
||||
case EFileType::BinaryGCode: { return this->load_from_binary_gcode_file(filename, compatibility_rule);}
|
||||
default: { throw Slic3r::RuntimeError(format("Invalid file %1%", filename)); }
|
||||
}
|
||||
}
|
||||
|
||||
ConfigSubstitutions ConfigBase::load_from_ini(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
@@ -858,6 +883,7 @@ size_t ConfigBase::load_from_gcode_string_legacy(ConfigBase& config, const char*
|
||||
end = start;
|
||||
}
|
||||
|
||||
config.handle_legacy_composite();
|
||||
return num_key_value_pairs;
|
||||
}
|
||||
|
||||
@@ -928,10 +954,10 @@ private:
|
||||
};
|
||||
|
||||
// Load the config keys from the tail of a G-code file.
|
||||
ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &filename, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
{
|
||||
// Read a 64k block from the end of the G-code.
|
||||
boost::nowide::ifstream ifs(file, std::ifstream::binary);
|
||||
boost::nowide::ifstream ifs(filename, std::ifstream::binary);
|
||||
// Look for Slic3r or QIDISlicer header.
|
||||
// Look for the header across the whole file as the G-code may have been extended at the start by a post-processing script or the user.
|
||||
bool has_delimiters = false;
|
||||
@@ -986,7 +1012,7 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
||||
break;
|
||||
}
|
||||
if (! end_found)
|
||||
throw Slic3r::RuntimeError(format("Configuration block closing tag \"; qidislicer_config = end\" not found when reading %1%", file));
|
||||
throw Slic3r::RuntimeError(format("Configuration block closing tag \"; qidislicer_config = end\" not found when reading %1%", filename));
|
||||
std::string key, value;
|
||||
while (reader.getline(line)) {
|
||||
if (line == "; qidislicer_config = begin") {
|
||||
@@ -1009,7 +1035,8 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
||||
}
|
||||
}
|
||||
if (! begin_found)
|
||||
throw Slic3r::RuntimeError(format("Configuration block opening tag \"; qidislicer_config = begin\" not found when reading %1%", file));
|
||||
throw Slic3r::RuntimeError(
|
||||
format("Configuration block opening tag \"; qidislicer_config = begin\" not found when reading %1%", filename));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1026,7 +1053,55 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
||||
}
|
||||
|
||||
if (key_value_pairs < 80)
|
||||
throw Slic3r::RuntimeError(format("Suspiciously low number of configuration values extracted from %1%: %2%", file, key_value_pairs));
|
||||
throw Slic3r::RuntimeError(format("Suspiciously low number of configuration values extracted from %1%: %2%", filename, key_value_pairs));
|
||||
|
||||
// Do legacy conversion on a completely loaded dictionary.
|
||||
// Perform composite conversions, for example merging multiple keys into one key.
|
||||
this->handle_legacy_composite();
|
||||
return std::move(substitutions_ctxt.substitutions);
|
||||
}
|
||||
|
||||
ConfigSubstitutions ConfigBase::load_from_binary_gcode_file(const std::string& filename, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||
{
|
||||
ConfigSubstitutionContext substitutions_ctxt(compatibility_rule);
|
||||
|
||||
FilePtr file{ boost::nowide::fopen(filename.c_str(), "rb") };
|
||||
if (file.f == nullptr)
|
||||
throw Slic3r::RuntimeError(format("Error opening file %1%", filename));
|
||||
|
||||
using namespace bgcode::core;
|
||||
using namespace bgcode::binarize;
|
||||
std::vector<std::byte> cs_buffer(65536);
|
||||
EResult res = is_valid_binary_gcode(*file.f, true, cs_buffer.data(), cs_buffer.size());
|
||||
if (res != EResult::Success)
|
||||
throw Slic3r::RuntimeError(format("File %1% does not contain a valid binary gcode\nError: %2%", filename,
|
||||
std::string(translate_result(res))));
|
||||
|
||||
FileHeader file_header;
|
||||
res = read_header(*file.f, file_header, nullptr);
|
||||
if (res != EResult::Success)
|
||||
throw Slic3r::RuntimeError(format("Error while reading file %1%: %2%", filename, std::string(translate_result(res))));
|
||||
|
||||
// searches for config block
|
||||
BlockHeader block_header;
|
||||
res = read_next_block_header(*file.f, file_header, block_header, EBlockType::SlicerMetadata, cs_buffer.data(), cs_buffer.size());
|
||||
if (res != EResult::Success)
|
||||
throw Slic3r::RuntimeError(format("Error while reading file %1%: %2%", filename, std::string(translate_result(res))));
|
||||
if ((EBlockType)block_header.type != EBlockType::SlicerMetadata)
|
||||
throw Slic3r::RuntimeError(format("Unable to find slicer metadata block in file %1%", filename));
|
||||
SlicerMetadataBlock slicer_metadata_block;
|
||||
res = slicer_metadata_block.read_data(*file.f, file_header, block_header);
|
||||
if (res != EResult::Success)
|
||||
throw Slic3r::RuntimeError(format("Error while reading file %1%: %2%", filename, std::string(translate_result(res))));
|
||||
|
||||
// extracts data from block
|
||||
for (const auto& [key, value] : slicer_metadata_block.raw_data) {
|
||||
this->set_deserialize(key, value, substitutions_ctxt);
|
||||
}
|
||||
|
||||
// Do legacy conversion on a completely loaded dictionary.
|
||||
// Perform composite conversions, for example merging multiple keys into one key.
|
||||
this->handle_legacy_composite();
|
||||
return std::move(substitutions_ctxt.substitutions);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user