Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::GCodeWriter Class Reference

#include <src/libslic3r/GCodeWriter.hpp>

+ Collaboration diagram for Slic3r::GCodeWriter:

Public Member Functions

 GCodeWriter ()
 
Extruderextruder ()
 
const Extruderextruder () const
 
std::string extrusion_axis () const
 
void apply_print_config (const PrintConfig &print_config)
 
void set_extruders (std::vector< unsigned int > extruder_ids)
 
const std::vector< Extruder > & extruders () const
 
std::vector< unsigned int > extruder_ids () const
 
std::string preamble ()
 
std::string postamble () const
 
std::string set_temperature (unsigned int temperature, bool wait=false, int tool=-1) const
 
std::string set_bed_temperature (unsigned int temperature, bool wait=false)
 
std::string set_print_acceleration (unsigned int acceleration)
 
std::string set_travel_acceleration (unsigned int acceleration)
 
std::string reset_e (bool force=false)
 
std::string update_progress (unsigned int num, unsigned int tot, bool allow_100=false) const
 
bool need_toolchange (unsigned int extruder_id) const
 
std::string set_extruder (unsigned int extruder_id)
 
std::string toolchange_prefix () const
 
std::string toolchange (unsigned int extruder_id)
 
std::string set_speed (double F, const std::string &comment=std::string(), const std::string &cooling_marker=std::string()) const
 
std::string travel_to_xy (const Vec2d &point, const std::string &comment=std::string())
 
std::string travel_to_xyz (const Vec3d &point, const std::string &comment=std::string())
 
std::string travel_to_z (double z, const std::string &comment=std::string())
 
bool will_move_z (double z) const
 
std::string extrude_to_xy (const Vec2d &point, double dE, const std::string &comment=std::string())
 
std::string retract (bool before_wipe=false)
 
std::string retract_for_toolchange (bool before_wipe=false)
 
std::string unretract ()
 
std::string lift ()
 
std::string unlift ()
 
Vec3d get_position () const
 
double get_zhop () const
 
void update_position (const Vec3d &new_pos)
 
std::string set_fan (unsigned int speed) const
 

Static Public Member Functions

static bool supports_separate_travel_acceleration (GCodeFlavor flavor)
 
static std::string set_fan (const GCodeFlavor gcode_flavor, bool gcode_comments, unsigned int speed)
 

Public Attributes

GCodeConfig config
 
bool multiple_extruders
 

Private Types

enum class  Acceleration { Travel , Print }
 

Private Member Functions

std::string _travel_to_z (double z, const std::string &comment)
 
std::string _retract (double length, double restart_extra, const std::string &comment)
 
std::string set_acceleration_internal (Acceleration type, unsigned int acceleration)
 

Private Attributes

std::vector< Extruderm_extruders
 
std::string m_extrusion_axis
 
bool m_single_extruder_multi_material
 
Extruderm_extruder
 
unsigned int m_last_acceleration = (unsigned int)(-1)
 
unsigned int m_last_travel_acceleration = (unsigned int)(-1)
 
unsigned int m_max_acceleration
 
unsigned int m_max_travel_acceleration
 
unsigned int m_last_bed_temperature
 
bool m_last_bed_temperature_reached
 
double m_lifted
 
Vec3d m_pos = Vec3d::Zero()
 

Detailed Description

Member Enumeration Documentation

◆ Acceleration

enum class Slic3r::GCodeWriter::Acceleration
strongprivate
Enumerator
Travel 
Print 

Constructor & Destructor Documentation

◆ GCodeWriter()

Slic3r::GCodeWriter::GCodeWriter ( )
inline
19 :
20 multiple_extruders(false), m_extrusion_axis("E"), m_extruder(nullptr),
24 m_lifted(0)
25 {}
unsigned int m_last_bed_temperature
Definition GCodeWriter.hpp:106
Extruder * m_extruder
Definition GCodeWriter.hpp:98
unsigned int m_max_acceleration
Definition GCodeWriter.hpp:103
bool m_last_bed_temperature_reached
Definition GCodeWriter.hpp:107
bool m_single_extruder_multi_material
Definition GCodeWriter.hpp:97
double m_lifted
Definition GCodeWriter.hpp:108
std::string m_extrusion_axis
Definition GCodeWriter.hpp:96
unsigned int m_last_acceleration
Definition GCodeWriter.hpp:99
bool multiple_extruders
Definition GCodeWriter.hpp:17

Member Function Documentation

◆ _retract()

