diff --git a/blender_manifest.toml b/blender_manifest.toml index 2be5f3b..b61725f 100644 --- a/blender_manifest.toml +++ b/blender_manifest.toml @@ -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: diff --git a/infra/bin/bgcode b/infra/bin/bgcode deleted file mode 100755 index 17efe52..0000000 Binary files a/infra/bin/bgcode and /dev/null differ diff --git a/infra/networking/bgcode.py b/infra/networking/bgcode.py index abf762b..072244a 100644 --- a/infra/networking/bgcode.py +++ b/infra/networking/bgcode.py @@ -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 - 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" - ]) - - 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" - raise RuntimeError(f"bgcode conversion failed: {err_msg}") + tmp_gcode_path.write_text(gcode, encoding="utf-8") - tmp_bgcode = tmp_gcode.with_suffix(".bgcode") - if tmp_bgcode.exists(): - return tmp_bgcode.read_bytes() + 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) + + if tmp_bgcode_path.exists(): + return tmp_bgcode_path.read_bytes() else: raise FileNotFoundError("Failed to find generated .bgcode file") diff --git a/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-macosx_11_0_arm64.whl b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-macosx_11_0_arm64.whl new file mode 100644 index 0000000..36b66ec Binary files /dev/null and b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-macosx_11_0_arm64.whl differ diff --git a/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl new file mode 100644 index 0000000..9a7ae61 Binary files /dev/null and b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl differ diff --git a/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl new file mode 100644 index 0000000..423b50f Binary files /dev/null and b/wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl differ