update libslic3r

This commit is contained in:
QIDI TECH
2025-05-05 19:52:57 +08:00
parent eae8e18c3a
commit 126534997a
180 changed files with 24833 additions and 5679 deletions

View File

@@ -32,11 +32,11 @@ 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_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
first extruder has index 0 */
@@ -165,7 +165,45 @@ std::string GCodeWriter::set_chamber_temperature(int temperature, bool wait)
return gcode.str();
}
std::string GCodeWriter::set_acceleration(unsigned int acceleration)
void GCodeWriter::set_acceleration(unsigned int acceleration)
{
m_acceleration = acceleration;
}
void GCodeWriter::set_travel_acceleration(const std::vector<unsigned int>& accelerations)
{
m_travel_accelerations = accelerations;
}
void GCodeWriter::set_first_layer_travel_acceleration(const std::vector<unsigned int> &travel_accelerations)
{
m_first_layer_travel_accelerations = travel_accelerations;
}
void GCodeWriter::set_first_layer(bool is_first_layer)
{
m_is_first_layer = is_first_layer;
}
std::string GCodeWriter::set_extrude_acceleration()
{
return set_acceleration_impl(m_acceleration);
}
std::string GCodeWriter::set_travel_acceleration()
{
std::vector<unsigned int> travel_accelerations = m_is_first_layer ? m_first_layer_travel_accelerations : m_travel_accelerations;
if (travel_accelerations.empty())
return std::string();
Extruder *cur_filament = filament();
if (!cur_filament)
return std::string();
return set_acceleration_impl(travel_accelerations[cur_filament->extruder_id()]);
}
std::string GCodeWriter::set_acceleration_impl(unsigned int acceleration)
{
// Clamp the acceleration to the allowed maximum.
if (m_max_acceleration > 0 && acceleration > m_max_acceleration)
@@ -253,11 +291,11 @@ std::string GCodeWriter::reset_e(bool force)
|| FLAVOR_IS(gcfMakerWare)
|| FLAVOR_IS(gcfSailfish))
return "";
if (m_extruder != nullptr) {
if (m_extruder->E() == 0. && ! force)
if (m_curr_extruder_id!=-1 && m_curr_filament_extruder[m_curr_extruder_id] != nullptr) {
if (m_curr_filament_extruder[m_curr_extruder_id]->E() == 0. && !force)
return "";
m_extruder->reset_E();
m_curr_filament_extruder[m_curr_extruder_id]->reset_E();
}
if (!this->config.use_relative_e_distances) {
@@ -294,18 +332,23 @@ 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
std::ostringstream gcode;
if (this->multiple_extruders) {
gcode << this->toolchange_prefix() << extruder_id;
// QDS
if (this->m_is_qdt_printer)
gcode << "M1020 S" << filament_id;
else
gcode << this->toolchange_prefix() << filament_id;
//QDS
if (GCodeWriter::full_gcode_comment)
gcode << " ; change extruder";
@@ -339,10 +382,80 @@ std::string GCodeWriter::travel_to_xy(const Vec2d &point, const std::string &com
GCodeG1Formatter w;
w.emit_xy(point_on_plate);
w.emit_f(this->config.travel_speed.value * 60.0);
w.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
return set_travel_acceleration() + w.string();
}
/* If this method is called more than once before calling unlift(),
it will not perform subsequent lifts, even if Z was raised manually
(i.e. with travel_to_z()) and thus _lifted was reduced. */
std::string GCodeWriter::lazy_lift(LiftType lift_type, bool spiral_vase, bool tool_change)
{
// check whether the above/below conditions are met
double target_lift = 0;
{
//QDS
int extruder_id = filament()->extruder_id();
int filament_id = filament()->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.z() >= above && m_pos.z() <= below){
target_lift = this->config.z_hop.get_at(filament_id);
if (tool_change && this->config.prime_tower_lift_height.value > 0) target_lift = this->config.prime_tower_lift_height.value;
}
}
// QDS
if (m_lifted == 0 && m_to_lift == 0 && target_lift > 0) {
if (spiral_vase) {
m_lifted = target_lift;
return this->_travel_to_z(m_pos(2) + target_lift, "lift Z",tool_change);
}
else {
m_to_lift = target_lift;
m_to_lift_type = lift_type;
}
}
return "";
}
// QDS: immediately execute an undelayed lift move with a spiral lift pattern
// designed specifically for subsequent gcode injection (e.g. timelapse)
std::string GCodeWriter::eager_lift(const LiftType type, bool tool_change)
{
std::string lift_move;
double target_lift = 0;
{
//QDS
int extruder_id = filament()->extruder_id();
int filament_id = filament()->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.z() >= above && m_pos.z() <= below){
target_lift = this->config.z_hop.get_at(filament_id);
if (tool_change && this->config.prime_tower_lift_height.value > 0) target_lift = this->config.prime_tower_lift_height.value;
}
}
// QDS: spiral lift only safe with known position
// TODO: check the arc will move within bed area
if (type == LiftType::SpiralLift && this->is_current_position_clear()) {
double radius = target_lift / (2 * PI * atan(GCodeWriter::slope_threshold));
// static spiral alignment when no move in x,y plane.
// spiral centra is a radius distance to the right (y=0)
Vec2d ij_offset = { radius, 0 };
if (target_lift > 0) {
lift_move = this->_spiral_travel_to_z(m_pos(2) + target_lift, ij_offset, "spiral lift Z",tool_change);
}
}
//QDS: if position is unknown use normal lift
else if (target_lift > 0) {
lift_move = _travel_to_z(m_pos(2) + target_lift, "normal lift Z",tool_change);
}
m_lifted = target_lift;
m_to_lift = 0;
return lift_move;
}
std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &comment)
@@ -388,9 +501,9 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
ij_offset = { -ij_offset(1), ij_offset(0) };
slop_move = this->_spiral_travel_to_z(target(2), ij_offset, "spiral lift Z");
}
//QDS: LazyLift
else if (m_to_lift_type == LiftType::LazyLift &&
this->is_current_position_clear() &&
//QDS: SlopeLift
else if (m_to_lift_type == LiftType::SlopeLift &&
this->is_current_position_clear() &&
atan2(delta(2), delta_no_z.norm()) < GCodeWriter::slope_threshold) {
//QDS: check whether we can make a travel like
// _____
@@ -399,7 +512,7 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
Vec3d slope_top_point = Vec3d(temp(0), temp(1), delta(2)) + source;
GCodeG1Formatter w0;
w0.emit_xyz(slope_top_point);
w0.emit_f(this->config.travel_speed.value * 60.0);
w0.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
//QDS
w0.emit_comment(GCodeWriter::full_gcode_comment, "slope lift Z");
slop_move = w0.string();
@@ -414,20 +527,20 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
GCodeG1Formatter w0;
if (this->is_current_position_clear()) {
w0.emit_xyz(target);
w0.emit_f(this->config.travel_speed.value * 60.0);
w0.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
w0.emit_comment(GCodeWriter::full_gcode_comment, comment);
xy_z_move = w0.string();
}
else {
w0.emit_xy(Vec2d(target.x(), target.y()));
w0.emit_f(this->config.travel_speed.value * 60.0);
w0.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
w0.emit_comment(GCodeWriter::full_gcode_comment, comment);
xy_z_move = w0.string() + _travel_to_z(target.z(), comment);
}
}
m_pos = dest_point;
this->set_current_position_clear(true);
return slop_move + xy_z_move;
return set_travel_acceleration() + slop_move + xy_z_move;
}
else if (!this->will_move_z(point(2))) {
double nominal_z = m_pos(2) - m_lifted;
@@ -454,20 +567,20 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
{
//force to move xy first then z after filament change
w.emit_xy(Vec2d(point_on_plate.x(), point_on_plate.y()));
w.emit_f(this->config.travel_speed.value * 60.0);
w.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
out_string = w.string() + _travel_to_z(point_on_plate.z(), comment);
} else {
GCodeG1Formatter w;
w.emit_xyz(point_on_plate);
w.emit_f(this->config.travel_speed.value * 60.0);
w.emit_f(this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id())) * 60.0);
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
out_string = w.string();
}
m_pos = dest_point;
this->set_current_position_clear(true);
return out_string;
return set_travel_acceleration() + out_string;
}
std::string GCodeWriter::travel_to_z(double z, const std::string &comment)
@@ -486,33 +599,37 @@ std::string GCodeWriter::travel_to_z(double z, const std::string &comment)
/* In all the other cases, we perform an actual Z move and cancel
the lift. */
m_lifted = 0;
return this->_travel_to_z(z, comment);
return set_travel_acceleration() + this->_travel_to_z(z, comment);
}
std::string GCodeWriter::_travel_to_z(double z, const std::string &comment)
std::string GCodeWriter::_travel_to_z(double z, const std::string &comment, bool tool_change)
{
m_pos(2) = z;
double speed = this->config.travel_speed_z.value;
double speed = this->config.travel_speed_z.get_at(get_extruder_index(this->config, filament()->id()));
if (speed == 0.)
speed = this->config.travel_speed.value;
speed = this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id()));
if (tool_change && this->config.prime_tower_lift_speed.value>0) {
speed = this->config.prime_tower_lift_speed.value; // lift speed
}
GCodeG1Formatter w;
w.emit_z(z);
w.emit_f(speed * 60.0);
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
return set_travel_acceleration() + w.string();
}
std::string GCodeWriter::_spiral_travel_to_z(double z, const Vec2d &ij_offset, const std::string &comment)
std::string GCodeWriter::_spiral_travel_to_z(double z, const Vec2d &ij_offset, const std::string &comment, bool tool_change)
{
m_pos(2) = z;
double speed = this->config.travel_speed_z.value;
double speed = this->config.travel_speed_z.get_at(get_extruder_index(this->config, filament()->id()));
if (speed == 0.)
speed = this->config.travel_speed.value;
speed = this->config.travel_speed.get_at(get_extruder_index(this->config, filament()->id()));
if (tool_change && this->config.prime_tower_lift_speed.value>0) {
speed = this->config.prime_tower_lift_speed.value; // lift speed
}
std::string output = "G17\n";
GCodeG2G3Formatter w(true);
w.emit_z(z);
@@ -520,7 +637,7 @@ std::string GCodeWriter::_spiral_travel_to_z(double z, const Vec2d &ij_offset, c
w.emit_string(" P1 ");
w.emit_f(speed * 60.0);
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return output + w.string();
return set_travel_acceleration() + output + w.string();
}
bool GCodeWriter::will_move_z(double z) const
@@ -544,8 +661,9 @@ std::string GCodeWriter::extrude_to_xy(const Vec2d &point, double dE, const std:
{
m_pos(0) = point(0);
m_pos(1) = point(1);
if (!force_no_extrusion)
m_extruder->extrude(dE);
filament()->extrude(dE);
//QDS: take plate offset into consider
Vec2d point_on_plate = { point(0) - m_x_offset, point(1) - m_y_offset };
@@ -553,10 +671,10 @@ 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());
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
return set_extrude_acceleration() + w.string();
}
//QDS: generate G2 or G3 extrude which moves by arc
@@ -567,7 +685,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 };
@@ -575,10 +693,10 @@ 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());
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
return set_extrude_acceleration() + w.string();
}
std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std::string &comment, bool force_no_extrusion)
@@ -586,38 +704,38 @@ 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);
//QDS: take plate offset into consider
Vec3d point_on_plate = { point(0) - m_x_offset, point(1) - m_y_offset, point(2) };
GCodeG1Formatter w;
w.emit_xyz(point_on_plate);
if (!force_no_extrusion)
w.emit_e(m_extruder->E());
w.emit_e(filament()->E());
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
return w.string();
return set_extrude_acceleration() + w.string();
}
std::string GCodeWriter::retract(bool before_wipe)
{
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(
factor * m_extruder->retraction_length(),
factor * m_extruder->retract_restart_extra(),
factor * filament()->retraction_length(),
factor * filament()->retract_restart_extra(),
"retract"
);
}
std::string GCodeWriter::retract_for_toolchange(bool before_wipe)
{
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(
factor * m_extruder->retract_length_toolchange(),
factor * m_extruder->retract_restart_extra_toolchange(),
factor * filament()->retract_length_toolchange(),
factor * filament()->retract_restart_extra_toolchange(),
"retract for toolchange"
);
}
@@ -627,7 +745,7 @@ std::string GCodeWriter::_retract(double length, double restart_extra, const std
std::string gcode;
if (config.use_firmware_retraction)
length = 1;
if (double dE = m_extruder->retract(length, restart_extra); dE != 0) {
if (double dE = filament()->retract(length, restart_extra); dE != 0) {
//add firmware retraction
if (config.use_firmware_retraction) {
gcode = FLAVOR_IS(gcfMachinekit) ? "G22 ;retract" : "G10 ;retract \n";
@@ -635,8 +753,8 @@ std::string GCodeWriter::_retract(double length, double restart_extra, const std
else {
//QDS
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.);
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, comment);
gcode = w.string();
@@ -655,8 +773,8 @@ std::string GCodeWriter::unretract()
if (FLAVOR_IS(gcfMakerWare))
gcode = "M101 ; extruder on\n";
if (double dE = m_extruder->unretract(); dE != 0) {
if (double dE = filament()->unretract(); dE != 0) {
if (config.use_firmware_retraction) {
gcode += FLAVOR_IS(gcfMachinekit) ? "G23 ;unretract \n" : "G11 ;unretract \n";
gcode += reset_e();
@@ -665,8 +783,8 @@ std::string GCodeWriter::unretract()
//QDS
// 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.);
//QDS
w.emit_comment(GCodeWriter::full_gcode_comment, " ; unretract");
gcode += w.string();
@@ -676,34 +794,6 @@ std::string GCodeWriter::unretract()
return gcode;
}
/* If this method is called more than once before calling unlift(),
it will not perform subsequent lifts, even if Z was raised manually
(i.e. with travel_to_z()) and thus _lifted was reduced. */
std::string GCodeWriter::lift(LiftType lift_type, bool spiral_vase)
{
// check whether the above/below conditions are met
double target_lift = 0;
{
//QDS
double above = this->config.retract_lift_above.get_at(m_extruder->id());
double below = this->config.retract_lift_below.get_at(m_extruder->id());
if (m_pos.z() >= above && m_pos.z() <= below)
target_lift = this->config.z_hop.get_at(m_extruder->id());
}
// QDS
if (m_lifted == 0 && m_to_lift == 0 && target_lift > 0) {
if (spiral_vase) {
m_lifted = target_lift;
return this->_travel_to_z(m_pos(2) + target_lift, "lift Z");
}
else {
m_to_lift = target_lift;
m_to_lift_type = lift_type;
}
}
return "";
}
std::string GCodeWriter::unlift()
{
std::string gcode;
@@ -742,7 +832,7 @@ std::string GCodeWriter::set_fan(const GCodeFlavor gcode_flavor, unsigned int sp
default:
gcode << "M106 S" << 255.0 * speed / 100.0; break;
}
if (GCodeWriter::full_gcode_comment)
if (GCodeWriter::full_gcode_comment)
gcode << " ; enable fan";
gcode << "\n";
}
@@ -803,6 +893,30 @@ 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) : "";
}
void GCodeWriter::init_extruder(unsigned int filament_id)
{
if (m_curr_extruder_id == -1 && filament_id != -1) {
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;
}
}
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};