Skip to content

Commit

Permalink
Added support for "Produced on" (time of creation)
Browse files Browse the repository at this point in the history
  • Loading branch information
barzto committed Oct 23, 2024
1 parent 3db61e4 commit a44f3bb
Show file tree
Hide file tree
Showing 5 changed files with 6,329 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/LibBGCode/convert/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,17 @@ BGCODE_CONVERT_EXPORT EResult from_ascii_to_binary(FILE& src_file, FILE& dst_fil
size_t pos = sv_line.find(GeneratedByPrusaSlicer);
if (pos != std::string_view::npos) {
std::string_view version = trim(sv_line.substr(pos + GeneratedByPrusaSlicer.length()));
std::string_view time;
pos = version.find(" ");
if (pos != std::string_view::npos)
if (pos != std::string_view::npos) {
if (version.substr(pos, 4) == " on ")
time = version.substr(pos + 4);
version = version.substr(0, pos);

}
binary_data.file_metadata.raw_data.emplace_back("Producer", "PrusaSlicer " + std::string(version));
if (!time.empty())
binary_data.file_metadata.raw_data.emplace_back("Produced on", time);
producer_found = true;
processed_lines.emplace_back(lines_counter++);
return;
Expand Down Expand Up @@ -636,9 +643,17 @@ BGCODE_CONVERT_EXPORT EResult from_binary_to_ascii(FILE& src_file, FILE& dst_fil
if (res != EResult::Success)
// propagate error
return res;

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::string producer_str = (producer_it != file_metadata_block.raw_data.end()) ? producer_it->second : "Unknown";
std::string producer_str = (producer_it != file_metadata_block.raw_data.end()) ? producer_it->second : "Unknown";


auto produced_on_it = std::find_if(file_metadata_block.raw_data.begin(), file_metadata_block.raw_data.end(),
[](const auto& item) { return item.first == "Produced on"; });
if (produced_on_it != file_metadata_block.raw_data.end())
producer_str += " on " + produced_on_it->second;

if (!write_line("; generated by " + producer_str + "\n\n\n"))
return EResult::WriteError;
res = read_next_block_header(src_file, file_header, block_header, checksum_buffer.data(), checksum_buffer.size());
Expand Down
41 changes: 41 additions & 0 deletions tests/convert/convert_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,44 @@ TEST_CASE("Convert from ascii to binary", "[Convert]")
// compare results
compare_text_files(ba_dst_filename, ab_src_filename);
}

TEST_CASE("Convert from binary to ascii PS 2.8.1", "[Convert]")
{
std::cout << "\nTEST: Convert from binary to ascii in PS 2.8.1 format\n";

const std::string src_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1.bgcode";
const std::string dst_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1_out_a.gcode";
const std::string check_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1_ref.gcode";

// convert from binary to ascii
binary_to_ascii(src_filename, dst_filename);
// compare results
compare_text_files(dst_filename, check_filename);
}

TEST_CASE("Convert from ascii to binary PS 2.8.1", "[Convert]")
{
std::cout << "\nTEST: Convert from ascii to binary in PS 2.8.1 format\n";

// convert from ascii to binary
const std::string ab_src_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1.gcode";
const std::string ab_dst_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1_out_b.bgcode";
BinarizerConfig config;
config.checksum = EChecksumType::CRC32;
config.compression.file_metadata = ECompressionType::None;
config.compression.print_metadata = ECompressionType::None;
config.compression.printer_metadata = ECompressionType::None;
config.compression.slicer_metadata = ECompressionType::Deflate;
config.compression.gcode = ECompressionType::Heatshrink_12_4;
config.gcode_encoding = EGCodeEncodingType::MeatPackComments;
config.metadata_encoding = EMetadataEncodingType::INI;
ascii_to_binary(ab_src_filename, ab_dst_filename, config);

// convert back from binary to ascii
const std::string ba_src_filename = ab_dst_filename;
const std::string ba_dst_filename = std::string(TEST_DATA_DIR) + "/mini_cube_ps2.8.1_final.gcode";
binary_to_ascii(ba_src_filename, ba_dst_filename);

// compare results
compare_text_files(ba_dst_filename, ab_src_filename);
}
Binary file added tests/data/mini_cube_ps2.8.1.bgcode
Binary file not shown.
Loading

0 comments on commit a44f3bb

Please sign in to comment.