Slic3r.pm 9.7 KB

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