2011-09-01 21:06:28 +02:00
|
|
|
package Slic3r;
|
|
|
|
|
2012-01-18 20:04:18 +01:00
|
|
|
# Copyright holder: Alessandro Ranellucci
|
|
|
|
# This application is licensed under the GNU Affero General Public License, version 3
|
|
|
|
|
2011-09-01 21:06:28 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2012-01-22 13:56:15 +01:00
|
|
|
require v5.10;
|
2011-09-01 21:06:28 +02:00
|
|
|
|
2014-08-04 12:43:18 +02:00
|
|
|
our $VERSION = "1.2.0";
|
2011-11-13 22:57:58 +01:00
|
|
|
|
2011-09-05 13:33:09 +02:00
|
|
|
our $debug = 0;
|
|
|
|
sub debugf {
|
|
|
|
printf @_ if $debug;
|
|
|
|
}
|
|
|
|
|
2012-05-19 20:25:59 +02:00
|
|
|
# load threads before Moo as required by it
|
2012-05-21 19:16:42 +02:00
|
|
|
our $have_threads;
|
2012-05-21 18:44:31 +02:00
|
|
|
BEGIN {
|
|
|
|
use Config;
|
2013-02-27 11:08:08 +01:00
|
|
|
$have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
|
2013-07-24 10:06:02 +02:00
|
|
|
|
|
|
|
### temporarily disable threads if using the broken Moo version
|
|
|
|
use Moo;
|
|
|
|
$have_threads = 0 if $Moo::VERSION == 1.003000;
|
2012-05-21 18:44:31 +02:00
|
|
|
}
|
2012-05-19 20:25:59 +02:00
|
|
|
|
2012-09-11 16:02:26 +02:00
|
|
|
warn "Running Slic3r under Perl >= 5.16 is not supported nor recommended\n"
|
|
|
|
if $^V >= v5.16;
|
|
|
|
|
2012-05-29 14:19:14 +02:00
|
|
|
use FindBin;
|
|
|
|
our $var = "$FindBin::Bin/var";
|
|
|
|
|
2013-01-13 10:18:34 +01:00
|
|
|
use Encode;
|
|
|
|
use Encode::Locale;
|
2013-09-12 11:09:03 +02:00
|
|
|
use Moo 1.003001;
|
2013-01-13 10:18:34 +01:00
|
|
|
|
2013-07-14 13:05:55 +02:00
|
|
|
use Slic3r::XS; # import all symbols (constants etc.) before they get parsed
|
2011-10-03 11:55:32 +02:00
|
|
|
use Slic3r::Config;
|
2011-10-15 11:36:05 +02:00
|
|
|
use Slic3r::ExPolygon;
|
2011-09-26 11:42:08 +02:00
|
|
|
use Slic3r::Extruder;
|
2011-09-25 23:15:45 +02:00
|
|
|
use Slic3r::ExtrusionLoop;
|
2011-09-04 12:04:01 +02:00
|
|
|
use Slic3r::ExtrusionPath;
|
2011-09-26 10:52:58 +02:00
|
|
|
use Slic3r::ExtrusionPath::Collection;
|
2011-09-05 12:21:27 +02:00
|
|
|
use Slic3r::Fill;
|
2012-06-06 18:05:03 +02:00
|
|
|
use Slic3r::Flow;
|
2012-02-25 17:35:25 +01:00
|
|
|
use Slic3r::Format::AMF;
|
2012-05-20 11:40:37 +02:00
|
|
|
use Slic3r::Format::OBJ;
|
2012-02-25 17:35:25 +01:00
|
|
|
use Slic3r::Format::STL;
|
2012-06-28 14:44:54 +02:00
|
|
|
use Slic3r::GCode;
|
2013-08-28 19:50:16 +02:00
|
|
|
use Slic3r::GCode::ArcFitting;
|
2013-04-04 01:17:44 +02:00
|
|
|
use Slic3r::GCode::CoolingBuffer;
|
2013-05-18 16:57:44 +02:00
|
|
|
use Slic3r::GCode::Layer;
|
2012-08-23 15:42:58 +02:00
|
|
|
use Slic3r::GCode::MotionPlanner;
|
2014-03-17 00:39:07 +01:00
|
|
|
use Slic3r::GCode::PlaceholderParser;
|
2013-05-13 20:14:33 +02:00
|
|
|
use Slic3r::GCode::Reader;
|
2013-05-13 20:15:45 +02:00
|
|
|
use Slic3r::GCode::SpiralVase;
|
2013-08-28 16:51:58 +02:00
|
|
|
use Slic3r::GCode::VibrationLimit;
|
2011-12-07 19:33:59 +01:00
|
|
|
use Slic3r::Geometry qw(PI);
|
2013-06-07 23:16:02 +02:00
|
|
|
use Slic3r::Geometry::Clipper;
|
2011-09-01 21:06:28 +02:00
|
|
|
use Slic3r::Layer;
|
2014-04-07 23:18:11 +02:00
|
|
|
use Slic3r::Layer::BridgeDetector;
|
2012-09-23 02:52:31 +02:00
|
|
|
use Slic3r::Layer::Region;
|
2011-09-01 21:06:28 +02:00
|
|
|
use Slic3r::Line;
|
2012-08-29 16:49:38 +02:00
|
|
|
use Slic3r::Model;
|
2011-09-01 21:06:28 +02:00
|
|
|
use Slic3r::Point;
|
2011-10-15 11:36:05 +02:00
|
|
|
use Slic3r::Polygon;
|
2011-09-01 21:06:28 +02:00
|
|
|
use Slic3r::Polyline;
|
|
|
|
use Slic3r::Print;
|
2012-04-29 12:51:20 +02:00
|
|
|
use Slic3r::Print::Object;
|
2014-01-02 17:54:18 +01:00
|
|
|
use Slic3r::Print::Simple;
|
2013-09-17 23:27:57 +02:00
|
|
|
use Slic3r::Print::SupportMaterial;
|
2011-09-01 21:06:28 +02:00
|
|
|
use Slic3r::Surface;
|
2011-11-27 11:40:03 +01:00
|
|
|
use Slic3r::TriangleMesh;
|
2013-04-27 20:55:43 +02:00
|
|
|
our $build = eval "use Slic3r::Build; 1";
|
2014-07-12 11:41:18 +02:00
|
|
|
use Thread::Semaphore;
|
2011-09-01 21:06:28 +02:00
|
|
|
|
2012-07-27 21:13:03 +02:00
|
|
|
use constant SCALING_FACTOR => 0.000001;
|
2012-11-18 17:38:08 +01:00
|
|
|
use constant RESOLUTION => 0.0125;
|
|
|
|
use constant SCALED_RESOLUTION => RESOLUTION / SCALING_FACTOR;
|
2012-07-27 21:13:03 +02:00
|
|
|
use constant SMALL_PERIMETER_LENGTH => (6.5 / SCALING_FACTOR) * 2 * PI;
|
2014-01-03 20:02:00 +01:00
|
|
|
use constant LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER => 0.15;
|
2013-03-17 00:57:58 +01:00
|
|
|
use constant INFILL_OVERLAP_OVER_SPACING => 0.45;
|
2013-08-13 09:45:33 +02:00
|
|
|
use constant EXTERNAL_INFILL_MARGIN => 3;
|
2014-04-15 01:41:40 +02:00
|
|
|
use constant INSET_OVERLAP_TOLERANCE => 0.2;
|
2012-07-27 21:13:03 +02:00
|
|
|
|
2014-06-13 19:23:51 +02:00
|
|
|
# keep track of threads we created
|
2014-07-01 19:00:23 +02:00
|
|
|
my @threads : shared = ();
|
2014-07-12 11:41:18 +02:00
|
|
|
my $sema = Thread::Semaphore->new;
|
|
|
|
my $paused = 0;
|
2014-07-01 19:00:23 +02:00
|
|
|
|
|
|
|
sub spawn_thread {
|
|
|
|
my ($cb) = @_;
|
|
|
|
|
2014-07-12 11:41:18 +02:00
|
|
|
@_ = ();
|
|
|
|
my $thread = threads->create(sub {
|
|
|
|
local $SIG{'KILL'} = sub {
|
|
|
|
Slic3r::debugf "Exiting thread...\n";
|
|
|
|
Slic3r::thread_cleanup();
|
|
|
|
threads->exit();
|
|
|
|
};
|
|
|
|
local $SIG{'STOP'} = sub {
|
|
|
|
$sema->down;
|
|
|
|
$sema->up;
|
|
|
|
};
|
|
|
|
$cb->();
|
|
|
|
});
|
2014-07-01 19:00:23 +02:00
|
|
|
push @threads, $thread->tid;
|
|
|
|
return $thread;
|
|
|
|
}
|
2014-06-13 19:23:51 +02:00
|
|
|
|
2012-04-09 12:29:47 +02:00
|
|
|
sub parallelize {
|
|
|
|
my %params = @_;
|
|
|
|
|
2013-12-24 11:29:31 +01:00
|
|
|
if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
|
2012-09-22 19:04:36 +02:00
|
|
|
my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
|
2012-04-09 12:29:47 +02:00
|
|
|
my $q = Thread::Queue->new;
|
2013-12-24 11:29:31 +01:00
|
|
|
$q->enqueue(@items, (map undef, 1..$params{threads}));
|
2012-04-09 12:29:47 +02:00
|
|
|
|
2013-07-07 18:43:04 +02:00
|
|
|
my $thread_cb = sub {
|
2014-06-13 19:23:51 +02:00
|
|
|
# execute thread callback
|
2013-11-12 12:09:33 +01:00
|
|
|
$params{thread_cb}->($q);
|
2014-06-13 19:23:51 +02:00
|
|
|
|
|
|
|
# cleanup before terminating thread
|
2013-07-11 16:17:36 +02:00
|
|
|
Slic3r::thread_cleanup();
|
2013-11-12 12:09:33 +01:00
|
|
|
|
|
|
|
# This explicit exit avoids an untrappable
|
|
|
|
# "Attempt to free unreferenced scalar" error
|
|
|
|
# triggered on Ubuntu 12.04 32-bit when we're running
|
|
|
|
# from the Wx plater and
|
|
|
|
# we're reusing the same plater object more than once.
|
|
|
|
# The downside to using this exit is that we can't return
|
|
|
|
# any value to the main thread but we're not doing that
|
|
|
|
# anymore anyway.
|
|
|
|
# collect_cb is completely useless now
|
|
|
|
# and should be removed from the codebase.
|
|
|
|
threads->exit;
|
2013-07-07 18:43:04 +02:00
|
|
|
};
|
2013-09-17 23:14:49 +02:00
|
|
|
$params{collect_cb} ||= sub {};
|
2013-07-07 18:43:04 +02:00
|
|
|
|
2013-05-19 17:34:33 +02:00
|
|
|
@_ = ();
|
2014-07-01 19:00:23 +02:00
|
|
|
my @my_threads = map spawn_thread($thread_cb), 1..$params{threads};
|
2014-06-13 19:23:51 +02:00
|
|
|
foreach my $th (@my_threads) {
|
2012-04-09 12:29:47 +02:00
|
|
|
$params{collect_cb}->($th->join);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$params{no_threads_cb}->();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:17:36 +02:00
|
|
|
# call this at the very end of each thread (except the main one)
|
|
|
|
# so that it does not try to free existing objects.
|
|
|
|
# at that stage, existing objects are only those that we
|
|
|
|
# inherited at the thread creation (thus shared) and those
|
|
|
|
# that we are returning: destruction will be handled by the
|
|
|
|
# main thread in both cases.
|
2013-09-17 23:14:49 +02:00
|
|
|
# reminder: do not destroy inherited objects in other threads,
|
|
|
|
# as the main thread will still try to destroy them when they
|
|
|
|
# go out of scope; in other words, if you're undef()'ing an
|
|
|
|
# object in a thread, make sure the main thread still holds a
|
|
|
|
# reference so that it won't be destroyed in thread.
|
2013-07-11 16:17:36 +02:00
|
|
|
sub thread_cleanup {
|
2014-06-13 19:23:51 +02:00
|
|
|
return if !$Slic3r::have_threads;
|
|
|
|
|
2013-07-11 16:17:36 +02:00
|
|
|
# prevent destruction of shared objects
|
|
|
|
no warnings 'redefine';
|
2013-12-22 00:39:03 +01:00
|
|
|
*Slic3r::Config::DESTROY = sub {};
|
2014-01-02 10:44:54 +01:00
|
|
|
*Slic3r::Config::Full::DESTROY = sub {};
|
2013-12-22 00:39:03 +01:00
|
|
|
*Slic3r::Config::Print::DESTROY = sub {};
|
2014-01-02 10:44:54 +01:00
|
|
|
*Slic3r::Config::PrintObject::DESTROY = sub {};
|
|
|
|
*Slic3r::Config::PrintRegion::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::ExPolygon::DESTROY = sub {};
|
2013-07-14 00:38:01 +02:00
|
|
|
*Slic3r::ExPolygon::Collection::DESTROY = sub {};
|
2014-04-08 02:42:29 +03:00
|
|
|
*Slic3r::Extruder::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::ExtrusionLoop::DESTROY = sub {};
|
|
|
|
*Slic3r::ExtrusionPath::DESTROY = sub {};
|
2013-07-18 22:29:12 +02:00
|
|
|
*Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
|
2014-01-14 20:11:08 +01:00
|
|
|
*Slic3r::Flow::DESTROY = sub {};
|
2014-05-06 11:07:18 +03:00
|
|
|
*Slic3r::GCode::PlaceholderParser::DESTROY = sub {};
|
2014-01-14 20:11:08 +01:00
|
|
|
*Slic3r::Geometry::BoundingBox::DESTROY = sub {};
|
|
|
|
*Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::Line::DESTROY = sub {};
|
2014-04-30 02:04:49 +03:00
|
|
|
*Slic3r::Model::DESTROY = sub {};
|
|
|
|
*Slic3r::Model::Object::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::Point::DESTROY = sub {};
|
2014-04-28 22:02:34 +02:00
|
|
|
*Slic3r::Pointf::DESTROY = sub {};
|
2014-01-14 20:11:08 +01:00
|
|
|
*Slic3r::Pointf3::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::Polygon::DESTROY = sub {};
|
|
|
|
*Slic3r::Polyline::DESTROY = sub {};
|
2013-09-12 11:09:03 +02:00
|
|
|
*Slic3r::Polyline::Collection::DESTROY = sub {};
|
2014-05-06 11:07:18 +03:00
|
|
|
*Slic3r::Print::DESTROY = sub {};
|
|
|
|
*Slic3r::Print::Region::DESTROY = sub {};
|
2013-07-16 09:49:34 +02:00
|
|
|
*Slic3r::Surface::DESTROY = sub {};
|
|
|
|
*Slic3r::Surface::Collection::DESTROY = sub {};
|
2013-09-12 11:09:03 +02:00
|
|
|
*Slic3r::TriangleMesh::DESTROY = sub {};
|
2013-09-19 16:00:47 +02:00
|
|
|
return undef; # this prevents a "Scalars leaked" warning
|
2013-07-11 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-12 11:41:18 +02:00
|
|
|
sub get_running_threads {
|
|
|
|
return grep defined($_), map threads->object($_), @threads;
|
|
|
|
}
|
|
|
|
|
2014-07-01 19:00:23 +02:00
|
|
|
sub kill_all_threads {
|
|
|
|
# detach any running thread created in the current one
|
2014-07-04 12:12:15 +02:00
|
|
|
my @killed = ();
|
2014-07-12 11:41:18 +02:00
|
|
|
foreach my $thread (get_running_threads()) {
|
2014-07-01 19:00:23 +02:00
|
|
|
$thread->kill('KILL');
|
2014-07-04 12:12:15 +02:00
|
|
|
push @killed, $thread;
|
2014-07-01 19:00:23 +02:00
|
|
|
}
|
2014-07-12 11:41:18 +02:00
|
|
|
|
|
|
|
# unlock semaphore before we block on wait
|
|
|
|
# otherwise we'd get a deadlock if threads were paused
|
|
|
|
resume_threads();
|
2014-07-04 12:12:15 +02:00
|
|
|
$_->join for @killed; # block until threads are killed
|
2014-07-01 19:00:23 +02:00
|
|
|
@threads = ();
|
|
|
|
}
|
|
|
|
|
2014-07-12 11:41:18 +02:00
|
|
|
sub pause_threads {
|
|
|
|
return if $paused;
|
|
|
|
$paused = 1;
|
|
|
|
$sema->down;
|
|
|
|
$_->kill('STOP') for get_running_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub resume_threads {
|
|
|
|
return unless $paused;
|
|
|
|
$paused = 0;
|
|
|
|
$sema->up;
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:40:00 +01:00
|
|
|
sub encode_path {
|
|
|
|
my ($filename) = @_;
|
|
|
|
return encode('locale_fs', $filename);
|
|
|
|
}
|
|
|
|
|
2013-01-13 10:18:34 +01:00
|
|
|
sub open {
|
|
|
|
my ($fh, $mode, $filename) = @_;
|
2013-02-22 18:40:00 +01:00
|
|
|
return CORE::open $$fh, $mode, encode_path($filename);
|
2013-01-13 10:18:34 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 12:18:30 +01:00
|
|
|
# this package declaration prevents an ugly fatal warning to be emitted when
|
|
|
|
# spawning a new thread
|
|
|
|
package GLUquadricObjPtr;
|
|
|
|
|
2011-09-01 21:06:28 +02:00
|
|
|
1;
|