Slic3r.pm 10 KB

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