bgcode support with wheels
This commit is contained in:
parent
d74bd94e0a
commit
bf5df3dbec
6 changed files with 36 additions and 48 deletions
|
|
@ -44,6 +44,10 @@ wheels = [
|
||||||
"./wheels/shapely-2.1.2-cp313-cp313-macosx_11_0_arm64.whl",
|
"./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-manylinux2014_x86_64.manylinux_2_17_x86_64.whl",
|
||||||
"./wheels/shapely-2.1.2-cp313-cp313-win_amd64.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:
|
# Optional: add-ons can list which resources they will require:
|
||||||
|
|
|
||||||
BIN
infra/bin/bgcode
BIN
infra/bin/bgcode
Binary file not shown.
|
|
@ -1,62 +1,46 @@
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import platform
|
|
||||||
from pathlib import Path
|
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:
|
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.
|
Returns the binary content of the converted file.
|
||||||
"""
|
"""
|
||||||
bgcode_bin = get_bgcode_binary()
|
try:
|
||||||
|
import pybgcode
|
||||||
if not os.path.exists(bgcode_bin):
|
except ImportError:
|
||||||
raise FileNotFoundError(f"bgcode binary not found at {bgcode_bin}")
|
raise ImportError("pybgcode library not found. Ensure wheels are correctly integrated.")
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
tmp_gcode = Path(tmpdir) / (name_part + ".gcode")
|
tmp_gcode_path = Path(tmpdir) / (name_part + ".gcode")
|
||||||
tmp_gcode.write_text(gcode, encoding="utf-8")
|
tmp_bgcode_path = Path(tmpdir) / (name_part + ".bgcode")
|
||||||
|
|
||||||
# Run conversion: bgcode <input>
|
tmp_gcode_path.write_text(gcode, encoding="utf-8")
|
||||||
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)
|
infile = pybgcode.open(str(tmp_gcode_path), "r")
|
||||||
if res.returncode != 0:
|
if not infile:
|
||||||
err_msg = res.stderr.strip() or res.stdout.strip() or "Unknown error"
|
raise RuntimeError(f"Failed to open input G-code file: {tmp_gcode_path}")
|
||||||
raise RuntimeError(f"bgcode conversion failed: {err_msg}")
|
|
||||||
|
|
||||||
tmp_bgcode = tmp_gcode.with_suffix(".bgcode")
|
outfile = pybgcode.open(str(tmp_bgcode_path), "wb")
|
||||||
if tmp_bgcode.exists():
|
if not outfile:
|
||||||
return tmp_bgcode.read_bytes()
|
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:
|
else:
|
||||||
raise FileNotFoundError("Failed to find generated .bgcode file")
|
raise FileNotFoundError("Failed to find generated .bgcode file")
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
BIN
wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl
Normal file
BIN
wheels/pybgcode_jneilliii-0.2.1-cp313-cp313-win_amd64.whl
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue