Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method for connect read #46

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pybgcode/pybgcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,79 @@ def read_metadata(gcodefile: FILEWrapper, type: str = 'printer'):

return get_metadata(gcodefile, header, block_header, metadata_block_class)

# this list was taken from gcode-metadata library
connect_metadata_keys = [
"filament used [cm3]", "filament used [mm]", "filament used [g]",
"filament cost", "filament_type", "temperature", "bed_temperature",
"nozzle_diameter", "estimated printing time (normal mode)",
"printer_model", "layer_height", "fill_density", "brim_width",
"support_material", "ironing", "quiet_percent_present",
"quiet_left_present", "quiet_change_in_present", "normal_percent_present",
"normal_left_present", "normal_change_in_present", "layer_info_present",
"max_layer_z"]


def filter_connect_metadata(output: dict) -> dict:
all_metadata = {**output['print'], **output['printer']}
connect_metadata = {
key: item for key, item in all_metadata.items()
if key in connect_metadata_keys}
return {'thumbnails': output['thumbnails'], 'metadata': connect_metadata}


def read_connect_metadata(wrapper: FILEWrapper):
"""Read metadata from binary gcode file."""
output = {'print': None, 'thumbnails': [], 'printer': None}

# read file header
res, header = get_header(wrapper)
block_header = BlockHeader()
while True:
# read next block header
res = read_next_block_header(wrapper, header, block_header)
if res != EResult.Success:
raise ResultError(res)
if block_header.type == 0:
# file metadata - we do not need them
metadata_block = FileMetadataBlock()
res = metadata_block.read_data(
wrapper, header, block_header)
if res != EResult.Success:
raise ResultError(res)
elif block_header.type in (1, 2):
# GCode block or Slicer metadata block - no more metadata
return filter_connect_metadata(output)
elif block_header.type == 3:
# printer metadata - we need them
metadata_block = PrinterMetadataBlock()
res = metadata_block.read_data(
wrapper, header, block_header)
if res != EResult.Success:
raise ResultError(res)
output['printer'] = dict(
metadata_block.raw_data) if metadata_block else None
elif block_header.type == 4:
# print metdata - we need them
metadata_block = PrintMetadataBlock()
res = metadata_block.read_data(
wrapper, header, block_header)
if res != EResult.Success:
raise ResultError(res)
output['print'] = dict(
metadata_block.raw_data) if metadata_block else None
return filter_connect_metadata(output)
elif block_header.type == 5:
# thumbnails block
thumbnail_block = ThumbnailBlock()
res = thumbnail_block.read_data(wrapper, header, block_header)
if res != EResult.Success:
raise ResultError(res)
output['thumbnails'].append(
{"meta": thumbnail_block.params,
"bytes": thumbnail_block.data()})
else:
# not documented value
return filter_connect_metadata(output)



9 changes: 8 additions & 1 deletion pybgcode/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
EResult,
EThumbnailFormat,
read_thumbnails,
read_metadata
read_metadata,
read_connect_metadata,
connect_metadata_keys
)

# pylint: disable=missing-function-docstring
Expand Down Expand Up @@ -82,6 +84,11 @@ def test_main():
assert print_metadata == TEST_PRINT_METADATA
slicer_metadata = read_metadata(thumb_f, 'slicer')
assert len(slicer_metadata) == TEST_LEN_SLICER_METADATA

all_metadata = read_connect_metadata(thumb_f)
assert len(all_metadata['thumbnails']) == TEST_THUMBNAILS
for key in all_metadata['metadata'].keys():
assert key in connect_metadata_keys
pybgcode.close(thumb_f)

# write thumbnails to png files
Expand Down
Loading