mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-03 02:00:40 +08:00
Added import of config from binary gcode files
This commit is contained in:
parent
87f6fed274
commit
eb8d01888d
@ -23,6 +23,10 @@
|
|||||||
#include <boost/format.hpp>
|
#include <boost/format.hpp>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#if ENABLE_BINARIZED_GCODE
|
||||||
|
#include <LibBGCode/base/base.hpp>
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
|
|
||||||
//FIXME for GCodeFlavor and gcfMarlin (for forward-compatibility conversion)
|
//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().
|
// This is not nice, likely it would be better to pass the ConfigSubstitutionContext to handle_legacy().
|
||||||
#include "PrintConfig.hpp"
|
#include "PrintConfig.hpp"
|
||||||
@ -720,11 +724,41 @@ 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) ?
|
#if ENABLE_BINARIZED_GCODE
|
||||||
this->load_from_gcode_file(file, compatibility_rule) :
|
enum class EFileType
|
||||||
this->load_from_ini(file, compatibility_rule);
|
{
|
||||||
|
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("Error opening the file: " + filename + "\n");
|
||||||
|
|
||||||
|
file_type = (bgcode::core::is_valid_binary_gcode(*file, true) == bgcode::core::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("Invalid file: " + filename + "\n"); }
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return is_gcode_file(filename) ?
|
||||||
|
this->load_from_gcode_file(filename, compatibility_rule) :
|
||||||
|
this->load_from_ini(filename, compatibility_rule);
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigSubstitutions ConfigBase::load_from_ini(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
ConfigSubstitutions ConfigBase::load_from_ini(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||||
@ -925,10 +959,10 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Load the config keys from the tail of a G-code file.
|
// 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.
|
// 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 PrusaSlicer header.
|
// Look for Slic3r or PrusaSlicer 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.
|
// 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;
|
bool has_delimiters = false;
|
||||||
@ -983,7 +1017,7 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (! end_found)
|
if (! end_found)
|
||||||
throw Slic3r::RuntimeError(format("Configuration block closing tag \"; prusaslicer_config = end\" not found when reading %1%", file));
|
throw Slic3r::RuntimeError(format("Configuration block closing tag \"; prusaslicer_config = end\" not found when reading %1%", filename));
|
||||||
std::string key, value;
|
std::string key, value;
|
||||||
while (reader.getline(line)) {
|
while (reader.getline(line)) {
|
||||||
if (line == "; prusaslicer_config = begin") {
|
if (line == "; prusaslicer_config = begin") {
|
||||||
@ -1006,7 +1040,7 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (! begin_found)
|
if (! begin_found)
|
||||||
throw Slic3r::RuntimeError(format("Configuration block opening tag \"; prusaslicer_config = begin\" not found when reading %1%", file));
|
throw Slic3r::RuntimeError(format("Configuration block opening tag \"; prusaslicer_config = begin\" not found when reading %1%", filename));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1023,10 +1057,52 @@ ConfigSubstitutions ConfigBase::load_from_gcode_file(const std::string &file, Fo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (key_value_pairs < 80)
|
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));
|
||||||
return std::move(substitutions_ctxt.substitutions);
|
return std::move(substitutions_ctxt.substitutions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLE_BINARIZED_GCODE
|
||||||
|
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 the file: %1%", filename));
|
||||||
|
|
||||||
|
bgcode::core::EResult res = bgcode::core::is_valid_binary_gcode(*file.f);
|
||||||
|
if (res != bgcode::core::EResult::Success)
|
||||||
|
throw Slic3r::RuntimeError(format("The selected file is not a valid binary gcode.\nError: %1%",
|
||||||
|
std::string(bgcode::core::translate_result(res))));
|
||||||
|
|
||||||
|
fseek(file.f, 0, SEEK_END);
|
||||||
|
const long file_size = ftell(file.f);
|
||||||
|
rewind(file.f);
|
||||||
|
|
||||||
|
bgcode::core::FileHeader file_header;
|
||||||
|
res = bgcode::core::read_header(*file.f, file_header, nullptr);
|
||||||
|
if (res != bgcode::core::EResult::Success)
|
||||||
|
throw Slic3r::RuntimeError(format("Error while reading file '%1%': %2%", filename, std::string(bgcode::core::translate_result(res))));
|
||||||
|
|
||||||
|
bgcode::core::BlockHeader block_header;
|
||||||
|
res = read_next_block_header(*file.f, file_header, block_header, bgcode::core::EBlockType::SlicerMetadata, true);
|
||||||
|
if (res != bgcode::core::EResult::Success)
|
||||||
|
throw Slic3r::RuntimeError(format("Error while reading file '%1%': %2%", filename, std::string(bgcode::core::translate_result(res))));
|
||||||
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::SlicerMetadata)
|
||||||
|
throw Slic3r::RuntimeError(format("Unable to find slicer metadata block in file: '%1%'", filename));
|
||||||
|
bgcode::base::SlicerMetadataBlock slicer_metadata_block;
|
||||||
|
res = slicer_metadata_block.read_data(*file.f, file_header, block_header);
|
||||||
|
if (res != bgcode::core::EResult::Success)
|
||||||
|
throw Slic3r::RuntimeError(format("Error while reading file '%1%': %2%", filename, std::string(bgcode::core::translate_result(res))));
|
||||||
|
|
||||||
|
for (const auto& [key, value] : slicer_metadata_block.raw_data) {
|
||||||
|
this->set_deserialize(key, value, substitutions_ctxt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(substitutions_ctxt.substitutions);
|
||||||
|
}
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
|
|
||||||
void ConfigBase::save(const std::string &file) const
|
void ConfigBase::save(const std::string &file) const
|
||||||
{
|
{
|
||||||
boost::nowide::ofstream c;
|
boost::nowide::ofstream c;
|
||||||
|
@ -2303,7 +2303,10 @@ public:
|
|||||||
// Loading a "will be one day a legacy format" of configuration stored into 3MF or AMF.
|
// Loading a "will be one day a legacy format" of configuration stored into 3MF or AMF.
|
||||||
// Accepts the same data as load_from_ini_string(), only with each configuration line possibly prefixed with a semicolon (G-code comment).
|
// Accepts the same data as load_from_ini_string(), only with each configuration line possibly prefixed with a semicolon (G-code comment).
|
||||||
ConfigSubstitutions load_from_ini_string_commented(std::string &&data, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
ConfigSubstitutions load_from_ini_string_commented(std::string &&data, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
||||||
ConfigSubstitutions load_from_gcode_file(const std::string &file, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
ConfigSubstitutions load_from_gcode_file(const std::string &filename, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
||||||
|
#if ENABLE_BINARIZED_GCODE
|
||||||
|
ConfigSubstitutions load_from_binary_gcode_file(const std::string& filename, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
ConfigSubstitutions load(const boost::property_tree::ptree &tree, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
ConfigSubstitutions load(const boost::property_tree::ptree &tree, ForwardCompatibilitySubstitutionRule compatibility_rule);
|
||||||
void save(const std::string &file) const;
|
void save(const std::string &file) const;
|
||||||
|
|
||||||
|
@ -1122,34 +1122,23 @@ void GCodeProcessor::process_ascii_file(const std::string& filename, std::functi
|
|||||||
#if ENABLE_BINARIZED_GCODE
|
#if ENABLE_BINARIZED_GCODE
|
||||||
void GCodeProcessor::process_binary_file(const std::string& filename, std::function<void()> cancel_callback)
|
void GCodeProcessor::process_binary_file(const std::string& filename, std::function<void()> cancel_callback)
|
||||||
{
|
{
|
||||||
class ScopedFile
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit ScopedFile(FILE* file) : m_file(file) {}
|
|
||||||
~ScopedFile() { if (m_file != nullptr) fclose(m_file); }
|
|
||||||
private:
|
|
||||||
FILE* m_file{ nullptr };
|
|
||||||
};
|
|
||||||
|
|
||||||
#if ENABLE_GCODE_VIEWER_STATISTICS
|
#if ENABLE_GCODE_VIEWER_STATISTICS
|
||||||
m_start_time = std::chrono::high_resolution_clock::now();
|
m_start_time = std::chrono::high_resolution_clock::now();
|
||||||
#endif // ENABLE_GCODE_VIEWER_STATISTICS
|
#endif // ENABLE_GCODE_VIEWER_STATISTICS
|
||||||
|
|
||||||
FILE* file = boost::nowide::fopen(filename.c_str(), "rb");
|
FilePtr file{ boost::nowide::fopen(filename.c_str(), "rb") };
|
||||||
if (file == nullptr)
|
if (file.f == nullptr)
|
||||||
throw Slic3r::RuntimeError("Unable to open file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to open file: " + filename + "\n");
|
||||||
|
|
||||||
fseek(file, 0, SEEK_END);
|
fseek(file.f, 0, SEEK_END);
|
||||||
const long file_size = ftell(file);
|
const long file_size = ftell(file.f);
|
||||||
rewind(file);
|
rewind(file.f);
|
||||||
|
|
||||||
ScopedFile scoped_file(file);
|
|
||||||
|
|
||||||
bgcode::core::set_checksum_max_cache_size(1024);
|
bgcode::core::set_checksum_max_cache_size(1024);
|
||||||
|
|
||||||
// read file header
|
// read file header
|
||||||
bgcode::core::FileHeader file_header;
|
bgcode::core::FileHeader file_header;
|
||||||
bgcode::core::EResult res = bgcode::core::read_header(*file, file_header, nullptr);
|
bgcode::core::EResult res = bgcode::core::read_header(*file.f, file_header, nullptr);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("File: " + filename + "does not contain a valid binary gcode\n Error: " +
|
throw Slic3r::RuntimeError("File: " + filename + "does not contain a valid binary gcode\n Error: " +
|
||||||
std::string(bgcode::core::translate_result(res)) + "\n");
|
std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
@ -1158,15 +1147,15 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
|
|
||||||
// read file metadata block
|
// read file metadata block
|
||||||
bgcode::core::BlockHeader block_header;
|
bgcode::core::BlockHeader block_header;
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::FileMetadata)
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::FileMetadata)
|
||||||
throw Slic3r::RuntimeError("Unable to find file metadata block in file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to find file metadata block in file: " + filename + "\n");
|
||||||
bgcode::base::FileMetadataBlock file_metadata_block;
|
bgcode::base::FileMetadataBlock file_metadata_block;
|
||||||
res = file_metadata_block.read_data(*file, file_header, block_header);
|
res = file_metadata_block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
auto producer_it = std::find_if(file_metadata_block.raw_data.begin(), file_metadata_block.raw_data.end(),
|
auto producer_it = std::find_if(file_metadata_block.raw_data.begin(), file_metadata_block.raw_data.end(),
|
||||||
[](const std::pair<std::string, std::string>& item) { return item.first == "Producer"; });
|
[](const std::pair<std::string, std::string>& item) { return item.first == "Producer"; });
|
||||||
if (producer_it != file_metadata_block.raw_data.end() && boost::starts_with(producer_it->second, std::string(SLIC3R_APP_NAME)))
|
if (producer_it != file_metadata_block.raw_data.end() && boost::starts_with(producer_it->second, std::string(SLIC3R_APP_NAME)))
|
||||||
@ -1175,15 +1164,15 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
m_producer = EProducer::Unknown;
|
m_producer = EProducer::Unknown;
|
||||||
|
|
||||||
// read printer metadata block
|
// read printer metadata block
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::PrinterMetadata)
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::PrinterMetadata)
|
||||||
throw Slic3r::RuntimeError("Unable to find printer metadata block in file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to find printer metadata block in file: " + filename + "\n");
|
||||||
bgcode::base::PrinterMetadataBlock printer_metadata_block;
|
bgcode::base::PrinterMetadataBlock printer_metadata_block;
|
||||||
res = printer_metadata_block.read_data(*file, file_header, block_header);
|
res = printer_metadata_block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
#if ENABLE_BINARIZED_GCODE_DEBUG
|
#if ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
OutputDebugStringA("Printer metadata:\n");
|
OutputDebugStringA("Printer metadata:\n");
|
||||||
for (const auto& [key, value] : printer_metadata_block.raw_data) {
|
for (const auto& [key, value] : printer_metadata_block.raw_data) {
|
||||||
@ -1195,15 +1184,15 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
|
|
||||||
// read thumbnail blocks
|
// read thumbnail blocks
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
|
|
||||||
while ((bgcode::core::EBlockType)block_header.type == bgcode::core::EBlockType::Thumbnail) {
|
while ((bgcode::core::EBlockType)block_header.type == bgcode::core::EBlockType::Thumbnail) {
|
||||||
bgcode::base::ThumbnailBlock thumbnail_block;
|
bgcode::base::ThumbnailBlock thumbnail_block;
|
||||||
res = thumbnail_block.read_data(*file, file_header, block_header);
|
res = thumbnail_block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
#if ENABLE_BINARIZED_GCODE_DEBUG
|
#if ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
if (thumbnail_block.data.size() > 0) {
|
if (thumbnail_block.data.size() > 0) {
|
||||||
auto format_filename = [](const std::string& stem, const bgcode::base::ThumbnailBlock& block) {
|
auto format_filename = [](const std::string& stem, const bgcode::base::ThumbnailBlock& block) {
|
||||||
@ -1228,18 +1217,18 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
}
|
}
|
||||||
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
|
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// read print metadata block
|
// read print metadata block
|
||||||
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::PrintMetadata)
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::PrintMetadata)
|
||||||
throw Slic3r::RuntimeError("Unable to find print metadata block in file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to find print metadata block in file: " + filename + "\n");
|
||||||
bgcode::base::PrintMetadataBlock print_metadata_block;
|
bgcode::base::PrintMetadataBlock print_metadata_block;
|
||||||
res = print_metadata_block.read_data(*file, file_header, block_header);
|
res = print_metadata_block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
#if ENABLE_BINARIZED_GCODE_DEBUG
|
#if ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
OutputDebugStringA("Print metadata:\n");
|
OutputDebugStringA("Print metadata:\n");
|
||||||
for (const auto& [key, value] : print_metadata_block.raw_data) {
|
for (const auto& [key, value] : print_metadata_block.raw_data) {
|
||||||
@ -1251,15 +1240,15 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
#endif // ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
|
|
||||||
// read slicer metadata block
|
// read slicer metadata block
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::SlicerMetadata)
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::SlicerMetadata)
|
||||||
throw Slic3r::RuntimeError("Unable to find slicer metadata block in file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to find slicer metadata block in file: " + filename + "\n");
|
||||||
bgcode::base::SlicerMetadataBlock slicer_metadata_block;
|
bgcode::base::SlicerMetadataBlock slicer_metadata_block;
|
||||||
res = slicer_metadata_block.read_data(*file, file_header, block_header);
|
res = slicer_metadata_block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
#if ENABLE_BINARIZED_GCODE_DEBUG
|
#if ENABLE_BINARIZED_GCODE_DEBUG
|
||||||
OutputDebugStringA("Slicer metadata:\n");
|
OutputDebugStringA("Slicer metadata:\n");
|
||||||
for (const auto& [key, value] : slicer_metadata_block.raw_data) {
|
for (const auto& [key, value] : slicer_metadata_block.raw_data) {
|
||||||
@ -1286,16 +1275,16 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
initialize_result_moves();
|
initialize_result_moves();
|
||||||
|
|
||||||
// read gcodes block
|
// read gcodes block
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::GCode)
|
if ((bgcode::core::EBlockType)block_header.type != bgcode::core::EBlockType::GCode)
|
||||||
throw Slic3r::RuntimeError("Unable to find gcode block in file: " + filename + "\n");
|
throw Slic3r::RuntimeError("Unable to find gcode block in file: " + filename + "\n");
|
||||||
while ((bgcode::core::EBlockType)block_header.type == bgcode::core::EBlockType::GCode) {
|
while ((bgcode::core::EBlockType)block_header.type == bgcode::core::EBlockType::GCode) {
|
||||||
bgcode::base::GCodeBlock block;
|
bgcode::base::GCodeBlock block;
|
||||||
res = block.read_data(*file, file_header, block_header);
|
res = block.read_data(*file.f, file_header, block_header);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
|
|
||||||
// TODO: Update m_result.lines_ends
|
// TODO: Update m_result.lines_ends
|
||||||
|
|
||||||
@ -1303,12 +1292,12 @@ void GCodeProcessor::process_binary_file(const std::string& filename, std::funct
|
|||||||
this->process_gcode_line(line, true);
|
this->process_gcode_line(line, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ftell(file) == file_size)
|
if (ftell(file.f) == file_size)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
res = bgcode::core::read_next_block_header(*file, file_header, block_header, verify_checksum);
|
res = bgcode::core::read_next_block_header(*file.f, file_header, block_header, verify_checksum);
|
||||||
if (res != bgcode::core::EResult::Success)
|
if (res != bgcode::core::EResult::Success)
|
||||||
throw Slic3r::RuntimeError("Error while reading file: " + filename + ": " + std::string(bgcode::core::translate_result(res)) + "\n");
|
throw Slic3r::RuntimeError("Error while reading file '" + filename + "': " + std::string(bgcode::core::translate_result(res)) + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't post-process the G-code to update time stamps.
|
// Don't post-process the G-code to update time stamps.
|
||||||
|
@ -22,6 +22,9 @@
|
|||||||
#include <boost/locale.hpp>
|
#include <boost/locale.hpp>
|
||||||
#include <boost/log/trivial.hpp>
|
#include <boost/log/trivial.hpp>
|
||||||
|
|
||||||
|
#if ENABLE_BINARIZED_GCODE
|
||||||
|
#include <LibBGCode/core/core.hpp>
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
|
|
||||||
// Store the print/filament/printer presets into a "presets" subdirectory of the Slic3rPE config dir.
|
// Store the print/filament/printer presets into a "presets" subdirectory of the Slic3rPE config dir.
|
||||||
// This breaks compatibility with the upstream Slic3r if the --datadir is used to switch between the two versions.
|
// This breaks compatibility with the upstream Slic3r if the --datadir is used to switch between the two versions.
|
||||||
@ -875,6 +878,23 @@ DynamicPrintConfig PresetBundle::full_sla_config() const
|
|||||||
// If the file is loaded successfully, its print / filament / printer profiles will be activated.
|
// If the file is loaded successfully, its print / filament / printer profiles will be activated.
|
||||||
ConfigSubstitutions PresetBundle::load_config_file(const std::string &path, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
ConfigSubstitutions PresetBundle::load_config_file(const std::string &path, ForwardCompatibilitySubstitutionRule compatibility_rule)
|
||||||
{
|
{
|
||||||
|
#if ENABLE_BINARIZED_GCODE
|
||||||
|
if (is_gcode_file(path)) {
|
||||||
|
FILE* file = boost::nowide::fopen(path.c_str(), "rb");
|
||||||
|
if (file == nullptr)
|
||||||
|
throw Slic3r::RuntimeError("Error opening the file: " + path + "\n");
|
||||||
|
const bool is_binary = bgcode::core::is_valid_binary_gcode(*file, true) == bgcode::core::EResult::Success;
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
DynamicPrintConfig config;
|
||||||
|
config.apply(FullPrintConfig::defaults());
|
||||||
|
ConfigSubstitutions config_substitutions = is_binary ? config.load_from_binary_gcode_file(path, compatibility_rule) :
|
||||||
|
config.load_from_gcode_file(path, compatibility_rule);
|
||||||
|
Preset::normalize(config);
|
||||||
|
load_config_file_config(path, true, std::move(config));
|
||||||
|
return config_substitutions;
|
||||||
|
}
|
||||||
|
#else
|
||||||
if (is_gcode_file(path)) {
|
if (is_gcode_file(path)) {
|
||||||
DynamicPrintConfig config;
|
DynamicPrintConfig config;
|
||||||
config.apply(FullPrintConfig::defaults());
|
config.apply(FullPrintConfig::defaults());
|
||||||
@ -883,6 +903,7 @@ ConfigSubstitutions PresetBundle::load_config_file(const std::string &path, Forw
|
|||||||
load_config_file_config(path, true, std::move(config));
|
load_config_file_config(path, true, std::move(config));
|
||||||
return config_substitutions;
|
return config_substitutions;
|
||||||
}
|
}
|
||||||
|
#endif // ENABLE_BINARIZED_GCODE
|
||||||
|
|
||||||
// 1) Try to load the config file into a boost property tree.
|
// 1) Try to load the config file into a boost property tree.
|
||||||
boost::property_tree::ptree tree;
|
boost::property_tree::ptree tree;
|
||||||
|
@ -5435,16 +5435,6 @@ void Plater::reload_gcode_from_disk()
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if ENABLE_BINARIZED_GCODE
|
#if ENABLE_BINARIZED_GCODE
|
||||||
class ScopedFile
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit ScopedFile(FILE* file) : m_file(file) {}
|
|
||||||
~ScopedFile() { if (m_file != nullptr) fclose(m_file); }
|
|
||||||
void unscope() { m_file = nullptr; }
|
|
||||||
private:
|
|
||||||
FILE* m_file{ nullptr };
|
|
||||||
};
|
|
||||||
|
|
||||||
void Plater::convert_gcode_to_ascii()
|
void Plater::convert_gcode_to_ascii()
|
||||||
{
|
{
|
||||||
// Ask user for a gcode file name.
|
// Ask user for a gcode file name.
|
||||||
@ -5452,36 +5442,33 @@ void Plater::convert_gcode_to_ascii()
|
|||||||
wxGetApp().load_gcode(this, input_file);
|
wxGetApp().load_gcode(this, input_file);
|
||||||
|
|
||||||
// Open source file
|
// Open source file
|
||||||
FILE* in_file = boost::nowide::fopen(into_u8(input_file).c_str(), "rb");
|
FilePtr in_file{ boost::nowide::fopen(into_u8(input_file).c_str(), "rb") };
|
||||||
if (in_file == nullptr) {
|
if (in_file.f == nullptr) {
|
||||||
MessageDialog msg_dlg(this, _L("Unable to open the selected file."), _L("Error"), wxICON_ERROR | wxOK);
|
MessageDialog msg_dlg(this, _L("Unable to open the selected file."), _L("Error"), wxICON_ERROR | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ScopedFile scoped_in_file(in_file);
|
|
||||||
|
|
||||||
// Set out filename
|
// Set out filename
|
||||||
boost::filesystem::path path(into_u8(input_file));
|
boost::filesystem::path path(into_u8(input_file));
|
||||||
const std::string output_file = path.parent_path().string() + "/" + path.stem().string() + "_ascii" + path.extension().string();
|
const std::string output_file = path.parent_path().string() + "/" + path.stem().string() + "_ascii" + path.extension().string();
|
||||||
|
|
||||||
// Open destination file
|
// Open destination file
|
||||||
FILE* out_file = boost::nowide::fopen(output_file.c_str(), "wb");
|
FilePtr out_file{ boost::nowide::fopen(output_file.c_str(), "wb") };
|
||||||
if (out_file == nullptr) {
|
if (out_file.f == nullptr) {
|
||||||
MessageDialog msg_dlg(this, _L("Unable to open output file."), _L("Error"), wxICON_ERROR | wxOK);
|
MessageDialog msg_dlg(this, _L("Unable to open output file."), _L("Error"), wxICON_ERROR | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ScopedFile scoped_out_file(out_file);
|
|
||||||
|
|
||||||
// Perform conversion
|
// Perform conversion
|
||||||
{
|
{
|
||||||
wxBusyCursor busy;
|
wxBusyCursor busy;
|
||||||
bgcode::core::EResult res = bgcode::convert::from_binary_to_ascii(*in_file, *out_file, true);
|
bgcode::core::EResult res = bgcode::convert::from_binary_to_ascii(*in_file.f, *out_file.f, true);
|
||||||
if (res != bgcode::core::EResult::Success) {
|
if (res != bgcode::core::EResult::Success) {
|
||||||
MessageDialog msg_dlg(this, _L(std::string(bgcode::core::translate_result(res))), _L("Error converting gcode file"), wxICON_INFORMATION | wxOK);
|
MessageDialog msg_dlg(this, _L(std::string(bgcode::core::translate_result(res))), _L("Error converting gcode file"), wxICON_INFORMATION | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
scoped_out_file.unscope();
|
out_file.close();
|
||||||
fclose(out_file);
|
|
||||||
boost::nowide::remove(output_file.c_str());
|
boost::nowide::remove(output_file.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -5498,37 +5485,34 @@ void Plater::convert_gcode_to_binary()
|
|||||||
wxGetApp().load_gcode(this, input_file);
|
wxGetApp().load_gcode(this, input_file);
|
||||||
|
|
||||||
// Open source file
|
// Open source file
|
||||||
FILE* in_file = boost::nowide::fopen(into_u8(input_file).c_str(), "rb");
|
FilePtr in_file{ boost::nowide::fopen(into_u8(input_file).c_str(), "rb") };
|
||||||
if (in_file == nullptr) {
|
if (in_file.f == nullptr) {
|
||||||
MessageDialog msg_dlg(this, _L("Unable to open the selected file."), _L("Error"), wxICON_ERROR | wxOK);
|
MessageDialog msg_dlg(this, _L("Unable to open the selected file."), _L("Error"), wxICON_ERROR | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ScopedFile scoped_in_file(in_file);
|
|
||||||
|
|
||||||
// Set out filename
|
// Set out filename
|
||||||
boost::filesystem::path path(into_u8(input_file));
|
boost::filesystem::path path(into_u8(input_file));
|
||||||
const std::string output_file = path.parent_path().string() + "/" + path.stem().string() + "_binary" + path.extension().string();
|
const std::string output_file = path.parent_path().string() + "/" + path.stem().string() + "_binary" + path.extension().string();
|
||||||
|
|
||||||
// Open destination file
|
// Open destination file
|
||||||
FILE* out_file = boost::nowide::fopen(output_file.c_str(), "wb");
|
FilePtr out_file{ boost::nowide::fopen(output_file.c_str(), "wb") };
|
||||||
if (out_file == nullptr) {
|
if (out_file.f == nullptr) {
|
||||||
MessageDialog msg_dlg(this, _L("Unable to open output file."), _L("Error"), wxICON_ERROR | wxOK);
|
MessageDialog msg_dlg(this, _L("Unable to open output file."), _L("Error"), wxICON_ERROR | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ScopedFile scoped_out_file(out_file);
|
|
||||||
|
|
||||||
// Perform conversion
|
// Perform conversion
|
||||||
{
|
{
|
||||||
wxBusyCursor busy;
|
wxBusyCursor busy;
|
||||||
const bgcode::base::BinarizerConfig& binarizer_config = GCodeProcessor::get_binarizer_config();
|
const bgcode::base::BinarizerConfig& binarizer_config = GCodeProcessor::get_binarizer_config();
|
||||||
bgcode::core::EResult res = bgcode::convert::from_ascii_to_binary(*in_file, *out_file, binarizer_config);
|
bgcode::core::EResult res = bgcode::convert::from_ascii_to_binary(*in_file.f, *out_file.f, binarizer_config);
|
||||||
if (res != bgcode::core::EResult::Success) {
|
if (res != bgcode::core::EResult::Success) {
|
||||||
MessageDialog msg_dlg(this, _L(std::string(bgcode::core::translate_result(res))), _L("Error converting gcode file"), wxICON_INFORMATION | wxOK);
|
MessageDialog msg_dlg(this, _L(std::string(bgcode::core::translate_result(res))), _L("Error converting gcode file"), wxICON_INFORMATION | wxOK);
|
||||||
msg_dlg.ShowModal();
|
msg_dlg.ShowModal();
|
||||||
scoped_out_file.unscope();
|
out_file.close();
|
||||||
fclose(out_file);
|
|
||||||
boost::nowide::remove(output_file.c_str());
|
boost::nowide::remove(output_file.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user