feature: Add granular print time placeholders for days, hours, minutes, and seconds (#13063)

This commit is contained in:
Matias Alejandro Yocca
2026-09-21 10:40:08 +08:00
committed by GitHub
parent 6e4be42a04
commit abf76490e4
9 changed files with 106 additions and 17 deletions
+8
View File
@@ -3032,6 +3032,10 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
// file_start_gcode runs before the parser copy that normally restores these, so set them here.
PlaceholderParser::update_timestamp(top_config);
PlaceholderParser::update_user_name(top_config);
top_config.set_key_value("print_time_total_sec", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Total_Sec_Placeholder)));
top_config.set_key_value("print_time_day", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Day_Placeholder)));
top_config.set_key_value("print_time_hour", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Hour_Placeholder)));
top_config.set_key_value("print_time_minute", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Minute_Placeholder)));
top_config.set_key_value("print_time_sec", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Sec_Placeholder)));
top_config.set_key_value("used_filament_length", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Used_Filament_Length_Placeholder)));
std::string top_gcode = print.placeholder_parser().process(top_gcode_template, 0, &top_config);
@@ -3661,6 +3665,10 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
this->placeholder_parser().set("hold_chamber_temp_for_flat_print", new ConfigOptionBool(hold_chamber_temp_for_flat_print));
}
this->placeholder_parser().set("print_time_total_sec", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Total_Sec_Placeholder)));
this->placeholder_parser().set("print_time_day", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Day_Placeholder)));
this->placeholder_parser().set("print_time_hour", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Hour_Placeholder)));
this->placeholder_parser().set("print_time_minute", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Minute_Placeholder)));
this->placeholder_parser().set("print_time_sec", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Print_Time_Sec_Placeholder)));
this->placeholder_parser().set("used_filament_length", new ConfigOptionString(GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Used_Filament_Length_Placeholder)));
+70 -8
View File
@@ -75,6 +75,10 @@ const std::vector<std::string> GCodeProcessor::Reserved_Tags = {
" WIPE_TOWER_START",
" WIPE_TOWER_END",
" PA_CHANGE:",
"@PRINT_TIME_TOTAL_SEC@",
"@PRINT_TIME_DAY@",
"@PRINT_TIME_HOUR@",
"@PRINT_TIME_MINUTE@",
"@PRINT_TIME_SEC@",
"@USED_FILAMENT_LENGTH@"
};
@@ -98,6 +102,10 @@ const std::vector<std::string> GCodeProcessor::Reserved_Tags_compatible = {
" WIPE_TOWER_START",
" WIPE_TOWER_END",
" PA_CHANGE:",
"@PRINT_TIME_TOTAL_SEC@",
"@PRINT_TIME_DAY@",
"@PRINT_TIME_HOUR@",
"@PRINT_TIME_MINUTE@",
"@PRINT_TIME_SEC@",
"@USED_FILAMENT_LENGTH@"
};
@@ -1215,22 +1223,76 @@ void GCodeProcessor::run_post_process()
return ret;
};
// Process inline placeholders (print_time_sec and used_filament_length)
// Process inline placeholders (print_time_total_sec, print_time_day, print_time_hour, print_time_minute, print_time_sec and used_filament_length)
auto process_inline_placeholders = [&](std::string& gcode_line) {
bool processed = false;
const std::string& print_time_placeholder = reserved_tag(ETags::Print_Time_Sec_Placeholder);
const std::string& print_time_total_placeholder = reserved_tag(ETags::Print_Time_Total_Sec_Placeholder);
const std::string& print_time_day_placeholder = reserved_tag(ETags::Print_Time_Day_Placeholder);
const std::string& print_time_hour_placeholder = reserved_tag(ETags::Print_Time_Hour_Placeholder);
const std::string& print_time_minute_placeholder = reserved_tag(ETags::Print_Time_Minute_Placeholder);
const std::string& print_time_sec_placeholder = reserved_tag(ETags::Print_Time_Sec_Placeholder);
const std::string& used_filament_placeholder = reserved_tag(ETags::Used_Filament_Length_Placeholder);
// Replace print_time_sec
size_t pos = gcode_line.find(print_time_placeholder);
double print_time_total_sec = m_time_processor.machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Normal)].time;
if (print_time_total_sec < 0.0)
print_time_total_sec = 0.0;
int total_seconds = static_cast<int>(print_time_total_sec);
int print_time_day = total_seconds / 86400;
int day_remainder_seconds = total_seconds % 86400;
int print_time_hour = day_remainder_seconds / 3600;
int print_time_minute = (day_remainder_seconds % 3600) / 60;
int print_time_sec = day_remainder_seconds % 60;
// Replace print_time_total_sec
size_t pos = gcode_line.find(print_time_total_placeholder);
while (pos != std::string::npos) {
double print_time_sec = m_time_processor.machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Normal)].time;
char buf[64];
sprintf(buf, "%.2f", print_time_sec);
gcode_line.replace(pos, print_time_placeholder.length(), buf);
sprintf(buf, "%.2f", print_time_total_sec);
gcode_line.replace(pos, print_time_total_placeholder.length(), buf);
processed = true;
pos = gcode_line.find(print_time_placeholder, pos + strlen(buf));
pos = gcode_line.find(print_time_total_placeholder, pos + strlen(buf));
}
// Replace print_time_day
pos = gcode_line.find(print_time_day_placeholder);
while (pos != std::string::npos) {
char buf[64];
sprintf(buf, "%d", print_time_day);
gcode_line.replace(pos, print_time_day_placeholder.length(), buf);
processed = true;
pos = gcode_line.find(print_time_day_placeholder, pos + strlen(buf));
}
// Replace print_time_hour
pos = gcode_line.find(print_time_hour_placeholder);
while (pos != std::string::npos) {
char buf[64];
sprintf(buf, "%d", print_time_hour);
gcode_line.replace(pos, print_time_hour_placeholder.length(), buf);
processed = true;
pos = gcode_line.find(print_time_hour_placeholder, pos + strlen(buf));
}
// Replace print_time_minute
pos = gcode_line.find(print_time_minute_placeholder);
while (pos != std::string::npos) {
char buf[64];
sprintf(buf, "%d", print_time_minute);
gcode_line.replace(pos, print_time_minute_placeholder.length(), buf);
processed = true;
pos = gcode_line.find(print_time_minute_placeholder, pos + strlen(buf));
}
// Replace print_time_sec
pos = gcode_line.find(print_time_sec_placeholder);
while (pos != std::string::npos) {
char buf[64];
sprintf(buf, "%d", print_time_sec);
gcode_line.replace(pos, print_time_sec_placeholder.length(), buf);
processed = true;
pos = gcode_line.find(print_time_sec_placeholder, pos + strlen(buf));
}
// Replace used_filament_length
+4 -2
View File
@@ -512,6 +512,10 @@ class Print;
Wipe_Tower_Start,
Wipe_Tower_End,
PA_Change,
Print_Time_Total_Sec_Placeholder,
Print_Time_Day_Placeholder,
Print_Time_Hour_Placeholder,
Print_Time_Minute_Placeholder,
Print_Time_Sec_Placeholder,
Used_Filament_Length_Placeholder,
};
@@ -1543,5 +1547,3 @@ class Print;
} /* namespace Slic3r */
#endif /* slic3r_GCodeProcessor_hpp_ */
+19 -3
View File
@@ -6610,7 +6610,7 @@ void PrintConfigDef::init_fff_params()
def->tooltip = L("G-code written at the very top of the output file, before any other content. "
"Useful for adding metadata that printer firmware reads from the first lines of the file "
"(e.g. estimated print time, filament usage). "
"Supports placeholders like {print_time_sec} and {used_filament_length}.");
"Supports placeholders like {print_time_total_sec}, {print_time_day}, {print_time_hour}, {print_time_minute}, {print_time_sec} and {used_filament_length}.");
def->multiline = true;
def->full_width = true;
def->height = 8;
@@ -12559,10 +12559,26 @@ PrintStatisticsConfigDef::PrintStatisticsConfigDef()
def->label = L("Used filament");
def->tooltip = L("Total length of filament used in the print.");
def = this->add("print_time_sec", coString);
def->label = L("Print time (seconds)");
def = this->add("print_time_total_sec", coString);
def->label = L("Print time (total seconds)");
def->tooltip = L("Total estimated print time in seconds. Replaced with actual value during post-processing.");
def = this->add("print_time_day", coString);
def->label = L("Print time (days component)");
def->tooltip = L("Estimated print time day component (normal mode). Replaced with actual value during post-processing.");
def = this->add("print_time_hour", coString);
def->label = L("Print time (hours component)");
def->tooltip = L("Estimated print time hour component (normal mode). Replaced with actual value during post-processing.");
def = this->add("print_time_minute", coString);
def->label = L("Print time (minutes component)");
def->tooltip = L("Estimated print time minute component (normal mode). Replaced with actual value during post-processing.");
def = this->add("print_time_sec", coString);
def->label = L("Print time (seconds component)");
def->tooltip = L("Estimated print time second component (normal mode). Replaced with actual value during post-processing.");
def = this->add("used_filament_length", coString);
def->label = L("Filament length (meters)");
def->tooltip = L("Total filament length used in meters. Replaced with actual value during post-processing.");