# # # Klipper Adaptive Meshing # # #

# Heads up! If you have any other BED_MESH_CALIBRATE macros defined elsewhere in your config, you will need to comment out / remove them for this to work. (Klicky/Euclid Probe)
# You will also need to be sure that [exclude_object] is defined in printer.cfg, and your slicer is labeling objects.
# This macro will parse information from objects in your gcode to define a min and max mesh area to probe, creating an adaptive mesh!
# This macro will not increase probe_count values in your [bed_mesh] config. If you want richer meshes, be sure to increase probe_count. We recommend at least 5,5.

[gcode_macro BED_MESH_CALIBRATE]
rename_existing: _BED_MESH_CALIBRATE

### This section allows control of status LEDs your printer may have.

variable_led_enable: False              # Enables/disables the use of status LEDs in this macro.
variable_status_macro: 'status_meshing' # If you have status LEDs in your printer (StealthBurner), you can use the macro that changes their status here.

### This section configures mesh point fuzzing, which allows probe points to be varied slightly if printing multiples of the same G-code file.

variable_fuzz_enable: False             # Enables/disables the use of mesh point fuzzing to slightly randomize probing points to spread out wear on a build surface, default is False.
variable_fuzz_min: 0                    # If enabled, the minimum amount in mm a probe point can be randomized, default is 0.
variable_fuzz_max: 4                    # If enabled, the maximum amount in mm a probe point can be randomized, default is 4.

### This section is for configuring a mesh margin, which allows the probed mesh to be expanded outwards from the print area.

variable_margin_enable: False           # Enables/disables adding a margin to the meshed area to pad a mesh out for specific needs, default is False.
variable_margin_size: 5                 # Size in millimeters to expand the mesh outwards from the print area in all directions.

### This section is for those using a dockable probe that is stored outside of the print area. ###

variable_probe_dock_enable: False       # Enables/disables the use of a dockable probe that is stored outside of the print area, default is False.
variable_attach_macro: 'Attach_Probe'   # Here is where you define the macro that ATTACHES the probe to the printhead. E.g. 'Attach_Probe'
variable_detach_macro: 'Dock_Probe'     # Here is where you define the macro that DETACHES the probe from the printhead. E.g. 'Dock_Probe'

### This section is for those who are using Moonraker's Update Manager for KAMP, or want a more verbose macro. ###

variable_display_parameters: True       # Display macro paramters in the console, useful for debugging the SETUP_KAMP_MESHING call, or more verbosity.

