bgcode support with wheels

This commit is contained in:
Nicolas Predella 2026-05-20 16:38:15 +03:00
parent d74bd94e0a
commit bf5df3dbec
6 changed files with 36 additions and 48 deletions

View file

@ -44,6 +44,10 @@ wheels = [
"./wheels/shapely-2.1.2-cp313-cp313-macosx_11_0_arm64.whl",
"./wheels/shapely-2.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl",
"./wheels/shapely-2.1.2-cp313-cp313-win_amd64.whl",
"./wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-macosx_11_0_arm64.whl",
"./wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl",
"./wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl",
]
# Optional: add-ons can list which resources they will require:

Binary file not shown.

View file

@ -1,62 +1,46 @@
import os
import subprocess
import tempfile
import platform
from pathlib import Path
def get_bgcode_binary():
"""Returns the path to the correct bgcode binary for the current platform."""
addon_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
os_name = platform.system()
if os_name == "Windows":
bin_name = "bgcode.exe"
elif os_name == "Darwin": # macOS
bin_name = "bgcode_mac"
else: # Linux/Other
bin_name = "bgcode"
bin_path = os.path.join(addon_dir, "infra", "bin", bin_name)
# Fallback to just "bgcode" if platform-specific one isn't found
if not os.path.exists(bin_path):
fallback_path = os.path.join(addon_dir, "infra", "bin", "bgcode")
if os.path.exists(fallback_path):
return fallback_path
return bin_path
def convert_to_bgcode(gcode: str, name_part: str, compress: bool = True) -> bytes:
"""
Converts a G-code string to binary BGCode using the internal bgcode tool.
Converts a G-code string to binary BGCode using the pybgcode library.
Returns the binary content of the converted file.
"""
bgcode_bin = get_bgcode_binary()
if not os.path.exists(bgcode_bin):
raise FileNotFoundError(f"bgcode binary not found at {bgcode_bin}")
try:
import pybgcode
except ImportError:
raise ImportError("pybgcode library not found. Ensure wheels are correctly integrated.")
with tempfile.TemporaryDirectory() as tmpdir:
tmp_gcode = Path(tmpdir) / (name_part + ".gcode")
tmp_gcode.write_text(gcode, encoding="utf-8")
tmp_gcode_path = Path(tmpdir) / (name_part + ".gcode")
tmp_bgcode_path = Path(tmpdir) / (name_part + ".bgcode")
# Run conversion: bgcode <input>
cmd = [bgcode_bin, str(tmp_gcode)]
if compress:
# Recommended settings for true binary bgcode
cmd.extend([
"--slicer_metadata_compression=1",
"--gcode_compression=3",
"--gcode_encoding=2"
])
tmp_gcode_path.write_text(gcode, encoding="utf-8")
res = subprocess.run(cmd, capture_output=True, text=True)
if res.returncode != 0:
err_msg = res.stderr.strip() or res.stdout.strip() or "Unknown error"
infile = pybgcode.open(str(tmp_gcode_path), "r")
if not infile:
raise RuntimeError(f"Failed to open input G-code file: {tmp_gcode_path}")
outfile = pybgcode.open(str(tmp_bgcode_path), "wb")
if not outfile:
pybgcode.close(infile)
raise RuntimeError(f"Failed to open output BGCode file: {tmp_bgcode_path}")
config = pybgcode.get_config()
# Default config from pybgcode usually has MeatPack + Heatshrink enabled.
try:
res = pybgcode.from_ascii_to_binary(infile, outfile, config)
if res != pybgcode.EResult.Success:
# pybgcode.translate_result provides a human-readable error message
err_msg = pybgcode.translate_result(res)
raise RuntimeError(f"bgcode conversion failed: {err_msg}")
finally:
pybgcode.close(infile)
pybgcode.close(outfile)
tmp_bgcode = tmp_gcode.with_suffix(".bgcode")
if tmp_bgcode.exists():
return tmp_bgcode.read_bytes()
if tmp_bgcode_path.exists():
return tmp_bgcode_path.read_bytes()
else:
raise FileNotFoundError("Failed to find generated .bgcode file")