New --print command line option to send G-code

This commit is contained in:
Alessandro Ranellucci 2021-07-15 18:58:32 +02:00
parent 8abcb51133
commit 473319b92d
7 changed files with 60 additions and 10 deletions

View File

@ -1,4 +1,5 @@
#include "slic3r.hpp"
#include "GCodeSender.hpp"
#include "Geometry.hpp"
#include "IO.hpp"
#include "Log.hpp"
@ -19,6 +20,8 @@
#include <boost/nowide/args.hpp>
#include <boost/nowide/iostream.hpp>
#include <stdexcept>
#include <sstream>
#include <string>
#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<second_>(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);

View File

@ -20,6 +20,7 @@ class CLI {
FullPrintConfig full_print_config;
t_config_option_keys input_files, actions, transforms;
std::vector<Model> models;
std::string last_outfile;
/// Prints usage of the CLI.
void print_help(bool include_print_options = false) const;

View File

@ -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.

View File

@ -1,4 +1,3 @@
#ifdef BOOST_LIBS
#include "GCodeSender.hpp"
#include <iostream>
#include <istream>
@ -564,4 +563,3 @@ GCodeSender::reset()
}
#endif

View File

@ -1,6 +1,5 @@
#ifndef slic3r_GCodeSender_hpp_
#define slic3r_GCodeSender_hpp_
#ifdef BOOST_LIBS
#include "libslic3r.h"
#include <queue>
@ -84,4 +83,3 @@ class GCodeSender : private boost::noncopyable {
}
#endif
#endif

View File

@ -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()
@ -2117,6 +2123,11 @@ CLIMiscConfigDef::CLIMiscConfigDef()
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);
def->label = __TRANS("Autosave");

View File

@ -1,7 +1,5 @@
%module{Slic3r::XS};
#ifdef BOOST_LIBS
%{
#include <xsinit.h>
#include "libslic3r/GCodeSender.hpp"
@ -24,5 +22,3 @@
std::string getT();
std::string getB();
};
#endif