@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import subprocess
|
||||
import configparser
|
||||
import uuid
|
||||
|
||||
class PrusaSlicerEngine:
|
||||
def __init__(self):
|
||||
@@ -14,7 +15,15 @@ class PrusaSlicerEngine:
|
||||
return b"Usage:" in result.stdout or b"Slic3r" in result.stdout or b"PrusaSlicer" in result.stdout or result.returncode == 0
|
||||
except (FileNotFoundError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def add_ini_keys(self, config_path, target_section, all_configs):
|
||||
config = configparser.ConfigParser(interpolation=None)
|
||||
config.read(config_path)
|
||||
if target_section not in config:
|
||||
config[target_section] = {}
|
||||
for k, v in config[target_section].items():
|
||||
all_configs[k] = v
|
||||
|
||||
def slice(self, app, stl_filepath, gcode_filepath, **kwargs):
|
||||
"""
|
||||
Slices via prusa-slicer CLI mapping standard kwargs to PRUSA parameters where possible.
|
||||
@@ -34,32 +43,52 @@ class PrusaSlicerEngine:
|
||||
infill_density = kwargs.get('infill_density')
|
||||
support_enable = kwargs.get('support_enable')
|
||||
support_pattern = kwargs.get('support_pattern')
|
||||
|
||||
|
||||
# print(support_pattern)
|
||||
all_configs = {}
|
||||
|
||||
printer_ini = os.path.join(app.root_path, '..', 'print_config', 'prusa_slicer', 'printers', 'Ender3_V3_SE.ini')
|
||||
if os.path.exists(printer_ini):
|
||||
self.add_ini_keys(printer_ini, 'settings', all_configs)
|
||||
|
||||
if quality_preset:
|
||||
q_ini = os.path.join(app.root_path, '..', 'print_config', 'prusa_slicer', 'quality', f"{quality_preset}.ini")
|
||||
if os.path.exists(q_ini):
|
||||
command.extend(['--load', q_ini])
|
||||
self.add_ini_keys(q_ini, 'settings', all_configs)
|
||||
|
||||
if material_preset:
|
||||
m_ini = os.path.join(app.root_path, '..', 'print_config', 'prusa_slicer', 'materials', f"{material_preset}.ini")
|
||||
if os.path.exists(m_ini):
|
||||
command.extend(['--load', m_ini])
|
||||
|
||||
self.add_ini_keys(m_ini, 'settings', all_configs)
|
||||
|
||||
if infill_density is not None:
|
||||
command.extend(["--fill-density", f"{infill_density}%"])
|
||||
command.extend([f"--fill-density={infill_density}%"])
|
||||
|
||||
if support_enable and support_enable != 'false':
|
||||
command.append("--support-material")
|
||||
# command.append("--support-material")
|
||||
if support_enable == 'buildplate':
|
||||
command.append("--support-material-buildplate-only")
|
||||
# PrusaSlicer equivalent for tree supports => organic
|
||||
if support_pattern == 'tree':
|
||||
command.extend(["--support-material-style", "organic"])
|
||||
elif support_pattern:
|
||||
pass # mapped to default/grid in prusa CLI without explicit config
|
||||
support_pattern_ini = os.path.join(app.root_path, '..', 'print_config', 'prusa_slicer', 'supports', f'{support_pattern}.ini')
|
||||
if os.path.exists(support_pattern_ini):
|
||||
self.add_ini_keys(support_pattern_ini, 'settings', all_configs)
|
||||
else:
|
||||
pass # Prusa defaults to no supports unless specified
|
||||
|
||||
# Load the default no_support.ini if no support is enabled
|
||||
no_support_ini = os.path.join(app.root_path, '..', 'print_config', 'prusa_slicer', 'supports', 'no_support.ini')
|
||||
if os.path.exists(no_support_ini):
|
||||
self.add_ini_keys(no_support_ini, 'settings', all_configs)
|
||||
else:
|
||||
all_configs['support_material'] = '0'
|
||||
|
||||
tmp_ini_filename = f"tmp_{uuid.uuid4().hex}.ini"
|
||||
tmp_ini_path = os.path.join(app.config['UPLOAD_FOLDER'], tmp_ini_filename)
|
||||
# print(all_configs)
|
||||
with open(tmp_ini_path, 'w') as f:
|
||||
for key, value in all_configs.items():
|
||||
f.write(f"{key} = {value}\n")
|
||||
|
||||
command.extend(["--load", tmp_ini_path])
|
||||
|
||||
env = os.environ.copy()
|
||||
app.logger.info(f"Running command: {' '.join(command)}")
|
||||
|
||||
@@ -67,6 +96,8 @@ class PrusaSlicerEngine:
|
||||
stdout, stderr = process.communicate()
|
||||
|
||||
if process.returncode == 0:
|
||||
# Clean up the temporary .ini file
|
||||
os.remove(tmp_ini_path)
|
||||
return True, None
|
||||
else:
|
||||
err_msg = stderr.decode() if stderr else "Unknown prusa-slicer error"
|
||||
|
||||
Reference in New Issue
Block a user