gcode:

    {% if display_parameters == True %}
      { action_respond_info("led_enable  : %d" % (led_enable))  }
      { action_respond_info("status_macro: \'%s\'" % (status_macro))  }
      { action_respond_info("fuzz_enable : %d" % (fuzz_enable))  }
      { action_respond_info("fuzz_min    : %f" % (fuzz_min))  }
      { action_respond_info("fuzz_max    : %f" % (fuzz_max))  }
      { action_respond_info("probe_dock_enable: %d" % (probe_dock_enable))  }
      { action_respond_info("attach_macro: \'%s\'" % (attach_macro))  }
      { action_respond_info("detach_macro: \'%s\'" % (detach_macro))  }
    {% endif %}
    
    {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
    {% set bed_mesh_min = printer.configfile.settings.bed_mesh.mesh_min %}
    {% set bed_mesh_max = printer.configfile.settings.bed_mesh.mesh_max %}
    {% set probe_count = printer.configfile.settings.bed_mesh.probe_count %}
    {% set probe_count = probe_count if probe_count|length > 1 else probe_count * 2  %}
    {% set max_probe_point_distance_x = ( bed_mesh_max[0] - bed_mesh_min[0] ) / (probe_count[0] - 1)  %}
    {% set max_probe_point_distance_y = ( bed_mesh_max[1] - bed_mesh_min[1] ) / (probe_count[1] - 1)  %}
    {% set x_min = all_points | map(attribute=0) | min | default(bed_mesh_min[0]) %}
    {% set y_min = all_points | map(attribute=1) | min | default(bed_mesh_min[1]) %}
    {% set x_max = all_points | map(attribute=0) | max | default(bed_mesh_max[0]) %}
    {% set y_max = all_points | map(attribute=1) | max | default(bed_mesh_max[1]) %}
    
    {% if margin_enable == False %}
        {% set margin_size = 0 %}
    {% endif %}
    
    { action_respond_info("{} object points, clamping to bed mesh [{!r} {!r}]".format(
        all_points | count,
        bed_mesh_min,
        bed_mesh_max,
    )) }

    {% if fuzz_enable == True %}
        {% set fuzz_range = range((fuzz_min * 100) | int, (fuzz_max * 100) | int + 1) %}
        {% set x_min = (bed_mesh_min[0] + fuzz_max - margin_size, x_min) | max - (fuzz_range | random / 100.0) %}
        {% set y_min = (bed_mesh_min[1] + fuzz_max - margin_size, y_min) | max - (fuzz_range | random / 100.0) %}
        {% set x_max = (bed_mesh_max[0] - fuzz_max + margin_size, x_max) | min + (fuzz_range | random / 100.0) %}
        {% set y_max = (bed_mesh_max[1] - fuzz_max + margin_size, y_max) | min + (fuzz_range | random / 100.0) %}
    {% else %}
        {% set x_min = [ bed_mesh_min[0], x_min - margin_size ] | max %}
        {% set y_min = [ bed_mesh_min[1], y_min - margin_size ] | max %}
        {% set x_max = [ bed_mesh_max[0], x_max + margin_size ] | min %}
        {% set y_max = [ bed_mesh_max[1], y_max + margin_size ] | min %}
    {% endif %}

    { action_respond_info("Object bounds, clamped to the bed_mesh: {!r}, {!r}".format(
        (x_min, y_min),
        (x_max, y_max),
    )) }

    {% set points_x = (((x_max - x_min) / max_probe_point_distance_x) | round(method='ceil') | int) + 1 %}
    {% set points_y = (((y_max - y_min) / max_probe_point_distance_y) | round(method='ceil') | int) + 1 %}

    {% if params.PROFILE is defined %}
        {% set PROFILE2 =params.PROFILE %}
    {% else %}
        {% set PROFILE2 ="default" %}
    {% endif %}
    
    {% if (([points_x, points_y]|max) > 4) %}
        {% set algorithm = "bicubic" %}
        {% set min_points = 4 %}
    {% else %}
        {% set algorithm = "lagrange" %}
        {% set min_points = 3 %}
    {% endif %}
    { action_respond_info( "Algorithm: {}".format(algorithm)) }

    {% set points_x = [points_x, min_points]|max  %}
    {% set points_y = [points_y, min_points]|max  %}
    { action_respond_info( "Points: x: {}, y: {}".format(points_x, points_y) ) }

    {% if printer.configfile.settings.bed_mesh.relative_reference_index is defined %}
        {% set ref_index = (points_x * points_y / 2) | int %}
        { action_respond_info( "Reference index: {}".format(ref_index) ) }
    {% else %}
        {% set ref_index = -1 %}
    {% endif %}

    {% if probe_dock_enable == True %}
        {attach_macro}              # Attach/deploy a probe if the probe is stored somewhere outside of the print area
    {% endif %}

    {% if led_enable == True %}
        {status_macro}              # Set status LEDs
    {% endif %}

    _BED_MESH_CALIBRATE mesh_min={x_min},{y_min} mesh_max={x_max},{y_max} ALGORITHM={algorithm} PROBE_COUNT={points_x},{points_y} RELATIVE_REFERENCE_INDEX={ref_index} PROFILE={params.PROFILE|default("default")}

    {% if probe_dock_enable == True %}
        {detach_macro}              # Detach/stow a probe if the probe is stored somewhere outside of the print area
    {% endif %}

[gcode_macro SETUP_KAMP_MESHING]
gcode:
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=display_parameters   VALUE={params.DISPLAY_PARAMETERS|default(True)|int}
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=led_enable   VALUE={params.LED_ENABLE|default(False)|int}
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=status_macro VALUE='"{params.STATUS_MACRO|default('status_meshing')|string}"'

  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=fuzz_enable VALUE={params.FUZZ_ENABLE|default(False)|int}
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=fuzz_min    VALUE={params.FUZZ_MIN|default(0)|float}
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=fuzz_max    VALUE={params.FUZZ_MAX|default(4)|float}

  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=probe_dock_enable  VALUE={params.PROBE_DOCK_ENABLE|default(False)|int}
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=attach_macro VALUE='"{params.ATTACH_MACRO|default('Attach_Probe')|string}"'
  SET_GCODE_VARIABLE MACRO=BED_MESH_CALIBRATE VARIABLE=detach_macro VALUE='"{params.DETACH_MACRO|default('Dock_Probe')|string}"'



# # # Klipper Adaptive Purging - Line # # #

# This macro will parse information from objects in your gcode and create a nearby purge!
# For successful purging, you may need to configure:
# 
# [extruder]
# ...
# max_extrude_cross_section: 5

[gcode_macro LINE_PURGE]
description: A purge macro that adapts to be near your actual printed objects

variable_adaptive_enable: True      # Change to False if you'd like the purge to be in the same spot every print
variable_z_height: 0.4              # Height above the bed to purge
variable_purge_amount: 40           # Amount of filament in millimeters to purge
variable_line_length: 50            # Overall desired length of purge line in millimeters, around 1/5th of X axis length is a good starting value
variable_flow_rate: 12              # Desired flow rate in mm3/s (Around 12 for standard flow hotends, around 24 for high flow hotends)
variable_x_default: 10              # Default X location to purge. If adaptive_enable is True, this is overwritten
variable_y_default: 10              # Default Y location to purge. If adaptive_enable is True, this is overwritten
variable_distance_to_object_y: 10   # Y distance in millimeters away from the print area for purging. Must be less than or equal to y_default if adaptive_enable is False

### This section is for those who are using Moonraker's Update Manager for KAMP, or want a more verbose macro. ###

variable_display_parameters: True   # Display macro paramters in the console, useful for debugging the SETUP_LINE_PURGE call, or more verbosity.

gcode:

    {% if display_parameters == True %}
      { action_respond_info("adaptive_enable : %d" % (adaptive_enable))  }
      { action_respond_info("z_height        : %f" % (z_height))  }
      { action_respond_info("purge_amount    : %f" % (purge_amount))  }
      { action_respond_info("line_length     : %f" % (line_length))  }
      { action_respond_info("flow_rate       : %f" % (flow_rate))  }
      { action_respond_info("x_default       : %f" % (x_default))  }
      { action_respond_info("y_default       : %f" % (y_default))  }
      { action_respond_info("distance_to_object_y : %f" % (distance_to_object_y))  }
    {% endif %}

    {% if adaptive_enable == True %}
        {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
        {% set x_origin = (all_points | map(attribute=0) | min | default(x_default)) %}
        {% set y_origin = (all_points | map(attribute=1) | min | default(y_default)) %}
        {% set x_origin = ([x_origin, 0] | max) %}
        {% set y_origin = ([y_origin, 0] | max) %}
    {% else %}
        {% set x_origin = x_default | float %}
        {% set y_origin = y_default | float %}
    {% endif %}
    {% set nozzle_dia = printer.configfile.config.extruder.nozzle_diameter | float %}
    {% set cross_section = nozzle_dia * z_height | float %}
    {% set purge_move_speed = (cross_section * flow_rate) * 60 | float %}
    {% set travel_speed = (printer.toolhead.max_velocity) * 30 | float %}

    G92 E0                                                                              # Reset extruder
    G0 F{travel_speed}                                                                  # Set travel speed
    G90                                                                                 # Absolute positioning
    G0 X{x_origin} Y{y_origin - distance_to_object_y}                                   # Move to purge position
    G0 Z{z_height}                                                                      # Move to purge Z height
    M83                                                                                 # Relative extrusion mode
    G1 X{x_origin + line_length} E{purge_amount} F{purge_move_speed}                    # Purge line
    G1 E-.5 F2100                                                                       # Retract
    G92 E0                                                                              # Reset extruder distance
    M82                                                                                 # Absolute extrusion mode
    G0 Z{z_height * 2} F{travel_speed}                                                  # Z hop

[gcode_macro SETUP_LINE_PURGE]
gcode:
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=display_parameters   VALUE={params.DISPLAY_PARAMETERS|default(True)|int}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=adaptive_enable   VALUE={params.ADAPTIVE_ENABLE|default(True)|int}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=z_height      VALUE={params.Z_HEIGHT|default(0.4)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=purge_amount  VALUE={params.PURGE_AMOUNT|default(40)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=line_length  VALUE={params.LINE_LENGTH|default(50)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=flow_rate     VALUE={params.FLOW_RATE|default(12)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=x_default     VALUE={params.X_DEFAULT|default(10)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=y_default     VALUE={params.Y_DEFAULT|default(10)|float}
    SET_GCODE_VARIABLE MACRO=LINE_PURGE  VARIABLE=distance_to_object_y     VALUE={params.DISTANCE_TO_OBJECT_Y|default(10)|float}

