###########################################
# Purge Line Macros
###########################################

[gcode_macro _LINE_PURGE]
description: A purge macro that adapts to be near your actual printed objects
gcode:
    {% set travel_speed = (printer.toolhead.max_velocity) * 60 | float %}
    {% set cross_section = printer.configfile.settings.extruder.max_extrude_cross_section | float %}
    {% if printer.firmware_retraction is defined %}
        {% set RETRACT = G10 | string %}
        {% set UNRETRACT = G11 | string %}
    {% else %}
        {% set RETRACT = 'G1 E-0.5 F2400' | string %}
        {% set UNRETRACT = 'G1 E0.5 F2400' | string %}
    {% endif %}
    {% set bed_x_max = printer["gcode_macro PRINTER_PARAM"].max_x_position | float %}
    {% set bed_y_max = printer["gcode_macro PRINTER_PARAM"].max_y_position | float %}
    {% set verbose_enable = printer["gcode_macro _KAMP_Settings"].verbose_enable | abs %}
    {% set purge_height = printer["gcode_macro _KAMP_Settings"].purge_height | float %}
    {% set tip_distance = printer["gcode_macro _KAMP_Settings"].tip_distance | float %}
    {% set purge_margin = printer["gcode_macro _KAMP_Settings"].purge_margin | float %}
    {% set purge_amount = printer["gcode_macro _KAMP_Settings"].purge_amount | float %}
    {% set flow_rate = printer["gcode_macro _KAMP_Settings"].flow_rate | float %}
    {% set rapid_move = 10 %}
    {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
    {% set purge_x_min = (all_points | map(attribute=0) | min | default(0)) %}
    {% set purge_x_max = (all_points | map(attribute=0) | max | default(0)) %}
    {% set purge_y_min = (all_points | map(attribute=1) | min | default(0)) %}
    {% set purge_y_max = (all_points | map(attribute=1) | max | default(0)) %}
    {% set detect_object = purge_x_min + purge_x_max + purge_y_min + purge_y_max %}
    {% set purge_x_center = ([((purge_x_max + purge_x_min) / 2) - (purge_amount / 2), 0] | max) %}
    {% set purge_y_center = ([((purge_y_max + purge_y_min) / 2) - (purge_amount / 2), 0] | max) %}
    {% if (purge_x_center + purge_amount + rapid_move) > bed_x_max %}
        {% set purge_x_center = (bed_x_max - (purge_amount + rapid_move)) %}
    {% endif %}
    {% if (purge_y_center + purge_amount + rapid_move) > bed_y_max %}
        {% set purge_y_center = (bed_y_max - (purge_amount + rapid_move)) %}
    {% endif %}
    {% set purge_x_origin_low = (purge_x_min - purge_margin) %}
    {% set purge_x_origin_high = (purge_x_max + purge_margin) %}
    {% set purge_y_origin_low = (purge_y_min - purge_margin) %}
    {% set purge_y_origin_high = (purge_y_max + purge_margin) %}
    {% set purge_move_speed = (flow_rate / 5.0) * 60 | float %}

    {% if cross_section < 5 %}
        RESPOND TYPE=command MSG="[Extruder] max_extrude_cross_section is insufficient for line purge, please set it to 5 or greater. Purge skipped."
    {% else %}
        {% if verbose_enable == True %}
            RESPOND TYPE=command MSG="Moving filament tip {tip_distance}mm"
        {% endif %}
        {% if detect_object == 0 %}
            RESPOND TYPE=command MSG="No object detected! Using classic purge line."
        {% elif purge_y_origin_low > 0 %}
            RESPOND TYPE=command MSG="KAMP line purge starting at {purge_x_center}, {purge_y_origin_low} and purging {purge_amount}mm of filament, requested flow rate is {flow_rate}mm3/s."
        {% elif purge_x_origin_low > 0 %}
            RESPOND TYPE=command MSG="KAMP line purge starting at {purge_x_origin_low}, {purge_y_center} and purging {purge_amount}mm of filament, requested flow rate is {flow_rate}mm3/s."
        {% elif purge_y_origin_high < bed_y_max %}
            RESPOND TYPE=command MSG="KAMP line purge starting at {purge_x_center}, {purge_y_origin_high} and purging {purge_amount}mm of filament, requested flow rate is {flow_rate}mm3/s."
        {% elif purge_x_origin_high < bed_x_max %}
            RESPOND TYPE=command MSG="KAMP line purge starting at {purge_x_origin_high}, {purge_y_center} and purging {purge_amount}mm of filament, requested flow rate is {flow_rate}mm3/s."
        {% else %}
             RESPOND TYPE=command MSG="No space for purge line! Using classic purge line."
        {% endif %}
        SAVE_GCODE_STATE NAME=Prepurge_State
        {% if detect_object == 0 %}
            CX_PRINT_DRAW_ONE_LINE
        {% elif purge_y_origin_low > 0 %}
            G92 E0
            G0 F{travel_speed}
            G90
            G0 X{purge_x_center} Y{purge_y_origin_low}
            G0 Z{purge_height}
            M83
            G1 E{tip_distance} F{purge_move_speed}
            G1 X{purge_x_center + purge_amount} E{purge_amount} F{purge_move_speed}
            {RETRACT}
            G0 X{purge_x_center + purge_amount + rapid_move} F{travel_speed}         
            G92 E0
            M82
            G0 Z{purge_height * 2} F{travel_speed}
        {% elif purge_x_origin_low > 0 %}
            G92 E0
            G0 F{travel_speed}
            G90
            G0 X{purge_x_origin_low} Y{purge_y_center}
            G0 Z{purge_height}
            M83
            G1 E{tip_distance} F{purge_move_speed}
            G1 Y{purge_y_center + purge_amount} E{purge_amount} F{purge_move_speed}
            {RETRACT}
            G0 Y{purge_y_center + purge_amount + rapid_move} F{travel_speed}
            G92 E0
            M82
            G0 Z{purge_height * 2} F{travel_speed}
        {% elif purge_y_origin_high < bed_y_max %}
            G92 E0
            G0 F{travel_speed}
            G90
            G0 X{purge_x_center} Y{purge_y_origin_high}
            G0 Z{purge_height}
            M83
            G1 E{tip_distance} F{purge_move_speed}
            G1 X{purge_x_center + purge_amount} E{purge_amount} F{purge_move_speed}
            {RETRACT}
            G0 X{purge_x_center + purge_amount + rapid_move} F{travel_speed}
            G92 E0
            M82
            G0 Z{purge_height * 2} F{travel_speed}
        {% elif purge_x_origin_high < bed_x_max %}
            G92 E0
            G0 F{travel_speed}
            G90
            G0 X{purge_x_origin_high} Y{purge_y_center}
            G0 Z{purge_height}
            M83
            G1 E{tip_distance} F{purge_move_speed}
            G1 Y{purge_y_center + purge_amount} E{purge_amount} F{purge_move_speed}
            {RETRACT}
            G0 Y{purge_y_center + purge_amount + rapid_move} F{travel_speed}
            G92 E0
            M82
            G0 Z{purge_height * 2} F{travel_speed}
        {% else %}
            CX_PRINT_DRAW_ONE_LINE
        {% endif %}
        RESTORE_GCODE_STATE NAME=Prepurge_State
    {% endif %}
