diff --git a/src/py/bbctrl/AuxPreprocessor.py b/src/py/bbctrl/AuxPreprocessor.py index 5a1ae48..10cd2ab 100644 --- a/src/py/bbctrl/AuxPreprocessor.py +++ b/src/py/bbctrl/AuxPreprocessor.py @@ -42,6 +42,30 @@ _PAREN_COMMENT_RE = re.compile(r'\([^)]*\)') # Modal G-code groups we care about. _MODAL_RE = re.compile(r'(?:) line and + # stripped from the residual. Multiple ATC codes per + # line are honoured but order is left-to-right. We do + # this BEFORE the W-axis path so a line like + # "M100 W10" cleanly emits drop+move. + atc_matches = list(_ATC_M_RE.finditer(line)) + if atc_matches: + rewrote_any = True + for m in atc_matches: + try: num = int(m.group(1)) + except ValueError: continue + event = _ATC_M_CODES.get(num) + if event: + fout.write('(MSG,HOOK:%s:)\n' % event) + line = _ATC_M_RE.sub('', line) + code = _PAREN_COMMENT_RE.sub('', line) + code = code.split(';', 1)[0] + # If nothing else is left on the line, we're done. + if not code.strip(): + # Preserve any trailing comment, but skip if the + # whole line is empty after the M-code strip. + rest = line.rstrip() + if rest: + fout.write(rest + '\n') + continue + if not _W_TOKEN_RE.search(code): - fout.write(raw) + # No W work to do; emit whatever's left after ATC + # M-code stripping (or the original line if there + # were no ATC codes). + fout.write(line + '\n' if atc_matches else raw) continue rewrote_any = True @@ -214,9 +275,10 @@ class AuxPreprocessor(object): def preprocess_file(src_path, log=None, w_first=True): - """Convenience: rewrite src_path in place if it uses W. + """Convenience: rewrite src_path in place if it uses anything the + preprocessor handles (W axis tokens or ATC M-codes). Returns True if the file was rewritten.""" - if not AuxPreprocessor.file_uses_w(src_path): + if not AuxPreprocessor.file_uses_aux(src_path): return False pre = AuxPreprocessor(log=log, w_first=w_first) fd, tmp = tempfile.mkstemp(prefix='auxpre_', suffix='.nc', diff --git a/src/py/bbctrl/Mach.py b/src/py/bbctrl/Mach.py index b5a79e6..834ddaf 100644 --- a/src/py/bbctrl/Mach.py +++ b/src/py/bbctrl/Mach.py @@ -270,8 +270,9 @@ class Mach(Comm): """Apply the W-axis preprocessor to a single MDI line. Returns possibly-multi-line G-code with HOOK: comments inserted.""" try: - from bbctrl.AuxPreprocessor import AuxPreprocessor, _W_TOKEN_RE - if not _W_TOKEN_RE.search(cmd): + from bbctrl.AuxPreprocessor import ( + AuxPreprocessor, _W_TOKEN_RE, _ATC_M_RE) + if not _W_TOKEN_RE.search(cmd) and not _ATC_M_RE.search(cmd): return cmd import io, tempfile, os # AuxPreprocessor.process is file-based; route through