From 473319b92d7ae5242981948b47f2c09db5cf9f96 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Thu, 15 Jul 2021 18:58:32 +0200 Subject: [PATCH] New --print command line option to send G-code --- src/slic3r.cpp | 47 ++++++++++++++++++++++++++++++++ src/slic3r.hpp | 3 +- xs/Build.PL | 1 - xs/src/libslic3r/GCodeSender.cpp | 2 -- xs/src/libslic3r/GCodeSender.hpp | 2 -- xs/src/libslic3r/PrintConfig.cpp | 11 ++++++++ xs/xsp/GCodeSender.xsp | 4 --- 7 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/slic3r.cpp b/src/slic3r.cpp index 1b642ce48..8ed723f36 100644 --- a/src/slic3r.cpp +++ b/src/slic3r.cpp @@ -1,4 +1,5 @@ #include "slic3r.hpp" +#include "GCodeSender.hpp" #include "Geometry.hpp" #include "IO.hpp" #include "Log.hpp" @@ -19,6 +20,8 @@ #include #include #include +#include +#include #ifdef USE_WX #include "GUI/GUI.hpp" @@ -337,6 +340,7 @@ int CLI::run(int argc, char **argv) { exit(EXIT_FAILURE); } Slic3r::Log::info("CLI") << "G-code exported to " << outfile << std::endl; + this->last_outfile = outfile; // output some statistics double duration { std::chrono::duration_cast(clock_::now() - t0).count() }; @@ -348,6 +352,49 @@ int CLI::run(int argc, char **argv) { << "Filament required: " << print.total_used_filament() << "mm" << " (" << print.total_extruded_volume()/1000 << "cm3)" << std::endl; } + } else if (opt_key == "print") { + if (this->models.size() > 1) { + Slic3r::Log::error("CLI") << "error: --print is not supported for multiple jobs" << std::endl; + exit(EXIT_FAILURE); + } + + // Get last sliced G-code or the manually supplied one + std::string gcode_file{ this->config.getString("gcode_file", "") }; + if (gcode_file.empty()) + gcode_file = this->last_outfile; + + if (gcode_file.empty()) { + Slic3r::Log::error("CLI") << "error: no G-code file to send; supply a model to slice or --gcode-file" << std::endl; + exit(EXIT_FAILURE); + } + + // Check serial port options + if (!this->print_config.has("serial_port") || !this->print_config.has("serial_speed")) { + Slic3r::Log::error("CLI") << "error: missing required --serial-port and --serial-speed" << std::endl; + exit(EXIT_FAILURE); + } + + // Connect to printer + Slic3r::GCodeSender sender; + sender.connect( + this->print_config.getString("serial_port"), + this->print_config.getInt("serial_speed") + ); + while (!sender.is_connected()) {} + boost::nowide::cout << "Connected to printer" << std::endl; + + // Send file line-by-line + std::ifstream infile(gcode_file); + std::string line; + while (std::getline(infile, line)) { + sender.send(line); + } + + // Print queue size + while (sender.queue_size() > 0) { + boost::nowide::cout << "Queue size: " << sender.queue_size() << std::endl; + } + boost::nowide::cout << "Print completed!" << std::endl; } else { Slic3r::Log::error("CLI") << "error: option not supported yet: " << opt_key << std::endl; exit(EXIT_FAILURE); diff --git a/src/slic3r.hpp b/src/slic3r.hpp index e4ba4a498..68707ecb3 100644 --- a/src/slic3r.hpp +++ b/src/slic3r.hpp @@ -20,7 +20,8 @@ class CLI { FullPrintConfig full_print_config; t_config_option_keys input_files, actions, transforms; std::vector models; - + std::string last_outfile; + /// Prints usage of the CLI. void print_help(bool include_print_options = false) const; diff --git a/xs/Build.PL b/xs/Build.PL index 0655b7c12..fabbf9339 100644 --- a/xs/Build.PL +++ b/xs/Build.PL @@ -208,7 +208,6 @@ if (!$ENV{SLIC3R_STATIC} && $have_boost) { } } } -push @cflags, '-DBOOST_LIBS' if $have_boost; die <<'EOF' if !$have_boost; Slic3r requires the Boost libraries. Please make sure they are installed. diff --git a/xs/src/libslic3r/GCodeSender.cpp b/xs/src/libslic3r/GCodeSender.cpp index 0ad3f8cb0..5b536031b 100644 --- a/xs/src/libslic3r/GCodeSender.cpp +++ b/xs/src/libslic3r/GCodeSender.cpp @@ -1,4 +1,3 @@ -#ifdef BOOST_LIBS #include "GCodeSender.hpp" #include #include @@ -564,4 +563,3 @@ GCodeSender::reset() } -#endif diff --git a/xs/src/libslic3r/GCodeSender.hpp b/xs/src/libslic3r/GCodeSender.hpp index 4285025f4..8f61a80b2 100644 --- a/xs/src/libslic3r/GCodeSender.hpp +++ b/xs/src/libslic3r/GCodeSender.hpp @@ -1,6 +1,5 @@ #ifndef slic3r_GCodeSender_hpp_ #define slic3r_GCodeSender_hpp_ -#ifdef BOOST_LIBS #include "libslic3r.h" #include @@ -84,4 +83,3 @@ class GCodeSender : private boost::noncopyable { } #endif -#endif diff --git a/xs/src/libslic3r/PrintConfig.cpp b/xs/src/libslic3r/PrintConfig.cpp index 9b72137a2..f463c3441 100644 --- a/xs/src/libslic3r/PrintConfig.cpp +++ b/xs/src/libslic3r/PrintConfig.cpp @@ -1992,6 +1992,12 @@ CLIActionsConfigDef::CLIActionsConfigDef() def->tooltip = __TRANS("Save configuration to the specified file."); def->cli = "save"; def->default_value = new ConfigOptionString(); + + def = this->add("print", coBool); + def->label = __TRANS("Send G-code"); + def->tooltip = __TRANS("Send G-code to a printer."); + def->cli = "print"; + def->default_value = new ConfigOptionBool(false); } CLITransformConfigDef::CLITransformConfigDef() @@ -2116,6 +2122,11 @@ CLIMiscConfigDef::CLIMiscConfigDef() def->label = __TRANS("Output File"); def->tooltip = __TRANS("The file where the output will be written (if not specified, it will be based on the input file)."); def->cli = "output|o"; + + def = this->add("gcode_file", coString); + def->label = __TRANS("G-code file"); + def->tooltip = __TRANS("The G-code file to send to the printer."); + def->cli = "gcode-file"; #ifdef USE_WX def = this->add("autosave", coString); diff --git a/xs/xsp/GCodeSender.xsp b/xs/xsp/GCodeSender.xsp index 9c55194e5..f99244a1f 100644 --- a/xs/xsp/GCodeSender.xsp +++ b/xs/xsp/GCodeSender.xsp @@ -1,7 +1,5 @@ %module{Slic3r::XS}; -#ifdef BOOST_LIBS - %{ #include #include "libslic3r/GCodeSender.hpp" @@ -24,5 +22,3 @@ std::string getT(); std::string getB(); }; - -#endif