Slic3r.pm 7.7 KB

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