Creality-Helper-Script/files/kamp/Line_Purge.cfg
2024-03-10 21:45:05 +01:00

200 lines
7.6 KiB
INI

###########################################
# Line Purge for Creality K1 Series
###########################################
[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 %}
G92 E0
G1 Z0.1 F600
M83
{RETRACT}
SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=5
M204 S12000
SET_VELOCITY_LIMIT ACCEL_TO_DECEL=6000
M220 S100
M221 S100
G1 Z2.0 F1200
G1 X0.1 Y20 Z0.3 F6000.0
G1 X0.1 Y180.0 Z0.3 F3000.0 E10.0
G1 X0.4 Y180.0 Z0.3 F3000.0
G1 X0.4 Y20.0 Z0.3 F3000.0 E10.0
G1 Y10.0 F3000.0
G1 Z2.0 F3000.0
G92 E0
M82
G1 F12000
G21
{% 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 %}
G92 E0
G1 Z0.1 F600
M83
{RETRACT}
SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=5
M204 S12000
SET_VELOCITY_LIMIT ACCEL_TO_DECEL=6000
M220 S100
M221 S100
G1 Z2.0 F1200
G1 X0.1 Y20 Z0.3 F6000.0
G1 X0.1 Y180.0 Z0.3 F3000.0 E10.0
G1 X0.4 Y180.0 Z0.3 F3000.0
G1 X0.4 Y20.0 Z0.3 F3000.0 E10.0
G1 Y10.0 F3000.0
G1 Z2.0 F3000.0
G92 E0
M82
G1 F12000
G21
{% endif %}
RESTORE_GCODE_STATE NAME=Prepurge_State
{% endif %}