FIX: wrong flush logic

1.Fix flush calc logic
2.Rename m_extruder in GCodeWriter

jira:NEW

Signed-off-by: xun.zhang <xun.zhang@bambulab.com>
Change-Id: I38f023fbad983305632ca62cbfb3909759013c25
(cherry picked from commit f1b0805ed13eb94d6eb61e12330db3d628c0241b)
This commit is contained in:
xun.zhang 2024-07-10 15:54:27 +08:00 committed by Noisyfox
parent 0ad75a223b
commit aee14d307a
11 changed files with 150 additions and 127 deletions

View file

@ -214,7 +214,7 @@ double Extruder::retract_restart_extra_toolchange() const
double Extruder::travel_slope() const
{
return m_config->travel_slope.get_at(m_id) * PI / 180;
return m_config->travel_slope.get_at(extruder_id()) * PI / 180;
}
}

View file

@ -36,7 +36,7 @@ namespace Slic3r
return count;
};
uint64_t max_group_num = (1 << m_filament_num);
uint64_t max_group_num = static_cast<uint64_t>(1 << m_filament_num);
int best_cost = std::numeric_limits<int>::max();
std::vector<int>best_label;
@ -46,7 +46,7 @@ namespace Slic3r
continue;
std::set<int>group_0, group_1;
for (int j = 0; j < m_filament_num; ++j) {
if (i & (1 << j))
if (i & static_cast<uint64_t>(1 << j))
group_1.insert(m_used_filaments[j]);
else
group_0.insert(m_used_filaments[j]);

View file

@ -256,7 +256,7 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
{
std::string gcode;
unsigned int extruder_id = gcodegen.writer().extruder()->id();
unsigned int extruder_id = gcodegen.writer().filament()->id();
const auto& filament_idle_temp = gcodegen.config().idle_temperature;
if (filament_idle_temp.get_at(extruder_id) == 0) {
// There is no idle temperature defined in filament settings.
@ -281,7 +281,7 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
std::string OozePrevention::post_toolchange(GCode& gcodegen)
{
return (gcodegen.config().standby_temperature_delta.value != 0) ?
gcodegen.writer().set_temperature(this->_get_temp(gcodegen), true, gcodegen.writer().extruder()->id()) :
gcodegen.writer().set_temperature(this->_get_temp(gcodegen), true, gcodegen.writer().filament()->id()) :
std::string();
}
@ -290,9 +290,9 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
// First layer temperature should be used when on the first layer (obviously) and when
// "other layers" is set to zero (which means it should not be used).
return (gcodegen.layer() == nullptr || gcodegen.layer()->id() == 0
|| gcodegen.config().nozzle_temperature.get_at(gcodegen.writer().extruder()->id()) == 0)
? gcodegen.config().nozzle_temperature_initial_layer.get_at(gcodegen.writer().extruder()->id())
: gcodegen.config().nozzle_temperature.get_at(gcodegen.writer().extruder()->id());
|| gcodegen.config().nozzle_temperature.get_at(gcodegen.writer().filament()->id()) == 0)
? gcodegen.config().nozzle_temperature_initial_layer.get_at(gcodegen.writer().filament()->id())
: gcodegen.config().nozzle_temperature.get_at(gcodegen.writer().filament()->id());
}
// Orca:
@ -301,8 +301,8 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
Wipe::RetractionValues Wipe::calculateWipeRetractionLengths(GCode& gcodegen, bool toolchange) {
auto& writer = gcodegen.writer();
auto& config = gcodegen.config();
auto extruder = writer.extruder();
auto extruder_id = extruder->id();
auto extruder = writer.filament();
auto extruder_id = extruder->extruder_id();
auto last_pos = gcodegen.last_pos();
// Declare & initialize retraction lengths
@ -369,7 +369,7 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
amount of retraction. In other words, how far do we move in XY at wipe_speed
for the time needed to consume retraction_length at retraction_speed? */
// BBS
double wipe_dist = scale_(gcodegen.config().wipe_distance.get_at(gcodegen.writer().extruder() ->extruder_id()));
double wipe_dist = scale_(gcodegen.config().wipe_distance.get_at(gcodegen.writer().filament() ->extruder_id()));
/* Take the stored wipe path and replace first point with the current actual position
(they might be different, for example, in case of loop clipping). */
@ -489,12 +489,12 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
// Process the end filament gcode.
std::string end_filament_gcode_str;
if (gcodegen.writer().extruder() != nullptr) {
if (gcodegen.writer().filament() != nullptr) {
// Process the custom filament_end_gcode in case of single_extruder_multi_material.
unsigned int old_extruder_id = gcodegen.writer().extruder()->id();
const std::string &filament_end_gcode = gcodegen.config().filament_end_gcode.get_at(old_extruder_id);
if (gcodegen.writer().extruder() != nullptr && !filament_end_gcode.empty()) {
end_filament_gcode_str = gcodegen.placeholder_parser_process("filament_end_gcode", filament_end_gcode, old_extruder_id);
unsigned int old_filament_id = gcodegen.writer().filament()->id();
const std::string& filament_end_gcode = gcodegen.config().filament_end_gcode.get_at(old_filament_id);
if (gcodegen.writer().filament() != nullptr && !filament_end_gcode.empty()) {
end_filament_gcode_str = gcodegen.placeholder_parser_process("filament_end_gcode", filament_end_gcode, old_filament_id);
check_add_eol(end_filament_gcode_str);
}
}
@ -512,8 +512,8 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
// m_max_layer_z = std::max(m_max_layer_z, tcr.print_z);
if (!change_filament_gcode.empty()) {
DynamicConfig config;
int previous_filament_id = gcodegen.writer().extruder() ? (int)gcodegen.writer().extruder()->id() : -1;
int previous_extruder_id = gcodegen.writer().extruder() ? (int)gcodegen.writer().extruder()->extruder_id() : -1;
int previous_filament_id = gcodegen.writer().filament(new_extruder_id) ? (int)gcodegen.writer().filament(new_extruder_id)->id() : -1;
int previous_extruder_id = gcodegen.writer().filament(new_extruder_id) ? (int)gcodegen.writer().filament(new_extruder_id)->extruder_id() : -1;
config.set_key_value("previous_extruder", new ConfigOptionInt(previous_filament_id));
config.set_key_value("next_extruder", new ConfigOptionInt((int)new_filament_id));
@ -525,11 +525,11 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
{
GCodeWriter& gcode_writer = gcodegen.m_writer;
FullPrintConfig& full_config = gcodegen.m_config;
float old_retract_length = gcode_writer.extruder() != nullptr ? full_config.retraction_length.get_at(previous_extruder_id) : 0;
float old_retract_length = gcode_writer.filament(new_extruder_id) != nullptr ? full_config.retraction_length.get_at(previous_filament_id) : 0;
float new_retract_length = full_config.retraction_length.get_at(new_extruder_id);
float old_retract_length_toolchange = gcode_writer.extruder() != nullptr ? full_config.retract_length_toolchange.get_at(previous_extruder_id) : 0;
float old_retract_length_toolchange = gcode_writer.filament(new_extruder_id) != nullptr ? full_config.retract_length_toolchange.get_at(previous_filament_id) : 0;
float new_retract_length_toolchange = full_config.retract_length_toolchange.get_at(new_extruder_id);
int old_filament_temp = gcode_writer.extruder() != nullptr ? (gcodegen.on_first_layer()? full_config.nozzle_temperature_initial_layer.get_at(previous_filament_id) : full_config.nozzle_temperature.get_at(previous_filament_id)) : 210;
int old_filament_temp = gcode_writer.filament(new_extruder_id) != nullptr ? (gcodegen.on_first_layer()? full_config.nozzle_temperature_initial_layer.get_at(previous_filament_id) : full_config.nozzle_temperature.get_at(previous_filament_id)) : 210;
int new_filament_temp = gcodegen.on_first_layer() ? full_config.nozzle_temperature_initial_layer.get_at(new_filament_id) : full_config.nozzle_temperature.get_at(new_filament_id);
Vec3d nozzle_pos = gcode_writer.get_position();
@ -537,7 +537,7 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
float filament_area = float((M_PI / 4.f) * pow(full_config.filament_diameter.get_at(new_filament_id), 2));
float purge_length = purge_volume / filament_area;
int old_filament_e_feedrate = gcode_writer.extruder() != nullptr ? (int)(60.0 * full_config.filament_max_volumetric_speed.get_at(previous_filament_id) / filament_area) : 200;
int old_filament_e_feedrate = gcode_writer.filament(new_extruder_id) != nullptr ? (int)(60.0 * full_config.filament_max_volumetric_speed.get_at(previous_filament_id) / filament_area) : 200;
old_filament_e_feedrate = old_filament_e_feedrate == 0 ? 100 : old_filament_e_feedrate;
int new_filament_e_feedrate = (int)(60.0 * full_config.filament_max_volumetric_speed.get_at(new_filament_id) / filament_area);
new_filament_e_feedrate = new_filament_e_feedrate == 0 ? 100 : new_filament_e_feedrate;
@ -1011,8 +1011,8 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
const std::vector<std::string> ColorPrintColors::Colors = { "#C0392B", "#E67E22", "#F1C40F", "#27AE60", "#1ABC9C", "#2980B9", "#9B59B6" };
#define EXTRUDER_CONFIG(OPT) m_config.OPT.get_at(m_writer.extruder()->extruder_id())
#define FILAMENT_CONFIG(OPT) m_config.OPT.get_at(m_writer.extruder()->id())
#define EXTRUDER_CONFIG(OPT) m_config.OPT.get_at(m_writer.filament()->extruder_id())
#define FILAMENT_CONFIG(OPT) m_config.OPT.get_at(m_writer.filament()->id())
void GCode::PlaceholderParserIntegration::reset()
{
@ -2652,7 +2652,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
if (print.config().single_extruder_multi_material) {
// Process the filament_end_gcode for the active filament only.
int extruder_id = m_writer.extruder()->id();
int extruder_id = m_writer.filament()->id();
config.set_key_value("filament_extruder_id", new ConfigOptionInt(extruder_id));
file.writeln(this->placeholder_parser_process("filament_end_gcode", print.config().filament_end_gcode.get_at(extruder_id), extruder_id, &config));
} else {
@ -2662,7 +2662,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
file.writeln(this->placeholder_parser_process("filament_end_gcode", end_gcode, extruder_id, &config));
}
}
file.writeln(this->placeholder_parser_process("machine_end_gcode", print.config().machine_end_gcode, m_writer.extruder()->id(), &config));
file.writeln(this->placeholder_parser_process("machine_end_gcode", print.config().machine_end_gcode, m_writer.filament()->id(), &config));
}
file.write(m_writer.update_progress(m_layer_count, m_layer_count, true)); // 100%
file.write(m_writer.postamble());
@ -2949,7 +2949,7 @@ void GCode::process_layers(
tbb::parallel_pipeline(12, generator & cooling & fan_mover & pa_processor_filter & output);
}
std::string GCode::placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_extruder_id, const DynamicConfig *config_override)
std::string GCode::placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_filament_id, const DynamicConfig *config_override)
{
// Orca: Added CMake config option since debug is rarely used in current workflow.
// Also changed from throwing error immediately to storing messages till slicing is completed
@ -2990,7 +2990,7 @@ std::string GCode::placeholder_parser_process(const std::string &name, const std
PlaceholderParserIntegration &ppi = m_placeholder_parser_integration;
try {
ppi.update_from_gcodewriter(m_writer);
std::string output = ppi.parser.process(templ, current_extruder_id, config_override, &ppi.output_config, &ppi.context);
std::string output = ppi.parser.process(templ, current_filament_id, config_override, &ppi.output_config, &ppi.context);
ppi.validate_output_vector_variables();
if (const std::vector<double> &pos = ppi.opt_position->values; ppi.position != pos) {
@ -3691,7 +3691,7 @@ LayerResult GCode::process_layer(
config.set_key_value("layer_z", new ConfigOptionFloat(print_z));
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
gcode += this->placeholder_parser_process("before_layer_change_gcode",
print.config().before_layer_change_gcode.value, m_writer.extruder()->id(), &config)
print.config().before_layer_change_gcode.value, m_writer.filament()->id(), &config)
+ "\n";
}
@ -3713,7 +3713,7 @@ LayerResult GCode::process_layer(
config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index));
config.set_key_value("layer_z", new ConfigOptionFloat(print_z));
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
gcode_res = this->placeholder_parser_process("timelapse_gcode", print.config().time_lapse_gcode.value, m_writer.extruder()->id(), &config) + "\n";
gcode_res = this->placeholder_parser_process("timelapse_gcode", print.config().time_lapse_gcode.value, m_writer.filament()->id(), &config) + "\n";
}
return gcode_res;
};
@ -3743,7 +3743,7 @@ LayerResult GCode::process_layer(
config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index));
config.set_key_value("layer_z", new ConfigOptionFloat(print_z));
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
gcode += this->placeholder_parser_process("timelapse_gcode", print.config().time_lapse_gcode.value, m_writer.extruder()->id(),
gcode += this->placeholder_parser_process("timelapse_gcode", print.config().time_lapse_gcode.value, m_writer.filament()->id(),
&config) +
"\n";
}
@ -3753,7 +3753,7 @@ LayerResult GCode::process_layer(
config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index));
config.set_key_value("layer_z", new ConfigOptionFloat(print_z));
gcode += this->placeholder_parser_process("layer_change_gcode",
print.config().layer_change_gcode.value, m_writer.extruder()->id(), &config)
print.config().layer_change_gcode.value, m_writer.filament()->id(), &config)
+ "\n";
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
}
@ -3864,7 +3864,7 @@ LayerResult GCode::process_layer(
// nozzle_temperature_initial_layer vs. temperature settings.
for (const Extruder &extruder : m_writer.extruders()) {
if ((print.config().single_extruder_multi_material.value || m_ooze_prevention.enable) &&
extruder.id() != m_writer.extruder()->id())
extruder.id() != m_writer.filament()->id())
// In single extruder multi material mode, set the temperature for the current extruder only.
continue;
int temperature = print.config().nozzle_temperature.get_at(extruder.id());
@ -3881,7 +3881,7 @@ LayerResult GCode::process_layer(
if (single_object_instance_idx == size_t(-1)) {
// Normal (non-sequential) print.
gcode += ProcessLayer::emit_custom_gcode_per_print_z(*this, layer_tools.custom_gcode, m_writer.extruder()->id(), first_extruder_id, print.config());
gcode += ProcessLayer::emit_custom_gcode_per_print_z(*this, layer_tools.custom_gcode, m_writer.filament()->id(), first_extruder_id, print.config());
}
// BBS: get next extruder according to flush and soluble
@ -5306,7 +5306,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
_mm3_per_mm *= m_config.scarf_joint_flow_ratio;
// Effective extrusion length per distance unit = (filament_flow_ratio/cross_section) * mm3_per_mm / print flow ratio
// m_writer.extruder()->e_per_mm3() below is (filament flow ratio / cross-sectional area)
double e_per_mm = m_writer.extruder()->e_per_mm3() * _mm3_per_mm;
double e_per_mm = m_writer.filament()->e_per_mm3() * _mm3_per_mm;
e_per_mm /= filament_flow_ratio;
@ -5522,7 +5522,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index + 1));
config.set_key_value("layer_z", new ConfigOptionFloat(m_layer == nullptr ? m_last_height : m_layer->print_z));
gcode += this->placeholder_parser_process("change_extrusion_role_gcode",
m_config.change_extrusion_role_gcode.value, m_writer.extruder()->id(), &config)
m_config.change_extrusion_role_gcode.value, m_writer.filament()->id(), &config)
+ "\n";
}
@ -5585,7 +5585,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
if (m_multi_flow_segment_path_average_mm3_per_mm > 0) {
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
m_multi_flow_segment_path_average_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -5598,7 +5598,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
// to issue a zero flow PA change command for this
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -5704,7 +5704,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
}
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -5719,7 +5719,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
}
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -5912,7 +5912,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
}
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -5927,7 +5927,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
}
sprintf(buf, ";%sT%u MM3MM:%g ACCEL:%u BR:%d RC:%d OV:%d\n",
GCodeProcessor::reserved_tag(GCodeProcessor::ETags::PA_Change).c_str(),
m_writer.extruder()->id(),
m_writer.filament()->id(),
_mm3_per_mm,
acceleration_i,
((path.role() == erBridgeInfill) ||(path.role() == erOverhangPerimeter)),
@ -6267,7 +6267,7 @@ bool GCode::needs_retraction(const Polyline &travel, ExtrusionRole role, LiftTyp
float max_z_hop = 0.f;
for (int i = 0; i < m_config.z_hop.size(); i++)
max_z_hop = std::max(max_z_hop, (float)m_config.z_hop.get_at(i));
float travel_len_thresh = scale_(max_z_hop / tan(this->writer().extruder()->travel_slope()));
float travel_len_thresh = scale_(max_z_hop / tan(this->writer().filament()->travel_slope()));
float accum_len = 0.f;
Polyline clipped_travel;
@ -6327,7 +6327,7 @@ std::string GCode::retract(bool toolchange, bool is_last_retraction, LiftType li
{
std::string gcode;
if (m_writer.extruder() == nullptr)
if (m_writer.filament() == nullptr)
return gcode;
// wipe (if it's enabled for this extruder and we have a stored wipe path and no-zero wipe distance)
@ -6350,7 +6350,7 @@ std::string GCode::retract(bool toolchange, bool is_last_retraction, LiftType li
RetractLiftEnforceType retract_lift_type = RetractLiftEnforceType(EXTRUDER_CONFIG(retract_lift_enforce));
bool needs_lift = toolchange
|| m_writer.extruder()->retraction_length() > 0
|| m_writer.filament()->retraction_length() > 0
|| m_config.use_firmware_retraction;
bool last_fill_extrusion_role_top_infill = (this->m_last_notgapfill_extrusion_role == ExtrusionRole::erTopSolidInfill || this->m_last_notgapfill_extrusion_role == ExtrusionRole::erIroning);
@ -6429,18 +6429,18 @@ std::string GCode::set_extruder(unsigned int filament_id, double print_z, bool b
if (by_object)
m_writer.add_object_change_labels(gcode);
if (m_writer.extruder() != nullptr) {
if (m_writer.filament() != nullptr) {
// Process the custom filament_end_gcode. set_extruder() is only called if there is no wipe tower
// so it should not be injected twice.
unsigned int old_extruder_id = m_writer.extruder()->id();
const std::string &filament_end_gcode = m_config.filament_end_gcode.get_at(old_extruder_id);
unsigned int old_filament_id = m_writer.filament()->id();
const std::string &filament_end_gcode = m_config.filament_end_gcode.get_at(old_filament_id);
if (! filament_end_gcode.empty()) {
DynamicConfig config;
config.set_key_value("layer_num", new ConfigOptionInt(m_layer_index));
config.set_key_value("layer_z", new ConfigOptionFloat(m_writer.get_position().z() - m_config.z_offset.value));
config.set_key_value("max_layer_z", new ConfigOptionFloat(m_max_layer_z));
config.set_key_value("filament_extruder_id", new ConfigOptionInt(int(old_extruder_id)));
gcode += placeholder_parser_process("filament_end_gcode", filament_end_gcode, old_extruder_id, &config);
config.set_key_value("filament_extruder_id", new ConfigOptionInt(int(get_extruder_id(old_filament_id))));
gcode += placeholder_parser_process("filament_end_gcode", filament_end_gcode, old_filament_id, &config);
check_add_eol(gcode);
}
}
@ -6448,7 +6448,7 @@ std::string GCode::set_extruder(unsigned int filament_id, double print_z, bool b
// If ooze prevention is enabled, park current extruder in the nearest
// standby point and set it to the standby temperature.
if (m_ooze_prevention.enable && m_writer.extruder() != nullptr)
if (m_ooze_prevention.enable && m_writer.filament() != nullptr)
gcode += m_ooze_prevention.pre_toolchange(*this);
// BBS
@ -6466,23 +6466,34 @@ std::string GCode::set_extruder(unsigned int filament_id, double print_z, bool b
float filament_area = float((M_PI / 4.f) * pow(m_config.filament_diameter.get_at(filament_id), 2));
//BBS: add handling for filament change in start gcode
int previous_filament_id = -1;
if (m_writer.extruder() != nullptr || m_start_gcode_filament != -1) {
std::vector<float> flush_matrix(cast<float>(get_flush_volumes_matrix(m_config.flush_volumes_matrix.values, 0, m_config.nozzle_diameter.values.size())));
if (m_writer.filament() != nullptr || m_start_gcode_filament != -1) {
std::vector<float> flush_matrix(cast<float>(get_flush_volumes_matrix(m_config.flush_volumes_matrix.values, extruder_id, m_config.nozzle_diameter.values.size())));
const unsigned int number_of_extruders = (unsigned int) (m_config.filament_colour.values.size()); // if is multi_extruder only use the fist extruder matrix
if (m_writer.extruder() != nullptr)
assert(m_writer.extruder()->id() < number_of_extruders);
if (m_writer.filament() != nullptr)
assert(m_writer.filament()->id() < number_of_extruders);
else
assert(m_start_gcode_filament < number_of_extruders);
previous_filament_id = m_writer.extruder() != nullptr ? m_writer.extruder()->id() : m_start_gcode_filament;
int previous_extruder_id = m_writer.extruder() != nullptr ? m_writer.extruder()->extruder_id() : m_start_gcode_filament;
previous_filament_id = m_writer.filament() != nullptr ? m_writer.filament()->id() : m_start_gcode_filament;
int previous_extruder_id = m_writer.filament() != nullptr ? m_writer.filament()->extruder_id() : get_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_filament_id) : m_config.nozzle_temperature.get_at(previous_filament_id);
//Orca: always calculate wipe volume and hence provide correct flush_length, so that MMU devices with cutter and purge bin (e.g. ERCF_v2 with a filament cutter or Filametrix can take advantage of it)
wipe_volume = flush_matrix[previous_filament_id * number_of_extruders + filament_id];
wipe_volume *= m_config.flush_multiplier.get_at(0); // if is multi_extruder only use the fist extruder matrix
if (previous_extruder_id != extruder_id) {
//calc flush volume between the same extruder id
int previous_filament_id_in_new_extruder = m_writer.filament(extruder_id) != nullptr ? m_writer.filament(extruder_id)->id() : -1;
if (previous_filament_id_in_new_extruder == -1)
wipe_volume = 0;
else {
wipe_volume = flush_matrix[previous_filament_id_in_new_extruder * number_of_extruders + filament_id];
wipe_volume *= m_config.flush_multiplier.get_at(extruder_id);
}
}
else {
wipe_volume = flush_matrix[previous_filament_id * number_of_extruders + filament_id];
wipe_volume *= m_config.flush_multiplier.get_at(extruder_id); // if is multi_extruder only use the fist extruder matrix
}
old_filament_e_feedrate = (int) (60.0 * m_config.filament_max_volumetric_speed.get_at(previous_filament_id) / filament_area);
old_filament_e_feedrate = old_filament_e_feedrate == 0 ? 100 : old_filament_e_feedrate;
@ -6699,9 +6710,9 @@ Vec2d GCode::point_to_gcode(const Point &point) const
Point GCode::gcode_to_point(const Vec2d &point) const
{
Vec2d pt = point - m_origin;
if (const Extruder *extruder = m_writer.extruder(); extruder)
if (const Extruder *extruder = m_writer.filament(); extruder)
// This function may be called at the very start from toolchange G-code when the extruder is not assigned yet.
pt += m_config.extruder_offset.get_at(extruder->id());
pt += m_config.extruder_offset.get_at(extruder->extruder_id());
return scaled<coord_t>(pt);
}

View file

@ -214,7 +214,7 @@ public:
const PlaceholderParser& placeholder_parser() const { return m_placeholder_parser_integration.parser; }
// Process a template through the placeholder parser, collect error messages to be reported
// inside the generated string and after the G-code export finishes.
std::string placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_extruder_id, const DynamicConfig *config_override = nullptr);
std::string placeholder_parser_process(const std::string &name, const std::string &templ, unsigned int current_filament_id, const DynamicConfig *config_override = nullptr);
bool enable_cooling_markers() const { return m_enable_cooling_markers; }
std::string extrusion_role_to_string_for_parser(const ExtrusionRole &);

View file

@ -773,7 +773,7 @@ static bool need_wipe(const GCode &gcodegen,
const Polyline &result_travel,
const size_t intersection_count)
{
bool z_lift_enabled = gcodegen.config().z_hop.get_at(gcodegen.writer().extruder()->extruder_id()) > 0.;
bool z_lift_enabled = gcodegen.config().z_hop.get_at(gcodegen.writer().filament()->id()) > 0.;
bool wipe_needed = false;
// If the original unmodified path doesn't have any intersection with boundary, then it is entirely inside the object otherwise is entirely

View file

@ -8,7 +8,7 @@
#include <utility>
#include <boost/container/small_vector.hpp>
#include "FilamentGroup.hpp"
#include "../FilamentGroup.hpp"
#include "ExtrusionEntity.hpp"
#include "PrintConfig.hpp"

View file

@ -46,11 +46,10 @@ void GCodeWriter::apply_print_config(const PrintConfig &print_config)
void GCodeWriter::set_extruders(std::vector<unsigned int> extruder_ids)
{
std::sort(extruder_ids.begin(), extruder_ids.end());
m_extruder = nullptr; // this points to object inside `m_extruders`, so should be cleared too
m_extruders.clear();
m_extruders.reserve(extruder_ids.size());
m_filament_extruders.clear();
m_filament_extruders.reserve(extruder_ids.size());
for (unsigned int extruder_id : extruder_ids)
m_extruders.emplace_back(Extruder(extruder_id, &this->config, config.single_extruder_multi_material.value));
m_filament_extruders.emplace_back(Extruder(extruder_id, &this->config, config.single_extruder_multi_material.value));
/* we enable support for multiple extruder if any extruder greater than 0 is used
(even if prints only uses that one) since we need to output Tx commands
@ -407,10 +406,10 @@ std::string GCodeWriter::reset_e(bool force)
|| FLAVOR_IS(gcfSailfish))
return "";
if (m_extruder != nullptr) {
if (is_zero(m_extruder->E()) && ! force)
if (m_curr_extruder_id!=-1 && m_curr_filament_extruder[m_curr_extruder_id] != nullptr) {
if (is_zero(m_curr_filament_extruder[m_curr_extruder_id]->E()) && ! force)
return "";
m_extruder->reset_E();
m_curr_filament_extruder[m_curr_extruder_id]->reset_E();
}
if (! this->config.use_relative_e_distances) {
@ -452,12 +451,13 @@ std::string GCodeWriter::toolchange_prefix() const
FLAVOR_IS(gcfSailfish) ? "M108 T" : "T";
}
std::string GCodeWriter::toolchange(unsigned int extruder_id)
std::string GCodeWriter::toolchange(unsigned int filament_id)
{
// set the new extruder
auto it_extruder = Slic3r::lower_bound_by_predicate(m_extruders.begin(), m_extruders.end(), [extruder_id](const Extruder &e) { return e.id() < extruder_id; });
assert(it_extruder != m_extruders.end() && it_extruder->id() == extruder_id);
m_extruder = &*it_extruder;
auto filament_extruder_iter = Slic3r::lower_bound_by_predicate(m_filament_extruders.begin(), m_filament_extruders.end(), [filament_id](const Extruder &e) { return e.id() < filament_id; });
assert(filament_extruder_iter != m_filament_extruders.end() && filament_extruder_iter->id() == filament_id);
m_curr_extruder_id = filament_extruder_iter->extruder_id();
m_curr_filament_extruder[m_curr_extruder_id] = &*filament_extruder_iter;
// return the toolchange command
// if we are running a single-extruder setup, just set the extruder and return nothing
@ -465,9 +465,9 @@ std::string GCodeWriter::toolchange(unsigned int extruder_id)
if (this->multiple_extruders || (this->config.filament_diameter.values.size() > 1 && !is_bbl_printers())) {
// BBS
if (this->m_is_bbl_printers)
gcode << "M1020 S" << extruder_id;
gcode << "M1020 S" << filament_id;
else
gcode << this->toolchange_prefix() << extruder_id;
gcode << this->toolchange_prefix() << filament_id;
//BBS
if (GCodeWriter::full_gcode_comment)
gcode << " ; change extruder";
@ -550,7 +550,7 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
//BBS: SpiralLift
if (m_to_lift_type == LiftType::SpiralLift && this->is_current_position_clear()) {
//BBS: todo: check the arc move all in bed area, if not, then use lazy lift
double radius = delta(2) / (2 * PI * atan(this->extruder()->travel_slope()));
double radius = delta(2) / (2 * PI * atan(this->filament()->travel_slope()));
Vec2d ij_offset = radius * delta_no_z.normalized();
ij_offset = { -ij_offset(1), ij_offset(0) };
slop_move = this->_spiral_travel_to_z(target(2), ij_offset, "spiral lift Z");
@ -558,11 +558,11 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
//BBS: LazyLift
else if (m_to_lift_type == LiftType::LazyLift &&
this->is_current_position_clear() &&
atan2(delta(2), delta_no_z.norm()) < this->extruder()->travel_slope()) {
atan2(delta(2), delta_no_z.norm()) < this->filament()->travel_slope()) {
//BBS: check whether we can make a travel like
// _____
// / to make the z list early to avoid to hit some warping place when travel is long.
Vec2d temp = delta_no_z.normalized() * delta(2) / tan(this->extruder()->travel_slope());
Vec2d temp = delta_no_z.normalized() * delta(2) / tan(this->filament()->travel_slope());
Vec3d slope_top_point = Vec3d(temp(0), temp(1), delta(2)) + source;
GCodeG1Formatter w0;
w0.emit_xyz(slope_top_point);
@ -719,7 +719,7 @@ std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std:
force_no_extrusion = true;
if (!force_no_extrusion)
m_extruder->extrude(dE);
filament()->extrude(dE);
//BBS: take plate offset into consider
Vec2d point_on_plate = { point(0) - m_x_offset, point(1) - m_y_offset };
@ -727,7 +727,7 @@ std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std:
GCodeG1Formatter w;
w.emit_xy(point_on_plate);
if (!force_no_extrusion)
w.emit_e(m_extruder->E());
w.emit_e(filament()->E());
//BBS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
@ -741,7 +741,7 @@ std::string GCodeWriter::extrude_arc_to_xy(const Vec2d& point, const Vec2d& cent
m_pos(0) = point(0);
m_pos(1) = point(1);
if (!force_no_extrusion)
m_extruder->extrude(dE);
filament()->extrude(dE);
Vec2d point_on_plate = { point(0) - m_x_offset, point(1) - m_y_offset };
@ -749,7 +749,7 @@ std::string GCodeWriter::extrude_arc_to_xy(const Vec2d& point, const Vec2d& cent
w.emit_xy(point_on_plate);
w.emit_ij(center_offset);
if (!force_no_extrusion)
w.emit_e(m_extruder->E());
w.emit_e(filament()->E());
//BBS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
@ -760,7 +760,7 @@ std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std
m_pos = point;
m_lifted = 0;
if (!force_no_extrusion)
m_extruder->extrude(dE);
filament()->extrude(dE);
//BBS: take plate offset into consider
Vec3d point_on_plate = { point(0) - m_x_offset, point(1) - m_y_offset, point(2) };
@ -768,7 +768,7 @@ std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std
GCodeG1Formatter w;
w.emit_xyz(point_on_plate);
if (!force_no_extrusion)
w.emit_e(m_extruder->E());
w.emit_e(filament()->E());
//BBS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
@ -776,22 +776,22 @@ std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std
std::string GCodeWriter::retract(bool before_wipe, double retract_length)
{
double factor = before_wipe ? m_extruder->retract_before_wipe() : 1.;
double factor = before_wipe ? filament()->retract_before_wipe() : 1.;
assert(factor >= 0. && factor <= 1. + EPSILON);
return this->_retract(
retract_length > EPSILON ? retract_length : factor * m_extruder->retraction_length(),
factor * m_extruder->retract_restart_extra(),
retract_length > EPSILON ? retract_length : factor * filament()->retraction_length(),
factor * filament()->retract_restart_extra(),
"retract"
);
}
std::string GCodeWriter::retract_for_toolchange(bool before_wipe, double retract_length)
{
double factor = before_wipe ? m_extruder->retract_before_wipe() : 1.;
double factor = before_wipe ? filament()->retract_before_wipe() : 1.;
assert(factor >= 0. && factor <= 1. + EPSILON);
return this->_retract(
retract_length > EPSILON ? retract_length : factor * m_extruder->retract_length_toolchange(),
factor * m_extruder->retract_restart_extra_toolchange(),
retract_length > EPSILON ? retract_length : factor * filament()->retract_length_toolchange(),
factor * filament()->retract_restart_extra_toolchange(),
"retract for toolchange"
);
}
@ -805,15 +805,15 @@ std::string GCodeWriter::_retract(double length, double restart_extra, const std
length = 1;
std::string gcode;
if (double dE = m_extruder->retract(length, restart_extra); !is_zero(dE)) {
if (double dE = filament()->retract(length, restart_extra); !is_zero(dE)) {
if (this->config.use_firmware_retraction) {
gcode = FLAVOR_IS(gcfMachinekit) ? "G22 ; retract\n" : "G10 ; retract\n";
}
else {
// BBS
GCodeG1Formatter w;
w.emit_e(m_extruder->E());
w.emit_f(m_extruder->retract_speed() * 60.);
w.emit_e(filament()->E());
w.emit_f(filament()->retract_speed() * 60.);
// BBS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
gcode = w.string();
@ -833,7 +833,7 @@ std::string GCodeWriter::unretract()
if (FLAVOR_IS(gcfMakerWare))
gcode = "M101 ; extruder on\n";
if (double dE = m_extruder->unretract(); !is_zero(dE)) {
if (double dE = filament()->unretract(); !is_zero(dE)) {
if (this->config.use_firmware_retraction) {
gcode += FLAVOR_IS(gcfMachinekit) ? "G23 ; unretract\n" : "G11 ; unretract\n";
gcode += this->reset_e();
@ -842,8 +842,8 @@ std::string GCodeWriter::unretract()
//BBS
// use G1 instead of G0 because G0 will blend the restart with the previous travel move
GCodeG1Formatter w;
w.emit_e(m_extruder->E());
w.emit_f(m_extruder->deretract_speed() * 60.);
w.emit_e(filament()->E());
w.emit_f(filament()->deretract_speed() * 60.);
//BBS
w.emit_comment(GCodeWriter::full_gcode_comment, " ; unretract");
gcode += w.string();
@ -861,7 +861,7 @@ std::string GCodeWriter::lift(LiftType lift_type, bool spiral_vase)
// check whether the above/below conditions are met
double target_lift = 0;
{
int extruder_id = m_extruder->extruder_id();
int extruder_id = filament()->extruder_id();
double above = this->config.retract_lift_above.get_at(extruder_id);
double below = this->config.retract_lift_below.get_at(extruder_id);
if (m_pos(2) >= above && (below == 0 || m_pos(2) <= below))
@ -985,6 +985,21 @@ void GCodeWriter::add_object_change_labels(std::string& gcode)
add_object_start_labels(gcode);
}
std::string GCodeWriter::set_extruder(unsigned int filament_id)
{
auto filament_ext_it = Slic3r::lower_bound_by_predicate(m_filament_extruders.begin(), m_filament_extruders.end(), [filament_id](const Extruder &e) { return e.id() < filament_id; });
unsigned int extruder_id = filament_ext_it->extruder_id();
assert(filament_ext_it != m_filament_extruders.end() && filament_ext_it->id() == filament_id);
//TODO: optmize here, pass extruder_id to toolchange
return this->need_toolchange(filament_id) ? this->toolchange(filament_id) : "";
}
bool GCodeWriter::need_toolchange(unsigned int filament_id)const
{
return filament()==nullptr || filament()->id()!=filament_id;
}
void GCodeFormatter::emit_axis(const char axis, const double v, size_t digits) {
assert(digits <= 9);
static constexpr const std::array<int, 10> pow_10{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};

View file

@ -15,9 +15,10 @@ class GCodeWriter {
public:
GCodeConfig config;
bool multiple_extruders;
GCodeWriter() :
multiple_extruders(false), m_extruder(nullptr),
GCodeWriter() :
multiple_extruders(false), m_curr_filament_extruder{ nullptr,nullptr },
m_curr_extruder_id (-1),
m_single_extruder_multi_material(false),
m_last_acceleration(0), m_max_acceleration(0),m_last_travel_acceleration(0), m_max_travel_acceleration(0),
m_last_jerk(0), m_max_jerk_x(0), m_max_jerk_y(0),
@ -27,18 +28,20 @@ public:
m_to_lift_type(LiftType::NormalLift),
m_current_speed(3600), m_is_first_layer(true)
{}
Extruder* extruder() { return m_extruder; }
const Extruder* extruder() const { return m_extruder; }
Extruder* filament(size_t extruder_id) { assert(extruder_id < m_curr_filament_extruder.size()); return m_curr_filament_extruder[extruder_id]; }
const Extruder* filament(size_t extruder_id) const { assert(extruder_id < m_curr_filament_extruder.size()); return m_curr_filament_extruder[extruder_id]; }
Extruder* filament() { if (m_curr_extruder_id == -1) return nullptr; return m_curr_filament_extruder[m_curr_extruder_id]; }
const Extruder* filament() const { if(m_curr_extruder_id==-1) return nullptr; return m_curr_filament_extruder[m_curr_extruder_id]; }
void apply_print_config(const PrintConfig &print_config);
// Extruders are expected to be sorted in an increasing order.
void set_extruders(std::vector<unsigned int> extruder_ids);
const std::vector<Extruder>& extruders() const { return m_extruders; }
std::vector<unsigned int> extruder_ids() const {
std::vector<unsigned int> out;
out.reserve(m_extruders.size());
for (const Extruder &e : m_extruders)
out.push_back(e.id());
const std::vector<Extruder>& extruders() const { return m_filament_extruders; }
std::vector<unsigned int> extruder_ids() const {
std::vector<unsigned int> out;
out.reserve(m_filament_extruders.size());
for (const Extruder &e : m_filament_extruders)
out.push_back(e.id());
return out;
}
std::string preamble();
@ -59,14 +62,12 @@ public:
std::string reset_e(bool force = false);
std::string update_progress(unsigned int num, unsigned int tot, bool allow_100 = false) const;
// return false if this extruder was already selected
bool need_toolchange(unsigned int extruder_id) const
{ return m_extruder == nullptr || m_extruder->id() != extruder_id; }
std::string set_extruder(unsigned int extruder_id)
{ return this->need_toolchange(extruder_id) ? this->toolchange(extruder_id) : ""; }
bool need_toolchange(unsigned int filament_id) const;
std::string set_extruder(unsigned int filament_id);
// Prefix of the toolchange G-code line, to be used by the CoolingBuffer to separate sections of the G-code
// printed with the same extruder.
std::string toolchange_prefix() const;
std::string toolchange(unsigned int extruder_id);
std::string toolchange(unsigned int filament_id);
std::string set_speed(double F, const std::string &comment = std::string(), const std::string &cooling_marker = std::string());
// SoftFever NOTE: the returned speed is mm/minute
double get_current_speed() const { return m_current_speed;}
@ -123,9 +124,10 @@ public:
static bool supports_separate_travel_acceleration(GCodeFlavor flavor);
private:
// Extruders are sorted by their ID, so that binary search is possible.
std::vector<Extruder> m_extruders;
std::vector<Extruder> m_filament_extruders;
bool m_single_extruder_multi_material;
Extruder* m_extruder;
std::vector<Extruder*> m_curr_filament_extruder;
int m_curr_extruder_id;
unsigned int m_last_acceleration;
unsigned int m_last_travel_acceleration;
unsigned int m_max_travel_acceleration;

View file

@ -2769,14 +2769,7 @@ void Print::_make_wipe_tower()
multi_extruder_flush.emplace_back(wipe_volumes);
}
// todo multi_extruders: get filament_maps
std::vector<int> filament_maps;
for (int i = 0; i <= number_of_extruders / 2; ++i) {
filament_maps.push_back(1);
}
for (int i = number_of_extruders / 2 + 1; i < number_of_extruders; ++i) {
filament_maps.push_back(2);
}
std::vector<int>filament_maps = get_filament_maps();
std::vector<unsigned int> nozzle_cur_filament_ids(nozzle_nums, -1);
unsigned int current_filament_id = m_wipe_tower_data.tool_ordering.first_extruder();
@ -2806,6 +2799,7 @@ void Print::_make_wipe_tower()
wipe_tower.plan_toolchange((float) layer_tools.print_z, (float) layer_tools.wipe_tower_layer_height, current_filament_id, filament_id,
m_config.prime_volume, volume_to_purge);
current_filament_id = filament_id;
nozzle_cur_filament_ids[nozzle_id] = filament_id;
}
layer_tools.wiping_extrusions().ensure_perimeters_infills_order(*this);

View file

@ -936,6 +936,7 @@ public:
void update_filament_maps_to_config(std::vector<int> f_maps);
std::vector<int> get_filament_maps() const;
// get the group label of filament
size_t get_extruder_id(unsigned int filament_id) const;
bool enable_timelapse_print() const;

View file

@ -56,7 +56,7 @@ namespace Slic3r {
size_t get_extruder_index(const GCodeConfig& config, unsigned int filament_id)
{
if (filament_id < config.filament_map.size()) {
return config.filament_map.get_at(filament_id);
return config.filament_map.get_at(filament_id)-1;
}
return 0;
}