Optimize G-code processing time by using M73OffsetTemplate for plate number offsets
This commit is contained in:
@@ -7,15 +7,17 @@ import shutil
|
||||
import tempfile
|
||||
import zipfile
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from .archive import GCODE_MEMBER_RE, MD5_MEMBER_RE, list_gcode_members
|
||||
from .gcode import (
|
||||
M73OffsetTemplate,
|
||||
apply_line_ending,
|
||||
apply_plate_number_offset,
|
||||
build_swap_block,
|
||||
insert_swap_block,
|
||||
normalize_newlines,
|
||||
prepare_m73_offset_template,
|
||||
read_swap_gcode_file,
|
||||
resolve_swap_gcode_path,
|
||||
)
|
||||
@@ -42,6 +44,12 @@ MIN_ZIP_COMPRESS_LEVEL = 1
|
||||
MAX_ZIP_COMPRESS_LEVEL = 9
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _PreparedPlateGcode:
|
||||
text: str
|
||||
m73_template: M73OffsetTemplate | None = None
|
||||
|
||||
|
||||
def normalized_zip_compress_level(level: int | None) -> int:
|
||||
try:
|
||||
value = int(level if level is not None else DEFAULT_ZIP_COMPRESS_LEVEL)
|
||||
@@ -120,27 +128,40 @@ def process_plate_gcode(
|
||||
swap_gcode_text: str,
|
||||
patch_config: GcodePatchConfig,
|
||||
) -> str:
|
||||
text = _prepare_plate_gcode(source, options, patch_config)
|
||||
return _process_prepared_plate_gcode(text, plate_number, total_plates, options, swap_gcode_text, patch_config)
|
||||
prepared = _prepare_plate_gcode(source, options, patch_config)
|
||||
return _process_prepared_plate_gcode(
|
||||
prepared,
|
||||
plate_number,
|
||||
total_plates,
|
||||
options,
|
||||
swap_gcode_text,
|
||||
patch_config,
|
||||
)
|
||||
|
||||
|
||||
def _prepare_plate_gcode(source: PlateSource, options: BuildOptions, patch_config: GcodePatchConfig) -> str:
|
||||
def _prepare_plate_gcode(
|
||||
source: PlateSource,
|
||||
options: BuildOptions,
|
||||
patch_config: GcodePatchConfig,
|
||||
) -> _PreparedPlateGcode:
|
||||
text = normalize_newlines(source.gcode_text)
|
||||
if options.apply_gcode_patches:
|
||||
text = apply_gcode_patches(text, patch_config)
|
||||
return text
|
||||
m73_template = prepare_m73_offset_template(text) if options.show_plate_number else None
|
||||
return _PreparedPlateGcode(text, m73_template)
|
||||
|
||||
|
||||
def _process_prepared_plate_gcode(
|
||||
text: str,
|
||||
prepared: _PreparedPlateGcode,
|
||||
plate_number: int,
|
||||
total_plates: int,
|
||||
options: BuildOptions,
|
||||
swap_gcode_text: str,
|
||||
patch_config: GcodePatchConfig,
|
||||
) -> str:
|
||||
if options.show_plate_number:
|
||||
text = apply_plate_number_offset(text, plate_number)
|
||||
text = prepared.text
|
||||
if options.show_plate_number and prepared.m73_template is not None:
|
||||
text = prepared.m73_template.apply(plate_number)
|
||||
should_swap = options.swap_after_final or plate_number < total_plates
|
||||
if should_swap:
|
||||
swap_block = build_swap_block(swap_gcode_text, options.cool_bed_temp, options.wait_after_eject_seconds)
|
||||
@@ -373,7 +394,7 @@ def build_packed_3mf(jobs: list[PlateJob], options: BuildOptions) -> BuildResult
|
||||
swap_gcode_text = read_swap_gcode_file(swap_gcode_path)
|
||||
patch_config = parse_patch_config() if options.apply_gcode_patches else GcodePatchConfig(rules=())
|
||||
processed: list[str] = []
|
||||
prepared_gcode_cache: dict[tuple[Path, str], str] = {}
|
||||
prepared_gcode_cache: dict[tuple[Path, str], _PreparedPlateGcode] = {}
|
||||
for plate_number, source in enumerate(sources, start=1):
|
||||
cache_key = _plate_gcode_cache_key(source)
|
||||
prepared_text = prepared_gcode_cache.get(cache_key)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from .models import DEFAULT_INSERT_BEFORE_MARKER, LineEnding
|
||||
@@ -73,13 +74,37 @@ def format_filament(weight_grams: float | None, used_m: float | None = None) ->
|
||||
return " / ".join(parts)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class M73OffsetTemplate:
|
||||
segments: tuple[str, ...]
|
||||
base_remaining_minutes: tuple[int, ...]
|
||||
|
||||
def apply(self, plate_number: int) -> str:
|
||||
if not self.base_remaining_minutes:
|
||||
return self.segments[0]
|
||||
minute_offset = plate_number * 100 * 60
|
||||
parts: list[str] = []
|
||||
for index, remaining_minutes in enumerate(self.base_remaining_minutes):
|
||||
parts.append(self.segments[index])
|
||||
parts.append(str(remaining_minutes + minute_offset))
|
||||
parts.append(self.segments[-1])
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def prepare_m73_offset_template(gcode: str) -> M73OffsetTemplate:
|
||||
segments: list[str] = []
|
||||
base_remaining_minutes: list[int] = []
|
||||
last_index = 0
|
||||
for match in M73_RE.finditer(gcode):
|
||||
segments.append(gcode[last_index : match.start(2)])
|
||||
base_remaining_minutes.append(int(match.group(2)))
|
||||
last_index = match.end(2)
|
||||
segments.append(gcode[last_index:])
|
||||
return M73OffsetTemplate(tuple(segments), tuple(base_remaining_minutes))
|
||||
|
||||
|
||||
def apply_plate_number_offset(gcode: str, plate_number: int) -> str:
|
||||
minute_offset = plate_number * 100 * 60
|
||||
|
||||
def replace(match: re.Match[str]) -> str:
|
||||
return f"{match.group(1)}{int(match.group(2)) + minute_offset}{match.group(3)}"
|
||||
|
||||
return M73_RE.sub(replace, gcode)
|
||||
return prepare_m73_offset_template(gcode).apply(plate_number)
|
||||
|
||||
|
||||
def build_swap_block(swap_gcode: str, cool_bed_temp: int | None, wait_seconds: int) -> str:
|
||||
|
||||
Reference in New Issue
Block a user