Slic3r.pm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package Slic3r;
  2. # Copyright holder: Alessandro Ranellucci
  3. # This application is licensed under the GNU Affero General Public License, version 3
  4. use strict;
  5. use warnings;
  6. require v5.10;
  7. our $VERSION = "1.0.1-dev";
  8. our $debug = 0;
  9. sub debugf {
  10. printf @_ if $debug;
  11. }
  12. # load threads before Moo as required by it
  13. our $have_threads;
  14. BEGIN {
  15. use Config;
  16. $have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
  17. ### temporarily disable threads if using the broken Moo version
  18. use Moo;
  19. $have_threads = 0 if $Moo::VERSION == 1.003000;
  20. }
  21. warn "Running Slic3r under Perl >= 5.16 is not supported nor recommended\n"
  22. if $^V >= v5.16;
  23. use FindBin;
  24. our $var = "$FindBin::Bin/var";
  25. use Encode;
  26. use Encode::Locale;
  27. use Moo 1.003001;
  28. use Slic3r::XS; # import all symbols (constants etc.) before they get parsed
  29. use Slic3r::Config;
  30. use Slic3r::ExPolygon;
  31. use Slic3r::Extruder;
  32. use Slic3r::ExtrusionLoop;
  33. use Slic3r::ExtrusionPath;
  34. use Slic3r::ExtrusionPath::Collection;
  35. use Slic3r::Fill;
  36. use Slic3r::Flow;
  37. use Slic3r::Format::AMF;
  38. use Slic3r::Format::OBJ;
  39. use Slic3r::Format::STL;
  40. use Slic3r::GCode;
  41. use Slic3r::GCode::ArcFitting;
  42. use Slic3r::GCode::CoolingBuffer;
  43. use Slic3r::GCode::Layer;
  44. use Slic3r::GCode::MotionPlanner;
  45. use Slic3r::GCode::Reader;
  46. use Slic3r::GCode::SpiralVase;
  47. use Slic3r::GCode::VibrationLimit;
  48. use Slic3r::Geometry qw(PI);
  49. use Slic3r::Geometry::BoundingBox;
  50. use Slic3r::Geometry::Clipper;
  51. use Slic3r::Layer;
  52. use Slic3r::Layer::Region;
  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::Region;
  61. use Slic3r::Print::Simple;
  62. use Slic3r::Print::SupportMaterial;
  63. use Slic3r::Surface;
  64. use Slic3r::TriangleMesh;
  65. our $build = eval "use Slic3r::Build; 1";
  66. use constant SCALING_FACTOR => 0.000001;
  67. use constant RESOLUTION => 0.0125;
  68. use constant SCALED_RESOLUTION => RESOLUTION / SCALING_FACTOR;
  69. use constant SMALL_PERIMETER_LENGTH => (6.5 / SCALING_FACTOR) * 2 * PI;
  70. use constant LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER => 0.15;
  71. use constant INFILL_OVERLAP_OVER_SPACING => 0.45;
  72. use constant EXTERNAL_INFILL_MARGIN => 3;
  73. sub parallelize {
  74. my %params = @_;
  75. if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
  76. my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
  77. my $q = Thread::Queue->new;
  78. $q->enqueue(@items, (map undef, 1..$params{threads}));
  79. my $thread_cb = sub {
  80. $params{thread_cb}->($q);
  81. Slic3r::thread_cleanup();
  82. # This explicit exit avoids an untrappable
  83. # "Attempt to free unreferenced scalar" error
  84. # triggered on Ubuntu 12.04 32-bit when we're running
  85. # from the Wx plater and
  86. # we're reusing the same plater object more than once.
  87. # The downside to using this exit is that we can't return
  88. # any value to the main thread but we're not doing that
  89. # anymore anyway.
  90. # collect_cb is completely useless now
  91. # and should be removed from the codebase.
  92. threads->exit;
  93. };
  94. $params{collect_cb} ||= sub {};
  95. @_ = ();
  96. foreach my $th (map threads->create($thread_cb), 1..$params{threads}) {
  97. $params{collect_cb}->($th->join);
  98. }
  99. } else {
  100. $params{no_threads_cb}->();
  101. }
  102. }
  103. # call this at the very end of each thread (except the main one)
  104. # so that it does not try to free existing objects.
  105. # at that stage, existing objects are only those that we
  106. # inherited at the thread creation (thus shared) and those
  107. # that we are returning: destruction will be handled by the
  108. # main thread in both cases.
  109. # reminder: do not destroy inherited objects in other threads,
  110. # as the main thread will still try to destroy them when they
  111. # go out of scope; in other words, if you're undef()'ing an
  112. # object in a thread, make sure the main thread still holds a
  113. # reference so that it won't be destroyed in thread.
  114. sub thread_cleanup {
  115. # prevent destruction of shared objects
  116. no warnings 'redefine';
  117. *Slic3r::Config::DESTROY = sub {};
  118. *Slic3r::Config::Full::DESTROY = sub {};
  119. *Slic3r::Config::Print::DESTROY = sub {};
  120. *Slic3r::Config::PrintObject::DESTROY = sub {};
  121. *Slic3r::Config::PrintRegion::DESTROY = sub {};
  122. *Slic3r::ExPolygon::DESTROY = sub {};
  123. *Slic3r::ExPolygon::Collection::DESTROY = sub {};
  124. *Slic3r::ExtrusionLoop::DESTROY = sub {};
  125. *Slic3r::ExtrusionPath::DESTROY = sub {};
  126. *Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
  127. *Slic3r::Line::DESTROY = sub {};
  128. *Slic3r::Point::DESTROY = sub {};
  129. *Slic3r::Polygon::DESTROY = sub {};
  130. *Slic3r::Polyline::DESTROY = sub {};
  131. *Slic3r::Polyline::Collection::DESTROY = sub {};
  132. *Slic3r::Print::State::DESTROY = sub {};
  133. *Slic3r::Surface::DESTROY = sub {};
  134. *Slic3r::Surface::Collection::DESTROY = sub {};
  135. *Slic3r::TriangleMesh::DESTROY = sub {};
  136. return undef; # this prevents a "Scalars leaked" warning
  137. }
  138. sub encode_path {
  139. my ($filename) = @_;
  140. return encode('locale_fs', $filename);
  141. }
  142. sub open {
  143. my ($fh, $mode, $filename) = @_;
  144. return CORE::open $$fh, $mode, encode_path($filename);
  145. }
  146. # this package declaration prevents an ugly fatal warning to be emitted when
  147. # spawning a new thread
  148. package GLUquadricObjPtr;
  149. 1;