Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- from __future__ import absolute_import
- import octoprint.filemanager
- import octoprint.filemanager.util
- import octoprint.plugin
- import octoprint.printer
- import octoprint.util
- import re
- import flask
- from octoprint.events import Events
- OBJECT_REGEX = "; process (.*)"
- class ModifyComments(octoprint.filemanager.util.LineProcessorStream):
- def process_line(self, line):
- if line.startswith(";"):
- line = self._matchComment(line)
- if not len(line):
- return None
- return line
- def _matchComment(self, line):
- pattern = re.compile(OBJECT_REGEX)
- matched = pattern.match(line)
- if matched:
- obj = matched.group(1)
- line = "#Object "+obj+"\n"
- return line
- class CancelObjectPlugin(
- octoprint.plugin.StartupPlugin,
- octoprint.plugin.SettingsPlugin,
- octoprint.plugin.EventHandlerPlugin):
- def __init__(self):
- self.object_list = []
- self.skipping = False
- self.active_object = None
- def modify_file(self, path, file_object, blinks=None, printer_profile=None, allow_overwrite=True, *args,**kwargs):
- if not octoprint.filemanager.valid_file_type(path, type="gcode"):
- return file_object
- import os
- name, _ = os.path.splitext(file_object.filename)
- modfile = octoprint.filemanager.util.StreamWrapper(file_object.filename,ModifyComments(file_object.stream()))
- return modfile
- def on_event(self, event, payload):
- if event == Events.FILE_SELECTED:
- self._logger.info("File selected. Getting object list.")
- self.object_list = []
- selectedFile = payload.get("file", "")
- with open(selectedFile, "r") as f:
- for line in f:
- try:
- self.process_line(line)
- except (ValueError, RuntimeError):
- print("Error")
- #just a test for now
- p2 = self.getentry('P2')
- p2["cancelled"] = True
- def process_line(self, line):
- if line.startswith("#"):
- obj = self.check_object(line)
- if obj:
- print(obj)
- #maybe it is faster to put them all in a list and uniquify with a set?
- entry = self.getentry(obj)
- if entry:
- return
- else:
- self.object_list.append({"object" : obj, "active" : False, "cancelled" : False})
- def getentry(self, name):
- for o in self.object_list:
- if o["object"] == name:
- return o
- return None
- def check_object(self, line):
- pattern = re.compile("#Object (.*)")
- matched = pattern.match(line)
- if matched:
- obj = matched.group(1)
- return obj
- return None
- def check_queue(self, comm_instance, phase, cmd, cmd_type, gcode, tags, *args, **kwargs):
- if cmd.startswith('#'):
- obj = self.check_object(cmd)
- if obj:
- entry = self.getentry(obj)
- if entry["cancelled"]:
- self._logger.info("Hit a cancelled object, %s" % obj)
- self.skipping = True
- else:
- #we are coming out of a skipping block, reset extrusion
- if self.skipping:
- self._logger.info("Coming out of skipping block")
- cmd = "G92 E0"
- self.skipping = False
- self.active_object = obj
- if self.skipping:
- #can we just return None? Keep this for now to confirm in terminal output
- return '; object skipped'
- else:
- return cmd
- __plugin_name__ = "CancelObject Plugin"
- def __plugin_load__():
- global __plugin_implementation__
- __plugin_implementation__ = CancelObjectPlugin()
- global __plugin_hooks__
- __plugin_hooks__ = {
- "octoprint.filemanager.preprocessor": __plugin_implementation__.modify_file,
- "octoprint.comm.protocol.gcode.queuing": __plugin_implementation__.check_queue
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement