Separate color change handing from emit_custom_color_change_gcode_per_print_z() into emit_custom_color_change_gcode_per_print_z().

This commit is contained in:
Lukáš Hejl 2024-01-25 17:04:38 +01:00
parent 145fb92e07
commit 1071645967

View File

@ -1901,40 +1901,27 @@ std::vector<GCodeGenerator::InstanceToPrint> GCodeGenerator::sort_print_object_i
namespace ProcessLayer namespace ProcessLayer
{ {
static std::string emit_custom_gcode_per_print_z( static std::string emit_custom_color_change_gcode_per_print_z(
GCodeGenerator &gcodegen, GCodeGenerator &gcodegen,
const CustomGCode::Item *custom_gcode, const CustomGCode::Item &custom_gcode,
unsigned int current_extruder_id, unsigned int current_extruder_id,
// ID of the first extruder printing this layer. unsigned int first_extruder_id, // ID of the first extruder printing this layer.
unsigned int first_extruder_id, const PrintConfig &config
const PrintConfig &config) ) {
{
std::string gcode;
bool single_extruder_printer = config.nozzle_diameter.size() == 1;
if (custom_gcode != nullptr) {
// Extruder switches are processed by LayerTools, they should be filtered out.
assert(custom_gcode->type != CustomGCode::ToolChange);
CustomGCode::Type gcode_type = custom_gcode->type;
const bool color_change = gcode_type == CustomGCode::ColorChange;
const bool tool_change = gcode_type == CustomGCode::ToolChange;
// Tool Change is applied as Color Change for a single extruder printer only.
assert(!tool_change || single_extruder_printer);
// we should add or not colorprint_change in respect to nozzle_diameter count instead of really used extruders count
if (color_change || tool_change) {
const bool single_extruder_multi_material = config.single_extruder_multi_material; const bool single_extruder_multi_material = config.single_extruder_multi_material;
const bool single_extruder_printer = config.nozzle_diameter.size() == 1; const bool single_extruder_printer = config.nozzle_diameter.size() == 1;
const bool color_change = custom_gcode.type == CustomGCode::ColorChange;
std::string gcode;
int color_change_extruder = -1; int color_change_extruder = -1;
if (color_change && custom_gcode->extruder > 0) if (color_change && custom_gcode.extruder > 0)
color_change_extruder = custom_gcode->extruder - 1; color_change_extruder = custom_gcode.extruder - 1;
assert(color_change_extruder >= 0); assert(color_change_extruder >= 0);
// Color Change or Tool Change as Color Change. // Color Change or Tool Change as Color Change.
// add tag for processor // add tag for processor
gcode += ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Color_Change) + ",T" + std::to_string(color_change_extruder) + "," + custom_gcode->color + "\n"; gcode += ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Color_Change) + ",T" + std::to_string(color_change_extruder) + "," + custom_gcode.color + "\n";
DynamicConfig cfg; DynamicConfig cfg;
cfg.set_key_value("color_change_extruder", new ConfigOptionInt(color_change_extruder)); cfg.set_key_value("color_change_extruder", new ConfigOptionInt(color_change_extruder));
@ -1952,9 +1939,35 @@ namespace ProcessLayer
// see GH issue #6362 // see GH issue #6362
gcodegen.writer().unretract(); gcodegen.writer().unretract();
} }
return gcode;
}
static std::string emit_custom_gcode_per_print_z(
GCodeGenerator &gcodegen,
const CustomGCode::Item &custom_gcode,
unsigned int current_extruder_id,
// ID of the first extruder printing this layer.
unsigned int first_extruder_id,
const PrintConfig &config)
{
std::string gcode;
// Extruder switches are processed by LayerTools, they should be filtered out.
assert(custom_gcode.type != CustomGCode::ToolChange);
CustomGCode::Type gcode_type = custom_gcode.type;
const bool color_change = gcode_type == CustomGCode::ColorChange;
const bool tool_change = gcode_type == CustomGCode::ToolChange;
// Tool Change is applied as Color Change for a single extruder printer only.
assert(!tool_change || config.nozzle_diameter.size() == 1);
// we should add or not colorprint_change in respect to nozzle_diameter count instead of really used extruders count
if (color_change || tool_change) {
gcode += emit_custom_color_change_gcode_per_print_z(gcodegen, custom_gcode, current_extruder_id, first_extruder_id, config);
} else { } else {
if (gcode_type == CustomGCode::PausePrint) { // Pause print if (gcode_type == CustomGCode::PausePrint) { // Pause print
const std::string pause_print_msg = custom_gcode->extra; const std::string pause_print_msg = custom_gcode.extra;
// add tag for processor // add tag for processor
gcode += ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Pause_Print) + "\n"; gcode += ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Pause_Print) + "\n";
@ -1971,11 +1984,10 @@ namespace ProcessLayer
if (gcode_type == CustomGCode::Template) // Template Custom Gcode if (gcode_type == CustomGCode::Template) // Template Custom Gcode
gcode += gcodegen.placeholder_parser_process("template_custom_gcode", config.template_custom_gcode, current_extruder_id); gcode += gcodegen.placeholder_parser_process("template_custom_gcode", config.template_custom_gcode, current_extruder_id);
else // custom Gcode else // custom Gcode
gcode += custom_gcode->extra; gcode += custom_gcode.extra;
} }
gcode += "\n"; gcode += "\n";
} }
}
return gcode; return gcode;
} }
@ -2243,10 +2255,10 @@ LayerResult GCodeGenerator::process_layer(
// Map from extruder ID to <begin, end> index of skirt loops to be extruded with that extruder. // Map from extruder ID to <begin, end> index of skirt loops to be extruded with that extruder.
std::map<unsigned int, std::pair<size_t, size_t>> skirt_loops_per_extruder; std::map<unsigned int, std::pair<size_t, size_t>> skirt_loops_per_extruder;
if (single_object_instance_idx == size_t(-1)) { if (single_object_instance_idx == size_t(-1) && layer_tools.custom_gcode != nullptr) {
// Normal (non-sequential) print. // Normal (non-sequential) print.
std::string custom_gcode = ProcessLayer::emit_custom_gcode_per_print_z(*this, layer_tools.custom_gcode, m_writer.extruder()->id(), first_extruder_id, print.config()); std::string custom_gcode = ProcessLayer::emit_custom_gcode_per_print_z(*this, *layer_tools.custom_gcode, m_writer.extruder()->id(), first_extruder_id, print.config());
if (layer_tools.custom_gcode != nullptr && layer_tools.custom_gcode->type == CustomGCode::ColorChange) { if (layer_tools.custom_gcode->type == CustomGCode::ColorChange) {
// We have a color change to do on this layer, but we want to do it immediately before the first extrusion instead of now, in order to fix GH #2672 // We have a color change to do on this layer, but we want to do it immediately before the first extrusion instead of now, in order to fix GH #2672
m_pending_pre_extrusion_gcode = custom_gcode; m_pending_pre_extrusion_gcode = custom_gcode;
} else { } else {