From a3af688173186aa47d7cf79ad08d511f8969dbf0 Mon Sep 17 00:00:00 2001 From: Mark Hindess Date: Wed, 27 Jun 2012 17:42:34 +0100 Subject: [PATCH] Simple remote interface to slic3r using plack. Only supports a single stl model but supporting other file formats and merge would be quite simple. Speed might mean requests timeout if slicing takes too long. Just a proof-of-concept really but I might use it to slice on my server from my laptop. --- utils/slic3r.psgi | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 utils/slic3r.psgi diff --git a/utils/slic3r.psgi b/utils/slic3r.psgi new file mode 100755 index 000000000..c0632d122 --- /dev/null +++ b/utils/slic3r.psgi @@ -0,0 +1,81 @@ +#!/usr/bin/perl +use strict; +use warnings; +use File::Temp qw/tempfile/; +use Plack::Request; +use Slic3r; + +# Usage: perl -Ilib utils/slic3r.psgi +# Then point your browser at http:/// or use: +# curl -o thing.gcode -F stlfile=@/path/to/thing.stl \ +# http://127.0.0.1:5000/gcode + +use constant { + CONFIG => $ENV{SLIC3R_CONFIG}, + DEBUG => $ENV{SLIC3R_PSGI_DEBUG}, +}; + +unless (caller) { + require Plack::Runner; + Plack::Runner->run(@ARGV, $0); +} + +if (defined CONFIG) { + Slic3r::Config->load(CONFIG); +} else { + print "Using defaults as environment variable SLIC3R_CONFIG is not set\n"; +} + +Slic3r::Config->validate; + +my $form; +{ + local $/; + $form = +}; + +my $app = sub { + my $env = shift; + my $req = Plack::Request->new($env); + my $path_info = $req->path_info; + if ($path_info =~ m!^/gcode!) { + my $upload = $req->uploads->{'stlfile'}->path; + rename $upload, $upload.'.stl'; + $upload .= '.stl'; + my $print = Slic3r::Print->new; + $print->add_object_from_file($upload); + if (0) { #$opt{merge}) { + $print->add_object_from_file($_) for splice @ARGV, 0; + } + $print->duplicate; + $print->arrange_objects if @{$print->objects} > 1; + $print->validate; + my ($fh, $filename) = tempfile(); + $print->export_gcode(output_file => $filename, + status_cb => sub { + my ($percent, $message) = @_; + printf "=> $message\n"; + }); + unlink $filename; + return [ 200, ['Content-Type' => 'text/plain'], $fh ]; + } else { + return [ 200, ['Content-Type' => 'text/html'], [$form] ]; + } +}; + +__DATA__ + + + Slic3r + + +

Slic3r

+
+

Select an stl file: + +

+ +
+ + +