std::string Slic3r::GCodeWriter::_retract ( double  length,
double  restart_extra,
const std::string &  comment 
)
private
401{
402 /* If firmware retraction is enabled, we use a fake value of 1
403 since we ignore the actual configured retract_length which
404 might be 0, in which case the retraction logic gets skipped. */
405 if (this->config.use_firmware_retraction)
406 length = 1;
407
408 // If we use volumetric E values we turn lengths into volumes */
409 if (this->config.use_volumetric_e) {
410 double d = m_extruder->filament_diameter();
411 double area = d * d * PI/4;
412 length = length * area;
413 restart_extra = restart_extra * area;
414 }
415
416 std::string gcode;
417 if (auto [dE, emitE] = m_extruder->retract(length, restart_extra); dE != 0) {
418 if (this->config.use_firmware_retraction) {
419 gcode = FLAVOR_IS(gcfMachinekit) ? "G22 ; retract\n" : "G10 ; retract\n";
420 } else if (! m_extrusion_axis.empty()) {
421 GCodeG1Formatter w;
422 w.emit_e(m_extrusion_axis, emitE);
423 w.emit_f(m_extruder->retract_speed() * 60.);
424 w.emit_comment(this->config.gcode_comments, comment);
425 gcode = w.string();
426 }
427 }
428
430 gcode += "M103 ; extruder off\n";
431
432 return gcode;
433}
#define FLAVOR_IS(val)
Definition GCodeWriter.cpp:13
std::pair< double, double > retract(double retract_length, double restart_extra)
Definition Extruder.cpp:38
int retract_speed() const
Definition Extruder.cpp:138
double filament_diameter() const
Definition Extruder.cpp:102
GCodeConfig config
Definition GCodeWriter.hpp:16
#define comment
Definition lexer.c:1004
static constexpr double PI
Definition libslic3r.h:58
@ gcfMachinekit
Definition PrintConfig.hpp:35
@ gcfMakerWare
Definition PrintConfig.hpp:35
double length(const Points &pts)
Definition MultiPoint.hpp:116
double area(const ExPolygon &poly)
Definition ExPolygon.hpp:467

References Slic3r::area(), config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_e(), Slic3r::GCodeFormatter::emit_f(), Slic3r::Extruder::filament_diameter(), FLAVOR_IS, Slic3r::gcfMachinekit, Slic3r::gcfMakerWare, Slic3r::length(), m_extruder, m_extrusion_axis, PI, Slic3r::Extruder::retract(), Slic3r::Extruder::retract_speed(), and Slic3r::GCodeFormatter::string().

Referenced by retract(), and retract_for_toolchange().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _travel_to_z()

std::string Slic3r::GCodeWriter::_travel_to_z ( double  z,
const std::string &  comment 
)
private
325{
326 m_pos.z() = z;
327
328 double speed = this->config.travel_speed_z.value;
329 if (speed == 0.)
330 speed = this->config.travel_speed.value;
331
332 GCodeG1Formatter w;
333 w.emit_z(z);
334 w.emit_f(speed * 60.0);
335 w.emit_comment(this->config.gcode_comments, comment);
336 return w.string();
337}
Vec3d m_pos
Definition GCodeWriter.hpp:109

References config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_f(), Slic3r::GCodeFormatter::emit_z(), m_pos, and Slic3r::GCodeFormatter::string().

Referenced by lift(), travel_to_z(), and unlift().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ apply_print_config()

void Slic3r::GCodeWriter::apply_print_config ( const PrintConfig &  print_config)
25{
26 this->config.apply(print_config, true);
28 m_single_extruder_multi_material = print_config.single_extruder_multi_material.value;
29 bool use_mach_limits = print_config.gcode_flavor.value == gcfMarlinLegacy
30 || print_config.gcode_flavor.value == gcfMarlinFirmware
31 || print_config.gcode_flavor.value == gcfRepRapFirmware;
32 m_max_acceleration = static_cast<unsigned int>(std::round((use_mach_limits && print_config.machine_limits_usage.value == MachineLimitsUsage::EmitToGCode) ?
33 print_config.machine_max_acceleration_extruding.values.front() : 0));
34 m_max_travel_acceleration = static_cast<unsigned int>(std::round((use_mach_limits && print_config.machine_limits_usage.value == MachineLimitsUsage::EmitToGCode && supports_separate_travel_acceleration(print_config.gcode_flavor.value)) ?
35 print_config.machine_max_acceleration_travel.values.front() : 0));
36}
unsigned int m_max_travel_acceleration
Definition GCodeWriter.hpp:104
static bool supports_separate_travel_acceleration(GCodeFlavor flavor)
Definition GCodeWriter.cpp:19
@ gcfMarlinFirmware
Definition PrintConfig.hpp:35
@ gcfRepRapFirmware
Definition PrintConfig.hpp:35
@ gcfMarlinLegacy
Definition PrintConfig.hpp:35
static template_custom_gcode std::string get_extrusion_axis(const GCodeConfig &cfg)
Definition PrintConfig.hpp:739

References config, Slic3r::EmitToGCode, Slic3r::gcfMarlinFirmware, Slic3r::gcfMarlinLegacy, Slic3r::gcfRepRapFirmware, Slic3r::get_extrusion_axis(), m_extrusion_axis, m_max_acceleration, m_max_travel_acceleration, m_single_extruder_multi_material, and supports_separate_travel_acceleration().

Referenced by Slic3r::GCode::apply_print_config().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ extrude_to_xy()

std::string Slic3r::GCodeWriter::extrude_to_xy ( const Vec2d point,
double  dE,
const std::string &  comment = std::string() 
)
352{
353 m_pos.x() = point.x();
354 m_pos.y() = point.y();
355
356 GCodeG1Formatter w;
357 w.emit_xy(point);
358 w.emit_e(m_extrusion_axis, m_extruder->extrude(dE).second);
359 w.emit_comment(this->config.gcode_comments, comment);
360 return w.string();
361}
std::pair< double, double > extrude(double dE)
Definition Extruder.cpp:17

