Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse flush multiplier and adjust flush matrix accordingly to match Orca and Bambu requested purge volumes #524

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions components/mmu_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,8 @@ def load_component(config):
PURGE_VOLUMES_REGEX = r"^;\s*(flush_volumes_matrix|wiping_volumes_matrix)\s*=\s*(.*)$" # flush.. in Orca/Bambu, wiping... in PS
METADATA_PURGE_VOLUMES = "!purge_volumes!"

FLUSH_MULTIPLIER_REGEX = r"^;\s*flush_multiplier\s*=\s*(.*)$" #flush multiplier in Orca/Bambu. Used to multiply the values in the purge volumes to match the slicer UI settings

FILAMENT_NAMES_REGEX = r"^;\s*(filament_settings_id)\s*=\s*(.*)$"
METADATA_FILAMENT_NAMES = "!filament_names!"

Expand All @@ -791,7 +793,7 @@ def gcode_processed_already(file_path):
def parse_gcode_file(file_path):
slicer_regex = re.compile(SLICER_REGEX, re.IGNORECASE)
has_tools_placeholder = has_colors_placeholder = has_temps_placeholder = has_materials_placeholder = has_purge_volumes_placeholder = filament_names_placeholder = False
found_colors = found_temps = found_materials = found_purge_volumes = found_filament_names = False
found_colors = found_temps = found_materials = found_purge_volumes = found_filament_names = found_flush_multiplier = False
slicer = None

tools_used = set()
Expand All @@ -800,6 +802,7 @@ def parse_gcode_file(file_path):
materials = []
purge_volumes = []
filament_names = []
flush_multiplier = 1.0 # Initialize flush_multiplier to 1.0

with open(file_path, 'r') as in_file:
for line in in_file:
Expand Down Expand Up @@ -833,6 +836,12 @@ def parse_gcode_file(file_path):
filament_names_regex = re.compile(FILAMENT_NAMES_REGEX[slicer], re.IGNORECASE)
else:
filament_names_regex = re.compile(FILAMENT_NAMES_REGEX, re.IGNORECASE)

if isinstance(FLUSH_MULTIPLIER_REGEX, dict):
flush_multiplier_regex = re.compile(FLUSH_MULTIPLIER_REGEX[slicer], re.IGNORECASE)
else:
flush_multiplier_regex = re.compile(FLUSH_MULTIPLIER_REGEX, re.IGNORECASE)

with open(file_path, 'r') as in_file:
for line in in_file:
# !referenced_tools! processing
Expand Down Expand Up @@ -880,6 +889,16 @@ def parse_gcode_file(file_path):
materials.extend(materials_csv)
found_materials = True

# flush_multiplier processing
if not found_flush_multiplier:
match = flush_multiplier_regex.match(line)
if match:
try:
flush_multiplier = float(match.group(1).strip())
except ValueError:
flush_multiplier = 1.0 # Default to 1.0 if conversion fails
found_flush_multiplier = True

# !purge_volumes! processing
if not has_purge_volumes_placeholder and METADATA_PURGE_VOLUMES in line:
has_purge_volumes_placeholder = True
Expand All @@ -888,7 +907,15 @@ def parse_gcode_file(file_path):
match = purge_volumes_regex.match(line)
if match:
purge_volumes_csv = match.group(2).strip().split(',')
purge_volumes.extend(purge_volumes_csv)
# Multiply each value by flush_multiplier
for volume_str in purge_volumes_csv:
try:
volume = float(volume_str)
multiplied_volume = round(volume * flush_multiplier,1)
purge_volumes.append(str(multiplied_volume))
except ValueError:
# If conversion fails, keep the original value
purge_volumes.append(volume_str)
found_purge_volumes = True

# !filament_names! processing
Expand All @@ -900,7 +927,7 @@ def parse_gcode_file(file_path):
if match:
filament_names_csv = [e.strip() for e in re.split(',|;', match.group(2).strip())]
filament_names.extend(filament_names_csv)
found_filament_names = True
found_filament_names = True

return (has_tools_placeholder or has_colors_placeholder or has_temps_placeholder or has_materials_placeholder or has_purge_volumes_placeholder or filament_names_placeholder,
sorted(tools_used), colors, temps, materials, purge_volumes, filament_names, slicer)
Expand Down