ENH: get start_gcode filament

Change-Id: Ia511f758d5c59ddc38c79f251e8c95511de4a235
This commit is contained in:
zhimin.zeng 2023-03-15 18:05:20 +08:00 committed by Lane.Wei
parent 81daebb5a5
commit dda90b1810
7 changed files with 54 additions and 11 deletions

View file

@ -1744,6 +1744,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
file.writeln(machine_start_gcode);
//BBS: gcode writer doesn't know where the real position of extruder is after inserting custom gcode
m_writer.set_current_position_clear(false);
m_start_gcode_filament = GCodeProcessor::get_gcode_last_filament(machine_start_gcode);
// Process filament-specific gcode.
/* if (has_wipe_tower) {
@ -4152,13 +4153,17 @@ std::string GCode::set_extruder(unsigned int extruder_id, double print_z)
int old_filament_temp, old_filament_e_feedrate;
float filament_area = float((M_PI / 4.f) * pow(m_config.filament_diameter.get_at(extruder_id), 2));
if (m_writer.extruder() != nullptr) {
//BBS: add handling for filament change in start gcode
int previous_extruder_id = -1;
if (m_writer.extruder() != nullptr || m_start_gcode_filament != -1) {
std::vector<float> flush_matrix(cast<float>(m_config.flush_volumes_matrix.values));
const unsigned int number_of_extruders = (unsigned int)(sqrt(flush_matrix.size()) + EPSILON);
assert(m_writer.extruder()->id() < number_of_extruders);
if (m_writer.extruder() != nullptr)
assert(m_writer.extruder()->id() < number_of_extruders);
else
assert(m_start_gcode_filament < number_of_extruders);
int previous_extruder_id = m_writer.extruder()->id();
previous_extruder_id = m_writer.extruder() != nullptr ? m_writer.extruder()->id() : m_start_gcode_filament;
old_retract_length = m_config.retraction_length.get_at(previous_extruder_id);
old_retract_length_toolchange = m_config.retract_length_toolchange.get_at(previous_extruder_id);
old_filament_temp = this->on_first_layer()? m_config.nozzle_temperature_initial_layer.get_at(previous_extruder_id) : m_config.nozzle_temperature.get_at(previous_extruder_id);
@ -4166,8 +4171,9 @@ std::string GCode::set_extruder(unsigned int extruder_id, double print_z)
wipe_volume *= m_config.flush_multiplier;
old_filament_e_feedrate = (int)(60.0 * m_config.filament_max_volumetric_speed.get_at(previous_extruder_id) / filament_area);
old_filament_e_feedrate = old_filament_e_feedrate == 0 ? 100 : old_filament_e_feedrate;
}
else {
//BBS: must clean m_start_gcode_filament
m_start_gcode_filament = -1;
} else {
old_retract_length = 0.f;
old_retract_length_toolchange = 0.f;
old_filament_temp = 0;
@ -4179,7 +4185,7 @@ std::string GCode::set_extruder(unsigned int extruder_id, double print_z)
new_filament_e_feedrate = new_filament_e_feedrate == 0 ? 100 : new_filament_e_feedrate;
DynamicConfig dyn_config;
dyn_config.set_key_value("previous_extruder", new ConfigOptionInt((int)(m_writer.extruder() != nullptr ? m_writer.extruder()->id() : -1)));
dyn_config.set_key_value("previous_extruder", new ConfigOptionInt(previous_extruder_id));
dyn_config.set_key_value("next_extruder", new ConfigOptionInt((int)extruder_id));
dyn_config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index));
dyn_config.set_key_value("layer_z", new ConfigOptionFloat(print_z));

View file

@ -486,6 +486,7 @@ private:
unsigned int m_toolchange_count;
coordf_t m_nominal_z;
bool m_need_change_layer_lift_z = false;
int m_start_gcode_filament = -1;
static bool gcode_label_objects;

View file

@ -1887,6 +1887,40 @@ template<typename T>
}
}
int GCodeProcessor::get_gcode_last_filament(const std::string& gcode_str)
{
int str_size = gcode_str.size();
int start_index = 0;
int end_index = 0;
int out_filament = -1;
while (end_index < str_size) {
if (gcode_str[end_index] != '\n') {
end_index++;
continue;
}
if (end_index > start_index) {
std::string line_str = gcode_str.substr(start_index, end_index - start_index);
line_str.erase(0, line_str.find_first_not_of(" "));
line_str.erase(line_str.find_last_not_of(" ") + 1);
if (line_str.empty() || line_str[0] != 'T') {
start_index = end_index + 1;
end_index = start_index;
continue;
}
int out = -1;
if (parse_number(line_str.substr(1), out) && out >= 0 && out < 255)
out_filament = out;
}
start_index = end_index + 1;
end_index = start_index;
}
return out_filament;
}
void GCodeProcessor::process_tags(const std::string_view comment, bool producers_enabled)
{
// producers tags

View file

@ -237,6 +237,8 @@ namespace Slic3r {
// (the first max_count found tags are returned into found_tag)
static bool contains_reserved_tags(const std::string& gcode, unsigned int max_count, std::vector<std::string>& found_tag);
static int get_gcode_last_filament(const std::string &gcode_str);
static const float Wipe_Width;
static const float Wipe_Height;

View file

@ -2509,7 +2509,7 @@ void PrintConfigDef::init_fff_params()
def = this->add("support_filament", coInt);
def->gui_type = ConfigOptionDef::GUIType::i_enum_open;
def->label = L("Support base");
def->label = L("Support/raft base");
def->category = L("Support");
def->tooltip = L("Filament to print support base and raft. \"Default\" means no specific filament for support and current filament is used");
def->min = 0;
@ -2534,7 +2534,7 @@ void PrintConfigDef::init_fff_params()
def = this->add("support_interface_filament", coInt);
def->gui_type = ConfigOptionDef::GUIType::i_enum_open;
def->label = L("Support interface");
def->label = L("Support/raft interface");
def->category = L("Support");
def->tooltip = L("Filament to print support interface. \"Default\" means no specific filament for support interface and current filament is used");
def->min = 0;