References config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_e(), Slic3r::GCodeFormatter::emit_xy(), Slic3r::Extruder::extrude(), m_extruder, m_extrusion_axis, m_pos, and Slic3r::GCodeFormatter::string().

Referenced by Slic3r::GCode::_extrude(), and Slic3r::Wipe::wipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ extruder() [1/2]

Extruder * Slic3r::GCodeWriter::extruder ( )
inline

◆ extruder() [2/2]

const Extruder * Slic3r::GCodeWriter::extruder ( ) const
inline
27{ return m_extruder; }

References m_extruder.

◆ extruder_ids()

std::vector< unsigned int > Slic3r::GCodeWriter::extruder_ids ( ) const
inline
35 {
36 std::vector<unsigned int> out;
37 out.reserve(m_extruders.size());
38 for (const Extruder &e : m_extruders)
39 out.push_back(e.id());
40 return out;
41 }
std::vector< Extruder > m_extruders
Definition GCodeWriter.hpp:95

References m_extruders.

Referenced by set_extruders().

+ Here is the caller graph for this function:

◆ extruders()

const std::vector< Extruder > & Slic3r::GCodeWriter::extruders ( ) const
inline

◆ extrusion_axis()

std::string Slic3r::GCodeWriter::extrusion_axis ( ) const
inline
30{ return m_extrusion_axis; }

References m_extrusion_axis.

Referenced by Slic3r::GCode::_extrude().

+ Here is the caller graph for this function:

◆ get_position()

Vec3d Slic3r::GCodeWriter::get_position ( ) const
inline
75{ return m_pos; }

References m_pos.

Referenced by Slic3r::CoolingBuffer::CoolingBuffer(), Slic3r::GCode::_do_export(), Slic3r::WipeTowerIntegration::append_tcr(), Slic3r::WipeTowerIntegration::finalize(), and Slic3r::GCode::PlaceholderParserIntegration::update_from_gcodewriter().

+ Here is the caller graph for this function:

◆ get_zhop()

double Slic3r::GCodeWriter::get_zhop ( ) const
inline
77{ return m_lifted; }

References m_lifted.

Referenced by Slic3r::GCode::PlaceholderParserIntegration::init(), and Slic3r::GCode::PlaceholderParserIntegration::update_from_gcodewriter().

+ Here is the caller graph for this function:

◆ lift()

std::string Slic3r::GCodeWriter::lift ( )
463{
464 // check whether the above/below conditions are met
465 double target_lift = 0;
466 {
467 double above = this->config.retract_lift_above.get_at(m_extruder->id());
468 double below = this->config.retract_lift_below.get_at(m_extruder->id());
469 if (m_pos.z() >= above && (below == 0 || m_pos.z() <= below))
470 target_lift = this->config.retract_lift.get_at(m_extruder->id());
471 }
472 if (m_lifted == 0 && target_lift > 0) {
473 m_lifted = target_lift;
474 return this->_travel_to_z(m_pos.z() + target_lift, "lift Z");
475 }
476 return {};
477}
unsigned int id() const
Definition Extruder.hpp:17
std::string _travel_to_z(double z, const std::string &comment)
Definition GCodeWriter.cpp:324

References _travel_to_z(), config, Slic3r::Extruder::id(), m_extruder, m_lifted, and m_pos.

Referenced by Slic3r::GCode::retract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ need_toolchange()

bool Slic3r::GCodeWriter::need_toolchange ( unsigned int  extruder_id) const
inline
52 { return m_extruder == nullptr || m_extruder->id() != extruder_id; }

References Slic3r::Extruder::id(), and m_extruder.

Referenced by Slic3r::WipeTowerIntegration::append_tcr(), set_extruder(), Slic3r::GCode::set_extruder(), and Slic3r::WipeTowerIntegration::tool_change().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ postamble()

std::string Slic3r::GCodeWriter::postamble ( ) const
81{
82 std::ostringstream gcode;
84 gcode << "M2 ; end of program\n";
85 return gcode.str();
86}

References FLAVOR_IS, and Slic3r::gcfMachinekit.

Referenced by Slic3r::GCode::_do_export().

+ Here is the caller graph for this function:

◆ preamble()

std::string Slic3r::GCodeWriter::preamble ( )
53{
54 std::ostringstream gcode;
55
57 gcode << "G21 ; set units to millimeters\n";
58 gcode << "G90 ; use absolute coordinates\n";
59 }
68 {
69 if (this->config.use_relative_e_distances) {
70 gcode << "M83 ; use relative distances for extrusion\n";
71 } else {
72 gcode << "M82 ; use absolute distances for extrusion\n";
73 }
74 gcode << this->reset_e(true);
75 }
76
77 return gcode.str();
78}
#define FLAVOR_IS_NOT(val)
Definition GCodeWriter.cpp:14
std::string reset_e(bool force=false)
Definition GCodeWriter.cpp:197
@ gcfTeacup
Definition PrintConfig.hpp:35
@ gcfKlipper
Definition PrintConfig.hpp:35
@ gcfRepetier
Definition PrintConfig.hpp:35
@ gcfSmoothie
Definition PrintConfig.hpp:36
@ gcfRepRapSprinter
Definition PrintConfig.hpp:35

