Skip to content
Merged
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
35 changes: 17 additions & 18 deletions bambu_cli/slicer/estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def _parse_gcode_header(gcode_bytes: bytes) -> tuple[int | None, float | None]:
grams = v
except (ValueError, TypeError):
pass
if seconds is not None and grams is not None:
break

return seconds, grams

Expand All @@ -106,12 +108,14 @@ def read_3mf_estimate(path: str) -> Estimate:

# Primary: Metadata/slice_info.config
slice_info_name: str | None = None
gcode_members: list[str] = []

for n in names:
normalised = n.replace("\\", "/")
parts = normalised.split("/")
if len(parts) == 2 and parts[0] == "Metadata" and parts[1] == "slice_info.config":
norm = n.replace("\\", "/")
if norm == "Metadata/slice_info.config":
slice_info_name = n
break
elif norm.startswith("Metadata/") and norm.endswith(".gcode") and norm.count("/") == 1:
gcode_members.append(n)

if slice_info_name is not None:
info = zf.getinfo(slice_info_name)
Expand All @@ -131,20 +135,15 @@ def read_3mf_estimate(path: str) -> Estimate:

# Fallback: gcode members under Metadata/, in plate order. Keep
# trying later plates if an earlier one carries no usable header.
gcode_members = sorted(
n
for n in names
if len(n.replace("\\", "/").split("/")) == 2
and n.replace("\\", "/").split("/")[0] == "Metadata"
and n.replace("\\", "/").split("/")[1].endswith(".gcode")
)
for n in gcode_members:
# Read only the header window; a real plate gcode can be tens of MB.
with zf.open(n) as fh:
gcode_bytes = fh.read(GCODE_READ_BYTES)
seconds, grams = _parse_gcode_header(gcode_bytes)
if seconds is not None or grams is not None:
return Estimate(seconds, grams)
if gcode_members:
gcode_members.sort()
for n in gcode_members:
# Read only the header window; a real plate gcode can be tens of MB.
with zf.open(n) as fh:
gcode_bytes = fh.read(GCODE_READ_BYTES)
seconds, grams = _parse_gcode_header(gcode_bytes)
if seconds is not None or grams is not None:
return Estimate(seconds, grams)

except Exception: # noqa: BLE001 — never raise, degrade gracefully
pass
Expand Down
Loading