~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to lib/Slic3r.pm

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Slic3r;
 
2
 
 
3
# Copyright holder: Alessandro Ranellucci
 
4
# This application is licensed under the GNU Affero General Public License, version 3
 
5
 
 
6
use strict;
 
7
use warnings;
 
8
require v5.10;
 
9
 
 
10
our $VERSION = "1.1.4";
 
11
 
 
12
our $debug = 0;
 
13
sub debugf {
 
14
    printf @_ if $debug;
 
15
}
 
16
 
 
17
# load threads before Moo as required by it
 
18
our $have_threads;
 
19
BEGIN {
 
20
    use Config;
 
21
    $have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
 
22
    
 
23
    ### temporarily disable threads if using the broken Moo version
 
24
    use Moo;
 
25
    $have_threads = 0 if $Moo::VERSION == 1.003000;
 
26
}
 
27
 
 
28
warn "Running Slic3r under Perl >= 5.16 is not supported nor recommended\n"
 
29
    if $^V >= v5.16;
 
30
 
 
31
use FindBin;
 
32
our $var = "$FindBin::Bin/var";
 
33
 
 
34
use Encode;
 
35
use Encode::Locale;
 
36
use Moo 1.003001;
 
37
 
 
38
use Slic3r::XS;   # import all symbols (constants etc.) before they get parsed
 
39
use Slic3r::Config;
 
40
use Slic3r::ExPolygon;
 
41
use Slic3r::Extruder;
 
42
use Slic3r::ExtrusionLoop;
 
43
use Slic3r::ExtrusionPath;
 
44
use Slic3r::ExtrusionPath::Collection;
 
45
use Slic3r::Fill;
 
46
use Slic3r::Flow;
 
47
use Slic3r::Format::AMF;
 
48
use Slic3r::Format::OBJ;
 
49
use Slic3r::Format::STL;
 
50
use Slic3r::GCode;
 
51
use Slic3r::GCode::ArcFitting;
 
52
use Slic3r::GCode::CoolingBuffer;
 
53
use Slic3r::GCode::Layer;
 
54
use Slic3r::GCode::MotionPlanner;
 
55
use Slic3r::GCode::PlaceholderParser;
 
56
use Slic3r::GCode::Reader;
 
57
use Slic3r::GCode::SpiralVase;
 
58
use Slic3r::GCode::VibrationLimit;
 
59
use Slic3r::Geometry qw(PI);
 
60
use Slic3r::Geometry::Clipper;
 
61
use Slic3r::Layer;
 
62
use Slic3r::Layer::BridgeDetector;
 
63
use Slic3r::Layer::Region;
 
64
use Slic3r::Line;
 
65
use Slic3r::Model;
 
66
use Slic3r::Point;
 
67
use Slic3r::Polygon;
 
68
use Slic3r::Polyline;
 
69
use Slic3r::Print;
 
70
use Slic3r::Print::Object;
 
71
use Slic3r::Print::Region;
 
72
use Slic3r::Print::Simple;
 
73
use Slic3r::Print::SupportMaterial;
 
74
use Slic3r::Surface;
 
75
use Slic3r::TriangleMesh;
 
76
our $build = eval "use Slic3r::Build; 1";
 
77
 
 
78
use constant SCALING_FACTOR         => 0.000001;
 
79
use constant RESOLUTION             => 0.0125;
 
80
use constant SCALED_RESOLUTION      => RESOLUTION / SCALING_FACTOR;
 
81
use constant SMALL_PERIMETER_LENGTH => (6.5 / SCALING_FACTOR) * 2 * PI;
 
82
use constant LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER => 0.15;
 
83
use constant INFILL_OVERLAP_OVER_SPACING  => 0.45;
 
84
use constant EXTERNAL_INFILL_MARGIN => 3;
 
85
use constant INSET_OVERLAP_TOLERANCE => 0.2;
 