References config, FLAVOR_IS, FLAVOR_IS_NOT, Slic3r::gcfKlipper, Slic3r::gcfMakerWare, Slic3r::gcfMarlinFirmware, Slic3r::gcfMarlinLegacy, Slic3r::gcfRepetier, Slic3r::gcfRepRapFirmware, Slic3r::gcfRepRapSprinter, Slic3r::gcfSmoothie, Slic3r::gcfTeacup, and reset_e().

Referenced by Slic3r::GCode::preamble().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reset_e()

std::string Slic3r::GCodeWriter::reset_e ( bool  force = false)
198{
199 return
200 FLAVOR_IS(gcfMach3) || FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish) || this->config.use_relative_e_distances ||
201 (m_extruder != nullptr && ! m_extruder->reset_E() && ! force) ||
202 m_extrusion_axis.empty() ?
203 std::string{} :
204 std::string("G92 ") + m_extrusion_axis + (this->config.gcode_comments ? "0 ; reset extrusion distance\n" : "0\n");
205}
bool reset_E()
Definition Extruder.hpp:32
@ gcfMach3
Definition PrintConfig.hpp:35
@ gcfSailfish
Definition PrintConfig.hpp:35

References config, FLAVOR_IS, Slic3r::gcfMach3, Slic3r::gcfMakerWare, Slic3r::gcfSailfish, m_extruder, m_extrusion_axis, and Slic3r::Extruder::reset_E().

Referenced by preamble(), Slic3r::GCode::retract(), toolchange(), and unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ retract()

std::string Slic3r::GCodeWriter::retract ( bool  before_wipe = false)
379{
380 double factor = before_wipe ? m_extruder->retract_before_wipe() : 1.;
381 assert(factor >= 0. && factor <= 1. + EPSILON);
382 return this->_retract(
383 factor * m_extruder->retract_length(),
385 "retract"
386 );
387}
double retract_restart_extra() const
Definition Extruder.cpp:149
double retract_before_wipe() const
Definition Extruder.cpp:123
double retract_length() const
Definition Extruder.cpp:128
std::string _retract(double length, double restart_extra, const std::string &comment)
Definition GCodeWriter.cpp:400
static constexpr double EPSILON
Definition libslic3r.h:51

References _retract(), EPSILON, m_extruder, Slic3r::Extruder::retract_before_wipe(), Slic3r::Extruder::retract_length(), and Slic3r::Extruder::retract_restart_extra().

Referenced by Slic3r::WipeTowerIntegration::append_tcr(), and Slic3r::GCode::retract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ retract_for_toolchange()

std::string Slic3r::GCodeWriter::retract_for_toolchange ( bool  before_wipe = false)
390{
391 double factor = before_wipe ? m_extruder->retract_before_wipe() : 1.;
392 assert(factor >= 0. && factor <= 1. + EPSILON);
393 return this->_retract(
396 "retract for toolchange"
397 );
398}
double retract_restart_extra_toolchange() const
Definition Extruder.cpp:159
double retract_length_toolchange() const
Definition Extruder.cpp:154

References _retract(), EPSILON, m_extruder, Slic3r::Extruder::retract_before_wipe(), Slic3r::Extruder::retract_length_toolchange(), and Slic3r::Extruder::retract_restart_extra_toolchange().

Referenced by Slic3r::GCode::retract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_acceleration_internal()

std::string Slic3r::GCodeWriter::set_acceleration_internal ( Acceleration  type,
unsigned int  acceleration 
)
private
167{
168 // Clamp the acceleration to the allowed maximum.
169 if (type == Acceleration::Print && m_max_acceleration > 0 && acceleration > m_max_acceleration)
170 acceleration = m_max_acceleration;
172 acceleration = m_max_travel_acceleration;
173
174 // Are we setting travel acceleration for a flavour that supports separate travel and print acc?
175 bool separate_travel = (type == Acceleration::Travel && supports_separate_travel_acceleration(this->config.gcode_flavor));
176
177 auto& last_value = separate_travel ? m_last_travel_acceleration : m_last_acceleration ;
178 if (acceleration == 0 || acceleration == last_value)
179 return std::string();
180
181 last_value = acceleration;
182
183 std::ostringstream gcode;
185 gcode << (separate_travel ? "M202 X" : "M201 X") << acceleration << " Y" << acceleration;
187 gcode << (separate_travel ? "M204 T" : "M204 P") << acceleration;
188 else
189 gcode << "M204 S" << acceleration;
190
191 if (this->config.gcode_comments) gcode << " ; adjust acceleration";
192 gcode << "\n";
193
194 return gcode.str();
195}
unsigned int m_last_travel_acceleration
Definition GCodeWriter.hpp:100

References config, FLAVOR_IS, Slic3r::gcfMarlinFirmware, Slic3r::gcfRepetier, Slic3r::gcfRepRapFirmware, m_last_acceleration, m_last_travel_acceleration, m_max_acceleration, m_max_travel_acceleration, Print, supports_separate_travel_acceleration(), and Travel.

Referenced by set_print_acceleration(), and set_travel_acceleration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_bed_temperature()

