Skip to content

Commit

Permalink
pause_print_gcode and color_change_gcode use default value if empty b…
Browse files Browse the repository at this point in the history
…ased on g-code flavor

also:
 . moved the code inside gcodewriter to try to centralize at least a bit the code about firmware.
 . cleaned dead code in tab
 . removed the PausePrintCode in gcode writer
 . set firmware without the gcode support to "" so it isn't show by default.
 . add warning when creating gcode if we have a pause, no custom gcode, no default (possible when switching firmware)
 . little rewrite of how to pass the mutable part of print inside GCode
 . GCode no more a friend of Print

Started by @your-friend-alice, thank you
#3943
  • Loading branch information
supermerill committed Nov 16, 2023
1 parent cb9dbed commit 44fa0ad
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 88 deletions.
134 changes: 77 additions & 57 deletions src/libslic3r/GCode.cpp

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/libslic3r/GCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ class GCode : ExtrusionVisitorConst {
};
void _do_export(Print &print, GCodeOutputStream &file, ThumbnailsGeneratorCallback thumbnail_cb);

void _init_multiextruders(Print& print, GCodeOutputStream& file, GCodeWriter& writer, ToolOrdering& tool_ordering, const std::string& custom_gcode);
void _init_multiextruders(const Print& print, GCodeOutputStream& file, GCodeWriter& writer, const ToolOrdering& tool_ordering, const std::string& custom_gcode);

static std::vector<LayerToPrint> collect_layers_to_print(const PrintObject &object);
static std::vector<std::pair<coordf_t, std::vector<LayerToPrint>>> collect_layers_to_print(const Print &print);
static std::vector<LayerToPrint> collect_layers_to_print(const PrintObject &object, Print::StatusMonitor &status_monitor);
static std::vector<std::pair<coordf_t, std::vector<LayerToPrint>>> collect_layers_to_print(const Print &print, Print::StatusMonitor &status_monitor);

