diff --git a/E3S1PROFORKBYTT_printdata_superslicer_thumbnail.py b/E3S1PROFORKBYTT_printdata_superslicer_thumbnail.py index 3c87570..11888a5 100644 --- a/E3S1PROFORKBYTT_printdata_superslicer_thumbnail.py +++ b/E3S1PROFORKBYTT_printdata_superslicer_thumbnail.py @@ -6,7 +6,7 @@ # # This script has been developed for E3S1PROFORKBYTT by Thomas Toka. # -# Intruduced with v008 into E3S1PROFORKBYTT. Extended in v023 +# Introduced with v008 into E3S1PROFORKBYTT. Extended in v023 # ------------------------------------------------------------------------------ import sys @@ -25,20 +25,33 @@ with open(sourceFile, "r", encoding='utf-8') as f: lines = f.readlines() new_lines = [] -remove_next_line = False -for line in lines: - if remove_next_line and line.strip() == ';': - remove_next_line = False - continue +thumbnail_header_found = False +thumbnail_lines = [] # Initialize a list to store thumbnail lines +remove_lines = False # Reset the remove_lines flag + +# Find the existing thumbnail header and footer lines +thumbnail_start = None +thumbnail_end = None + +for i, line in enumerate(lines): if line.startswith('; generated by SuperSlicer'): - remove_next_line = True - elif remove_next_line and line.strip() == '': - continue - else: + remove_lines = True + elif line.startswith('; thumbnail begin'): + if not thumbnail_header_found: + thumbnail_start = i + thumbnail_header_found = True + else: + # If the new thumbnail header has been added, skip this line + continue + elif line.startswith('; thumbnail end'): + thumbnail_end = i + elif remove_lines and line.strip() == ';': + remove_lines = False + elif not remove_lines: new_lines.append(line) -# Extract additional information (moved from PrusaSlicer script) +# Extract additional information filament_used_m, filament_used_g, filament_diameter, filament_density, layer_height, layers = "0", "0", "0", "0", "0", "0" for line in new_lines: if line.startswith("; filament used [mm] ="): @@ -88,55 +101,36 @@ remaining_filament_g = filament_used_g m117_added = 0 # Counter for added M117 commands first_layer = True -# Find the existing thumbnail header and footer lines -thumbnail_start = None -thumbnail_end = None - -for i, line in enumerate(new_lines): - if line.startswith('; thumbnail_JPG begin'): - thumbnail_start = i - elif line.startswith('; thumbnail_JPG end'): - thumbnail_end = i - if thumbnail_start is not None and thumbnail_end is not None: - # Extract the PNG data without decoding - original_png_data = "".join(new_lines[thumbnail_start + 1:thumbnail_end]).replace("; ", "") + # Extract the JPEG data without decoding + original_jpeg_data = "".join(lines[thumbnail_start + 1:thumbnail_end]).replace("; ", "") - # Define a maximum line length for the PNG data + # Define a maximum line length for the JPEG data max_line_length = 75 - len("; ") - # Split the PNG data into lines with a maximum length - num_lines = 0 - while original_png_data: - if len(original_png_data) <= max_line_length: - line = original_png_data - original_png_data = "" - else: - line = original_png_data[:max_line_length] - original_png_data = original_png_data[max_line_length:] - - # Add the line to the list of lines - lines.append(line) - num_lines += 1 - - # Calculate the number of lines in the thumbnail data - # (this was already done but not used) - num_lines = len(lines) - - # Replace the old PNG lines with the new PNG lines - new_lines[thumbnail_start + 1:thumbnail_end] = [line + "\n" for line in lines] - - # Update the '; thumbnail_JPG begin' line - start_line_number = 1 - end_line_number = start_line_number + num_lines - new_lines[thumbnail_start] = ( - f"; thumbnail_JPG begin 250x250 {len(''.join(lines))} {start_line_number} {end_line_number}" - f" {filament_used_m} {filament_used_g} {layer_height} {filament_diameter} {filament_density} {layers}\n" + # Split the JPEG data into lines with a maximum length + num_lines = math.ceil(len(original_jpeg_data) / max_line_length) + + # Add new thumbnail header + new_thumbnail_header = ( + f"; thumbnail begin 250x250 {len(original_jpeg_data)} " + f"1 {num_lines} {filament_used_m} {filament_used_g} " + f"{layer_height} {filament_diameter} {filament_density} {layers}\n" ) + new_lines.insert(0, new_thumbnail_header) -# Find the ';AFTER_LAYER_CHANGE' line and add 'M117' after it + # Add JPEG lines after the new thumbnail header + new_lines.extend([original_jpeg_data[i:i+max_line_length] for i in range(0, len(original_jpeg_data), max_line_length)]) + +# Process thumbnail section for i, line in enumerate(new_lines): - if line.strip() == ';AFTER_LAYER_CHANGE': + if line.startswith(';AFTER_LAYER_CHANGE'): + after_layer_change_index = i # Store the index of ';AFTER_LAYER_CHANGE' + break # Exit loop once we find ';AFTER_LAYER_CHANGE' + +# Add lines after ';AFTER_LAYER_CHANGE' +for i in range(after_layer_change_index, len(new_lines)): + if new_lines[i].startswith(';AFTER_LAYER_CHANGE'): if first_layer: m117_line = "M117 L1 M{} G{} Z{} Q{}".format(math.ceil(remaining_filament_m), math.ceil(remaining_filament_g), layers, layer_height) new_lines.insert(i + 1, m117_line + '\n') @@ -158,13 +152,12 @@ for i, line in enumerate(new_lines): new_lines.insert(i + 2, m73_line_r + '\n') m73_line_p = "M73 P{}".format(int((m117_added / layers) * 100)) new_lines.insert(i + 3, m73_line_p + '\n') - new_lines.insert(i + 2, m73_line + '\n') - remaining_filament_m -= filament_used_m_per_layer - remaining_filament_g -= filament_used_g_per_layer - m117_added += 1 + remaining_filament_m -= filament_used_m_per_layer + remaining_filament_g -= filament_used_g_per_layer + m117_added += 1 # Write the modified content back to the original file with open(sourceFile, "w", encoding='utf-8') as f: f.writelines(new_lines) -print(f"Added {m117_added} M117 commands and M73 with time information.") \ No newline at end of file +print(f"Added {m117_added} M117 commands and M73 with time information.")