Skip to content

Commit 128f55b

Browse files
committed
Handle T filter for temp cooldown
1 parent e10309d commit 128f55b

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

src/mfm/app_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
APP_NAME_ABBREVIATION = 'MFM'
22
APP_NAME = f'3D G-code Map Feature Modifier ({APP_NAME_ABBREVIATION})'
3-
APP_VERSION = '1.6.9'
3+
APP_VERSION = '1.6.10'

src/mfm/map_post_process.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,11 @@ def checkAndInsertToolchange(ps: PrintState, cf: Feature, f: typing.TextIO, out:
375375
skipWriteToolchange = True
376376

377377
if skipWriteToolchange == False:
378-
cl = substituteNewColor(cl, nextFeatureColor)
378+
379+
if nextFeatureColor == 2 and ps.printingColor == 1:
380+
0==0
381+
382+
cl = substituteNewColor(cl, nextFeatureColor, ps.printingColor)
379383
writeWithFilters(out, cl, loadedColors)
380384

381385
# Write Extra Purge Gcode if previous color needs it
@@ -522,9 +526,36 @@ def updatePrintState(ps: PrintState, cl: str, sw: bool, cp: int):
522526
ps.printingColor = int(toolchangeMatch.groups()[0]) #ps.originalColor
523527
print(f"found printing color toolchange to {int(toolchangeMatch.groups()[0])}")
524528

525-
def substituteNewColor(cl, newColorIndex: int):
529+
def substituteNewColor(cl, newColorIndex: int, previousColorIndex: int = -1):
530+
"""Substitute a new/prev color into a string
531+
532+
The T filter is only applied to M620, M621, TXXX, M104, and M109 commands.
533+
534+
This function can be used in 2 modes, if previous color index is not specified, it always applies the 'new' color which can be either a color swap or color replacement depending on how `newColorIndex` is set.
535+
536+
If `previousColorIndex` is supplied and the input string is a M104/M109 tagged as a cooldown, the previous color index is substituted. This should be used for color swap.
537+
538+
`previousColorIndex` should not be supplied for color replacement.
539+
540+
:param cl: Input string
541+
:type cl: str
542+
:param newColorIndex: New color index
543+
:type newColorIndex: int
544+
:param previousColorIndex: Previous color index
545+
:type previousColorIndex: int
546+
547+
:return: String after substitution
548+
:rtype: str
549+
"""
550+
526551
cl = re.sub(M620, f"M620 S{newColorIndex}A", cl)
527-
cl = re.sub(TOOLCHANGE_T, f"T{newColorIndex}", cl)
552+
553+
# substitute the prev color if previousColorIndex specified and line is a M104/M109 command tagged with ;cooldown
554+
if previousColorIndex != -1 and re.match(M104M109_COOLDOWN, cl):
555+
cl = re.sub(TOOL_T, f"T{previousColorIndex}", cl)
556+
elif cl.startswith('T') or re.match(M104M109, cl): # only sub new color if T or M104/109 command
557+
cl = re.sub(TOOL_T, f"T{newColorIndex}", cl)
558+
528559
cl = re.sub(M621, f"M621 S{newColorIndex}A", cl)
529560
return cl
530561

@@ -639,17 +670,30 @@ def currentPrintingColorIndexForColorIndex(colorIndex: int, lc: list[PrintColor]
639670
return colorIndex
640671

641672
def writeWithFilters(out: typing.TextIO, cl: str, lc: list[PrintColor]):
673+
"""Write a string to output after applying a T filter.
674+
675+
The T filter is only applied to M620, M621, TXXX, M104, and M109 commands.
676+
677+
:param out: Output stream
678+
:type out: typing.TextIO
679+
:param cl: Input string
680+
:type cl: str
681+
:param lc: Print color list
682+
:type lc: list[PrintColor]
683+
"""
642684
cmdColorIndex = -1
643685
#print(f"writewithcolorfilter {cl}")
644686
#look for color specific matches to find the original color to check for replacement colors
645687
m620Match = re.match(M620, cl)
646-
toolchangeMatch = re.match(TOOLCHANGE_T, cl)
688+
toolMatch = None
689+
if cl.startswith('T') or re.match(M104M109, cl): # Only search for T in T or M104/109 commands
690+
toolMatch = re.search(TOOL_T, cl) # search whole string because T can be parameter
647691
m621Match = re.match(M621, cl)
648692

649693
if m620Match:
650694
cmdColorIndex = int(m620Match.groups()[0])
651-
elif toolchangeMatch:
652-
cmdColorIndex = int(toolchangeMatch.groups()[0])
695+
elif toolMatch:
696+
cmdColorIndex = int(toolMatch.groups()[0])
653697
elif m621Match:
654698
cmdColorIndex = int(m621Match.groups()[0])
655699

src/mfm/printing_constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
# Toolchange
6161
M620 = '^M620 S(\d*)A'
6262
TOOLCHANGE_T = '^\s*T(?!255$|1000|1100$)(\d+)' # Do not match T255,T1000,T1100 which are nonstandard by Bambu. We do not change tabbed T commands. There is a bug where the file pointer will jump to the beginning. This regex needs to handle whitespace in beginning to get the initial AMS toolchange
63+
TOOL_T = 'T(?!255$|1000|1100$)(\d+)' # Tool parameter. Check if line begins with ;(comment) or (M104/M109) beforehand.
64+
M104M109 = '^M10[49].*;cooldown$' # M104 or M109 tagged as ;cooldown
65+
M104M109_COOLDOWN = '^M10[49].*;cooldown$' # M104 or M109 tagged as ;cooldown
6366
M621 = '^M621 S(\d*)A'
6467
EXTRA_PURGE_INSERTION = '; EXTRA_PURGE_INSERTION'
6568
EXTRA_PURGE_INSERTION_RE = f'^{EXTRA_PURGE_INSERTION}'

0 commit comments

Comments
 (0)