86
 
 
87
sub parallelize {
 
88
    my %params = @_;
 
89
    
 
90
    if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
 
91
        my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
 
92
        my $q = Thread::Queue->new;
 
93
        $q->enqueue(@items, (map undef, 1..$params{threads}));
 
94
        
 
95
        my $thread_cb = sub {
 
96
            $params{thread_cb}->($q);
 
97
            Slic3r::thread_cleanup();
 
98
            
 
99
            # This explicit exit avoids an untrappable 
 
100
            # "Attempt to free unreferenced scalar" error
 
101
            # triggered on Ubuntu 12.04 32-bit when we're running 
 
102
            # from the Wx plater and
 
103
            # we're reusing the same plater object more than once.
 
104
            # The downside to using this exit is that we can't return
 
105
            # any value to the main thread but we're not doing that
 
106
            # anymore anyway.
 
107
            # collect_cb is completely useless now
 
108
            # and should be removed from the codebase.
 
109
            threads->exit;
 
110
        };
 
111
        $params{collect_cb} ||= sub {};
 
112
            
 
113
        @_ = ();
 
114
        foreach my $th (map threads->create($thread_cb), 1..$params{threads}) {
 
115
            $params{collect_cb}->($th->join);
 
116
        }
 
117
    } else {
 
118
        $params{no_threads_cb}->();
 
119
    }
 
120
}
 
121
 
 
122
# call this at the very end of each thread (except the main one)
 
123
# so that it does not try to free existing objects.
 
124
# at that stage, existing objects are only those that we 
 
125
# inherited at the thread creation (thus shared) and those 
 
126
# that we are returning: destruction will be handled by the
 
127
# main thread in both cases.
 
128
# reminder: do not destroy inherited objects in other threads,
 
129
# as the main thread will still try to destroy them when they
 
130
# go out of scope; in other words, if you're undef()'ing an 
 
131
# object in a thread, make sure the main thread still holds a
 
132
# reference so that it won't be destroyed in thread.
 
133
sub thread_cleanup {
 
134
    # prevent destruction of shared objects
 
135
    no warnings 'redefine';
 
136
    *Slic3r::Config::DESTROY                = sub {};
 
137
    *Slic3r::Config::Full::DESTROY          = sub {};
 
138
    *Slic3r::Config::Print::DESTROY         = sub {};
 
139
    *Slic3r::Config::PrintObject::DESTROY   = sub {};
 
140
    *Slic3r::Config::PrintRegion::DESTROY   = sub {};
 
141
    *Slic3r::ExPolygon::DESTROY             = sub {};
 
142
    *Slic3r::ExPolygon::Collection::DESTROY = sub {};
 
143
    *Slic3r::Extruder::DESTROY              = sub {};
 
144
    *Slic3r::ExtrusionLoop::DESTROY         = sub {};
 
145
    *Slic3r::ExtrusionPath::DESTROY         = sub {};
 
146
    *Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
 
147
    *Slic3r::Flow::DESTROY                  = sub {};
 
148
    *Slic3r::Geometry::BoundingBox::DESTROY = sub {};
 
149
    *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
 
150
    *Slic3r::Line::DESTROY                  = sub {};
 
151
    *Slic3r::Model::DESTROY                 = sub {};
 
152
    *Slic3r::Model::Object::DESTROY         = sub {};
 
153
    *Slic3r::Point::DESTROY                 = sub {};
 
154
    *Slic3r::Pointf::DESTROY                = sub {};
 
155
    *Slic3r::Pointf3::DESTROY               = sub {};
 
156
    *Slic3r::Polygon::DESTROY               = sub {};
 
157
    *Slic3r::Polyline::DESTROY              = sub {};
 
158
    *Slic3r::Polyline::Collection::DESTROY  = sub {};
 
159
    *Slic3r::Print::State::DESTROY          = sub {};
 
160
    *Slic3r::Surface::DESTROY               = sub {};
 
161
    *Slic3r::Surface::Collection::DESTROY   = sub {};
 
162
    *Slic3r::TriangleMesh::DESTROY          = sub {};
 
163
    return undef;  # this prevents a "Scalars leaked" warning
 
164
}
 
165
 
 
166
sub encode_path {
 
167
    my ($filename) = @_;
 
168
    return encode('locale_fs', $filename);
 
169
}
 
170
 
 
171
sub open {
 
172
    my ($fh, $mode, $filename) = @_;
 
173
    return CORE::open $$fh, $mode, encode_path($filename);
 
174
}
 
175
 
 
176
# this package declaration prevents an ugly fatal warning to be emitted when
 
177
# spawning a new thread
 
178
package GLUquadricObjPtr;
 
179
 
 
180
1;