Advertisement
Guest User

Untitled

a guest
May 6th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. # coding=utf-8
  2. from __future__ import absolute_import
  3.  
  4. import octoprint.filemanager
  5. import octoprint.filemanager.util
  6. import octoprint.plugin
  7. import octoprint.printer
  8. import octoprint.util
  9. import re
  10. import flask
  11.  
  12. from octoprint.events import Events
  13.  
  14. OBJECT_REGEX = "; process (.*)"
  15.  
  16. class ModifyComments(octoprint.filemanager.util.LineProcessorStream):
  17.     def process_line(self, line):
  18.  
  19.         if line.startswith(";"):
  20.             line = self._matchComment(line)
  21.  
  22.         if not len(line):
  23.             return None
  24.         return line
  25.  
  26.     def _matchComment(self, line):
  27.         pattern = re.compile(OBJECT_REGEX)
  28.         matched = pattern.match(line)
  29.         if matched:
  30.             obj = matched.group(1)
  31.             line = "#Object "+obj+"\n"
  32.         return line
  33.  
  34. class CancelObjectPlugin(
  35.     octoprint.plugin.StartupPlugin,
  36.     octoprint.plugin.SettingsPlugin,
  37.     octoprint.plugin.EventHandlerPlugin):
  38.  
  39.     def __init__(self):
  40.        
  41.         self.object_list = []
  42.         self.skipping = False
  43.         self.active_object = None
  44.  
  45.  
  46.     def modify_file(self, path, file_object, blinks=None, printer_profile=None, allow_overwrite=True, *args,**kwargs):
  47.         if not octoprint.filemanager.valid_file_type(path, type="gcode"):
  48.             return file_object
  49.  
  50.         import os
  51.         name, _ = os.path.splitext(file_object.filename)
  52.  
  53.         modfile = octoprint.filemanager.util.StreamWrapper(file_object.filename,ModifyComments(file_object.stream()))
  54.        
  55.         return modfile
  56.        
  57.     def on_event(self, event, payload):
  58.         if event == Events.FILE_SELECTED:
  59.             self._logger.info("File selected. Getting object list.")
  60.             self.object_list = []
  61.             selectedFile = payload.get("file", "")
  62.             with open(selectedFile, "r") as f:
  63.                 for line in f:
  64.                     try:
  65.                         self.process_line(line)
  66.                     except (ValueError, RuntimeError):
  67.                         print("Error")
  68.             #just a test for now                        
  69.             p2 = self.getentry('P2')
  70.             p2["cancelled"] = True
  71.          
  72.     def process_line(self, line):
  73.        
  74.         if line.startswith("#"):
  75.             obj = self.check_object(line)
  76.             if obj:
  77.                 print(obj)
  78.             #maybe it is faster to put them all in a list and uniquify with a set?
  79.                 entry = self.getentry(obj)
  80.                 if entry:
  81.                     return
  82.                 else:
  83.                     self.object_list.append({"object" : obj, "active" : False, "cancelled" : False})
  84.                
  85.                
  86.     def getentry(self, name):
  87.         for o in self.object_list:
  88.             if o["object"] == name:
  89.                 return o
  90.         return None
  91.                
  92.     def check_object(self, line):
  93.         pattern = re.compile("#Object (.*)")
  94.         matched = pattern.match(line)
  95.         if matched:
  96.             obj = matched.group(1)
  97.             return obj
  98.         return None
  99.  
  100.     def check_queue(self, comm_instance, phase, cmd, cmd_type, gcode, tags, *args, **kwargs):    
  101.         if cmd.startswith('#'):
  102.             obj = self.check_object(cmd)
  103.             if obj:
  104.                 entry = self.getentry(obj)
  105.                 if entry["cancelled"]:
  106.                     self._logger.info("Hit a cancelled object, %s" % obj)
  107.                     self.skipping = True
  108.                 else:
  109.                 #we are coming out of a skipping block, reset extrusion
  110.                     if self.skipping:
  111.                         self._logger.info("Coming out of skipping block")
  112.                         cmd = "G92 E0"
  113.                         self.skipping = False
  114.                     self.active_object = obj  
  115.         if self.skipping:
  116.             #can we just return None? Keep this for now to confirm in terminal output
  117.             return '; object skipped'
  118.         else:
  119.             return cmd
  120.  
  121. __plugin_name__ = "CancelObject Plugin"
  122.  
  123.  
  124. def __plugin_load__():
  125.     global __plugin_implementation__
  126.     __plugin_implementation__ = CancelObjectPlugin()
  127.  
  128.     global __plugin_hooks__
  129.     __plugin_hooks__ = {
  130.        
  131.         "octoprint.filemanager.preprocessor": __plugin_implementation__.modify_file,
  132.         "octoprint.comm.protocol.gcode.queuing": __plugin_implementation__.check_queue
  133.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement