Skip to content

Commit

Permalink
Merge branch 'octoprint'
Browse files Browse the repository at this point in the history
  • Loading branch information
bubnikv committed Mar 6, 2018
2 parents 51da427 + e26ccfc commit 4a90ab1
Show file tree
Hide file tree
Showing 15 changed files with 978 additions and 56 deletions.
5 changes: 2 additions & 3 deletions lib/Slic3r/GUI/MainFrame.pm
Expand Up @@ -174,7 +174,6 @@ sub _init_tabpanel {
EVT_COMMAND($self, -1, $BUTTON_BROWSE_EVENT, sub {
my ($self, $event) = @_;
my $msg = $event->GetString;
print "BUTTON_BROWSE_EVENT: ", $msg, "\n";

# look for devices
my $entries;
Expand All @@ -197,7 +196,6 @@ sub _init_tabpanel {
EVT_COMMAND($self, -1, $BUTTON_TEST_EVENT, sub {
my ($self, $event) = @_;
my $msg = $event->GetString;
print "BUTTON_TEST_EVENT: ", $msg, "\n";

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
Expand Down Expand Up @@ -409,7 +407,7 @@ sub _init_menubar {
wxTheApp->about;
});
}

# menubar
# assign menubar to frame after appending items, otherwise special items
# will not be handled correctly
Expand All @@ -424,6 +422,7 @@ sub _init_menubar {
# (Select application language from the list of installed languages)
Slic3r::GUI::add_debug_menu($menubar, $self->{lang_ch_event});
$menubar->Append($helpMenu, L("&Help"));
# Add an optional debug menu. In production code, the add_debug_menu() call should do nothing.
$self->SetMenuBar($menubar);
}
}
Expand Down
48 changes: 7 additions & 41 deletions lib/Slic3r/GUI/Plater.pm
Expand Up @@ -52,7 +52,7 @@ sub new {
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
$self->{config} = Slic3r::Config::new_from_defaults_keys([qw(
bed_shape complete_objects extruder_clearance_radius skirts skirt_distance brim_width variable_layer_height
serial_port serial_speed octoprint_host octoprint_apikey
serial_port serial_speed octoprint_host octoprint_apikey octoprint_cafile
nozzle_diameter single_extruder_multi_material
wipe_tower wipe_tower_x wipe_tower_y wipe_tower_width wipe_tower_per_color_wipe extruder_colour filament_colour
)]);
Expand Down Expand Up @@ -1458,8 +1458,13 @@ sub on_export_completed {
wxTheApp->notify($message);

$self->do_print if $do_print;

# Send $self->{send_gcode_file} to OctoPrint.
$self->send_gcode if $send_gcode;
if ($send_gcode) {
my $op = Slic3r::OctoPrint->new($self->{config});
$op->send_gcode($self->GetId(), $PROGRESS_BAR_EVENT, $ERROR_EVENT, $self->{send_gcode_file});
}

$self->{print_file} = undef;
$self->{send_gcode_file} = undef;
$self->{"print_info_cost"}->SetLabel(sprintf("%.2f" , $self->{print}->total_cost));
Expand Down Expand Up @@ -1488,45 +1493,6 @@ sub do_print {
my $filament_names = wxTheApp->{preset_bundle}->filament_presets;
$filament_stats = { map { $filament_names->[$_] => $filament_stats->{$_} } keys %$filament_stats };
$printer_panel->load_print_job($self->{print_file}, $filament_stats);

$self->GetFrame->select_tab(1);
}

# Send $self->{send_gcode_file} to OctoPrint.
#FIXME Currently this call blocks the UI. Make it asynchronous.
sub send_gcode {
my ($self) = @_;

$self->statusbar->StartBusy;

my $ua = LWP::UserAgent->new;
$ua->timeout(180);

my $res = $ua->post(
"http://" . $self->{config}->octoprint_host . "/api/files/local",
Content_Type => 'form-data',
'X-Api-Key' => $self->{config}->octoprint_apikey,
Content => [
file => [
# On Windows, the path has to be encoded in local code page for perl to be able to open it.
Slic3r::encode_path($self->{send_gcode_file}),
# Remove the UTF-8 flag from the perl string, so the LWP::UserAgent can insert
# the UTF-8 encoded string into the request as a byte stream.
Slic3r::path_to_filename_raw($self->{send_gcode_file})
],
print => $self->{send_gcode_file_print} ? 1 : 0,
],
);

$self->statusbar->StopBusy;

if ($res->is_success) {
$self->statusbar->SetStatusText(L("G-code file successfully uploaded to the OctoPrint server"));
} else {
my $message = L("Error while uploading to the OctoPrint server: ") . $res->status_line;
Slic3r::GUI::show_error($self, $message);
$self->statusbar->SetStatusText($message);
}
}

sub export_stl {
Expand Down
28 changes: 20 additions & 8 deletions xs/CMakeLists.txt
Expand Up @@ -201,6 +201,10 @@ add_library(libslic3r_gui STATIC
${LIBDIR}/slic3r/GUI/wxExtensions.hpp
${LIBDIR}/slic3r/Utils/Http.cpp
${LIBDIR}/slic3r/Utils/Http.hpp
${LIBDIR}/slic3r/Utils/OctoPrint.cpp
${LIBDIR}/slic3r/Utils/OctoPrint.hpp
${LIBDIR}/slic3r/Utils/Bonjour.cpp
${LIBDIR}/slic3r/Utils/Bonjour.hpp
)

add_library(admesh STATIC
Expand Down Expand Up @@ -340,6 +344,7 @@ set(XS_XSP_FILES
${XSP_DIR}/Surface.xsp
${XSP_DIR}/SurfaceCollection.xsp
${XSP_DIR}/TriangleMesh.xsp
${XSP_DIR}/Utils_OctoPrint.xsp
${XSP_DIR}/XS.xsp
)
foreach (file ${XS_XSP_FILES})
Expand Down Expand Up @@ -529,14 +534,21 @@ find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
target_link_libraries(XS ${CURL_LIBRARIES})

if (SLIC3R_STATIC AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
# As of now, our build system produces a statically linked libcurl,
# which links the OpenSSL library dynamically.
find_package(OpenSSL REQUIRED)
message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(XS ${OPENSSL_LIBRARIES})
if (SLIC3R_STATIC)
if (NOT APPLE)
# libcurl is always linked dynamically to the system libcurl on OSX.
# On other systems, libcurl is linked statically if SLIC3R_STATIC is set.
add_definitions(-DCURL_STATICLIB)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# As of now, our build system produces a statically linked libcurl,
# which links the OpenSSL library dynamically.
find_package(OpenSSL REQUIRED)
message("OpenSSL include dir: ${OPENSSL_INCLUDE_DIR}")
message("OpenSSL libraries: ${OPENSSL_LIBRARIES}")
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(XS ${OPENSSL_LIBRARIES})
endif()
endif()

## OPTIONAL packages
Expand Down
11 changes: 9 additions & 2 deletions xs/src/libslic3r/PrintConfig.cpp
Expand Up @@ -904,10 +904,17 @@ PrintConfigDef::PrintConfigDef()
def->cli = "octoprint-apikey=s";
def->default_value = new ConfigOptionString("");

def = this->add("octoprint_cafile", coString);
def->label = "HTTPS CA file";
def->tooltip = "Custom CA certificate file can be specified for HTTPS OctoPrint connections, in crt/pem format. "
"If left blank, the default OS CA certificate repository is used.";
def->cli = "octoprint-cafile=s";
def->default_value = new ConfigOptionString("");

def = this->add("octoprint_host", coString);
def->label = L("Host or IP");
def->label = L("Hostname, IP or URL");
def->tooltip = L("Slic3r can upload G-code files to OctoPrint. This field should contain "
"the hostname or IP address of the OctoPrint instance.");
"the hostname, IP address or URL of the OctoPrint instance.");
def->cli = "octoprint-host=s";
def->default_value = new ConfigOptionString("");

Expand Down
2 changes: 2 additions & 0 deletions xs/src/libslic3r/PrintConfig.hpp
Expand Up @@ -684,6 +684,7 @@ class HostConfig : public StaticPrintConfig
public:
ConfigOptionString octoprint_host;
ConfigOptionString octoprint_apikey;
ConfigOptionString octoprint_cafile;
ConfigOptionString serial_port;
ConfigOptionInt serial_speed;

Expand All @@ -692,6 +693,7 @@ class HostConfig : public StaticPrintConfig
{
OPT_PTR(octoprint_host);
OPT_PTR(octoprint_apikey);
OPT_PTR(octoprint_cafile);
OPT_PTR(serial_port);
OPT_PTR(serial_speed);
}
Expand Down
1 change: 1 addition & 0 deletions xs/src/perlglue.cpp
Expand Up @@ -64,6 +64,7 @@ REGISTER_CLASS(PresetCollection, "GUI::PresetCollection");
REGISTER_CLASS(PresetBundle, "GUI::PresetBundle");
REGISTER_CLASS(PresetHints, "GUI::PresetHints");
REGISTER_CLASS(TabIface, "GUI::Tab");
REGISTER_CLASS(OctoPrint, "OctoPrint");

SV* ConfigBase__as_hash(ConfigBase* THIS)
{
Expand Down
16 changes: 15 additions & 1 deletion xs/src/slic3r/GUI/GUI.cpp
Expand Up @@ -5,9 +5,9 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/format.hpp>

#if __APPLE__
#import <IOKit/pwr_mgt/IOPMLib.h>
Expand Down Expand Up @@ -573,4 +573,18 @@ wxString from_u8(std::string str)
return wxString::FromUTF8(str.c_str());
}

wxWindow *get_widget_by_id(int id)
{
if (g_wxMainFrame == nullptr) {
throw std::runtime_error("Main frame not set");
}

wxWindow *window = g_wxMainFrame->FindWindow(id);
if (window == nullptr) {
throw std::runtime_error((boost::format("Could not find widget by ID: %1%") % id).str());
}

return window;
}

} }
3 changes: 3 additions & 0 deletions xs/src/slic3r/GUI/GUI.hpp
Expand Up @@ -6,6 +6,7 @@
#include "Config.hpp"

class wxApp;
class wxWindow;
class wxFrame;
class wxWindow;
class wxMenuBar;
Expand Down Expand Up @@ -121,6 +122,8 @@ wxString L_str(std::string str);
// Return wxString from std::string in UTF8
wxString from_u8(std::string str);

wxWindow *get_widget_by_id(int id);

}
}

Expand Down
2 changes: 1 addition & 1 deletion xs/src/slic3r/GUI/Preset.cpp
Expand Up @@ -224,7 +224,7 @@ const std::vector<std::string>& Preset::printer_options()
if (s_opts.empty()) {
s_opts = {
"bed_shape", "z_offset", "gcode_flavor", "use_relative_e_distances", "serial_port", "serial_speed",
"octoprint_host", "octoprint_apikey", "use_firmware_retraction", "use_volumetric_e", "variable_layer_height",
"octoprint_host", "octoprint_apikey", "octoprint_cafile", "use_firmware_retraction", "use_volumetric_e", "variable_layer_height",
"single_extruder_multi_material", "start_gcode", "end_gcode", "before_layer_gcode", "layer_gcode", "toolchange_gcode",
"between_objects_gcode", "printer_notes"
};
Expand Down

0 comments on commit 4a90ab1

Please sign in to comment.