std::string Slic3r::GCodeWriter::set_bed_temperature ( unsigned int  temperature,
bool  wait = false 
)
131{
132 if (temperature == m_last_bed_temperature && (! wait || m_last_bed_temperature_reached))
133 return std::string();
134
135 m_last_bed_temperature = temperature;
137
138 std::string code, comment;
139 if (wait && FLAVOR_IS_NOT(gcfTeacup)) {
141 code = "M109";
142 } else {
143 code = "M190";
144 }
145 comment = "set bed temperature and wait for it to be reached";
146 } else {
147 code = "M140";
148 comment = "set bed temperature";
149 }
150
151 std::ostringstream gcode;
152 gcode << code << " ";
154 gcode << "P";
155 } else {
156 gcode << "S";
157 }
158 gcode << temperature << " ; " << comment << "\n";
159
160 if (FLAVOR_IS(gcfTeacup) && wait)
161 gcode << "M116 ; wait for bed temperature to be reached\n";
162
163 return gcode.str();
164}

References comment, FLAVOR_IS, FLAVOR_IS_NOT, Slic3r::gcfMach3, Slic3r::gcfMachinekit, Slic3r::gcfMakerWare, Slic3r::gcfSailfish, Slic3r::gcfTeacup, m_last_bed_temperature, and m_last_bed_temperature_reached.

Referenced by Slic3r::GCode::_print_first_layer_bed_temperature(), and Slic3r::GCode::process_layer().

+ Here is the caller graph for this function:

◆ set_extruder()

std::string Slic3r::GCodeWriter::set_extruder ( unsigned int  extruder_id)
inline
54 { return this->need_toolchange(extruder_id) ? this->toolchange(extruder_id) : ""; }
std::string toolchange(unsigned int extruder_id)
Definition GCodeWriter.cpp:228
bool need_toolchange(unsigned int extruder_id) const
Definition GCodeWriter.hpp:51

References need_toolchange(), and toolchange().

+ Here is the call graph for this function:

◆ set_extruders()

void Slic3r::GCodeWriter::set_extruders ( std::vector< unsigned int >  extruder_ids)
39{
40 std::sort(extruder_ids.begin(), extruder_ids.end());
41 m_extruders.clear();
42 m_extruders.reserve(extruder_ids.size());
43 for (unsigned int extruder_id : extruder_ids)
44 m_extruders.emplace_back(Extruder(extruder_id, &this->config));
45
46 /* we enable support for multiple extruder if any extruder greater than 0 is used
47 (even if prints only uses that one) since we need to output Tx commands
48 first extruder has index 0 */
49 this->multiple_extruders = (*std::max_element(extruder_ids.begin(), extruder_ids.end())) > 0;
50}
std::vector< unsigned int > extruder_ids() const
Definition GCodeWriter.hpp:35

References config, extruder_ids(), m_extruders, and multiple_extruders.

Referenced by Slic3r::GCode::set_extruders().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_fan() [1/2]

std::string Slic3r::GCodeWriter::set_fan ( const GCodeFlavor  gcode_flavor,
bool  gcode_comments,
unsigned int  speed 
)
static
504{
505 std::ostringstream gcode;
506 if (speed == 0) {
507 switch (gcode_flavor) {
508 case gcfTeacup:
509 gcode << "M106 S0"; break;
510 case gcfMakerWare:
511 case gcfSailfish:
512 gcode << "M127"; break;
513 default:
514 gcode << "M107"; break;
515 }
516 if (gcode_comments)
517 gcode << " ; disable fan";
518 gcode << "\n";
519 } else {
520 switch (gcode_flavor) {
521 case gcfMakerWare:
522 case gcfSailfish:
523 gcode << "M126"; break;
524 case gcfMach3:
525 case gcfMachinekit:
526 gcode << "M106 P" << 255.0 * speed / 100.0; break;
527 default:
528 gcode << "M106 S" << 255.0 * speed / 100.0; break;
529 }
530 if (gcode_comments)
531 gcode << " ; enable fan";
532 gcode << "\n";
533 }
534 return gcode.str();
535}
gcode_flavor((ConfigOptionBool, gcode_label_objects))((ConfigOptionStrings

References Slic3r::gcfMach3, Slic3r::gcfMachinekit, Slic3r::gcfMakerWare, Slic3r::gcfSailfish, Slic3r::gcfTeacup, and Slic3r::gcode_flavor().

Referenced by Slic3r::GCode::_do_export(), Slic3r::CoolingBuffer::apply_layer_cooldown(), and set_fan().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_fan() [2/2]

std::string Slic3r::GCodeWriter::set_fan ( unsigned int  speed) const
538{
539 return GCodeWriter::set_fan(this->config.gcode_flavor, this->config.gcode_comments, speed);
540}
static std::string set_fan(const GCodeFlavor gcode_flavor, bool gcode_comments, unsigned int speed)
Definition GCodeWriter.cpp:503

References config, and set_fan().

+ Here is the call graph for this function:

◆ set_print_acceleration()

std::string Slic3r::GCodeWriter::set_print_acceleration ( unsigned int  acceleration)
inline
46{ return set_acceleration_internal(Acceleration::Print, acceleration); }
std::string set_acceleration_internal(Acceleration type, unsigned int acceleration)
Definition GCodeWriter.cpp:166

References Print, and set_acceleration_internal().

Referenced by Slic3r::GCode::_extrude(), Slic3r::GCode::extrude_loop(), Slic3r::GCode::extrude_multi_path(), and Slic3r::GCode::extrude_path().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_speed()

std::string Slic3r::GCodeWriter::set_speed ( double  F,
const std::string &  comment = std::string(),
const std::string &  cooling_marker = std::string() 
) const
249{
250 assert(F > 0.);
251 assert(F < 100000.);
252
253 GCodeG1Formatter w;
254 w.emit_f(F);
255 w.emit_comment(this->config.gcode_comments, comment);
256 w.emit_string(cooling_marker);
257 return w.string();
258}
@ F
Definition libslic3r.h:102

References config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_f(), Slic3r::GCodeFormatter::emit_string(), Slic3r::F, and Slic3r::GCodeFormatter::string().

Referenced by Slic3r::GCode::_extrude(), and Slic3r::Wipe::wipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_temperature()

std::string Slic3r::GCodeWriter::set_temperature ( unsigned int  temperature,
bool  wait = false,
int  tool = -1 
) const
89{
91 return {};
92
93 std::string code, comment;
95 code = "M109";
96 comment = "set temperature and wait for it to be reached";
97 } else {
98 if (FLAVOR_IS(gcfRepRapFirmware)) { // M104 is deprecated on RepRapFirmware
99 code = "G10";
100 } else {
101 code = "M104";
102 }
103 comment = "set temperature";
104 }
105
106 std::ostringstream gcode;
107 gcode << code << " ";
109 gcode << "P";
110 } else {
111 gcode << "S";
112 }
113 gcode << temperature;
114 bool multiple_tools = this->multiple_extruders && ! m_single_extruder_multi_material;
115 if (tool != -1 && (multiple_tools || FLAVOR_IS(gcfMakerWare) || FLAVOR_IS(gcfSailfish) || FLAVOR_IS(gcfRepRapFirmware)) ) {
117 gcode << " P" << tool;
118 } else {
119 gcode << " T" << tool;
120 }
121 }
122 gcode << " ; " << comment << "\n";
123
125 gcode << "M116 ; wait for temperature to be reached\n";
126
127 return gcode.str();
128}

