Slic3r.pm 11 KB

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