Skip to content

Commit

Permalink
New --print command line option to send G-code
Browse files Browse the repository at this point in the history
  • Loading branch information
alranel committed Jul 15, 2021
1 parent 8abcb51 commit 473319b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
47 changes: 47 additions & 0 deletions src/slic3r.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "slic3r.hpp"
#include "GCodeSender.hpp"
#include "Geometry.hpp"
#include "IO.hpp"
#include "Log.hpp"
Expand All @@ -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"
Expand Down Expand Up @@ -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() };
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/slic3r.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ 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;

Expand Down
1 change: 0 additions & 1 deletion xs/Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions xs/src/libslic3r/GCodeSender.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#ifdef BOOST_LIBS
#include "GCodeSender.hpp"
#include <iostream>
#include <istream>
Expand Down Expand Up @@ -564,4 +563,3 @@ GCodeSender::reset()

}

#endif
2 changes: 0 additions & 2 deletions xs/src/libslic3r/GCodeSender.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef slic3r_GCodeSender_hpp_
#define slic3r_GCodeSender_hpp_
#ifdef BOOST_LIBS

#include "libslic3r.h"
#include <queue>
Expand Down Expand Up @@ -84,4 +83,3 @@ class GCodeSender : private boost::noncopyable {
}

#endif
#endif
11 changes: 11 additions & 0 deletions xs/src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions xs/xsp/GCodeSender.xsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
%module{Slic3r::XS};

#ifdef BOOST_LIBS

%{
#include <xsinit.h>
#include "libslic3r/GCodeSender.hpp"
Expand All @@ -24,5 +22,3 @@
std::string getT();
std::string getB();
};

#endif

0 comments on commit 473319b

Please sign in to comment.