References comment, FLAVOR_IS, FLAVOR_IS_NOT, Slic3r::gcfMach3, Slic3r::gcfMachinekit, Slic3r::gcfMakerWare, Slic3r::gcfRepRapFirmware, Slic3r::gcfSailfish, Slic3r::gcfTeacup, m_single_extruder_multi_material, and multiple_extruders.

Referenced by Slic3r::GCode::_print_first_layer_extruder_temperatures(), Slic3r::OozePrevention::post_toolchange(), Slic3r::OozePrevention::pre_toolchange(), Slic3r::GCode::process_layer(), and Slic3r::GCode::set_extruder().

+ Here is the caller graph for this function:

◆ set_travel_acceleration()

std::string Slic3r::GCodeWriter::set_travel_acceleration ( unsigned int  acceleration)
inline
47{ return set_acceleration_internal(Acceleration::Travel, acceleration); }

References set_acceleration_internal(), and Travel.

Referenced by Slic3r::GCode::travel_to().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ supports_separate_travel_acceleration()

bool Slic3r::GCodeWriter::supports_separate_travel_acceleration ( GCodeFlavor  flavor)
static
20{
21 return (flavor == gcfRepetier || flavor == gcfMarlinFirmware || flavor == gcfRepRapFirmware);
22}

References Slic3r::gcfMarlinFirmware, Slic3r::gcfRepetier, and Slic3r::gcfRepRapFirmware.

Referenced by Slic3r::GCodeProcessor::apply_config(), apply_print_config(), set_acceleration_internal(), and Slic3r::GCode::travel_to().

+ Here is the caller graph for this function:

◆ toolchange()

std::string Slic3r::GCodeWriter::toolchange ( unsigned int  extruder_id)
229{
230 // set the new extruder
231 auto it_extruder = Slic3r::lower_bound_by_predicate(m_extruders.begin(), m_extruders.end(), [extruder_id](const Extruder &e) { return e.id() < extruder_id; });
232 assert(it_extruder != m_extruders.end() && it_extruder->id() == extruder_id);
233 m_extruder = &*it_extruder;
234
235 // return the toolchange command
236 // if we are running a single-extruder setup, just set the extruder and return nothing
237 std::ostringstream gcode;
238 if (this->multiple_extruders) {
239 gcode << this->toolchange_prefix() << extruder_id;
240 if (this->config.gcode_comments)
241 gcode << " ; change extruder";
242 gcode << "\n";
243 gcode << this->reset_e(true);
244 }
245 return gcode.str();
246}
std::string toolchange_prefix() const
Definition GCodeWriter.cpp:222
ForwardIt lower_bound_by_predicate(ForwardIt first, ForwardIt last, LowerThanKeyPredicate lower_than_key)
Definition libslic3r.h:203

References config, Slic3r::lower_bound_by_predicate(), m_extruder, m_extruders, multiple_extruders, reset_e(), and toolchange_prefix().

Referenced by set_extruder(), and Slic3r::GCode::set_extruder().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toolchange_prefix()