LayerResult process_layer(
const Print &print,
PrintStatistics &print_stat,
Print::StatusMonitor &status_monitor,
// Set of object & print layers of the same PrintObject and with the same print_z.
const std::vector<LayerToPrint> &layers,
const LayerTools &layer_tools,
Expand All @@ -269,7 +269,7 @@ class GCode : ExtrusionVisitorConst {
// and export G-code into file.
void process_layers(
const Print &print,
PrintStatistics &print_stat,
Print::StatusMonitor &status_monitor,
const ToolOrdering &tool_ordering,
const std::vector<const PrintInstance*> &print_object_instances_ordering,
const std::vector<std::pair<coordf_t, std::vector<LayerToPrint>>> &layers_to_print,
Expand All @@ -279,7 +279,7 @@ class GCode : ExtrusionVisitorConst {
// and export G-code into file.
void process_layers(
const Print &print,
PrintStatistics &print_stat,
Print::StatusMonitor &status_monitor,
const ToolOrdering &tool_ordering,
std::vector<LayerToPrint> layers_to_print,
const size_t single_object_idx,
Expand Down Expand Up @@ -506,9 +506,9 @@ class GCode : ExtrusionVisitorConst {
std::string _before_extrude(const ExtrusionPath &path, const std::string &description, double speed = -1);
double_t _compute_speed_mm_per_sec(const ExtrusionPath& path, double speed = -1);
std::string _after_extrude(const ExtrusionPath &path);
void print_machine_envelope(GCodeOutputStream &file, Print &print);
void _print_first_layer_bed_temperature(GCodeOutputStream &file, Print &print, const std::string &gcode, uint16_t first_printing_extruder_id, bool wait);
void _print_first_layer_extruder_temperatures(GCodeOutputStream &file, Print &print, const std::string &gcode, uint16_t first_printing_extruder_id, bool wait);
void print_machine_envelope(GCodeOutputStream &file, const Print &print);
void _print_first_layer_bed_temperature(GCodeOutputStream &file, const Print &print, const std::string &gcode, uint16_t first_printing_extruder_id, bool wait);
void _print_first_layer_extruder_temperatures(GCodeOutputStream &file, const Print &print, const std::string &gcode, uint16_t first_printing_extruder_id, bool wait);
// On the first printing layer. This flag triggers first layer speeds.
bool on_first_layer() const { return m_layer != nullptr && m_layer->id() == 0; }
// To control print speed of 1st object layer over raft interface.
Expand Down
30 changes: 29 additions & 1 deletion src/libslic3r/GCodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,36 @@
#define E_NUM(val) PRECISION(val, this->config.gcode_precision_e.value)
namespace Slic3r {

std::string GCodeWriter::get_default_pause_gcode(const GCodeConfig &config)
{
if (config.pause_print_gcode.value.empty()) {
if (config.gcode_flavor.value == GCodeFlavor::gcfKlipper) {
return "PAUSE";
} else if (config.gcode_flavor.value == GCodeFlavor::gcfRepRap || config.gcode_flavor.value == GCodeFlavor::gcfMarlinLegacy /*prusa only*/) {
return "M601";
} else {
// no pause command for other firmware, for what i am aware. Please submit a pullrequest or issue if they change.
return "";
}
} else {
return config.pause_print_gcode.value;
}
}

std::string GCodeWriter::PausePrintCode = "M601";
std::string GCodeWriter::get_default_color_change_gcode(const GCodeConfig &config)
{
if (config.color_change_gcode.value.empty()) {
if (config.gcode_flavor.value == GCodeFlavor::gcfRepRap || config.gcode_flavor.value == GCodeFlavor::gcfMarlinLegacy ||
config.gcode_flavor.value == GCodeFlavor::gcfMarlinFirmware || config.gcode_flavor.value == GCodeFlavor::gcfSmoothie) {
return "M600";
} else {
// no pause command for other firmware, for what i am aware. Please submit a pullrequest or issue if they change.
return "";
}
} else {
return config.color_change_gcode.value;
}
}

void GCodeWriter::apply_print_config(const PrintConfig &print_config)
{
Expand Down
4 changes: 3 additions & 1 deletion src/libslic3r/GCodeWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Slic3r {

class GCodeWriter {
public:
static std::string PausePrintCode;
GCodeConfig config;
bool multiple_extruders;
// override from region
Expand Down Expand Up @@ -90,6 +89,9 @@ class GCodeWriter {
std::string set_fan(uint8_t speed, uint16_t default_tool = 0);
uint8_t get_fan() { return m_last_fan_speed; }

static std::string get_default_pause_gcode(const GCodeConfig &config);
static std::string get_default_color_change_gcode(const GCodeConfig &config);

private:
// Extruders are sorted by their ID, so that binary search is possible.
std::vector<Extruder> m_extruders;
Expand Down
24 changes: 22 additions & 2 deletions src/libslic3r/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,28 @@ class Print : public PrintBaseWithState<PrintStep, psCount>
// Invalidates the step, and its depending steps in Print.
//in public to invalidate gcode when the physical printer change. It's needed if we allow the gcode macro to read these values.
bool invalidate_step(PrintStep step);

// just a little wrapper to let the user know that this print can only be modified to emit warnings & update advancement status, change stats.
// TODO: have the status out of the printbase class and into another one, so we can have a const print & a mutable statusmonitor
class StatusMonitor
{
private:
Print& print;

public:
StatusMonitor(Print &print_mutable) : print(print_mutable) {}

// need this extra method because active_step_add_warning is protected and so need the friend status, and Gcode has it.
void active_step_add_warning(PrintStateBase::WarningLevel warning_level, const std::string &message, int message_id = 0)
{
print.active_step_add_warning(warning_level, message, message_id);
}
PrintStatistics &stats() { return print.m_print_statistics; }
bool set_started(PrintStep step) { return print.set_started(step); }
PrintStateBase::TimeStamp set_done(PrintStep step) { return print.set_done(step); }

};

protected:
private:

Expand Down Expand Up @@ -708,8 +730,6 @@ class Print : public PrintBaseWithState<PrintStep, psCount>
// tiem of last change, to see if the gui need to be updated
std::time_t m_timestamp_last_change;

// To allow GCode to set the Print's GCodeExport step status.
friend class GCode;
// Allow PrintObject to access m_mutex and m_cancel_callback.
friend class PrintObject;
};
Expand Down
6 changes: 4 additions & 2 deletions src/libslic3r/PrintBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ class PrintBase : public ObjectBase
// To be called by the worker thread and its sub-threads (mostly launched on the TBB thread pool) regularly.
//public to ebablet ot call it from brim code.
void throw_if_canceled() const { if (m_cancel_status.load(std::memory_order_acquire)) throw CanceledException(); }

// Update "scale", "input_filename", "input_filename_base" placeholders from the current printable ModelObjects.
void update_object_placeholders(DynamicConfig &config, const std::string &default_ext) const;

protected:
friend class PrintObjectBase;
friend class BackgroundSlicingProcess;
Expand All @@ -522,8 +526,6 @@ class PrintBase : public ObjectBase

// To be called by this->output_filename() with the format string pulled from the configuration layer.
std::string output_filename(const std::string &format, const std::string &default_ext, const std::string &filename_base, const DynamicConfig *config_override = nullptr) const;
// Update "scale", "input_filename", "input_filename_base" placeholders from the current printable ModelObjects.
void update_object_placeholders(DynamicConfig &config, const std::string &default_ext) const;

Model m_model;
DynamicPrintConfig m_full_print_config;
Expand Down
10 changes: 6 additions & 4 deletions src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5044,21 +5044,23 @@ void PrintConfigDef::init_fff_params()

def = this->add("color_change_gcode", coString);
def->label = L("Color change G-code");
def->tooltip = L("This G-code will be used as a code for the color change");
def->tooltip = L("This G-code will be used as a code for the color change"
" If empty, the default color change print command for the selected G-code flavor will be used (if any).");
def->multiline = true;
def->full_width = true;
def->height = 12;
def->mode = comExpert | comPrusa;
def->set_default_value(new ConfigOptionString("M600"));
def->set_default_value(new ConfigOptionString(""));

def = this->add("pause_print_gcode", coString);
def->label = L("Pause Print G-code");
def->tooltip = L("This G-code will be used as a code for the pause print");
def->tooltip = L("This G-code will be used as a code for the pause print."
" If empty, the default pause print command for the selected G-code flavor will be used (if any).");
def->multiline = true;
def->full_width = true;
def->height = 12;
def->mode = comExpert | comPrusa;
def->set_default_value(new ConfigOptionString("M601"));
def->set_default_value(new ConfigOptionString(""));

def = this->add("template_custom_gcode", coString);
def->label = L("Custom G-code");
Expand Down
14 changes: 8 additions & 6 deletions src/slic3r/GUI/DoubleSlider.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "libslic3r/libslic3r.h"
#include "DoubleSlider.hpp"
#include "libslic3r/GCode.hpp"
#include "GUI.hpp"
#include "GUI_App.hpp"
#include "Plater.hpp"
#include "I18N.hpp"
#include "ExtruderSequenceDialog.hpp"
#include "libslic3r/Print.hpp"
#include "libslic3r/AppConfig.hpp"
#include "libslic3r/GCode.hpp"
#include "libslic3r/GCodeWriter.hpp"
#include "libslic3r/Print.hpp"
#include "GUI_Utils.hpp"
#include "MsgDialog.hpp"
#include "Tab.hpp"
Expand Down Expand Up @@ -51,10 +52,11 @@ wxDEFINE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent);

static std::string gcode(Type type)
{
const PrintConfig& config = GUI::wxGetApp().plater()->fff_print().config();
const Print& print = GUI::wxGetApp().plater()->fff_print();
const PrintConfig &config = print.config();
switch (type) {
case ColorChange: return config.color_change_gcode;
case PausePrint: return config.pause_print_gcode;
case ColorChange: return Slic3r::GCodeWriter::get_default_color_change_gcode(config);
case PausePrint: return Slic3r::GCodeWriter::get_default_pause_gcode(config);
case Template: return config.template_custom_gcode;
default: return "";
}
Expand Down Expand Up @@ -2014,7 +2016,7 @@ void Control::show_add_context_menu()
{
wxMenu menu;

if (m_mode == SingleExtruder) {
if (m_mode == SingleExtruder && !gcode(ColorChange).empty()) {
append_menu_item(&menu, wxID_ANY, _L("Add color change") + " (" + gcode(ColorChange) + ")", "",
[this](wxCommandEvent&) { add_code_as_tick(ColorChange); }, "colorchange_add_m", &menu);

Expand Down
6 changes: 0 additions & 6 deletions src/slic3r/GUI/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "PresetComboBoxes.hpp"
#include <wx/wupdlock.h>

#include <libslic3r/GCodeWriter.hpp>
#include <libslic3r/Slicing.hpp>

#include "GUI_App.hpp"
Expand Down Expand Up @@ -3363,11 +3362,6 @@ void TabPrinter::toggle_options()
field = get_field("silent_mode");
if (field) field->toggle(is_marlin_flavor);

if (m_config->option<ConfigOptionEnum<GCodeFlavor>>("gcode_flavor")->value == gcfKlipper)
GCodeWriter::PausePrintCode = "PAUSE";
else
GCodeWriter::PausePrintCode = "M601";

if (m_last_gcode_flavor != uint8_t(m_config->option<ConfigOptionEnum<GCodeFlavor>>("gcode_flavor")->value)) {
m_last_gcode_flavor = uint8_t(m_config->option<ConfigOptionEnum<GCodeFlavor>>("gcode_flavor")->value);
m_rebuild_kinematics_page = true;
Expand Down

0 comments on commit 44fa0ad

Please sign in to comment.