Slic3r.pm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. use Config;
  9. require v5.10;
  10. our $VERSION = VERSION();
  11. our $BUILD = BUILD();
  12. our $FORK_NAME = FORK_NAME();
  13. our $debug = 0;
  14. sub debugf {
  15. printf @_ if $debug;
  16. }
  17. our $loglevel = 0;
  18. # load threads before Moo as required by it
  19. our $have_threads;
  20. BEGIN {
  21. # Test, whether the perl was compiled with ithreads support and ithreads actually work.
  22. use Config;
  23. $have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
  24. warn "threads.pm >= 1.96 is required, please update\n" if $have_threads && $threads::VERSION < 1.96;
  25. ### temporarily disable threads if using the broken Moo version
  26. use Moo;
  27. $have_threads = 0 if $Moo::VERSION == 1.003000;
  28. # Disable multi threading completely by an environment value.
  29. # This is useful for debugging as the Perl debugger does not work
  30. # in multi-threaded context at all.
  31. # A good interactive perl debugger is the ActiveState Komodo IDE
  32. # or the EPIC http://www.epic-ide.org/
  33. $have_threads = 0 if (defined($ENV{'SLIC3R_SINGLETHREADED'}) && $ENV{'SLIC3R_SINGLETHREADED'} == 1);
  34. print "Threading disabled\n" if !$have_threads;
  35. $debug = 1 if (defined($ENV{'SLIC3R_DEBUGOUT'}) && $ENV{'SLIC3R_DEBUGOUT'} == 1);
  36. print "Debugging output enabled\n" if $debug;
  37. }
  38. warn "Running Slic3r under Perl 5.16 is neither supported nor recommended\n"
  39. if $^V == v5.16;
  40. use FindBin;
  41. # Path to the images.
  42. our $var = sub { decode_path($FindBin::Bin) . "/var/" . $_[0] };
  43. use Moo 1.003001;
  44. use Slic3r::XS; # import all symbols (constants etc.) before they get parsed
  45. use Slic3r::Config;
  46. use Slic3r::ExPolygon;
  47. use Slic3r::ExtrusionLoop;
  48. use Slic3r::ExtrusionPath;
  49. use Slic3r::Flow;
  50. use Slic3r::GCode::Reader;
  51. use Slic3r::Geometry::Clipper;
  52. use Slic3r::Layer;
  53. use Slic3r::Line;
  54. use Slic3r::Model;
  55. use Slic3r::Point;
  56. use Slic3r::Polygon;
  57. use Slic3r::Polyline;
  58. use Slic3r::Print;
  59. use Slic3r::Print::Object;
  60. use Slic3r::Print::Simple;
  61. use Slic3r::Surface;
  62. our $build = eval "use Slic3r::Build; 1";
  63. use Thread::Semaphore;
  64. # Scaling between the float and integer coordinates.
  65. # Floats are in mm.
  66. use constant SCALING_FACTOR => 0.000001;
  67. # Keep track of threads we created. Perl worker threads shall not create further threads.
  68. my @threads = ();
  69. my $pause_sema = Thread::Semaphore->new;
  70. my $paused = 0;
  71. # Set the logging level at the Slic3r XS module.
  72. $Slic3r::loglevel = (defined($ENV{'SLIC3R_LOGLEVEL'}) && $ENV{'SLIC3R_LOGLEVEL'} =~ /^[1-9]/) ? $ENV{'SLIC3R_LOGLEVEL'} : 0;
  73. set_logging_level($Slic3r::loglevel);
  74. sub spawn_thread {
  75. my ($cb) = @_;
  76. @_ = ();
  77. my $thread = threads->create(sub {
  78. Slic3r::debugf "Starting thread %d...\n", threads->tid;
  79. local $SIG{'KILL'} = sub {
  80. Slic3r::debugf "Exiting thread %d...\n", threads->tid;
  81. Slic3r::thread_cleanup();
  82. threads->exit();
  83. };
  84. local $SIG{'STOP'} = sub {
  85. $pause_sema->down;
  86. $pause_sema->up;
  87. };
  88. $cb->();
  89. });
  90. push @threads, $thread->tid;
  91. return $thread;
  92. }
  93. # call this at the very end of each thread (except the main one)
  94. # so that it does not try to free existing objects.
  95. # at that stage, existing objects are only those that we
  96. # inherited at the thread creation (thus shared) and those
  97. # that we are returning: destruction will be handled by the
  98. # main thread in both cases.
  99. # reminder: do not destroy inherited objects in other threads,
  100. # as the main thread will still try to destroy them when they
  101. # go out of scope; in other words, if you're undef()'ing an
  102. # object in a thread, make sure the main thread still holds a
  103. # reference so that it won't be destroyed in thread.
  104. sub thread_cleanup {
  105. # prevent destruction of shared objects
  106. no warnings 'redefine';
  107. *Slic3r::BridgeDetector::DESTROY = sub {};
  108. *Slic3r::Config::DESTROY = sub {};
  109. *Slic3r::Config::Full::DESTROY = sub {};
  110. *Slic3r::Config::GCode::DESTROY = sub {};
  111. *Slic3r::Config::Print::DESTROY = sub {};
  112. *Slic3r::Config::PrintObject::DESTROY = sub {};
  113. *Slic3r::Config::PrintRegion::DESTROY = sub {};
  114. *Slic3r::Config::Static::DESTROY = sub {};
  115. *Slic3r::ExPolygon::DESTROY = sub {};
  116. *Slic3r::ExPolygon::Collection::DESTROY = sub {};
  117. *Slic3r::ExtrusionLoop::DESTROY = sub {};
  118. *Slic3r::ExtrusionMultiPath::DESTROY = sub {};
  119. *Slic3r::ExtrusionPath::DESTROY = sub {};
  120. *Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
  121. *Slic3r::ExtrusionSimulator::DESTROY = sub {};
  122. *Slic3r::Flow::DESTROY = sub {};
  123. *Slic3r::GCode::DESTROY = sub {};
  124. *Slic3r::GCode::PlaceholderParser::DESTROY = sub {};
  125. *Slic3r::GCode::Sender::DESTROY = sub {};
  126. *Slic3r::Geometry::BoundingBox::DESTROY = sub {};
  127. *Slic3r::Geometry::BoundingBoxf::DESTROY = sub {};
  128. *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
  129. *Slic3r::Layer::PerimeterGenerator::DESTROY = sub {};
  130. *Slic3r::Line::DESTROY = sub {};
  131. *Slic3r::Linef3::DESTROY = sub {};
  132. *Slic3r::Model::DESTROY = sub {};
  133. *Slic3r::Model::Object::DESTROY = sub {};
  134. *Slic3r::Point::DESTROY = sub {};
  135. *Slic3r::Pointf::DESTROY = sub {};
  136. *Slic3r::Pointf3::DESTROY = sub {};
  137. *Slic3r::Polygon::DESTROY = sub {};
  138. *Slic3r::Polyline::DESTROY = sub {};
  139. *Slic3r::Polyline::Collection::DESTROY = sub {};
  140. *Slic3r::Print::DESTROY = sub {};
  141. *Slic3r::Print::Object::DESTROY = sub {};
  142. *Slic3r::Print::Region::DESTROY = sub {};
  143. *Slic3r::Surface::DESTROY = sub {};
  144. *Slic3r::Surface::Collection::DESTROY = sub {};
  145. *Slic3r::Print::SupportMaterial2::DESTROY = sub {};
  146. *Slic3r::TriangleMesh::DESTROY = sub {};
  147. return undef; # this prevents a "Scalars leaked" warning
  148. }
  149. sub _get_running_threads {
  150. return grep defined($_), map threads->object($_), @threads;
  151. }
  152. sub kill_all_threads {
  153. # Send SIGKILL to all the running threads to let them die.
  154. foreach my $thread (_get_running_threads) {
  155. Slic3r::debugf "Thread %d killing %d...\n", threads->tid, $thread->tid;
  156. $thread->kill('KILL');
  157. }
  158. # unlock semaphore before we block on wait
  159. # otherwise we'd get a deadlock if threads were paused
  160. resume_all_threads();
  161. # in any thread we wait for our children
  162. foreach my $thread (_get_running_threads) {
  163. Slic3r::debugf " Thread %d waiting for %d...\n", threads->tid, $thread->tid;
  164. $thread->join; # block until threads are killed
  165. Slic3r::debugf " Thread %d finished waiting for %d...\n", threads->tid, $thread->tid;
  166. }
  167. @threads = ();
  168. }
  169. sub pause_all_threads {
  170. return if $paused;
  171. $paused = 1;
  172. $pause_sema->down;
  173. $_->kill('STOP') for _get_running_threads;
  174. }
  175. sub resume_all_threads {
  176. return unless $paused;
  177. $paused = 0;
  178. $pause_sema->up;
  179. }
  180. # Open a file by converting $filename to local file system locales.
  181. sub open {
  182. my ($fh, $mode, $filename) = @_;
  183. return CORE::open $$fh, $mode, encode_path($filename);
  184. }
  185. sub tags {
  186. my ($format) = @_;
  187. $format //= '';
  188. my %tags;
  189. # End of line
  190. $tags{eol} = ($format eq 'html') ? '<br>' : "\n";
  191. # Heading
  192. $tags{h2start} = ($format eq 'html') ? '<b>' : '';
  193. $tags{h2end} = ($format eq 'html') ? '</b>' : '';
  194. # Bold font
  195. $tags{bstart} = ($format eq 'html') ? '<b>' : '';
  196. $tags{bend} = ($format eq 'html') ? '</b>' : '';
  197. # Verbatim
  198. $tags{vstart} = ($format eq 'html') ? '<pre>' : '';
  199. $tags{vend} = ($format eq 'html') ? '</pre>' : '';
  200. return %tags;
  201. }
  202. sub slic3r_info
  203. {
  204. my (%params) = @_;
  205. my %tag = Slic3r::tags($params{format});
  206. my $out = '';
  207. $out .= "$tag{bstart}$Slic3r::FORK_NAME$tag{bend}$tag{eol}";
  208. $out .= "$tag{bstart}Version: $tag{bend}$Slic3r::VERSION$tag{eol}";
  209. $out .= "$tag{bstart}Build: $tag{bend}$Slic3r::BUILD$tag{eol}";
  210. return $out;
  211. }
  212. sub copyright_info
  213. {
  214. my (%params) = @_;
  215. my %tag = Slic3r::tags($params{format});
  216. my $out =
  217. 'Copyright &copy; 2016 Vojtech Bubnik, Prusa Research. <br />' .
  218. 'Copyright &copy; 2011-2016 Alessandro Ranellucci. <br />' .
  219. '<a href="http://slic3r.org/">Slic3r</a> is licensed under the ' .
  220. '<a href="http://www.gnu.org/licenses/agpl-3.0.html">GNU Affero General Public License, version 3</a>.' .
  221. '<br /><br /><br />' .
  222. 'Contributions by Henrik Brix Andersen, Nicolas Dandrimont, Mark Hindess, Petr Ledvina, Y. Sapir, Mike Sheldrake and numerous others. ' .
  223. 'Manual by Gary Hodgson. Inspired by the RepRap community. <br />' .
  224. 'Slic3r logo designed by Corey Daniels, <a href="http://www.famfamfam.com/lab/icons/silk/">Silk Icon Set</a> designed by Mark James. ';
  225. return $out;
  226. }
  227. sub system_info
  228. {
  229. my (%params) = @_;
  230. my %tag = Slic3r::tags($params{format});
  231. my $out = '';
  232. $out .= "$tag{bstart}Operating System: $tag{bend}$Config{osname}$tag{eol}";
  233. $out .= "$tag{bstart}System Architecture: $tag{bend}$Config{archname}$tag{eol}";
  234. if ($^O eq 'MSWin32') {
  235. $out .= "$tag{bstart}Windows Version: $tag{bend}" . `ver` . $tag{eol};
  236. } else {
  237. # Hopefully some kind of unix / linux.
  238. $out .= "$tag{bstart}System Version: $tag{bend}" . `uname -a` . $tag{eol};
  239. }
  240. $out .= $tag{vstart} . Config::myconfig . $tag{vend};
  241. $out .= " $tag{bstart}\@INC:$tag{bend}$tag{eol}$tag{vstart}";
  242. foreach my $i (@INC) {
  243. $out .= " $i\n";
  244. }
  245. $out .= "$tag{vend}";
  246. return $out;
  247. }
  248. # this package declaration prevents an ugly fatal warning to be emitted when
  249. # spawning a new thread
  250. package GLUquadricObjPtr;
  251. 1;