std::string Slic3r::GCodeWriter::toolchange_prefix ( ) const
223{
224 return FLAVOR_IS(gcfMakerWare) ? "M135 T" :
225 FLAVOR_IS(gcfSailfish) ? "M108 T" : "T";
226}

References FLAVOR_IS, Slic3r::gcfMakerWare, and Slic3r::gcfSailfish.

Referenced by Slic3r::GCode::set_extruder(), and toolchange().

+ Here is the caller graph for this function:

◆ travel_to_xy()

std::string Slic3r::GCodeWriter::travel_to_xy ( const Vec2d point,
const std::string &  comment = std::string() 
)
261{
262 m_pos.x() = point.x();
263 m_pos.y() = point.y();
264
265 GCodeG1Formatter w;
266 w.emit_xy(point);
267 w.emit_f(this->config.travel_speed.value * 60.0);
268 w.emit_comment(this->config.gcode_comments, comment);
269 return w.string();
270}

References config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_f(), Slic3r::GCodeFormatter::emit_xy(), m_pos, and Slic3r::GCodeFormatter::string().

Referenced by Slic3r::WipeTowerIntegration::append_tcr(), Slic3r::GCode::extrude_loop(), Slic3r::GCode::travel_to(), and travel_to_xyz().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ travel_to_xyz()

std::string Slic3r::GCodeWriter::travel_to_xyz ( const Vec3d point,
const std::string &  comment = std::string() 
)
273{
274 // FIXME: This function was not being used when travel_speed_z was separated (bd6badf).
275 // Calculation of feedrate was not updated accordingly. If you want to use
276 // this function, fix it first.
277 std::terminate();
278
279 /* If target Z is lower than current Z but higher than nominal Z we
280 don't perform the Z move but we only move in the XY plane and
281 adjust the nominal Z by reducing the lift amount that will be
282 used for unlift. */
283 if (!this->will_move_z(point.z())) {
284 double nominal_z = m_pos.z() - m_lifted;
285 m_lifted -= (point.z() - nominal_z);
286 // In case that retract_lift == layer_height we could end up with almost zero in_m_lifted
287 // and a retract could be skipped (https://github.com/prusa3d/PrusaSlicer/issues/2154
288 if (std::abs(m_lifted) < EPSILON)
289 m_lifted = 0.;
290 return this->travel_to_xy(to_2d(point));
291 }
292
293 /* In all the other cases, we perform an actual XYZ move and cancel
294 the lift. */
295 m_lifted = 0;
296 m_pos = point;
297
298 GCodeG1Formatter w;
299 w.emit_xyz(point);
300 w.emit_f(this->config.travel_speed.value * 60.0);
301 w.emit_comment(this->config.gcode_comments, comment);
302 return w.string();
303}
bool will_move_z(double z) const
Definition GCodeWriter.cpp:339
std::string travel_to_xy(const Vec2d &point, const std::string &comment=std::string())
Definition GCodeWriter.cpp:260
Eigen::Matrix< typename Derived::Scalar, 2, 1, Eigen::DontAlign > to_2d(const Eigen::MatrixBase< Derived > &ptN)
Definition Point.hpp:121

References config, Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_f(), Slic3r::GCodeFormatter::emit_xyz(), EPSILON, m_lifted, m_pos, Slic3r::GCodeFormatter::string(), Slic3r::to_2d(), travel_to_xy(), and will_move_z().

+ Here is the call graph for this function:

◆ travel_to_z()

std::string Slic3r::GCodeWriter::travel_to_z ( double  z,
const std::string &  comment = std::string() 
)
306{
307 /* If target Z is lower than current Z but higher than nominal Z
308 we don't perform the move but we only adjust the nominal Z by
309 reducing the lift amount that will be used for unlift. */
310 if (!this->will_move_z(z)) {
311 double nominal_z = m_pos.z() - m_lifted;
312 m_lifted -= (z - nominal_z);
313 if (std::abs(m_lifted) < EPSILON)
314 m_lifted = 0.;
315 return {};
316 }
317
318 /* In all the other cases, we perform an actual Z move and cancel
319 the lift. */
320 m_lifted = 0;
321 return this->_travel_to_z(z, comment);
322}

References _travel_to_z(), comment, EPSILON, m_lifted, m_pos, and will_move_z().

Referenced by Slic3r::GCode::_do_export(), Slic3r::WipeTowerIntegration::append_tcr(), Slic3r::GCode::change_layer(), and Slic3r::GCode::preamble().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unlift()

std::string Slic3r::GCodeWriter::unlift ( )
480{
481 std::string gcode;
482 if (m_lifted > 0) {
483 gcode += this->_travel_to_z(m_pos.z() - m_lifted, "restore layer Z");
484 m_lifted = 0;
485 }
486 return gcode;
487}

References _travel_to_z(), m_lifted, and m_pos.

Referenced by Slic3r::WipeTowerIntegration::append_tcr(), and Slic3r::GCode::unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unretract()

