Slic3r.pm 9.8 KB

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