Skip to content

Commit

Permalink
M486 (#81)
Browse files Browse the repository at this point in the history
* being inclusion of m486 detection

* return the line

* nothing getting added to list

* logging

* more logging

* fix log

* only matching -1

* towards working

* include stops

* not returning 0 index

* return line error

* include 0 index by removing statement

* update readme, prepare PR
  • Loading branch information
paukstelis authored Feb 5, 2024
1 parent e4af569 commit ba77a78
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Want to support this work? Buy Me a Coffee. https://www.buymeacoffee.com/ppaukst

This plugin allows the user to interactively cancel objects in gcode based on comment tags added by the slicer.
See below for instructions for specific slicers.
### New version 0.5.0, 01/2024
* Parse M486 commands and add necessary `@` commands. This circumvents forcing PrusaSlicer to use `OctoPrint` exclusively in the Label Objects output. There may be issues with other slicers that have not yet been tested.

### New version 0.4.2, 07/2020
* Remove case sensitivity for `@Object` tags.
* Improvements to absolute extrusion tracking.
Expand Down
47 changes: 45 additions & 2 deletions octoprint_cancelobject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from octoprint.filemanager import FileDestinations

class ModifyComments(octoprint.filemanager.util.LineProcessorStream):

def __init__(self, fileBufferedReader, object_regex, reptag):
super(ModifyComments, self).__init__(fileBufferedReader)
#self._console_logger = logging.getLogger("octoprint.plugins.cancelobject")
self.patterns = []
for each in object_regex:
if each["objreg"]:
Expand All @@ -27,6 +28,10 @@ def __init__(self, fileBufferedReader, object_regex, reptag):
self._reptag = "@{0}".format(reptag)
self.infomatch = re.compile("; object:.*")
self.stopmatch = re.compile("; stop printing object ([^\t\n\r\f\v]*)")
self.m486_control = re.compile("M486 S([-]*\d+)|$") #matches end of line after digits
self.m486_descriptor = re.compile("M486 (.*)|$")
self.last_m486 = None
self.m486_list = []

def process_line(self, line):
try:
Expand All @@ -37,6 +42,8 @@ def process_line(self, line):

if line.startswith(";"):
line = self._matchComment(line)
if line.startswith("M486"):
line = self._match_m486(line)
if not len(line):
return None
return line.encode('ascii','xmlcharrefreplace')
Expand All @@ -60,6 +67,42 @@ def _matchComment(self, line):
stopobj = stop.group(1).encode('ascii','xmlcharrefreplace')
line = "{0}stop {1}\n".format(self._reptag, stopobj.decode('utf-8'))
return line

def _match_m486(self, line):
#Try to match control values first
matched = self.m486_control.match(line)
if matched:
last_m486 = int(matched.group(1))
#self._console_logger.info("Matched {}".format(last_m486))
if last_m486 != -1:
obj_name = self._get_m846(last_m486)
if obj_name:
#self._console_logger.info(obj_name)
line = line+"{0} {1}\n".format(self._reptag, obj_name)
else:
line = line+"{0}stop\n".format(self._reptag)


#if we didn't match a control, it is going to be the descriptor
else:
descriptor = self.m486_descriptor.match(line)
if descriptor:
for each in self.m486_list:
#each.update((name,descriptor.group(1)) for name, index in each.iteritems() if index == self.last_m486)
if each["index"] == self.last_m486:
each["name"] = descriptor.group(1)
self.last_m486 = None
return line

def _get_m846(self, index):
for each in self.m486_list:
if each["index"] == index:
return each["name"]

self.m486_list.append({"index": index, "name": None})
self.last_m486 = index
#self._console_logger.info(self.m486_list)
return None

# stolen directly from filaswitch, https://github.com/spegelius/filaswitch
class Gcode_parser:
Expand All @@ -72,7 +115,7 @@ class Gcode_parser:

def __init__(self):
self.last_match = None

def is_extrusion_move(self, line):
"""
Match given line against extrusion move regex
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "OctoPrint-Cancelobject"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.4.7"
plugin_version = "0.5.0"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit ba77a78

Please sign in to comment.