43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
"""打印机发送模块:通过局域网将 3MF 发送到 Bambu A1 并开始打印。"""
|
|
|
|
from __future__ import annotations
|
|
from pathlib import Path
|
|
from dataclasses import dataclass
|
|
|
|
@dataclass
|
|
class PrinterConfig:
|
|
"""打印机连接配置。"""
|
|
ip: str = "" # 打印机 IP 地址
|
|
access_code: str = "" # 局域网访问码
|
|
serial: str = "" # 打印机序列号
|
|
|
|
def send_to_printer(file_path: Path, config: PrinterConfig) -> bool:
|
|
"""将 3MF 文件上传至打印机并开始打印。
|
|
|
|
Args:
|
|
file_path: 要发送的 3MF 文件路径
|
|
config: 打印机连接配置
|
|
|
|
Returns:
|
|
True 表示成功,False 表示失败
|
|
|
|
Raises:
|
|
ImportError: bambulabs_api 未安装
|
|
Exception: 连接或上传失败
|
|
"""
|
|
# 延迟导入,避免在没有 bambulabs_api 时报错
|
|
from bambulabs_api import Printer as BambuPrinter
|
|
printer = BambuPrinter(config.ip, config.access_code, config.serial)
|
|
try:
|
|
printer.connect()
|
|
file_name = file_path.name
|
|
with open(file_path, "rb") as f:
|
|
import io
|
|
result = printer.upload_file(io.BytesIO(f.read()), file_name)
|
|
if "226" not in result:
|
|
raise RuntimeError(f"上传文件失败: {result}")
|
|
printer.start_print(file_name, plate_number=1)
|
|
return True
|
|
finally:
|
|
printer.disconnect()
|