std::string Slic3r::GCodeWriter::unretract ( )
436{
437 std::string gcode;
438
440 gcode = "M101 ; extruder on\n";
441
442 if (auto [dE, emitE] = m_extruder->unretract(); dE != 0) {
443 if (this->config.use_firmware_retraction) {
444 gcode += FLAVOR_IS(gcfMachinekit) ? "G23 ; unretract\n" : "G11 ; unretract\n";
445 gcode += this->reset_e();
446 } else if (! m_extrusion_axis.empty()) {
447 // use G1 instead of G0 because G0 will blend the restart with the previous travel move
448 GCodeG1Formatter w;
449 w.emit_e(m_extrusion_axis, emitE);
450 w.emit_f(m_extruder->deretract_speed() * 60.);
451 w.emit_comment(this->config.gcode_comments, " ; unretract");
452 gcode += w.string();
453 }
454 }
455
456 return gcode;
457}
int deretract_speed() const
Definition Extruder.cpp:143
std::pair< double, double > unretract()
Definition Extruder.cpp:60

References config, Slic3r::Extruder::deretract_speed(), Slic3r::GCodeFormatter::emit_comment(), Slic3r::GCodeFormatter::emit_e(), Slic3r::GCodeFormatter::emit_f(), FLAVOR_IS, Slic3r::gcfMachinekit, Slic3r::gcfMakerWare, m_extruder, m_extrusion_axis, reset_e(), Slic3r::GCodeFormatter::string(), and Slic3r::Extruder::unretract().

Referenced by Slic3r::WipeTowerIntegration::append_tcr(), Slic3r::ProcessLayer::emit_custom_gcode_per_print_z(), and Slic3r::GCode::unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update_position()

void Slic3r::GCodeWriter::update_position ( const Vec3d new_pos)
490{
491 assert(this->m_lifted >= 0);
492 const double nominal_z = m_pos.z() - m_lifted;
493 m_lifted = new_pos.z() - nominal_z;
494 if (m_lifted < - EPSILON)
495 throw Slic3r::RuntimeError("Custom G-code reports negative Z-hop. Final Z position is below the print_z height.");
496 // In case that retract_lift == layer_height we could end up with almost zero in_m_lifted
497 // and a retract could be skipped (https://github.com/prusa3d/PrusaSlicer/issues/2154
498 if (m_lifted < EPSILON)
499 m_lifted = 0.;
500 m_pos = new_pos;
501}

References EPSILON, m_lifted, and m_pos.

Referenced by Slic3r::GCode::placeholder_parser_process().

+ Here is the caller graph for this function:

◆ update_progress()

std::string Slic3r::GCodeWriter::update_progress ( unsigned int  num,
unsigned int  tot,
bool  allow_100 = false 
) const
208{
210 return {};
211
212 unsigned int percent = (unsigned int)floor(100.0 * num / tot + 0.5);
213 if (!allow_100) percent = std::min(percent, (unsigned int)99);
214
215 std::ostringstream gcode;
216 gcode << "M73 P" << percent;
217 if (this->config.gcode_comments) gcode << " ; update progress";
218 gcode << "\n";
219 return gcode.str();
220}
EIGEN_DEVICE_FUNC const FloorReturnType floor() const
Definition ArrayCwiseUnaryOps.h:388

References config, FLAVOR_IS_NOT, floor(), Slic3r::gcfMakerWare, and Slic3r::gcfSailfish.

Referenced by Slic3r::GCode::_do_export(), and Slic3r::GCode::change_layer().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ will_move_z()

bool Slic3r::GCodeWriter::will_move_z ( double  z) const
340{
341 /* If target Z is lower than current Z but higher than nominal Z
342 we don't perform an actual Z move. */
343 if (m_lifted > 0) {
344 double nominal_z = m_pos.z() - m_lifted;
345 if (z >= nominal_z && z <= m_pos.z())
346 return false;
347 }
348 return true;
349}

References m_lifted, and m_pos.

Referenced by Slic3r::GCode::change_layer(), travel_to_xyz(), and travel_to_z().

+ Here is the caller graph for this function:

Member Data Documentation

◆ config

◆ m_extruder

◆ m_extruders

std::vector<Extruder> Slic3r::GCodeWriter::m_extruders
private

◆ m_extrusion_axis

std::string Slic3r::GCodeWriter::m_extrusion_axis
private

◆ m_last_acceleration

unsigned int Slic3r::GCodeWriter::m_last_acceleration = (unsigned int)(-1)
private

◆ m_last_bed_temperature

unsigned int Slic3r::GCodeWriter::m_last_bed_temperature
private

Referenced by set_bed_temperature().

◆ m_last_bed_temperature_reached

bool Slic3r::GCodeWriter::m_last_bed_temperature_reached
private

Referenced by set_bed_temperature().

◆ m_last_travel_acceleration

unsigned int Slic3r::GCodeWriter::m_last_travel_acceleration = (unsigned int)(-1)
private

◆ m_lifted

double Slic3r::GCodeWriter::m_lifted
private

◆ m_max_acceleration

unsigned int Slic3r::GCodeWriter::m_max_acceleration
private

◆ m_max_travel_acceleration

unsigned int Slic3r::GCodeWriter::m_max_travel_acceleration
private

◆ m_pos

Vec3d Slic3r::GCodeWriter::m_pos = Vec3d::Zero()
private

◆ m_single_extruder_multi_material

bool Slic3r::GCodeWriter::m_single_extruder_multi_material
private

◆ multiple_extruders

bool Slic3r::GCodeWriter::multiple_extruders

The documentation for this class was generated from the following files: