Slic3r.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. # This package loads all the non-GUI Slic3r perl packages.
  2. # In addition, it implements utility functions for file handling and threading.
  3. package Slic3r;
  4. # Copyright holder: Alessandro Ranellucci
  5. # This application is licensed under the GNU Affero General Public License, version 3
  6. use strict;
  7. use warnings;
  8. require v5.10;
  9. our $VERSION = VERSION();
  10. our $GITVERSION = GITVERSION();
  11. our $debug = 0;
  12. sub debugf {
  13. printf @_ if $debug;
  14. }
  15. # load threads before Moo as required by it
  16. our $have_threads;
  17. BEGIN {
  18. # Test, whether the perl was compiled with ithreads support and ithreads actually work.
  19. use Config;
  20. $have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
  21. warn "threads.pm >= 1.96 is required, please update\n" if $have_threads && $threads::VERSION < 1.96;
  22. ### temporarily disable threads if using the broken Moo version
  23. use Moo;
  24. $have_threads = 0 if $Moo::VERSION == 1.003000;
  25. # Disable multi threading completely by an environment value.
  26. # This is useful for debugging as the Perl debugger does not work
  27. # in multi-threaded context at all.
  28. # A good interactive perl debugger is the ActiveState Komodo IDE
  29. # or the EPIC http://www.epic-ide.org/
  30. $have_threads = 0 if (defined($ENV{'SLIC3R_SINGLETHREADED'}) && $ENV{'SLIC3R_SINGLETHREADED'} == 1)
  31. }
  32. warn "Running Slic3r under Perl 5.16 is neither supported nor recommended\n"
  33. if $^V == v5.16;
  34. use FindBin;
  35. # Path to the images.
  36. my $varpath = decode_path($FindBin::Bin) . "/var";
  37. if ($^O eq 'darwin' && !-d $varpath) {
  38. $varpath = decode_path($FindBin::Bin) . "/../Resources/var";
  39. }
  40. our $var = sub { "$varpath/$_[0]" };
  41. use Moo 1.003001;
  42. use Slic3r::XS; # import all symbols (constants etc.) before they get parsed
  43. use Slic3r::Config;
  44. use Slic3r::ExPolygon;
  45. use Slic3r::ExtrusionLoop;
  46. use Slic3r::ExtrusionPath;
  47. use Slic3r::Flow;
  48. use Slic3r::GCode::ArcFitting;
  49. use Slic3r::GCode::MotionPlanner;
  50. use Slic3r::GCode::PressureRegulator;
  51. use Slic3r::GCode::Reader;
  52. use Slic3r::GCode::VibrationLimit;
  53. use Slic3r::Geometry qw(PI);
  54. use Slic3r::Geometry::Clipper;
  55. use Slic3r::Layer;
  56. use Slic3r::Line;
  57. use Slic3r::Model;
  58. use Slic3r::Point;
  59. use Slic3r::Polygon;
  60. use Slic3r::Polyline;
  61. use Slic3r::Print;
  62. use Slic3r::Print::GCode;
  63. use Slic3r::Print::Object;
  64. use Slic3r::Print::Simple;
  65. use Slic3r::Print::SupportMaterial;
  66. use Slic3r::Surface;
  67. our $build = eval "use Slic3r::Build; 1";
  68. use Thread::Semaphore;
  69. use Encode::Locale 1.05;
  70. use Encode;
  71. use Unicode::Normalize;
  72. # Scaling between the float and integer coordinates.
  73. # Floats are in mm.
  74. use constant SCALING_FACTOR => 0.000001;
  75. # Resolution to simplify perimeters to. These constants are now used in C++ code only. Better to publish them to Perl from the C++ code.
  76. # use constant RESOLUTION => 0.0125;
  77. # use constant SCALED_RESOLUTION => RESOLUTION / SCALING_FACTOR;
  78. use constant LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER => 0.15;
  79. # use constant INFILL_OVERLAP_OVER_SPACING => 0.3;
  80. # Keep track of threads we created. Each thread keeps its own list of threads it spwaned.
  81. my @my_threads = ();
  82. my @threads : shared = ();
  83. my $pause_sema = Thread::Semaphore->new;
  84. my $parallel_sema;
  85. my $paused = 0;
  86. sub spawn_thread {
  87. my ($cb) = @_;
  88. my $parent_tid = threads->tid;
  89. lock @threads;
  90. # Set up a default handler for preventing crashes in case signals are received before
  91. # thread sets its handlers.
  92. $SIG{'STOP'} = sub {};
  93. @_ = ();
  94. my $thread = threads->create(sub {
  95. @my_threads = ();
  96. local $SIG{'KILL'} = sub {
  97. Slic3r::debugf "Exiting thread %d...\n", threads->tid;
  98. $parallel_sema->up if $parallel_sema;
  99. kill_all_threads();
  100. Slic3r::thread_cleanup();
  101. threads->exit();
  102. };
  103. local $SIG{'STOP'} = sub {
  104. $pause_sema->down;
  105. $pause_sema->up;
  106. };
  107. Slic3r::debugf "Starting thread %d (parent: %d)...\n", threads->tid, $parent_tid;
  108. $cb->();
  109. });
  110. push @my_threads, $thread->tid;
  111. push @threads, $thread->tid;
  112. return $thread;
  113. }
  114. # If the threading is enabled, spawn a set of threads.
  115. # Otherwise run the task on the current thread.
  116. # Used for
  117. # Slic3r::Print::Object->layers->make_perimeters
  118. # Slic3r::Print::Object->layers->make_fill
  119. # Slic3r::Print::SupportMaterial::generate_toolpaths
  120. sub parallelize {
  121. my %params = @_;
  122. lock @threads;
  123. if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
  124. my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
  125. my $q = Thread::Queue->new;
  126. $q->enqueue(@items, (map undef, 1..$params{threads}));
  127. $parallel_sema = Thread::Semaphore->new(-$params{threads});
  128. $parallel_sema->up;
  129. my $thread_cb = sub {
  130. # execute thread callback
  131. $params{thread_cb}->($q);
  132. # signal the parent thread that we're done
  133. $parallel_sema->up;
  134. # cleanup before terminating thread
  135. Slic3r::thread_cleanup();
  136. # This explicit exit avoids an untrappable
  137. # "Attempt to free unreferenced scalar" error
  138. # triggered on Ubuntu 12.04 32-bit when we're running
  139. # from the Wx plater and
  140. # we're reusing the same plater object more than once.
  141. # The downside to using this exit is that we can't return
  142. # any value to the main thread but we're not doing that
  143. # anymore anyway.
  144. threads->exit;
  145. };
  146. @_ = ();
  147. my @my_threads = map spawn_thread($thread_cb), 1..$params{threads};
  148. # We use a semaphore instead of $th->join because joined threads are
  149. # not listed by threads->list or threads->object anymore, thus can't
  150. # be signalled.
  151. $parallel_sema->down;
  152. $_->detach for @my_threads;
  153. } else {
  154. $params{no_threads_cb}->();
  155. }
  156. }
  157. # call this at the very end of each thread (except the main one)
  158. # so that it does not try to free existing objects.
  159. # at that stage, existing objects are only those that we
  160. # inherited at the thread creation (thus shared) and those
  161. # that we are returning: destruction will be handled by the
  162. # main thread in both cases.
  163. # reminder: do not destroy inherited objects in other threads,
  164. # as the main thread will still try to destroy them when they
  165. # go out of scope; in other words, if you're undef()'ing an
  166. # object in a thread, make sure the main thread still holds a
  167. # reference so that it won't be destroyed in thread.
  168. sub thread_cleanup {
  169. return if !$Slic3r::have_threads;
  170. if (threads->tid == 0) {
  171. warn "Calling thread_cleanup() from main thread\n";
  172. return;
  173. }
  174. # prevent destruction of shared objects
  175. no warnings 'redefine';
  176. *Slic3r::BridgeDetector::DESTROY = sub {};
  177. *Slic3r::Config::DESTROY = sub {};
  178. *Slic3r::Config::Full::DESTROY = sub {};
  179. *Slic3r::Config::GCode::DESTROY = sub {};
  180. *Slic3r::Config::Print::DESTROY = sub {};
  181. *Slic3r::Config::PrintObject::DESTROY = sub {};
  182. *Slic3r::Config::PrintRegion::DESTROY = sub {};
  183. *Slic3r::Config::Static::DESTROY = sub {};
  184. *Slic3r::ExPolygon::DESTROY = sub {};
  185. *Slic3r::ExPolygon::Collection::DESTROY = sub {};
  186. *Slic3r::Extruder::DESTROY = sub {};
  187. *Slic3r::ExtrusionLoop::DESTROY = sub {};
  188. *Slic3r::ExtrusionPath::DESTROY = sub {};
  189. *Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
  190. *Slic3r::Filler::DESTROY = sub {};
  191. *Slic3r::Flow::DESTROY = sub {};
  192. *Slic3r::GCode::DESTROY = sub {};
  193. *Slic3r::GCode::AvoidCrossingPerimeters::DESTROY = sub {};
  194. *Slic3r::GCode::OozePrevention::DESTROY = sub {};
  195. *Slic3r::GCode::PlaceholderParser::DESTROY = sub {};
  196. *Slic3r::GCode::Sender::DESTROY = sub {};
  197. *Slic3r::GCode::Wipe::DESTROY = sub {};
  198. *Slic3r::GCode::Writer::DESTROY = sub {};
  199. *Slic3r::Geometry::BoundingBox::DESTROY = sub {};
  200. *Slic3r::Geometry::BoundingBoxf::DESTROY = sub {};
  201. *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
  202. *Slic3r::Layer::PerimeterGenerator::DESTROY = sub {};
  203. *Slic3r::LayerHeightSpline::DESTROY = sub {};
  204. *Slic3r::Line::DESTROY = sub {};
  205. *Slic3r::Linef3::DESTROY = sub {};
  206. *Slic3r::Model::DESTROY = sub {};
  207. *Slic3r::Model::Object::DESTROY = sub {};
  208. *Slic3r::Point::DESTROY = sub {};
  209. *Slic3r::Pointf::DESTROY = sub {};
  210. *Slic3r::Pointf3::DESTROY = sub {};
  211. *Slic3r::Polygon::DESTROY = sub {};
  212. *Slic3r::Polyline::DESTROY = sub {};
  213. *Slic3r::Polyline::Collection::DESTROY = sub {};
  214. *Slic3r::Print::DESTROY = sub {};
  215. *Slic3r::Print::Object::DESTROY = sub {};
  216. *Slic3r::Print::Region::DESTROY = sub {};
  217. *Slic3r::SLAPrint::DESTROY = sub {};
  218. *Slic3r::Surface::DESTROY = sub {};
  219. *Slic3r::Surface::Collection::DESTROY = sub {};
  220. *Slic3r::TriangleMesh::DESTROY = sub {};
  221. return undef; # this prevents a "Scalars leaked" warning
  222. }
  223. sub get_running_threads {
  224. return grep defined($_), map threads->object($_), @_;
  225. }
  226. sub kill_all_threads {
  227. # if we're the main thread, we send SIGKILL to all the running threads
  228. if (threads->tid == 0) {
  229. lock @threads;
  230. foreach my $thread (get_running_threads(@threads)) {
  231. Slic3r::debugf "Thread %d killing %d...\n", threads->tid, $thread->tid;
  232. $thread->kill('KILL');
  233. }
  234. # unlock semaphore before we block on wait
  235. # otherwise we'd get a deadlock if threads were paused
  236. resume_all_threads();
  237. }
  238. # in any thread we wait for our children
  239. foreach my $thread (get_running_threads(@my_threads)) {
  240. Slic3r::debugf " Thread %d waiting for %d...\n", threads->tid, $thread->tid;
  241. $thread->join; # block until threads are killed
  242. Slic3r::debugf " Thread %d finished waiting for %d...\n", threads->tid, $thread->tid;
  243. }
  244. @my_threads = ();
  245. }
  246. sub pause_all_threads {
  247. return if $paused;
  248. lock @threads;
  249. $paused = 1;
  250. $pause_sema->down;
  251. $_->kill('STOP') for get_running_threads(@threads);
  252. }
  253. sub resume_all_threads {
  254. return unless $paused;
  255. lock @threads;
  256. $paused = 0;
  257. $pause_sema->up;
  258. }
  259. # Convert a Unicode path to a file system locale.
  260. # The encoding is (from Encode::Locale POD):
  261. # Alias | Windows | Mac OS X | POSIX
  262. # locale_fs | ANSI | UTF-8 | nl_langinfo
  263. # where nl_langinfo is en-US.UTF-8 on a modern Linux as well.
  264. # So this conversion seems to make the most sense on Windows.
  265. sub encode_path {
  266. my ($path) = @_;
  267. return undef if !defined $path;
  268. $path = Unicode::Normalize::NFC($path);
  269. $path = Encode::encode(locale_fs => $path);
  270. return $path;
  271. }
  272. # Convert a path coded by a file system locale to Unicode.
  273. sub decode_path {
  274. my ($path) = @_;
  275. return undef if !defined $path;
  276. $path = Encode::decode(locale_fs => $path)
  277. unless utf8::is_utf8($path);
  278. # The filesystem might force a normalization form (like HFS+ does) so
  279. # if we rely on the filename being comparable after the open() + readdir()
  280. # roundtrip (like when creating and then selecting a preset), we need to
  281. # restore our normalization form.
  282. $path = Unicode::Normalize::NFC($path);
  283. return $path;
  284. }
  285. # Open a file by converting $filename to local file system locales.
  286. sub open {
  287. my ($fh, $mode, $filename) = @_;
  288. return CORE::open $$fh, $mode, encode_path($filename);
  289. }
  290. # this package declaration prevents an ugly fatal warning to be emitted when
  291. # spawning a new thread
  292. package GLUquadricObjPtr;
  293. 1;