Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress bar #63

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions octoprint_slic3r/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ def save_slicer_profile(self, path, profile, allow_overwrite=True, overrides=Non
self._save_profile(path, new_profile, allow_overwrite=allow_overwrite, display_name=profile.display_name, description=profile.description)

def do_slice(self, model_path, printer_profile, machinecode_path=None, profile_path=None, position=None, on_progress=None, on_progress_args=None, on_progress_kwargs=None):
if on_progress is not None:
if on_progress_args is None:
on_progress_args = ()
if on_progress_kwargs is None:
on_progress_kwargs = dict()
on_progress_kwargs["_progress"] = 0
on_progress(*on_progress_args, **on_progress_kwargs)

if not profile_path:
profile_path = self._settings.get(["default_profile"])
if not machinecode_path:
Expand All @@ -274,7 +282,8 @@ def do_slice(self, model_path, printer_profile, machinecode_path=None, profile_p
return False, "Path to Slic3r is not configured "

args = ['"%s"' % executable, '--load', '"%s"' % profile_path, '--print-center', '"%f,%f"' % (posX, posY), '-o', '"%s"' % machinecode_path, '"%s"' % model_path]

env = {}

try:
import subprocess

Expand All @@ -283,6 +292,7 @@ def do_slice(self, model_path, printer_profile, machinecode_path=None, profile_p

if help_text.startswith(b'PrusaSlicer-2.3'):
args = ['"%s"' % executable, '-g --load', '"%s"' % profile_path, '--center', '"%f,%f"' % (posX, posY), '-o', '"%s"' % machinecode_path, '"%s"' % model_path]
env['SLIC3R_LOGLEVEL'] = "9"
self._logger.info("Running Prusa Slic3r >= 2.3")
elif help_text.startswith(b'PrusaSlicer-2'):
args = ['"%s"' % executable, '--slice --load', '"%s"' % profile_path, '--center', '"%f,%f"' % (posX, posY), '-o', '"%s"' % machinecode_path, '"%s"' % model_path]
Expand All @@ -301,32 +311,60 @@ def do_slice(self, model_path, printer_profile, machinecode_path=None, profile_p
async_kwarg = 'async_'
else:
async_kwarg = 'async'
p = sarge.run(command, cwd=working_dir, stdout=sarge.Capture(), stderr=sarge.Capture(), **{async_kwarg: True})
p = sarge.run(command, cwd=working_dir, stdout=sarge.Capture(buffer_size=1), stderr=sarge.Capture(buffer_size=1), env=env, **{async_kwarg: True})
p.wait_events()
last_error=""
try:
with self._slicing_commands_mutex:
self._slicing_commands[machinecode_path] = p.commands[0]

line_seen = False
stdout_buffer = b""
stderr_buffer = b""
total_layers = 1

matched_lines = 0
while p.returncode is None:
stdout_line = p.stdout.readline(timeout=0.5)
stderr_line = p.stderr.readline(timeout=0.5)

if not stdout_line and not stderr_line:
if line_seen:
break
else:
continue

line_seen = True
if stdout_line:
self._slic3r_logger.debug("stdout: " + str(stdout_line.strip()))
if stderr_line:
self._slic3r_logger.debug("stderr: " + str(stderr_line.strip()))
if ( len(stderr_line.strip()) > 0 ):
last_error = stderr_line.strip()
p.commands[0].poll()

# Can't use readline because it removes newlines and we can't tell if we have gotten a full line.
stdout_buffer += p.stdout.read(block=False)
stderr_buffer += p.stderr.read(block=False)

stdout_lines = stdout_buffer.split(b'\n')
stdout_buffer = stdout_lines[-1]
stdout_lines = stdout_lines[0:-1]
for stdout_line in stdout_lines:
self._slic3r_logger.debug("stdout: " + str(stdout_line))
print(stdout_line.decode('utf-8'))
m = re.search(r"\[trace\].*layer ([0-9]+)", stdout_line.decode('utf-8'))
if m:
matched_lines += 1
current_layer = int(m.group(1))
total_layers = max(total_layers, current_layer)
if on_progress is not None:
print("sending progress" + str(matched_lines / total_layers / 4))
on_progress_kwargs["_progress"] = matched_lines / total_layers / 4
on_progress(*on_progress_args, **on_progress_kwargs)

stderr_lines = stderr_buffer.split(b'\n')
stderr_buffer = stderr_lines[-1]
stderr_lines = stderr_lines[0:-1]
for stderr_line in stderr_lines:
self._slic3r_logger.debug("stderr: " + str(stderr_line))
if len(stderr_line.strip()) > 0:
last_error = stderr_line.strip()
finally:
if stdout_buffer:
stdout_lines = stdout_buffer.split(b'\n')
for stdout_line in stdout_lines:
self._slic3r_logger.debug("stdout: " + str(stdout_line))

if stderr_buffer:
stderr_lines = stderr_buffer.split(b'\n')
for stderr_line in stderr_lines:
self._slic3r_logger.debug("stderr: " + str(stderr_line))
if len(stderr_line.strip()) > 0:
last_error = stderr_line.strip()
p.close()

with self._cancelled_jobs_mutex:
Expand Down