Merge branch 'master' into adaptive-slicing

Conflicts:
	lib/Slic3r/GUI/Tab.pm
	lib/Slic3r/Print/Object.pm
	lib/Slic3r/Test.pm
	xs/src/libslic3r/PrintConfig.cpp
	xs/src/libslic3r/PrintConfig.hpp
	xs/src/libslic3r/PrintObject.cpp
This commit is contained in:
Florens Wasserfall 2016-06-06 12:07:34 +02:00
commit 8485e4bea2
1917 changed files with 24218 additions and 320967 deletions

22
.github/ISSUE_TEMPLATE.md vendored Normal file
View File

@ -0,0 +1,22 @@
### Version
_Version of Slic3r used goes here_
_Use `About->About Slic3r` for release versions_
_For -dev versions, use `git describe --tag` or get the hash value for the version you downloaded or `git rev-parse HEAD`_
### Operating system type + version
_What OS are you using, and state any version #s_
### Behavior
* _Describe the problem_
* _Steps needed to reproduce the problem_
* _If this is a command-line slicing issue, include the options used_
* _Expected Results_
* _Actual Results_
* _Screenshots from __*Slic3r*__ preview are preferred_
_Is this a new feature request?_
#### STL/Config (.ZIP) where problem occurs
_Upload a zipped copy of an STL and your config (`File -> Export Config`)_

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ xs/buildtmp
*.o
MANIFEST.bak
xs/MANIFEST.bak
xs/assertlib*
.init_bundle.ini

View File

@ -2,11 +2,20 @@ language: perl
install: true
script: perl ./Build.PL
perl:
- "5.12"
- "5.14"
- "5.18"
- "5.20"
branches:
only:
- master
- stable
sudo: false
cache:
- apt
addons:
apt:
sources:
- boost-latest
packages:
- libboost-thread1.55-dev
- libboost-system1.55-dev

View File

@ -7,7 +7,9 @@ use Config;
use File::Spec;
my %prereqs = qw(
Encode::Locale 0
Devel::CheckLib 0
Encode 0
Encode::Locale 1.05
ExtUtils::MakeMaker 6.80
ExtUtils::ParseXS 3.22
File::Basename 0
@ -18,15 +20,17 @@ my %prereqs = qw(
Moo 1.003001
POSIX 0
Scalar::Util 0
Test::Harness 0
Test::More 0
Thread::Semaphore 0
IO::Scalar 0
threads 1.96
Time::HiRes 0
Unicode::Normalize 0
);
my %recommends = qw(
Class::XSAccessor 0
XML::SAX::ExpatXS 0
Test::Harness 0
);
my $sudo = grep { $_ eq '--sudo' } @ARGV;
@ -34,13 +38,23 @@ my $gui = grep { $_ eq '--gui' } @ARGV;
my $xs_only = grep { $_ eq '--xs' } @ARGV;
if ($gui) {
%prereqs = qw(
Class::Accessor 0
Wx 0.9918
Socket 2.016
);
%recommends = qw(
Growl::GNTP 0.15
Wx::GLCanvas 0
OpenGL 0
LWP::UserAgent 0
Net::Bonjour 0
);
if ($^O eq 'MSWin32') {
$recommends{"Win32::TieRegistry"} = 0;
# we need an up-to-date Win32::API because older aren't thread-safe (GH #2517)
$prereqs{'Win32::API'} = 0.79;
}
} elsif ($xs_only) {
%prereqs = %recommends = ();
}
@ -106,7 +120,13 @@ EOF
my %modules = (%prereqs, %recommends);
foreach my $module (sort keys %modules) {
my $version = $modules{$module};
my @cmd = ($cpanm, @cpanm_args, "$module~$version");
my @cmd = ($cpanm, @cpanm_args);
# temporary workaround for upstream bug in test
push @cmd, '--notest'
if $module =~ /^(?:OpenGL|Math::PlanePath|Test::Harness)$/;
push @cmd, "$module~$version";
if ($module eq 'XML::SAX::ExpatXS' && $^O eq 'MSWin32') {
my $mingw = 'C:\dev\CitrusPerl\mingw64';
$mingw = 'C:\dev\CitrusPerl\mingw32' if !-d $mingw;

View File

@ -8,7 +8,7 @@ Slic3r [![Build Status](https://travis-ci.org/alexrj/Slic3r.png?branch=master)](
Slic3r takes 3D models (STL, OBJ, AMF) and converts them into G-code instructions for
3D printers. It's compatible with any modern printer based on the RepRap toolchain,
including all those based on the Marlin, Sprinter and Repetier firmware. It also works
with Mach3 and LinuxCNC controllers.
with Mach3, LinuxCNC and Machinekit controllers.
See the [project homepage](http://slic3r.org/) at slic3r.org and the
[manual](http://manual.slic3r.org/) for more information.
@ -30,7 +30,7 @@ Key features are:
* **multi-platform** (Linux/Mac/Win) and packaged as standalone-app with no dependencies required
* complete **command-line interface** to use it with no GUI
* multi-material **(multiple extruders)** object printing
* multiple G-code flavors supported (RepRap, Makerbot, Mach3 etc.)
* multiple G-code flavors supported (RepRap, Makerbot, Mach3, Machinekit etc.)
* ability to plate **multiple objects having distinct print settings**
* **multithread** processing
* **STL auto-repair** (tolerance for broken models)
@ -61,8 +61,8 @@ If you want to compile the source yourself just do the following (checkout
```
$ git clone https://github.com/alexrj/Slic3r.git
$ cd Slic3r
$ sudo perl Build.PL
$ sudo perl Build.PL --gui
$ perl Build.PL --sudo
$ perl Build.PL --sudo --gui
$ ./slic3r.pl
```
@ -109,6 +109,8 @@ The author of the Silk icon set is Mark James.
-j, --threads <num> Number of threads to use (1+, default: 2)
GUI options:
--gui Forces the GUI launch instead of command line slicing (if you
supply a model file, it will be loaded into the plater)
--no-plater Disable the plater tab
--gui-mode Overrides the configured mode (simple/expert)
--autosave <file> Automatically export current configuration to the specified file
@ -130,17 +132,18 @@ The author of the Silk icon set is Mark James.
(default: 100,100)
--z-offset Additional height in mm to add to vertical coordinates
(+/-, default: 0)
--gcode-flavor The type of G-code to generate (reprap/teacup/makerware/sailfish/mach3/no-extrusion,
--gcode-flavor The type of G-code to generate (reprap/teacup/makerware/sailfish/mach3/machinekit/no-extrusion,
default: reprap)
--use-relative-e-distances Enable this to get relative E values (default: no)
--use-firmware-retraction Enable firmware-controlled retraction using G10/G11 (default: no)
--use-volumetric-e Express E in cubic millimeters and prepend M200 (default: no)
--gcode-arcs Use G2/G3 commands for native arcs (experimental, not supported
by all firmwares)
--g0 Use G0 commands for retraction (experimental, not supported by all
firmwares)
--gcode-comments Make G-code verbose by adding comments (default: no)
--vibration-limit Limit the frequency of moves on X and Y axes (Hz, set zero to disable;
default: 0)
--pressure-advance Adjust pressure using the experimental advance algorithm (K constant,
set zero to disable; default: 0)
Filament options:
--filament-diameter Diameter in mm of your raw filament (default: 3)
@ -194,7 +197,7 @@ The author of the Silk icon set is Mark James.
to disable; default: 0)
--default-acceleration
Acceleration will be reset to this value after the specific settings above
have been applied. (mm/s^2, set zero to disable; default: 130)
have been applied. (mm/s^2, set zero to disable; default: 0)
Accuracy options:
--layer-height Layer height in mm (default: 0.3)
@ -218,7 +221,8 @@ The author of the Silk icon set is Mark James.
--end-gcode Load final G-code from the supplied file. This will overwrite
the default commands (turn off temperature [M104 S0],
home X axis [G28 X], disable motors [M84]).
--layer-gcode Load layer-change G-code from the supplied file (default: nothing).
--before-layer-gcode Load before-layer-change G-code from the supplied file (default: nothing).
--layer-gcode Load after-layer-change G-code from the supplied file (default: nothing).
--toolchange-gcode Load tool-change G-code from the supplied file (default: nothing).
--seam-position Position of loop starting points (random/nearest/aligned, default: aligned).
--external-perimeters-first Reverse perimeter order. (default: no)
@ -252,6 +256,9 @@ The author of the Silk icon set is Mark James.
Spacing between pattern lines (mm, default: 2.5)
--support-material-angle
Support material angle in degrees (range: 0-90, default: 0)
--support-material-contact-distance
Vertical distance between object and support material
(0+, default: 0.2)
--support-material-interface-layers
Number of perpendicular layers between support material and object (0+, default: 3)
--support-material-interface-spacing
@ -272,14 +279,16 @@ The author of the Silk icon set is Mark James.
--retract-before-travel
Only retract before travel moves of this length in mm (default: 2)
--retract-lift Lift Z by the given distance in mm when retracting (default: 0)
--retract-lift-above Only lift Z when above the specified height (default: 0)
--retract-lift-below Only lift Z when below the specified height (default: 0)
--retract-layer-change
Enforce a retraction before each Z move (default: yes)
Enforce a retraction before each Z move (default: no)
--wipe Wipe the nozzle while doing a retraction (default: no)
Retraction options for multi-extruder setups:
--retract-length-toolchange
Length of retraction in mm when disabling tool (default: 1)
--retract-restart-extra-toolchnage
Length of retraction in mm when disabling tool (default: 10)
--retract-restart-extra-toolchange
Additional amount of filament in mm to push after
switching tool (default: 0)
@ -345,16 +354,18 @@ The author of the Silk icon set is Mark James.
Set a different extrusion width for top infill
--support-material-extrusion-width
Set a different extrusion width for support material
--infill-overlap Overlap between infill and perimeters (default: 15%)
--bridge-flow-ratio Multiplier for extrusion when bridging (> 0, default: 1)
Multiple extruder options:
--extruder-offset Offset of each extruder, if firmware doesn't handle the displacement
(can be specified multiple times, default: 0x0)
--perimeter-extruder
Extruder to use for perimeters (1+, default: 1)
Extruder to use for perimeters and brim (1+, default: 1)
--infill-extruder Extruder to use for infill (1+, default: 1)
--solid-infill-extruder Extruder to use for solid infill (1+, default: 1)
--support-material-extruder
Extruder to use for support material (1+, default: 1)
Extruder to use for support material, raft and skirt (1+, default: 1)
--support-material-interface-extruder
Extruder to use for support material interface (1+, default: 1)
--ooze-prevention Drop temperature and park extruders outside a full skirt for automatic wiping

View File

@ -7,7 +7,7 @@ use strict;
use warnings;
require v5.10;
our $VERSION = "1.2.0";
our $VERSION = VERSION();
our $debug = 0;
sub debugf {
@ -19,92 +19,97 @@ our $have_threads;
BEGIN {
use Config;
$have_threads = $Config{useithreads} && eval "use threads; use threads::shared; use Thread::Queue; 1";
warn "threads.pm >= 1.96 is required, please update\n" if $have_threads && $threads::VERSION < 1.96;
### temporarily disable threads if using the broken Moo version
use Moo;
$have_threads = 0 if $Moo::VERSION == 1.003000;
}
warn "Running Slic3r under Perl >= 5.16 is not supported nor recommended\n"
if $^V >= v5.16;
warn "Running Slic3r under Perl 5.16 is not supported nor recommended\n"
if $^V == v5.16;
use FindBin;
our $var = "$FindBin::Bin/var";
our $var = sub { decode_path($FindBin::Bin) . "/var/" . $_[0] };
use Encode;
use Encode::Locale;
use Moo 1.003001;
use Slic3r::XS; # import all symbols (constants etc.) before they get parsed
use Slic3r::AdaptiveSlicing;
use Slic3r::Config;
use Slic3r::ExPolygon;
use Slic3r::Extruder;
use Slic3r::ExtrusionLoop;
use Slic3r::ExtrusionPath;
use Slic3r::ExtrusionPath::Collection;
use Slic3r::Fill;
use Slic3r::Flow;
use Slic3r::Format::AMF;
use Slic3r::Format::OBJ;
use Slic3r::Format::STL;
use Slic3r::GCode;
use Slic3r::GCode::ArcFitting;
use Slic3r::GCode::CoolingBuffer;
use Slic3r::GCode::Layer;
use Slic3r::GCode::MotionPlanner;
use Slic3r::GCode::PlaceholderParser;
use Slic3r::GCode::PressureRegulator;
use Slic3r::GCode::Reader;
use Slic3r::GCode::SpiralVase;
use Slic3r::GCode::VibrationLimit;
use Slic3r::Geometry qw(PI);
use Slic3r::Geometry::Clipper;
use Slic3r::Layer;
use Slic3r::Layer::BridgeDetector;
use Slic3r::Layer::Region;
use Slic3r::Line;
use Slic3r::Model;
use Slic3r::Point;
use Slic3r::Polygon;
use Slic3r::Polyline;
use Slic3r::Print;
use Slic3r::Print::GCode;
use Slic3r::Print::Object;
use Slic3r::Print::Simple;
use Slic3r::Print::SupportMaterial;
use Slic3r::Surface;
our $build = eval "use Slic3r::Build; 1";
use Thread::Semaphore;
use Encode::Locale 1.05;
use Encode;
use Unicode::Normalize;
use constant SCALING_FACTOR => 0.000001;
use constant RESOLUTION => 0.0125;
use constant SCALED_RESOLUTION => RESOLUTION / SCALING_FACTOR;
use constant SMALL_PERIMETER_LENGTH => (6.5 / SCALING_FACTOR) * 2 * PI;
use constant LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER => 0.15;
use constant INFILL_OVERLAP_OVER_SPACING => 0.45;
use constant EXTERNAL_INFILL_MARGIN => 3;
use constant INSET_OVERLAP_TOLERANCE => 0.2;
use constant INFILL_OVERLAP_OVER_SPACING => 0.3;
# keep track of threads we created
my @my_threads = ();
my @threads : shared = ();
my $sema = Thread::Semaphore->new;
my $pause_sema = Thread::Semaphore->new;
my $parallel_sema;
my $paused = 0;
sub spawn_thread {
my ($cb) = @_;
my $parent_tid = threads->tid;
lock @threads;
@_ = ();
my $thread = threads->create(sub {
@my_threads = ();
Slic3r::debugf "Starting thread %d (parent: %d)...\n", threads->tid, $parent_tid;
local $SIG{'KILL'} = sub {
Slic3r::debugf "Exiting thread...\n";
Slic3r::debugf "Exiting thread %d...\n", threads->tid;
$parallel_sema->up if $parallel_sema;
kill_all_threads();
Slic3r::thread_cleanup();
threads->exit();
};
local $SIG{'STOP'} = sub {
$sema->down;
$sema->up;
$pause_sema->down;
$pause_sema->up;
};
$cb->();
});
push @my_threads, $thread->tid;
push @threads, $thread->tid;
return $thread;
}
@ -112,15 +117,21 @@ sub spawn_thread {
sub parallelize {
my %params = @_;
lock @threads;
if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
my $q = Thread::Queue->new;
$q->enqueue(@items, (map undef, 1..$params{threads}));
$parallel_sema = Thread::Semaphore->new(-$params{threads});
$parallel_sema->up;
my $thread_cb = sub {
# execute thread callback
$params{thread_cb}->($q);
# signal the parent thread that we're done
$parallel_sema->up;
# cleanup before terminating thread
Slic3r::thread_cleanup();
@ -132,17 +143,17 @@ sub parallelize {
# The downside to using this exit is that we can't return
# any value to the main thread but we're not doing that
# anymore anyway.
# collect_cb is completely useless now
# and should be removed from the codebase.
threads->exit;
};
$params{collect_cb} ||= sub {};
@_ = ();
my @my_threads = map spawn_thread($thread_cb), 1..$params{threads};
foreach my $th (@my_threads) {
$params{collect_cb}->($th->join);
}
# We use a semaphore instead of $th->join because joined threads are
# not listed by threads->list or threads->object anymore, thus can't
# be signalled.
$parallel_sema->down;
$_->detach for @my_threads;
} else {
$params{no_threads_cb}->();
}
@ -162,13 +173,21 @@ sub parallelize {
sub thread_cleanup {
return if !$Slic3r::have_threads;
if (threads->tid == 0) {
warn "Calling thread_cleanup() from main thread\n";
return;
}
# prevent destruction of shared objects
no warnings 'redefine';
*Slic3r::BridgeDetector::DESTROY = sub {};
*Slic3r::Config::DESTROY = sub {};
*Slic3r::Config::Full::DESTROY = sub {};
*Slic3r::Config::GCode::DESTROY = sub {};
*Slic3r::Config::Print::DESTROY = sub {};
*Slic3r::Config::PrintObject::DESTROY = sub {};
*Slic3r::Config::PrintRegion::DESTROY = sub {};
*Slic3r::Config::Static::DESTROY = sub {};
*Slic3r::ExPolygon::DESTROY = sub {};
*Slic3r::ExPolygon::Collection::DESTROY = sub {};
*Slic3r::Extruder::DESTROY = sub {};
@ -176,10 +195,19 @@ sub thread_cleanup {
*Slic3r::ExtrusionPath::DESTROY = sub {};
*Slic3r::ExtrusionPath::Collection::DESTROY = sub {};
*Slic3r::Flow::DESTROY = sub {};
*Slic3r::GCode::DESTROY = sub {};
*Slic3r::GCode::AvoidCrossingPerimeters::DESTROY = sub {};
*Slic3r::GCode::OozePrevention::DESTROY = sub {};
*Slic3r::GCode::PlaceholderParser::DESTROY = sub {};
*Slic3r::GCode::Sender::DESTROY = sub {};
*Slic3r::GCode::Wipe::DESTROY = sub {};
*Slic3r::GCode::Writer::DESTROY = sub {};
*Slic3r::Geometry::BoundingBox::DESTROY = sub {};
*Slic3r::Geometry::BoundingBoxf::DESTROY = sub {};
*Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
*Slic3r::Layer::PerimeterGenerator::DESTROY = sub {};
*Slic3r::Line::DESTROY = sub {};
*Slic3r::Linef3::DESTROY = sub {};
*Slic3r::Model::DESTROY = sub {};
*Slic3r::Model::Object::DESTROY = sub {};
*Slic3r::Point::DESTROY = sub {};
@ -189,6 +217,7 @@ sub thread_cleanup {
*Slic3r::Polyline::DESTROY = sub {};
*Slic3r::Polyline::Collection::DESTROY = sub {};
*Slic3r::Print::DESTROY = sub {};
*Slic3r::Print::Object::DESTROY = sub {};
*Slic3r::Print::Region::DESTROY = sub {};
*Slic3r::Surface::DESTROY = sub {};
*Slic3r::Surface::Collection::DESTROY = sub {};
@ -197,40 +226,69 @@ sub thread_cleanup {
}
sub get_running_threads {
return grep defined($_), map threads->object($_), @threads;
return grep defined($_), map threads->object($_), @_;
}
sub kill_all_threads {
# detach any running thread created in the current one
my @killed = ();
foreach my $thread (get_running_threads()) {
$thread->kill('KILL');
push @killed, $thread;
# if we're the main thread, we send SIGKILL to all the running threads
if (threads->tid == 0) {
lock @threads;
foreach my $thread (get_running_threads(@threads)) {
Slic3r::debugf "Thread %d killing %d...\n", threads->tid, $thread->tid;
$thread->kill('KILL');
}
# unlock semaphore before we block on wait
# otherwise we'd get a deadlock if threads were paused
resume_all_threads();
}
# unlock semaphore before we block on wait
# otherwise we'd get a deadlock if threads were paused
resume_threads();
$_->join for @killed; # block until threads are killed
@threads = ();
# in any thread we wait for our children
foreach my $thread (get_running_threads(@my_threads)) {
Slic3r::debugf " Thread %d waiting for %d...\n", threads->tid, $thread->tid;
$thread->join; # block until threads are killed
Slic3r::debugf " Thread %d finished waiting for %d...\n", threads->tid, $thread->tid;
}
@my_threads = ();
}
sub pause_threads {
sub pause_all_threads {
return if $paused;
lock @threads;
$paused = 1;
$sema->down;
$_->kill('STOP') for get_running_threads();
$pause_sema->down;
$_->kill('STOP') for get_running_threads(@threads);
}
sub resume_threads {
sub resume_all_threads {
return unless $paused;
lock @threads;
$paused = 0;
$sema->up;
$pause_sema->up;
}
sub encode_path {
my ($filename) = @_;
return encode('locale_fs', $filename);
my ($path) = @_;
$path = Unicode::Normalize::NFC($path);
$path = Encode::encode(locale_fs => $path);
return $path;
}
sub decode_path {
my ($path) = @_;
$path = Encode::decode(locale_fs => $path)
unless utf8::is_utf8($path);
# The filesystem might force a normalization form (like HFS+ does) so
# if we rely on the filename being comparable after the open() + readdir()
# roundtrip (like when creating and then selecting a preset), we need to
# restore our normalization form.
$path = Unicode::Normalize::NFC($path);
return $path;
}
sub open {

View File

@ -9,7 +9,7 @@ use List::Util qw(first max);
our @Ignore = qw(duplicate_x duplicate_y multiply_x multiply_y support_material_tool acceleration
adjust_overhang_flow standby_temperature scale rotate duplicate duplicate_grid
rotate scale duplicate_grid start_perimeters_at_concave_points start_perimeters_at_non_overhang
randomize_start seal_position bed_size print_center);
randomize_start seal_position bed_size print_center g0);
our $Options = print_config_def();
@ -31,7 +31,8 @@ sub new_from_defaults {
my $self = $class->new;
my $defaults = Slic3r::Config::Full->new;
if (@opt_keys) {
$self->set($_, $defaults->get($_)) for @opt_keys;
$self->set($_, $defaults->get($_))
for grep $defaults->has($_), @opt_keys;
} else {
$self->apply_static($defaults);
}
@ -174,19 +175,6 @@ sub _handle_legacy {
return ($opt_key, $value);
}
sub set_ifndef {
my $self = shift;
my ($opt_key, $value, $deserialize) = @_;
if (!$self->has($opt_key)) {
if ($deserialize) {
$self->set_deserialize($opt_key, $value);
} else {
$self->set($opt_key, $value);
}
}
}
sub as_ini {
my ($self) = @_;
@ -205,31 +193,6 @@ sub save {
__PACKAGE__->write_ini($file, $self->as_ini);
}
sub setenv {
my $self = shift;
foreach my $opt_key (@{$self->get_keys}) {
$ENV{"SLIC3R_" . uc $opt_key} = $self->serialize($opt_key);
}
}
sub equals {
my ($self, $other) = @_;
return @{ $self->diff($other) } == 0;
}
# this will *ignore* options not present in both configs
sub diff {
my ($self, $other) = @_;
my @diff = ();
foreach my $opt_key (sort @{$self->get_keys}) {
push @diff, $opt_key
if $other->has($opt_key) && $other->serialize($opt_key) ne $self->serialize($opt_key);
}
return [@diff];
}
# this method is idempotent by design and only applies to ::DynamicConfig or ::Full
# objects because it performs cross checks
sub validate {
@ -248,6 +211,8 @@ sub validate {
# --first-layer-height
die "Invalid value for --first-layer-height\n"
if $self->first_layer_height !~ /^(?:\d*(?:\.\d+)?)%?$/;
die "Invalid value for --first-layer-height\n"
if $self->get_value('first_layer_height') <= 0;
# --filament-diameter
die "Invalid value for --filament-diameter\n"
@ -271,7 +236,7 @@ sub validate {
if !first { $_ eq $self->gcode_flavor } @{$Options->{gcode_flavor}{values}};
die "--use-firmware-retraction is only supported by Marlin firmware\n"
if $self->use_firmware_retraction && $self->gcode_flavor ne 'reprap';
if $self->use_firmware_retraction && $self->gcode_flavor ne 'reprap' && $self->gcode_flavor ne 'machinekit';
die "--use-firmware-retraction is not compatible with --wipe\n"
if $self->use_firmware_retraction && first {$_} @{$self->wipe};
@ -280,14 +245,14 @@ sub validate {
die "Invalid value for --fill-pattern\n"
if !first { $_ eq $self->fill_pattern } @{$Options->{fill_pattern}{values}};
# --solid-fill-pattern
die "Invalid value for --solid-fill-pattern\n"
if !first { $_ eq $self->solid_fill_pattern } @{$Options->{solid_fill_pattern}{values}};
# --external-fill-pattern
die "Invalid value for --external-fill-pattern\n"
if !first { $_ eq $self->external_fill_pattern } @{$Options->{external_fill_pattern}{values}};
# --fill-density
die "The selected fill pattern is not supposed to work at 100% density\n"
if $self->fill_density == 100
&& !first { $_ eq $self->fill_pattern } @{$Options->{solid_fill_pattern}{values}};
&& !first { $_ eq $self->fill_pattern } @{$Options->{external_fill_pattern}{values}};
# --infill-every-layers
die "Invalid value for --infill-every-layers\n"
@ -376,15 +341,6 @@ sub validate {
return 1;
}
# min object distance is max(duplicate_distance, clearance_radius)
sub min_object_distance {
my $self = shift;
return ($self->complete_objects && $self->extruder_clearance_radius > $self->duplicate_distance)
? $self->extruder_clearance_radius
: $self->duplicate_distance;
}
# CLASS METHODS:
sub write_ini {
@ -410,7 +366,8 @@ sub read_ini {
my ($file) = @_;
local $/ = "\n";
Slic3r::open(\my $fh, '<', $file);
Slic3r::open(\my $fh, '<', $file)
or die "Unable to open $file: $!\n";
binmode $fh, ':utf8';
my $ini = { _ => {} };
@ -424,7 +381,7 @@ sub read_ini {
$category = $1;
next;
}
/^(\w+) = (.*)/ or die "Unreadable configuration file (invalid data at line $.)\n";
/^(\w+) *= *(.*)/ or die "Unreadable configuration file (invalid data at line $.)\n";
$ini->{$category}{$1} = $2;
}
close $fh;
@ -432,16 +389,13 @@ sub read_ini {
return $ini;
}
package Slic3r::Config::Print;
package Slic3r::Config::Static;
use parent 'Slic3r::Config';
package Slic3r::Config::PrintObject;
use parent 'Slic3r::Config';
package Slic3r::Config::PrintRegion;
use parent 'Slic3r::Config';
package Slic3r::Config::Full;
use parent 'Slic3r::Config';
sub Slic3r::Config::GCode::new { Slic3r::Config::Static::new_GCodeConfig }
sub Slic3r::Config::Print::new { Slic3r::Config::Static::new_PrintConfig }
sub Slic3r::Config::PrintObject::new { Slic3r::Config::Static::new_PrintObjectConfig }
sub Slic3r::Config::PrintRegion::new { Slic3r::Config::Static::new_PrintRegionConfig }
sub Slic3r::Config::Full::new { Slic3r::Config::Static::new_FullPrintConfig }
1;

View File

@ -1,52 +0,0 @@
package Slic3r::Extruder;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(EXTRUDER_ROLE_PERIMETER EXTRUDER_ROLE_INFILL EXTRUDER_ROLE_SUPPORT_MATERIAL
EXTRUDER_ROLE_SUPPORT_MATERIAL_INTERFACE);
our %EXPORT_TAGS = (roles => \@EXPORT_OK);
use Slic3r::Geometry qw(PI scale);
# has 'e_per_mm3' => (is => 'lazy');
# has 'retract_speed_mm_min' => (is => 'lazy');
use constant EXTRUDER_ROLE_PERIMETER => 1;
use constant EXTRUDER_ROLE_INFILL => 2;
use constant EXTRUDER_ROLE_SUPPORT_MATERIAL => 3;
use constant EXTRUDER_ROLE_SUPPORT_MATERIAL_INTERFACE => 4;
sub e_per_mm3 {
my $self = shift;
return $self->extrusion_multiplier * (4 / (($self->filament_diameter ** 2) * PI));
}
sub retract_speed_mm_min {
my $self = shift;
return $self->retract_speed * 60;
}
sub scaled_wipe_distance {
my ($self, $travel_speed) = @_;
# how far do we move in XY at travel_speed for the time needed to consume
# retract_length at retract_speed?
# reduce feedrate a bit; travel speed is often too high to move on existing material
# too fast = ripping of existing material; too slow = short wipe path, thus more blob
return scale($self->retract_length / $self->retract_speed * $travel_speed * 0.8);
}
sub extruded_volume {
my ($self, $E) = @_;
return $E * ($self->filament_diameter**2) * PI/4;
}
sub e_per_mm {
my ($self, $mm3_per_mm) = @_;
return $mm3_per_mm * $self->e_per_mm3;
}
1;

View File

@ -4,8 +4,8 @@ use warnings;
use parent qw(Exporter);
our @EXPORT_OK = qw(EXTRL_ROLE_DEFAULT EXTRL_ROLE_EXTERNAL_PERIMETER
EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER);
our @EXPORT_OK = qw(EXTRL_ROLE_DEFAULT
EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER EXTRL_ROLE_SKIRT);
our %EXPORT_TAGS = (roles => \@EXPORT_OK);

View File

@ -6,7 +6,8 @@ use parent qw(Exporter);
our @EXPORT_OK = qw(EXTR_ROLE_PERIMETER EXTR_ROLE_EXTERNAL_PERIMETER EXTR_ROLE_OVERHANG_PERIMETER
EXTR_ROLE_FILL EXTR_ROLE_SOLIDFILL EXTR_ROLE_TOPSOLIDFILL EXTR_ROLE_GAPFILL EXTR_ROLE_BRIDGE
EXTR_ROLE_SKIRT EXTR_ROLE_SUPPORTMATERIAL EXTR_ROLE_SUPPORTMATERIAL_INTERFACE);
EXTR_ROLE_SKIRT EXTR_ROLE_SUPPORTMATERIAL EXTR_ROLE_SUPPORTMATERIAL_INTERFACE
EXTR_ROLE_NONE);
our %EXPORT_TAGS = (roles => \@EXPORT_OK);
1;

View File

@ -1,5 +0,0 @@
package Slic3r::ExtrusionPath::Collection;
use strict;
use warnings;
1;

View File

@ -1,16 +1,12 @@
package Slic3r::Fill;
use Moo;
use List::Util qw(max);
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Fill::3DHoneycomb;
use Slic3r::Fill::ArchimedeanChords;
use Slic3r::Fill::Base;
use Slic3r::Fill::Concentric;
use Slic3r::Fill::Flowsnake;
use Slic3r::Fill::HilbertCurve;
use Slic3r::Fill::Honeycomb;
use Slic3r::Fill::Line;
use Slic3r::Fill::OctagramSpiral;
use Slic3r::Fill::PlanePath;
use Slic3r::Fill::Rectilinear;
use Slic3r::Flow ':roles';
@ -25,6 +21,7 @@ has 'fillers' => (is => 'rw', default => sub { {} });
our %FillTypes = (
archimedeanchords => 'Slic3r::Fill::ArchimedeanChords',
rectilinear => 'Slic3r::Fill::Rectilinear',
grid => 'Slic3r::Fill::Grid',
flowsnake => 'Slic3r::Fill::Flowsnake',
octagramspiral => 'Slic3r::Fill::OctagramSpiral',
hilbertcurve => 'Slic3r::Fill::HilbertCurve',
@ -52,11 +49,12 @@ sub make_fill {
my $self = shift;
my ($layerm) = @_;
Slic3r::debugf "Filling layer %d:\n", $layerm->id;
Slic3r::debugf "Filling layer %d:\n", $layerm->layer->id;
my $fill_density = $layerm->config->fill_density;
my $infill_flow = $layerm->flow(FLOW_ROLE_INFILL);
my $solid_infill_flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL);
my $fill_density = $layerm->region->config->fill_density;
my $infill_flow = $layerm->flow(FLOW_ROLE_INFILL);
my $solid_infill_flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL);
my $top_solid_infill_flow = $layerm->flow(FLOW_ROLE_TOP_SOLID_INFILL);
my @surfaces = ();
@ -77,13 +75,13 @@ sub make_fill {
for (my $i = 0; $i <= $#groups; $i++) {
# we can only merge solid non-bridge surfaces, so discard
# non-solid surfaces
if ($groups[$i][0]->is_solid && (!$groups[$i][0]->is_bridge || $layerm->id == 0)) {
if ($groups[$i][0]->is_solid && (!$groups[$i][0]->is_bridge || $layerm->layer->id == 0)) {
$is_solid[$i] = 1;
$fw[$i] = ($groups[$i][0]->surface_type == S_TYPE_TOP)
? $layerm->flow(FLOW_ROLE_TOP_SOLID_INFILL)->width
? $top_solid_infill_flow->width
: $solid_infill_flow->width;
$pattern[$i] = $groups[$i][0]->is_external
? $layerm->config->solid_fill_pattern
? $layerm->region->config->external_fill_pattern
: 'rectilinear';
} else {
$is_solid[$i] = 0;
@ -147,8 +145,12 @@ sub make_fill {
# we are going to grow such regions by overlapping them with the void (if any)
# TODO: detect and investigate whether there could be narrow regions without
# any void neighbors
my $distance_between_surfaces = $infill_flow->scaled_spacing * &Slic3r::INFILL_OVERLAP_OVER_SPACING;
{
my $distance_between_surfaces = max(
$infill_flow->scaled_spacing,
$solid_infill_flow->scaled_spacing,
$top_solid_infill_flow->scaled_spacing,
);
my $collapsed = diff(
[ map @{$_->expolygon}, @surfaces ],
offset2([ map @{$_->expolygon}, @surfaces ], -$distance_between_surfaces/2, +$distance_between_surfaces/2),
@ -167,9 +169,6 @@ sub make_fill {
)};
}
# add spacing between surfaces
@surfaces = map @{$_->offset(-$distance_between_surfaces / 2)}, @surfaces;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output("fill_" . $layerm->print_z . ".svg",
@ -179,83 +178,116 @@ sub make_fill {
}
my @fills = ();
my @fills_ordering_points = ();
SURFACE: foreach my $surface (@surfaces) {
next if $surface->surface_type == S_TYPE_INTERNALVOID;
my $filler = $layerm->config->fill_pattern;
my $filler = $layerm->region->config->fill_pattern;
my $density = $fill_density;
my $role = ($surface->surface_type == S_TYPE_TOP) ? FLOW_ROLE_TOP_SOLID_INFILL
: $surface->is_solid ? FLOW_ROLE_SOLID_INFILL
: FLOW_ROLE_INFILL;
my $is_bridge = $layerm->id > 0 && $surface->is_bridge;
my $is_bridge = $layerm->layer->id > 0 && $surface->is_bridge;
my $is_solid = $surface->is_solid;
# force 100% density and rectilinear fill for external surfaces
if ($surface->surface_type != S_TYPE_INTERNAL) {
if ($surface->is_solid) {
$density = 100;
$filler = $layerm->config->solid_fill_pattern;
if ($is_bridge) {
$filler = 'rectilinear';
} elsif ($surface->surface_type == S_TYPE_INTERNALSOLID) {
$filler = 'rectilinear';
$filler = 'rectilinear';
if ($surface->is_external && !$is_bridge) {
$filler = $layerm->region->config->external_fill_pattern;
}
} else {
next SURFACE unless $density > 0;
}
my $h = $surface->thickness == -1 ? $layerm->height : $surface->thickness;
# get filler object
my $f = $self->filler($filler);
# calculate the actual flow we'll be using for this infill
my $h = $surface->thickness == -1 ? $layerm->layer->height : $surface->thickness;
my $flow = $layerm->region->flow(
$role,
$h,
$is_bridge,
$layerm->id == 0,
$is_bridge || $f->use_bridge_flow,
$layerm->layer->id == 0,
-1,
$layerm->object,
$layerm->layer->object,
);
my $f = $self->filler($filler);
$f->layer_id($layerm->id);
$f->z($layerm->print_z);
$f->angle(deg2rad($layerm->config->fill_angle));
my ($params, @polylines) = $f->fill_surface(
$surface,
# calculate flow spacing for infill pattern generation
my $using_internal_flow = 0;
if (!$is_solid && !$is_bridge) {
# it's internal infill, so we can calculate a generic flow spacing
# for all layers, for avoiding the ugly effect of
# misaligned infill on first layer because of different extrusion width and
# layer height
my $internal_flow = $layerm->region->flow(
FLOW_ROLE_INFILL,
$layerm->layer->object->config->layer_height, # TODO: handle infill_every_layers?
0, # no bridge
0, # no first layer
-1, # auto width
$layerm->layer->object,
);
$f->spacing($internal_flow->spacing);
$using_internal_flow = 1;
} else {
$f->spacing($flow->spacing);
}
$f->layer_id($layerm->layer->id);
$f->z($layerm->layer->print_z);
$f->angle(deg2rad($layerm->region->config->fill_angle));
$f->loop_clipping(scale($flow->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER);
# apply half spacing using this flow's own spacing and generate infill
my @polylines = map $f->fill_surface(
$_,
density => $density/100,
flow => $flow,
layer_height => $h,
);
), @{ $surface->offset(-scale($f->spacing)/2) };
next unless @polylines;
# calculate actual flow from spacing (which might have been adjusted by the infill
# pattern generator)
if ($using_internal_flow) {
# if we used the internal flow we're not doing a solid infill
# so we can safely ignore the slight variation that might have
# been applied to $f->flow_spacing
} else {
$flow = Slic3r::Flow->new_from_spacing(
spacing => $f->spacing,
nozzle_diameter => $flow->nozzle_diameter,
layer_height => $h,
bridge => $is_bridge || $f->use_bridge_flow,
);
}
my $mm3_per_mm = $flow->mm3_per_mm;
# save into layer
push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
$collection->no_sort($params->{no_sort});
$collection->append(
map Slic3r::ExtrusionPath->new(
polyline => $_,
role => ($is_bridge
? EXTR_ROLE_BRIDGE
: $is_solid
? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
: EXTR_ROLE_FILL),
mm3_per_mm => $mm3_per_mm,
width => $flow->width,
height => ($is_bridge ? $flow->width : $h),
), @polylines,
);
push @fills_ordering_points, $polylines[0]->first_point;
{
my $role = $is_bridge ? EXTR_ROLE_BRIDGE
: $is_solid ? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
: EXTR_ROLE_FILL;
push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
$collection->no_sort($f->no_sort);
$collection->append(
map Slic3r::ExtrusionPath->new(
polyline => $_,
role => $role,
mm3_per_mm => $mm3_per_mm,
width => $flow->width,
height => $flow->height,
), @polylines,
);
}
}
# add thin fill regions
foreach my $thin_fill (@{$layerm->thin_fills}) {
push @fills, Slic3r::ExtrusionPath::Collection->new($thin_fill);
push @fills_ordering_points, $thin_fill->first_point;
}
# organize infill paths using a nearest-neighbor search
@fills = @fills[ @{chained_path(\@fills_ordering_points)} ];
return @fills;
}

View File

@ -7,29 +7,26 @@ use POSIX qw(ceil fmod);
use Slic3r::Geometry qw(scale scaled_epsilon);
use Slic3r::Geometry::Clipper qw(intersection_pl);
# require bridge flow since most of this pattern hangs in air
sub use_bridge_flow { 1 }
sub fill_surface {
my ($self, $surface, %params) = @_;
# use bridge flow since most of this pattern hangs in air
my $flow = Slic3r::Flow->new(
width => $params{flow}->width,
height => $params{flow}->height,
nozzle_diameter => $params{flow}->nozzle_diameter,
bridge => 1,
);
my $expolygon = $surface->expolygon;
my $bb = $expolygon->bounding_box;
my $size = $bb->size;
my $distance = $flow->scaled_spacing / $params{density};
my $distance = scale($self->spacing) / $params{density};
# align bounding box to a multiple of our honeycomb grid
# align bounding box to a multiple of our honeycomb grid module
# (a module is 2*$distance since one $distance half-module is
# growing while the other $distance half-module is shrinking)
{
my $min = $bb->min_point;
$min->translate(
-($bb->x_min % $distance),
-($bb->y_min % $distance),
-($bb->x_min % (2*$distance)),
-($bb->y_min % (2*$distance)),
);
$bb->merge_point($min);
}
@ -39,8 +36,8 @@ sub fill_surface {
makeGrid(
scale($self->z),
$distance,
ceil($size->x / $distance),
ceil($size->y / $distance), #//
ceil($size->x / $distance) + 1,
ceil($size->y / $distance) + 1, #//
(($self->layer_id / $surface->thickness_layers) % 2) + 1,
);
@ -71,7 +68,7 @@ sub fill_surface {
}
# TODO: return ExtrusionLoop objects to get better chained paths
return { flow => $flow}, @polylines;
return @polylines;
}

View File

@ -1,7 +0,0 @@
package Slic3r::Fill::ArchimedeanChords;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::ArchimedeanChords;
1;

View File

@ -4,6 +4,8 @@ use Moo;
has 'layer_id' => (is => 'rw');
has 'z' => (is => 'rw'); # in unscaled coordinates
has 'angle' => (is => 'rw'); # in radians, ccw, 0 = East
has 'spacing' => (is => 'rw'); # in unscaled coordinates
has 'loop_clipping' => (is => 'rw', default => sub { 0 }); # in scaled coordinates
has 'bounding_box' => (is => 'ro', required => 0); # Slic3r::Geometry::BoundingBox object
sub adjust_solid_spacing {
@ -17,6 +19,9 @@ sub adjust_solid_spacing {
return $params{distance} + $extra_space / ($number_of_lines - 1);
}
sub no_sort { 0 }
sub use_bridge_flow { 0 }
package Slic3r::Fill::WithDirection;
use Moo::Role;

View File

@ -6,6 +6,8 @@ extends 'Slic3r::Fill::Base';
use Slic3r::Geometry qw(scale unscale X);
use Slic3r::Geometry::Clipper qw(offset offset2 union_pt_chained);
sub no_sort { 1 }
sub fill_surface {
my $self = shift;
my ($surface, %params) = @_;
@ -15,27 +17,18 @@ sub fill_surface {
my $expolygon = $surface->expolygon;
my $bounding_box = $expolygon->bounding_box;
my $flow = $params{flow};
my $min_spacing = $flow->scaled_spacing;
my $min_spacing = scale($self->spacing);
my $distance = $min_spacing / $params{density};
my $flow_spacing = $flow->spacing;
if ($params{density} == 1 && !$params{dont_adjust}) {
$distance = $self->adjust_solid_spacing(
width => $bounding_box->size->[X],
distance => $distance,
);
$flow = Slic3r::Flow->new_from_spacing(
spacing => unscale($distance),
nozzle_diameter => $flow->nozzle_diameter,
layer_height => ($params{layer_height} or die "No layer_height supplied to fill_surface()"),
bridge => $flow->bridge,
);
$self->spacing(unscale $distance);
}
# compensate the overlap which is good for rectilinear but harmful for concentric
# where the perimeter/infill spacing should be equal to any other loop spacing
my @loops = my @last = @{offset(\@$expolygon, -&Slic3r::INFILL_OVERLAP_OVER_SPACING * $min_spacing / 2)};
my @loops = my @last = map $_->clone, @$expolygon;
while (@last) {
push @loops, @last = @{offset2(\@last, -($distance + 0.5*$min_spacing), +0.5*$min_spacing)};
}
@ -45,7 +38,7 @@ sub fill_surface {
@loops = map Slic3r::Polygon->new(@$_),
reverse @{union_pt_chained(\@loops)};
# order paths using a nearest neighbor search
# split paths using a nearest neighbor search
my @paths = ();
my $last_pos = Slic3r::Point->new(0,0);
foreach my $loop (@loops) {
@ -54,12 +47,11 @@ sub fill_surface {
}
# clip the paths to prevent the extruder from getting exactly on the first point of the loop
my $clip_length = scale($flow->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER;
$_->clip_end($clip_length) for @paths;
$_->clip_end($self->loop_clipping) for @paths;
@paths = grep $_->is_valid, @paths; # remove empty paths (too short, thus eaten by clipping)
# TODO: return ExtrusionLoop objects to get better chained paths
return { flow => $flow, no_sort => 1 }, @paths;
return @paths;
}
1;

View File

@ -1,18 +0,0 @@
package Slic3r::Fill::Flowsnake;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::Flowsnake;
use Slic3r::Geometry qw(X);
# Sorry, this fill is currently broken.
sub process_polyline {
my $self = shift;
my ($polyline, $bounding_box) = @_;
$_->[X] += $bounding_box->center->[X] for @$polyline;
}
1;

View File

@ -1,7 +0,0 @@
package Slic3r::Fill::HilbertCurve;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::HilbertCurve;
1;

View File

@ -18,11 +18,11 @@ sub fill_surface {
my $rotate_vector = $self->infill_direction($surface);
# cache hexagons math
my $cache_id = sprintf "d%s_s%s", $params{density}, $params{flow}->spacing;
my $cache_id = sprintf "d%s_s%s", $params{density}, $self->spacing;
my $m;
if (!($m = $self->cache->{$cache_id})) {
$m = $self->cache->{$cache_id} = {};
my $min_spacing = $params{flow}->scaled_spacing;
my $min_spacing = scale($self->spacing);
$m->{distance} = $min_spacing / $params{density};
$m->{hex_side} = $m->{distance} / (sqrt(3)/2);
$m->{hex_width} = $m->{distance} * 2; # $m->{hex_width} == $m->{hex_side} * sqrt(3);
@ -81,7 +81,7 @@ sub fill_surface {
}
my @paths;
if ($params{complete}) {
if ($params{complete} || 1) {
# we were requested to complete each loop;
# in this case we don't try to make more continuous paths
@paths = map $_->split_at_first_point,
@ -123,7 +123,7 @@ sub fill_surface {
)};
}
return { flow => $params{flow} }, @paths;
return @paths;
}
1;

View File

@ -1,8 +0,0 @@
package Slic3r::Fill::Line;
use Moo;
extends 'Slic3r::Fill::Rectilinear';
# Sorry for breaking OOP, but Line is implemented inside Rectilinear.
1;

View File

@ -1,9 +0,0 @@
package Slic3r::Fill::OctagramSpiral;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::OctagramSpiral;
sub multiplier () { sqrt(2) }
1;

View File

@ -7,16 +7,9 @@ with qw(Slic3r::Fill::WithDirection);
use Slic3r::Geometry qw(scale X1 Y1 X2 Y2);
use Slic3r::Geometry::Clipper qw(intersection_pl);
sub angles () { [0] }
sub multiplier () { 1 }
sub get_n {
my $self = shift;
my ($path, $bounding_box) = @_;
my ($n_lo, $n_hi) = $path->rect_to_n_range(@$bounding_box);
return ($n_lo .. $n_hi);
}
sub process_polyline {}
sub fill_surface {
@ -28,16 +21,37 @@ sub fill_surface {
my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($expolygon, $rotate_vector);
my $flow = $params{flow};
my $distance_between_lines = $flow->scaled_spacing / $params{density} * $self->multiplier;
my $bounding_box = $expolygon->bounding_box;
my $distance_between_lines = scale($self->spacing) / $params{density} * $self->multiplier;
# align infill across layers using the object's bounding box
my $bb_polygon = $self->bounding_box->polygon;
$self->rotate_points($bb_polygon, $rotate_vector);
my $bounding_box = $bb_polygon->bounding_box;
(ref $self) =~ /::([^:]+)$/;
my $path = "Math::PlanePath::$1"->new;
my @n = $self->get_n($path, [ map +($_ / $distance_between_lines), @{$bounding_box->min_point}, @{$bounding_box->max_point} ]);
my $translate = Slic3r::Point->new(0,0); # vector
if ($path->x_negative || $path->y_negative) {
# if the curve extends on both positive and negative coordinate space,
# center our expolygon around origin
$translate = $bounding_box->center->negative;
} else {
# if the curve does not extend in negative coordinate space,
# move expolygon entirely in positive coordinate space
$translate = $bounding_box->min_point->negative;
}
$expolygon->translate(@$translate);
$bounding_box->translate(@$translate);
my ($n_lo, $n_hi) = $path->rect_to_n_range(
map { $_ / $distance_between_lines }
@{$bounding_box->min_point},
@{$bounding_box->max_point},
);
my $polyline = Slic3r::Polyline->new(
map [ map {$_*$distance_between_lines} $path->n_to_xy($_) ], @n,
map [ map { $_ * $distance_between_lines } $path->n_to_xy($_) ], ($n_lo..$n_hi)
);
return {} if @$polyline <= 1;
@ -48,15 +62,57 @@ sub fill_surface {
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output("fill.svg",
polygons => $expolygon,
polylines => [map $_->p, @paths],
no_arrows => 1,
polygons => \@$expolygon,
green_polygons => [ $bounding_box->polygon ],
polylines => [ $polyline ],
red_polylines => \@paths,
);
}
# paths must be rotated back
# paths must be repositioned and rotated back
$_->translate(@{$translate->negative}) for @paths;
$self->rotate_points_back(\@paths, $rotate_vector);
return { flow => $flow }, @paths;
return @paths;
}
package Slic3r::Fill::ArchimedeanChords;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::ArchimedeanChords;
package Slic3r::Fill::Flowsnake;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::Flowsnake;
use Slic3r::Geometry qw(X);
# Sorry, this fill is currently broken.
sub process_polyline {
my $self = shift;
my ($polyline, $bounding_box) = @_;
$_->[X] += $bounding_box->center->[X] for @$polyline;
}
package Slic3r::Fill::HilbertCurve;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::HilbertCurve;
package Slic3r::Fill::OctagramSpiral;
use Moo;
extends 'Slic3r::Fill::PlanePath';
use Math::PlanePath::OctagramSpiral;
sub multiplier () { sqrt(2) }
1;

View File

@ -4,10 +4,15 @@ use Moo;
extends 'Slic3r::Fill::Base';
with qw(Slic3r::Fill::WithDirection);
has 'cache' => (is => 'rw', default => sub {{}});
has '_min_spacing' => (is => 'rw');
has '_line_spacing' => (is => 'rw');
has '_diagonal_distance' => (is => 'rw');
has '_line_oscillation' => (is => 'rw');
use Slic3r::Geometry qw(A B X Y MIN scale unscale scaled_epsilon);
use Slic3r::Geometry::Clipper qw(intersection_pl offset);
use Slic3r::Geometry qw(scale unscale scaled_epsilon);
use Slic3r::Geometry::Clipper qw(intersection_pl);
sub horizontal_lines { 0 }
sub fill_surface {
my $self = shift;
@ -18,47 +23,41 @@ sub fill_surface {
my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($expolygon, $rotate_vector);
my $flow = $params{flow} or die "No flow supplied to fill_surface()";
my $min_spacing = $flow->scaled_spacing;
my $line_spacing = $min_spacing / $params{density};
my $line_oscillation = $line_spacing - $min_spacing;
my $is_line_pattern = $self->isa('Slic3r::Fill::Line');
my $bounding_box = $expolygon->bounding_box;
$self->_min_spacing(scale $self->spacing);
$self->_line_spacing($self->_min_spacing / $params{density});
$self->_diagonal_distance($self->_line_spacing * 2);
$self->_line_oscillation($self->_line_spacing - $self->_min_spacing); # only for Line infill
my $bounding_box = $expolygon->bounding_box;
# define flow spacing according to requested density
if ($params{density} == 1 && !$params{dont_adjust}) {
$line_spacing = $self->adjust_solid_spacing(
width => $bounding_box->size->[X],
distance => $line_spacing,
);
$flow = Slic3r::Flow->new_from_spacing(
spacing => unscale($line_spacing),
nozzle_diameter => $flow->nozzle_diameter,
layer_height => ($params{layer_height} or die "No layer_height supplied to fill_surface()"),
bridge => $flow->bridge,
);
$self->_line_spacing($self->adjust_solid_spacing(
width => $bounding_box->size->x,
distance => $self->_line_spacing,
));
$self->spacing(unscale $self->_line_spacing);
} else {
# extend bounding box so that our pattern will be aligned with other layers
$bounding_box->merge_point(Slic3r::Point->new(
$bounding_box->x_min - ($bounding_box->x_min % $line_spacing),
$bounding_box->y_min - ($bounding_box->y_min % $line_spacing),
$bounding_box->x_min - ($bounding_box->x_min % $self->_line_spacing),
$bounding_box->y_min - ($bounding_box->y_min % $self->_line_spacing),
));
}
# generate the basic pattern
my $i = 0;
my $x = $bounding_box->x_min;
my $x_max = $bounding_box->x_max + scaled_epsilon;
my @vertical_lines = ();
while ($x <= $x_max) {
my $vertical_line = [ [$x, $bounding_box->y_max], [$x, $bounding_box->y_min] ];
if ($is_line_pattern && $i % 2) {
$vertical_line->[A][X] += $line_oscillation;
$vertical_line->[B][X] -= $line_oscillation;
my $x_max = $bounding_box->x_max + scaled_epsilon;
my @lines = ();
for (my $x = $bounding_box->x_min; $x <= $x_max; $x += $self->_line_spacing) {
push @lines, $self->_line($#lines, $x, $bounding_box->y_min, $bounding_box->y_max);
}
if ($self->horizontal_lines) {
my $y_max = $bounding_box->y_max + scaled_epsilon;
for (my $y = $bounding_box->y_min; $y <= $y_max; $y += $self->_line_spacing) {
push @lines, Slic3r::Polyline->new(
[$bounding_box->x_min, $y],
[$bounding_box->x_max, $y],
);
}
push @vertical_lines, Slic3r::Polyline->new(@$vertical_line);
$i++;
$x += $line_spacing;
}
# clip paths against a slightly larger expolygon, so that the first and last paths
@ -66,23 +65,25 @@ sub fill_surface {
# the minimum offset for preventing edge lines from being clipped is scaled_epsilon;
# however we use a larger offset to support expolygons with slightly skewed sides and
# not perfectly straight
my @polylines = @{intersection_pl(\@vertical_lines, $expolygon->offset(scale 0.02))};
my @polylines = @{intersection_pl(\@lines, $expolygon->offset(+scale 0.02))};
my $extra = $self->_min_spacing * &Slic3r::INFILL_OVERLAP_OVER_SPACING;
foreach my $polyline (@polylines) {
my ($first_point, $last_point) = @$polyline[0,-1];
if ($first_point->y > $last_point->y) { #>
($first_point, $last_point) = ($last_point, $first_point);
}
$first_point->set_y($first_point->y - $extra); #--
$last_point->set_y($last_point->y + $extra); #++
}
# connect lines
unless ($params{dont_connect} || !@polylines) { # prevent calling leftmost_point() on empty collections
my ($expolygon_off) = @{$expolygon->offset_ex($min_spacing/2)};
# offset the expolygon by max(min_spacing/2, extra)
my ($expolygon_off) = @{$expolygon->offset_ex($self->_min_spacing/2)};
my $collection = Slic3r::Polyline::Collection->new(@polylines);
@polylines = ();
my $tolerance = 10 * scaled_epsilon;
my $diagonal_distance = $line_spacing * 2;
my $can_connect = $is_line_pattern
? sub {
($_[X] >= ($line_spacing - $line_oscillation) - $tolerance) && ($_[X] <= ($line_spacing + $line_oscillation) + $tolerance)
&& $_[Y] <= $diagonal_distance
}
: sub { $_[X] <= $diagonal_distance && $_[Y] <= $diagonal_distance };
foreach my $polyline (@{$collection->chained_path_from($collection->leftmost_point, 0)}) {
if (@polylines) {
my $first_point = $polyline->first_point;
@ -91,7 +92,7 @@ sub fill_surface {
# TODO: we should also check that both points are on a fill_boundary to avoid
# connecting paths on the boundaries of internal regions
if ($can_connect->(@distance) && $expolygon_off->contains_line(Slic3r::Line->new($last_point, $first_point))) {
if ($self->_can_connect(@distance) && $expolygon_off->contains_line(Slic3r::Line->new($last_point, $first_point))) {
$polylines[-1]->append_polyline($polyline);
next;
}
@ -105,7 +106,63 @@ sub fill_surface {
# paths must be rotated back
$self->rotate_points_back(\@polylines, $rotate_vector);
return { flow => $flow }, @polylines;
return @polylines;
}
sub _line {
my ($self, $i, $x, $y_min, $y_max) = @_;
return Slic3r::Polyline->new(
[$x, $y_min],
[$x, $y_max],
);
}
sub _can_connect {
my ($self, $dist_X, $dist_Y) = @_;
return $dist_X <= $self->_diagonal_distance
&& $dist_Y <= $self->_diagonal_distance;
}
package Slic3r::Fill::Line;
use Moo;
extends 'Slic3r::Fill::Rectilinear';
use Slic3r::Geometry qw(scaled_epsilon);
sub _line {
my ($self, $i, $x, $y_min, $y_max) = @_;
if ($i % 2) {
return Slic3r::Polyline->new(
[$x - $self->_line_oscillation, $y_min],
[$x + $self->_line_oscillation, $y_max],
);
} else {
return Slic3r::Polyline->new(
[$x, $y_min],
[$x, $y_max],
);
}
}
sub _can_connect {
my ($self, $dist_X, $dist_Y) = @_;
my $TOLERANCE = 10 * scaled_epsilon;
return ($dist_X >= ($self->_line_spacing - $self->_line_oscillation) - $TOLERANCE)
&& ($dist_X <= ($self->_line_spacing + $self->_line_oscillation) + $TOLERANCE)
&& $dist_Y <= $self->_diagonal_distance;
}
package Slic3r::Fill::Grid;
use Moo;
extends 'Slic3r::Fill::Rectilinear';
sub angles () { [0] }
sub horizontal_lines { 1 }
1;

View File

@ -36,7 +36,9 @@ sub write_file {
printf $fh qq{<amf unit="millimeter">\n};
printf $fh qq{ <metadata type="cad">Slic3r %s</metadata>\n}, $Slic3r::VERSION;
for my $material_id (sort @{ $model->material_names }) {
next if $material_id eq '';
my $material = $model->get_material($material_id);
# note that material-id must never be 0 since it's reserved by the AMF spec
printf $fh qq{ <material id="%s">\n}, $material_id;
for (keys %{$material->attributes}) {
printf $fh qq{ <metadata type=\"%s\">%s</metadata>\n}, $_, $material->attributes->{$_};

View File

@ -143,6 +143,7 @@ sub end_document {
}
foreach my $instance (@{ $self->{_instances}{$object_id} }) {
next if !defined($instance->{deltax}) || !defined($instance->{deltay});
$self->{_model}->objects->[$new_object_id]->add_instance(
rotation => $instance->{rz} || 0,
offset => Slic3r::Pointf->new($instance->{deltax} || 0, $instance->{deltay} || 0),

View File

@ -14,6 +14,9 @@ sub read_file {
$mesh->ReadSTLFile($path);
$mesh->repair;
die "This STL file couldn't be read because it's empty.\n"
if $mesh->facets_count == 0;
my $model = Slic3r::Model->new;
my $basename = basename($file);

View File

@ -1,713 +0,0 @@
package Slic3r::GCode;
use Moo;
use List::Util qw(min max first);
use Slic3r::ExtrusionLoop ':roles';
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon points_coincide PI X Y B);
use Slic3r::Geometry::Clipper qw(union_ex offset_ex);
use Slic3r::Surface ':types';
has 'config' => (is => 'ro', default => sub { Slic3r::Config::Full->new });
has 'placeholder_parser' => (is => 'rw', default => sub { Slic3r::GCode::PlaceholderParser->new });
has 'standby_points' => (is => 'rw');
has 'enable_loop_clipping' => (is => 'rw', default => sub {1});
has 'enable_wipe' => (is => 'rw', default => sub {0}); # at least one extruder has wipe enabled
has 'layer_count' => (is => 'ro', required => 1 );
has '_layer_index' => (is => 'rw', default => sub {-1}); # just a counter
has 'layer' => (is => 'rw');
has '_layer_islands' => (is => 'rw');
has '_upper_layer_islands' => (is => 'rw');
has '_seam_position' => (is => 'ro', default => sub { {} }); # $object => pos
has 'shift_x' => (is => 'rw', default => sub {0} );
has 'shift_y' => (is => 'rw', default => sub {0} );
has 'z' => (is => 'rw');
has 'extruders' => (is => 'ro', default => sub {{}});
has 'multiple_extruders' => (is => 'rw', default => sub {0});
has 'extruder' => (is => 'rw');
has 'external_mp' => (is => 'rw');
has 'layer_mp' => (is => 'rw');
has 'new_object' => (is => 'rw', default => sub {0});
has 'straight_once' => (is => 'rw', default => sub {1});
has 'elapsed_time' => (is => 'rw', default => sub {0} ); # seconds
has 'lifted' => (is => 'rw', default => sub {0} );
has 'last_pos' => (is => 'rw', default => sub { Slic3r::Point->new(0,0) } );
has 'last_fan_speed' => (is => 'rw', default => sub {0});
has 'last_acceleration' => (is => 'rw', default => sub {0});
has 'wipe_path' => (is => 'rw');
sub set_extruders {
my ($self, $extruder_ids, $print_config) = @_;
foreach my $i (@$extruder_ids) {
$self->extruders->{$i} = my $e = Slic3r::Extruder->new($i, $print_config);
$self->enable_wipe(1) if $e->wipe;
}
# we enable support for multiple extruder if any extruder greater than 0 is used
# (even if prints only uses that one) since we need to output Tx commands
# first extruder has index 0
$self->multiple_extruders(max(@$extruder_ids) > 0);
}
sub set_shift {
my ($self, @shift) = @_;
# if shift increases (goes towards right), last_pos decreases because it goes towards left
my @translate = (
scale ($self->shift_x - $shift[X]),
scale ($self->shift_y - $shift[Y]),
);
$self->last_pos->translate(@translate);
$self->wipe_path->translate(@translate) if $self->wipe_path;
$self->shift_x($shift[X]);
$self->shift_y($shift[Y]);
}
sub change_layer {
my ($self, $layer) = @_;
$self->layer($layer);
$self->_layer_index($self->_layer_index + 1);
# avoid computing islands and overhangs if they're not needed
$self->_layer_islands($layer->islands);
$self->_upper_layer_islands($layer->upper_layer ? $layer->upper_layer->islands : []);
if ($self->config->avoid_crossing_perimeters) {
$self->layer_mp(Slic3r::MotionPlanner->new(
union_ex([ map @$_, @{$layer->slices} ], 1),
));
}
my $gcode = "";
if ($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/) {
# TODO: cap this to 99% and add an explicit M73 P100 in the end G-code
$gcode .= sprintf "M73 P%s%s\n",
int(99 * ($self->_layer_index / ($self->layer_count - 1))),
($self->config->gcode_comments ? ' ; update progress' : '');
}
$gcode .= $self->move_z($layer->print_z);
return $gcode;
}
# this method accepts Z in unscaled coordinates
sub move_z {
my ($self, $z, $comment) = @_;
my $gcode = "";
$z += $self->config->z_offset;
my $current_z = $self->z;
my $nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef;
if (!defined $current_z || $z > $current_z || $z < $nominal_z) {
# we're moving above the current actual Z (so above the lift height of the current
# layer if any) or below the current nominal layer
# in both cases, we're going to the nominal Z of the next layer
$self->lifted(0);
if ($self->extruder->retract_layer_change) {
# this retraction may alter $self->z
$gcode .= $self->retract(move_z => $z);
$current_z = $self->z; # update current z in case retract() changed it
$nominal_z = defined $current_z ? ($current_z - $self->lifted) : undef;
}
$gcode .= $self->G0(undef, $z, 0, $self->config->travel_speed*60, $comment || ('move to next layer (' . $self->layer->id . ')'))
if !defined $current_z || abs($z - $nominal_z) > epsilon;
} elsif ($z < $current_z) {
# we're moving above the current nominal layer height and below the current actual one.
# we're basically advancing to next layer, whose nominal Z is still lower than the previous
# layer Z with lift.
$self->lifted($current_z - $z);
}
return $gcode;
}
sub extrude {
my $self = shift;
$_[0]->isa('Slic3r::ExtrusionLoop')
? $self->extrude_loop(@_)
: $self->extrude_path(@_);
}
sub extrude_loop {
my ($self, $loop, $description, $speed) = @_;
# make a copy; don't modify the orientation of the original loop object otherwise
# next copies (if any) would not detect the correct orientation
$loop = $loop->clone;
# extrude all loops ccw
my $was_clockwise = $loop->make_counter_clockwise;
# find the point of the loop that is closest to the current extruder position
# or randomize if requested
my $last_pos = $self->last_pos;
if ($self->config->spiral_vase) {
$loop->split_at($last_pos);
} elsif ($self->config->seam_position eq 'nearest' || $self->config->seam_position eq 'aligned') {
# simplify polygon in order to skip false positives in concave/convex detection
my $polygon = $loop->polygon;
my @simplified = @{$polygon->simplify(scale $self->extruder->nozzle_diameter/2)};
# concave vertices have priority
my @candidates = map @{$_->concave_points(PI*4/3)}, @simplified;
# if no concave points were found, look for convex vertices
@candidates = map @{$_->convex_points(PI*2/3)}, @simplified if !@candidates;
# retrieve the last start position for this object
my $obj_ptr;
if (defined $self->layer) {
$obj_ptr = $self->layer->object->ptr;
if (defined $self->_seam_position->{$self->layer->object}) {
$last_pos = $self->_seam_position->{$obj_ptr};
}
}
my $point;
if ($self->config->seam_position eq 'nearest') {
@candidates = @$polygon if !@candidates;
$point = $last_pos->nearest_point(\@candidates);
$loop->split_at_vertex($point);
} elsif (@candidates) {
my @non_overhang = grep !$loop->has_overhang_point($_), @candidates;
@candidates = @non_overhang if @non_overhang;
$point = $last_pos->nearest_point(\@candidates);
$loop->split_at_vertex($point);
} else {
$point = $last_pos->projection_onto_polygon($polygon);
$loop->split_at($point);
}
$self->_seam_position->{$obj_ptr} = $point;
} elsif ($self->config->seam_position eq 'random') {
if ($loop->role == EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER) {
my $polygon = $loop->polygon;
my $centroid = $polygon->centroid;
$last_pos = Slic3r::Point->new($polygon->bounding_box->x_max, $centroid->y); #))
$last_pos->rotate(rand(2*PI), $centroid);
}
$loop->split_at($last_pos);
}
# clip the path to avoid the extruder to get exactly on the first point of the loop;
# if polyline was shorter than the clipping distance we'd get a null polyline, so
# we discard it in that case
my $clip_length = $self->enable_loop_clipping
? scale($self->extruder->nozzle_diameter) * &Slic3r::LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
: 0;
# get paths
my @paths = @{$loop->clip_end($clip_length)};
return '' if !@paths;
# apply the small perimeter speed
if ($paths[0]->is_perimeter && $loop->length <= &Slic3r::SMALL_PERIMETER_LENGTH) {
$speed //= $self->config->get_abs_value('small_perimeter_speed');
}
$speed //= -1;
# extrude along the path
my $gcode = join '', map $self->_extrude_path($_, $description, $speed), @paths;
# reset acceleration
$gcode .= $self->set_acceleration($self->config->default_acceleration);
$self->wipe_path($paths[-1]->polyline->clone) if $self->enable_wipe; # TODO: don't limit wipe to last path
# make a little move inwards before leaving loop
if ($paths[-1]->role == EXTR_ROLE_EXTERNAL_PERIMETER && defined $self->layer && $self->config->perimeters > 1) {
my $last_path_polyline = $paths[-1]->polyline;
# detect angle between last and first segment
# the side depends on the original winding order of the polygon (left for contours, right for holes)
my @points = $was_clockwise ? (-2, 1) : (1, -2);
my $angle = Slic3r::Geometry::angle3points(@$last_path_polyline[0, @points]) / 3;
$angle *= -1 if $was_clockwise;
# create the destination point along the first segment and rotate it
# we make sure we don't exceed the segment length because we don't know
# the rotation of the second segment so we might cross the object boundary
my $first_segment = Slic3r::Line->new(@$last_path_polyline[0,1]);
my $distance = min(scale($self->extruder->nozzle_diameter), $first_segment->length);
my $point = $first_segment->point_at($distance);
$point->rotate($angle, $last_path_polyline->first_point);
# generate the travel move
$gcode .= $self->travel_to($point, $paths[-1]->role, "move inwards before travel");
}
return $gcode;
}
sub extrude_path {
my ($self, $path, $description, $speed) = @_;
my $gcode = $self->_extrude_path($path, $description, $speed);
# reset acceleration
$gcode .= $self->set_acceleration($self->config->default_acceleration);
return $gcode;
}
sub _extrude_path {
my ($self, $path, $description, $speed) = @_;
$path->simplify(&Slic3r::SCALED_RESOLUTION);
# go to first point of extrusion path
my $gcode = "";
{
my $first_point = $path->first_point;
$gcode .= $self->travel_to($first_point, $path->role, "move to first $description point")
if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point);
}
# compensate retraction
$gcode .= $self->unretract;
# adjust acceleration
{
my $acceleration;
if ($self->config->first_layer_acceleration && $self->layer->id == 0) {
$acceleration = $self->config->first_layer_acceleration;
} elsif ($self->config->perimeter_acceleration && $path->is_perimeter) {
$acceleration = $self->config->perimeter_acceleration;
} elsif ($self->config->infill_acceleration && $path->is_fill) {
$acceleration = $self->config->infill_acceleration;
} elsif ($self->config->infill_acceleration && $path->is_bridge) {
$acceleration = $self->config->bridge_acceleration;
} else {
$acceleration = $self->config->default_acceleration;
}
$gcode .= $self->set_acceleration($acceleration);
}
# calculate extrusion length per distance unit
my $e = $self->extruder->e_per_mm3 * $path->mm3_per_mm;
$e = 0 if !$self->config->get_extrusion_axis;
# set speed
my $F;
if ($path->role == EXTR_ROLE_PERIMETER) {
$F = $self->config->get_abs_value('perimeter_speed');
} elsif ($path->role == EXTR_ROLE_EXTERNAL_PERIMETER) {
$F = $self->config->get_abs_value('external_perimeter_speed');
} elsif ($path->role == EXTR_ROLE_OVERHANG_PERIMETER || $path->role == EXTR_ROLE_BRIDGE) {
$F = $self->config->get_abs_value('bridge_speed');
} elsif ($path->role == EXTR_ROLE_FILL) {
$F = $self->config->get_abs_value('infill_speed');
} elsif ($path->role == EXTR_ROLE_SOLIDFILL) {
$F = $self->config->get_abs_value('solid_infill_speed');
} elsif ($path->role == EXTR_ROLE_TOPSOLIDFILL) {
$F = $self->config->get_abs_value('top_solid_infill_speed');
} elsif ($path->role == EXTR_ROLE_GAPFILL) {
$F = $self->config->get_abs_value('gap_fill_speed');
} else {
$F = $speed // -1;
die "Invalid speed" if $F < 0; # $speed == -1
}
$F *= 60; # convert mm/sec to mm/min
if ($self->layer->id == 0) {
$F = $self->config->get_abs_value_over('first_layer_speed', $F/60) * 60;
}
# extrude arc or line
$gcode .= ";_BRIDGE_FAN_START\n" if $path->is_bridge;
my $path_length = unscale $path->length;
{
$gcode .= $path->gcode($self->extruder, $e, $F,
$self->shift_x - $self->extruder->extruder_offset->x,
$self->shift_y - $self->extruder->extruder_offset->y, #,,
$self->config->get_extrusion_axis,
$self->config->gcode_comments ? " ; $description" : "");
if ($self->enable_wipe) {
$self->wipe_path($path->polyline->clone);
$self->wipe_path->reverse;
}
}
$gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge;
$self->last_pos($path->last_point);
if ($self->config->cooling) {
my $path_time = $path_length / $F * 60;
$self->elapsed_time($self->elapsed_time + $path_time);
}
return $gcode;
}
sub travel_to {
my ($self, $point, $role, $comment) = @_;
my $gcode = "";
my $travel = Slic3r::Line->new($self->last_pos, $point);
# move travel back to original layer coordinates for the island check.
# note that we're only considering the current object's islands, while we should
# build a more complete configuration space
$travel->translate(-$self->shift_x, -$self->shift_y);
# skip retraction if the travel move is contained in an island in the current layer
# *and* in an island in the upper layer (so that the ooze will not be visible)
if ($travel->length < scale $self->extruder->retract_before_travel
|| ($self->config->only_retract_when_crossing_perimeters
&& $self->config->fill_density > 0
&& (first { $_->contains_line($travel) } @{$self->_upper_layer_islands})
&& (first { $_->contains_line($travel) } @{$self->_layer_islands}))
|| (defined $role && $role == EXTR_ROLE_SUPPORTMATERIAL && (first { $_->contains_line($travel) } @{$self->layer->support_islands}))
) {
$self->straight_once(0);
$gcode .= $self->G0($point, undef, 0, $self->config->travel_speed*60, $comment || "");
} elsif (!$self->config->avoid_crossing_perimeters || $self->straight_once) {
$self->straight_once(0);
$gcode .= $self->retract;
$gcode .= $self->G0($point, undef, 0, $self->config->travel_speed*60, $comment || "");
} else {
if ($self->new_object) {
$self->new_object(0);
# represent $point in G-code coordinates
$point = $point->clone;
my @shift = ($self->shift_x, $self->shift_y);
$point->translate(map scale $_, @shift);
# calculate path (external_mp uses G-code coordinates so we temporary need a null shift)
$self->set_shift(0,0);
$gcode .= $self->_plan($self->external_mp, $point, $comment);
$self->set_shift(@shift);
} else {
$gcode .= $self->_plan($self->layer_mp, $point, $comment);
}
}
return $gcode;
}
sub _plan {
my ($self, $mp, $point, $comment) = @_;
my $gcode = "";
my @travel = @{$mp->shortest_path($self->last_pos, $point)->lines};
# if the path is not contained in a single island we need to retract
my $need_retract = !$self->config->only_retract_when_crossing_perimeters;
if (!$need_retract) {
$need_retract = 1;
foreach my $island (@{$self->_upper_layer_islands}) {
# discard the island if at any line is not enclosed in it
next if first { !$island->contains_line($_) } @travel;
# okay, this island encloses the full travel path
$need_retract = 0;
last;
}
}
# do the retract (the travel_to argument is broken)
$gcode .= $self->retract if $need_retract;
# append the actual path and return
# use G1 because we rely on paths being straight (G0 may make round paths)
$gcode .= join '', map $self->G1($_->b, undef, 0, $self->config->travel_speed*60, $comment || ""), @travel;
return $gcode;
}
sub retract {
my ($self, %params) = @_;
# get the retraction length and abort if none
my ($length, $restart_extra, $comment) = $params{toolchange}
? ($self->extruder->retract_length_toolchange, $self->extruder->retract_restart_extra_toolchange, "retract for tool change")
: ($self->extruder->retract_length, $self->extruder->retract_restart_extra, "retract");
# if we already retracted, reduce the required amount of retraction
$length -= $self->extruder->retracted;
return "" unless $length > 0;
my $gcode = "";
# wipe
my $wipe_path;
if ($self->extruder->wipe && $self->wipe_path) {
my @points = @{$self->wipe_path};
$wipe_path = Slic3r::Polyline->new($self->last_pos, @{$self->wipe_path}[1..$#{$self->wipe_path}]);
$wipe_path->clip_end($wipe_path->length - $self->extruder->scaled_wipe_distance($self->config->travel_speed));
}
# prepare moves
my $retract = [undef, undef, -$length, $self->extruder->retract_speed_mm_min, $comment];
my $lift = ($self->config->retract_lift->[0] == 0 || defined $params{move_z}) && !$self->lifted
? undef
: [undef, $self->z + $self->config->retract_lift->[0], 0, $self->config->travel_speed*60, 'lift plate during travel'];
# check that we have a positive wipe length
if ($wipe_path) {
# subdivide the retraction
my $retracted = 0;
foreach my $line (@{$wipe_path->lines}) {
my $segment_length = $line->length;
# reduce retraction length a bit to avoid effective retraction speed to be greater than the configured one
# due to rounding
my $e = $retract->[2] * ($segment_length / $self->extruder->scaled_wipe_distance($self->config->travel_speed)) * 0.95;
$retracted += $e;
$gcode .= $self->G1($line->b, undef, $e, $self->config->travel_speed*60*0.8, $retract->[3] . ";_WIPE");
}
if ($retracted > $retract->[2]) {
# if we retracted less than we had to, retract the remainder
# TODO: add regression test
$gcode .= $self->G1(undef, undef, $retract->[2] - $retracted, $self->extruder->retract_speed_mm_min, $comment);
}
$gcode .= $self->reset_e;
} elsif ($self->config->use_firmware_retraction) {
$gcode .= "G10 ; retract\n";
} else {
$gcode .= $self->G1(@$retract);
# reset extrusion distance during retracts
# this makes sure we leave sufficient precision in the firmware
$gcode .= $self->reset_e;
}
if (!$self->lifted) {
if (defined $params{move_z} && $self->config->retract_lift->[0] > 0) {
my $travel = [undef, $params{move_z} + $self->config->retract_lift->[0], 0, $self->config->travel_speed*60, 'move to next layer (' . $self->layer->id . ') and lift'];
$gcode .= $self->G0(@$travel);
$self->lifted($self->config->retract_lift->[0]);
} elsif ($lift) {
$gcode .= $self->G1(@$lift);
}
}
$self->extruder->set_retracted($self->extruder->retracted + $length);
$self->extruder->set_restart_extra($restart_extra);
$self->lifted($self->config->retract_lift->[0]) if $lift;
$gcode .= "M103 ; extruder off\n" if $self->config->gcode_flavor eq 'makerware';
return $gcode;
}
sub unretract {
my ($self) = @_;
my $gcode = "";
$gcode .= "M101 ; extruder on\n" if $self->config->gcode_flavor eq 'makerware';
if ($self->lifted) {
$gcode .= $self->G0(undef, $self->z - $self->lifted, 0, $self->config->travel_speed*60, 'restore layer Z');
$self->lifted(0);
}
my $to_unretract = $self->extruder->retracted + $self->extruder->restart_extra;
if ($to_unretract) {
if ($self->config->use_firmware_retraction) {
$gcode .= "G11 ; unretract\n";
$gcode .= $self->reset_e;
} elsif ($self->config->get_extrusion_axis) {
# use G1 instead of G0 because G0 will blend the restart with the previous travel move
$gcode .= sprintf "G1 %s%.5f F%.3f",
$self->config->get_extrusion_axis,
$self->extruder->extrude($to_unretract),
$self->extruder->retract_speed_mm_min;
$gcode .= " ; compensate retraction" if $self->config->gcode_comments;
$gcode .= "\n";
}
$self->extruder->set_retracted(0);
$self->extruder->set_restart_extra(0);
}
return $gcode;
}
sub reset_e {
my ($self) = @_;
return "" if $self->config->gcode_flavor =~ /^(?:mach3|makerware|sailfish)$/;
$self->extruder->set_E(0) if $self->extruder;
return sprintf "G92 %s0%s\n", $self->config->get_extrusion_axis, ($self->config->gcode_comments ? ' ; reset extrusion distance' : '')
if $self->config->get_extrusion_axis && !$self->config->use_relative_e_distances;
}
sub set_acceleration {
my ($self, $acceleration) = @_;
return "" if !$acceleration || $acceleration == $self->last_acceleration;
$self->last_acceleration($acceleration);
return sprintf "M204 S%s%s\n",
$acceleration, ($self->config->gcode_comments ? ' ; adjust acceleration' : '');
}
sub G0 {
my $self = shift;
return $self->G1(@_) if !($self->config->g0 || $self->config->gcode_flavor eq 'mach3');
return $self->_G0_G1("G0", @_);
}
sub G1 {
my $self = shift;
return $self->_G0_G1("G1", @_);
}
sub _G0_G1 {
my ($self, $gcode, $point, $z, $e, $F, $comment) = @_;
if ($point) {
$gcode .= sprintf " X%.3f Y%.3f",
($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->extruder->extruder_offset->x,
($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->extruder->extruder_offset->y; #**
$self->last_pos($point->clone);
}
if (defined $z && (!defined $self->z || $z != $self->z)) {
$self->z($z);
$gcode .= sprintf " Z%.3f", $z;
}
return $self->_Gx($gcode, $e, $F, $comment);
}
sub _Gx {
my ($self, $gcode, $e, $F, $comment) = @_;
$gcode .= sprintf " F%.3f", $F;
# output extrusion distance
if ($e && $self->config->get_extrusion_axis) {
$gcode .= sprintf " %s%.5f", $self->config->get_extrusion_axis, $self->extruder->extrude($e);
}
$gcode .= " ; $comment" if $comment && $self->config->gcode_comments;
return "$gcode\n";
}
sub set_extruder {
my ($self, $extruder_id) = @_;
# return nothing if this extruder was already selected
return "" if (defined $self->extruder) && ($self->extruder->id == $extruder_id);
# if we are running a single-extruder setup, just set the extruder and return nothing
if (!$self->multiple_extruders) {
$self->extruder($self->extruders->{$extruder_id});
return "";
}
# trigger retraction on the current extruder (if any)
my $gcode = "";
$gcode .= $self->retract(toolchange => 1) if defined $self->extruder;
# append custom toolchange G-code
if (defined $self->extruder && $self->config->toolchange_gcode) {
$gcode .= sprintf "%s\n", $self->placeholder_parser->process($self->config->toolchange_gcode, {
previous_extruder => $self->extruder->id,
next_extruder => $extruder_id,
});
}
# set the current extruder to the standby temperature
if ($self->standby_points && defined $self->extruder) {
# move to the nearest standby point
{
my $last_pos = $self->last_pos->clone;
$last_pos->translate(scale +$self->shift_x, scale +$self->shift_y);
my $standby_point = $last_pos->nearest_point($self->standby_points);
$standby_point->translate(scale -$self->shift_x, scale -$self->shift_y);
$gcode .= $self->travel_to($standby_point);
}
if ($self->config->standby_temperature_delta != 0) {
my $temp = defined $self->layer && $self->layer->id == 0
? $self->extruder->first_layer_temperature
: $self->extruder->temperature;
# we assume that heating is always slower than cooling, so no need to block
$gcode .= $self->set_temperature($temp + $self->config->standby_temperature_delta, 0);
}
}
# set the new extruder
$self->extruder($self->extruders->{$extruder_id});
$gcode .= sprintf "%s%d%s\n",
($self->config->gcode_flavor eq 'makerware'
? 'M135 T'
: $self->config->gcode_flavor eq 'sailfish'
? 'M108 T'
: 'T'),
$extruder_id,
($self->config->gcode_comments ? ' ; change extruder' : '');
$gcode .= $self->reset_e;
# set the new extruder to the operating temperature
if ($self->config->ooze_prevention && $self->config->standby_temperature_delta != 0) {
my $temp = defined $self->layer && $self->layer->id == 0
? $self->extruder->first_layer_temperature
: $self->extruder->temperature;
$gcode .= $self->set_temperature($temp, 1);
}
return $gcode;
}
sub set_fan {
my ($self, $speed, $dont_save) = @_;
if ($self->last_fan_speed != $speed || $dont_save) {
$self->last_fan_speed($speed) if !$dont_save;
if ($speed == 0) {
my $code = $self->config->gcode_flavor eq 'teacup'
? 'M106 S0'
: $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/
? 'M127'
: 'M107';
return sprintf "$code%s\n", ($self->config->gcode_comments ? ' ; disable fan' : '');
} else {
if ($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/) {
return sprintf "M126%s\n", ($self->config->gcode_comments ? ' ; enable fan' : '');
} else {
return sprintf "M106 %s%d%s\n", ($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'),
(255 * $speed / 100), ($self->config->gcode_comments ? ' ; enable fan' : '');
}
}
}
return "";
}
sub set_temperature {
my ($self, $temperature, $wait, $tool) = @_;
return "" if $wait && $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/;
my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup')
? ('M109', 'wait for temperature to be reached')
: ('M104', 'set temperature');
my $gcode = sprintf "$code %s%d %s; $comment\n",
($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature,
(defined $tool && ($self->multiple_extruders || $self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/)) ? "T$tool " : "";
$gcode .= "M116 ; wait for temperature to be reached\n"
if $self->config->gcode_flavor eq 'teacup' && $wait;
return $gcode;
}
sub set_bed_temperature {
my ($self, $temperature, $wait) = @_;
my ($code, $comment) = ($wait && $self->config->gcode_flavor ne 'teacup')
? (($self->config->gcode_flavor =~ /^(?:makerware|sailfish)$/ ? 'M109' : 'M190'), 'wait for bed temperature to be reached')
: ('M140', 'set bed temperature');
my $gcode = sprintf "$code %s%d ; $comment\n",
($self->config->gcode_flavor eq 'mach3' ? 'P' : 'S'), $temperature;
$gcode .= "M116 ; wait for bed temperature to be reached\n"
if $self->config->gcode_flavor eq 'teacup' && $wait;
return $gcode;
}
1;

View File

@ -27,7 +27,7 @@ sub append {
$self->last_z->{$obj_id} = $print_z;
$self->gcode($self->gcode . $gcode);
$self->elapsed_time($self->elapsed_time + $self->gcodegen->elapsed_time);
$self->gcodegen->elapsed_time(0);
$self->gcodegen->set_elapsed_time(0);
return $return;
}
@ -56,23 +56,24 @@ sub flush {
Slic3r::debugf " fan = %d%%, speed = %d%%\n", $fan_speed, $speed_factor * 100;
if ($speed_factor < 1) {
$gcode =~ s/^(?=.*? [XY])(?=.*? E)(?!;_WIPE)(?<!;_BRIDGE_FAN_START\n)(G1 .*?F)(\d+(?:\.\d+)?)/
$gcode =~ s/^(?=.*?;_EXTRUDE_SET_SPEED)(?!.*?;_WIPE)(?<!;_BRIDGE_FAN_START\n)(G1\sF)(\d+(?:\.\d+)?)/
my $new_speed = $2 * $speed_factor;
$1 . sprintf("%.3f", $new_speed < $self->min_print_speed ? $self->min_print_speed : $new_speed)
/gexm;
}
}
$fan_speed = 0 if $self->layer_id < $self->config->disable_fan_first_layers;
$gcode = $self->gcodegen->set_fan($fan_speed) . $gcode;
$gcode = $self->gcodegen->writer->set_fan($fan_speed) . $gcode;
# bridge fan speed
if (!$self->config->cooling || $self->config->bridge_fan_speed == 0 || $self->layer_id < $self->config->disable_fan_first_layers) {
$gcode =~ s/^;_BRIDGE_FAN_(?:START|END)\n//gm;
} else {
$gcode =~ s/^;_BRIDGE_FAN_START\n/ $self->gcodegen->set_fan($self->config->bridge_fan_speed, 1) /gmex;
$gcode =~ s/^;_BRIDGE_FAN_END\n/ $self->gcodegen->set_fan($fan_speed, 1) /gmex;
$gcode =~ s/^;_BRIDGE_FAN_START\n/ $self->gcodegen->writer->set_fan($self->config->bridge_fan_speed, 1) /gmex;
$gcode =~ s/^;_BRIDGE_FAN_END\n/ $self->gcodegen->writer->set_fan($fan_speed, 1) /gmex;
}
$gcode =~ s/;_WIPE//g;
$gcode =~ s/;_EXTRUDE_SET_SPEED//g;
return $gcode;
}

View File

@ -1,238 +0,0 @@
package Slic3r::GCode::Layer;
use Moo;
use List::Util qw(first);
use Slic3r::Geometry qw(X Y unscale);
has 'print' => (is => 'ro', required => 1);
has 'gcodegen' => (is => 'ro', required => 1, handles => [qw(extruders)]);
has 'shift' => (is => 'ro', default => sub { [0,0] });
has 'spiralvase' => (is => 'lazy');
has 'vibration_limit' => (is => 'lazy');
has 'arc_fitting' => (is => 'lazy');
has 'skirt_done' => (is => 'rw', default => sub { {} }); # print_z => 1
has 'brim_done' => (is => 'rw');
has 'second_layer_things_done' => (is => 'rw');
has '_last_obj_copy' => (is => 'rw');
sub _build_spiralvase {
my $self = shift;
return $self->print->config->spiral_vase
? Slic3r::GCode::SpiralVase->new(config => $self->print->config)
: undef;
}
sub _build_vibration_limit {
my $self = shift;
return $self->print->config->vibration_limit
? Slic3r::GCode::VibrationLimit->new(config => $self->print->config)
: undef;
}
sub _build_arc_fitting {
my $self = shift;
return $self->print->config->gcode_arcs
? Slic3r::GCode::ArcFitting->new(config => $self->print->config)
: undef;
}
sub process_layer {
my $self = shift;
my ($layer, $object_copies) = @_;
my $gcode = "";
my $object = $layer->object;
$self->gcodegen->config->apply_object_config($object->config);
# check whether we're going to apply spiralvase logic
if (defined $self->spiralvase) {
$self->spiralvase->enable(
($layer->id > 0 || $self->print->config->brim_width == 0)
&& ($layer->id >= $self->print->config->skirt_height && $self->print->config->skirt_height != -1)
&& !defined(first { $_->config->bottom_solid_layers > $layer->id } @{$layer->regions})
&& !defined(first { @{$_->perimeters} > 1 } @{$layer->regions})
&& !defined(first { @{$_->fills} > 0 } @{$layer->regions})
);
}
# if we're going to apply spiralvase to this layer, disable loop clipping
$self->gcodegen->enable_loop_clipping(!defined $self->spiralvase || !$self->spiralvase->enable);
if (!$self->second_layer_things_done && $layer->id == 1) {
for my $extruder_id (sort keys %{$self->extruders}) {
my $extruder = $self->extruders->{$extruder_id};
$gcode .= $self->gcodegen->set_temperature($extruder->temperature, 0, $extruder->id)
if $extruder->temperature && $extruder->temperature != $extruder->first_layer_temperature;
}
$gcode .= $self->gcodegen->set_bed_temperature($self->print->config->bed_temperature)
if $self->print->config->bed_temperature && $self->print->config->bed_temperature != $self->print->config->first_layer_bed_temperature;
$self->second_layer_things_done(1);
}
# set new layer - this will change Z and force a retraction if retract_layer_change is enabled
$gcode .= $self->gcodegen->change_layer($layer);
$gcode .= $self->gcodegen->placeholder_parser->process($self->print->config->layer_gcode, {
layer_num => $self->gcodegen->layer->id,
}) . "\n" if $self->print->config->layer_gcode;
# extrude skirt
if (((values %{$self->skirt_done}) < $self->print->config->skirt_height || $self->print->config->skirt_height == -1)
&& !$self->skirt_done->{$layer->print_z}) {
$self->gcodegen->set_shift(@{$self->shift});
my @extruder_ids = sort keys %{$self->extruders};
$gcode .= $self->gcodegen->set_extruder($extruder_ids[0]);
# skip skirt if we have a large brim
if ($layer->id < $self->print->config->skirt_height || $self->print->config->skirt_height == -1) {
# distribute skirt loops across all extruders
my @skirt_loops = @{$self->print->skirt};
for my $i (0 .. $#skirt_loops) {
# when printing layers > 0 ignore 'min_skirt_length' and
# just use the 'skirts' setting; also just use the current extruder
last if ($layer->id > 0) && ($i >= $self->print->config->skirts);
my $extruder_id = $extruder_ids[($i/@extruder_ids) % @extruder_ids];
$gcode .= $self->gcodegen->set_extruder($extruder_id)
if $layer->id == 0;
$gcode .= $self->gcodegen->extrude_loop($skirt_loops[$i], 'skirt', $object->config->support_material_speed);
}
}
$self->skirt_done->{$layer->print_z} = 1;
$self->gcodegen->straight_once(1);
}
# extrude brim
if (!$self->brim_done) {
$gcode .= $self->gcodegen->set_extruder($self->print->objects->[0]->config->support_material_extruder-1);
$self->gcodegen->set_shift(@{$self->shift});
$gcode .= $self->gcodegen->extrude_loop($_, 'brim', $object->config->support_material_speed)
for @{$self->print->brim};
$self->brim_done(1);
$self->gcodegen->straight_once(1);
}
for my $copy (@$object_copies) {
$self->gcodegen->new_object(1) if ($self->_last_obj_copy // '') ne "$copy";
$self->_last_obj_copy("$copy");
$self->gcodegen->set_shift(map $self->shift->[$_] + unscale $copy->[$_], X,Y);
# extrude support material before other things because it might use a lower Z
# and also because we avoid travelling on other things when printing it
if ($layer->isa('Slic3r::Layer::Support')) {
if ($layer->support_interface_fills->count > 0) {
$gcode .= $self->gcodegen->set_extruder($object->config->support_material_interface_extruder-1);
$gcode .= $self->gcodegen->extrude_path($_, 'support material interface', $object->config->get_abs_value('support_material_interface_speed'))
for @{$layer->support_interface_fills->chained_path_from($self->gcodegen->last_pos, 0)};
}
if ($layer->support_fills->count > 0) {
$gcode .= $self->gcodegen->set_extruder($object->config->support_material_extruder-1);
$gcode .= $self->gcodegen->extrude_path($_, 'support material', $object->config->get_abs_value('support_material_speed'))
for @{$layer->support_fills->chained_path_from($self->gcodegen->last_pos, 0)};
}
}
# tweak region ordering to save toolchanges
my @region_ids = 0 .. ($self->print->region_count-1);
if ($self->gcodegen->multiple_extruders) {
my $last_extruder = $self->gcodegen->extruder;
my $best_region_id = first { $self->print->regions->[$_]->config->perimeter_extruder-1 eq $last_extruder } @region_ids;
@region_ids = ($best_region_id, grep $_ != $best_region_id, @region_ids) if $best_region_id;
}
foreach my $region_id (@region_ids) {
my $layerm = $layer->regions->[$region_id] or next;
my $region = $self->print->regions->[$region_id];
$self->gcodegen->config->apply_region_config($region->config);
# group extrusions by island
my @perimeters_by_island = map [], 0..$#{$layer->slices}; # slice idx => @perimeters
my @infill_by_island = map [], 0..$#{$layer->slices}; # slice idx => @fills
# NOTE: we assume $layer->slices was already ordered with chained_path()!
PERIMETER: foreach my $perimeter (@{$layerm->perimeters}) {
for my $i (0 .. $#{$layer->slices}-1) {
if ($layer->slices->[$i]->contour->contains_point($perimeter->first_point)) {
push @{ $perimeters_by_island[$i] }, $perimeter;
next PERIMETER;
}
}
push @{ $perimeters_by_island[-1] }, $perimeter; # optimization
}
FILL: foreach my $fill (@{$layerm->fills}) {
for my $i (0 .. $#{$layer->slices}-1) {
if ($layer->slices->[$i]->contour->contains_point($fill->first_point)) {
push @{ $infill_by_island[$i] }, $fill;
next FILL;
}
}
push @{ $infill_by_island[-1] }, $fill; # optimization
}
for my $i (0 .. $#{$layer->slices}) {
# give priority to infill if we were already using its extruder and it wouldn't
# be good for perimeters
if ($self->print->config->infill_first
|| ($self->gcodegen->multiple_extruders && $region->config->infill_extruder-1 == $self->gcodegen->extruder->id && $region->config->infill_extruder != $region->config->perimeter_extruder)) {
$gcode .= $self->_extrude_infill($infill_by_island[$i], $region);
$gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region);
} else {
$gcode .= $self->_extrude_perimeters($perimeters_by_island[$i], $region);
$gcode .= $self->_extrude_infill($infill_by_island[$i], $region);
}
}
}
}
# apply spiral vase post-processing if this layer contains suitable geometry
# (we must feed all the G-code into the post-processor, including the first
# bottom non-spiral layers otherwise it will mess with positions)
$gcode = $self->spiralvase->process_layer($gcode)
if defined $self->spiralvase;
# apply vibration limit if enabled
$gcode = $self->vibration_limit->process($gcode)
if $self->print->config->vibration_limit != 0;
# apply arc fitting if enabled
$gcode = $self->arc_fitting->process($gcode)
if $self->print->config->gcode_arcs;
return $gcode;
}
sub _extrude_perimeters {
my $self = shift;
my ($island_perimeters, $region) = @_;
return "" if !@$island_perimeters;
my $gcode = "";
$gcode .= $self->gcodegen->set_extruder($region->config->perimeter_extruder-1);
$gcode .= $self->gcodegen->extrude($_, 'perimeter') for @$island_perimeters;
return $gcode;
}
sub _extrude_infill {
my $self = shift;
my ($island_fills, $region) = @_;
return "" if !@$island_fills;
my $gcode = "";
$gcode .= $self->gcodegen->set_extruder($region->config->infill_extruder-1);
for my $fill (@$island_fills) {
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
$gcode .= $self->gcodegen->extrude($_, 'fill')
for @{$fill->chained_path_from($self->gcodegen->last_pos, 0)};
} else {
$gcode .= $self->gcodegen->extrude($fill, 'fill') ;
}
}
return $gcode;
}
1;

View File

@ -1,62 +0,0 @@
package Slic3r::GCode::PlaceholderParser;
use strict;
use warnings;
sub new {
# TODO: move this code to C++ constructor, remove this method
my ($class) = @_;
my $self = $class->_new;
$self->apply_env_variables;
$self->update_timestamp;
return $self;
}
sub apply_env_variables {
my ($self) = @_;
$self->_single_set($_, $ENV{$_}) for grep /^SLIC3R_/, keys %ENV;
}
sub update_timestamp {
my ($self) = @_;
my @lt = localtime; $lt[5] += 1900; $lt[4] += 1;
$self->_single_set('timestamp', sprintf '%04d%02d%02d-%02d%02d%02d', @lt[5,4,3,2,1,0]);
$self->_single_set('year', "$lt[5]");
$self->_single_set('month', "$lt[4]");
$self->_single_set('day', "$lt[3]");
$self->_single_set('hour', "$lt[2]");
$self->_single_set('minute', "$lt[1]");
$self->_single_set('second', "$lt[0]");
$self->_single_set('version', $Slic3r::VERSION);
}
# TODO: or this could be an alias
sub set {
my ($self, $key, $val) = @_;
$self->_single_set($key, $val);
}
sub process {
my ($self, $string, $extra) = @_;
# extra variables have priority over the stored ones
if ($extra) {
my $regex = join '|', keys %$extra;
$string =~ s/\[($regex)\]/$extra->{$1}/eg;
}
{
my $regex = join '|', @{$self->_single_keys};
$string =~ s/\[($regex)\]/$self->_single_get("$1")/eg;
}
{
my $regex = join '|', @{$self->_multiple_keys};
$string =~ s/\[($regex)\]/$self->_multiple_get("$1")/egx;
# unhandled indices are populated using the first value
$string =~ s/\[($regex)_\d+\]/$self->_multiple_get("$1")/egx;
}
return $string;
}
1;

View File

@ -0,0 +1,95 @@
package Slic3r::GCode::PressureRegulator;
use Moo;
has 'config' => (is => 'ro', required => 1);
has 'enable' => (is => 'rw', default => sub { 0 });
has 'reader' => (is => 'ro', default => sub { Slic3r::GCode::Reader->new });
has '_extrusion_axis' => (is => 'rw', default => sub { "E" });
has '_tool' => (is => 'rw', default => sub { 0 });
has '_last_print_F' => (is => 'rw', default => sub { 0 });
has '_advance' => (is => 'rw', default => sub { 0 }); # extra E injected
use Slic3r::Geometry qw(epsilon);
# Acknowledgements:
# The advance algorithm was proposed by Matthew Roberts.
# The initial work on this Slic3r feature was done by Luís Andrade (lluis)
sub BUILD {
my ($self) = @_;
$self->reader->apply_print_config($self->config);
$self->_extrusion_axis($self->config->get_extrusion_axis);
}
sub process {
my $self = shift;
my ($gcode, $flush) = @_;
my $new_gcode = "";
$self->reader->parse($gcode, sub {
my ($reader, $cmd, $args, $info) = @_;
if ($cmd =~ /^T(\d+)/) {
$self->_tool($1);
} elsif ($info->{extruding} && $info->{dist_XY} > 0) {
# This is a print move.
my $F = $args->{F} // $reader->F;
if ($F != $self->_last_print_F) {
# We are setting a (potentially) new speed, so we calculate the new advance amount.
# First calculate relative flow rate (mm of filament over mm of travel)
my $rel_flow_rate = $info->{dist_E} / $info->{dist_XY};
# Then calculate absolute flow rate (mm/sec of feedstock)
my $flow_rate = $rel_flow_rate * $F / 60;
# And finally calculate advance by using the user-configured K factor.
my $new_advance = $self->config->pressure_advance * ($flow_rate**2);
if (abs($new_advance - $self->_advance) > 1E-5) {
my $new_E = ($self->config->use_relative_e_distances ? 0 : $reader->E) + ($new_advance - $self->_advance);
$new_gcode .= sprintf "G1 %s%.5f F%.3f ; pressure advance\n",
$self->_extrusion_axis, $new_E, $self->_unretract_speed;
$new_gcode .= sprintf "G92 %s%.5f ; restore E\n", $self->_extrusion_axis, $reader->E
if !$self->config->use_relative_e_distances;
$self->_advance($new_advance);
}
$self->_last_print_F($F);
}
} elsif (($info->{retracting} || $cmd eq 'G10') && $self->_advance != 0) {
# We need to bring pressure to zero when retracting.
$new_gcode .= $self->_discharge($args->{F});
}
$new_gcode .= "$info->{raw}\n";
});
if ($flush) {
$new_gcode .= $self->_discharge;
}
return $new_gcode;
}
sub _discharge {
my ($self, $F) = @_;
my $new_E = ($self->config->use_relative_e_distances ? 0 : $self->reader->E) - $self->_advance;
my $gcode = sprintf "G1 %s%.5f F%.3f ; pressure discharge\n",
$self->_extrusion_axis, $new_E, $F // $self->_unretract_speed;
$gcode .= sprintf "G92 %s%.5f ; restore E\n", $self->_extrusion_axis, $self->reader->E
if !$self->config->use_relative_e_distances;
$self->_advance(0);
return $gcode;
}
sub _unretract_speed {
my ($self) = @_;
return $self->config->get_at('retract_speed', $self->_tool) * 60;
}
1;

View File

@ -1,19 +1,28 @@
package Slic3r::GCode::Reader;
use Moo;
has 'config' => (is => 'ro', default => sub { Slic3r::Config::GCode->new });
has 'X' => (is => 'rw', default => sub {0});
has 'Y' => (is => 'rw', default => sub {0});
has 'Z' => (is => 'rw', default => sub {0});
has 'E' => (is => 'rw', default => sub {0});
has 'F' => (is => 'rw', default => sub {0});
has '_extrusion_axis' => (is => 'rw', default => sub {"E"});
our $Verbose = 0;
my @AXES = qw(X Y Z E);
sub apply_print_config {
my ($self, $print_config) = @_;
$self->config->apply_static($print_config);
$self->_extrusion_axis($self->config->get_extrusion_axis);
}
sub clone {
my $self = shift;
return (ref $self)->new(
map { $_ => $self->$_ } (@AXES, 'F'),
map { $_ => $self->$_ } (@AXES, 'F', '_extrusion_axis'),
);
}
@ -32,10 +41,16 @@ sub parse {
$command //= '';
my %args = map { /([A-Z])(.*)/; ($1 => $2) } @args;
# convert extrusion axis
if (exists $args{ $self->_extrusion_axis }) {
$args{E} = $args{ $self->_extrusion_axis };
}
# check motion
if ($command =~ /^G[01]$/) {
foreach my $axis (@AXES) {
if (exists $args{$axis}) {
$self->$axis(0) if $axis eq 'E' && $self->config->use_relative_e_distances;
$info{"dist_$axis"} = $args{$axis} - $self->$axis;
$info{"new_$axis"} = $args{$axis};
} else {
@ -43,7 +58,7 @@ sub parse {
$info{"new_$axis"} = $self->$axis;
}
}
$info{dist_XY} = Slic3r::Geometry::unscale(Slic3r::Line->new_scale([0,0], [@info{qw(dist_X dist_Y)}])->length);
$info{dist_XY} = sqrt(($info{dist_X}**2) + ($info{dist_Y}**2));
if (exists $args{E}) {
if ($info{dist_E} > 0) {
$info{extruding} = 1;

View File

@ -7,6 +7,11 @@ has 'reader' => (is => 'ro', default => sub { Slic3r::GCode::Reader->new });
use Slic3r::Geometry qw(unscale);
sub BUILD {
my ($self) = @_;
$self->reader->apply_print_config($self->config);
}
sub process_layer {
my $self = shift;
my ($gcode) = @_;

View File

@ -3,7 +3,6 @@ use Moo;
extends 'Slic3r::GCode::Reader';
has 'config' => (is => 'ro', required => 1);
has '_min_time' => (is => 'lazy');
has '_last_dir' => (is => 'ro', default => sub { [0,0] });
has '_dir_time' => (is => 'ro', default => sub { [0,0] });
@ -11,7 +10,6 @@ has '_dir_time' => (is => 'ro', default => sub { [0,0] });
# inspired by http://hydraraptor.blogspot.it/2010/12/frequency-limit.html
use List::Util qw(max);
use Slic3r::Geometry qw(X Y);
sub _build__min_time {
my ($self) = @_;
@ -27,15 +25,16 @@ sub process {
my ($reader, $cmd, $args, $info) = @_;
if ($cmd eq 'G1' && $info->{dist_XY} > 0) {
my $point = Slic3r::Point->new($args->{X} // $reader->X, $args->{Y} // $reader->Y);
my $point = Slic3r::Pointf->new($args->{X} // $reader->X, $args->{Y} // $reader->Y);
my @dir = (
($point->x <=> $reader->X),
($point->y <=> $reader->Y), #$
);
my $time = $info->{dist_XY} / ($args->{F} // $reader->F); # in minutes
if ($time > 0) {
my @pause = ();
foreach my $axis (X,Y) {
foreach my $axis (0..$#dir) {
if ($dir[$axis] != 0 && $self->_last_dir->[$axis] != $dir[$axis]) {
if ($self->_last_dir->[$axis] != 0) {
# this axis is changing direction: check whether we need to pause

View File

@ -5,32 +5,40 @@ use utf8;
use File::Basename qw(basename);
use FindBin;
use List::Util qw(first);
use Slic3r::GUI::2DBed;
use Slic3r::GUI::AboutDialog;
use Slic3r::GUI::BedShapeDialog;
use Slic3r::GUI::BonjourBrowser;
use Slic3r::GUI::ConfigWizard;
use Slic3r::GUI::Controller;
use Slic3r::GUI::Controller::ManualControlDialog;
use Slic3r::GUI::Controller::PrinterPanel;
use Slic3r::GUI::MainFrame;
use Slic3r::GUI::Notifier;
use Slic3r::GUI::Plater;
use Slic3r::GUI::Plater::2D;
use Slic3r::GUI::Plater::2DToolpaths;
use Slic3r::GUI::Plater::3D;
use Slic3r::GUI::Plater::3DPreview;
use Slic3r::GUI::Plater::ObjectPartsPanel;
use Slic3r::GUI::Plater::ObjectCutDialog;
use Slic3r::GUI::Plater::ObjectPreviewDialog;
use Slic3r::GUI::Plater::ObjectSettingsDialog;
use Slic3r::GUI::Plater::OverrideSettingsPanel;
use Slic3r::GUI::Preferences;
use Slic3r::GUI::ProgressStatusBar;
use Slic3r::GUI::Projector;
use Slic3r::GUI::OptionsGroup;
use Slic3r::GUI::OptionsGroup::Field;
use Slic3r::GUI::SimpleTab;
use Slic3r::GUI::Tab;
our $have_OpenGL = eval "use Slic3r::GUI::PreviewCanvas; 1";
our $have_OpenGL = eval "use Slic3r::GUI::3DScene; 1";
our $have_LWP = eval "use LWP::UserAgent; 1";
use Wx 0.9901 qw(:bitmap :dialog :icon :id :misc :systemsettings :toplevelwindow
:filedialog);
use Wx::Event qw(EVT_IDLE);
:filedialog :font);
use Wx::Event qw(EVT_IDLE EVT_COMMAND);
use base 'Wx::App';
use constant FILE_WILDCARDS => {
@ -45,6 +53,8 @@ use constant FILE_WILDCARDS => {
use constant MODEL_WILDCARD => join '|', @{&FILE_WILDCARDS}{qw(known stl obj amf)};
our $datadir;
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
our $no_controller;
our $no_plater;
our $mode;
our $autosave;
@ -56,14 +66,25 @@ our $Settings = {
version_check => 1,
autocenter => 1,
background_processing => 1,
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
# By default, Prusa has the controller hidden.
no_controller => 1,
},
};
our $have_button_icons = &Wx::wxVERSION_STRING =~ / 2\.9\.[1-9]/;
our $have_button_icons = &Wx::wxVERSION_STRING =~ / (?:2\.9\.[1-9]|3\.)/;
our $small_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$small_font->SetPointSize(11) if !&Wx::wxMSW;
our $small_bold_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$small_bold_font->SetPointSize(11) if !&Wx::wxMSW;
$small_bold_font->SetWeight(wxFONTWEIGHT_BOLD);
our $medium_font = Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
$medium_font->SetPointSize(12);
our $grey = Wx::Colour->new(200,200,200);
our $VERSION_CHECK_EVENT : shared = Wx::NewEventType;
our $DLP_projection_screen;
sub OnInit {
my ($self) = @_;
@ -74,27 +95,33 @@ sub OnInit {
$self->{notifier} = Slic3r::GUI::Notifier->new;
# locate or create data directory
$datadir ||= Wx::StandardPaths::Get->GetUserDataDir;
$datadir = Slic3r::encode_path($datadir);
$datadir ||= Slic3r::decode_path(Wx::StandardPaths::Get->GetUserDataDir);
my $enc_datadir = Slic3r::encode_path($datadir);
Slic3r::debugf "Data directory: %s\n", $datadir;
# just checking for existence of $datadir is not enough: it may be an empty directory
# supplied as argument to --datadir; in that case we should still run the wizard
my $run_wizard = (-d $datadir && -e "$datadir/slic3r.ini") ? 0 : 1;
for ($datadir, "$datadir/print", "$datadir/filament", "$datadir/printer") {
mkdir or $self->fatal_error("Slic3r was unable to create its data directory at $_ (errno: $!).")
unless -d $_;
my $run_wizard = (-d $enc_datadir && -e "$enc_datadir/slic3r.ini") ? 0 : 1;
foreach my $dir ($enc_datadir, "$enc_datadir/print", "$enc_datadir/filament", "$enc_datadir/printer") {
next if -d $dir;
if (!mkdir $dir) {
my $error = "Slic3r was unable to create its data directory at $dir ($!).";
warn "$error\n";
fatal_error(undef, $error);
}
}
# load settings
my $last_version;
if (-f "$datadir/slic3r.ini") {
if (-f "$enc_datadir/slic3r.ini") {
my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") };
$Settings = $ini if $ini;
$last_version = $Settings->{_}{version};
$Settings->{_}{mode} ||= 'expert';
$Settings->{_}{autocenter} //= 1;
$Settings->{_}{background_processing} //= 1;
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
$Settings->{_}{no_controller} //= 1;
}
$Settings->{_}{version} = $Slic3r::VERSION;
$self->save_settings;
@ -102,11 +129,29 @@ sub OnInit {
# application frame
Wx::Image::AddHandler(Wx::PNGHandler->new);
$self->{mainframe} = my $frame = Slic3r::GUI::MainFrame->new(
mode => $mode // $Settings->{_}{mode},
no_plater => $no_plater,
mode => $mode // $Settings->{_}{mode},
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
no_controller => $no_controller // $Settings->{_}{no_controller},
no_plater => $no_plater,
);
$self->SetTopWindow($frame);
# load init bundle
{
my @dirs = ($FindBin::Bin);
if (&Wx::wxMAC) {
push @dirs, qw();
} elsif (&Wx::wxMSW) {
push @dirs, qw();
}
my $init_bundle = first { -e $_ } map "$_/.init_bundle.ini", @dirs;
if ($init_bundle) {
Slic3r::debugf "Loading config bundle from %s\n", $init_bundle;
$self->{mainframe}->load_configbundle($init_bundle, 1);
$run_wizard = 0;
}
}
if (!$run_wizard && (!defined $last_version || $last_version ne $Slic3r::VERSION)) {
# user was running another Slic3r version on this computer
if (!defined $last_version || $last_version =~ /^0\./) {
@ -134,6 +179,25 @@ sub OnInit {
}
});
EVT_COMMAND($self, -1, $VERSION_CHECK_EVENT, sub {
my ($self, $event) = @_;
my ($success, $response, $manual_check) = @{$event->GetData};
if ($success) {
if ($response =~ /^obsolete ?= ?([a-z0-9.-]+,)*\Q$Slic3r::VERSION\E(?:,|$)/) {
my $res = Wx::MessageDialog->new(undef, "A new version is available. Do you want to open the Slic3r website now?",
'Update', wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_INFORMATION | wxICON_ERROR)->ShowModal;
Wx::LaunchDefaultBrowser('http://slic3r.org/') if $res == wxID_YES;
} else {
Slic3r::GUI::show_info(undef, "You're using the latest version. No updates are available.") if $manual_check;
}
$Settings->{_}{last_version_check} = time();
$self->save_settings;
} else {
Slic3r::GUI::show_error(undef, "Failed to check for updates. Try later.") if $manual_check;
}
});
return 1;
}
@ -160,22 +224,19 @@ sub catch_error {
# static method accepting a wxWindow object as first parameter
sub show_error {
my $self = shift;
my ($message) = @_;
Wx::MessageDialog->new($self, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
my ($parent, $message) = @_;
Wx::MessageDialog->new($parent, $message, 'Error', wxOK | wxICON_ERROR)->ShowModal;
}
# static method accepting a wxWindow object as first parameter
sub show_info {
my $self = shift;
my ($message, $title) = @_;
Wx::MessageDialog->new($self, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
my ($parent, $message, $title) = @_;
Wx::MessageDialog->new($parent, $message, $title || 'Notice', wxOK | wxICON_INFORMATION)->ShowModal;
}
# static method accepting a wxWindow object as first parameter
sub fatal_error {
my $self = shift;
$self->show_error(@_);
show_error(@_);
exit 1;
}
@ -212,8 +273,10 @@ sub presets {
my ($self, $section) = @_;
my %presets = ();
opendir my $dh, "$Slic3r::GUI::datadir/$section" or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n";
opendir my $dh, Slic3r::encode_path("$Slic3r::GUI::datadir/$section")
or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n";
foreach my $file (grep /\.ini$/i, readdir $dh) {
$file = Slic3r::decode_path($file);
my $name = basename($file);
$name =~ s/\.ini$//;
$presets{$name} = "$Slic3r::GUI::datadir/$section/$file";
@ -227,11 +290,11 @@ sub have_version_check {
my ($self) = @_;
# return an explicit 0
return ($Slic3r::have_threads && $Slic3r::build && eval "use LWP::UserAgent; 1") || 0;
return ($Slic3r::have_threads && $Slic3r::build && $have_LWP) || 0;
}
sub check_version {
my ($self, %p) = @_;
my ($self, $manual_check) = @_;
Slic3r::debugf "Checking for updates...\n";
@ -240,19 +303,9 @@ sub check_version {
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $response = $ua->get('http://slic3r.org/updatecheck');
if ($response->is_success) {
if ($response->decoded_content =~ /^obsolete ?= ?([a-z0-9.-]+,)*\Q$Slic3r::VERSION\E(?:,|$)/) {
my $res = Wx::MessageDialog->new(undef, "A new version is available. Do you want to open the Slic3r website now?",
'Update', wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_INFORMATION | wxICON_ERROR)->ShowModal;
Wx::LaunchDefaultBrowser('http://slic3r.org/') if $res == wxID_YES;
} else {
Slic3r::GUI::show_info(undef, "You're using the latest version. No updates are available.") if $p{manual};
}
$Settings->{_}{last_version_check} = time();
$self->save_settings;
} else {
Slic3r::GUI::show_error(undef, "Failed to check for updates. Try later.") if $p{manual};
}
Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $VERSION_CHECK_EVENT,
threads::shared::shared_clone([ $response->is_success, $response->decoded_content, $manual_check ])));
Slic3r::thread_cleanup();
})->detach;
}
@ -278,7 +331,7 @@ sub open_model {
$dialog->Destroy;
return;
}
my @input_files = $dialog->GetPaths;
my @input_files = map Slic3r::decode_path($_), $dialog->GetPaths;
$dialog->Destroy;
return @input_files;
@ -289,4 +342,29 @@ sub CallAfter {
push @cb, $cb;
}
sub scan_serial_ports {
my ($self) = @_;
my @ports = ();
if ($^O eq 'MSWin32') {
# Windows
if (eval "use Win32::TieRegistry; 1") {
my $ts = Win32::TieRegistry->new("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM",
{ Access => 'KEY_READ' });
if ($ts) {
# when no serial ports are available, the registry key doesn't exist and
# TieRegistry->new returns undef
$ts->Tie(\my %reg);
push @ports, sort values %reg;
}
}
} else {
# UNIX and OS X
push @ports, glob '/dev/{ttyUSB,ttyACM,tty.,cu.,rfcomm}*';
}
return grep !/Bluetooth|FireFly/, @ports;
}
1;

196
lib/Slic3r/GUI/2DBed.pm Normal file
View File

@ -0,0 +1,196 @@
package Slic3r::GUI::2DBed;
use strict;
use warnings;
use List::Util qw(min max);
use Slic3r::Geometry qw(PI X Y unscale deg2rad);
use Slic3r::Geometry::Clipper qw(intersection_pl);
use Wx qw(:misc :pen :brush :font wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_PAINT EVT_MOUSE_EVENTS);
use base qw(Wx::Panel Class::Accessor);
__PACKAGE__->mk_accessors(qw(bed_shape interactive pos _scale_factor _shift on_move));
sub new {
my ($class, $parent, $bed_shape) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [250,-1], wxTAB_TRAVERSAL);
$self->bed_shape($bed_shape // []);
EVT_PAINT($self, \&_repaint);
EVT_MOUSE_EVENTS($self, \&_mouse_event);
return $self;
}
sub _repaint {
my ($self) = @_;
my $dc = Wx::AutoBufferedPaintDC->new($self);
my ($cw, $ch) = $self->GetSizeWH;
return if $cw == 0; # when canvas is not rendered yet, size is 0,0
# turn $cw and $ch from sizes to max coordinates
$cw--;
$ch--;
my $cbb = Slic3r::Geometry::BoundingBoxf->new_from_points([
Slic3r::Pointf->new(0, 0),
Slic3r::Pointf->new($cw, $ch),
]);
# leave space for origin point
$cbb->set_x_min($cbb->x_min + 2);
$cbb->set_y_max($cbb->y_max - 2);
# leave space for origin label
$cbb->set_y_max($cbb->y_max - 10);
# read new size
($cw, $ch) = @{$cbb->size};
my $ccenter = $cbb->center;
# get bounding box of bed shape in G-code coordinates
my $bed_shape = $self->bed_shape;
my $bed_polygon = Slic3r::Polygon->new_scale(@$bed_shape);
my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($bed_shape);
$bb->merge_point(Slic3r::Pointf->new(0,0)); # origin needs to be in the visible area
my ($bw, $bh) = @{$bb->size};
my $bcenter = $bb->center;
# calculate the scaling factor for fitting bed shape in canvas area
my $sfactor = min($cw/$bw, $ch/$bh);
my $shift = Slic3r::Pointf->new(
$ccenter->x - $bcenter->x * $sfactor,
$ccenter->y - $bcenter->y * $sfactor, #-
);
$self->_scale_factor($sfactor);
$self->_shift(Slic3r::Pointf->new(
$shift->x + $cbb->x_min,
$shift->y - ($cbb->y_max-$self->GetSize->GetHeight), #++
));
# draw bed fill
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxSOLID));
$dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
}
# draw grid
{
my $step = 10; # 1cm grid
my @polylines = ();
for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
push @polylines, Slic3r::Polyline->new_scale([$x, $bb->y_min], [$x, $bb->y_max]);
}
for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
push @polylines, Slic3r::Polyline->new_scale([$bb->x_min, $y], [$bb->x_max, $y]);
}
@polylines = @{intersection_pl(\@polylines, [$bed_polygon])};
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(230,230,230), 1, wxSOLID));
$dc->DrawLine(map @{$self->to_pixels([map unscale($_), @$_])}, @$_[0,-1]) for @polylines;
}
# draw bed contour
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxTRANSPARENT));
$dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
}
my $origin_px = $self->to_pixels(Slic3r::Pointf->new(0,0));
# draw axes
{
my $axes_len = 50;
my $arrow_len = 6;
my $arrow_angle = deg2rad(45);
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(255,0,0), 2, wxSOLID)); # red
my $x_end = Slic3r::Pointf->new($origin_px->[X] + $axes_len, $origin_px->[Y]);
$dc->DrawLine(@$origin_px, @$x_end);
foreach my $angle (-$arrow_angle, +$arrow_angle) {
my $end = $x_end->clone;
$end->translate(-$arrow_len, 0);
$end->rotate($angle, $x_end);
$dc->DrawLine(@$x_end, @$end);
}
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,255,0), 2, wxSOLID)); # green
my $y_end = Slic3r::Pointf->new($origin_px->[X], $origin_px->[Y] - $axes_len);
$dc->DrawLine(@$origin_px, @$y_end);
foreach my $angle (-$arrow_angle, +$arrow_angle) {
my $end = $y_end->clone;
$end->translate(0, +$arrow_len);
$end->rotate($angle, $y_end);
$dc->DrawLine(@$y_end, @$end);
}
}
# draw origin
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(0,0,0), wxSOLID));
$dc->DrawCircle(@$origin_px, 3);
$dc->SetTextForeground(Wx::Colour->new(0,0,0));
$dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
$dc->DrawText("(0,0)", $origin_px->[X] + 1, $origin_px->[Y] + 2);
}
# draw current position
if (defined $self->pos) {
my $pos_px = $self->to_pixels($self->pos);
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(200,0,0), 2, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(200,0,0), wxTRANSPARENT));
$dc->DrawCircle(@$pos_px, 5);
$dc->DrawLine($pos_px->[X]-15, $pos_px->[Y], $pos_px->[X]+15, $pos_px->[Y]);
$dc->DrawLine($pos_px->[X], $pos_px->[Y]-15, $pos_px->[X], $pos_px->[Y]+15);
}
}
sub _mouse_event {
my ($self, $event) = @_;
return if !$self->interactive;
my $pos = $event->GetPosition;
my $point = $self->to_units([ $pos->x, $pos->y ]); #]]
if ($event->LeftDown || $event->Dragging) {
$self->on_move->($point) if $self->on_move;
$self->Refresh;
}
}
# convert G-code coordinates into pixels
sub to_pixels {
my ($self, $point) = @_;
my $p = Slic3r::Pointf->new(@$point);
$p->scale($self->_scale_factor);
$p->translate(@{$self->_shift});
return [$p->x, $self->GetSize->GetHeight - $p->y]; #]]
}
# convert pixels into G-code coordinates
sub to_units {
my ($self, $point) = @_;
my $p = Slic3r::Pointf->new(
$point->[X],
$self->GetSize->GetHeight - $point->[Y],
);
$p->translate(@{$self->_shift->negative});
$p->scale(1/$self->_scale_factor);
return $p;
}
sub set_pos {
my ($self, $pos) = @_;
$self->pos($pos);
$self->Refresh;
}
1;

1305
lib/Slic3r/GUI/3DScene.pm Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,8 @@ use strict;
use warnings;
use utf8;
use Wx qw(:font :html :misc :sizer :systemsettings);
use Wx::Event qw(EVT_HTML_LINK_CLICKED);
use Wx qw(:font :html :misc :dialog :sizer :systemsettings :frame :id);
use Wx::Event qw(EVT_HTML_LINK_CLICKED EVT_LEFT_DOWN EVT_BUTTON);
use Wx::Print;
use Wx::Html;
use base 'Wx::Dialog';
@ -12,7 +12,7 @@ use base 'Wx::Dialog';
sub new {
my $class = shift;
my ($parent) = @_;
my $self = $class->SUPER::new($parent, -1, 'About Slic3r', wxDefaultPosition, [600, 270]);
my $self = $class->SUPER::new($parent, -1, 'About Slic3r', wxDefaultPosition, [600, 340], wxCAPTION);
$self->SetBackgroundColour(Wx::wxWHITE);
my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
@ -47,7 +47,7 @@ sub new {
'<html>' .
'<body bgcolor="#ffffff" link="#808080">' .
'<font color="#808080">' .
'Copyright &copy; 2011-2014 Alessandro Ranellucci. <br />' .
'Copyright &copy; 2011-2016 Alessandro Ranellucci. <br />' .
'<a href="http://slic3r.org/">Slic3r</a> is licensed under the ' .
'<a href="http://www.gnu.org/licenses/agpl-3.0.html">GNU Affero General Public License, version 3</a>.' .
'<br /><br /><br />' .
@ -65,7 +65,18 @@ sub new {
$html->SetPage($text);
$vsizer->Add($html, 1, wxEXPAND | wxALIGN_LEFT | wxRIGHT | wxBOTTOM, 20);
EVT_HTML_LINK_CLICKED($self, $html, \&link_clicked);
my $buttons = $self->CreateStdDialogButtonSizer(wxOK);
$self->SetEscapeId(wxID_CLOSE);
EVT_BUTTON($self, wxID_CLOSE, sub {
$self->EndModal(wxID_CLOSE);
$self->Close;
});
$vsizer->Add($buttons, 0, wxEXPAND | wxRIGHT | wxBOTTOM, 3);
EVT_LEFT_DOWN($self, sub { $self->Close });
EVT_LEFT_DOWN($logo, sub { $self->Close });
return $self;
}
@ -85,7 +96,7 @@ sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{logo} = Wx::Bitmap->new("$Slic3r::var/Slic3r_192px.png", wxBITMAP_TYPE_PNG);
$self->{logo} = Wx::Bitmap->new($Slic3r::var->("Slic3r_192px.png"), wxBITMAP_TYPE_PNG);
$self->SetMinSize(Wx::Size->new($self->{logo}->GetWidth, $self->{logo}->GetHeight));
EVT_PAINT($self, \&repaint);

View File

@ -41,10 +41,10 @@ sub GetValue {
package Slic3r::GUI::BedShapePanel;
use List::Util qw(min max sum first);
use Scalar::Util qw(looks_like_number);
use Slic3r::Geometry qw(PI X Y scale unscale scaled_epsilon deg2rad);
use Slic3r::Geometry::Clipper qw(intersection_pl);
use Wx qw(:font :id :misc :sizer :choicebook :filedialog :pen :brush wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_CLOSE EVT_CHOICEBOOK_PAGE_CHANGED EVT_BUTTON EVT_PAINT);
use Wx::Event qw(EVT_CLOSE EVT_CHOICEBOOK_PAGE_CHANGED EVT_BUTTON);
use base 'Wx::Panel';
use constant SHAPE_RECTANGULAR => 0;
@ -82,7 +82,6 @@ sub new {
tooltip => 'Distance of the 0,0 G-code coordinate from the front left corner of the rectangle.',
default => [0,0],
));
$optgroup->on_change->($_) for qw(rect_size rect_origin); # set defaults
}
{
my $optgroup = $self->_init_shape_options_page('Circular');
@ -94,7 +93,6 @@ sub new {
sidetext => 'mm',
default => 200,
));
$optgroup->on_change->($_) for qw(diameter); # set defaults
}
{
my $optgroup = $self->_init_shape_options_page('Custom');
@ -115,10 +113,7 @@ sub new {
});
# right pane with preview canvas
my $canvas = $self->{canvas} = Wx::Panel->new($self, -1, wxDefaultPosition, [250,-1], wxTAB_TRAVERSAL);
EVT_PAINT($canvas, sub {
$self->_repaint_canvas;
});
my $canvas = $self->{canvas} = Slic3r::GUI::2DBed->new($self);
# main sizer
my $top_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
@ -162,6 +157,7 @@ sub _set_shape {
my $optgroup = $self->{optgroups}[SHAPE_RECTANGULAR];
$optgroup->set_value('rect_size', [ $x_max-$x_min, $y_max-$y_min ]);
$optgroup->set_value('rect_origin', $origin);
$self->_update_shape;
return;
}
}
@ -172,16 +168,18 @@ sub _set_shape {
my $center = $polygon->bounding_box->center;
my @vertex_distances = map $center->distance_to($_), @$polygon;
my $avg_dist = sum(@vertex_distances)/@vertex_distances;
if (!defined first { abs($_ - $avg_dist) > scaled_epsilon } @vertex_distances) {
if (!defined first { abs($_ - $avg_dist) > 10*scaled_epsilon } @vertex_distances) {
# all vertices are equidistant to center
$self->{shape_options_book}->SetSelection(SHAPE_CIRCULAR);
my $optgroup = $self->{optgroups}[SHAPE_CIRCULAR];
$optgroup->set_value('diameter', sprintf("%.0f", unscale($avg_dist*2)));
$self->_update_shape;
return;
}
}
$self->{shape_options_book}->SetSelection(SHAPE_CUSTOM);
$self->_update_shape;
}
sub _update_shape {
@ -189,38 +187,39 @@ sub _update_shape {
my $page_idx = $self->{shape_options_book}->GetSelection;
if ($page_idx == SHAPE_RECTANGULAR) {
return if grep !defined($self->{"_$_"}), qw(rect_size rect_origin); # not loaded yet
my ($x, $y) = @{$self->{_rect_size}};
return if !$x || !$y; # empty strings
my $rect_size = $self->{optgroups}[SHAPE_RECTANGULAR]->get_value('rect_size');
my $rect_origin = $self->{optgroups}[SHAPE_RECTANGULAR]->get_value('rect_origin');
my ($x, $y) = @$rect_size;
return if !looks_like_number($x) || !looks_like_number($y); # empty strings or '-' or other things
my ($x0, $y0) = (0,0);
my ($x1, $y1) = ($x,$y);
{
my ($dx, $dy) = @{$self->{_rect_origin}};
return if $dx eq '' || $dy eq ''; # empty strings
my ($dx, $dy) = @$rect_origin;
return if !looks_like_number($dx) || !looks_like_number($dy); # empty strings or '-' or other things
$x0 -= $dx;
$x1 -= $dx;
$y0 -= $dy;
$y1 -= $dy;
}
$self->{bed_shape} = [
$self->{canvas}->bed_shape([
[$x0,$y0],
[$x1,$y0],
[$x1,$y1],
[$x0,$y1],
];
]);
} elsif ($page_idx == SHAPE_CIRCULAR) {
return if grep !defined($self->{"_$_"}), qw(diameter); # not loaded yet
return if !$self->{_diameter};
my $r = $self->{_diameter}/2;
my $diameter = $self->{optgroups}[SHAPE_CIRCULAR]->get_value('diameter');
return if !$diameter;
my $r = $diameter/2;
my $twopi = 2*PI;
my $edges = 60;
my $polygon = Slic3r::Polygon->new_scale(
map [ $r * cos $_, $r * sin $_ ],
map { $twopi/$edges*$_ } 1..$edges
);
$self->{bed_shape} = [
$self->{canvas}->bed_shape([
map [ unscale($_->x), unscale($_->y) ], @$polygon #))
];
]);
}
$self->{on_change}->();
@ -232,129 +231,6 @@ sub _update_preview {
$self->{canvas}->Refresh if $self->{canvas};
}
sub _repaint_canvas {
my ($self) = @_;
my $canvas = $self->{canvas};
my $dc = Wx::PaintDC->new($canvas);
my ($cw, $ch) = $canvas->GetSizeWH;
return if $cw == 0; # when canvas is not rendered yet, size is 0,0
# turn $cw and $ch from sizes to max coordinates
$cw--;
$ch--;
my $cbb = Slic3r::Geometry::BoundingBoxf->new_from_points([
Slic3r::Pointf->new(0, 0),
Slic3r::Pointf->new($cw, $ch),
]);
# leave space for origin point
$cbb->set_x_min($cbb->x_min + 2);
$cbb->set_y_max($cbb->y_max - 2);
# leave space for origin label
$cbb->set_y_max($cbb->y_max - 10);
# read new size
($cw, $ch) = @{$cbb->size};
my $ccenter = $cbb->center;
# get bounding box of bed shape in G-code coordinates
my $bed_shape = $self->{bed_shape};
my $bed_polygon = Slic3r::Polygon->new_scale(@$bed_shape);
my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($bed_shape);
$bb->merge_point(Slic3r::Pointf->new(0,0)); # origin needs to be in the visible area
my ($bw, $bh) = @{$bb->size};
my $bcenter = $bb->center;
# calculate the scaling factor for fitting bed shape in canvas area
my $sfactor = min($cw/$bw, $ch/$bh);
my $shift = [
$ccenter->x - $bcenter->x * $sfactor,
$ccenter->y - $bcenter->y * $sfactor, #-
];
# prepare function to convert G-code coordinates into pixels
my $to_pixel = sub {
my ($point) = @_;
my $p = Slic3r::Pointf->new(@$point);
$p->scale($sfactor);
$p->translate(@$shift);
return [ $p->x + $cbb->x_min, $ch-$p->y + $cbb->y_min ]; #++
};
# draw bed fill
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxSOLID));
$dc->DrawPolygon([ map $to_pixel->($_), @$bed_shape ], 0, 0);
}
# draw grid
{
my $step = 10; # 1cm grid
my @polylines = ();
for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
push @polylines, Slic3r::Polyline->new_scale([$x, $bb->y_min], [$x, $bb->y_max]);
}
for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
push @polylines, Slic3r::Polyline->new_scale([$bb->x_min, $y], [$bb->x_max, $y]);
}
@polylines = @{intersection_pl(\@polylines, [$bed_polygon])};
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(230,230,230), 1, wxSOLID));
$dc->DrawLine(map @{$to_pixel->([map unscale($_), @$_])}, @$_) for @polylines;
}
# draw bed contour
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxTRANSPARENT));
$dc->DrawPolygon([ map $to_pixel->($_), @$bed_shape ], 0, 0);
}
my $origin_px = $to_pixel->(Slic3r::Pointf->new(0,0));
# draw axes
{
my $axes_len = 50;
my $arrow_len = 6;
my $arrow_angle = deg2rad(45);
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(255,0,0), 2, wxSOLID)); # red
my $x_end = Slic3r::Pointf->new($origin_px->[X] + $axes_len, $origin_px->[Y]);
$dc->DrawLine(@$origin_px, @$x_end);
foreach my $angle (-$arrow_angle, +$arrow_angle) {
my $end = $x_end->clone;
$end->translate(-$arrow_len, 0);
$end->rotate($angle, $x_end);
$dc->DrawLine(@$x_end, @$end);
}
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,255,0), 2, wxSOLID)); # green
my $y_end = Slic3r::Pointf->new($origin_px->[X], $origin_px->[Y] - $axes_len);
$dc->DrawLine(@$origin_px, @$y_end);
foreach my $angle (-$arrow_angle, +$arrow_angle) {
my $end = $y_end->clone;
$end->translate(0, +$arrow_len);
$end->rotate($angle, $y_end);
$dc->DrawLine(@$y_end, @$end);
}
}
# draw origin
{
$dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(Wx::Colour->new(0,0,0), wxSOLID));
$dc->DrawCircle(@$origin_px, 3);
$dc->SetTextForeground(Wx::Colour->new(0,0,0));
$dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
$dc->DrawText("(0,0)", $origin_px->[X] + 1, $origin_px->[Y] + 2);
}
}
sub _init_shape_options_page {
my ($self, $title) = @_;
@ -366,7 +242,7 @@ sub _init_shape_options_page {
label_width => 100,
on_change => sub {
my ($opt_id) = @_;
$self->{"_$opt_id"} = $optgroup->get_value($opt_id);
#$self->{"_$opt_id"} = $optgroup->get_value($opt_id);
$self->_update_shape;
},
);
@ -384,7 +260,7 @@ sub _load_stl {
$dialog->Destroy;
return;
}
my $input_file = $dialog->GetPaths;
my $input_file = Slic3r::decode_path($dialog->GetPaths);
$dialog->Destroy;
my $model = Slic3r::Model->read_from_file($input_file);
@ -401,12 +277,12 @@ sub _load_stl {
}
my $polygon = $expolygons->[0]->contour;
$self->{bed_shape} = [ map [ unscale($_->x), unscale($_->y) ], @$polygon ]; #))
$self->{canvas}->bed_shape([ map [ unscale($_->x), unscale($_->y) ], @$polygon ]); #))
}
sub GetValue {
my ($self) = @_;
return $self->{bed_shape};
return $self->{canvas}->bed_shape;
}
1;

View File

@ -0,0 +1,55 @@
package Slic3r::GUI::BonjourBrowser;
use strict;
use warnings;
use utf8;
use Wx qw(:dialog :id :misc :sizer :choicebook wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_CLOSE);
use base 'Wx::Dialog';
sub new {
my $class = shift;
my ($parent) = @_;
my $self = $class->SUPER::new($parent, -1, "Device Browser", wxDefaultPosition, [350,700], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
# look for devices
eval "use Net::Bonjour; 1";
my $res = Net::Bonjour->new('http');
$res->discover;
$self->{devices} = [ $res->entries ];
# label
my $text = Wx::StaticText->new($self, -1, "Choose an OctoPrint device in your network:", wxDefaultPosition, wxDefaultSize);
# selector
$self->{choice} = my $choice = Wx::Choice->new($self, -1, wxDefaultPosition, wxDefaultSize,
[ map $_->name, @{$self->{devices}} ]);
my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
$main_sizer->Add($text, 1, wxEXPAND | wxALL, 10);
$main_sizer->Add($choice, 1, wxEXPAND | wxALL, 10);
$main_sizer->Add($self->CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND);
$self->SetSizer($main_sizer);
$self->SetMinSize($self->GetSize);
$main_sizer->SetSizeHints($self);
# needed to actually free memory
EVT_CLOSE($self, sub {
$self->EndModal(wxID_OK);
$self->Destroy;
});
return $self;
}
sub GetValue {
my ($self) = @_;
return $self->{devices}[ $self->{choice}->GetSelection ]->address;
}
sub GetPort {
my ($self) = @_;
return $self->{devices}[ $self->{choice}->GetSelection ]->port;
}
1;

View File

@ -87,11 +87,11 @@ sub new {
push @{$self->{titles}}, $title;
$self->{own_index} = 0;
$self->{bullets}->{before} = Wx::Bitmap->new("$Slic3r::var/bullet_black.png", wxBITMAP_TYPE_PNG);
$self->{bullets}->{own} = Wx::Bitmap->new("$Slic3r::var/bullet_blue.png", wxBITMAP_TYPE_PNG);
$self->{bullets}->{after} = Wx::Bitmap->new("$Slic3r::var/bullet_white.png", wxBITMAP_TYPE_PNG);
$self->{bullets}->{before} = Wx::Bitmap->new($Slic3r::var->("bullet_black.png"), wxBITMAP_TYPE_PNG);
$self->{bullets}->{own} = Wx::Bitmap->new($Slic3r::var->("bullet_blue.png"), wxBITMAP_TYPE_PNG);
$self->{bullets}->{after} = Wx::Bitmap->new($Slic3r::var->("bullet_white.png"), wxBITMAP_TYPE_PNG);
$self->{background} = Wx::Bitmap->new("$Slic3r::var/Slic3r_192px_transparent.png", wxBITMAP_TYPE_PNG);
$self->{background} = Wx::Bitmap->new($Slic3r::var->("Slic3r_192px_transparent.png"), wxBITMAP_TYPE_PNG);
$self->SetMinSize(Wx::Size->new($self->{background}->GetWidth, $self->{background}->GetHeight));
EVT_PAINT($self, \&repaint);
@ -199,8 +199,7 @@ sub append_option {
my ($full_key) = @_;
# populate repository with the factory default
my $opt_key = $full_key;
$opt_key =~ s/#.+//;
my ($opt_key, $opt_index) = split /#/, $full_key, 2;
$self->config->apply(Slic3r::Config->new_from_defaults($opt_key));
# draw the control
@ -208,9 +207,9 @@ sub append_option {
parent => $self,
title => '',
config => $self->config,
options => [$full_key],
full_labels => 1,
);
$optgroup->append_single_option_line($opt_key, $opt_index);
$self->{vsizer}->Add($optgroup->sizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
}

View File

@ -0,0 +1,193 @@
package Slic3r::GUI::Controller;
use strict;
use warnings;
use utf8;
use Wx qw(wxTheApp :frame :id :misc :sizer :bitmap :button :icon :dialog);
use Wx::Event qw(EVT_CLOSE EVT_LEFT_DOWN EVT_MENU);
use base qw(Wx::ScrolledWindow Class::Accessor);
__PACKAGE__->mk_accessors(qw(_selected_printer_preset));
our @ConfigOptions = qw(bed_shape serial_port serial_speed);
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [600,350]);
$self->SetScrollbars(0, 1, 0, 1);
$self->{sizer} = my $sizer = Wx::BoxSizer->new(wxVERTICAL);
# warning to show when there are no printers configured
{
$self->{text_no_printers} = Wx::StaticText->new($self, -1,
"No printers were configured for USB/serial control.",
wxDefaultPosition, wxDefaultSize);
$self->{sizer}->Add($self->{text_no_printers}, 0, wxTOP | wxLEFT, 30);
}
# button for adding new printer panels
{
my $btn = $self->{btn_add} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("add.png"), wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize, Wx::wxBORDER_NONE);
$btn->SetToolTipString("Add printer…")
if $btn->can('SetToolTipString');
EVT_LEFT_DOWN($btn, sub {
my $menu = Wx::Menu->new;
my %presets = wxTheApp->presets('printer');
# remove printers that already exist
my @panels = $self->print_panels;
delete $presets{$_} for map $_->printer_name, @panels;
foreach my $preset_name (sort keys %presets) {
my $config = Slic3r::Config->load($presets{$preset_name});
next if !$config->serial_port;
my $id = &Wx::NewId();
$menu->Append($id, $preset_name);
EVT_MENU($menu, $id, sub {
$self->add_printer($preset_name, $config);
});
}
$self->PopupMenu($menu, $btn->GetPosition);
$menu->Destroy;
});
$self->{sizer}->Add($btn, 0, wxTOP | wxLEFT, 10);
}
$self->SetSizer($sizer);
$self->SetMinSize($self->GetSize);
#$sizer->SetSizeHints($self);
EVT_CLOSE($self, sub {
my (undef, $event) = @_;
if ($event->CanVeto) {
foreach my $panel ($self->print_panels) {
if ($panel->printing) {
my $confirm = Wx::MessageDialog->new(
$self, "Printer '" . $panel->printer_name . "' is printing.\n\nDo you want to stop printing?",
'Unfinished Print', wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION,
);
if ($confirm->ShowModal == wxID_NO) {
$event->Veto;
return;
}
}
}
}
foreach my $panel ($self->print_panels) {
$panel->disconnect;
}
$event->Skip;
});
$self->Layout;
return $self;
}
sub OnActivate {
my ($self) = @_;
# get all available presets
my %presets = ();
{
my %all = wxTheApp->presets('printer');
my %configs = map { my $name = $_; $name => Slic3r::Config->load($all{$name}) } keys %all;
%presets = map { $_ => $configs{$_} } grep $configs{$_}->serial_port, keys %all;
}
# decide which ones we want to keep
my %active = ();
# keep the ones that are currently connected or have jobs in queue
$active{$_} = 1 for map $_->printer_name,
grep { $_->is_connected || @{$_->jobs} > 0 }
$self->print_panels;
if (%presets) {
# if there are no active panels, use sensible defaults
if (!%active && keys %presets <= 2) {
# if only one or two presets exist, load them
$active{$_} = 1 for keys %presets;
}
if (!%active) {
# enable printers whose port is available
my %ports = map { $_ => 1 } wxTheApp->scan_serial_ports;
$active{$_} = 1
for grep exists $ports{$presets{$_}->serial_port}, keys %presets;
}
if (!%active && $self->_selected_printer_preset) {
# enable currently selected printer if it is configured
$active{$self->_selected_printer_preset} = 1
if $presets{$self->_selected_printer_preset};
}
}
# apply changes
for my $panel ($self->print_panels) {
next if $active{$panel->printer_name};
$self->{sizer}->DetachWindow($panel);
$panel->Destroy;
}
$self->add_printer($_, $presets{$_}) for sort keys %active;
# show/hide the warning about no printers
$self->{text_no_printers}->Show(!%presets);
# show/hide the Add button
$self->{btn_add}->Show(keys %presets != keys %active);
$self->Layout;
# we need this in order to trigger the OnSize event of wxScrolledWindow which
# recalculates the virtual size
Wx::GetTopLevelParent($self)->SendSizeEvent;
}
sub add_printer {
my ($self, $printer_name, $config) = @_;
# check that printer doesn't exist already
foreach my $panel ($self->print_panels) {
if ($panel->printer_name eq $printer_name) {
return $panel;
}
}
my $printer_panel = Slic3r::GUI::Controller::PrinterPanel->new($self, $printer_name, $config);
$self->{sizer}->Prepend($printer_panel, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
$self->Layout;
return $printer_panel;
}
sub print_panels {
my ($self) = @_;
return grep $_->isa('Slic3r::GUI::Controller::PrinterPanel'),
map $_->GetWindow, $self->{sizer}->GetChildren;
}
sub update_presets {
my $self = shift;
my ($group, $presets, $selected, $is_dirty) = @_;
# update configs of currently loaded print panels
foreach my $panel ($self->print_panels) {
foreach my $preset (@$presets) {
if ($panel->printer_name eq $preset->name) {
my $config = $preset->config(\@ConfigOptions);
$panel->config->apply($config);
}
}
}
$self->_selected_printer_preset($presets->[$selected]->name);
}
1;

View File

@ -0,0 +1,191 @@
package Slic3r::GUI::Controller::ManualControlDialog;
use strict;
use warnings;
use utf8;
use Slic3r::Geometry qw(PI X Y unscale);
use Wx qw(:dialog :id :misc :sizer :choicebook :button :bitmap
wxBORDER_NONE wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
use base qw(Wx::Dialog Class::Accessor);
__PACKAGE__->mk_accessors(qw(sender config2 x_homed y_homed));
sub new {
my ($class, $parent, $config, $sender) = @_;
my $self = $class->SUPER::new($parent, -1, "Manual Control", wxDefaultPosition,
[500,380], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
$self->sender($sender);
$self->config2({
xy_travel_speed => 130,
z_travel_speed => 10,
});
my $bed_sizer = Wx::FlexGridSizer->new(2, 3, 1, 1);
$bed_sizer->AddGrowableCol(1, 1);
$bed_sizer->AddGrowableRow(0, 1);
my $move_button = sub {
my ($sizer, $label, $icon, $bold, $pos, $handler) = @_;
my $btn = Wx::Button->new($self, -1, $label, wxDefaultPosition, wxDefaultSize,
wxBU_LEFT | wxBU_EXACTFIT);
$btn->SetFont($bold ? $Slic3r::GUI::small_bold_font : $Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("$icon.png"), wxBITMAP_TYPE_PNG));
$btn->SetBitmapPosition($pos);
}
EVT_BUTTON($self, $btn, $handler);
$sizer->Add($btn, 1, wxEXPAND | wxALL, 0);
};
# Y buttons
{
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
for my $d (qw(+10 +1 +0.1)) {
$move_button->($sizer, $d, 'arrow_up', 0, wxLEFT, sub { $self->rel_move('Y', $d) });
}
$move_button->($sizer, 'Y', 'house', 1, wxLEFT, sub { $self->home('Y') });
for my $d (qw(-0.1 -1 -10)) {
$move_button->($sizer, $d, 'arrow_down', 0, wxLEFT, sub { $self->rel_move('Y', $d) });
};
$bed_sizer->Add($sizer, 1, wxEXPAND, 0);
}
# Bed canvas
{
my $bed_shape = $config->bed_shape;
$self->{canvas} = my $canvas = Slic3r::GUI::2DBed->new($self, $bed_shape);
$canvas->interactive(1);
$canvas->on_move(sub {
my ($pos) = @_;
if (!($self->x_homed && $self->y_homed)) {
Slic3r::GUI::show_error($self, "Please home both X and Y before moving.");
return ;
}
# delete any pending commands to get a smoother movement
$self->sender->purge_queue(1);
$self->abs_xy_move($pos);
});
$bed_sizer->Add($canvas, 0, wxEXPAND | wxRIGHT, 3);
}
# Z buttons
{
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
for my $d (qw(+10 +1 +0.1)) {
$move_button->($sizer, $d, 'arrow_up', 0, wxLEFT, sub { $self->rel_move('Z', $d) });
}
$move_button->($sizer, 'Z', 'house', 1, wxLEFT, sub { $self->home('Z') });
for my $d (qw(-0.1 -1 -10)) {
$move_button->($sizer, $d, 'arrow_down', 0, wxLEFT, sub { $self->rel_move('Z', $d) });
};
$bed_sizer->Add($sizer, 1, wxEXPAND, 0);
}
# XYZ home button
$move_button->($bed_sizer, 'XYZ', 'house', 1, wxTOP, sub { $self->home(undef) });
# X buttons
{
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
for my $d (qw(-10 -1 -0.1)) {
$move_button->($sizer, $d, 'arrow_left', 0, wxTOP, sub { $self->rel_move('X', $d) });
}
$move_button->($sizer, 'X', 'house', 1, wxTOP, sub { $self->home('X') });
for my $d (qw(+0.1 +1 +10)) {
$move_button->($sizer, $d, 'arrow_right', 0, wxTOP, sub { $self->rel_move('X', $d) });
}
$bed_sizer->Add($sizer, 1, wxEXPAND, 0);
}
my $optgroup = Slic3r::GUI::OptionsGroup->new(
parent => $self,
title => 'Settings',
on_change => sub {
my ($opt_id, $value) = @_;
$self->config2->{$opt_id} = $value;
},
);
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Speed (mm/s)',
);
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'xy_travel_speed',
type => 'f',
label => 'X/Y',
tooltip => '',
default => $self->config2->{xy_travel_speed},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'z_travel_speed',
type => 'f',
label => 'Z',
tooltip => '',
default => $self->config2->{z_travel_speed},
));
$optgroup->append_line($line);
}
my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
$main_sizer->Add($bed_sizer, 1, wxEXPAND | wxALL, 10);
$main_sizer->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
#$main_sizer->Add($self->CreateButtonSizer(wxCLOSE), 0, wxEXPAND);
#EVT_BUTTON($self, wxID_CLOSE, sub { $self->Close });
$self->SetSizer($main_sizer);
$self->SetMinSize($self->GetSize);
#$main_sizer->SetSizeHints($self);
$self->Layout;
# needed to actually free memory
EVT_CLOSE($self, sub {
$self->EndModal(wxID_OK);
$self->Destroy;
});
return $self;
}
sub abs_xy_move {
my ($self, $pos) = @_;
$self->sender->send("G90", 1); # set absolute positioning
$self->sender->send(sprintf("G1 X%.1f Y%.1f F%d", @$pos, $self->config2->{xy_travel_speed}*60), 1);
$self->{canvas}->set_pos($pos);
}
sub rel_move {
my ($self, $axis, $distance) = @_;
my $speed = ($axis eq 'Z') ? $self->config2->{z_travel_speed} : $self->config2->{xy_travel_speed};
$self->sender->send("G91", 1); # set relative positioning
$self->sender->send(sprintf("G1 %s%.1f F%d", $axis, $distance, $speed*60), 1);
$self->sender->send("G90", 1); # set absolute positioning
if (my $pos = $self->{canvas}->pos) {
if ($axis eq 'X') {
$pos->translate($distance, 0);
} elsif ($axis eq 'Y') {
$pos->translate(0, $distance);
}
$self->{canvas}->set_pos($pos);
}
}
sub home {
my ($self, $axis) = @_;
$axis //= '';
$self->sender->send(sprintf("G28 %s", $axis), 1);
$self->{canvas}->set_pos(undef);
$self->x_homed(1) if $axis eq 'X';
$self->y_homed(1) if $axis eq 'Y';
}
1;

View File

@ -0,0 +1,723 @@
package Slic3r::GUI::Controller::PrinterPanel;
use strict;
use warnings;
use utf8;
use Wx qw(wxTheApp :panel :id :misc :sizer :button :bitmap :window :gauge :timer
:textctrl :font :systemsettings);
use Wx::Event qw(EVT_BUTTON EVT_MOUSEWHEEL EVT_TIMER EVT_SCROLLWIN);
use base qw(Wx::Panel Class::Accessor);
__PACKAGE__->mk_accessors(qw(printer_name config sender jobs
printing status_timer temp_timer));
use constant CONNECTION_TIMEOUT => 3; # seconds
use constant STATUS_TIMER_INTERVAL => 1000; # milliseconds
use constant TEMP_TIMER_INTERVAL => 5000; # milliseconds
sub new {
my ($class, $parent, $printer_name, $config) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [500, 250]);
$self->printer_name($printer_name || 'Printer');
$self->config($config);
$self->jobs([]);
# set up the timer that polls for updates
{
my $timer_id = &Wx::NewId();
$self->status_timer(Wx::Timer->new($self, $timer_id));
EVT_TIMER($self, $timer_id, sub {
my ($self, $event) = @_;
if ($self->printing) {
my $queue_size = $self->sender->queue_size;
$self->{gauge}->SetValue($self->{gauge}->GetRange - $queue_size);
if ($queue_size == 0) {
$self->print_completed;
}
}
$self->{log_textctrl}->AppendText("$_\n") for @{$self->sender->purge_log};
{
my $temp = $self->sender->getT;
if ($temp eq '') {
$self->{temp_panel}->Hide;
} else {
if (!$self->{temp_panel}->IsShown) {
$self->{temp_panel}->Show;
$self->Layout;
}
$self->{temp_text}->SetLabel($temp . "°C");
$temp = $self->sender->getB;
if ($temp eq '') {
$self->{bed_temp_text}->SetLabel('n.a.');
} else {
$self->{bed_temp_text}->SetLabel($temp . "°C");
}
}
}
});
}
# set up the timer that sends temperature requests
# (responses are handled by status_timer)
{
my $timer_id = &Wx::NewId();
$self->temp_timer(Wx::Timer->new($self, $timer_id));
EVT_TIMER($self, $timer_id, sub {
my ($self, $event) = @_;
$self->sender->send("M105", 1); # send it through priority queue
});
}
my $box = Wx::StaticBox->new($self, -1, "");
my $sizer = Wx::StaticBoxSizer->new($box, wxHORIZONTAL);
my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
# printer name
{
my $text = Wx::StaticText->new($box, -1, $self->printer_name, wxDefaultPosition, [220,-1]);
my $font = $text->GetFont;
$font->SetPointSize(20);
$text->SetFont($font);
$left_sizer->Add($text, 0, wxEXPAND, 0);
}
# connection info
{
my $conn_sizer = Wx::FlexGridSizer->new(2, 2, 1, 0);
$conn_sizer->SetFlexibleDirection(wxHORIZONTAL);
$conn_sizer->AddGrowableCol(1, 1);
$left_sizer->Add($conn_sizer, 0, wxEXPAND | wxTOP, 5);
{
my $text = Wx::StaticText->new($box, -1, "Port:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$conn_sizer->Add($text, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 5);
}
my $serial_port_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
{
$self->{serial_port_combobox} = Wx::ComboBox->new($box, -1, $config->serial_port, wxDefaultPosition, wxDefaultSize, []);
$self->{serial_port_combobox}->SetFont($Slic3r::GUI::small_font);
$self->update_serial_ports;
$serial_port_sizer->Add($self->{serial_port_combobox}, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 1);
}
{
$self->{btn_rescan_serial} = my $btn = Wx::BitmapButton->new($box, -1, Wx::Bitmap->new($Slic3r::var->("arrow_rotate_clockwise.png"), wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize, &Wx::wxBORDER_NONE);
$btn->SetToolTipString("Rescan serial ports")
if $btn->can('SetToolTipString');
$serial_port_sizer->Add($btn, 0, wxALIGN_CENTER_VERTICAL, 0);
EVT_BUTTON($self, $btn, sub { $self->update_serial_ports });
}
$conn_sizer->Add($serial_port_sizer, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 5);
{
my $text = Wx::StaticText->new($box, -1, "Speed:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$conn_sizer->Add($text, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 5);
}
my $serial_speed_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
{
$self->{serial_speed_combobox} = Wx::ComboBox->new($box, -1, $config->serial_speed, wxDefaultPosition, wxDefaultSize,
["115200", "250000"]);
$self->{serial_speed_combobox}->SetFont($Slic3r::GUI::small_font);
$serial_speed_sizer->Add($self->{serial_speed_combobox}, 0, wxALIGN_CENTER_VERTICAL, 0);
}
{
$self->{btn_disconnect} = my $btn = Wx::Button->new($box, -1, "Disconnect", wxDefaultPosition, wxDefaultSize);
$btn->SetFont($Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("delete.png"), wxBITMAP_TYPE_PNG));
}
$serial_speed_sizer->Add($btn, 0, wxLEFT, 5);
EVT_BUTTON($self, $btn, \&disconnect);
}
$conn_sizer->Add($serial_speed_sizer, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 5);
}
# buttons
{
$self->{btn_connect} = my $btn = Wx::Button->new($box, -1, "Connect to printer", wxDefaultPosition, [-1, 40]);
my $font = $btn->GetFont;
$font->SetPointSize($font->GetPointSize + 2);
$btn->SetFont($font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("arrow_up.png"), wxBITMAP_TYPE_PNG));
}
$left_sizer->Add($btn, 0, wxTOP, 15);
EVT_BUTTON($self, $btn, \&connect);
}
# print progress bar
{
my $gauge = $self->{gauge} = Wx::Gauge->new($self, wxGA_HORIZONTAL, 100, wxDefaultPosition, wxDefaultSize);
$left_sizer->Add($self->{gauge}, 0, wxEXPAND | wxTOP, 15);
$gauge->Hide;
}
# status
$self->{status_text} = Wx::StaticText->new($box, -1, "", wxDefaultPosition, [200,-1]);
$left_sizer->Add($self->{status_text}, 1, wxEXPAND | wxTOP, 15);
# manual control
{
$self->{btn_manual_control} = my $btn = Wx::Button->new($box, -1, "Manual control", wxDefaultPosition, wxDefaultSize);
$btn->SetFont($Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG));
}
$btn->Hide;
$left_sizer->Add($btn, 0, wxTOP, 15);
EVT_BUTTON($self, $btn, sub {
my $dlg = Slic3r::GUI::Controller::ManualControlDialog->new
($self, $self->config, $self->sender);
$dlg->ShowModal;
});
}
# temperature
{
my $temp_panel = $self->{temp_panel} = Wx::Panel->new($box, -1);
my $temp_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
my $temp_font = Wx::Font->new($Slic3r::GUI::small_font);
$temp_font->SetWeight(wxFONTWEIGHT_BOLD);
{
my $text = Wx::StaticText->new($temp_panel, -1, "Temperature:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$temp_sizer->Add($text, 0, wxALIGN_CENTER_VERTICAL);
$self->{temp_text} = Wx::StaticText->new($temp_panel, -1, "", wxDefaultPosition, wxDefaultSize);
$self->{temp_text}->SetFont($temp_font);
$self->{temp_text}->SetForegroundColour(Wx::wxRED);
$temp_sizer->Add($self->{temp_text}, 1, wxALIGN_CENTER_VERTICAL);
}
{
my $text = Wx::StaticText->new($temp_panel, -1, "Bed:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$temp_sizer->Add($text, 0, wxALIGN_CENTER_VERTICAL);
$self->{bed_temp_text} = Wx::StaticText->new($temp_panel, -1, "", wxDefaultPosition, wxDefaultSize);
$self->{bed_temp_text}->SetFont($temp_font);
$self->{bed_temp_text}->SetForegroundColour(Wx::wxRED);
$temp_sizer->Add($self->{bed_temp_text}, 1, wxALIGN_CENTER_VERTICAL);
}
$temp_panel->SetSizer($temp_sizer);
$temp_panel->Hide;
$left_sizer->Add($temp_panel, 0, wxEXPAND | wxTOP | wxBOTTOM, 4);
}
# print jobs panel
$self->{print_jobs_sizer} = my $print_jobs_sizer = Wx::BoxSizer->new(wxVERTICAL);
{
my $text = Wx::StaticText->new($box, -1, "Queue:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$print_jobs_sizer->Add($text, 0, wxEXPAND, 0);
$self->{jobs_panel} = Wx::ScrolledWindow->new($box, -1, wxDefaultPosition, wxDefaultSize,
wxVSCROLL | wxBORDER_NONE);
$self->{jobs_panel}->SetScrollbars(0, 1, 0, 1);
$self->{jobs_panel_sizer} = Wx::BoxSizer->new(wxVERTICAL);
$self->{jobs_panel}->SetSizer($self->{jobs_panel_sizer});
$print_jobs_sizer->Add($self->{jobs_panel}, 1, wxEXPAND, 0);
# TODO: fix this. We're trying to pass the scroll event to the parent but it
# doesn't work.
EVT_SCROLLWIN($self->{jobs_panel}, sub {
my ($panel, $event) = @_;
my $controller = $self->GetParent;
my $new_event = Wx::ScrollWinEvent->new(
$event->GetEventType,
$event->GetPosition,
$event->GetOrientation,
);
$controller->ProcessEvent($new_event);
}) if 0;
}
my $log_sizer = Wx::BoxSizer->new(wxVERTICAL);
{
my $text = Wx::StaticText->new($box, -1, "Log:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$log_sizer->Add($text, 0, wxEXPAND, 0);
my $log = $self->{log_textctrl} = Wx::TextCtrl->new($box, -1, "", wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxBORDER_NONE);
$log->SetBackgroundColour($box->GetBackgroundColour);
$log->SetFont($Slic3r::GUI::small_font);
$log->SetEditable(0);
$log_sizer->Add($self->{log_textctrl}, 1, wxEXPAND, 0);
}
$sizer->Add($left_sizer, 0, wxEXPAND | wxALL, 0);
$sizer->Add($print_jobs_sizer, 2, wxEXPAND | wxALL, 0);
$sizer->Add($log_sizer, 1, wxEXPAND | wxLEFT, 15);
$self->SetSizer($sizer);
$self->SetMinSize($self->GetSize);
$self->_update_connection_controls;
return $self;
}
sub is_connected {
my ($self) = @_;
return $self->sender && $self->sender->is_connected;
}
sub _update_connection_controls {
my ($self) = @_;
$self->{btn_connect}->Show;
$self->{btn_disconnect}->Hide;
$self->{serial_port_combobox}->Enable;
$self->{serial_speed_combobox}->Enable;
$self->{btn_rescan_serial}->Enable;
$self->{btn_manual_control}->Hide;
$self->{btn_manual_control}->Disable;
if ($self->is_connected) {
$self->{btn_connect}->Hide;
$self->{btn_manual_control}->Show;
if (!$self->printing || $self->printing->paused) {
$self->{btn_disconnect}->Show;
$self->{btn_manual_control}->Enable;
}
$self->{serial_port_combobox}->Disable;
$self->{serial_speed_combobox}->Disable;
$self->{btn_rescan_serial}->Disable;
}
$self->Layout;
}
sub set_status {
my ($self, $status) = @_;
$self->{status_text}->SetLabel($status);
$self->{status_text}->Wrap($self->{status_text}->GetSize->GetWidth);
$self->{status_text}->Refresh;
$self->Layout;
}
sub connect {
my ($self) = @_;
return if $self->is_connected;
$self->set_status("Connecting...");
$self->sender(Slic3r::GCode::Sender->new);
my $res = $self->sender->connect(
$self->{serial_port_combobox}->GetValue,
$self->{serial_speed_combobox}->GetValue,
);
if (!$res) {
$self->set_status("Connection failed. Check serial port and speed.");
} else {
if ($self->sender->wait_connected) {
$self->set_status("Printer is online. You can now start printing from the queue on the right.");
$self->status_timer->Start(STATUS_TIMER_INTERVAL, wxTIMER_CONTINUOUS);
$self->temp_timer->Start(TEMP_TIMER_INTERVAL, wxTIMER_CONTINUOUS);
# request temperature now, without waiting for the timer
$self->sender->send("M105", 1);
} else {
$self->set_status("Connection failed. Check serial port and speed.");
}
}
$self->_update_connection_controls;
$self->reload_jobs;
}
sub disconnect {
my ($self) = @_;
$self->status_timer->Stop;
$self->temp_timer->Stop;
return if !$self->is_connected;
$self->printing->printing(0) if $self->printing;
$self->printing(undef);
$self->{gauge}->Hide;
$self->{temp_panel}->Hide;
$self->sender->disconnect;
$self->set_status("");
$self->_update_connection_controls;
$self->reload_jobs;
}
sub update_serial_ports {
my ($self) = @_;
my $cb = $self->{serial_port_combobox};
my $current = $cb->GetValue;
$cb->Clear;
$cb->Append($_) for wxTheApp->scan_serial_ports;
$cb->SetValue($current);
}
sub load_print_job {
my ($self, $gcode_file, $filament_stats) = @_;
push @{$self->jobs}, my $job = Slic3r::GUI::Controller::PrinterPanel::PrintJob->new(
id => time() . $gcode_file . rand(1000),
gcode_file => $gcode_file,
filament_stats => $filament_stats,
);
$self->reload_jobs;
return $job;
}
sub delete_job {
my ($self, $job) = @_;
$self->jobs([ grep $_->id ne $job->id, @{$self->jobs} ]);
$self->reload_jobs;
}
sub print_job {
my ($self, $job) = @_;
$self->printing($job);
$job->printing(1);
$self->reload_jobs;
open my $fh, '<', $job->gcode_file;
my $line_count = 0;
while (my $row = <$fh>) {
$self->sender->send($row);
$line_count++;
}
close $fh;
$self->_update_connection_controls;
$self->{gauge}->SetRange($line_count);
$self->{gauge}->SetValue(0);
$self->{gauge}->Enable;
$self->{gauge}->Show;
$self->Layout;
$self->set_status('Printing...');
$self->{log_textctrl}->AppendText(sprintf "=====\n");
$self->{log_textctrl}->AppendText(sprintf "Printing %s\n", $job->name);
$self->{log_textctrl}->AppendText(sprintf "Print started at %s\n", $self->_timestamp);
}
sub print_completed {
my ($self) = @_;
my $job = $self->printing;
$self->printing(undef);
$job->printing(0);
$job->printed(1);
$self->_update_connection_controls;
$self->{gauge}->Hide;
$self->Layout;
$self->set_status('Print completed.');
$self->{log_textctrl}->AppendText(sprintf "Print completed at %s\n", $self->_timestamp);
$self->reload_jobs;
}
sub reload_jobs {
my ($self) = @_;
# reorder jobs
@{$self->jobs} = sort { ($a->printed <=> $b->printed) || ($a->timestamp <=> $b->timestamp) }
@{$self->jobs};
# remove all panels
foreach my $child ($self->{jobs_panel_sizer}->GetChildren) {
my $window = $child->GetWindow;
$self->{jobs_panel_sizer}->Detach($window);
# now $child does not exist anymore
$window->Destroy;
}
# re-add all panels
foreach my $job (@{$self->jobs}) {
my $panel = Slic3r::GUI::Controller::PrinterPanel::PrintJobPanel->new($self->{jobs_panel}, $job);
$self->{jobs_panel_sizer}->Add($panel, 0, wxEXPAND | wxBOTTOM, 5);
$panel->on_delete_job(sub {
my ($job) = @_;
$self->delete_job($job);
});
$panel->on_print_job(sub {
my ($job) = @_;
$self->print_job($job);
});
$panel->on_pause_print(sub {
my ($job) = @_;
$self->sender->pause_queue;
$job->paused(1);
$self->reload_jobs;
$self->_update_connection_controls;
$self->{gauge}->Disable;
$self->set_status('Print is paused. Click on Resume to continue.');
});
$panel->on_abort_print(sub {
my ($job) = @_;
$self->sender->purge_queue;
$self->printing(undef);
$job->printing(0);
$job->paused(0);
$self->reload_jobs;
$self->_update_connection_controls;
$self->{gauge}->Disable;
$self->{gauge}->Hide;
$self->set_status('Print was aborted.');
$self->{log_textctrl}->AppendText(sprintf "Print aborted at %s\n", $self->_timestamp);
});
$panel->on_resume_print(sub {
my ($job) = @_;
$self->sender->resume_queue;
$job->paused(0);
$self->reload_jobs;
$self->_update_connection_controls;
$self->{gauge}->Enable;
$self->set_status('Printing...');
});
$panel->enable_print if $self->is_connected && !$self->printing;
EVT_MOUSEWHEEL($panel, sub {
my (undef, $event) = @_;
Wx::PostEvent($self->{jobs_panel}, $event);
$event->Skip;
});
}
$self->{jobs_panel}->Layout;
$self->{print_jobs_sizer}->Layout;
}
sub _timestamp {
my ($self) = @_;
my @time = localtime(time);
return sprintf '%02d:%02d:%02d', @time[2,1,0];
}
package Slic3r::GUI::Controller::PrinterPanel::PrintJob;
use Moo;
use File::Basename qw(basename);
has 'id' => (is => 'ro', required => 1);
has 'timestamp' => (is => 'ro', default => sub { time });
has 'gcode_file' => (is => 'ro', required => 1);
has 'filament_stats' => (is => 'rw');
has 'printing' => (is => 'rw', default => sub { 0 });
has 'paused' => (is => 'rw', default => sub { 0 });
has 'printed' => (is => 'rw', default => sub { 0 });
sub name {
my ($self) = @_;
return basename($self->gcode_file);
}
package Slic3r::GUI::Controller::PrinterPanel::PrintJobPanel;
use strict;
use warnings;
use utf8;
use Wx qw(wxTheApp :panel :id :misc :sizer :button :bitmap :font :dialog :icon :timer
:colour :brush :pen);
use Wx::Event qw(EVT_BUTTON EVT_TIMER EVT_ERASE_BACKGROUND);
use base qw(Wx::Panel Class::Accessor);
__PACKAGE__->mk_accessors(qw(job on_delete_job on_print_job on_pause_print on_resume_print
on_abort_print blink_timer));
sub new {
my ($class, $parent, $job) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize);
$self->job($job);
$self->SetBackgroundColour(wxWHITE);
{
my $white_brush = Wx::Brush->new(wxWHITE, wxSOLID);
my $pen = Wx::Pen->new(Wx::Colour->new(200,200,200), 1, wxSOLID);
EVT_ERASE_BACKGROUND($self, sub {
my ($self, $event) = @_;
my $dc = $event->GetDC;
my $size = $self->GetSize;
$dc->SetBrush($white_brush);
$dc->SetPen($pen);
$dc->DrawRoundedRectangle(0, 0, $size->GetWidth,$size->GetHeight, 6);
});
}
my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
{
$self->{job_name_textctrl} = my $text = Wx::StaticText->new($self, -1, $job->name, wxDefaultPosition, wxDefaultSize);
my $font = $text->GetFont;
$font->SetWeight(wxFONTWEIGHT_BOLD);
$text->SetFont($font);
if ($job->printed) {
$text->SetForegroundColour($Slic3r::GUI::grey);
}
$left_sizer->Add($text, 0, wxEXPAND, 0);
}
{
my $filament_stats = join "\n",
map "$_ (" . sprintf("%.2f", $job->filament_stats->{$_}/1000) . "m)",
sort keys %{$job->filament_stats};
my $text = Wx::StaticText->new($self, -1, $filament_stats, wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
if ($job->printed && !$job->printing) {
$text->SetForegroundColour($Slic3r::GUI::grey);
}
$left_sizer->Add($text, 0, wxEXPAND | wxTOP, 6);
}
my $buttons_sizer = Wx::BoxSizer->new(wxVERTICAL);
my $button_style = Wx::wxBORDER_NONE | wxBU_EXACTFIT;
{
my $btn = $self->{btn_delete} = Wx::Button->new($self, -1, 'Delete',
wxDefaultPosition, wxDefaultSize, $button_style);
$btn->SetToolTipString("Delete this job from print queue")
if $btn->can('SetToolTipString');
$btn->SetFont($Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("delete.png"), wxBITMAP_TYPE_PNG));
}
if ($job->printing) {
$btn->Hide;
}
$buttons_sizer->Add($btn, 0, wxBOTTOM, 2);
EVT_BUTTON($self, $btn, sub {
my $res = Wx::MessageDialog->new($self, "Are you sure you want to delete this print job?", 'Delete Job', wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION)->ShowModal;
return unless $res == wxID_YES;
wxTheApp->CallAfter(sub {
$self->on_delete_job->($job);
});
});
}
{
my $label = $job->printed ? 'Print Again' : 'Print This';
my $btn = $self->{btn_print} = Wx::Button->new($self, -1, $label, wxDefaultPosition, wxDefaultSize,
$button_style);
$btn->SetFont($Slic3r::GUI::small_bold_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_play.png"), wxBITMAP_TYPE_PNG));
$btn->SetBitmapCurrent(Wx::Bitmap->new($Slic3r::var->("control_play_blue.png"), wxBITMAP_TYPE_PNG));
#$btn->SetBitmapPosition(wxRIGHT);
}
$btn->Hide;
$buttons_sizer->Add($btn, 0, wxBOTTOM, 2);
EVT_BUTTON($self, $btn, sub {
wxTheApp->CallAfter(sub {
$self->on_print_job->($job);
});
});
}
{
my $btn = $self->{btn_pause} = Wx::Button->new($self, -1, "Pause", wxDefaultPosition, wxDefaultSize,
$button_style);
$btn->SetFont($Slic3r::GUI::small_font);
if (!$job->printing || $job->paused) {
$btn->Hide;
}
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_pause.png"), wxBITMAP_TYPE_PNG));
$btn->SetBitmapCurrent(Wx::Bitmap->new($Slic3r::var->("control_pause_blue.png"), wxBITMAP_TYPE_PNG));
}
$buttons_sizer->Add($btn, 0, wxBOTTOM, 2);
EVT_BUTTON($self, $btn, sub {
wxTheApp->CallAfter(sub {
$self->on_pause_print->($job);
});
});
}
{
my $btn = $self->{btn_resume} = Wx::Button->new($self, -1, "Resume", wxDefaultPosition, wxDefaultSize,
$button_style);
$btn->SetFont($Slic3r::GUI::small_font);
if (!$job->printing || !$job->paused) {
$btn->Hide;
}
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_play.png"), wxBITMAP_TYPE_PNG));
$btn->SetBitmapCurrent(Wx::Bitmap->new($Slic3r::var->("control_play_blue.png"), wxBITMAP_TYPE_PNG));
}
$buttons_sizer->Add($btn, 0, wxBOTTOM, 2);
EVT_BUTTON($self, $btn, sub {
wxTheApp->CallAfter(sub {
$self->on_resume_print->($job);
});
});
}
{
my $btn = $self->{btn_abort} = Wx::Button->new($self, -1, "Abort", wxDefaultPosition, wxDefaultSize,
$button_style);
$btn->SetFont($Slic3r::GUI::small_font);
if (!$job->printing) {
$btn->Hide;
}
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_stop.png"), wxBITMAP_TYPE_PNG));
$btn->SetBitmapCurrent(Wx::Bitmap->new($Slic3r::var->("control_stop_blue.png"), wxBITMAP_TYPE_PNG));
}
$buttons_sizer->Add($btn, 0, wxBOTTOM, 2);
EVT_BUTTON($self, $btn, sub {
wxTheApp->CallAfter(sub {
$self->on_abort_print->($job);
});
});
}
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer->Add($left_sizer, 1, wxEXPAND | wxALL, 6);
$sizer->Add($buttons_sizer, 0, wxEXPAND | wxALL, 6);
$self->SetSizer($sizer);
# set-up the timer that changes the job name color while printing
if ($self->job->printing && !$self->job->paused) {
my $timer_id = &Wx::NewId();
$self->blink_timer(Wx::Timer->new($self, $timer_id));
my $blink = 0; # closure
my $colour = Wx::Colour->new(0, 190, 0);
EVT_TIMER($self, $timer_id, sub {
my ($self, $event) = @_;
$self->{job_name_textctrl}->SetForegroundColour($blink ? Wx::wxBLACK : $colour);
$blink = !$blink;
});
$self->blink_timer->Start(1000, wxTIMER_CONTINUOUS);
}
return $self;
}
sub enable_print {
my ($self) = @_;
if (!$self->job->printing) {
$self->{btn_print}->Show;
}
$self->Layout;
}
sub Destroy {
my ($self) = @_;
# There's a gap between the time Perl destroys the wxPanel object and
# the blink_timer member, so the wxTimer might still fire an event which
# isn't handled properly, causing a crash. So we ensure that blink_timer
# is stopped before we destroy the wxPanel.
$self->blink_timer->Stop if $self->blink_timer;
return $self->SUPER::Destroy;
}
1;

View File

@ -8,22 +8,24 @@ use List::Util qw(min);
use Slic3r::Geometry qw(X Y Z);
use Wx qw(:frame :bitmap :id :misc :notebook :panel :sizer :menu :dialog :filedialog
:font :icon wxTheApp);
use Wx::Event qw(EVT_CLOSE EVT_MENU);
use Wx::Event qw(EVT_CLOSE EVT_MENU EVT_NOTEBOOK_PAGE_CHANGED);
use base 'Wx::Frame';
our $last_input_file;
our $last_output_file;
our $qs_last_input_file;
our $qs_last_output_file;
our $last_config;
sub new {
my ($class, %params) = @_;
my $self = $class->SUPER::new(undef, -1, 'Slic3r', wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
$self->SetIcon(Wx::Icon->new("$Slic3r::var/Slic3r_128px.png", wxBITMAP_TYPE_PNG) );
$self->SetIcon(Wx::Icon->new($Slic3r::var->("Slic3r_128px.png"), wxBITMAP_TYPE_PNG) );
# store input params
$self->{mode} = $params{mode};
$self->{mode} = 'expert' if $self->{mode} !~ /^(?:simple|expert)$/;
# If set, the "Controller" tab for the control of the printer over serial line and the serial port settings are hidden.
$self->{no_controller} = $params{no_controller};
$self->{no_plater} = $params{no_plater};
$self->{loaded} = 0;
@ -38,16 +40,6 @@ sub new {
$self->{loaded} = 1;
# declare events
EVT_CLOSE($self, sub {
my (undef, $event) = @_;
if ($event->CanVeto && !$self->check_unsaved_changes) {
$event->Veto;
return;
}
$event->Skip;
});
# initialize layout
{
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
@ -56,11 +48,42 @@ sub new {
$self->SetSizer($sizer);
$self->Fit;
$self->SetMinSize([760, 490]);
$self->SetSize($self->GetMinSize);
if (defined $Slic3r::GUI::Settings->{_}{main_frame_size}) {
my $size = [ split ',', $Slic3r::GUI::Settings->{_}{main_frame_size}, 2 ];
$self->SetSize($size);
my $display = Wx::Display->new->GetClientArea();
my $pos = [ split ',', $Slic3r::GUI::Settings->{_}{main_frame_pos}, 2 ];
if (($pos->[X] + $size->[X]/2) < $display->GetRight && ($pos->[Y] + $size->[Y]/2) < $display->GetBottom) {
$self->Move($pos);
}
$self->Maximize(1) if $Slic3r::GUI::Settings->{_}{main_frame_maximized};
} else {
$self->SetSize($self->GetMinSize);
}
$self->Show;
$self->Layout;
}
# declare events
EVT_CLOSE($self, sub {
my (undef, $event) = @_;
if ($event->CanVeto && !$self->check_unsaved_changes) {
$event->Veto;
return;
}
# save window size
$Slic3r::GUI::Settings->{_}{main_frame_pos} = join ',', $self->GetScreenPositionXY;
$Slic3r::GUI::Settings->{_}{main_frame_size} = join ',', $self->GetSizeWH;
$Slic3r::GUI::Settings->{_}{main_frame_maximized} = $self->IsMaximized;
wxTheApp->save_settings;
# propagate event
$event->Skip;
});
return $self;
}
@ -68,25 +91,39 @@ sub _init_tabpanel {
my ($self) = @_;
$self->{tabpanel} = my $panel = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{tabpanel}, sub {
my $panel = $self->{tabpanel}->GetCurrentPage;
$panel->OnActivate if $panel->can('OnActivate');
});
if (!$self->{no_plater}) {
$panel->AddPage($self->{plater} = Slic3r::GUI::Plater->new($panel), "Plater");
if (!$self->{no_controller}) {
$panel->AddPage($self->{controller} = Slic3r::GUI::Controller->new($panel), "Controller");
}
}
$self->{options_tabs} = {};
my $simple_config;
if ($self->{mode} eq 'simple') {
$simple_config = Slic3r::Config->load("$Slic3r::GUI::datadir/simple.ini")
if -e "$Slic3r::GUI::datadir/simple.ini";
if -e Slic3r::encode_path("$Slic3r::GUI::datadir/simple.ini");
}
my $class_prefix = $self->{mode} eq 'simple' ? "Slic3r::GUI::SimpleTab::" : "Slic3r::GUI::Tab::";
for my $tab_name (qw(print filament printer)) {
my $tab;
$tab = $self->{options_tabs}{$tab_name} = ($class_prefix . ucfirst $tab_name)->new($panel);
$tab = $self->{options_tabs}{$tab_name} = ($class_prefix . ucfirst $tab_name)->new(
$panel,
no_controller => $self->{no_controller});
$tab->on_value_change(sub {
my ($opt_key, $value) = @_;
my $config = $tab->config;
$self->{plater}->on_config_change($config) if $self->{plater}; # propagate config change events to the plater
if ($self->{plater}) {
$self->{plater}->on_config_change($config); # propagate config change events to the plater
$self->{plater}->on_extruders_change($value) if $opt_key eq 'extruders_count';
}
if ($self->{loaded}) { # don't save while loading for the first time
if ($self->{mode} eq 'simple') {
# save config
@ -105,6 +142,9 @@ sub _init_tabpanel {
if ($self->{plater}) {
$self->{plater}->update_presets($tab_name, @_);
$self->{plater}->on_config_change($tab->config);
if ($self->{controller}) {
$self->{controller}->update_presets($tab_name, @_);
}
}
});
$tab->load_presets;
@ -114,8 +154,8 @@ sub _init_tabpanel {
if ($self->{plater}) {
$self->{plater}->on_select_preset(sub {
my ($group, $preset) = @_;
$self->{options_tabs}{$group}->select_preset($preset);
my ($group, $i) = @_;
$self->{options_tabs}{$group}->select_preset($i);
});
# load initial config
@ -131,40 +171,47 @@ sub _init_menubar {
{
$self->_append_menu_item($fileMenu, "&Load Config…\tCtrl+L", 'Load exported configuration file', sub {
$self->load_config_file;
});
}, undef, 'plugin_add.png');
$self->_append_menu_item($fileMenu, "&Export Config…\tCtrl+E", 'Export current configuration to file', sub {
$self->export_config;
});
}, undef, 'plugin_go.png');
$self->_append_menu_item($fileMenu, "&Load Config Bundle…", 'Load presets from a bundle', sub {
$self->load_configbundle;
});
}, undef, 'lorry_add.png');
$self->_append_menu_item($fileMenu, "&Export Config Bundle…", 'Export all presets to file', sub {
$self->export_configbundle;
});
}, undef, 'lorry_go.png');
$fileMenu->AppendSeparator();
my $repeat;
$self->_append_menu_item($fileMenu, "Q&uick Slice…\tCtrl+U", 'Slice file', sub {
$self->quick_slice;
$repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
});
wxTheApp->CallAfter(sub {
$self->quick_slice;
$repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
});
}, undef, 'cog_go.png');
$self->_append_menu_item($fileMenu, "Quick Slice and Save &As…\tCtrl+Alt+U", 'Slice file and save as', sub {
$self->quick_slice(save_as => 1);
$repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
});
wxTheApp->CallAfter(sub {
$self->quick_slice(save_as => 1);
$repeat->Enable(defined $Slic3r::GUI::MainFrame::last_input_file);
});
}, undef, 'cog_go.png');
$repeat = $self->_append_menu_item($fileMenu, "&Repeat Last Quick Slice\tCtrl+Shift+U", 'Repeat last quick slice', sub {
$self->quick_slice(reslice => 1);
});
wxTheApp->CallAfter(sub {
$self->quick_slice(reslice => 1);
});
}, undef, 'cog_go.png');
$repeat->Enable(0);
$fileMenu->AppendSeparator();
$self->_append_menu_item($fileMenu, "Slice to SV&G…\tCtrl+G", 'Slice file to SVG', sub {
$self->quick_slice(save_as => 1, export_svg => 1);
});
}, undef, 'shape_handles.png');
$fileMenu->AppendSeparator();
$self->_append_menu_item($fileMenu, "Repair STL file…", 'Automatically repair an STL file', sub {
$self->repair_stl;
});
}, undef, 'wrench.png');
$fileMenu->AppendSeparator();
$self->_append_menu_item($fileMenu, "Preferences…", 'Application preferences', sub {
# Cmd+, is standard on OS X - what about other operating systems?
$self->_append_menu_item($fileMenu, "Preferences…\tCtrl+,", 'Application preferences', sub {
Slic3r::GUI::Preferences->new($self)->ShowModal;
}, wxID_PREFERENCES);
$fileMenu->AppendSeparator();
@ -180,17 +227,20 @@ sub _init_menubar {
$self->{plater_menu} = Wx::Menu->new;
$self->_append_menu_item($self->{plater_menu}, "Export G-code...", 'Export current plate as G-code', sub {
$plater->export_gcode;
});
$self->_append_menu_item($self->{plater_menu}, "Export STL...", 'Export current plate as STL', sub {
}, undef, 'cog_go.png');
$self->_append_menu_item($self->{plater_menu}, "Export plate as STL...", 'Export current plate as STL', sub {
$plater->export_stl;
});
$self->_append_menu_item($self->{plater_menu}, "Export AMF...", 'Export current plate as AMF', sub {
}, undef, 'brick_go.png');
$self->_append_menu_item($self->{plater_menu}, "Export plate as AMF...", 'Export current plate as AMF', sub {
$plater->export_amf;
});
$self->{plater_menu}->AppendSeparator();
$self->_append_menu_item($self->{plater_menu}, "Toolpaths preview…", 'Open a viewer with toolpaths preview', sub {
$plater->toolpaths_preview;
});
}, undef, 'brick_go.png');
$self->_append_menu_item($self->{plater_menu}, "Open DLP Projector…\tCtrl+L", 'Open projector window for DLP printing', sub {
my $projector = Slic3r::GUI::Projector->new($self);
# this double invocation is needed for properly hiding the MainFrame
$projector->Show;
$projector->ShowModal;
}, undef, 'film.png');
$self->{object_menu} = $self->{plater}->object_menu;
$self->on_plater_selection_changed(0);
@ -199,19 +249,28 @@ sub _init_menubar {
# Window menu
my $windowMenu = Wx::Menu->new;
{
my $tab_count = $self->{no_plater} ? 3 : 4;
$self->_append_menu_item($windowMenu, "Select &Plater Tab\tCtrl+1", 'Show the plater', sub {
$self->select_tab(0);
}) unless $self->{no_plater};
my $tab_offset = 0;
if (!$self->{no_plater}) {
$self->_append_menu_item($windowMenu, "Select &Plater Tab\tCtrl+1", 'Show the plater', sub {
$self->select_tab(0);
}, undef, 'application_view_tile.png');
if (!$self->{no_controller}) {
$self->_append_menu_item($windowMenu, "Select &Controller Tab\tCtrl+T", 'Show the printer controller', sub {
$self->select_tab(1);
}, undef, 'printer_empty.png');
}
$windowMenu->AppendSeparator();
$tab_offset += 2;
}
$self->_append_menu_item($windowMenu, "Select P&rint Settings Tab\tCtrl+2", 'Show the print settings', sub {
$self->select_tab($tab_count-3);
});
$self->select_tab($tab_offset+0);
}, undef, 'cog.png');
$self->_append_menu_item($windowMenu, "Select &Filament Settings Tab\tCtrl+3", 'Show the filament settings', sub {
$self->select_tab($tab_count-2);
});
$self->select_tab($tab_offset+1);
}, undef, 'spool.png');
$self->_append_menu_item($windowMenu, "Select Print&er Settings Tab\tCtrl+4", 'Show the printer settings', sub {
$self->select_tab($tab_count-1);
});
$self->select_tab($tab_offset+2);
}, undef, 'printer_empty.png');
}
# Help menu
@ -225,7 +284,7 @@ sub _init_menubar {
Wx::LaunchDefaultBrowser('http://slic3r.org/');
});
my $versioncheck = $self->_append_menu_item($helpMenu, "Check for &Updates...", 'Check for new Slic3r versions', sub {
wxTheApp->check_version(manual => 1);
wxTheApp->check_version(1);
});
$versioncheck->Enable(wxTheApp->have_version_check);
$self->_append_menu_item($helpMenu, "Slic3r &Manual", 'Open the Slic3r manual in your browser', sub {
@ -283,27 +342,34 @@ sub quick_slice {
$dialog->Destroy;
return;
}
$input_file = $dialog->GetPaths;
$input_file = Slic3r::decode_path($dialog->GetPaths);
$dialog->Destroy;
$last_input_file = $input_file unless $params{export_svg};
$qs_last_input_file = $input_file unless $params{export_svg};
} else {
if (!defined $last_input_file) {
if (!defined $qs_last_input_file) {
Wx::MessageDialog->new($self, "No previously sliced file.",
'Error', wxICON_ERROR | wxOK)->ShowModal();
return;
}
if (! -e $last_input_file) {
Wx::MessageDialog->new($self, "Previously sliced file ($last_input_file) not found.",
if (! -e $qs_last_input_file) {
Wx::MessageDialog->new($self, "Previously sliced file ($qs_last_input_file) not found.",
'File Not Found', wxICON_ERROR | wxOK)->ShowModal();
return;
}
$input_file = $last_input_file;
$input_file = $qs_last_input_file;
}
my $input_file_basename = basename($input_file);
$Slic3r::GUI::Settings->{recent}{skein_directory} = dirname($input_file);
wxTheApp->save_settings;
my $print_center;
{
my $bed_shape = Slic3r::Polygon->new_scale(@{$config->bed_shape});
$print_center = Slic3r::Pointf->new_unscale(@{$bed_shape->bounding_box->center});
}
my $sprint = Slic3r::Print::Simple->new(
print_center => $print_center,
status_cb => sub {
my ($percent, $message) = @_;
return if &Wx::wxVERSION_STRING !~ / 2\.(8\.|9\.[2-9])/;
@ -325,19 +391,19 @@ sub quick_slice {
# select output file
my $output_file;
if ($params{reslice}) {
$output_file = $last_output_file if defined $last_output_file;
$output_file = $qs_last_output_file if defined $qs_last_output_file;
} elsif ($params{save_as}) {
$output_file = $sprint->expanded_output_filepath;
$output_file =~ s/\.gcode$/.svg/i if $params{export_svg};
my $dlg = Wx::FileDialog->new($self, 'Save ' . ($params{export_svg} ? 'SVG' : 'G-code') . ' file as:',
wxTheApp->output_path(dirname($output_file)),
basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE);
basename($output_file), $params{export_svg} ? &Slic3r::GUI::FILE_WILDCARDS->{svg} : &Slic3r::GUI::FILE_WILDCARDS->{gcode}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if ($dlg->ShowModal != wxID_OK) {
$dlg->Destroy;
return;
}
$output_file = $dlg->GetPath;
$last_output_file = $output_file unless $params{export_svg};
$output_file = Slic3r::decode_path($dlg->GetPath);
$qs_last_output_file = $output_file unless $params{export_svg};
$Slic3r::GUI::Settings->{_}{last_output_path} = dirname($output_file);
wxTheApp->save_settings;
$dlg->Destroy;
@ -383,7 +449,7 @@ sub repair_stl {
$dialog->Destroy;
return;
}
$input_file = $dialog->GetPaths;
$input_file = Slic3r::decode_path($dialog->GetPaths);
$dialog->Destroy;
}
@ -396,7 +462,7 @@ sub repair_stl {
$dlg->Destroy;
return undef;
}
$output_file = $dlg->GetPath;
$output_file = Slic3r::decode_path($dlg->GetPath);
$dlg->Destroy;
}
@ -412,7 +478,7 @@ sub extra_variables {
my %extra_variables = ();
if ($self->{mode} eq 'expert') {
$extra_variables{"${_}_preset"} = $self->{options_tabs}{$_}->current_preset->{name}
$extra_variables{"${_}_preset"} = $self->{options_tabs}{$_}->get_current_preset->name
for qw(print filament printer);
}
return { %extra_variables };
@ -433,7 +499,7 @@ sub export_config {
my $dlg = Wx::FileDialog->new($self, 'Save configuration as:', $dir, $filename,
&Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if ($dlg->ShowModal == wxID_OK) {
my $file = $dlg->GetPath;
my $file = Slic3r::decode_path($dlg->GetPath);
$Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
wxTheApp->save_settings;
$last_config = $file;
@ -452,7 +518,7 @@ sub load_config_file {
my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
&Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
return unless $dlg->ShowModal == wxID_OK;
($file) = $dlg->GetPaths;
$file = Slic3r::decode_path($dlg->GetPaths);
$dlg->Destroy;
}
$Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
@ -477,7 +543,7 @@ sub export_configbundle {
my $dlg = Wx::FileDialog->new($self, 'Save presets bundle as:', $dir, $filename,
&Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if ($dlg->ShowModal == wxID_OK) {
my $file = $dlg->GetPath;
my $file = Slic3r::decode_path($dlg->GetPath);
$Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
wxTheApp->save_settings;
@ -504,14 +570,16 @@ sub export_configbundle {
}
sub load_configbundle {
my $self = shift;
my ($self, $file, $skip_no_id) = @_;
my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
&Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
return unless $dlg->ShowModal == wxID_OK;
my ($file) = $dlg->GetPaths;
$dlg->Destroy;
if (!$file) {
my $dir = $last_config ? dirname($last_config) : $Slic3r::GUI::Settings->{recent}{config_directory} || $Slic3r::GUI::Settings->{recent}{skein_directory} || '';
my $dlg = Wx::FileDialog->new($self, 'Select configuration to load:', $dir, "config.ini",
&Slic3r::GUI::FILE_WILDCARDS->{ini}, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
return unless $dlg->ShowModal == wxID_OK;
$file = Slic3r::decode_path($dlg->GetPaths);
$dlg->Destroy;
}
$Slic3r::GUI::Settings->{recent}{config_directory} = dirname($file);
wxTheApp->save_settings;
@ -537,11 +605,23 @@ sub load_configbundle {
}
}
my $imported = 0;
foreach my $ini_category (sort keys %$ini) {
INI_BLOCK: foreach my $ini_category (sort keys %$ini) {
next unless $ini_category =~ /^(print|filament|printer):(.+)$/;
my ($section, $preset_name) = ($1, $2);
my $config = Slic3r::Config->load_ini_hash($ini->{$ini_category});
next if $skip_no_id && !$config->get($section . "_settings_id");
{
my %current_presets = Slic3r::GUI->presets($section);
my %current_ids = map { $_ => 1 }
grep $_,
map Slic3r::Config->load($_)->get($section . "_settings_id"),
values %current_presets;
next INI_BLOCK if exists $current_ids{$config->get($section . "_settings_id")};
}
$config->save(sprintf "$Slic3r::GUI::datadir/%s/%s.ini", $section, $preset_name);
Slic3r::debugf "Imported %s preset %s\n", $section, $preset_name;
$imported++;
}
if ($self->{mode} eq 'expert') {
@ -549,6 +629,9 @@ sub load_configbundle {
$tab->load_presets;
}
}
return if !$imported;
my $message = sprintf "%d presets successfully imported.", $imported;
if ($self->{mode} eq 'simple' && $Slic3r::GUI::Settings->{_}{mode} eq 'expert') {
Slic3r::GUI::show_info($self, "$message You need to restart Slic3r to make the changes effective.");
@ -564,6 +647,9 @@ sub load_config {
foreach my $tab (values %{$self->{options_tabs}}) {
$tab->load_config($config);
}
if ($self->{plater}) {
$self->{plater}->on_config_change($config);
}
}
sub config_wizard {
@ -575,6 +661,8 @@ sub config_wizard {
for my $tab (values %{$self->{options_tabs}}) {
$tab->select_default_preset;
}
} else {
# TODO: select default settings in simple mode
}
$self->load_config($config);
if ($self->{mode} eq 'expert') {
@ -604,13 +692,19 @@ sub config {
if (!$self->{plater} || $self->{plater}->filament_presets == 1 || $self->{mode} eq 'simple') {
$filament_config = $self->{options_tabs}{filament}->config;
} else {
# TODO: handle dirty presets.
# perhaps plater shouldn't expose dirty presets at all in multi-extruder environments.
my $i = -1;
foreach my $preset_idx ($self->{plater}->filament_presets) {
$i++;
my $preset = $self->{options_tabs}{filament}->get_preset($preset_idx);
my $config = $self->{options_tabs}{filament}->get_preset_config($preset);
my $config;
if ($preset_idx == $self->{options_tabs}{filament}->current_preset) {
# the selected preset for this extruder is the one in the tab
# use the tab's config instead of the preset in case it is dirty
# perhaps plater shouldn't expose dirty presets at all in multi-extruder environments.
$config = $self->{options_tabs}{filament}->config;
} else {
my $preset = $self->{options_tabs}{filament}->get_preset($preset_idx);
$config = $self->{options_tabs}{filament}->get_preset_config($preset);
}
if (!$filament_config) {
$filament_config = $config->clone;
next;
@ -639,16 +733,31 @@ sub config {
} else {
my $extruders_count = $self->{options_tabs}{printer}{extruders_count};
$config->set("${_}_extruder", min($config->get("${_}_extruder"), $extruders_count))
for qw(perimeter infill support_material support_material_interface);
for qw(perimeter infill solid_infill support_material support_material_interface);
}
return $config;
}
sub filament_preset_names {
my ($self) = @_;
if ($self->{mode} eq 'simple') {
return '';
}
return map $self->{options_tabs}{filament}->get_preset($_)->name,
$self->{plater}->filament_presets;
}
sub check_unsaved_changes {
my $self = shift;
my @dirty = map $_->title, grep $_->is_dirty, values %{$self->{options_tabs}};
my @dirty = ();
foreach my $tab (values %{$self->{options_tabs}}) {
push @dirty, $tab->title if $tab->is_dirty;
}
if (@dirty) {
my $titles = join ', ', @dirty;
my $confirm = Wx::MessageDialog->new($self, "You have unsaved changes ($titles). Discard changes and continue anyway?",
@ -661,16 +770,27 @@ sub check_unsaved_changes {
sub select_tab {
my ($self, $tab) = @_;
$self->{tabpanel}->ChangeSelection($tab);
$self->{tabpanel}->SetSelection($tab);
}
sub _append_menu_item {
my ($self, $menu, $string, $description, $cb, $id) = @_;
my ($self, $menu, $string, $description, $cb, $id, $icon) = @_;
$id //= &Wx::NewId();
my $item = $menu->Append($id, $string, $description);
$self->_set_menu_item_icon($item, $icon);
EVT_MENU($self, $id, $cb);
return $item;
}
sub _set_menu_item_icon {
my ($self, $menuItem, $icon) = @_;
# SetBitmap was not available on OS X before Wx 0.9927
if ($icon && $menuItem->can('SetBitmap')) {
$menuItem->SetBitmap(Wx::Bitmap->new($Slic3r::var->($icon), wxBITMAP_TYPE_PNG));
}
}
1;

View File

@ -3,7 +3,7 @@ use Moo;
has 'growler' => (is => 'rw');
my $icon = "$Slic3r::var/Slic3r.png";
my $icon = $Slic3r::var->("Slic3r.png");
sub BUILD {
my ($self) = @_;
@ -14,6 +14,8 @@ sub BUILD {
$self->growler(Growl::GNTP->new(AppName => 'Slic3r', AppIcon => $icon));
$self->growler->register([{Name => 'SKEIN_DONE', DisplayName => 'Slicing Done'}]);
};
# if register() fails (for example because of a timeout), disable growler at all
$self->growler(undef) if $@;
}
}

View File

@ -83,7 +83,7 @@ sub append_line {
# if we have a single option with no sidetext just add it directly to the grid sizer
my @options = @{$line->get_options};
$self->_options->{$_->opt_id} = $_ for @options;
if (@options == 1 && !$options[0]->sidetext) {
if (@options == 1 && !$options[0]->sidetext && !$options[0]->side_widget && !@{$line->get_extra_widgets}) {
my $option = $options[0];
my $field = $self->_build_field($option);
$grid_sizer->Add($field, 0, ($option->full_width ? wxEXPAND : 0) | wxALIGN_CENTER_VERTICAL, 0);
@ -95,7 +95,9 @@ sub append_line {
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
$grid_sizer->Add($sizer, 0, 0, 0);
foreach my $option (@options) {
foreach my $i (0..$#options) {
my $option = $options[$i];
# add label if any
if ($option->label) {
my $field_label = Wx::StaticText->new($self->parent, -1, $option->label . ":", wxDefaultPosition, wxDefaultSize);
@ -111,12 +113,26 @@ sub append_line {
if ($option->sidetext) {
my $sidetext = Wx::StaticText->new($self->parent, -1, $option->sidetext, wxDefaultPosition, wxDefaultSize);
$sidetext->SetFont($self->sidetext_font);
$sizer->Add($sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL , 4);
$sizer->Add($sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 4);
}
# add side widget if any
if ($option->side_widget) {
$sizer->Add($option->side_widget->($self->parent), 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 1);
}
if ($option != $#options) {
$sizer->AddSpacer(4);
}
}
# add extra sizers if any
foreach my $extra_widget (@{$line->get_extra_widgets}) {
$sizer->Add($extra_widget->($self->parent), 0, wxLEFT | wxALIGN_CENTER_VERTICAL , 4);
}
}
sub append_single_option_line {
sub create_single_option_line {
my ($self, $option) = @_;
my $line = Slic3r::GUI::OptionsGroup::Line->new(
@ -125,11 +141,15 @@ sub append_single_option_line {
);
$option->label("");
$line->append_option($option);
$self->append_line($line);
return $line;
}
sub append_single_option_line {
my ($self, $option) = @_;
return $self->append_line($self->create_single_option_line($option));
}
sub _build_field {
my $self = shift;
my ($opt) = @_;
@ -158,12 +178,17 @@ sub _build_field {
parent => $self->parent,
option => $opt,
);
} elsif ($type eq 'color') {
$field = Slic3r::GUI::OptionsGroup::Field::ColourPicker->new(
parent => $self->parent,
option => $opt,
);
} elsif ($type =~ /^(f|s|s@|percent)$/) {
$field = Slic3r::GUI::OptionsGroup::Field::TextCtrl->new(
parent => $self->parent,
option => $opt,
);
} elsif ($type eq 'select') {
} elsif ($type eq 'select' || $type eq 'select_open') {
$field = Slic3r::GUI::OptionsGroup::Field::Choice->new(
parent => $self->parent,
option => $opt,
@ -222,8 +247,20 @@ sub set_value {
}
sub _on_change {
my ($self, $opt_id) = @_;
$self->on_change->($opt_id);
my ($self, $opt_id, $value) = @_;
$self->on_change->($opt_id, $value);
}
sub enable {
my ($self) = @_;
$_->enable for values %{$self->_fields};
}
sub disable {
my ($self) = @_;
$_->disable for values %{$self->_fields};
}
sub _on_kill_focus {
@ -241,6 +278,7 @@ has 'label_tooltip' => (is => 'rw', default => sub { "" });
has 'sizer' => (is => 'rw');
has 'widget' => (is => 'rw');
has '_options' => (is => 'ro', default => sub { [] });
has '_extra_widgets' => (is => 'ro', default => sub { [] });
# this method accepts a Slic3r::GUI::OptionsGroup::Option object
sub append_option {
@ -248,11 +286,21 @@ sub append_option {
push @{$self->_options}, $option;
}
sub append_widget {
my ($self, $widget) = @_;
push @{$self->_extra_widgets}, $widget;
}
sub get_options {
my ($self) = @_;
return [ @{$self->_options} ];
}
sub get_extra_widgets {
my ($self) = @_;
return [ @{$self->_extra_widgets} ];
}
package Slic3r::GUI::OptionsGroup::Option;
use Moo;
@ -274,6 +322,7 @@ has 'max' => (is => 'rw', default => sub { undef });
has 'labels' => (is => 'rw', default => sub { [] });
has 'values' => (is => 'rw', default => sub { [] });
has 'readonly' => (is => 'rw', default => sub { 0 });
has 'side_widget' => (is => 'rw', default => sub { undef });
package Slic3r::GUI::ConfigOptionsGroup;
@ -309,7 +358,8 @@ sub get_option {
gui_flags => $optdef->{gui_flags},
label => ($self->full_labels && defined $optdef->{full_label}) ? $optdef->{full_label} : $optdef->{label},
sidetext => $optdef->{sidetext},
tooltip => $optdef->{tooltip} . " (default: " . $default_value . ")",
# calling serialize() ensures we get a stringified value
tooltip => $optdef->{tooltip} . " (default: " . $self->config->serialize($opt_key) . ")",
multiline => $optdef->{multiline},
width => $optdef->{width},
min => $optdef->{min},
@ -320,7 +370,7 @@ sub get_option {
);
}
sub append_single_option_line {
sub create_single_option_line {
my ($self, $opt_key, $opt_index) = @_;
my $option;
@ -329,7 +379,12 @@ sub append_single_option_line {
} else {
$option = $self->get_option($opt_key, $opt_index);
}
return $self->SUPER::append_single_option_line($option);
return $self->SUPER::create_single_option_line($option);
}
sub append_single_option_line {
my ($self, $option, $opt_index) = @_;
return $self->append_line($self->create_single_option_line($option, $opt_index));
}
sub reload_config {
@ -365,7 +420,7 @@ sub _get_config_value {
}
sub _on_change {
my ($self, $opt_id) = @_;
my ($self, $opt_id, $value) = @_;
if (exists $self->_opt_map->{$opt_id}) {
my ($opt_key, $opt_index) = @{ $self->_opt_map->{$opt_id} };
@ -387,7 +442,7 @@ sub _on_change {
}
}
$self->SUPER::_on_change($opt_id);
$self->SUPER::_on_change($opt_id, $value);
}
sub _on_kill_focus {

View File

@ -7,7 +7,6 @@ has 'parent' => (is => 'ro', required => 1);
has 'option' => (is => 'ro', required => 1); # Slic3r::GUI::OptionsGroup::Option
has 'on_change' => (is => 'rw', default => sub { sub {} });
has 'on_kill_focus' => (is => 'rw', default => sub { sub {} });
has 'wxSsizer' => (is => 'rw'); # alternatively, wxSizer object
has 'disable_change_event' => (is => 'rw', default => sub { 0 });
# This method should not fire the on_change event
@ -36,7 +35,7 @@ sub toggle {
sub _on_change {
my ($self, $opt_id) = @_;
$self->on_change->($opt_id)
$self->on_change->($opt_id, $self->get_value)
unless $self->disable_change_event;
}
@ -128,6 +127,8 @@ extends 'Slic3r::GUI::OptionsGroup::Field::wxWindow';
use Wx qw(:misc);
use Wx::Event qw(EVT_SPINCTRL EVT_TEXT EVT_KILL_FOCUS);
has 'tmp_value' => (is => 'rw');
sub BUILD {
my ($self) = @_;
@ -136,16 +137,34 @@ sub BUILD {
$self->wxWindow($field);
EVT_SPINCTRL($self->parent, $field, sub {
$self->tmp_value(undef);
$self->_on_change($self->option->opt_id);
});
EVT_TEXT($self->parent, $field, sub {
my ($s, $event) = @_;
# On OSX/Cocoa, wxSpinCtrl::GetValue() doesn't return the new value
# when it was changed from the text control, so the on_change callback
# gets the old one, and on_kill_focus resets the control to the old value.
# As a workaround, we get the new value from $event->GetString and store
# here temporarily so that we can return it from $self->get_value
$self->tmp_value($event->GetString) if $event->GetString =~ /^\d+$/;
$self->_on_change($self->option->opt_id);
# We don't reset tmp_value here because _on_change might put callbacks
# in the CallAfter queue, and we want the tmp value to be available from
# them as well.
});
EVT_KILL_FOCUS($field, sub {
$self->tmp_value(undef);
$self->_on_kill_focus($self->option->opt_id, @_);
});
}
sub get_value {
my ($self) = @_;
return $self->tmp_value // $self->wxWindow->GetValue;
}
package Slic3r::GUI::OptionsGroup::Field::TextCtrl;
use Moo;
@ -194,44 +213,86 @@ extends 'Slic3r::GUI::OptionsGroup::Field::wxWindow';
use List::Util qw(first);
use Wx qw(:misc :combobox);
use Wx::Event qw(EVT_COMBOBOX);
use Wx::Event qw(EVT_COMBOBOX EVT_TEXT);
sub BUILD {
my ($self) = @_;
my $style = 0;
$style |= wxCB_READONLY if defined $self->option->gui_type && $self->option->gui_type ne 'select_open';
my $field = Wx::ComboBox->new($self->parent, -1, "", wxDefaultPosition, $self->_default_size,
$self->option->labels || $self->option->values, wxCB_READONLY);
$self->option->labels || $self->option->values || [], $style);
$self->wxWindow($field);
$self->set_value($self->option->default);
EVT_COMBOBOX($self->parent, $field, sub {
$self->_on_change($self->option->opt_id);
});
EVT_TEXT($self->parent, $field, sub {
$self->_on_change($self->option->opt_id);
});
}
sub set_value {
my ($self, $value) = @_;
my $idx = first { $self->option->values->[$_] eq $value } 0..$#{$self->option->values};
$self->disable_change_event(1);
my $idx;
if ($self->option->values) {
$idx = first { $self->option->values->[$_] eq $value } 0..$#{$self->option->values};
# if value is not among indexes values we use SetValue()
}
if (defined $idx) {
$self->wxWindow->SetSelection($idx);
} else {
$self->wxWindow->SetValue($value);
}
$self->disable_change_event(0);
}
sub set_values {
my ($self, $values) = @_;
$self->disable_change_event(1);
$self->wxWindow->SetSelection($idx);
# it looks that Clear() also clears the text field in recent wxWidgets versions,
# but we want to preserve it
my $ww = $self->wxWindow;
my $value = $ww->GetValue;
$ww->Clear;
$ww->Append($_) for @$values;
$ww->SetValue($value);
$self->disable_change_event(0);
}
sub get_value {
my ($self) = @_;
return $self->option->values->[$self->wxWindow->GetSelection];
if ($self->option->values) {
my $idx = $self->wxWindow->GetSelection;
if ($idx != &Wx::wxNOT_FOUND) {
return $self->option->values->[$idx];
}
}
return $self->wxWindow->GetValue;
}
package Slic3r::GUI::OptionsGroup::Field::NumericChoice;
use Moo;
extends 'Slic3r::GUI::OptionsGroup::Field::wxWindow';
use List::Util qw(first);
use Wx qw(:misc :combobox);
use Wx qw(wxTheApp :misc :combobox);
use Wx::Event qw(EVT_COMBOBOX EVT_TEXT);
# if option has no 'values', indices are values
# if option has no 'labels', values are labels
sub BUILD {
my ($self) = @_;
@ -245,18 +306,28 @@ sub BUILD {
my $disable_change_event = $self->disable_change_event;
$self->disable_change_event(1);
my $value = $field->GetSelection;
my $idx = $field->GetSelection; # get index of selected value
my $label;
if ($self->option->values) {
$label = $value = $self->option->values->[$value];
} elsif ($value <= $#{$self->option->labels}) {
$label = $self->option->labels->[$value];
if ($self->option->labels && $idx <= $#{$self->option->labels}) {
$label = $self->option->labels->[$idx];
} elsif ($self->option->values && $idx <= $#{$self->option->values}) {
$label = $self->option->values->[$idx];
} else {
$label = $value;
$label = $idx;
}
$field->SetValue($label);
# The MSW implementation of wxComboBox will leave the field blank if we call
# SetValue() in the EVT_COMBOBOX event handler, so we postpone the call.
wxTheApp->CallAfter(sub {
my $dce = $self->disable_change_event;
$self->disable_change_event(1);
# ChangeValue() is not exported in wxPerl
$field->SetValue($label);
$self->disable_change_event($dce);
});
$self->disable_change_event($disable_change_event);
$self->_on_change($self->option->opt_id);
@ -283,8 +354,8 @@ sub set_value {
$self->disable_change_event(0);
return;
}
}
if ($self->option->labels && $value <= $#{$self->option->labels}) {
} elsif ($self->option->labels && $value <= $#{$self->option->labels}) {
# if we have no values, we expect value to be an index
$field->SetValue($self->option->labels->[$value]);
$self->disable_change_event(0);
return;
@ -299,17 +370,60 @@ sub get_value {
my ($self) = @_;
my $label = $self->wxWindow->GetValue;
my $value_idx = first { $self->option->labels->[$_] eq $label } 0..$#{$self->option->labels};
if (defined $value_idx) {
if ($self->option->values) {
return $self->option->values->[$value_idx];
if ($self->option->labels) {
my $value_idx = first { $self->option->labels->[$_] eq $label } 0..$#{$self->option->labels};
if (defined $value_idx) {
if ($self->option->values) {
return $self->option->values->[$value_idx];
}
return $value_idx;
}
return $value_idx;
}
return $label;
}
package Slic3r::GUI::OptionsGroup::Field::ColourPicker;
use Moo;
extends 'Slic3r::GUI::OptionsGroup::Field::wxWindow';
use Wx qw(:misc :colour);
use Wx::Event qw(EVT_COLOURPICKER_CHANGED);
sub BUILD {
my ($self) = @_;
my $field = Wx::ColourPickerCtrl->new($self->parent, -1,
$self->_string_to_colour($self->option->default), wxDefaultPosition,
$self->_default_size);
$self->wxWindow($field);
EVT_COLOURPICKER_CHANGED($self->parent, $field, sub {
$self->_on_change($self->option->opt_id);
});
}
sub set_value {
my ($self, $value) = @_;
$self->disable_change_event(1);
$self->wxWindow->SetColour($self->_string_to_colour($value));
$self->disable_change_event(0);
}
sub get_value {
my ($self) = @_;
return $self->wxWindow->GetColour->GetAsString(wxC2S_HTML_SYNTAX);
}
sub _string_to_colour {
my ($self, $string) = @_;
$string =~ s/^#//;
return Wx::Colour->new(unpack 'C*', pack 'H*', $string);
}
package Slic3r::GUI::OptionsGroup::Field::wxSizer;
use Moo;
extends 'Slic3r::GUI::OptionsGroup::Field';
@ -335,6 +449,7 @@ sub BUILD {
$self->wxSizer($sizer);
my $field_size = Wx::Size->new(40, -1);
$self->x_textctrl(Wx::TextCtrl->new($self->parent, -1, $self->option->default->[X], wxDefaultPosition, $field_size));
$self->y_textctrl(Wx::TextCtrl->new($self->parent, -1, $self->option->default->[Y], wxDefaultPosition, $field_size));
@ -397,11 +512,11 @@ extends 'Slic3r::GUI::OptionsGroup::Field::wxSizer';
has 'scale' => (is => 'rw', default => sub { 10 });
has 'slider' => (is => 'rw');
has 'statictext' => (is => 'rw');
has 'textctrl' => (is => 'rw');
use Slic3r::Geometry qw(X Y);
use Wx qw(:misc :sizer);
use Wx::Event qw(EVT_SLIDER);
use Wx::Event qw(EVT_SLIDER EVT_TEXT EVT_KILL_FOCUS);
sub BUILD {
my ($self) = @_;
@ -419,23 +534,35 @@ sub BUILD {
);
$self->slider($slider);
my $statictext = Wx::StaticText->new($self->parent, -1, $slider->GetValue/$self->scale);
$self->statictext($statictext);
my $textctrl = Wx::TextCtrl->new($self->parent, -1, $slider->GetValue/$self->scale,
wxDefaultPosition, [50,-1]);
$self->textctrl($textctrl);
$sizer->Add($_, 0, wxALIGN_CENTER_VERTICAL, 0) for $slider, $statictext;
$sizer->Add($slider, 1, wxALIGN_CENTER_VERTICAL, 0);
$sizer->Add($textctrl, 0, wxALIGN_CENTER_VERTICAL, 0);
EVT_SLIDER($self->parent, $slider, sub {
$self->_update_statictext;
$self->_update_textctrl;
$self->_on_change($self->option->opt_id);
});
EVT_TEXT($self->parent, $textctrl, sub {
my $value = $textctrl->GetValue;
if ($value =~ /^-?\d+(\.\d*)?$/) {
$self->set_value($value);
$self->_on_change($self->option->opt_id);
}
});
EVT_KILL_FOCUS($textctrl, sub {
$self->_on_kill_focus($self->option->opt_id, @_);
});
}
sub set_value {
my ($self, $value) = @_;
$self->disable_change_event(1);
$self->slider->SetValue($value);
$self->_update_statictext;
$self->slider->SetValue($value*$self->scale);
$self->_update_textctrl;
$self->disable_change_event(0);
}
@ -444,9 +571,25 @@ sub get_value {
return $self->slider->GetValue/$self->scale;
}
sub _update_statictext {
sub _update_textctrl {
my ($self) = @_;
$self->statictext->SetLabel($self->get_value);
$self->textctrl->SetLabel($self->get_value);
}
sub enable {
my ($self) = @_;
$self->slider->Enable;
$self->textctrl->Enable;
$self->textctrl->SetEditable(1);
}
sub disable {
my ($self) = @_;
$self->slider->Disable;
$self->textctrl->Disable;
$self->textctrl->SetEditable(0);
}
1;

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@ sub new {
$self->{on_select_object} = sub {};
$self->{on_double_click} = sub {};
$self->{on_right_click} = sub {};
$self->{on_instance_moved} = sub {};
$self->{on_instances_moved} = sub {};
$self->{objects_brush} = Wx::Brush->new(Wx::Colour->new(210,210,210), wxSOLID);
$self->{selected_brush} = Wx::Brush->new(Wx::Colour->new(255,128,128), wxSOLID);
@ -63,15 +63,15 @@ sub on_right_click {
$self->{on_right_click} = $cb;
}
sub on_instance_moved {
sub on_instances_moved {
my ($self, $cb) = @_;
$self->{on_instance_moved} = $cb;
$self->{on_instances_moved} = $cb;
}
sub repaint {
my ($self, $event) = @_;
my $dc = Wx::PaintDC->new($self);
my $dc = Wx::AutoBufferedPaintDC->new($self);
my $size = $self->GetSize;
my @size = ($size->GetWidth, $size->GetHeight);
@ -183,9 +183,11 @@ sub mouse_event {
my $point = $self->point_to_model_units([ $pos->x, $pos->y ]); #]]
if ($event->ButtonDown) {
$self->{on_select_object}->(undef);
OBJECTS: for my $obj_idx (0 .. $#{$self->{objects}}) {
# traverse objects and instances in reverse order, so that if they're overlapping
# we get the one that gets drawn last, thus on top (as user expects that to move)
OBJECTS: for my $obj_idx (reverse 0 .. $#{$self->{objects}}) {
my $object = $self->{objects}->[$obj_idx];
for my $instance_idx (0 .. $#{ $object->instance_thumbnails }) {
for my $instance_idx (reverse 0 .. $#{ $object->instance_thumbnails }) {
my $thumbnail = $object->instance_thumbnails->[$instance_idx];
if (defined first { $_->contour->contains_point($point) } @$thumbnail) {
$self->{on_select_object}->($obj_idx);
@ -208,13 +210,13 @@ sub mouse_event {
}
}
$self->Refresh;
} elsif ($event->ButtonUp(&Wx::wxMOUSE_BTN_LEFT)) {
$self->{on_instance_moved}->();
$self->Refresh;
} elsif ($event->LeftUp) {
$self->{on_instances_moved}->()
if $self->{drag_object};
$self->{drag_start_pos} = undef;
$self->{drag_object} = undef;
$self->SetCursor(wxSTANDARD_CURSOR);
} elsif ($event->ButtonDClick) {
} elsif ($event->LeftDClick) {
$self->{on_double_click}->();
} elsif ($event->Dragging) {
return if !$self->{drag_start_pos}; # concurrency problems
@ -274,7 +276,7 @@ sub update_bed_size {
push @polylines, Slic3r::Polyline->new([$bb->x_min, $y], [$bb->x_max, $y]);
}
@polylines = @{intersection_pl(\@polylines, [$polygon])};
$self->{grid} = [ map $self->scaled_points_to_pixel(\@$_, 1), @polylines ];
$self->{grid} = [ map $self->scaled_points_to_pixel([ @$_[0,-1] ], 1), @polylines ];
}
}

View File

@ -3,21 +3,19 @@ use strict;
use warnings;
use utf8;
use List::Util qw();
use Slic3r::Geometry qw();
use Wx qw(:misc :sizer :slider :statictext);
use Slic3r::Print::State ':steps';
use Wx qw(:misc :sizer :slider :statictext wxWHITE);
use Wx::Event qw(EVT_SLIDER EVT_KEY_DOWN);
use base 'Wx::Panel';
use base qw(Wx::Panel Class::Accessor);
__PACKAGE__->mk_accessors(qw(print enabled));
sub new {
my $class = shift;
my ($parent, $print) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition);
# init print
$self->{print} = $print;
$self->reload_print;
$self->SetBackgroundColour(wxWHITE);
# init GUI elements
my $canvas = $self->{canvas} = Slic3r::GUI::Plater::2DToolpaths::Canvas->new($self, $print);
@ -25,7 +23,9 @@ sub new {
$self, -1,
0, # default
0, # min
scalar(@{$self->{layers_z}})-1, # max
# we set max to a bogus non-zero value because the MSW implementation of wxSlider
# will skip drawing the slider if max <= min:
1, # max
wxDefaultPosition,
wxDefaultSize,
wxVERTICAL | wxSL_INVERSE,
@ -43,7 +43,8 @@ sub new {
$sizer->Add($vsizer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
EVT_SLIDER($self, $slider, sub {
$self->set_z($self->{layers_z}[$slider->GetValue]);
$self->set_z($self->{layers_z}[$slider->GetValue])
if $self->enabled;
});
EVT_KEY_DOWN($canvas, sub {
my ($s, $event) = @_;
@ -62,7 +63,9 @@ sub new {
$self->SetMinSize($self->GetSize);
$sizer->SetSizeHints($self);
$self->set_z($self->{layers_z}[0]);
# init print
$self->{print} = $print;
$self->reload_print;
return $self;
}
@ -70,18 +73,42 @@ sub new {
sub reload_print {
my ($self) = @_;
# we require that there's at least one object and the posSlice step
# is performed on all of them (this ensures that _shifted_copies was
# populated and we know the number of layers)
if (!$self->print->object_step_done(STEP_SLICE)) {
$self->enabled(0);
$self->{slider}->Hide;
$self->{canvas}->Refresh; # clears canvas
return;
}
$self->{canvas}->bb($self->print->total_bounding_box);
$self->{canvas}->_dirty(1);
my %z = (); # z => 1
foreach my $object (@{$self->{print}->objects}) {
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
$z{$layer->print_z} = 1;
}
}
$self->enabled(1);
$self->{layers_z} = [ sort { $a <=> $b } keys %z ];
$self->{slider}->SetRange(0, scalar(@{$self->{layers_z}})-1);
if ((my $z_idx = $self->{slider}->GetValue) <= $#{$self->{layers_z}}) {
$self->set_z($self->{layers_z}[$z_idx]);
} else {
$self->{slider}->SetValue(0);
$self->set_z($self->{layers_z}[0]) if @{$self->{layers_z}};
}
$self->{slider}->Show;
$self->Layout;
}
sub set_z {
my ($self, $z) = @_;
return if !$self->enabled;
$self->{z_label}->SetLabel(sprintf '%.2f', $z);
$self->{canvas}->set_z($z);
}
@ -89,14 +116,23 @@ sub set_z {
package Slic3r::GUI::Plater::2DToolpaths::Canvas;
use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
use OpenGL qw(:glconstants :glfunctions :glufunctions);
use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
use OpenGL qw(:glconstants :glfunctions :glufunctions :gluconstants);
use base qw(Wx::GLCanvas Class::Accessor);
use Wx::GLCanvas qw(:all);
use List::Util qw(min first);
use Slic3r::Geometry qw(scale unscale epsilon);
use List::Util qw(min max first);
use Slic3r::Geometry qw(scale unscale epsilon X Y);
use Slic3r::Print::State ':steps';
__PACKAGE__->mk_accessors(qw(print z layers color init dirty bb));
__PACKAGE__->mk_accessors(qw(
print z layers color init
bb
_camera_bb
_dirty
_zoom
_camera_target
_drag_start_xy
));
# make OpenGL::Array thread-safe
{
@ -109,23 +145,103 @@ sub new {
my $self = $class->SUPER::new($parent);
$self->print($print);
$self->bb($self->print->total_bounding_box);
$self->_zoom(1);
# 2D point in model space
$self->_camera_target(Slic3r::Pointf->new(0,0));
EVT_PAINT($self, sub {
my $dc = Wx::PaintDC->new($self);
$self->Render($dc);
});
EVT_SIZE($self, sub { $self->dirty(1) });
EVT_SIZE($self, sub { $self->_dirty(1) });
EVT_IDLE($self, sub {
return unless $self->dirty;
return unless $self->_dirty;
return if !$self->IsShownOnScreen;
$self->Resize( $self->GetSizeWH );
$self->Resize;
$self->Refresh;
});
EVT_MOUSEWHEEL($self, sub {
my ($self, $e) = @_;
return if !$self->GetParent->enabled;
my $old_zoom = $self->_zoom;
# Calculate the zoom delta and apply it to the current zoom factor
my $zoom = $e->GetWheelRotation() / $e->GetWheelDelta();
$zoom = max(min($zoom, 4), -4);
$zoom /= 10;
$self->_zoom($self->_zoom / (1-$zoom));
$self->_zoom(1) if $self->_zoom > 1; # prevent from zooming out too much
{
# In order to zoom around the mouse point we need to translate
# the camera target. This math is almost there but not perfect yet...
my $camera_bb_size = $self->_camera_bb->size;
my $size = Slic3r::Pointf->new($self->GetSizeWH);
my $pos = Slic3r::Pointf->new($e->GetPositionXY);
# calculate the zooming center in pixel coordinates relative to the viewport center
my $vec = Slic3r::Pointf->new($pos->x - $size->x/2, $pos->y - $size->y/2); #-
# calculate where this point will end up after applying the new zoom
my $vec2 = $vec->clone;
$vec2->scale($old_zoom / $self->_zoom);
# move the camera target by the difference of the two positions
$self->_camera_target->translate(
-($vec->x - $vec2->x) * $camera_bb_size->x / $size->x,
($vec->y - $vec2->y) * $camera_bb_size->y / $size->y, #//
);
}
$self->_dirty(1);
$self->Refresh;
});
EVT_MOUSE_EVENTS($self, \&mouse_event);
return $self;
}
sub mouse_event {
my ($self, $e) = @_;
return if !$self->GetParent->enabled;
my $pos = Slic3r::Pointf->new($e->GetPositionXY);
if ($e->Entering && &Wx::wxMSW) {
# wxMSW needs focus in order to catch mouse wheel events
$self->SetFocus;
} elsif ($e->Dragging) {
if ($e->LeftIsDown || $e->MiddleIsDown || $e->RightIsDown) {
# if dragging, translate view
if (defined $self->_drag_start_xy) {
my $move = $self->_drag_start_xy->vector_to($pos); # in pixels
# get viewport and camera size in order to convert pixel to model units
my ($x, $y) = $self->GetSizeWH;
my $camera_bb_size = $self->_camera_bb->size;
# compute translation in model units
$self->_camera_target->translate(
-$move->x * $camera_bb_size->x / $x,
$move->y * $camera_bb_size->y / $y, # /**
);
$self->_dirty(1);
$self->Refresh;
}
$self->_drag_start_xy($pos);
}
} elsif ($e->LeftUp || $e->MiddleUp || $e->RightUp) {
$self->_drag_start_xy(undef);
} else {
$e->Skip();
}
}
sub set_z {
my ($self, $z) = @_;
@ -150,9 +266,10 @@ sub set_z {
}
}
# reverse layers so that we draw the lowermost (i.e. current) on top
$self->z($z);
$self->layers([ @layers ]);
$self->dirty(1);
$self->layers([ reverse @layers ]);
$self->Refresh;
}
sub Render {
@ -164,28 +281,79 @@ sub Render {
$self->SetCurrent($context);
$self->InitGL;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
my $bb = $self->bb;
my ($x1, $y1, $x2, $y2) = ($bb->x_min, $bb->y_min, $bb->x_max, $bb->y_max);
my ($x, $y) = $self->GetSizeWH;
if (($x2 - $x1)/($y2 - $y1) > $x/$y) {
# adjust Y
my $new_y = $y * ($x2 - $x1) / $x;
$y1 = ($y2 + $y1)/2 - $new_y/2;
$y2 = $y1 + $new_y;
} else {
my $new_x = $x * ($y2 - $y1) / $y;
$x1 = ($x2 + $x1)/2 - $new_x/2;
$x2 = $x1 + $new_x;
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
if (!$self->GetParent->enabled || !$self->layers) {
glFlush();
$self->SwapBuffers;
return;
}
glOrtho($x1, $x2, $y1, $y2, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
# anti-alias
if (0) {
glEnable(GL_LINE_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
}
my $tess;
if (!(&Wx::wxMSW && $OpenGL::VERSION < 0.6704)) {
# We can't use the GLU tesselator on MSW with older OpenGL versions
# because of an upstream bug:
# http://sourceforge.net/p/pogl/bugs/16/
$tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
}
foreach my $layer (@{$self->layers}) {
my $object = $layer->object;
# only draw the slice for the current layer
next unless abs($layer->print_z - $self->z) < epsilon;
# draw slice contour
glLineWidth(1);
foreach my $copy (@{ $object->_shifted_copies }) {
glPushMatrix();
glTranslatef(@$copy, 0);
foreach my $slice (@{$layer->slices}) {
glColor3f(0.95, 0.95, 0.95);
if ($tess) {
gluTessBeginPolygon($tess);
foreach my $polygon (@$slice) {
gluTessBeginContour($tess);
gluTessVertex_p($tess, @$_, 0) for @$polygon;
gluTessEndContour($tess);
}
gluTessEndPolygon($tess);
}
glColor3f(0.9, 0.9, 0.9);
foreach my $polygon (@$slice) {
foreach my $line (@{$polygon->lines}) {
glBegin(GL_LINES);
glVertex2f(@{$line->a});
glVertex2f(@{$line->b});
glEnd();
}
}
}
glPopMatrix();
}
}
my $skirt_drawn = 0;
my $brim_drawn = 0;
@ -194,32 +362,41 @@ sub Render {
my $print_z = $layer->print_z;
# draw brim
if ($layer->id == 0 && !$brim_drawn) {
if ($self->print->step_done(STEP_BRIM) && $layer->id == 0 && !$brim_drawn) {
$self->color([0, 0, 0]);
$self->_draw(undef, $print_z, $_) for @{$self->print->brim};
$brim_drawn = 1;
}
if (($self->print->config->skirt_height == -1 || $self->print->config->skirt_height >= $layer->id) && !$skirt_drawn) {
if ($self->print->step_done(STEP_SKIRT)
&& ($self->print->has_infinite_skirt() || $self->print->config->skirt_height > $layer->id)
&& !$skirt_drawn) {
$self->color([0, 0, 0]);
$self->_draw(undef, $print_z, $_) for @{$self->print->skirt};
$skirt_drawn = 1;
}
foreach my $layerm (@{$layer->regions}) {
$self->color([0.7, 0, 0]);
$self->_draw($object, $print_z, $_) for @{$layerm->perimeters};
if ($object->step_done(STEP_PERIMETERS)) {
$self->color([0.7, 0, 0]);
$self->_draw($object, $print_z, $_) for map @$_, @{$layerm->perimeters};
}
$self->color([0, 0, 0.7]);
$self->_draw($object, $print_z, $_) for map @$_, @{$layerm->fills};
if ($object->step_done(STEP_INFILL)) {
$self->color([0, 0, 0.7]);
$self->_draw($object, $print_z, $_) for map @$_, @{$layerm->fills};
}
}
if ($layer->isa('Slic3r::Layer::Support')) {
$self->color([0, 0, 0]);
$self->_draw($object, $print_z, $_) for @{$layer->support_fills};
$self->_draw($object, $print_z, $_) for @{$layer->support_interface_fills};
if ($object->step_done(STEP_SUPPORTMATERIAL)) {
if ($layer->isa('Slic3r::Layer::Support')) {
$self->color([0, 0, 0]);
$self->_draw($object, $print_z, $_) for @{$layer->support_fills};
$self->_draw($object, $print_z, $_) for @{$layer->support_interface_fills};
}
}
}
gluDeleteTess($tess) if $tess;
glFlush();
$self->SwapBuffers;
}
@ -249,13 +426,15 @@ sub _draw_path {
if (defined $object) {
foreach my $copy (@{ $object->_shifted_copies }) {
glPushMatrix();
glTranslatef(@$copy, 0);
foreach my $line (@{$path->polyline->lines}) {
$line->translate(@$copy);
glBegin(GL_LINES);
glVertex2f(@{$line->a});
glVertex2f(@{$line->b});
glEnd();
}
glPopMatrix();
}
} else {
foreach my $line (@{$path->polyline->lines}) {
@ -296,13 +475,71 @@ sub SetCurrent {
}
sub Resize {
my ($self, $x, $y) = @_;
my ($self) = @_;
return unless $self->GetContext;
$self->dirty(0);
return unless $self->bb;
$self->_dirty(0);
$self->SetCurrent($self->GetContext);
my ($x, $y) = $self->GetSizeWH;
glViewport(0, 0, $x, $y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
my $bb = $self->bb->clone;
# center bounding box around origin before scaling it
my $bb_center = $bb->center;
$bb->translate(@{$bb_center->negative});
# scale bounding box according to zoom factor
$bb->scale($self->_zoom);
# reposition bounding box around original center
$bb->translate(@{$bb_center});
# translate camera
$bb->translate(@{$self->_camera_target});
# keep camera_bb within total bb
# (i.e. prevent user from panning outside the bounding box)
{
my @translate = (0,0);
if ($bb->x_min < $self->bb->x_min) {
$translate[X] += $self->bb->x_min - $bb->x_min;
}
if ($bb->y_min < $self->bb->y_min) {
$translate[Y] += $self->bb->y_min - $bb->y_min;
}
if ($bb->x_max > $self->bb->x_max) {
$translate[X] -= $bb->x_max - $self->bb->x_max;
}
if ($bb->y_max > $self->bb->y_max) {
$translate[Y] -= $bb->y_max - $self->bb->y_max;
}
$self->_camera_target->translate(@translate);
$bb->translate(@translate);
}
# save camera
$self->_camera_bb($bb);
my ($x1, $y1, $x2, $y2) = ($bb->x_min, $bb->y_min, $bb->x_max, $bb->y_max);
if (($x2 - $x1)/($y2 - $y1) > $x/$y) {
# adjust Y
my $new_y = $y * ($x2 - $x1) / $x;
$y1 = ($y2 + $y1)/2 - $new_y/2;
$y2 = $y1 + $new_y;
} else {
my $new_x = $x * ($y2 - $y1) / $y;
$x1 = ($x2 + $x1)/2 - $new_x/2;
$x2 = $x1 + $new_x;
}
glOrtho($x1, $x2, $y1, $y2, 0, 1);
glMatrixMode(GL_MODELVIEW);
}
sub line {

View File

@ -8,39 +8,98 @@ use Slic3r::Geometry qw();
use Slic3r::Geometry::Clipper qw();
use Wx qw(:misc :pen :brush :sizer :font :cursor wxTAB_TRAVERSAL);
use Wx::Event qw();
use base 'Slic3r::GUI::PreviewCanvas';
use base qw(Slic3r::GUI::3DScene Class::Accessor);
sub new {
my $class = shift;
my ($parent, $objects, $model, $config) = @_;
my $self = $class->SUPER::new($parent);
$self->enable_picking(1);
$self->enable_moving(1);
$self->select_by('object');
$self->drag_by('instance');
$self->{objects} = $objects;
$self->{model} = $model;
$self->{config} = $config;
$self->{on_select_object} = sub {};
$self->{on_double_click} = sub {};
$self->{on_right_click} = sub {};
$self->{on_instance_moved} = sub {};
$self->{on_instances_moved} = sub {};
$self->on_select(sub {
my ($volume_idx) = @_;
my $obj_idx = undef;
if ($volume_idx != -1) {
$obj_idx = $self->object_idx($volume_idx);
}
$self->{on_select_object}->($obj_idx)
if $self->{on_select_object};
});
$self->on_move(sub {
my @volume_idxs = @_;
my %done = (); # prevent moving instances twice
foreach my $volume_idx (@volume_idxs) {
my $volume = $self->volumes->[$volume_idx];
my $obj_idx = $self->object_idx($volume_idx);
my $instance_idx = $self->instance_idx($volume_idx);
next if $done{"${obj_idx}_${instance_idx}"};
$done{"${obj_idx}_${instance_idx}"} = 1;
my $model_object = $self->{model}->get_object($obj_idx);
$model_object
->instances->[$instance_idx]
->offset
->translate($volume->origin->x, $volume->origin->y); #))
$model_object->invalidate_bounding_box;
}
$self->{on_instances_moved}->()
if $self->{on_instances_moved};
});
return $self;
}
sub set_on_select_object {
my ($self, $cb) = @_;
$self->{on_select_object} = $cb;
}
sub set_on_double_click {
my ($self, $cb) = @_;
$self->on_double_click($cb);
}
sub set_on_right_click {
my ($self, $cb) = @_;
$self->on_right_click($cb);
}
sub set_on_instances_moved {
my ($self, $cb) = @_;
$self->{on_instances_moved} = $cb;
}
sub update {
my ($self) = @_;
$self->reset_objects;
return if $self->{model}->objects_count == 0;
$self->update_bed_size;
$self->set_bounding_box($self->{model}->bounding_box);
$self->set_bed_shape($self->{config}->bed_shape);
foreach my $model_object (@{$self->{model}->objects}) {
$self->load_object($model_object, 1);
foreach my $obj_idx (0..$#{$self->{model}->objects}) {
my @volume_idxs = $self->load_object($self->{model}, $obj_idx);
if ($self->{objects}[$obj_idx]->selected) {
$self->select_volume($_) for @volume_idxs;
}
}
}
1;
sub update_bed_size {
my ($self) = @_;
$self->set_bed_shape($self->{config}->bed_shape);
}
1;

View File

@ -0,0 +1,150 @@
package Slic3r::GUI::Plater::3DPreview;
use strict;
use warnings;
use utf8;
use Slic3r::Print::State ':steps';
use Wx qw(:misc :sizer :slider :statictext wxWHITE);
use Wx::Event qw(EVT_SLIDER EVT_KEY_DOWN);
use base qw(Wx::Panel Class::Accessor);
__PACKAGE__->mk_accessors(qw(print enabled _loaded canvas slider));
sub new {
my $class = shift;
my ($parent, $print) = @_;
my $self = $class->SUPER::new($parent, -1, wxDefaultPosition);
# init GUI elements
my $canvas = Slic3r::GUI::3DScene->new($self);
$self->canvas($canvas);
my $slider = Wx::Slider->new(
$self, -1,
0, # default
0, # min
# we set max to a bogus non-zero value because the MSW implementation of wxSlider
# will skip drawing the slider if max <= min:
1, # max
wxDefaultPosition,
wxDefaultSize,
wxVERTICAL | wxSL_INVERSE,
);
$self->slider($slider);
my $z_label = $self->{z_label} = Wx::StaticText->new($self, -1, "", wxDefaultPosition,
[40,-1], wxALIGN_CENTRE_HORIZONTAL);
$z_label->SetFont($Slic3r::GUI::small_font);
my $vsizer = Wx::BoxSizer->new(wxVERTICAL);
$vsizer->Add($slider, 1, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
$vsizer->Add($z_label, 0, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer->Add($canvas, 1, wxALL | wxEXPAND, 0);
$sizer->Add($vsizer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
EVT_SLIDER($self, $slider, sub {
$self->set_z($self->{layers_z}[$slider->GetValue])
if $self->enabled;
});
EVT_KEY_DOWN($canvas, sub {
my ($s, $event) = @_;
my $key = $event->GetKeyCode;
if ($key == 85 || $key == 315) {
$slider->SetValue($slider->GetValue + 1);
$self->set_z($self->{layers_z}[$slider->GetValue]);
} elsif ($key == 68 || $key == 317) {
$slider->SetValue($slider->GetValue - 1);
$self->set_z($self->{layers_z}[$slider->GetValue]);
}
});
$self->SetSizer($sizer);
$self->SetMinSize($self->GetSize);
$sizer->SetSizeHints($self);
# init canvas
$self->print($print);
$self->reload_print;
return $self;
}
sub reload_print {
my ($self) = @_;
$self->canvas->reset_objects;
$self->_loaded(0);
$self->load_print;
}
sub load_print {
my ($self) = @_;
return if $self->_loaded;
# we require that there's at least one object and the posSlice step
# is performed on all of them (this ensures that _shifted_copies was
# populated and we know the number of layers)
if (!$self->print->object_step_done(STEP_SLICE)) {
$self->enabled(0);
$self->slider->Hide;
$self->canvas->Refresh; # clears canvas
return;
}
my $z_idx;
{
my %z = (); # z => 1
foreach my $object (@{$self->{print}->objects}) {
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
$z{$layer->print_z} = 1;
}
}
$self->enabled(1);
$self->{layers_z} = [ sort { $a <=> $b } keys %z ];
$self->slider->SetRange(0, scalar(@{$self->{layers_z}})-1);
if (($z_idx = $self->slider->GetValue) <= $#{$self->{layers_z}} && $self->slider->GetValue != 0) {
# use $z_idx
} else {
$self->slider->SetValue(scalar(@{$self->{layers_z}})-1);
$z_idx = @{$self->{layers_z}} ? -1 : undef;
}
$self->slider->Show;
$self->Layout;
}
if ($self->IsShown) {
# load skirt and brim
$self->canvas->load_print_toolpaths($self->print);
foreach my $object (@{$self->print->objects}) {
$self->canvas->load_print_object_toolpaths($object);
#my @volume_ids = $self->canvas->load_object($object->model_object);
#$self->canvas->volumes->[$_]->color->[3] = 0.2 for @volume_ids;
}
$self->canvas->zoom_to_volumes;
$self->_loaded(1);
}
$self->set_z($self->{layers_z}[$z_idx]);
}
sub set_z {
my ($self, $z) = @_;
return if !$self->enabled;
$self->{z_label}->SetLabel(sprintf '%.2f', $z);
$self->canvas->set_toolpaths_range(0, $z);
$self->canvas->Refresh if $self->IsShown;
}
sub set_bed_shape {
my ($self, $bed_shape) = @_;
$self->canvas->set_bed_shape($bed_shape);
}
1;

View File

@ -4,7 +4,7 @@ use warnings;
use utf8;
use Slic3r::Geometry qw(PI X);
use Wx qw(:dialog :id :misc :sizer wxTAB_TRAVERSAL);
use Wx qw(wxTheApp :dialog :id :misc :sizer wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
use base 'Wx::Dialog';
@ -22,6 +22,7 @@ sub new {
keep_upper => 1,
keep_lower => 1,
rotate_lower => 1,
preview => 1,
};
my $optgroup;
@ -32,7 +33,9 @@ sub new {
my ($opt_id) = @_;
$self->{cut_options}{$opt_id} = $optgroup->get_value($opt_id);
$self->_update;
wxTheApp->CallAfter(sub {
$self->_update;
});
},
label_width => 120,
);
@ -70,6 +73,13 @@ sub new {
tooltip => 'If enabled, the lower part will be rotated by 180° so that the flat cut surface lies on the print bed.',
default => $self->{cut_options}{rotate_lower},
));
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'preview',
label => 'Show preview',
type => 'bool',
tooltip => 'If enabled, object will be cut in real time.',
default => $self->{cut_options}{preview},
));
{
my $cut_button_sizer = Wx::BoxSizer->new(wxVERTICAL);
$self->{btn_cut} = Wx::Button->new($self, -1, "Perform cut", wxDefaultPosition, wxDefaultSize);
@ -86,12 +96,12 @@ sub new {
# right pane with preview canvas
my $canvas;
if ($Slic3r::GUI::have_OpenGL) {
$canvas = $self->{canvas} = Slic3r::GUI::PreviewCanvas->new($self);
$canvas->load_object($self->{model_object});
$canvas->set_bounding_box($self->{model_object}->bounding_box);
$canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
$canvas->load_object($self->{model_object}, undef, [0]);
$canvas->set_auto_bed_shape;
$canvas->SetSize([500,500]);
$canvas->SetMinSize($canvas->GetSize);
$canvas->zoom_to_volumes;
}
$self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
@ -102,14 +112,21 @@ sub new {
$self->SetMinSize($self->GetSize);
$self->{sizer}->SetSizeHints($self);
# needed to actually free memory
EVT_CLOSE($self, sub {
EVT_BUTTON($self, $self->{btn_cut}, sub {
if ($self->{new_model_objects}{lower}) {
if ($self->{cut_options}{rotate_lower}) {
$self->{new_model_objects}{lower}->rotate(PI, X);
$self->{new_model_objects}{lower}->center_around_origin; # align to Z = 0
}
}
if ($self->{new_model_objects}{upper}) {
$self->{new_model_objects}{upper}->center_around_origin; # align to Z = 0
}
$self->EndModal(wxID_OK);
$self->Destroy;
$self->Close;
});
EVT_BUTTON($self, $self->{btn_cut}, sub { $self->perform_cut });
$self->_update;
return $self;
@ -118,55 +135,80 @@ sub new {
sub _update {
my ($self) = @_;
my $optgroup = $self->{optgroup};
# update canvas
if ($self->{canvas}) {
$self->{canvas}->SetCuttingPlane($self->{cut_options}{z});
$self->{canvas}->Render;
}
# update controls
my $z = $self->{cut_options}{z};
$optgroup->get_field('keep_upper')->toggle(my $have_upper = abs($z - $optgroup->get_option('z')->max) > 0.1);
$optgroup->get_field('keep_lower')->toggle(my $have_lower = $z > 0.1);
$optgroup->get_field('rotate_lower')->toggle($z > 0 && $self->{cut_options}{keep_lower});
# update cut button
if (($self->{cut_options}{keep_upper} && $have_upper)
|| ($self->{cut_options}{keep_lower} && $have_lower)) {
$self->{btn_cut}->Enable;
} else {
$self->{btn_cut}->Disable;
}
}
sub perform_cut {
my ($self) = @_;
# scale Z down to original size since we're using the transformed mesh for 3D preview
# and cut dialog but ModelObject::cut() needs Z without any instance transformation
my $z = $self->{cut_options}{z} / $self->{model_object}->instances->[0]->scaling_factor;
my ($new_model, $upper_object, $lower_object) = $self->{model_object}->cut($z);
$self->{new_model} = $new_model;
$self->{new_model_objects} = [];
if ($self->{cut_options}{keep_upper} && defined $upper_object) {
push @{$self->{new_model_objects}}, $upper_object;
}
if ($self->{cut_options}{keep_lower} && defined $lower_object) {
push @{$self->{new_model_objects}}, $lower_object;
if ($self->{cut_options}{rotate_lower}) {
$lower_object->rotate(PI, X);
{
# scale Z down to original size since we're using the transformed mesh for 3D preview
# and cut dialog but ModelObject::cut() needs Z without any instance transformation
my $z = $self->{cut_options}{z} / $self->{model_object}->instances->[0]->scaling_factor;
{
my ($new_model) = $self->{model_object}->cut($z);
my ($upper_object, $lower_object) = @{$new_model->objects};
$self->{new_model} = $new_model;
$self->{new_model_objects} = {};
if ($self->{cut_options}{keep_upper} && $upper_object->volumes_count > 0) {
$self->{new_model_objects}{upper} = $upper_object;
}
if ($self->{cut_options}{keep_lower} && $lower_object->volumes_count > 0) {
$self->{new_model_objects}{lower} = $lower_object;
}
}
# update canvas
if ($self->{canvas}) {
# get volumes to render
my @objects = ();
if ($self->{cut_options}{preview}) {
push @objects, values %{$self->{new_model_objects}};
} else {
push @objects, $self->{model_object};
}
# get section contour
my @expolygons = ();
foreach my $volume (@{$self->{model_object}->volumes}) {
next if !$volume->mesh;
next if $volume->modifier;
my $expp = $volume->mesh->slice([ $z + $volume->mesh->bounding_box->z_min ])->[0];
push @expolygons, @$expp;
}
foreach my $expolygon (@expolygons) {
$self->{model_object}->instances->[0]->transform_polygon($_)
for @$expolygon;
$expolygon->translate(map Slic3r::Geometry::scale($_), @{ $self->{model_object}->instances->[0]->offset });
}
$self->{canvas}->reset_objects;
$self->{canvas}->load_object($_, undef, [0]) for @objects;
$self->{canvas}->SetCuttingPlane(
$self->{cut_options}{z},
[@expolygons],
);
$self->{canvas}->Render;
}
}
$self->Close;
# update controls
{
my $z = $self->{cut_options}{z};
my $optgroup = $self->{optgroup};
$optgroup->get_field('keep_upper')->toggle(my $have_upper = abs($z - $optgroup->get_option('z')->max) > 0.1);
$optgroup->get_field('keep_lower')->toggle(my $have_lower = $z > 0.1);
$optgroup->get_field('rotate_lower')->toggle($z > 0 && $self->{cut_options}{keep_lower});
$optgroup->get_field('preview')->toggle($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
# update cut button
if (($self->{cut_options}{keep_upper} && $have_upper)
|| ($self->{cut_options}{keep_lower} && $have_lower)) {
$self->{btn_cut}->Enable;
} else {
$self->{btn_cut}->Disable;
}
}
}
sub NewModelObjects {
my ($self) = @_;
return @{ $self->{new_model_objects} };
return values %{ $self->{new_model_objects} };
}
1;

View File

@ -27,9 +27,9 @@ sub new {
{
$self->{tree_icons} = Wx::ImageList->new(16, 16, 1);
$tree->AssignImageList($self->{tree_icons});
$self->{tree_icons}->Add(Wx::Bitmap->new("$Slic3r::var/brick.png", wxBITMAP_TYPE_PNG)); # ICON_OBJECT
$self->{tree_icons}->Add(Wx::Bitmap->new("$Slic3r::var/package.png", wxBITMAP_TYPE_PNG)); # ICON_SOLIDMESH
$self->{tree_icons}->Add(Wx::Bitmap->new("$Slic3r::var/plugin.png", wxBITMAP_TYPE_PNG)); # ICON_MODIFIERMESH
$self->{tree_icons}->Add(Wx::Bitmap->new($Slic3r::var->("brick.png"), wxBITMAP_TYPE_PNG)); # ICON_OBJECT
$self->{tree_icons}->Add(Wx::Bitmap->new($Slic3r::var->("package.png"), wxBITMAP_TYPE_PNG)); # ICON_SOLIDMESH
$self->{tree_icons}->Add(Wx::Bitmap->new($Slic3r::var->("plugin.png"), wxBITMAP_TYPE_PNG)); # ICON_MODIFIERMESH
my $rootId = $tree->AddRoot("Object", ICON_OBJECT);
$tree->SetPlData($rootId, { type => 'object' });
@ -40,9 +40,9 @@ sub new {
$self->{btn_load_modifier} = Wx::Button->new($self, -1, "Load modifier…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
$self->{btn_delete} = Wx::Button->new($self, -1, "Delete part", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
if ($Slic3r::GUI::have_button_icons) {
$self->{btn_load_part}->SetBitmap(Wx::Bitmap->new("$Slic3r::var/brick_add.png", wxBITMAP_TYPE_PNG));
$self->{btn_load_modifier}->SetBitmap(Wx::Bitmap->new("$Slic3r::var/brick_add.png", wxBITMAP_TYPE_PNG));
$self->{btn_delete}->SetBitmap(Wx::Bitmap->new("$Slic3r::var/brick_delete.png", wxBITMAP_TYPE_PNG));
$self->{btn_load_part}->SetBitmap(Wx::Bitmap->new($Slic3r::var->("brick_add.png"), wxBITMAP_TYPE_PNG));
$self->{btn_load_modifier}->SetBitmap(Wx::Bitmap->new($Slic3r::var->("brick_add.png"), wxBITMAP_TYPE_PNG));
$self->{btn_delete}->SetBitmap(Wx::Bitmap->new($Slic3r::var->("brick_delete.png"), wxBITMAP_TYPE_PNG));
}
# buttons sizer
@ -68,11 +68,21 @@ sub new {
# right pane with preview canvas
my $canvas;
if ($Slic3r::GUI::have_OpenGL) {
$canvas = $self->{canvas} = Slic3r::GUI::PreviewCanvas->new($self);
$canvas->load_object($self->{model_object});
$canvas->set_bounding_box($self->{model_object}->bounding_box);
$canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
$canvas->enable_picking(1);
$canvas->select_by('volume');
$canvas->on_select(sub {
my ($volume_idx) = @_;
# convert scene volume to model object volume
$self->reload_tree($canvas->volume_idx($volume_idx));
});
$canvas->load_object($self->{model_object}, undef, [0]);
$canvas->set_auto_bed_shape;
$canvas->SetSize([500,500]);
$canvas->zoom_to_volumes;
}
$self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
@ -89,6 +99,7 @@ sub new {
});
EVT_TREE_SEL_CHANGED($self, $tree, sub {
my ($self, $event) = @_;
return if $self->{disable_tree_sel_changed_event};
$self->selection_changed;
});
EVT_BUTTON($self, $self->{btn_load_part}, sub { $self->on_btn_load(0) });
@ -101,20 +112,31 @@ sub new {
}
sub reload_tree {
my ($self) = @_;
my ($self, $selected_volume_idx) = @_;
$selected_volume_idx //= -1;
my $object = $self->{model_object};
my $tree = $self->{tree};
my $rootId = $tree->GetRootItem;
# despite wxWidgets states that DeleteChildren "will not generate any events unlike Delete() method",
# the MSW implementation of DeleteChildren actually calls Delete() for each item, so
# EVT_TREE_SEL_CHANGED is being called, with bad effects (the event handler is called; this
# subroutine is never continued; an invisible EndModal is called on the dialog causing Plater
# to continue its logic and rescheduling the background process etc. GH #2774)
$self->{disable_tree_sel_changed_event} = 1;
$tree->DeleteChildren($rootId);
$self->{disable_tree_sel_changed_event} = 0;
my $itemId;
my $selectedId = $rootId;
foreach my $volume_id (0..$#{$object->volumes}) {
my $volume = $object->volumes->[$volume_id];
my $icon = $volume->modifier ? ICON_MODIFIERMESH : ICON_SOLIDMESH;
$itemId = $tree->AppendItem($rootId, $volume->name || $volume_id, $icon);
my $itemId = $tree->AppendItem($rootId, $volume->name || $volume_id, $icon);
if ($volume_id == $selected_volume_idx) {
$selectedId = $itemId;
}
$tree->SetPlData($itemId, {
type => 'volume',
volume_id => $volume_id,
@ -122,10 +144,13 @@ sub reload_tree {
}
$tree->ExpandAll;
# select last appended part
# This will trigger the selection_changed() event
Slic3r::GUI->CallAfter(sub {
$self->{tree}->SelectItem($itemId);
$self->{tree}->SelectItem($selectedId);
# SelectItem() should trigger EVT_TREE_SEL_CHANGED as per wxWidgets docs,
# but in fact it doesn't if the given item is already selected (this happens
# on first load)
$self->selection_changed;
});
}
@ -144,7 +169,7 @@ sub selection_changed {
# deselect all meshes
if ($self->{canvas}) {
$_->{selected} = 0 for @{$self->{canvas}->volumes};
$_->selected(0) for @{$self->{canvas}->volumes};
}
# disable things as if nothing is selected
@ -169,10 +194,7 @@ sub selection_changed {
# get default values
@opt_keys = @{Slic3r::Config::PrintRegion->new->get_keys};
} elsif ($itemData->{type} eq 'object') {
# select all object volumes in 3D preview
if ($self->{canvas}) {
$_->{selected} = 1 for @{$self->{canvas}->volumes};
}
# select nothing in 3D preview
# attach object config to settings panel
$self->{staticbox}->SetLabel('Object Settings');
@ -214,7 +236,7 @@ sub on_btn_load {
$new_volume->set_name(basename($input_file));
# apply the same translation we applied to the object
$new_volume->mesh->translate(@{$self->{model_object}->origin_translation}, 0);
$new_volume->mesh->translate(@{$self->{model_object}->origin_translation});
# set a default extruder value, since user can't add it manually
$new_volume->config->set_ifndef('extruder', 0);
@ -224,11 +246,7 @@ sub on_btn_load {
}
}
$self->reload_tree;
if ($self->{canvas}) {
$self->{canvas}->load_object($self->{model_object});
$self->{canvas}->Render;
}
$self->_parts_changed;
}
sub on_btn_delete {
@ -248,9 +266,17 @@ sub on_btn_delete {
$self->{parts_changed} = 1;
}
$self->_parts_changed;
}
sub _parts_changed {
my ($self) = @_;
$self->reload_tree;
if ($self->{canvas}) {
$self->{canvas}->reset_objects;
$self->{canvas}->load_object($self->{model_object});
$self->{canvas}->zoom_to_volumes;
$self->{canvas}->Render;
}
}

View File

@ -1,35 +0,0 @@
package Slic3r::GUI::Plater::ObjectPreviewDialog;
use strict;
use warnings;
use utf8;
use Wx qw(:dialog :id :misc :sizer :systemsettings :notebook wxTAB_TRAVERSAL);
use Wx::Event qw(EVT_CLOSE);
use base 'Wx::Dialog';
sub new {
my $class = shift;
my ($parent, %params) = @_;
my $self = $class->SUPER::new($parent, -1, $params{object}->name, wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
$self->{model_object} = $params{model_object};
my $canvas = Slic3r::GUI::PreviewCanvas->new($self);
$canvas->load_object($self->{model_object});
$canvas->set_bounding_box($self->{model_object}->bounding_box);
$canvas->set_auto_bed_shape;
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$sizer->Add($canvas, 1, wxEXPAND, 0);
$self->SetSizer($sizer);
$self->SetMinSize($self->GetSize);
# needed to actually free memory
EVT_CLOSE($self, sub {
$self->EndModal(wxID_OK);
$self->Destroy;
});
return $self;
}
1;

View File

@ -107,12 +107,17 @@ sub new {
$grid->SetCellValue($event->GetRow, $event->GetCol, $value);
# if there's no empty row, let's append one
for my $i (0 .. $grid->GetNumberRows-1) {
for my $i (0 .. $grid->GetNumberRows) {
if ($i == $grid->GetNumberRows) {
# if we're here then we found no empty row
$grid->AppendRows(1);
last;
}
if (!grep $grid->GetCellValue($i, $_), 0..2) {
return;
# exit loop if this row is empty
last;
}
}
$grid->AppendRows(1);
$self->{layers_changed} = 1;
});

View File

@ -30,7 +30,7 @@ sub new {
# option selector
{
# create the button
my $btn = $self->{btn_add} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new("$Slic3r::var/add.png", wxBITMAP_TYPE_PNG),
my $btn = $self->{btn_add} = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("add.png"), wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize, Wx::wxBORDER_NONE);
EVT_LEFT_DOWN($btn, sub {
my $menu = Wx::Menu->new;
@ -118,7 +118,7 @@ sub update_optgroup {
# disallow deleting fixed options
return undef if $self->{fixed_options}{$opt_key};
my $btn = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new("$Slic3r::var/delete.png", wxBITMAP_TYPE_PNG),
my $btn = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("delete.png"), wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize, Wx::wxBORDER_NONE);
EVT_BUTTON($self, $btn, sub {
$self->{config}->erase($opt_key);
@ -133,7 +133,7 @@ sub update_optgroup {
}
$self->{options_sizer}->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM, 0);
}
$self->Layout;
$self->GetParent->Layout; # we need this for showing scrollbars
}
# work around a wxMAC bug causing controls not being disabled when calling Disable() on a Window

View File

@ -5,7 +5,7 @@ use base 'Wx::Dialog';
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new($parent, -1, "Preferences", wxDefaultPosition, [500,200]);
my $self = $class->SUPER::new($parent, -1, "Preferences", wxDefaultPosition, wxDefaultSize);
$self->{values} = {};
my $optgroup;
@ -16,7 +16,7 @@ sub new {
my ($opt_id) = @_;
$self->{values}{$opt_id} = $optgroup->get_value($opt_id);
},
label_width => 100,
label_width => 200,
);
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'mode',
@ -26,6 +26,7 @@ sub new {
labels => ['Simple','Expert'],
values => ['simple','expert'],
default => $Slic3r::GUI::Settings->{_}{mode},
width => 100,
));
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'version_check',
@ -57,6 +58,13 @@ sub new {
default => $Slic3r::GUI::Settings->{_}{background_processing},
readonly => !$Slic3r::have_threads,
));
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'no_controller',
type => 'bool',
label => 'Disable USB/serial connection',
tooltip => 'Disable communication with the printer over a serial / USB cable. This simplifies the user interface in case the printer is never attached to the computer.',
default => $Slic3r::GUI::Settings->{_}{no_controller},
));
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
@ -74,7 +82,7 @@ sub new {
sub _accept {
my $self = shift;
if ($self->{values}{mode}) {
if ($self->{values}{mode} || defined($self->{values}{no_controller})) {
Slic3r::GUI::warning_catcher($self)->("You need to restart Slic3r to make the changes effective.");
}

View File

@ -1,642 +0,0 @@
package Slic3r::GUI::PreviewCanvas;
use strict;
use warnings;
use Wx::Event qw(EVT_PAINT EVT_SIZE EVT_ERASE_BACKGROUND EVT_IDLE EVT_MOUSEWHEEL EVT_MOUSE_EVENTS);
# must load OpenGL *before* Wx::GLCanvas
use OpenGL qw(:glconstants :glfunctions :glufunctions);
use base qw(Wx::GLCanvas Class::Accessor);
use Math::Trig qw(asin);
use List::Util qw(reduce min max first);
use Slic3r::Geometry qw(X Y Z MIN MAX triangle_normal normalize deg2rad tan scale unscale);
use Slic3r::Geometry::Clipper qw(offset_ex intersection_pl);
use Wx::GLCanvas qw(:all);
__PACKAGE__->mk_accessors( qw(quat dirty init mview_init
object_bounding_box
volumes initpos
sphi stheta
cutting_plane_z
cut_lines_vertices
bed_triangles
bed_grid_lines
origin
) );
use constant TRACKBALLSIZE => 0.8;
use constant TURNTABLE_MODE => 1;
use constant GROUND_Z => 0.02;
use constant SELECTED_COLOR => [0,1,0,1];
use constant COLORS => [ [1,1,0], [1,0.5,0.5], [0.5,1,0.5], [0.5,0.5,1] ];
# make OpenGL::Array thread-safe
{
no warnings 'redefine';
*OpenGL::Array::CLONE_SKIP = sub { 1 };
}
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new($parent);
$self->quat((0, 0, 0, 1));
$self->sphi(45);
$self->stheta(-45);
$self->reset_objects;
EVT_PAINT($self, sub {
my $dc = Wx::PaintDC->new($self);
return if !$self->object_bounding_box;
$self->Render($dc);
});
EVT_SIZE($self, sub { $self->dirty(1) });
EVT_IDLE($self, sub {
return unless $self->dirty;
return if !$self->IsShownOnScreen;
return if !$self->object_bounding_box;
$self->Resize( $self->GetSizeWH );
$self->Refresh;
});
EVT_MOUSEWHEEL($self, sub {
my ($self, $e) = @_;
my $zoom = ($e->GetWheelRotation() / $e->GetWheelDelta() / 10);
$zoom = $zoom > 0 ? (1.0 + $zoom) : 1 / (1.0 - $zoom);
my @pos3d = $self->mouse_to_3d($e->GetX(), $e->GetY());
$self->ZoomTo($zoom, $pos3d[0], $pos3d[1]);
$self->Refresh;
});
EVT_MOUSE_EVENTS($self, sub {
my ($self, $e) = @_;
if ($e->Dragging() && $e->LeftIsDown()) {
$self->handle_rotation($e);
} elsif ($e->Dragging() && $e->RightIsDown()) {
$self->handle_translation($e);
} elsif ($e->LeftUp() || $e->RightUp()) {
$self->initpos(undef);
} else {
$e->Skip();
}
});
return $self;
}
sub reset_objects {
my ($self) = @_;
$self->volumes([]);
$self->dirty(1);
}
# this method accepts a Slic3r::BoudingBox3f object
sub set_bounding_box {
my ($self, $bb) = @_;
$self->object_bounding_box($bb);
$self->dirty(1);
}
sub set_auto_bed_shape {
my ($self, $bed_shape) = @_;
# draw a default square bed around object center
my $max_size = max(@{ $self->object_bounding_box->size });
my $center = $self->object_bounding_box->center;
$self->set_bed_shape([
[ $center->x - $max_size, $center->y - $max_size ], #--
[ $center->x + $max_size, $center->y - $max_size ], #--
[ $center->x + $max_size, $center->y + $max_size ], #++
[ $center->x - $max_size, $center->y + $max_size ], #++
]);
$self->origin(Slic3r::Pointf->new(@$center[X,Y]));
}
sub set_bed_shape {
my ($self, $bed_shape) = @_;
# triangulate bed
my $expolygon = Slic3r::ExPolygon->new([ map [map scale($_), @$_], @$bed_shape ]);
my $bed_bb = $expolygon->bounding_box;
{
my @points = ();
foreach my $triangle (@{ $expolygon->triangulate }) {
push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$triangle; #))
}
$self->bed_triangles(OpenGL::Array->new_list(GL_FLOAT, @points));
}
{
my @lines = ();
for (my $x = $bed_bb->x_min; $x <= $bed_bb->x_max; $x += scale 10) {
push @lines, Slic3r::Polyline->new([$x,$bed_bb->y_min], [$x,$bed_bb->y_max]);
}
for (my $y = $bed_bb->y_min; $y <= $bed_bb->y_max; $y += scale 10) {
push @lines, Slic3r::Polyline->new([$bed_bb->x_min,$y], [$bed_bb->x_max,$y]);
}
@lines = @{intersection_pl(\@lines, [ @$expolygon ])};
my @points = ();
foreach my $polyline (@lines) {
push @points, map {+ unscale($_->x), unscale($_->y), GROUND_Z } @$polyline; #))
}
$self->bed_grid_lines(OpenGL::Array->new_list(GL_FLOAT, @points));
}
$self->origin(Slic3r::Pointf->new(0,0));
}
sub load_object {
my ($self, $object, $all_instances) = @_;
my $z_min = $object->raw_bounding_box->z_min;
# color mesh(es) by material
my @materials = ();
# sort volumes: non-modifiers first
my @volumes = sort { ($a->modifier // 0) <=> ($b->modifier // 0) } @{$object->volumes};
foreach my $volume (@volumes) {
my @instances = $all_instances ? @{$object->instances} : $object->instances->[0];
foreach my $instance (@instances) {
my $mesh = $volume->mesh->clone;
$instance->transform_mesh($mesh);
my $material_id = $volume->material_id // '_';
my $color_idx = first { $materials[$_] eq $material_id } 0..$#materials;
if (!defined $color_idx) {
push @materials, $material_id;
$color_idx = $#materials;
}
my $color = [ @{COLORS->[ $color_idx % scalar(@{&COLORS}) ]} ];
push @$color, $volume->modifier ? 0.5 : 1;
push @{$self->volumes}, my $v = {
mesh => $mesh,
color => $color,
z_min => $z_min,
};
{
my $vertices = $mesh->vertices;
my @verts = map @{ $vertices->[$_] }, map @$_, @{$mesh->facets};
$v->{verts} = OpenGL::Array->new_list(GL_FLOAT, @verts);
}
{
my @norms = map { @$_, @$_, @$_ } @{$mesh->normals};
$v->{norms} = OpenGL::Array->new_list(GL_FLOAT, @norms);
}
}
}
}
sub SetCuttingPlane {
my ($self, $z) = @_;
$self->cutting_plane_z($z);
# perform cut and cache section lines
my @verts = ();
foreach my $volume (@{$self->volumes}) {
foreach my $volume (@{$self->volumes}) {
my $expolygons = $volume->{mesh}->slice([ $z + $volume->{z_min} ])->[0];
$expolygons = offset_ex([ map @$_, @$expolygons ], scale 0.1);
foreach my $line (map @{$_->lines}, map @$_, @$expolygons) {
push @verts, (
unscale($line->a->x), unscale($line->a->y), $z, #))
unscale($line->b->x), unscale($line->b->y), $z, #))
);
}
}
}
$self->cut_lines_vertices(OpenGL::Array->new_list(GL_FLOAT, @verts));
}
# Given an axis and angle, compute quaternion.
sub axis_to_quat {
my ($ax, $phi) = @_;
my $lena = sqrt(reduce { $a + $b } (map { $_ * $_ } @$ax));
my @q = map { $_ * (1 / $lena) } @$ax;
@q = map { $_ * sin($phi / 2.0) } @q;
$q[$#q + 1] = cos($phi / 2.0);
return @q;
}
# Project a point on the virtual trackball.
# If it is inside the sphere, map it to the sphere, if it outside map it
# to a hyperbola.
sub project_to_sphere {
my ($r, $x, $y) = @_;
my $d = sqrt($x * $x + $y * $y);
if ($d < $r * 0.70710678118654752440) { # Inside sphere
return sqrt($r * $r - $d * $d);
} else { # On hyperbola
my $t = $r / 1.41421356237309504880;
return $t * $t / $d;
}
}
sub cross {
my ($v1, $v2) = @_;
return (@$v1[1] * @$v2[2] - @$v1[2] * @$v2[1],
@$v1[2] * @$v2[0] - @$v1[0] * @$v2[2],
@$v1[0] * @$v2[1] - @$v1[1] * @$v2[0]);
}
# Simulate a track-ball. Project the points onto the virtual trackball,
# then figure out the axis of rotation, which is the cross product of
# P1 P2 and O P1 (O is the center of the ball, 0,0,0) Note: This is a
# deformed trackball-- is a trackball in the center, but is deformed
# into a hyperbolic sheet of rotation away from the center.
# It is assumed that the arguments to this routine are in the range
# (-1.0 ... 1.0).
sub trackball {
my ($p1x, $p1y, $p2x, $p2y) = @_;
if ($p1x == $p2x && $p1y == $p2y) {
# zero rotation
return (0.0, 0.0, 0.0, 1.0);
}
# First, figure out z-coordinates for projection of P1 and P2 to
# deformed sphere
my @p1 = ($p1x, $p1y, project_to_sphere(TRACKBALLSIZE, $p1x, $p1y));
my @p2 = ($p2x, $p2y, project_to_sphere(TRACKBALLSIZE, $p2x, $p2y));
# axis of rotation (cross product of P1 and P2)
my @a = cross(\@p2, \@p1);
# Figure out how much to rotate around that axis.
my @d = map { $_ * $_ } (map { $p1[$_] - $p2[$_] } 0 .. $#p1);
my $t = sqrt(reduce { $a + $b } @d) / (2.0 * TRACKBALLSIZE);
# Avoid problems with out-of-control values...
$t = 1.0 if ($t > 1.0);
$t = -1.0 if ($t < -1.0);
my $phi = 2.0 * asin($t);
return axis_to_quat(\@a, $phi);
}
# Build a rotation matrix, given a quaternion rotation.
sub quat_to_rotmatrix {
my ($q) = @_;
my @m = ();
$m[0] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[2] * @$q[2]);
$m[1] = 2.0 * (@$q[0] * @$q[1] - @$q[2] * @$q[3]);
$m[2] = 2.0 * (@$q[2] * @$q[0] + @$q[1] * @$q[3]);
$m[3] = 0.0;
$m[4] = 2.0 * (@$q[0] * @$q[1] + @$q[2] * @$q[3]);
$m[5] = 1.0 - 2.0 * (@$q[2] * @$q[2] + @$q[0] * @$q[0]);
$m[6] = 2.0 * (@$q[1] * @$q[2] - @$q[0] * @$q[3]);
$m[7] = 0.0;
$m[8] = 2.0 * (@$q[2] * @$q[0] - @$q[1] * @$q[3]);
$m[9] = 2.0 * (@$q[1] * @$q[2] + @$q[0] * @$q[3]);
$m[10] = 1.0 - 2.0 * (@$q[1] * @$q[1] + @$q[0] * @$q[0]);
$m[11] = 0.0;
$m[12] = 0.0;
$m[13] = 0.0;
$m[14] = 0.0;
$m[15] = 1.0;
return @m;
}
sub mulquats {
my ($q1, $rq) = @_;
return (@$q1[3] * @$rq[0] + @$q1[0] * @$rq[3] + @$q1[1] * @$rq[2] - @$q1[2] * @$rq[1],
@$q1[3] * @$rq[1] + @$q1[1] * @$rq[3] + @$q1[2] * @$rq[0] - @$q1[0] * @$rq[2],
@$q1[3] * @$rq[2] + @$q1[2] * @$rq[3] + @$q1[0] * @$rq[1] - @$q1[1] * @$rq[0],
@$q1[3] * @$rq[3] - @$q1[0] * @$rq[0] - @$q1[1] * @$rq[1] - @$q1[2] * @$rq[2])
}
sub handle_rotation {
my ($self, $e) = @_;
if (not defined $self->initpos) {
$self->initpos($e->GetPosition());
} else {
my $orig = $self->initpos;
my $new = $e->GetPosition();
my $size = $self->GetClientSize();
if (TURNTABLE_MODE) {
$self->sphi($self->sphi + ($new->x - $orig->x)*TRACKBALLSIZE);
$self->stheta($self->stheta + ($new->y - $orig->y)*TRACKBALLSIZE); #-
} else {
my @quat = trackball($orig->x / ($size->width / 2) - 1,
1 - $orig->y / ($size->height / 2), #/
$new->x / ($size->width / 2) - 1,
1 - $new->y / ($size->height / 2), #/
);
$self->quat(mulquats($self->quat, \@quat));
}
$self->initpos($new);
$self->Refresh;
}
}
sub handle_translation {
my ($self, $e) = @_;
if (not defined $self->initpos) {
$self->initpos($e->GetPosition());
} else {
my $new = $e->GetPosition();
my $orig = $self->initpos;
my @orig3d = $self->mouse_to_3d($orig->x, $orig->y); #)()
my @new3d = $self->mouse_to_3d($new->x, $new->y); #)()
glTranslatef($new3d[0] - $orig3d[0], $new3d[1] - $orig3d[1], 0);
$self->initpos($new);
$self->Refresh;
}
}
sub mouse_to_3d {
my ($self, $x, $y) = @_;
my @viewport = glGetIntegerv_p(GL_VIEWPORT); # 4 items
my @mview = glGetDoublev_p(GL_MODELVIEW_MATRIX); # 16 items
my @proj = glGetDoublev_p(GL_PROJECTION_MATRIX); # 16 items
my @projected = gluUnProject_p($x, $viewport[3] - $y, 1.0, @mview, @proj, @viewport);
return @projected;
}
sub ZoomTo {
my ($self, $factor, $tox, $toy) = @_;
return if !$self->init;
glTranslatef($tox, $toy, 0);
glMatrixMode(GL_MODELVIEW);
$self->Zoom($factor);
glTranslatef(-$tox, -$toy, 0);
}
sub Zoom {
my ($self, $factor) = @_;
glMatrixMode(GL_MODELVIEW);
glScalef($factor, $factor, 1);
}
sub GetContext {
my ($self) = @_;
if (Wx::wxVERSION >= 2.009) {
return $self->{context} ||= Wx::GLContext->new($self);
} else {
return $self->SUPER::GetContext;
}
}
sub SetCurrent {
my ($self, $context) = @_;
if (Wx::wxVERSION >= 2.009) {
return $self->SUPER::SetCurrent($context);
} else {
return $self->SUPER::SetCurrent;
}
}
sub ResetModelView {
my ($self, $factor) = @_;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
my $win_size = $self->GetClientSize();
my $ratio = $factor * min($win_size->width, $win_size->height) / (2 * max(@{ $self->object_bounding_box->size }));
glScalef($ratio, $ratio, 1);
}
sub Resize {
my ($self, $x, $y) = @_;
return unless $self->GetContext;
$self->dirty(0);
$self->SetCurrent($self->GetContext);
glViewport(0, 0, $x, $y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(
-$x/2, $x/2, -$y/2, $y/2,
-200, 10 * max(@{ $self->object_bounding_box->size }),
);
glMatrixMode(GL_MODELVIEW);
unless ($self->mview_init) {
$self->mview_init(1);
$self->ResetModelView(0.9);
}
}
sub InitGL {
my $self = shift;
return if $self->init;
return unless $self->GetContext;
$self->init(1);
glClearColor(0, 0, 0, 1);
glColor3f(1, 0, 0);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
# ambient lighting
glLightModelfv_p(GL_LIGHT_MODEL_AMBIENT, 0.1, 0.1, 0.1, 1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glLightfv_p(GL_LIGHT0, GL_POSITION, 0.5, 0.5, 1, 0);
glLightfv_p(GL_LIGHT0, GL_SPECULAR, 0.5, 0.5, 0.5, 1);
glLightfv_p(GL_LIGHT0, GL_DIFFUSE, 0.8, 0.8, 0.8, 1);
glLightfv_p(GL_LIGHT1, GL_POSITION, 1, 0, 0.5, 0);
glLightfv_p(GL_LIGHT1, GL_SPECULAR, 0.5, 0.5, 0.5, 1);
glLightfv_p(GL_LIGHT1, GL_DIFFUSE, 1, 1, 1, 1);
# Enables Smooth Color Shading; try GL_FLAT for (lack of) fun.
glShadeModel(GL_SMOOTH);
glMaterialfv_p(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 0.5, 0.3, 0.3, 1);
glMaterialfv_p(GL_FRONT_AND_BACK, GL_SPECULAR, 1, 1, 1, 1);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50);
glMaterialfv_p(GL_FRONT_AND_BACK, GL_EMISSION, 0.1, 0, 0, 0.9);
# A handy trick -- have surface material mirror the color.
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_MULTISAMPLE);
}
sub Render {
my ($self, $dc) = @_;
# prevent calling SetCurrent() when window is not shown yet
return unless $self->IsShownOnScreen;
return unless my $context = $self->GetContext;
$self->SetCurrent($context);
$self->InitGL;
glClearColor(1, 1, 1, 1);
glClearDepth(1);
glDepthFunc(GL_LESS);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
my $bb = $self->object_bounding_box;
my $object_size = $bb->size;
glTranslatef(0, 0, -max(@$object_size[0..1]));
my @rotmat = quat_to_rotmatrix($self->quat);
glMultMatrixd_p(@rotmat[0..15]);
glRotatef($self->stheta, 1, 0, 0);
glRotatef($self->sphi, 0, 0, 1);
# center everything around 0,0 since that's where we're looking at (glOrtho())
my $center = $bb->center;
glTranslatef(-$center->x, -$center->y, 0); #,,
# draw objects
$self->draw_mesh;
# draw ground and axes
glDisable(GL_LIGHTING);
my $z0 = 0;
{
# draw ground
my $ground_z = GROUND_Z;
if ($self->bed_triangles) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(0.5, 0.5, 0.5, 0.3);
glNormal3d(0,0,1);
glVertexPointer_p(3, $self->bed_triangles);
glDrawArrays(GL_TRIANGLES, 0, $self->bed_triangles->elements / 3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
# draw grid
glTranslatef(0, 0, 0.02);
glLineWidth(3);
glColor3f(1.0, 1.0, 1.0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer_p(3, $self->bed_grid_lines);
glDrawArrays(GL_LINES, 0, $self->bed_grid_lines->elements / 3);
glDisableClientState(GL_VERTEX_ARRAY);
}
{
# draw axes
$ground_z += 0.02;
my $origin = $self->origin;
my $axis_len = 2 * max(@{ $object_size });
glLineWidth(2);
glBegin(GL_LINES);
# draw line for x axis
glColor3f(1, 0, 0);
glVertex3f(@$origin, $ground_z);
glVertex3f($origin->x + $axis_len, $origin->y, $ground_z); #,,
# draw line for y axis
glColor3f(0, 1, 0);
glVertex3f(@$origin, $ground_z);
glVertex3f($origin->x, $origin->y + $axis_len, $ground_z); #++
# draw line for Z axis
glColor3f(0, 0, 1);
glVertex3f(@$origin, $ground_z);
glVertex3f(@$origin, $ground_z+$axis_len);
glEnd();
}
# draw cutting plane
if (defined $self->cutting_plane_z) {
my $plane_z = $z0 + $self->cutting_plane_z;
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glColor4f(0.8, 0.8, 0.8, 0.5);
glVertex3f($bb->x_min-20, $bb->y_min-20, $plane_z);
glVertex3f($bb->x_max+20, $bb->y_min-20, $plane_z);
glVertex3f($bb->x_max+20, $bb->y_max+20, $plane_z);
glVertex3f($bb->x_min-20, $bb->y_max+20, $plane_z);
glEnd();
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
}
glEnable(GL_LIGHTING);
glPopMatrix();
glFlush();
$self->SwapBuffers();
}
sub draw_mesh {
my $self = shift;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
foreach my $volume (@{$self->volumes}) {
glTranslatef(0, 0, -$volume->{z_min});
glVertexPointer_p(3, $volume->{verts});
glCullFace(GL_BACK);
glNormalPointer_p($volume->{norms});
if ($volume->{selected}) {
glColor4f(@{ &SELECTED_COLOR });
} else {
glColor4f(@{ $volume->{color} });
}
glDrawArrays(GL_TRIANGLES, 0, $volume->{verts}->elements / 3);
glTranslatef(0, 0, +$volume->{z_min});
}
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
if (defined $self->cutting_plane_z) {
glLineWidth(2);
glColor3f(0, 0, 0);
glVertexPointer_p(3, $self->cut_lines_vertices);
glDrawArrays(GL_LINES, 0, $self->cut_lines_vertices->elements / 3);
}
glDisableClientState(GL_VERTEX_ARRAY);
}
1;

920
lib/Slic3r/GUI/Projector.pm Normal file
View File

@ -0,0 +1,920 @@
package Slic3r::GUI::Projector;
use strict;
use warnings;
use Wx qw(:dialog :id :misc :sizer :systemsettings :bitmap :button :icon wxTheApp);
use Wx::Event qw(EVT_BUTTON EVT_TEXT_ENTER EVT_SPINCTRL EVT_SLIDER);
use base qw(Wx::Dialog Class::Accessor);
use utf8;
__PACKAGE__->mk_accessors(qw(config config2 screen controller _optgroups));
sub new {
my ($class, $parent) = @_;
my $self = $class->SUPER::new($parent, -1, "Projector for DLP", wxDefaultPosition, wxDefaultSize);
$self->config2({
display => 0,
show_bed => 1,
invert_y => 0,
zoom => 100,
exposure_time => 2,
bottom_exposure_time => 7,
settle_time => 1.5,
bottom_layers => 3,
z_lift => 5,
z_lift_speed => 8,
offset => [0,0],
});
my $ini = eval { Slic3r::Config->read_ini("$Slic3r::GUI::datadir/DLP.ini") };
if ($ini) {
foreach my $opt_id (keys %{$ini->{_}}) {
my $value = $ini->{_}{$opt_id};
if ($opt_id eq 'offset') {
$value = [ split /,/, $value ];
}
$self->config2->{$opt_id} = $value;
}
}
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
$self->config(Slic3r::Config->new_from_defaults(
qw(serial_port serial_speed bed_shape start_gcode end_gcode z_offset)
));
$self->config->apply(wxTheApp->{mainframe}->config);
my @optgroups = ();
{
push @optgroups, my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
parent => $self,
title => 'USB/Serial connection',
config => $self->config,
label_width => 200,
);
$sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Serial port',
);
my $serial_port = $optgroup->get_option('serial_port');
$serial_port->side_widget(sub {
my ($parent) = @_;
my $btn = Wx::BitmapButton->new($self, -1, Wx::Bitmap->new($Slic3r::var->("arrow_rotate_clockwise.png"), wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize, &Wx::wxBORDER_NONE);
$btn->SetToolTipString("Rescan serial ports")
if $btn->can('SetToolTipString');
EVT_BUTTON($self, $btn, sub {
$optgroup->get_field('serial_port')->set_values([ wxTheApp->scan_serial_ports ]);
});
return $btn;
});
my $serial_test = sub {
my ($parent) = @_;
my $btn = $self->{serial_test_btn} = Wx::Button->new($parent, -1,
"Test", wxDefaultPosition, wxDefaultSize, wxBU_LEFT | wxBU_EXACTFIT);
$btn->SetFont($Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("wrench.png"), wxBITMAP_TYPE_PNG));
}
EVT_BUTTON($self, $btn, sub {
my $sender = Slic3r::GCode::Sender->new;
my $res = $sender->connect(
$self->{config}->serial_port,
$self->{config}->serial_speed,
);
if ($res && $sender->wait_connected) {
Slic3r::GUI::show_info($self, "Connection to printer works correctly.", "Success!");
} else {
Slic3r::GUI::show_error($self, "Connection failed.");
}
});
return $btn;
};
$line->append_option($serial_port);
$line->append_option($optgroup->get_option('serial_speed'));
$line->append_widget($serial_test);
$optgroup->append_line($line);
}
}
{
push @optgroups, my $optgroup = Slic3r::GUI::ConfigOptionsGroup->new(
parent => $self,
title => 'G-code',
config => $self->config,
label_width => 200,
);
$sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
my $option = $optgroup->get_option('start_gcode');
$option->height(50);
$option->full_width(1);
$optgroup->append_single_option_line($option);
}
{
my $option = $optgroup->get_option('end_gcode');
$option->height(50);
$option->full_width(1);
$optgroup->append_single_option_line($option);
}
}
my $on_change = sub {
my ($opt_id, $value) = @_;
$self->config2->{$opt_id} = $value;
$self->screen->reposition;
$self->show_print_time;
my $serialized = {};
foreach my $opt_id (keys %{$self->config2}) {
my $value = $self->config2->{$opt_id};
if (ref($value) eq 'ARRAY') {
$value = join ',', @$value;
}
$serialized->{$opt_id} = $value;
}
Slic3r::Config->write_ini(
"$Slic3r::GUI::datadir/DLP.ini",
{ _ => $serialized });
};
{
push @optgroups, my $optgroup = Slic3r::GUI::OptionsGroup->new(
parent => $self,
title => 'Projection',
on_change => $on_change,
label_width => 200,
);
$sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Display',
);
my @displays = 0 .. (Wx::Display::GetCount()-1);
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'display',
type => 'select',
label => 'Display',
tooltip => '',
labels => [@displays],
values => [@displays],
default => $self->config2->{display},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'zoom',
type => 'percent',
label => 'Zoom %',
tooltip => '',
default => $self->config2->{zoom},
min => 0.1,
max => 100,
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'offset',
type => 'point',
label => 'Offset',
tooltip => '',
default => $self->config2->{offset},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'invert_y',
type => 'bool',
label => 'Invert Y',
tooltip => '',
default => $self->config2->{invert_y},
));
$optgroup->append_line($line);
}
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'show_bed',
type => 'bool',
label => 'Show bed',
tooltip => '',
default => $self->config2->{show_bed},
));
}
{
push @optgroups, my $optgroup = Slic3r::GUI::OptionsGroup->new(
parent => $self,
title => 'Print',
on_change => $on_change,
label_width => 200,
);
$sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Time (seconds)',
);
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'bottom_exposure_time',
type => 'f',
label => 'Bottom exposure',
tooltip => '',
default => $self->config2->{bottom_exposure_time},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'exposure_time',
type => 'f',
label => 'Exposure',
tooltip => '',
default => $self->config2->{exposure_time},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'settle_time',
type => 'f',
label => 'Settle',
tooltip => '',
default => $self->config2->{settle_time},
));
$optgroup->append_line($line);
}
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'bottom_layers',
type => 'i',
label => 'Bottom layers',
tooltip => '',
default => $self->config2->{bottom_layers},
));
{
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Z Lift',
);
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'z_lift',
type => 'f',
label => 'Distance',
sidetext => 'mm',
tooltip => '',
default => $self->config2->{z_lift},
));
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
opt_id => 'z_lift_speed',
type => 'f',
label => 'Speed',
sidetext => 'mm/s',
tooltip => '',
default => $self->config2->{z_lift_speed},
));
$optgroup->append_line($line);
}
}
$self->_optgroups([@optgroups]);
{
my $sizer1 = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer->Add($sizer1, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
my $btn = $self->{btn_manual_control} = Wx::Button->new($self, -1, 'Manual Control', wxDefaultPosition, wxDefaultSize);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG));
}
$sizer1->Add($btn, 0);
EVT_BUTTON($self, $btn, sub {
my $sender = Slic3r::GCode::Sender->new;
my $res = $sender->connect(
$self->config->serial_port,
$self->config->serial_speed,
);
if (!$res || !$sender->wait_connected) {
Slic3r::GUI::show_error(undef, "Connection failed. Check serial port and speed.");
return;
}
my $dlg = Slic3r::GUI::Controller::ManualControlDialog->new
($self, $self->config, $sender);
$dlg->ShowModal;
$sender->disconnect;
});
}
{
my $btn = $self->{btn_print} = Wx::Button->new($self, -1, 'Print', wxDefaultPosition, wxDefaultSize);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_play.png"), wxBITMAP_TYPE_PNG));
}
$sizer1->Add($btn, 0);
EVT_BUTTON($self, $btn, sub {
$self->controller->start_print;
$self->_update_buttons;
$self->_set_status('');
});
}
{
my $btn = $self->{btn_stop} = Wx::Button->new($self, -1, 'Stop/Black', wxDefaultPosition, wxDefaultSize);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("control_stop.png"), wxBITMAP_TYPE_PNG));
}
$sizer1->Add($btn, 0);
EVT_BUTTON($self, $btn, sub {
$self->controller->stop_print;
$self->_update_buttons;
$self->_set_status('');
});
}
{
{
my $text = Wx::StaticText->new($self, -1, "Layer:", wxDefaultPosition, wxDefaultSize);
$text->SetFont($Slic3r::GUI::small_font);
$sizer1->Add($text, 0, wxEXPAND | wxLEFT, 10);
}
{
my $spin = $self->{layers_spinctrl} = Wx::SpinCtrl->new($self, -1, 0, wxDefaultPosition, [60,-1],
0, 0, 300, 0);
$sizer1->Add($spin, 0);
EVT_SPINCTRL($self, $spin, sub {
my $value = $spin->GetValue;
$self->{layers_slider}->SetValue($value);
$self->controller->project_layer($value);
$self->_update_buttons;
});
}
{
my $slider = $self->{layers_slider} = Wx::Slider->new(
$self, -1,
0, # default
0, # min
300, # max
wxDefaultPosition,
wxDefaultSize,
);
$sizer1->Add($slider, 1);
EVT_SLIDER($self, $slider, sub {
my $value = $slider->GetValue;
$self->{layers_spinctrl}->SetValue($value);
$self->controller->project_layer($value);
$self->_update_buttons;
});
}
}
my $sizer2 = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer->Add($sizer2, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
{
$self->{status_text} = Wx::StaticText->new($self, -1, "", wxDefaultPosition, wxDefaultSize);
$self->{status_text}->SetFont($Slic3r::GUI::small_font);
$sizer2->Add($self->{status_text}, 1 | wxEXPAND);
}
}
{
my $buttons = $self->CreateStdDialogButtonSizer(wxOK);
EVT_BUTTON($self, wxID_CLOSE, sub {
$self->_close;
});
$sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
}
$self->SetSizer($sizer);
$sizer->SetSizeHints($self);
# reuse existing screen if any
if ($Slic3r::GUI::DLP_projection_screen) {
$self->screen($Slic3r::GUI::DLP_projection_screen);
$self->screen->config($self->config);
$self->screen->config2($self->config2);
} else {
$self->screen(Slic3r::GUI::Projector::Screen->new($parent, $self->config, $self->config2));
$Slic3r::GUI::DLP_projection_screen = $self->screen;
}
$self->screen->reposition;
$self->screen->Show;
wxTheApp->{mainframe}->Hide;
# initialize controller
$self->controller(Slic3r::GUI::Projector::Controller->new(
config => $self->config,
config2 => $self->config2,
screen => $self->screen,
on_project_layer => sub {
my ($layer_num) = @_;
$self->{layers_spinctrl}->SetValue($layer_num);
$self->{layers_slider}->SetValue($layer_num);
my $duration = $self->controller->remaining_print_time;
$self->_set_status(sprintf "Printing layer %d/%d (z = %.2f); %d minutes and %d seconds left",
$layer_num, $self->controller->layer_count,
$self->controller->current_layer_height,
int($duration/60), ($duration - int($duration/60)*60)); # % truncates to integer
},
on_print_completed => sub {
$self->_update_buttons;
$self->_set_status('');
Wx::Bell();
},
));
{
my $max = $self->controller->layer_count-1;
$self->{layers_spinctrl}->SetRange(0, $max);
$self->{layers_slider}->SetRange(0, $max);
}
$self->_update_buttons;
$self->show_print_time;
return $self;
}
sub _update_buttons {
my ($self) = @_;
my $is_printing = $self->controller->is_printing;
my $is_projecting = $self->controller->is_projecting;
$self->{btn_manual_control}->Show(!$is_printing);
$self->{btn_print}->Show(!$is_printing && !$is_projecting);
$self->{btn_stop}->Show($is_printing || $is_projecting);
$self->{layers_spinctrl}->Enable(!$is_printing);
$self->{layers_slider}->Enable(!$is_printing);
if ($is_printing) {
$_->disable for @{$self->_optgroups};
} else {
$_->enable for @{$self->_optgroups};
}
$self->Layout;
}
sub _set_status {
my ($self, $status) = @_;
$self->{status_text}->SetLabel($status // '');
$self->{status_text}->Wrap($self->{status_text}->GetSize->GetWidth);
$self->{status_text}->Refresh;
$self->Layout;
}
sub show_print_time {
my ($self) = @_;
my $duration = $self->controller->print_time;
$self->_set_status(sprintf "Estimated print time: %d minutes and %d seconds",
int($duration/60), ($duration - int($duration/60)*60)); # % truncates to integer
}
sub _close {
my $self = shift;
# if projection screen is not on the same display as our dialog,
# ask the user whether they want to keep it open
my $keep_screen = 0;
my $display_area = Wx::Display->new($self->config2->{display})->GetGeometry;
if (!$display_area->Contains($self->GetScreenPosition)) {
my $res = Wx::MessageDialog->new($self, "Do you want to keep the black screen open?", 'Black screen', wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION)->ShowModal;
$keep_screen = ($res == wxID_YES);
}
if ($keep_screen) {
$self->screen->config(undef);
$self->screen->config2(undef);
$self->screen->Refresh;
} else {
$self->screen->Destroy;
$self->screen(undef);
$Slic3r::GUI::DLP_projection_screen = undef;
}
wxTheApp->{mainframe}->Show;
my $printer_tab = wxTheApp->{mainframe}{options_tabs}{printer};
$printer_tab->load_config($self->config);
$self->EndModal(wxID_OK);
}
package Slic3r::GUI::Projector::Controller;
use Moo;
use Wx qw(wxTheApp :id :timer);
use Wx::Event qw(EVT_TIMER);
use Slic3r::Print::State ':steps';
use Time::HiRes qw(gettimeofday tv_interval);
has 'config' => (is => 'ro', required => 1);
has 'config2' => (is => 'ro', required => 1);
has 'screen' => (is => 'ro', required => 1);
has 'on_project_layer' => (is => 'rw');
has 'on_print_completed' => (is => 'rw');
has 'sender' => (is => 'rw');
has 'timer' => (is => 'rw');
has 'is_printing' => (is => 'rw', default => sub { 0 });
has '_print' => (is => 'rw');
has '_layers' => (is => 'rw');
has '_heights' => (is => 'rw');
has '_layer_num' => (is => 'rw');
has '_timer_cb' => (is => 'rw');
sub BUILD {
my ($self) = @_;
Slic3r::GUI::disable_screensaver();
$self->set_print(wxTheApp->{mainframe}->{plater}->{print});
# projection timer
my $timer_id = &Wx::NewId();
$self->timer(Wx::Timer->new($self->screen, $timer_id));
EVT_TIMER($self->screen, $timer_id, sub {
my $cb = $self->_timer_cb;
$self->_timer_cb(undef);
$cb->();
});
}
sub delay {
my ($self, $wait, $cb) = @_;
$self->_timer_cb($cb);
$self->timer->Start($wait * 1000, wxTIMER_ONE_SHOT);
}
sub set_print {
my ($self, $print) = @_;
# make sure layers were sliced
{
my $progress_dialog;
foreach my $object (@{$print->objects}) {
next if $object->step_done(STEP_SLICE);
$progress_dialog //= Wx::ProgressDialog->new('Slicing…', "Processing layers…", 100, undef, 0);
$progress_dialog->Pulse;
$object->slice;
}
$progress_dialog->Destroy if $progress_dialog;
}
$self->_print($print);
# sort layers by Z
my %layers = ();
foreach my $layer (map { @{$_->layers}, @{$_->support_layers} } @{$print->objects}) {
my $height = $layer->print_z;
$layers{$height} //= [];
push @{$layers{$height}}, $layer;
}
$self->_layers({ %layers });
$self->_heights([ sort { $a <=> $b } keys %layers ]);
}
sub layer_count {
my ($self) = @_;
return scalar @{$self->_heights};
}
sub current_layer_height {
my ($self) = @_;
return $self->_heights->[$self->_layer_num];
}
sub start_print {
my ($self) = @_;
{
$self->sender(Slic3r::GCode::Sender->new);
my $res = $self->sender->connect(
$self->config->serial_port,
$self->config->serial_speed,
);
if (!$res || !$self->sender->wait_connected) {
Slic3r::GUI::show_error(undef, "Connection failed. Check serial port and speed.");
return;
}
Slic3r::debugf "connected to " . $self->config->serial_port . "\n";
# send custom start G-code
$self->sender->send($_, 1) for grep !/^;/, split /\n/, $self->config->start_gcode;
}
$self->is_printing(1);
# TODO: block until the G1 command has been performed
# we could do this with M400 + M115 but maybe it's not portable
$self->delay(5, sub {
# start with black
Slic3r::debugf "starting black projection\n";
$self->_layer_num(-1);
$self->screen->project_layers(undef);
$self->delay($self->config2->{settle_time}, sub {
$self->project_next_layer;
});
});
}
sub stop_print {
my ($self) = @_;
if ($self->sender) {
$self->sender->disconnect;
}
$self->is_printing(0);
$self->timer->Stop;
$self->_timer_cb(undef);
$self->screen->project_layers(undef);
}
sub print_completed {
my ($self) = @_;
# send custom end G-code
if ($self->sender) {
$self->sender->send($_, 1) for grep !/^;/, split /\n/, $self->config->end_gcode;
}
# call this before the on_print_completed callback otherwise buttons
# won't be updated correctly
$self->stop_print;
$self->on_print_completed->()
if $self->is_printing && $self->on_print_completed;
}
sub is_projecting {
my ($self) = @_;
return defined $self->screen->layers;
}
sub project_layer {
my ($self, $layer_num) = @_;
if (!defined $layer_num || $layer_num >= $self->layer_count) {
$self->screen->project_layers(undef);
return;
}
my @layers = @{ $self->_layers->{ $self->_heights->[$layer_num] } };
$self->screen->project_layers([ @layers ]);
}
sub project_next_layer {
my ($self) = @_;
$self->_layer_num($self->_layer_num + 1);
Slic3r::debugf "projecting layer %d\n", $self->_layer_num;
if ($self->_layer_num >= $self->layer_count) {
$self->print_completed;
return;
}
$self->on_project_layer->($self->_layer_num) if $self->on_project_layer;
if ($self->sender) {
my $z = $self->current_layer_height + $self->config->z_offset;
my $F = $self->config2->{z_lift_speed} * 60;
if ($self->config2->{z_lift} != 0) {
$self->sender->send(sprintf("G1 Z%.5f F%d", $z + $self->config2->{z_lift}, $F), 1);
}
$self->sender->send(sprintf("G1 Z%.5f F%d", $z, $F), 1);
}
# TODO: we should block until G1 commands have been performed, see note below
$self->delay($self->config2->{settle_time}, sub {
$self->project_layer($self->_layer_num);
# get exposure time
my $time = $self->config2->{exposure_time};
if ($self->_layer_num < $self->config2->{bottom_layers}) {
$time = $self->config2->{bottom_exposure_time};
}
$self->delay($time, sub {
$self->screen->project_layers(undef);
$self->project_next_layer;
});
});
}
sub remaining_print_time {
my ($self) = @_;
my $remaining_layers = @{$self->_heights} - $self->_layer_num;
my $remaining_bottom_layers = $self->_layer_num >= $self->config2->{bottom_layers}
? 0
: $self->config2->{bottom_layers} - $self->_layer_num;
return $remaining_bottom_layers * $self->config2->{bottom_exposure_time}
+ ($remaining_layers - $remaining_bottom_layers) * $self->config2->{exposure_time}
+ $remaining_layers * $self->config2->{settle_time};
}
sub print_time {
my ($self) = @_;
return $self->config2->{bottom_layers} * $self->config2->{bottom_exposure_time}
+ (@{$self->_heights} - $self->config2->{bottom_layers}) * $self->config2->{exposure_time}
+ @{$self->_heights} * $self->config2->{settle_time};
}
sub DESTROY {
my ($self) = @_;
$self->timer->Stop if $self->timer;
$self->sender->disconnect if $self->sender;
Slic3r::GUI::enable_screensaver();
}
package Slic3r::GUI::Projector::Screen;
use Wx qw(:dialog :id :misc :sizer :colour :pen :brush :font wxBG_STYLE_CUSTOM);
use Wx::Event qw(EVT_PAINT EVT_SIZE);
use base qw(Wx::Dialog Class::Accessor);
use List::Util qw(min);
use Slic3r::Geometry qw(X Y unscale scale);
use Slic3r::Geometry::Clipper qw(intersection_pl);
__PACKAGE__->mk_accessors(qw(config config2 scaling_factor bed_origin layers));
sub new {
my ($class, $parent, $config, $config2) = @_;
my $self = $class->SUPER::new($parent, -1, "Projector", wxDefaultPosition, wxDefaultSize, 0);
$self->config($config);
$self->config2($config2);
$self->SetBackgroundStyle(wxBG_STYLE_CUSTOM);
EVT_SIZE($self, \&_resize);
EVT_PAINT($self, \&_repaint);
$self->_resize;
return $self;
}
sub reposition {
my ($self) = @_;
my $display = Wx::Display->new($self->config2->{display});
my $area = $display->GetGeometry;
$self->Move($area->GetPosition);
# ShowFullScreen doesn't use the right screen
#$self->ShowFullScreen($self->config2->{fullscreen});
$self->SetSize($area->GetSize);
$self->_resize;
$self->Refresh;
}
sub _resize {
my ($self) = @_;
return if !$self->config;
my ($cw, $ch) = $self->GetSizeWH;
# get bed shape polygon
my $bed_polygon = Slic3r::Polygon->new_scale(@{$self->config->bed_shape});
my $bb = $bed_polygon->bounding_box;
my $size = $bb->size;
my $center = $bb->center;
# calculate the scaling factor needed for constraining print bed area inside preview
# scaling_factor is expressed in pixel / mm
$self->scaling_factor(min($cw / unscale($size->x), $ch / unscale($size->y))); #)
# apply zoom to scaling factor
if ($self->config2->{zoom} != 0) {
# TODO: make sure min and max in the option config are enforced
$self->scaling_factor($self->scaling_factor * ($self->config2->{zoom}/100));
}
# calculate the displacement needed for centering bed on screen
$self->bed_origin([
$cw/2 - (unscale($center->x) - $self->config2->{offset}->[X]) * $self->scaling_factor,
$ch/2 - (unscale($center->y) - $self->config2->{offset}->[Y]) * $self->scaling_factor, #))
]);
$self->Refresh;
}
sub project_layers {
my ($self, $layers) = @_;
$self->layers($layers);
$self->Refresh;
}
sub _repaint {
my ($self) = @_;
my $dc = Wx::AutoBufferedPaintDC->new($self);
my ($cw, $ch) = $self->GetSizeWH;
return if $cw == 0; # when canvas is not rendered yet, size is 0,0
$dc->SetPen(Wx::Pen->new(wxBLACK, 1, wxSOLID));
$dc->SetBrush(Wx::Brush->new(wxBLACK, wxSOLID));
$dc->DrawRectangle(0, 0, $cw, $ch);
return if !$self->config;
# turn size into max visible coordinates
# TODO: or should we use ClientArea?
$cw--;
$ch--;
# draw bed
if ($self->config2->{show_bed}) {
$dc->SetPen(Wx::Pen->new(wxRED, 2, wxSOLID));
$dc->SetBrush(Wx::Brush->new(wxWHITE, wxTRANSPARENT));
# draw contour
my $bed_polygon = Slic3r::Polygon->new_scale(@{$self->config->bed_shape});
$dc->DrawPolygon($self->scaled_points_to_pixel($bed_polygon), 0, 0);
# draw grid
$dc->SetPen(Wx::Pen->new(wxRED, 1, wxSOLID));
{
my $bb = $bed_polygon->bounding_box;
my $step = scale 10; # 1cm grid
my @polylines = ();
for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
push @polylines, Slic3r::Polyline->new([$x, $bb->y_min], [$x, $bb->y_max]);
}
for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
push @polylines, Slic3r::Polyline->new([$bb->x_min, $y], [$bb->x_max, $y]);
}
$dc->DrawLine(map @$_, @$_)
for map $self->scaled_points_to_pixel([ @$_[0,-1] ]),
@{intersection_pl(\@polylines, [$bed_polygon])};
}
# draw axes orientation
$dc->SetPen(Wx::Pen->new(wxWHITE, 4, wxSOLID));
{
foreach my $endpoint ([10, 0], [0, 10]) {
$dc->DrawLine(
map @{$self->unscaled_point_to_pixel($_)}, [0,0], $endpoint
);
}
$dc->SetTextForeground(wxWHITE);
$dc->SetFont(Wx::Font->new(20, wxDEFAULT, wxNORMAL, wxNORMAL));
$dc->DrawText("X", @{$self->unscaled_point_to_pixel([10, -2])});
$dc->DrawText("Y", @{$self->unscaled_point_to_pixel([-2, 10])});
}
}
return if !defined $self->layers;
# get layers at this height
# draw layers
$dc->SetPen(Wx::Pen->new(wxWHITE, 1, wxSOLID));
foreach my $layer (@{$self->layers}) {
my @polygons = sort { $a->contains_point($b->first_point) ? -1 : 1 } map @$_, @{ $layer->slices };
foreach my $copy (@{$layer->object->_shifted_copies}) {
foreach my $polygon (@polygons) {
$polygon = $polygon->clone;
$polygon->translate(@$copy);
if ($polygon->is_counter_clockwise) {
$dc->SetBrush(Wx::Brush->new(wxWHITE, wxSOLID));
} else {
$dc->SetBrush(Wx::Brush->new(wxBLACK, wxSOLID));
}
$dc->DrawPolygon($self->scaled_points_to_pixel($polygon->pp), 0, 0);
}
}
}
}
# convert a model coordinate into a pixel coordinate
sub unscaled_point_to_pixel {
my ($self, $point) = @_;
my $zero = $self->bed_origin;
my $p = [
$point->[X] * $self->scaling_factor + $zero->[X],
$point->[Y] * $self->scaling_factor + $zero->[Y],
];
if (!$self->config2->{invert_y}) {
my $ch = $self->GetSize->GetHeight;
$p->[Y] = $ch - $p->[Y];
}
return $p;
}
sub scaled_points_to_pixel {
my ($self, $points) = @_;
return [
map $self->unscaled_point_to_pixel($_),
map Slic3r::Pointf->new_unscale(@$_),
@$points
];
}
1;

View File

@ -22,6 +22,7 @@ sub new {
$self->{vsizer} = Wx::BoxSizer->new(wxVERTICAL);
$self->SetSizer($self->{vsizer});
$self->build;
$self->_update;
{
my $label = Wx::StaticText->new($self, -1, "Want more options? Switch to the Expert Mode.", wxDefaultPosition, wxDefaultSize);
@ -71,12 +72,14 @@ sub load_config {
$self->{config}->set($opt_key, $config->get($opt_key));
}
$_->reload_config for @{$self->{optgroups}};
$self->_update;
}
sub load_presets {}
sub is_dirty { 0 }
sub config { $_[0]->{config}->clone }
sub _update {}
sub on_value_change {
my ($self, $cb) = @_;
@ -88,7 +91,19 @@ sub on_presets_changed {}
# propagate event to the parent
sub _on_value_change {
my $self = shift;
$self->{on_value_change}->(@_) if $self->{on_value_change};
$self->_update;
}
sub get_field {
my ($self, $opt_key, $opt_index) = @_;
foreach my $optgroup (@{ $self->{optgroups} }) {
my $field = $optgroup->get_fieldc($opt_key, $opt_index);
return $field if defined $field;
}
return undef;
}
package Slic3r::GUI::SimpleTab::Print;
@ -104,10 +119,12 @@ sub build {
$self->init_config_options(qw(
layer_height perimeters top_solid_layers bottom_solid_layers
fill_density fill_pattern support_material support_material_spacing raft_layers
fill_density fill_pattern external_fill_pattern
support_material support_material_spacing raft_layers
support_material_contact_distance dont_support_bridges
perimeter_speed infill_speed travel_speed
brim_width
complete_objects extruder_clearance_radius extruder_clearance_height
xy_size_compensation
));
{
@ -127,12 +144,15 @@ sub build {
my $optgroup = $self->new_optgroup('Infill');
$optgroup->append_single_option_line('fill_density');
$optgroup->append_single_option_line('fill_pattern');
$optgroup->append_single_option_line('external_fill_pattern');
}
{
my $optgroup = $self->new_optgroup('Support material');
$optgroup->append_single_option_line('support_material');
$optgroup->append_single_option_line('support_material_spacing');
$optgroup->append_single_option_line('support_material_contact_distance');
$optgroup->append_single_option_line('dont_support_bridges');
$optgroup->append_single_option_line('raft_layers');
}
@ -149,18 +169,35 @@ sub build {
}
{
my $optgroup = $self->new_optgroup('Sequential printing');
$optgroup->append_single_option_line('complete_objects');
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Extruder clearance (mm)',
);
$line->append_option($optgroup->get_option('extruder_clearance_radius'));
$line->append_option($optgroup->get_option('extruder_clearance_height'));
$optgroup->append_line($line);
my $optgroup = $self->new_optgroup('Other');
$optgroup->append_single_option_line('xy_size_compensation');
}
}
sub _update {
my ($self) = @_;
my $config = $self->{config};
my $have_perimeters = $config->perimeters > 0;
$self->get_field($_)->toggle($have_perimeters)
for qw(perimeter_speed);
my $have_infill = $config->fill_density > 0;
my $have_solid_infill = $config->top_solid_layers > 0 || $config->bottom_solid_layers > 0;
$self->get_field($_)->toggle($have_infill)
for qw(fill_pattern);
$self->get_field($_)->toggle($have_solid_infill)
for qw(external_fill_pattern);
$self->get_field($_)->toggle($have_infill || $have_solid_infill)
for qw(infill_speed);
my $have_support_material = $config->support_material || $config->raft_layers > 0;
$self->get_field($_)->toggle($have_support_material)
for qw(support_material_spacing dont_support_bridges
support_material_contact_distance);
}
package Slic3r::GUI::SimpleTab::Filament;
use base 'Slic3r::GUI::SimpleTab';
@ -206,6 +243,8 @@ sub build {
package Slic3r::GUI::SimpleTab::Printer;
use base 'Slic3r::GUI::SimpleTab';
use Wx qw(:sizer :button :bitmap :misc :id);
use Wx::Event qw(EVT_BUTTON);
sub name { 'printer' }
sub title { 'Printer Settings' }
@ -214,17 +253,46 @@ sub build {
my $self = shift;
$self->init_config_options(qw(
bed_shape
z_offset
gcode_flavor
nozzle_diameter
retract_length retract_lift
retract_length retract_lift wipe
start_gcode
end_gcode
));
{
my $bed_shape_widget = sub {
my ($parent) = @_;
my $btn = Wx::Button->new($parent, -1, "Set…", wxDefaultPosition, wxDefaultSize, wxBU_LEFT);
$btn->SetFont($Slic3r::GUI::small_font);
if ($Slic3r::GUI::have_button_icons) {
$btn->SetBitmap(Wx::Bitmap->new($Slic3r::var->("cog.png"), wxBITMAP_TYPE_PNG));
}
my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer->Add($btn);
EVT_BUTTON($self, $btn, sub {
my $dlg = Slic3r::GUI::BedShapeDialog->new($self, $self->{config}->bed_shape);
if ($dlg->ShowModal == wxID_OK) {
my $value = $dlg->GetValue;
$self->{config}->set('bed_shape', $value);
$self->_on_value_change('bed_shape', $value);
}
});
return $sizer;
};
my $optgroup = $self->new_optgroup('Size and coordinates');
# TODO: add bed_shape
my $line = Slic3r::GUI::OptionsGroup::Line->new(
label => 'Bed shape',
widget => $bed_shape_widget,
);
$optgroup->append_line($line);
$optgroup->append_single_option_line('z_offset');
}
@ -242,6 +310,7 @@ sub build {
my $optgroup = $self->new_optgroup('Retraction');
$optgroup->append_single_option_line('retract_length', 0);
$optgroup->append_single_option_line('retract_lift', 0);
$optgroup->append_single_option_line('wipe', 0);
}
{
@ -265,4 +334,14 @@ sub build {
}
}
sub _update {
my ($self) = @_;
my $config = $self->{config};
my $have_retraction = $config->retract_length->[0] > 0;
$self->get_field($_, 0)->toggle($have_retraction)
for qw(retract_lift wipe);
}
1;

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@ our @EXPORT_OK = qw(
point_along_segment polygon_segment_having_point polygon_has_subsegment
deg2rad rad2deg
rotate_points move_points
dot perp polygon_points_visibility
dot perp
line_intersection bounding_box bounding_box_intersect
angle3points
chained_path chained_path_from collinear scale unscale
@ -27,9 +27,6 @@ our @EXPORT_OK = qw(
use constant PI => 4 * atan2(1, 1);
use constant A => 0;
use constant B => 1;
use constant X => 0;
use constant Y => 1;
use constant Z => 2;
use constant X1 => 0;
use constant Y1 => 1;
use constant X2 => 2;
@ -207,23 +204,6 @@ sub polygon_is_convex {
return 1;
}
sub deg2rad {
my ($degrees) = @_;
return PI() * $degrees / 180;
}
sub rad2deg {
my ($rad) = @_;
return $rad / PI() * 180;
}
sub rad2deg_dir {
my ($rad) = @_;
$rad = ($rad < PI) ? (-$rad + PI/2) : ($rad + PI/2);
$rad += PI if $rad < 0;
return rad2deg($rad);
}
sub rotate_points {
my ($radians, $center, @points) = @_;
$center //= [0,0];
@ -291,19 +271,6 @@ sub perp {
return $u->[X] * $v->[Y] - $u->[Y] * $v->[X];
}
sub polygon_points_visibility {
my ($polygon, $p1, $p2) = @_;
my $our_line = [ $p1, $p2 ];
foreach my $line (polygon_lines($polygon)) {
my $intersection = line_intersection($our_line, $line, 1) // next;
next if grep points_coincide($intersection, $_), $p1, $p2;
return 0;
}
return 1;
}
sub line_intersects_any {
my ($line, $lines) = @_;
for (@$lines) {
@ -635,119 +602,4 @@ sub douglas_peucker2 {
return [ map $points->[$_], sort keys %keep ];
}
sub arrange {
my ($total_parts, $partx, $party, $dist, $bb) = @_;
my $linint = sub {
my ($value, $oldmin, $oldmax, $newmin, $newmax) = @_;
return ($value - $oldmin) * ($newmax - $newmin) / ($oldmax - $oldmin) + $newmin;
};
# use actual part size (the largest) plus separation distance (half on each side) in spacing algorithm
$partx += $dist;
$party += $dist;
my ($areax, $areay);
if (defined $bb) {
my $size = $bb->size;
($areax, $areay) = @$size[X,Y];
} else {
# bogus area size, large enough not to trigger the error below
$areax = $partx * $total_parts;
$areay = $party * $total_parts;
}
# this is how many cells we have available into which to put parts
my $cellw = int(($areax + $dist) / $partx);
my $cellh = int(($areay + $dist) / $party);
die "$total_parts parts won't fit in your print area!\n" if $total_parts > ($cellw * $cellh);
# width and height of space used by cells
my $w = $cellw * $partx;
my $h = $cellh * $party;
# left and right border positions of space used by cells
my $l = ($areax - $w) / 2;
my $r = $l + $w;
# top and bottom border positions
my $t = ($areay - $h) / 2;
my $b = $t + $h;
# list of cells, sorted by distance from center
my @cellsorder;
# work out distance for all cells, sort into list
for my $i (0..$cellw-1) {
for my $j (0..$cellh-1) {
my $cx = $linint->($i + 0.5, 0, $cellw, $l, $r);
my $cy = $linint->($j + 0.5, 0, $cellh, $t, $b);
my $xd = abs(($areax / 2) - $cx);
my $yd = abs(($areay / 2) - $cy);
my $c = {
location => [$cx, $cy],
index => [$i, $j],
distance => $xd * $xd + $yd * $yd - abs(($cellw / 2) - ($i + 0.5)),
};
BINARYINSERTIONSORT: {
my $index = $c->{distance};
my $low = 0;
my $high = @cellsorder;
while ($low < $high) {
my $mid = ($low + (($high - $low) / 2)) | 0;
my $midval = $cellsorder[$mid]->[0];
if ($midval < $index) {
$low = $mid + 1;
} elsif ($midval > $index) {
$high = $mid;
} else {
splice @cellsorder, $mid, 0, [$index, $c];
last BINARYINSERTIONSORT;
}
}
splice @cellsorder, $low, 0, [$index, $c];
}
}
}
# the extents of cells actually used by objects
my ($lx, $ty, $rx, $by) = (0, 0, 0, 0);
# now find cells actually used by objects, map out the extents so we can position correctly
for my $i (1..$total_parts) {
my $c = $cellsorder[$i - 1];
my $cx = $c->[1]->{index}->[0];
my $cy = $c->[1]->{index}->[1];
if ($i == 1) {
$lx = $rx = $cx;
$ty = $by = $cy;
} else {
$rx = $cx if $cx > $rx;
$lx = $cx if $cx < $lx;
$by = $cy if $cy > $by;
$ty = $cy if $cy < $ty;
}
}
# now we actually place objects into cells, positioned such that the left and bottom borders are at 0
my @positions = ();
for (1..$total_parts) {
my $c = shift @cellsorder;
my $cx = $c->[1]->{index}->[0] - $lx;
my $cy = $c->[1]->{index}->[1] - $ty;
push @positions, [$cx * $partx, $cy * $party];
}
if (defined $bb) {
$_->[X] += $bb->x_min for @positions;
$_->[Y] += $bb->y_min for @positions;
}
return @positions;
}
1;

View File

@ -2,10 +2,6 @@ package Slic3r::Layer;
use strict;
use warnings;
use List::Util qw(first);
use Slic3r::Geometry qw(scale chained_path);
use Slic3r::Geometry::Clipper qw(union_ex intersection_ex);
# the following two were previously generated by Moo
sub print {
my $self = shift;
@ -17,12 +13,6 @@ sub config {
return $self->object->config;
}
# the purpose of this method is to be overridden for ::Support layers
sub islands {
my $self = shift;
return $self->slices;
}
sub region {
my $self = shift;
my ($region_id) = @_;
@ -39,94 +29,16 @@ sub regions {
return [ map $self->get_region($_), 0..($self->region_count-1) ];
}
sub merge_slices {
sub make_fill {
my ($self) = @_;
$_->merge_slices for @{$self->regions};
}
sub make_perimeters {
my $self = shift;
Slic3r::debugf "Making perimeters for layer %d\n", $self->id;
# keep track of regions whose perimeters we have already generated
my %done = (); # region_id => 1
for my $region_id (0..$#{$self->regions}) {
next if $done{$region_id};
my $layerm = $self->regions->[$region_id];
$done{$region_id} = 1;
# find compatible regions
my @layerms = ($layerm);
for my $i (($region_id+1)..$#{$self->regions}) {
my $config = $self->regions->[$i]->config;
my $layerm_config = $layerm->config;
if ($config->perimeter_extruder == $layerm_config->perimeter_extruder
&& $config->perimeters == $layerm_config->perimeters
&& $config->perimeter_speed == $layerm_config->perimeter_speed
&& $config->gap_fill_speed == $layerm_config->gap_fill_speed
&& $config->overhangs == $layerm_config->overhangs
&& $config->perimeter_extrusion_width == $layerm_config->perimeter_extrusion_width
&& $config->thin_walls == $layerm_config->thin_walls
&& $config->external_perimeters_first == $layerm_config->external_perimeters_first) {
push @layerms, $self->regions->[$i];
$done{$i} = 1;
}
}
if (@layerms == 1) { # optimization
$layerm->fill_surfaces->clear;
$layerm->make_perimeters($layerm->slices, $layerm->fill_surfaces);
} else {
# group slices (surfaces) according to number of extra perimeters
my %slices = (); # extra_perimeters => [ surface, surface... ]
foreach my $surface (map @{$_->slices}, @layerms) {
my $extra = $surface->extra_perimeters;
$slices{$extra} ||= [];
push @{$slices{$extra}}, $surface;
}
# merge the surfaces assigned to each group
my $new_slices = Slic3r::Surface::Collection->new;
foreach my $surfaces (values %slices) {
$new_slices->append(Slic3r::Surface->new(
surface_type => $surfaces->[0]->surface_type,
extra_perimeters => $surfaces->[0]->extra_perimeters,
expolygon => $_,
)) for @{union_ex([ map $_->p, @$surfaces ], 1)};
}
# make perimeters
my $fill_surfaces = Slic3r::Surface::Collection->new;
$layerm->make_perimeters($new_slices, $fill_surfaces);
# assign fill_surfaces to each layer
if ($fill_surfaces->count > 0) {
foreach my $lm (@layerms) {
my $expolygons = intersection_ex(
[ map $_->p, @$fill_surfaces ],
[ map $_->p, @{$lm->slices} ],
);
$lm->fill_surfaces->clear;
$lm->fill_surfaces->append(Slic3r::Surface->new(
surface_type => $fill_surfaces->[0]->surface_type,
extra_perimeters => $fill_surfaces->[0]->extra_perimeters,
expolygon => $_,
)) for @$expolygons;
}
}
}
foreach my $layerm (@{$self->regions}) {
$layerm->fills->clear;
$layerm->fills->append($_) for $self->object->fill_maker->make_fill($layerm);
}
}
package Slic3r::Layer::Support;
our @ISA = qw(Slic3r::Layer);
sub islands {
my $self = shift;
return [ @{$self->slices}, @{$self->support_islands} ];
}
1;

View File

@ -1,277 +0,0 @@
package Slic3r::Layer::BridgeDetector;
use Moo;
use List::Util qw(first sum max min);
use Slic3r::Geometry qw(PI unscale scaled_epsilon rad2deg epsilon directions_parallel_within);
use Slic3r::Geometry::Clipper qw(intersection_pl intersection_ex union offset diff_pl union_ex
intersection_ppl);
has 'expolygon' => (is => 'ro', required => 1);
has 'lower_slices' => (is => 'rw', required => 1); # ExPolygons or ExPolygonCollection
has 'extrusion_width' => (is => 'rw', required => 1); # scaled
has 'resolution' => (is => 'rw', default => sub { PI/36 });
has '_edges' => (is => 'rw'); # Polylines representing the supporting edges
has '_anchors' => (is => 'rw'); # ExPolygons
has 'angle' => (is => 'rw');
sub BUILD {
my ($self) = @_;
# outset our bridge by an arbitrary amout; we'll use this outer margin
# for detecting anchors
my $grown = $self->expolygon->offset(+$self->extrusion_width);
# detect what edges lie on lower slices
$self->_edges(my $edges = []);
foreach my $lower (@{$self->lower_slices}) {
# turn bridge contour and holes into polylines and then clip them
# with each lower slice's contour
push @$edges, @{intersection_ppl($grown, [ $lower->contour ])};
}
Slic3r::debugf " bridge has %d support(s)\n", scalar(@$edges);
# detect anchors as intersection between our bridge expolygon and the lower slices
$self->_anchors(intersection_ex(
$grown,
[ map @$_, @{$self->lower_slices} ],
1, # safety offset required to avoid Clipper from detecting empty intersection while Boost actually found some @edges
));
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output("bridge.svg",
expolygons => [ $self->expolygon ],
red_expolygons => $self->lower_slices,
polylines => $self->_edges,
);
}
}
sub detect_angle {
my ($self) = @_;
return undef if !@{$self->_edges};
my @edges = @{$self->_edges};
my $anchors = $self->_anchors;
if (!@$anchors) {
$self->angle(undef);
return undef;
}
# Outset the bridge expolygon by half the amount we used for detecting anchors;
# we'll use this one to clip our test lines and be sure that their endpoints
# are inside the anchors and not on their contours leading to false negatives.
my $clip_area = $self->expolygon->offset_ex(+$self->extrusion_width/2);
# we'll now try several directions using a rudimentary visibility check:
# bridge in several directions and then sum the length of lines having both
# endpoints within anchors
# we test angles according to configured resolution
my @angles = map { $_*$self->resolution } 0..(PI/$self->resolution);
# we also test angles of each bridge contour
push @angles, map $_->direction, map @{$_->lines}, @{$self->expolygon};
# we also test angles of each open supporting edge
# (this finds the optimal angle for C-shaped supports)
push @angles,
map Slic3r::Line->new($_->first_point, $_->last_point)->direction,
grep { !$_->first_point->coincides_with($_->last_point) }
@edges;
# remove duplicates
my $min_resolution = PI/180; # 1 degree
# proceed in reverse order so that when we compare first value with last one (-1)
# we remove the greatest one (PI) in case they are parallel (PI, 0)
@angles = reverse sort @angles;
for (my $i = 0; $i <= $#angles; ++$i) {
if (directions_parallel_within($angles[$i], $angles[$i-1], $min_resolution)) {
splice @angles, $i, 1;
--$i;
}
}
my %directions_coverage = (); # angle => score
my %directions_avg_length = (); # angle => score
my $line_increment = $self->extrusion_width;
my %unique_angles = map { $_ => 1 } @angles;
for my $angle (@angles) {
my $my_clip_area = [ map $_->clone, @$clip_area ];
my $my_anchors = [ map $_->clone, @$anchors ];
# rotate everything - the center point doesn't matter
$_->rotate(-$angle, [0,0]) for @$my_clip_area, @$my_anchors;
# generate lines in this direction
my $bounding_box = Slic3r::Geometry::BoundingBox->new_from_points([ map @$_, map @$_, @$my_anchors ]);
my @lines = ();
for (my $y = $bounding_box->y_min; $y <= $bounding_box->y_max; $y+= $line_increment) {
push @lines, Slic3r::Polyline->new(
[$bounding_box->x_min, $y],
[$bounding_box->x_max, $y],
);
}
my @clipped_lines = map Slic3r::Line->new(@$_), @{ intersection_pl(\@lines, [ map @$_, @$my_clip_area ]) };
# remove any line not having both endpoints within anchors
@clipped_lines = grep {
my $line = $_;
(first { $_->contains_point($line->a) } @$my_anchors)
&& (first { $_->contains_point($line->b) } @$my_anchors);
} @clipped_lines;
my @lengths = map $_->length, @clipped_lines;
# sum length of bridged lines
$directions_coverage{$angle} = sum(@lengths) // 0;
### The following produces more correct results in some cases and more broken in others.
### TODO: investigate, as it looks more reliable than line clipping.
###$directions_coverage{$angle} = sum(map $_->area, @{$self->coverage($angle)}) // 0;
# max length of bridged lines
$directions_avg_length{$angle} = @lengths ? (max(@lengths)) : -1;
}
# if no direction produced coverage, then there's no bridge direction
return undef if !defined first { $_ > 0 } values %directions_coverage;
# the best direction is the one causing most lines to be bridged (thus most coverage)
# and shortest max line length
my @sorted_directions = sort {
my $cmp;
my $coverage_diff = $directions_coverage{$a} - $directions_coverage{$b};
if (abs($coverage_diff) < $self->extrusion_width) {
$cmp = $directions_avg_length{$b} <=> $directions_avg_length{$a};
} else {
$cmp = ($coverage_diff > 0) ? 1 : -1;
}
$cmp;
} keys %directions_coverage;
$self->angle($sorted_directions[-1]);
if ($self->angle >= PI) {
$self->angle($self->angle - PI);
}
Slic3r::debugf " Optimal infill angle is %d degrees\n", rad2deg($self->angle);
return $self->angle;
}
sub coverage {
my ($self, $angle) = @_;
if (!defined $angle) {
return [] if !defined($angle = $self->angle);
}
# Clone our expolygon and rotate it so that we work with vertical lines.
my $expolygon = $self->expolygon->clone;
$expolygon->rotate(PI/2 - $angle, [0,0]);
# Outset the bridge expolygon by half the amount we used for detecting anchors;
# we'll use this one to generate our trapezoids and be sure that their vertices
# are inside the anchors and not on their contours leading to false negatives.
my $grown = $expolygon->offset_ex(+$self->extrusion_width/2);
# Compute trapezoids according to a vertical orientation
my $trapezoids = [ map @{$_->get_trapezoids2(PI/2)}, @$grown ];
# get anchors and rotate them too
my $anchors = [ map $_->clone, @{$self->_anchors} ];
$_->rotate(PI/2 - $angle, [0,0]) for @$anchors;
my @covered = (); # polygons
foreach my $trapezoid (@$trapezoids) {
my @polylines = map $_->as_polyline, @{$trapezoid->lines};
my @supported = @{intersection_pl(\@polylines, [map @$_, @$anchors])};
# not nice, we need a more robust non-numeric check
@supported = grep $_->length >= $self->extrusion_width, @supported;
if (@supported >= 2) {
push @covered, $trapezoid;
}
}
# merge trapezoids and rotate them back
my $coverage = union(\@covered);
$_->rotate(-(PI/2 - $angle), [0,0]) for @$coverage;
# intersect trapezoids with actual bridge area to remove extra margins
$coverage = intersection_ex($coverage, [ @{$self->expolygon} ]);
if (0) {
my @lines = map @{$_->lines}, @$trapezoids;
$_->rotate(-(PI/2 - $angle), [0,0]) for @lines;
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"coverage_" . rad2deg($angle) . ".svg",
expolygons => [$self->expolygon],
green_expolygons => $self->_anchors,
red_expolygons => $coverage,
lines => \@lines,
);
}
return $coverage;
}
# this method returns the bridge edges (as polylines) that are not supported
# but would allow the entire bridge area to be bridged with detected angle
# if supported too
sub unsupported_edges {
my ($self, $angle) = @_;
if (!defined $angle) {
return [] if !defined($angle = $self->angle);
}
# get bridge edges (both contour and holes)
my @bridge_edges = map $_->split_at_first_point, @{$self->expolygon};
$_->[0]->translate(1,0) for @bridge_edges; # workaround for Clipper bug, see comments in Slic3r::Polygon::clip_as_polyline()
# get unsupported edges
my $grown_lower = offset([ map @$_, @{$self->lower_slices} ], +$self->extrusion_width);
my $unsupported = diff_pl(
\@bridge_edges,
$grown_lower,
);
# split into individual segments and filter out edges parallel to the bridging angle
# TODO: angle tolerance should probably be based on segment length and flow width,
# so that we build supports whenever there's a chance that at least one or two bridge
# extrusions would be anchored within such length (i.e. a slightly non-parallel bridging
# direction might still benefit from anchors if long enough)
my $angle_tolerance = PI/180*5;
@$unsupported = map $_->as_polyline,
grep !directions_parallel_within($_->direction, $angle, $angle_tolerance),
map @{$_->lines},
@$unsupported;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"unsupported_" . rad2deg($angle) . ".svg",
expolygons => [$self->expolygon],
green_expolygons => $self->_anchors,
red_expolygons => union_ex($grown_lower),
no_arrows => 1,
polylines => \@bridge_edges,
red_polylines => $unsupported,
);
}
return $unsupported;
}
1;

View File

@ -1,556 +0,0 @@
package Slic3r::Layer::Region;
use strict;
use warnings;
use List::Util qw(sum first);
use Slic3r::ExtrusionLoop ':roles';
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(PI A B scale unscale chained_path points_coincide);
use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex
offset offset_ex offset2 offset2_ex union_pt diff intersection
union diff intersection_ppl diff_ppl);
use Slic3r::Surface ':types';
# TODO: lazy
sub infill_area_threshold {
my $self = shift;
return $self->flow(FLOW_ROLE_SOLID_INFILL)->scaled_spacing ** 2;
}
sub id { return $_[0]->layer->id; }
sub slice_z { return $_[0]->layer->slice_z; }
sub print_z { return $_[0]->layer->print_z; }
sub height { return $_[0]->layer->height; }
sub object { return $_[0]->layer->object; }
sub print { return $_[0]->layer->print; }
sub config { return $_[0]->region->config; }
sub merge_slices {
my ($self) = @_;
my $expolygons = union_ex([ map $_->p, @{$self->slices} ]);
$self->slices->clear;
$self->slices->append(Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNAL,
)) for @$expolygons;
}
sub make_perimeters {
my ($self, $slices, $fill_surfaces) = @_;
# other perimeters
my $perimeter_flow = $self->flow(FLOW_ROLE_PERIMETER);
my $mm3_per_mm = $perimeter_flow->mm3_per_mm;
my $pwidth = $perimeter_flow->scaled_width;
my $pspacing = $perimeter_flow->scaled_spacing;
# external perimeters
my $ext_perimeter_flow = $self->flow(FLOW_ROLE_EXTERNAL_PERIMETER);
my $ext_mm3_per_mm = $ext_perimeter_flow->mm3_per_mm;
my $ext_pwidth = $ext_perimeter_flow->scaled_width;
my $ext_pspacing = scale($ext_perimeter_flow->spacing_to($perimeter_flow));
# overhang perimeters
my $overhang_flow = $self->region->flow(FLOW_ROLE_PERIMETER, -1, 1, 0, -1, $self->layer->object);
my $mm3_per_mm_overhang = $overhang_flow->mm3_per_mm;
# solid infill
my $solid_infill_flow = $self->flow(FLOW_ROLE_SOLID_INFILL);
my $ispacing = $solid_infill_flow->scaled_spacing;
my $gap_area_threshold = $pwidth ** 2;
# Calculate the minimum required spacing between two adjacent traces.
# This should be equal to the nominal flow spacing but we experiment
# with some tolerance in order to avoid triggering medial axis when
# some squishing might work. Loops are still spaced by the entire
# flow spacing; this only applies to collapsing parts.
my $min_spacing = $pspacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
my $ext_min_spacing = $ext_pspacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
$self->perimeters->clear;
$self->thin_fills->clear;
my @contours = (); # array of Polygons with ccw orientation
my @holes = (); # array of Polygons with cw orientation
my @thin_walls = (); # array of ExPolygons
# we need to process each island separately because we might have different
# extra perimeters for each one
foreach my $surface (@$slices) {
# detect how many perimeters must be generated for this island
my $loop_number = $self->config->perimeters + ($surface->extra_perimeters || 0);
my @last = @{$surface->expolygon};
my @gaps = (); # array of ExPolygons
if ($loop_number > 0) {
# we loop one time more than needed in order to find gaps after the last perimeter was applied
for my $i (1 .. ($loop_number+1)) { # outer loop is 1
my @offsets = ();
if ($i == 1) {
# the minimum thickness of a single loop is:
# ext_width/2 + ext_spacing/2 + spacing/2 + width/2
if ($self->config->thin_walls) {
@offsets = @{offset2(
\@last,
-(0.5*$ext_pwidth + 0.5*$ext_min_spacing - 1),
+(0.5*$ext_min_spacing - 1),
)};
} else {
@offsets = @{offset(
\@last,
-0.5*$ext_pwidth,
)};
}
# look for thin walls
if ($self->config->thin_walls) {
my $diff = diff_ex(
\@last,
offset(\@offsets, +0.5*$ext_pwidth),
1, # medial axis requires non-overlapping geometry
);
push @thin_walls, @$diff;
}
} else {
my $distance = ($i == 2) ? $ext_pspacing : $pspacing;
if ($self->config->thin_walls) {
@offsets = @{offset2(
\@last,
-($distance + 0.5*$min_spacing - 1),
+(0.5*$min_spacing - 1),
)};
} else {
@offsets = @{offset(
\@last,
-$distance,
)};
}
# look for gaps
if ($self->region->config->gap_fill_speed > 0 && $self->config->fill_density > 0) {
# not using safety offset here would "detect" very narrow gaps
# (but still long enough to escape the area threshold) that gap fill
# won't be able to fill but we'd still remove from infill area
my $diff = diff_ex(
offset(\@last, -0.5*$pspacing),
offset(\@offsets, +0.5*$pspacing + 10), # safety offset
);
push @gaps, grep abs($_->area) >= $gap_area_threshold, @$diff;
}
}
last if !@offsets;
last if $i > $loop_number; # we were only looking for gaps this time
# clone polygons because these ExPolygons will go out of scope very soon
@last = @offsets;
foreach my $polygon (@offsets) {
if ($polygon->is_counter_clockwise) {
push @contours, $polygon;
} else {
push @holes, $polygon;
}
}
}
}
# fill gaps
if (@gaps) {
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"gaps.svg",
expolygons => \@gaps,
);
}
# where $pwidth < thickness < 2*$pspacing, infill with width = 1.5*$pwidth
# where 0.5*$pwidth < thickness < $pwidth, infill with width = 0.5*$pwidth
my @gap_sizes = (
[ $pwidth, 2*$pspacing, unscale 1.5*$pwidth ],
[ 0.5*$pwidth, $pwidth, unscale 0.5*$pwidth ],
);
foreach my $gap_size (@gap_sizes) {
my @gap_fill = $self->_fill_gaps(@$gap_size, \@gaps);
$self->thin_fills->append(@gap_fill);
# Make sure we don't infill narrow parts that are already gap-filled
# (we only consider this surface's gaps to reduce the diff() complexity).
# Growing actual extrusions ensures that gaps not filled by medial axis
# are not subtracted from fill surfaces (they might be too short gaps
# that medial axis skips but infill might join with other infill regions
# and use zigzag).
my $w = $gap_size->[2];
my @filled = map {
@{($_->isa('Slic3r::ExtrusionLoop') ? $_->polygon->split_at_first_point : $_->polyline)
->grow(scale $w/2)};
} @gap_fill;
@last = @{diff(\@last, \@filled)};
}
}
# create one more offset to be used as boundary for fill
# we offset by half the perimeter spacing (to get to the actual infill boundary)
# and then we offset back and forth by half the infill spacing to only consider the
# non-collapsing regions
my $min_perimeter_infill_spacing = $ispacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
$fill_surfaces->append(
map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL), # use a bogus surface type
@{offset2_ex(
[ map @{$_->simplify_p(&Slic3r::SCALED_RESOLUTION)}, @{union_ex(\@last)} ],
-($pspacing/2 + $min_perimeter_infill_spacing/2),
+$min_perimeter_infill_spacing/2,
)}
);
}
# process thin walls by collapsing slices to single passes
my @thin_wall_polylines = ();
if (@thin_walls) {
# the following offset2 ensures almost nothing in @thin_walls is narrower than $min_width
# (actually, something larger than that still may exist due to mitering or other causes)
my $min_width = $pwidth / 4;
@thin_walls = @{offset2_ex([ map @$_, @thin_walls ], -$min_width/2, +$min_width/2)};
# the maximum thickness of our thin wall area is equal to the minimum thickness of a single loop
@thin_wall_polylines = map @{$_->medial_axis($pwidth + $pspacing, $min_width)}, @thin_walls;
Slic3r::debugf " %d thin walls detected\n", scalar(@thin_wall_polylines) if $Slic3r::debug;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"medial_axis.svg",
no_arrows => 1,
expolygons => \@thin_walls,
green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
red_polylines => \@thin_wall_polylines,
);
}
}
# find nesting hierarchies separately for contours and holes
my $contours_pt = union_pt(\@contours);
my $holes_pt = union_pt(\@holes);
# prepare grown lower layer slices for overhang detection
my $lower_slices = Slic3r::ExPolygon::Collection->new;
if ($self->layer->lower_layer && $self->region->config->overhangs) {
# We consider overhang any part where the entire nozzle diameter is not supported by the
# lower layer, so we take lower slices and offset them by half the nozzle diameter used
# in the current layer
my $nozzle_diameter = $self->layer->print->config->get_at('nozzle_diameter', $self->region->config->perimeter_extruder-1);
$lower_slices->append(
@{offset_ex([ map @$_, @{$self->layer->lower_layer->slices} ], scale +$nozzle_diameter/2)},
);
}
my $lower_slices_p = $lower_slices->polygons;
# prepare a coderef for traversing the PolyTree object
# external contours are root items of $contours_pt
# internal contours are the ones next to external
my $traverse;
$traverse = sub {
my ($polynodes, $depth, $is_contour) = @_;
# convert all polynodes to ExtrusionLoop objects
my $collection = Slic3r::ExtrusionPath::Collection->new;
my @children = ();
foreach my $polynode (@$polynodes) {
my $polygon = ($polynode->{outer} // $polynode->{hole})->clone;
my $role = EXTR_ROLE_PERIMETER;
my $loop_role = EXTRL_ROLE_DEFAULT;
my $root_level = $depth == 0;
my $no_children = !@{ $polynode->{children} };
my $is_external = $is_contour ? $root_level : $no_children;
my $is_internal = $is_contour ? $no_children : $root_level;
if ($is_external) {
# external perimeters are root level in case of contours
# and items with no children in case of holes
$role = EXTR_ROLE_EXTERNAL_PERIMETER;
$loop_role = EXTRL_ROLE_EXTERNAL_PERIMETER;
} elsif ($is_contour && $is_internal) {
# internal perimeters are root level in case of holes
# and items with no children in case of contours
$loop_role = EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER;
}
# detect overhanging/bridging perimeters
my @paths = ();
if ($self->region->config->overhangs && $lower_slices->count > 0) {
# get non-overhang paths by intersecting this loop with the grown lower slices
foreach my $polyline (@{ intersection_ppl([ $polygon ], $lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => $role,
mm3_per_mm => ($is_external ? $ext_mm3_per_mm : $mm3_per_mm),
width => ($is_external ? $ext_perimeter_flow->width : $perimeter_flow->width),
height => $self->height,
);
}
# get overhang paths by checking what parts of this loop fall
# outside the grown lower slices (thus where the distance between
# the loop centerline and original lower slices is >= half nozzle diameter
foreach my $polyline (@{ diff_ppl([ $polygon ], $lower_slices_p) }) {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => EXTR_ROLE_OVERHANG_PERIMETER,
mm3_per_mm => $mm3_per_mm_overhang,
width => $overhang_flow->width,
height => $self->height,
);
}
# reapply the nearest point search for starting point
# (clone because the collection gets DESTROY'ed)
# We allow polyline reversal because Clipper may have randomly
# reversed polylines during clipping.
my $collection = Slic3r::ExtrusionPath::Collection->new(@paths);
@paths = map $_->clone, @{$collection->chained_path(0)};
} else {
push @paths, Slic3r::ExtrusionPath->new(
polyline => $polygon->split_at_first_point,
role => $role,
mm3_per_mm => $mm3_per_mm,
width => $perimeter_flow->width,
height => $self->height,
);
}
my $loop = Slic3r::ExtrusionLoop->new_from_paths(@paths);
$loop->role($loop_role);
# return ccw contours and cw holes
# GCode.pm will convert all of them to ccw, but it needs to know
# what the holes are in order to compute the correct inwards move
# We do this on the final Loop object instead of the polygon because
# overhang clipping might have reversed its order since Clipper does
# not preserve polyline orientation.
if ($is_contour) {
$loop->make_counter_clockwise;
} else {
$loop->make_clockwise;
}
$collection->append($loop);
# save the children
push @children, $polynode->{children};
}
# if we're handling the top-level contours, add thin walls as candidates too
# in order to include them in the nearest-neighbor search
if ($is_contour && $depth == 0) {
foreach my $polyline (@thin_wall_polylines) {
$collection->append(Slic3r::ExtrusionPath->new(
polyline => $polyline,
role => EXTR_ROLE_EXTERNAL_PERIMETER,
mm3_per_mm => $mm3_per_mm,
width => $perimeter_flow->width,
height => $self->height,
));
}
}
# use a nearest neighbor search to order these children
# TODO: supply second argument to chained_path() too?
# Optimization: since islands are going to be sorted by slice anyway in the
# G-code export process, we skip chained_path here
my ($sorted_collection, @orig_indices);
if ($is_contour && $depth == 0) {
$sorted_collection = $collection;
@orig_indices = (0..$#$sorted_collection);
} else {
$sorted_collection = $collection->chained_path_indices(0);
@orig_indices = @{$sorted_collection->orig_indices};
}
my @loops = ();
foreach my $loop (@$sorted_collection) {
my $orig_index = shift @orig_indices;
if ($loop->isa('Slic3r::ExtrusionPath')) {
push @loops, $loop->clone;
} else {
# if this is an external contour find all holes belonging to this contour(s)
# and prepend them
if ($is_contour && $depth == 0) {
# $loop is the outermost loop of an island
my @holes = ();
for (my $i = 0; $i <= $#$holes_pt; $i++) {
if ($loop->polygon->contains_point($holes_pt->[$i]{outer}->first_point)) {
push @holes, splice @$holes_pt, $i, 1; # remove from candidates to reduce complexity
$i--;
}
}
# order holes efficiently
@holes = @holes[@{chained_path([ map {($_->{outer} // $_->{hole})->first_point} @holes ])}];
push @loops, reverse map $traverse->([$_], 0, 0), @holes;
}
# traverse children and prepend them to this loop
push @loops, $traverse->($children[$orig_index], $depth+1, $is_contour);
push @loops, $loop->clone;
}
}
return @loops;
};
# order loops from inner to outer (in terms of object slices)
my @loops = $traverse->($contours_pt, 0, 1);
# if brim will be printed, reverse the order of perimeters so that
# we continue inwards after having finished the brim
# TODO: add test for perimeter order
@loops = reverse @loops
if $self->region->config->external_perimeters_first
|| ($self->layer->id == 0 && $self->print->config->brim_width > 0);
# append perimeters
$self->perimeters->append(@loops);
}
sub _fill_gaps {
my ($self, $min, $max, $w, $gaps) = @_;
my $this = diff_ex(
offset2([ map @$_, @$gaps ], -$min/2, +$min/2),
offset2([ map @$_, @$gaps ], -$max/2, +$max/2),
1,
);
my $flow = $self->flow(FLOW_ROLE_SOLID_INFILL, 0, $w);
my %path_args = (
role => EXTR_ROLE_GAPFILL,
mm3_per_mm => $flow->mm3_per_mm,
width => $flow->width,
height => $self->height,
);
my @polylines = map @{$_->medial_axis($max, $min/2)}, @$this;
Slic3r::debugf " %d gaps filled with extrusion width = %s\n", scalar @$this, $w
if @$this;
for my $i (0..$#polylines) {
if ($polylines[$i]->isa('Slic3r::Polygon')) {
my $loop = Slic3r::ExtrusionLoop->new;
$loop->append(Slic3r::ExtrusionPath->new(polyline => $polylines[$i]->split_at_first_point, %path_args));
$polylines[$i] = $loop;
} elsif ($polylines[$i]->is_valid && $polylines[$i]->first_point->coincides_with($polylines[$i]->last_point)) {
# since medial_axis() now returns only Polyline objects, detect loops here
my $loop = Slic3r::ExtrusionLoop->new;
$loop->append(Slic3r::ExtrusionPath->new(polyline => $polylines[$i], %path_args));
$polylines[$i] = $loop;
} else {
$polylines[$i] = Slic3r::ExtrusionPath->new(polyline => $polylines[$i], %path_args);
}
}
return @polylines;
}
sub prepare_fill_surfaces {
my $self = shift;
# Note: in order to make the psPrepareInfill step idempotent, we should never
# alter fill_surfaces boundaries on which our idempotency relies since that's
# the only meaningful information returned by psPerimeters.
# if no solid layers are requested, turn top/bottom surfaces to internal
if ($self->config->top_solid_layers == 0) {
$_->surface_type(S_TYPE_INTERNAL) for @{$self->fill_surfaces->filter_by_type(S_TYPE_TOP)};
}
if ($self->config->bottom_solid_layers == 0) {
$_->surface_type(S_TYPE_INTERNAL)
for @{$self->fill_surfaces->filter_by_type(S_TYPE_BOTTOM)}, @{$self->fill_surfaces->filter_by_type(S_TYPE_BOTTOMBRIDGE)};
}
# turn too small internal regions into solid regions according to the user setting
if ($self->config->fill_density > 0) {
my $min_area = scale scale $self->config->solid_infill_below_area; # scaling an area requires two calls!
$_->surface_type(S_TYPE_INTERNALSOLID)
for grep { $_->area <= $min_area } @{$self->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
}
}
sub process_external_surfaces {
my ($self, $lower_layer) = @_;
my @surfaces = @{$self->fill_surfaces};
my $margin = scale &Slic3r::EXTERNAL_INFILL_MARGIN;
my @bottom = ();
foreach my $surface (grep $_->is_bottom, @surfaces) {
my $grown = $surface->expolygon->offset_ex(+$margin);
# detect bridge direction before merging grown surfaces otherwise adjacent bridges
# would get merged into a single one while they need different directions
# also, supply the original expolygon instead of the grown one, because in case
# of very thin (but still working) anchors, the grown expolygon would go beyond them
my $angle;
if ($lower_layer) {
my $bridge_detector = Slic3r::Layer::BridgeDetector->new(
expolygon => $surface->expolygon,
lower_slices => $lower_layer->slices,
extrusion_width => $self->flow(FLOW_ROLE_INFILL, $self->height, 1)->scaled_width,
);
Slic3r::debugf "Processing bridge at layer %d:\n", $self->id;
$angle = $bridge_detector->detect_angle;
if (defined $angle && $self->object->config->support_material) {
$self->bridged->append(@{ $bridge_detector->coverage($angle) });
$self->unsupported_bridge_edges->append(@{ $bridge_detector->unsupported_edges });
}
}
push @bottom, map $surface->clone(expolygon => $_, bridge_angle => $angle), @$grown;
}
my @top = ();
foreach my $surface (grep $_->surface_type == S_TYPE_TOP, @surfaces) {
# give priority to bottom surfaces
my $grown = diff_ex(
$surface->expolygon->offset(+$margin),
[ map $_->p, @bottom ],
);
push @top, map $surface->clone(expolygon => $_), @$grown;
}
# if we're slicing with no infill, we can't extend external surfaces
# over non-existent infill
my @fill_boundaries = $self->config->fill_density > 0
? @surfaces
: grep $_->surface_type != S_TYPE_INTERNAL, @surfaces;
# intersect the grown surfaces with the actual fill boundaries
my @new_surfaces = ();
foreach my $group (@{Slic3r::Surface::Collection->new(@top, @bottom)->group}) {
push @new_surfaces,
map $group->[0]->clone(expolygon => $_),
@{intersection_ex(
[ map $_->p, @$group ],
[ map $_->p, @fill_boundaries ],
1, # to ensure adjacent expolygons are unified
)};
}
# subtract the new top surfaces from the other non-top surfaces and re-add them
my @other = grep $_->surface_type != S_TYPE_TOP && !$_->is_bottom, @surfaces;
foreach my $group (@{Slic3r::Surface::Collection->new(@other)->group}) {
push @new_surfaces, map $group->[0]->clone(expolygon => $_), @{diff_ex(
[ map $_->p, @$group ],
[ map $_->p, @new_surfaces ],
)};
}
$self->fill_surfaces->clear;
$self->fill_surfaces->append(@new_surfaces);
}
1;

View File

@ -5,8 +5,6 @@ use warnings;
# a line is a two-points line
use parent 'Slic3r::Polyline';
use Slic3r::Geometry qw(A B X Y);
sub intersection {
my $self = shift;
my ($line, $require_crossing) = @_;

View File

@ -1,6 +1,6 @@
package Slic3r::Model;
use List::Util qw(first max);
use List::Util qw(first max any);
use Slic3r::Geometry qw(X Y Z move_points);
sub read_from_file {
@ -12,6 +12,9 @@ sub read_from_file {
: $input_file =~ /\.amf(\.xml)?$/i ? Slic3r::Format::AMF->read_file($input_file)
: die "Input file must have .stl, .obj or .amf(.xml) extension\n";
die "The supplied file couldn't be read because it's empty.\n"
if $model->objects_count == 0;
$_->set_input_file($input_file) for @{$model->objects};
return $model;
}
@ -64,226 +67,36 @@ sub set_material {
return $material;
}
sub duplicate_objects_grid {
my ($self, $grid, $distance) = @_;
die "Grid duplication is not supported with multiple objects\n"
if @{$self->objects} > 1;
my $object = $self->objects->[0];
$object->clear_instances;
my $size = $object->bounding_box->size;
for my $x_copy (1..$grid->[X]) {
for my $y_copy (1..$grid->[Y]) {
$object->add_instance(
offset => Slic3r::Pointf->new(
($size->[X] + $distance) * ($x_copy-1),
($size->[Y] + $distance) * ($y_copy-1),
),
);
}
}
}
# this will append more instances to each object
# and then automatically rearrange everything
sub duplicate_objects {
my ($self, $copies_num, $distance, $bb) = @_;
foreach my $object (@{$self->objects}) {
my @instances = @{$object->instances};
foreach my $instance (@instances) {
$object->add_instance($instance) for 2..$copies_num;
}
}
$self->arrange_objects($distance, $bb);
}
# arrange objects preserving their instance count
# but altering their instance positions
sub arrange_objects {
my ($self, $distance, $bb) = @_;
# get the (transformed) size of each instance so that we take
# into account their different transformations when packing
my @instance_sizes = ();
foreach my $object (@{$self->objects}) {
push @instance_sizes, map $object->instance_bounding_box($_)->size, 0..$#{$object->instances};
}
my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
foreach my $object (@{$self->objects}) {
$_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
$object->update_bounding_box;
}
}
# duplicate the entire model preserving instance relative positions
sub duplicate {
my ($self, $copies_num, $distance, $bb) = @_;
my $model_size = $self->bounding_box->size;
my @positions = $self->_arrange([ map $model_size, 2..$copies_num ], $distance, $bb);
# note that this will leave the object count unaltered
foreach my $object (@{$self->objects}) {
my @instances = @{$object->instances}; # store separately to avoid recursion from add_instance() below
foreach my $instance (@instances) {
foreach my $pos (@positions) {
$object->add_instance(
offset => Slic3r::Pointf->new($instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y]),
rotation => $instance->rotation,
scaling_factor => $instance->scaling_factor,
);
}
}
$object->update_bounding_box;
}
}
sub _arrange {
my ($self, $sizes, $distance, $bb) = @_;
# we supply unscaled data to arrange()
return Slic3r::Geometry::arrange(
scalar(@$sizes), # number of parts
max(map $_->x, @$sizes), # cell width
max(map $_->y, @$sizes), # cell height ,
$distance, # distance between cells
$bb, # bounding box of the area to fill (can be undef)
);
}
# this returns the bounding box of the *transformed* instances
sub bounding_box {
my $self = shift;
return undef if !@{$self->objects};
my $bb = $self->objects->[0]->bounding_box;
$bb->merge($_->bounding_box) for @{$self->objects}[1..$#{$self->objects}];
return $bb;
}
# input point is expressed in unscaled coordinates
sub center_instances_around_point {
my ($self, $point) = @_;
my $bb = $self->bounding_box;
return if !defined $bb;
my $size = $bb->size;
my @shift = (
-$bb->x_min + $point->[X] - $size->x/2,
-$bb->y_min + $point->[Y] - $size->y/2, #//
);
foreach my $object (@{$self->objects}) {
foreach my $instance (@{$object->instances}) {
$instance->set_offset(Slic3r::Pointf->new(
$instance->offset->x + $shift[X],
$instance->offset->y + $shift[Y], #++
));
}
$object->update_bounding_box;
}
}
sub align_instances_to_origin {
my ($self) = @_;
my $bb = $self->bounding_box;
return if !defined $bb;
my $new_center = $bb->size;
$new_center->translate(-$new_center->x/2, -$new_center->y/2); #//
$self->center_instances_around_point($new_center);
}
sub translate {
my $self = shift;
my @shift = @_;
$_->translate(@shift) for @{$self->objects};
}
# flattens everything to a single mesh
sub mesh {
my $self = shift;
my $mesh = Slic3r::TriangleMesh->new;
$mesh->merge($_->mesh) for @{$self->objects};
return $mesh;
}
# flattens everything to a single mesh
sub raw_mesh {
my $self = shift;
my $mesh = Slic3r::TriangleMesh->new;
$mesh->merge($_->raw_mesh) for @{$self->objects};
return $mesh;
}
# this method splits objects into multiple distinct objects by walking their meshes
sub split_meshes {
my $self = shift;
my @objects = @{$self->objects};
@{$self->objects} = ();
foreach my $object (@objects) {
if (@{$object->volumes} > 1) {
# We can't split meshes if there's more than one material, because
# we can't group the resulting meshes by object afterwards
$self->_add_object($object);
next;
}
my $volume = $object->volumes->[0];
foreach my $mesh (@{$volume->mesh->split}) {
my $new_object = $self->add_object(
input_file => $object->input_file,
config => $object->config->clone,
layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned
origin_translation => $object->origin_translation,
);
$new_object->add_volume(
mesh => $mesh,
name => $volume->name,
material_id => $volume->material_id,
config => $volume->config,
);
# add one instance per original instance
$new_object->add_instance(
offset => Slic3r::Pointf->new(@{$_->offset}),
rotation => $_->rotation,
scaling_factor => $_->scaling_factor,
) for @{ $object->instances // [] };
}
}
}
sub print_info {
my $self = shift;
$_->print_info for @{$self->objects};
}
sub get_material_name {
my $self = shift;
my ($material_id) = @_;
sub looks_like_multipart_object {
my ($self) = @_;
my $name;
if ($self->has_material($material_id)) {
$name //= $self->get_material($material_id)
->attributes->{$_} for qw(Name name);
return 0 if $self->objects_count == 1;
return 0 if any { $_->volumes_count > 1 } @{$self->objects};
return 0 if any { @{$_->config->get_keys} > 1 } @{$self->objects};
my %heights = map { $_ => 1 } map $_->mesh->bounding_box->z_min, map @{$_->volumes}, @{$self->objects};
return scalar(keys %heights) > 1;
}
sub convert_multipart_object {
my ($self) = @_;
my @objects = @{$self->objects};
my $object = $self->add_object(
input_file => $objects[0]->input_file,
);
foreach my $v (map @{$_->volumes}, @objects) {
my $volume = $object->add_volume($v);
$volume->set_name($v->object->name);
}
$name //= $material_id;
return $name;
$object->add_instance($_) for map @{$_->instances}, @objects;
$self->delete_object($_) for reverse 0..($self->objects_count-2);
}
package Slic3r::Model::Material;
@ -369,157 +182,6 @@ sub add_instance {
}
}
sub raw_bounding_box {
my $self = shift;
my @meshes = map $_->mesh->clone, grep !$_->modifier, @{ $self->volumes };
die "No meshes found" if !@meshes;
my $instance = $self->instances->[0];
$instance->transform_mesh($_, 1) for @meshes;
my $bb = (shift @meshes)->bounding_box;
$bb->merge($_->bounding_box) for @meshes;
return $bb;
}
# flattens all volumes and instances into a single mesh
sub mesh {
my $self = shift;
my $mesh = $self->raw_mesh;
my @instance_meshes = ();
foreach my $instance (@{ $self->instances }) {
my $m = $mesh->clone;
$instance->transform_mesh($m);
push @instance_meshes, $m;
}
my $full_mesh = Slic3r::TriangleMesh->new;
$full_mesh->merge($_) for @instance_meshes;
return $full_mesh;
}
sub update_bounding_box {
my ($self) = @_;
$self->_bounding_box($self->mesh->bounding_box);
}
# this returns the bounding box of the *transformed* instances
sub bounding_box {
my $self = shift;
$self->update_bounding_box if !defined $self->_bounding_box;
return $self->_bounding_box->clone;
}
# this returns the bounding box of the *transformed* given instance
sub instance_bounding_box {
my ($self, $instance_idx) = @_;
$instance_idx //= 0;
my $mesh = $self->raw_mesh;
$self->instances->[$instance_idx]->transform_mesh($mesh);
return $mesh->bounding_box;
}
sub center_around_origin {
my $self = shift;
# calculate the displacements needed to
# center this object around the origin
my $bb = $self->raw_mesh->bounding_box;
# first align to origin on XY
my @shift = (
-$bb->x_min,
-$bb->y_min,
0,
);
# then center it on XY
my $size = $bb->size;
$shift[X] -= $size->x/2;
$shift[Y] -= $size->y/2; #//
$self->translate(@shift);
$self->origin_translation->translate(@shift[X,Y]);
if ($self->instances_count > 0) {
foreach my $instance (@{ $self->instances }) {
$instance->set_offset(Slic3r::Pointf->new(
$instance->offset->x - $shift[X],
$instance->offset->y - $shift[Y], #--
));
}
$self->update_bounding_box;
}
return @shift;
}
sub translate {
my $self = shift;
my @shift = @_;
$_->mesh->translate(@shift) for @{$self->volumes};
$self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
}
sub rotate {
my ($self, $angle, $axis) = @_;
# we accept angle in radians but mesh currently uses degrees
$angle = rad2deg($angle);
if ($axis == X) {
$_->mesh->rotate_x($angle) for @{$self->volumes};
} elsif ($axis == Y) {
$_->mesh->rotate_y($angle) for @{$self->volumes};
} elsif ($axis == Z) {
$_->mesh->rotate_z($angle) for @{$self->volumes};
}
$self->invalidate_bounding_box;
}
sub flip {
my ($self, $axis) = @_;
if ($axis == X) {
$_->mesh->flip_x for @{$self->volumes};
} elsif ($axis == Y) {
$_->mesh->flip_y for @{$self->volumes};
} elsif ($axis == Z) {
$_->mesh->flip_z for @{$self->volumes};
}
$self->invalidate_bounding_box;
}
sub scale_xyz {
my ($self, $versor) = @_;
$_->mesh->scale_xyz($versor) for @{$self->volumes};
$self->invalidate_bounding_box;
}
sub materials_count {
my $self = shift;
my %materials = map { $_->material_id // '_default' => 1 } @{$self->volumes};
return scalar keys %materials;
}
sub unique_materials {
my $self = shift;
my %materials = ();
$materials{ $_->material_id } = 1
for grep { defined $_->material_id } @{$self->volumes};
return sort keys %materials;
}
sub mesh_stats {
my $self = shift;
@ -552,54 +214,4 @@ sub print_info {
}
}
sub cut {
my ($self, $z) = @_;
# clone this one to duplicate instances, materials etc.
my $model = Slic3r::Model->new;
my $upper = $model->add_object($self);
my $lower = $model->add_object($self);
$upper->clear_volumes;
$lower->clear_volumes;
foreach my $volume (@{$self->volumes}) {
if ($volume->modifier) {
# don't cut modifiers
$upper->add_volume($volume);
$lower->add_volume($volume);
} else {
my $upper_mesh = Slic3r::TriangleMesh->new;
my $lower_mesh = Slic3r::TriangleMesh->new;
$volume->mesh->cut($z + $volume->mesh->bounding_box->z_min, $upper_mesh, $lower_mesh);
$upper_mesh->repair;
$lower_mesh->repair;
$upper_mesh->reset_repair_stats;
$lower_mesh->reset_repair_stats;
if ($upper_mesh->facets_count > 0) {
$upper->add_volume(
name => $volume->name,
material_id => $volume->material_id,
mesh => $upper_mesh,
modifier => $volume->modifier,
config => $volume->config,
);
}
if ($lower_mesh->facets_count > 0) {
$lower->add_volume(
name => $volume->name,
material_id => $volume->material_id,
mesh => $lower_mesh,
modifier => $volume->modifier,
config => $volume->config,
);
}
}
}
$upper = undef if !@{$upper->volumes};
$lower = undef if !@{$lower->volumes};
return ($model, $upper, $lower);
}
1;

View File

@ -7,4 +7,27 @@ sub new_scale {
return $class->new(map Slic3r::Geometry::scale($_), @_);
}
sub dump_perl {
my $self = shift;
return sprintf "[%s,%s]", @$self;
}
package Slic3r::Pointf;
use strict;
use warnings;
sub new_unscale {
my $class = shift;
return $class->new(map Slic3r::Geometry::unscale($_), @_);
}
package Slic3r::Pointf3;
use strict;
use warnings;
sub new_unscale {
my $class = shift;
return $class->new(map Slic3r::Geometry::unscale($_), @_);
}
1;

View File

@ -5,20 +5,7 @@ use warnings;
# a polygon is a closed polyline.
use parent 'Slic3r::Polyline';
use Slic3r::Geometry qw(
polygon_segment_having_point
PI X1 X2 Y1 Y2 epsilon scaled_epsilon);
use Slic3r::Geometry::Clipper qw(intersection_pl);
sub wkt {
my $self = shift;
return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self;
}
sub dump_perl {
my $self = shift;
return sprintf "[%s]", join ',', map "[$_->[0],$_->[1]]", @$self;
}
use Slic3r::Geometry qw(PI);
sub grow {
my $self = shift;
@ -45,51 +32,4 @@ sub subdivide {
return Slic3r::Polygon->new(@new_points);
}
sub concave_points {
my ($self, $angle) = @_;
$angle //= PI;
# input angle threshold is checked on the internal side of the polygon
# but angle3points measures CCW angle, so we calculate the complementary angle
my $ccw_angle = 2*PI-$angle;
my @concave = ();
my @points = @$self;
my @points_pp = @{$self->pp};
for my $i (-1 .. ($#points-1)) {
# angle is measured in ccw orientation
my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
if ($vertex_angle <= $ccw_angle) {
push @concave, $points[$i];
}
}
return [@concave];
}
sub convex_points {
my ($self, $angle) = @_;
$angle //= PI;
# input angle threshold is checked on the internal side of the polygon
# but angle3points measures CCW angle, so we calculate the complementary angle
my $ccw_angle = 2*PI-$angle;
my @convex = ();
my @points = @$self;
my @points_pp = @{$self->pp};
for my $i (-1 .. ($#points-1)) {
# angle is measured in ccw orientation
my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
if ($vertex_angle >= $ccw_angle) {
push @convex, $points[$i];
}
}
return [@convex];
}
1;

View File

@ -2,9 +2,7 @@ package Slic3r::Polyline;
use strict;
use warnings;
use List::Util qw(first);
use Slic3r::Geometry qw(X Y PI epsilon);
use Slic3r::Geometry::Clipper qw(JT_SQUARE);
use Slic3r::Geometry qw(X Y);
sub new_scale {
my $class = shift;
@ -12,29 +10,9 @@ sub new_scale {
return $class->new(map [ Slic3r::Geometry::scale($_->[X]), Slic3r::Geometry::scale($_->[Y]) ], @points);
}
sub wkt {
sub dump_perl {
my $self = shift;
return sprintf "LINESTRING((%s))", join ',', map "$_->[0] $_->[1]", @$self;
}
sub bounding_box {
my $self = shift;
return Slic3r::Geometry::BoundingBox->new_from_points([ @$self ]);
}
sub size {
my $self = shift;
return [ Slic3r::Geometry::size_2D($self) ];
}
sub is_straight {
my ($self) = @_;
# Check that each segment's direction is equal to the line connecting
# first point and last point. (Checking each line against the previous
# one would have caused the error to accumulate.)
my $dir = Slic3r::Line->new($self->first_point, $self->last_point)->direction;
return !defined first { !$_->parallel_to($dir) } @{$self->lines};
return sprintf "[%s]", join ',', map "[$_->[0],$_->[1]]", @$self;
}
1;

View File

@ -5,26 +5,16 @@ use warnings;
use File::Basename qw(basename fileparse);
use File::Spec;
use List::Util qw(min max first sum);
use Slic3r::ExtrusionLoop ':roles';
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN MAX PI scale unscale move_points chained_path
convex_hull);
use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex intersection offset
use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN MAX PI scale unscale convex_hull);
use Slic3r::Geometry::Clipper qw(diff_ex union_ex intersection_ex intersection offset
offset2 union union_pt_chained JT_ROUND JT_SQUARE);
use Slic3r::Print::State ':steps';
our $status_cb;
sub new {
# TODO: port PlaceholderParser methods to C++, then its own constructor
# can call them and no need for this new() method at all
my ($class) = @_;
my $self = $class->_new;
$self->placeholder_parser->apply_env_variables;
$self->placeholder_parser->update_timestamp;
return $self;
}
sub set_status_cb {
my ($class, $cb) = @_;
$status_cb = $cb;
@ -34,276 +24,6 @@ sub status_cb {
return $status_cb // sub {};
}
sub apply_config {
my ($self, $config) = @_;
$config = $config->clone;
$config->normalize;
# apply variables to placeholder parser
$self->placeholder_parser->apply_config($config);
my $invalidated = 0;
# handle changes to print config
my $print_diff = $self->config->diff($config);
if (@$print_diff) {
$self->config->apply_dynamic($config);
$invalidated = 1
if $self->invalidate_state_by_config_options($print_diff);
}
# handle changes to object config defaults
$self->default_object_config->apply_dynamic($config);
foreach my $object (@{$self->objects}) {
# we don't assume that $config contains a full ObjectConfig,
# so we base it on the current print-wise default
my $new = $self->default_object_config->clone;
$new->apply_dynamic($config);
# we override the new config with object-specific options
my $model_object_config = $object->model_object->config->clone;
$model_object_config->normalize;
$new->apply_dynamic($model_object_config);
# check whether the new config is different from the current one
my $diff = $object->config->diff($new);
if (@$diff) {
$object->config->apply($new);
$invalidated = 1
if $object->invalidate_state_by_config_options($diff);
}
}
# handle changes to regions config defaults
$self->default_region_config->apply_dynamic($config);
# All regions now have distinct settings.
# Check whether applying the new region config defaults we'd get different regions.
my $rearrange_regions = 0;
my @other_region_configs = ();
REGION: foreach my $region_id (0..($self->region_count - 1)) {
my $region = $self->regions->[$region_id];
my @this_region_configs = ();
foreach my $object (@{$self->objects}) {
foreach my $volume_id (@{ $object->get_region_volumes($region_id) }) {
my $volume = $object->model_object->volumes->[$volume_id];
my $new = $self->default_region_config->clone;
{
my $model_object_config = $object->model_object->config->clone;
$model_object_config->normalize;
$new->apply_dynamic($model_object_config);
}
if ($volume->material_id ne '') {
my $material_config = $object->model_object->model->get_material($volume->material_id)->config->clone;
$material_config->normalize;
$new->apply_dynamic($material_config);
}
if (defined first { !$_->equals($new) } @this_region_configs) {
# if the new config for this volume differs from the other
# volume configs currently associated to this region, it means
# the region subdivision does not make sense anymore
$rearrange_regions = 1;
last REGION;
}
push @this_region_configs, $new;
if (defined first { $_->equals($new) } @other_region_configs) {
# if the new config for this volume equals any of the other
# volume configs that are not currently associated to this
# region, it means the region subdivision does not make
# sense anymore
$rearrange_regions = 1;
last REGION;
}
# if we're here and the new region config is different from the old
# one, we need to apply the new config and invalidate all objects
# (possible optimization: only invalidate objects using this region)
my $region_config_diff = $region->config->diff($new);
if (@$region_config_diff) {
$region->config->apply($new);
foreach my $o (@{$self->objects}) {
$invalidated = 1
if $o->invalidate_state_by_config_options($region_config_diff);
}
}
}
}
push @other_region_configs, @this_region_configs;
}
if ($rearrange_regions) {
# the current subdivision of regions does not make sense anymore.
# we need to remove all objects and re-add them
my @model_objects = map $_->model_object, @{$self->objects};
$self->clear_objects;
$self->add_model_object($_) for @model_objects;
$invalidated = 1;
}
return $invalidated;
}
# caller is responsible for supplying models whose objects don't collide
# and have explicit instance positions
sub add_model_object {
my $self = shift;
my ($object, $obj_idx) = @_;
my $object_config = $object->config->clone;
$object_config->normalize;
# initialize print object and store it at the given position
my $o;
if (defined $obj_idx) {
$o = $self->set_new_object($obj_idx, $object, $object->raw_bounding_box);
} else {
$o = $self->add_object($object, $object->raw_bounding_box);
}
$o->set_copies([ map Slic3r::Point->new_scale(@{ $_->offset }), @{ $object->instances } ]);
$o->set_layer_height_ranges($object->layer_height_ranges);
# TODO: translate _trigger_copies to C++, then this can be done by
# PrintObject constructor
$o->_trigger_copies;
foreach my $volume_id (0..$#{$object->volumes}) {
my $volume = $object->volumes->[$volume_id];
# get the config applied to this volume: start from our global defaults
my $config = Slic3r::Config::PrintRegion->new;
$config->apply($self->default_region_config);
# override the defaults with per-object config and then with per-material and per-volume configs
$config->apply_dynamic($object_config);
$config->apply_dynamic($volume->config);
if ($volume->material_id ne '') {
my $material_config = $volume->material->config->clone;
$material_config->normalize;
$config->apply_dynamic($material_config);
}
# find an existing print region with the same config
my $region_id;
foreach my $i (0..($self->region_count - 1)) {
my $region = $self->regions->[$i];
if ($config->equals($region->config)) {
$region_id = $i;
last;
}
}
# if no region exists with the same config, create a new one
if (!defined $region_id) {
my $r = $self->add_region();
$r->config->apply($config);
$region_id = $self->region_count - 1;
}
# assign volume to region
$o->add_region_volume($region_id, $volume_id);
}
# apply config to print object
$o->config->apply($self->default_object_config);
$o->config->apply_dynamic($object_config);
}
sub reload_object {
my ($self, $obj_idx) = @_;
# TODO: this method should check whether the per-object config and per-material configs
# have changed in such a way that regions need to be rearranged or we can just apply
# the diff and invalidate something. Same logic as apply_config()
# For now we just re-add all objects since we haven't implemented this incremental logic yet.
# This should also check whether object volumes (parts) have changed.
my @models_objects = map $_->model_object, @{$self->objects};
$self->clear_objects;
$self->add_model_object($_) for @models_objects;
}
sub validate {
my $self = shift;
if ($self->config->complete_objects) {
# check horizontal clearance
{
my @a = ();
foreach my $object (@{$self->objects}) {
# get convex hulls of all meshes assigned to this print object
my @mesh_convex_hulls = map $object->model_object->volumes->[$_]->mesh->convex_hull,
map @$_,
grep defined $_,
@{$object->region_volumes};
# make a single convex hull for all of them
my $convex_hull = convex_hull([ map @$_, @mesh_convex_hulls ]);
# apply the same transformations we apply to the actual meshes when slicing them
$object->model_object->instances->[0]->transform_polygon($convex_hull);
# align object to Z = 0 and apply XY shift
$convex_hull->translate(@{$object->_copies_shift});
# grow convex hull with the clearance margin
($convex_hull) = @{offset([$convex_hull], scale $self->config->extruder_clearance_radius / 2, 1, JT_ROUND, scale(0.1))};
# now we need that no instance of $convex_hull does not intersect any of the previously checked object instances
for my $copy (@{$object->_shifted_copies}) {
my $p = $convex_hull->clone;
$p->translate(@$copy);
if (@{ intersection(\@a, [$p]) }) {
die "Some objects are too close; your extruder will collide with them.\n";
}
@a = @{union([@a, $p])};
}
}
}
# check vertical clearance
{
my @object_height = ();
foreach my $object (@{$self->objects}) {
my $height = $object->size->z;
push @object_height, $height for @{$object->copies};
}
@object_height = sort { $a <=> $b } @object_height;
# ignore the tallest *copy* (this is why we repeat height for all of them):
# it will be printed as last one so its height doesn't matter
pop @object_height;
if (@object_height && max(@object_height) > scale $self->config->extruder_clearance_height) {
die "Some objects are too tall and cannot be printed without extruder collisions.\n";
}
}
}
if ($self->config->spiral_vase) {
if ((map @{$_->copies}, @{$self->objects}) > 1) {
die "The Spiral Vase option can only be used when printing a single object.\n";
}
if (@{$self->regions} > 1) {
die "The Spiral Vase option can only be used when printing single material objects.\n";
}
}
{
my $max_layer_height = max(
map { $_->config->layer_height, $_->config->get_value('first_layer_height') } @{$self->objects},
);
my $extruders = $self->extruders;
die "Layer height can't be greater than nozzle diameter\n"
if grep { $max_layer_height > $self->config->get_at('nozzle_diameter', $_) } @$extruders;
}
}
# this value is not supposed to be compared with $layer->id
# since they have different semantics
sub total_layer_count {
@ -311,46 +31,6 @@ sub total_layer_count {
return max(map $_->total_layer_count, @{$self->objects});
}
# the bounding box of objects placed in copies position
# (without taking skirt/brim/support material into account)
sub bounding_box {
my $self = shift;
my @points = ();
foreach my $object (@{$self->objects}) {
foreach my $copy (@{$object->_shifted_copies}) {
push @points,
[ $copy->[X], $copy->[Y] ],
[ $copy->[X] + $object->size->[X], $copy->[Y] + $object->size->[Y] ];
}
}
return Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_), @points ]);
}
# the total bounding box of extrusions, including skirt/brim/support material
sub total_bounding_box {
my ($self) = @_;
# get objects bounding box
my $bb = $self->bounding_box;
# check how much we need to increase it
my $extra = 0;
if ($self->has_support_material) {
$extra = &Slic3r::Print::SupportMaterial::MARGIN;
}
$extra = max($extra, $self->config->brim_width);
if ($self->config->skirts > 0) {
my $skirt_flow = $self->skirt_flow;
$extra = max($extra, $self->config->brim_width + $self->config->skirt_distance + ($self->config->skirts * $skirt_flow->spacing));
}
if ($extra > 0) {
$bb->offset(scale $extra);
}
return $bb;
}
sub size {
my $self = shift;
return $self->bounding_box->size;
@ -398,9 +78,13 @@ sub export_gcode {
if (@{$self->config->post_process}) {
$self->status_cb->(95, "Running post-processing scripts");
$self->config->setenv;
for (@{$self->config->post_process}) {
Slic3r::debugf " '%s' '%s'\n", $_, $output_file;
system($_, $output_file);
for my $script (@{$self->config->post_process}) {
Slic3r::debugf " '%s' '%s'\n", $script, $output_file;
# -x doesn't return true on Windows except for .exe files
if (($^O eq 'MSWin32') ? !(-e $script) : !(-x $script)) {
die "The configured post-processing script is not executable: check permissions. ($script)\n";
}
system($script, $output_file);
}
}
}
@ -409,9 +93,6 @@ sub export_svg {
my $self = shift;
my %params = @_;
# is this needed?
$self->init_extruders;
$_->slice for @{$self->objects};
my $fh = $params{output_fh};
@ -422,7 +103,8 @@ sub export_svg {
print "Exporting to $output_file..." unless $params{quiet};
}
my $print_size = $self->size;
my $print_bb = $self->bounding_box;
my $print_size = $print_bb->size;
print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
@ -448,16 +130,20 @@ EOF
my @previous_layer_slices = ();
for my $layer (@layers) {
$layer_id++;
# TODO: remove slic3r:z for raft layers
printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale($layer->slice_z);
if ($layer->slice_z == -1) {
printf $fh qq{ <g id="layer%d">\n}, $layer_id;
} else {
printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale($layer->slice_z);
}
my @current_layer_slices = ();
# sort slices so that the outermost ones come first
my @slices = sort { $a->contour->contains_point($b->contour->[0]) ? 0 : 1 } @{$layer->slices};
foreach my $copy (@{$layer->object->copies}) {
my @slices = sort { $a->contour->contains_point($b->contour->first_point) ? 0 : 1 } @{$layer->slices};
foreach my $copy (@{$layer->object->_shifted_copies}) {
foreach my $slice (@slices) {
my $expolygon = $slice->clone;
$expolygon->translate(@$copy);
$expolygon->translate(-$print_bb->x_min, -$print_bb->y_min);
$print_polygon->($expolygon->contour, 'contour');
$print_polygon->($_, 'hole') for @{$expolygon->holes};
push @current_layer_slices, $expolygon;
@ -511,8 +197,7 @@ sub make_skirt {
# checking whether we need to generate them
$self->skirt->clear;
if ($self->config->skirts == 0
&& (!$self->config->ooze_prevention || @{$self->extruders} == 1)) {
if (!$self->has_skirt) {
$self->set_step_done(STEP_SKIRT);
return;
}
@ -522,7 +207,7 @@ sub make_skirt {
# The skirt_height option from config is expressed in layers, but our
# object might have different layer heights, so we need to find the print_z
# of the highest layer involved.
# Note that unless skirt_height == -1 (which means it's printed on all layers)
# Note that unless has_infinite_skirt() == true
# the actual skirt might not reach this $skirt_height_z value since the print
# order of objects on each layer is not guaranteed and will not generally
# include the thickest object first. It is just guaranteed that a skirt is
@ -530,10 +215,9 @@ sub make_skirt {
# $skirt_height_z in this case is the highest possible skirt height for safety.
my $skirt_height_z = -1;
foreach my $object (@{$self->objects}) {
my $skirt_height = ($self->config->skirt_height == -1)
? scalar(@{$object->layers})
: min($self->config->skirt_height, scalar(@{$object->layers}));
my $skirt_height = $self->has_infinite_skirt
? $object->layer_count
: min($self->config->skirt_height, $object->layer_count);
my $highest_layer = $object->get_layer($skirt_height - 1);
$skirt_height_z = max($skirt_height_z, $highest_layer->print_z);
}
@ -581,26 +265,33 @@ sub make_skirt {
my @extruders_e_per_mm = ();
my $extruder_idx = 0;
my $skirts = $self->config->skirts;
$skirts ||= 1 if $self->has_infinite_skirt;
# draw outlines from outside to inside
# loop while we have less skirts than required or any extruder hasn't reached the min length if any
my $distance = scale max($self->config->skirt_distance, $self->config->brim_width);
for (my $i = $self->config->skirts; $i > 0; $i--) {
for (my $i = $skirts; $i > 0; $i--) {
$distance += scale $spacing;
my $loop = offset([$convex_hull], $distance, 1, JT_ROUND, scale(0.1))->[0];
$self->skirt->append(Slic3r::ExtrusionLoop->new_from_paths(
my $eloop = Slic3r::ExtrusionLoop->new_from_paths(
Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polygon->new(@$loop)->split_at_first_point,
role => EXTR_ROLE_SKIRT,
mm3_per_mm => $mm3_per_mm,
mm3_per_mm => $mm3_per_mm, # this will be overridden at G-code export time
width => $flow->width,
height => $first_layer_height,
height => $first_layer_height, # this will be overridden at G-code export time
),
));
);
$eloop->role(EXTRL_ROLE_SKIRT);
$self->skirt->append($eloop);
if ($self->config->min_skirt_length > 0) {
$extruded_length[$extruder_idx] ||= 0;
if (!$extruders_e_per_mm[$extruder_idx]) {
my $extruder = Slic3r::Extruder->new($extruder_idx, $self->config);
my $config = Slic3r::Config::GCode->new;
$config->apply_static($self->config);
my $extruder = Slic3r::Extruder->new($extruder_idx, $config);
$extruders_e_per_mm[$extruder_idx] = $extruder->e_per_mm($mm3_per_mm);
}
$extruded_length[$extruder_idx] += unscale $loop->length * $extruders_e_per_mm[$extruder_idx];
@ -641,9 +332,9 @@ sub make_brim {
}
$self->status_cb->(88, "Generating brim");
# brim is only printed on first layer and uses support material extruder
# brim is only printed on first layer and uses perimeter extruder
my $first_layer_height = $self->skirt_first_layer_height;
my $flow = $self->skirt_flow;
my $flow = $self->brim_flow;
my $mm3_per_mm = $flow->mm3_per_mm;
my $grow_distance = $flow->scaled_width / 2;
@ -691,23 +382,6 @@ sub make_brim {
$self->set_step_done(STEP_BRIM);
}
sub skirt_first_layer_height {
my ($self) = @_;
return $self->objects->[0]->config->get_abs_value('first_layer_height');
}
sub skirt_flow {
my ($self) = @_;
return Slic3r::Flow->new_from_width(
width => ($self->config->first_layer_extrusion_width || $self->regions->[0]->config->perimeter_extrusion_width),
role => FLOW_ROLE_PERIMETER,
nozzle_diameter => $self->config->get_at('nozzle_diameter', $self->objects->[0]->config->support_material_extruder-1),
layer_height => $self->skirt_first_layer_height,
bridge_flow_ratio => 0,
);
}
sub write_gcode {
my $self = shift;
my ($file) = @_;
@ -724,244 +398,11 @@ sub write_gcode {
binmode $fh, ':utf8';
}
# write some information
my @lt = localtime;
printf $fh "; generated by Slic3r $Slic3r::VERSION on %04d-%02d-%02d at %02d:%02d:%02d\n\n",
$lt[5] + 1900, $lt[4]+1, $lt[3], $lt[2], $lt[1], $lt[0];
print $fh "; $_\n" foreach split /\R/, $self->config->notes;
print $fh "\n" if $self->config->notes;
my $first_object = $self->objects->[0];
my $layer_height = $first_object->config->layer_height;
for my $region_id (0..$#{$self->regions}) {
printf $fh "; external perimeters extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_EXTERNAL_PERIMETER, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; perimeters extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_PERIMETER, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; infill extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; solid infill extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_SOLID_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; top infill extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_TOP_SOLID_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; support material extrusion width = %.2fmm\n",
$self->objects->[0]->support_material_flow->width
if $self->has_support_material;
printf $fh "; first layer extrusion width = %.2fmm\n",
$self->regions->[$region_id]->flow(FLOW_ROLE_PERIMETER, $layer_height, 0, 1, -1, $self->objects->[0])->width
if $self->regions->[$region_id]->config->first_layer_extrusion_width;
print $fh "\n";
}
# prepare the helper object for replacing placeholders in custom G-code and output filename
$self->placeholder_parser->update_timestamp;
# estimate the total number of layer changes
# TODO: only do this when M73 is enabled
my $layer_count;
if ($self->config->complete_objects) {
$layer_count = sum(map { $_->total_layer_count * @{$_->copies} } @{$self->objects});
} else {
# if sequential printing is not enable, all copies of the same object share the same layer change command(s)
$layer_count = sum(map { $_->total_layer_count } @{$self->objects});
}
# set up our helper object
my $gcodegen = Slic3r::GCode->new(
placeholder_parser => $self->placeholder_parser,
layer_count => $layer_count,
my $exporter = Slic3r::Print::GCode->new(
print => $self,
fh => $fh,
);
$gcodegen->config->apply_print_config($self->config);
$gcodegen->set_extruders($self->extruders, $self->config);
print $fh "G21 ; set units to millimeters\n" if $self->config->gcode_flavor ne 'makerware';
print $fh $gcodegen->set_fan(0, 1) if $self->config->cooling && $self->config->disable_fan_first_layers;
# set bed temperature
if ((my $temp = $self->config->first_layer_bed_temperature) && $self->config->start_gcode !~ /M(?:190|140)/i) {
printf $fh $gcodegen->set_bed_temperature($temp, 1);
}
# set extruder(s) temperature before and after start G-code
my $print_first_layer_temperature = sub {
my ($wait) = @_;
return if $self->config->start_gcode =~ /M(?:109|104)/i;
for my $t (@{$self->extruders}) {
my $temp = $self->config->get_at('first_layer_temperature', $t);
$temp += $self->config->standby_temperature_delta if $self->config->ooze_prevention;
printf $fh $gcodegen->set_temperature($temp, $wait, $t) if $temp > 0;
}
};
$print_first_layer_temperature->(0);
printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->start_gcode);
$print_first_layer_temperature->(1);
# set other general things
print $fh "G90 ; use absolute coordinates\n" if $self->config->gcode_flavor ne 'makerware';
if ($self->config->gcode_flavor =~ /^(?:reprap|teacup)$/) {
printf $fh $gcodegen->reset_e;
if ($self->config->use_relative_e_distances) {
print $fh "M83 ; use relative distances for extrusion\n";
} else {
print $fh "M82 ; use absolute distances for extrusion\n";
}
}
# initialize a motion planner for object-to-object travel moves
if ($self->config->avoid_crossing_perimeters) {
my $distance_from_objects = 1;
# compute the offsetted convex hull for each object and repeat it for each copy.
my @islands = ();
foreach my $obj_idx (0 .. ($self->object_count - 1)) {
my $convex_hull = convex_hull([
map @{$_->contour}, map @{$_->slices}, @{$self->objects->[$obj_idx]->layers},
]);
# discard layers only containing thin walls (offset would fail on an empty polygon)
if (@$convex_hull) {
my $expolygon = Slic3r::ExPolygon->new($convex_hull);
my @island = @{$expolygon->offset_ex(scale $distance_from_objects, 1, JT_SQUARE)};
foreach my $copy (@{ $self->objects->[$obj_idx]->_shifted_copies }) {
push @islands, map { my $c = $_->clone; $c->translate(@$copy); $c } @island;
}
}
}
$gcodegen->external_mp(Slic3r::MotionPlanner->new(union_ex([ map @$_, @islands ])));
}
# calculate wiping points if needed
if ($self->config->ooze_prevention) {
my @skirt_points = map @$_, map @$_, @{$self->skirt};
if (@skirt_points) {
my $outer_skirt = convex_hull(\@skirt_points);
my @skirts = ();
foreach my $extruder_id (@{$self->extruders}) {
push @skirts, my $s = $outer_skirt->clone;
$s->translate(map scale($_), @{$self->config->get_at('extruder_offset', $extruder_id)});
}
my $convex_hull = convex_hull([ map @$_, @skirts ]);
$gcodegen->standby_points([ map $_->clone, map @$_, map $_->subdivide(scale 10), @{offset([$convex_hull], scale 3)} ]);
}
}
# prepare the layer processor
my $layer_gcode = Slic3r::GCode::Layer->new(
print => $self,
gcodegen => $gcodegen,
);
# set initial extruder only after custom start G-code
print $fh $gcodegen->set_extruder($self->extruders->[0]);
# do all objects for each layer
if ($self->config->complete_objects) {
# print objects from the smallest to the tallest to avoid collisions
# when moving onto next object starting point
my @obj_idx = sort { $self->objects->[$a]->size->[Z] <=> $self->objects->[$b]->size->[Z] } 0..($self->object_count - 1);
my $finished_objects = 0;
for my $obj_idx (@obj_idx) {
my $object = $self->objects->[$obj_idx];
for my $copy (@{ $self->objects->[$obj_idx]->_shifted_copies }) {
# move to the origin position for the copy we're going to print.
# this happens before Z goes down to layer 0 again, so that
# no collision happens hopefully.
if ($finished_objects > 0) {
$gcodegen->set_shift(map unscale $copy->[$_], X,Y);
print $fh $gcodegen->retract;
print $fh $gcodegen->G0($object->_copies_shift->negative, undef, 0, $gcodegen->config->travel_speed*60, 'move to origin position for next object');
}
my $buffer = Slic3r::GCode::CoolingBuffer->new(
config => $self->config,
gcodegen => $gcodegen,
);
my @layers = sort { $a->print_z <=> $b->print_z } @{$object->layers}, @{$object->support_layers};
for my $layer (@layers) {
# if we are printing the bottom layer of an object, and we have already finished
# another one, set first layer temperatures. this happens before the Z move
# is triggered, so machine has more time to reach such temperatures
if ($layer->id == 0 && $finished_objects > 0) {
printf $fh $gcodegen->set_bed_temperature($self->config->first_layer_bed_temperature),
if $self->config->first_layer_bed_temperature;
$print_first_layer_temperature->();
}
print $fh $buffer->append(
$layer_gcode->process_layer($layer, [$copy]),
$layer->object->ptr,
$layer->id,
$layer->print_z,
);
}
print $fh $buffer->flush;
$finished_objects++;
}
}
} else {
# order objects using a nearest neighbor search
my @obj_idx = @{chained_path([ map Slic3r::Point->new(@{$_->_shifted_copies->[0]}), @{$self->objects} ])};
# sort layers by Z
my %layers = (); # print_z => [ [layers], [layers], [layers] ] by obj_idx
foreach my $obj_idx (0 .. ($self->object_count - 1)) {
my $object = $self->objects->[$obj_idx];
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
$layers{ $layer->print_z } ||= [];
$layers{ $layer->print_z }[$obj_idx] ||= [];
push @{$layers{ $layer->print_z }[$obj_idx]}, $layer;
}
}
my $buffer = Slic3r::GCode::CoolingBuffer->new(
config => $self->config,
gcodegen => $gcodegen,
);
foreach my $print_z (sort { $a <=> $b } keys %layers) {
foreach my $obj_idx (@obj_idx) {
foreach my $layer (@{ $layers{$print_z}[$obj_idx] // [] }) {
print $fh $buffer->append(
$layer_gcode->process_layer($layer, $layer->object->_shifted_copies),
$layer->object->ptr . ref($layer), # differentiate $obj_id between normal layers and support layers
$layer->id,
$layer->print_z,
);
}
}
}
print $fh $buffer->flush;
}
# write end commands to file
print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder
print $fh $gcodegen->set_fan(0);
printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->end_gcode);
$self->total_used_filament(0);
$self->total_extruded_volume(0);
foreach my $extruder_id (@{$self->extruders}) {
my $extruder = $gcodegen->extruders->{$extruder_id};
# the final retraction doesn't really count as "used filament"
my $used_filament = $extruder->absolute_E + $extruder->retract_length;
my $extruded_volume = $extruder->extruded_volume($used_filament);
printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
$used_filament, $extruded_volume/1000;
$self->total_used_filament($self->total_used_filament + $used_filament);
$self->total_extruded_volume($self->total_extruded_volume + $extruded_volume);
}
# append full config
print $fh "\n";
foreach my $config ($self->config, $self->default_object_config, $self->default_region_config) {
foreach my $opt_key (sort @{$config->get_keys}) {
next if $Slic3r::Config::Options->{$opt_key}{shortcut};
printf $fh "; %s = %s\n", $opt_key, $config->serialize($opt_key);
}
}
$exporter->export;
# close our gcode file
close $fh;
@ -979,10 +420,15 @@ sub expanded_output_filepath {
my $filename = my $filename_base = basename($input_file);
$filename_base =~ s/\.[^.]+$//; # without suffix
my $extra = {
input_filename => $filename,
input_filename_base => $filename_base,
};
# set filename in placeholder parser so that it's available also in custom G-code
$self->placeholder_parser->set(input_filename => $filename);
$self->placeholder_parser->set(input_filename_base => $filename_base);
# set other variables from model object
$self->placeholder_parser->set_multiple(
scale => [ map $_->model_object->instances->[0]->scaling_factor * 100 . "%", @{$self->objects} ],
);
if ($path && -d $path) {
# if output path is an existing directory, we take that and append
@ -998,27 +444,7 @@ sub expanded_output_filepath {
# make sure we use an up-to-date timestamp
$self->placeholder_parser->update_timestamp;
return $self->placeholder_parser->process($path, $extra);
}
# This method assigns extruders to the volumes having a material
# but not having extruders set in the material config.
sub auto_assign_extruders {
my ($self, $model_object) = @_;
# only assign extruders if object has more than one volume
return if @{$model_object->volumes} == 1;
my $extruders = scalar @{ $self->config->nozzle_diameter };
foreach my $i (0..$#{$model_object->volumes}) {
my $volume = $model_object->volumes->[$i];
if ($volume->material_id ne '') {
my $material = $model_object->model->get_material($volume->material_id);
my $config = $material->config;
my $extruder_id = $i + 1;
$config->set_ifndef('extruder', $extruder_id);
}
}
return $self->placeholder_parser->process($path);
}
1;

646
lib/Slic3r/Print/GCode.pm Normal file
View File

@ -0,0 +1,646 @@
package Slic3r::Print::GCode;
use Moo;
has 'print' => (is => 'ro', required => 1, handles => [qw(objects placeholder_parser config)]);
has 'fh' => (is => 'ro', required => 1);
has '_gcodegen' => (is => 'rw');
has '_cooling_buffer' => (is => 'rw');
has '_spiral_vase' => (is => 'rw');
has '_vibration_limit' => (is => 'rw');
has '_arc_fitting' => (is => 'rw');
has '_pressure_regulator' => (is => 'rw');
has '_skirt_done' => (is => 'rw', default => sub { {} }); # print_z => 1
has '_brim_done' => (is => 'rw');
has '_second_layer_things_done' => (is => 'rw');
has '_last_obj_copy' => (is => 'rw');
use List::Util qw(first sum min max);
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(X Y scale unscale chained_path convex_hull);
use Slic3r::Geometry::Clipper qw(JT_SQUARE union_ex offset);
sub BUILD {
my ($self) = @_;
{
# estimate the total number of layer changes
# TODO: only do this when M73 is enabled
my $layer_count;
if ($self->config->complete_objects) {
$layer_count = sum(map { $_->total_layer_count * @{$_->copies} } @{$self->objects});
} else {
# if sequential printing is not enable, all copies of the same object share the same layer change command(s)
$layer_count = sum(map { $_->total_layer_count } @{$self->objects});
}
# set up our helper object
my $gcodegen = Slic3r::GCode->new;
$self->_gcodegen($gcodegen);
$gcodegen->set_placeholder_parser($self->placeholder_parser);
$gcodegen->set_layer_count($layer_count);
$gcodegen->set_enable_cooling_markers(1);
$gcodegen->apply_print_config($self->config);
$gcodegen->set_extruders($self->print->extruders);
# initialize autospeed
{
# get the minimum cross-section used in the print
my @mm3_per_mm = ();
foreach my $object (@{$self->print->objects}) {
foreach my $region_id (0..$#{$self->print->regions}) {
my $region = $self->print->get_region($region_id);
foreach my $layer (@{$object->layers}) {
my $layerm = $layer->get_region($region_id);
if ($region->config->get_abs_value('perimeter_speed') == 0
|| $region->config->get_abs_value('small_perimeter_speed') == 0
|| $region->config->get_abs_value('external_perimeter_speed') == 0
|| $region->config->get_abs_value('bridge_speed') == 0) {
push @mm3_per_mm, $layerm->perimeters->min_mm3_per_mm;
}
if ($region->config->get_abs_value('infill_speed') == 0
|| $region->config->get_abs_value('solid_infill_speed') == 0
|| $region->config->get_abs_value('top_solid_infill_speed') == 0
|| $region->config->get_abs_value('bridge_speed') == 0) {
push @mm3_per_mm, $layerm->fills->min_mm3_per_mm;
}
}
}
if ($object->config->get_abs_value('support_material_speed') == 0
|| $object->config->get_abs_value('support_material_interface_speed') == 0) {
foreach my $layer (@{$object->support_layers}) {
push @mm3_per_mm, $layer->support_fills->min_mm3_per_mm;
push @mm3_per_mm, $layer->support_interface_fills->min_mm3_per_mm;
}
}
}
# filter out 0-width segments
@mm3_per_mm = grep $_ > 0.000001, @mm3_per_mm;
if (@mm3_per_mm) {
my $min_mm3_per_mm = min(@mm3_per_mm);
# In order to honor max_print_speed we need to find a target volumetric
# speed that we can use throughout the print. So we define this target
# volumetric speed as the volumetric speed produced by printing the
# smallest cross-section at the maximum speed: any larger cross-section
# will need slower feedrates.
my $volumetric_speed = $min_mm3_per_mm * $self->config->max_print_speed;
# limit such volumetric speed with max_volumetric_speed if set
if ($self->config->max_volumetric_speed > 0) {
$volumetric_speed = min(
$volumetric_speed,
$self->config->max_volumetric_speed,
);
}
$gcodegen->set_volumetric_speed($volumetric_speed);
}
}
}
$self->_cooling_buffer(Slic3r::GCode::CoolingBuffer->new(
config => $self->config,
gcodegen => $self->_gcodegen,
));
$self->_spiral_vase(Slic3r::GCode::SpiralVase->new(config => $self->config))
if $self->config->spiral_vase;
$self->_vibration_limit(Slic3r::GCode::VibrationLimit->new(config => $self->config))
if $self->config->vibration_limit != 0;
$self->_arc_fitting(Slic3r::GCode::ArcFitting->new(config => $self->config))
if $self->config->gcode_arcs;
$self->_pressure_regulator(Slic3r::GCode::PressureRegulator->new(config => $self->config))
if $self->config->pressure_advance > 0;
}
sub export {
my ($self) = @_;
my $fh = $self->fh;
my $gcodegen = $self->_gcodegen;
# write some information
my @lt = localtime;
printf $fh "; generated by Slic3r $Slic3r::VERSION on %04d-%02d-%02d at %02d:%02d:%02d\n\n",
$lt[5] + 1900, $lt[4]+1, $lt[3], $lt[2], $lt[1], $lt[0];
print $fh "; $_\n" foreach split /\R/, $self->config->notes;
print $fh "\n" if $self->config->notes;
my $first_object = $self->objects->[0];
my $layer_height = $first_object->config->layer_height;
for my $region_id (0..$#{$self->print->regions}) {
my $region = $self->print->regions->[$region_id];
printf $fh "; external perimeters extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_EXTERNAL_PERIMETER, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; perimeters extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_PERIMETER, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; infill extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; solid infill extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_SOLID_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; top infill extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_TOP_SOLID_INFILL, $layer_height, 0, 0, -1, $first_object)->width;
printf $fh "; support material extrusion width = %.2fmm\n",
$self->objects->[0]->support_material_flow->width
if $self->print->has_support_material;
printf $fh "; first layer extrusion width = %.2fmm\n",
$region->flow(FLOW_ROLE_PERIMETER, $layer_height, 0, 1, -1, $self->objects->[0])->width
if $region->config->first_layer_extrusion_width;
print $fh "\n";
}
# prepare the helper object for replacing placeholders in custom G-code and output filename
$self->placeholder_parser->update_timestamp;
print $fh $gcodegen->writer->set_fan(0, 1)
if $self->config->cooling && $self->config->disable_fan_first_layers;
# set bed temperature
if ((my $temp = $self->config->first_layer_bed_temperature) && $self->config->start_gcode !~ /M(?:190|140)/i) {
printf $fh $gcodegen->writer->set_bed_temperature($temp, 1);
}
# set extruder(s) temperature before and after start G-code
$self->_print_first_layer_temperature(0);
printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->start_gcode);
$self->_print_first_layer_temperature(1);
# set other general things
print $fh $gcodegen->preamble;
# initialize a motion planner for object-to-object travel moves
if ($self->config->avoid_crossing_perimeters) {
my $distance_from_objects = scale 1;
# compute the offsetted convex hull for each object and repeat it for each copy.
my @islands_p = ();
foreach my $object (@{$self->objects}) {
# discard objects only containing thin walls (offset would fail on an empty polygon)
my @polygons = map $_->contour, map @{$_->slices}, @{$object->layers};
next if !@polygons;
# translate convex hull for each object copy and append it to the islands array
foreach my $copy (@{ $object->_shifted_copies }) {
my @copy_islands_p = map $_->clone, @polygons;
$_->translate(@$copy) for @copy_islands_p;
push @islands_p, @copy_islands_p;
}
}
$gcodegen->avoid_crossing_perimeters->init_external_mp(union_ex(\@islands_p));
}
# calculate wiping points if needed
if ($self->config->ooze_prevention) {
my @skirt_points = map @$_, map @$_, @{$self->print->skirt};
if (@skirt_points) {
my $outer_skirt = convex_hull(\@skirt_points);
my @skirts = ();
foreach my $extruder_id (@{$self->print->extruders}) {
my $extruder_offset = $self->config->get_at('extruder_offset', $extruder_id);
push @skirts, my $s = $outer_skirt->clone;
$s->translate(-scale($extruder_offset->x), -scale($extruder_offset->y)); #)
}
my $convex_hull = convex_hull([ map @$_, @skirts ]);
$gcodegen->ooze_prevention->set_enable(1);
$gcodegen->ooze_prevention->set_standby_points(
[ map @{$_->equally_spaced_points(scale 10)}, @{offset([$convex_hull], scale 3)} ]
);
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"ooze_prevention.svg",
red_polygons => \@skirts,
polygons => [$outer_skirt],
points => $gcodegen->ooze_prevention->standby_points,
);
}
}
}
# set initial extruder only after custom start G-code
print $fh $gcodegen->set_extruder($self->print->extruders->[0]);
# do all objects for each layer
if ($self->config->complete_objects) {
# print objects from the smallest to the tallest to avoid collisions
# when moving onto next object starting point
my @obj_idx = sort { $self->objects->[$a]->size->z <=> $self->objects->[$b]->size->z } 0..($self->print->object_count - 1);
my $finished_objects = 0;
for my $obj_idx (@obj_idx) {
my $object = $self->objects->[$obj_idx];
for my $copy (@{ $self->objects->[$obj_idx]->_shifted_copies }) {
# move to the origin position for the copy we're going to print.
# this happens before Z goes down to layer 0 again, so that
# no collision happens hopefully.
if ($finished_objects > 0) {
$gcodegen->set_origin(Slic3r::Pointf->new(map unscale $copy->[$_], X,Y));
$gcodegen->set_enable_cooling_markers(0); # we're not filtering these moves through CoolingBuffer
$gcodegen->avoid_crossing_perimeters->set_use_external_mp_once(1);
print $fh $gcodegen->retract;
print $fh $gcodegen->travel_to(
Slic3r::Point->new(0,0),
EXTR_ROLE_NONE,
'move to origin position for next object',
);
$gcodegen->set_enable_cooling_markers(1);
# disable motion planner when traveling to first object point
$gcodegen->avoid_crossing_perimeters->set_disable_once(1);
}
my @layers = sort { $a->print_z <=> $b->print_z } @{$object->layers}, @{$object->support_layers};
for my $layer (@layers) {
# if we are printing the bottom layer of an object, and we have already finished
# another one, set first layer temperatures. this happens before the Z move
# is triggered, so machine has more time to reach such temperatures
if ($layer->id == 0 && $finished_objects > 0) {
printf $fh $gcodegen->writer->set_bed_temperature($self->config->first_layer_bed_temperature),
if $self->config->first_layer_bed_temperature;
$self->_print_first_layer_temperature(0);
}
$self->process_layer($layer, [$copy]);
}
$self->flush_filters;
$finished_objects++;
$self->_second_layer_things_done(0);
}
}
} else {
# order objects using a nearest neighbor search
my @obj_idx = @{chained_path([ map Slic3r::Point->new(@{$_->_shifted_copies->[0]}), @{$self->objects} ])};
# sort layers by Z
my %layers = (); # print_z => [ [layers], [layers], [layers] ] by obj_idx
foreach my $obj_idx (0 .. ($self->print->object_count - 1)) {
my $object = $self->objects->[$obj_idx];
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
$layers{ $layer->print_z } ||= [];
$layers{ $layer->print_z }[$obj_idx] ||= [];
push @{$layers{ $layer->print_z }[$obj_idx]}, $layer;
}
}
foreach my $print_z (sort { $a <=> $b } keys %layers) {
foreach my $obj_idx (@obj_idx) {
foreach my $layer (@{ $layers{$print_z}[$obj_idx] // [] }) {
$self->process_layer($layer, $layer->object->_shifted_copies);
}
}
}
$self->flush_filters;
}
# write end commands to file
print $fh $gcodegen->retract; # TODO: process this retract through PressureRegulator in order to discharge fully
print $fh $gcodegen->writer->set_fan(0);
printf $fh "%s\n", $gcodegen->placeholder_parser->process($self->config->end_gcode);
print $fh $gcodegen->writer->update_progress($gcodegen->layer_count, $gcodegen->layer_count, 1); # 100%
print $fh $gcodegen->writer->postamble;
# get filament stats
$self->print->clear_filament_stats;
$self->print->total_used_filament(0);
$self->print->total_extruded_volume(0);
foreach my $extruder (@{$gcodegen->writer->extruders}) {
my $used_filament = $extruder->used_filament;
my $extruded_volume = $extruder->extruded_volume;
$self->print->set_filament_stats($extruder->id, $used_filament);
printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
$used_filament, $extruded_volume/1000;
$self->print->total_used_filament($self->print->total_used_filament + $used_filament);
$self->print->total_extruded_volume($self->print->total_extruded_volume + $extruded_volume);
}
# append full config
print $fh "\n";
foreach my $config ($self->print->config, $self->print->default_object_config, $self->print->default_region_config) {
foreach my $opt_key (sort @{$config->get_keys}) {
next if $Slic3r::Config::Options->{$opt_key}{shortcut};
printf $fh "; %s = %s\n", $opt_key, $config->serialize($opt_key);
}
}
}
sub _print_first_layer_temperature {
my ($self, $wait) = @_;
return if $self->config->start_gcode =~ /M(?:109|104)/i;
for my $t (@{$self->print->extruders}) {
my $temp = $self->config->get_at('first_layer_temperature', $t);
$temp += $self->config->standby_temperature_delta if $self->config->ooze_prevention;
printf {$self->fh} $self->_gcodegen->writer->set_temperature($temp, $wait, $t) if $temp > 0;
}
}
sub process_layer {
my $self = shift;
my ($layer, $object_copies) = @_;
my $gcode = "";
my $object = $layer->object;
$self->_gcodegen->config->apply_static($object->config);
# check whether we're going to apply spiralvase logic
if (defined $self->_spiral_vase) {
$self->_spiral_vase->enable(
($layer->id > 0 || $self->print->config->brim_width == 0)
&& ($layer->id >= $self->print->config->skirt_height && !$self->print->has_infinite_skirt)
&& !defined(first { $_->region->config->bottom_solid_layers > $layer->id } @{$layer->regions})
&& !defined(first { $_->perimeters->items_count > 1 } @{$layer->regions})
&& !defined(first { $_->fills->items_count > 0 } @{$layer->regions})
);
}
# if we're going to apply spiralvase to this layer, disable loop clipping
$self->_gcodegen->set_enable_loop_clipping(!defined $self->_spiral_vase || !$self->_spiral_vase->enable);
if (!$self->_second_layer_things_done && $layer->id == 1) {
for my $extruder (@{$self->_gcodegen->writer->extruders}) {
my $temperature = $self->config->get_at('temperature', $extruder->id);
$gcode .= $self->_gcodegen->writer->set_temperature($temperature, 0, $extruder->id)
if $temperature && $temperature != $self->config->get_at('first_layer_temperature', $extruder->id);
}
$gcode .= $self->_gcodegen->writer->set_bed_temperature($self->print->config->bed_temperature)
if $self->print->config->bed_temperature && $self->print->config->bed_temperature != $self->print->config->first_layer_bed_temperature;
$self->_second_layer_things_done(1);
}
# set new layer - this will change Z and force a retraction if retract_layer_change is enabled
if ($self->print->config->before_layer_gcode) {
my $pp = $self->_gcodegen->placeholder_parser->clone;
$pp->set('layer_num' => $self->_gcodegen->layer_index + 1);
$pp->set('layer_z' => $layer->print_z);
$gcode .= $pp->process($self->print->config->before_layer_gcode) . "\n";
}
$gcode .= $self->_gcodegen->change_layer($layer->as_layer); # this will increase $self->_gcodegen->layer_index
if ($self->print->config->layer_gcode) {
my $pp = $self->_gcodegen->placeholder_parser->clone;
$pp->set('layer_num' => $self->_gcodegen->layer_index);
$pp->set('layer_z' => $layer->print_z);
$gcode .= $pp->process($self->print->config->layer_gcode) . "\n";
}
# extrude skirt along raft layers and normal object layers
# (not along interlaced support material layers)
if (((values %{$self->_skirt_done}) < $self->print->config->skirt_height || $self->print->has_infinite_skirt)
&& !$self->_skirt_done->{$layer->print_z}
&& (!$layer->isa('Slic3r::Layer::Support') || $layer->id < $object->config->raft_layers)) {
$self->_gcodegen->set_origin(Slic3r::Pointf->new(0,0));
$self->_gcodegen->avoid_crossing_perimeters->set_use_external_mp(1);
my @extruder_ids = map { $_->id } @{$self->_gcodegen->writer->extruders};
$gcode .= $self->_gcodegen->set_extruder($extruder_ids[0]);
# skip skirt if we have a large brim
if ($layer->id < $self->print->config->skirt_height || $self->print->has_infinite_skirt) {
my $skirt_flow = $self->print->skirt_flow;
# distribute skirt loops across all extruders
my @skirt_loops = @{$self->print->skirt};
for my $i (0 .. $#skirt_loops) {
# when printing layers > 0 ignore 'min_skirt_length' and
# just use the 'skirts' setting; also just use the current extruder
last if ($layer->id > 0) && ($i >= $self->print->config->skirts);
my $extruder_id = $extruder_ids[($i/@extruder_ids) % @extruder_ids];
$gcode .= $self->_gcodegen->set_extruder($extruder_id)
if $layer->id == 0;
# adjust flow according to this layer's layer height
my $loop = $skirt_loops[$i]->clone;
{
my $layer_skirt_flow = $skirt_flow->clone;
$layer_skirt_flow->set_height($layer->height);
my $mm3_per_mm = $layer_skirt_flow->mm3_per_mm;
foreach my $path (@$loop) {
$path->height($layer->height);
$path->mm3_per_mm($mm3_per_mm);
}
}
$gcode .= $self->_gcodegen->extrude_loop($loop, 'skirt', $object->config->support_material_speed);
}
}
$self->_skirt_done->{$layer->print_z} = 1;
$self->_gcodegen->avoid_crossing_perimeters->set_use_external_mp(0);
# allow a straight travel move to the first object point if this is the first layer
# (but don't in next layers)
if ($layer->id == 0) {
$self->_gcodegen->avoid_crossing_perimeters->set_disable_once(1);
}
}
# extrude brim
if (!$self->_brim_done) {
$gcode .= $self->_gcodegen->set_extruder($self->print->regions->[0]->config->perimeter_extruder-1);
$self->_gcodegen->set_origin(Slic3r::Pointf->new(0,0));
$self->_gcodegen->avoid_crossing_perimeters->set_use_external_mp(1);
$gcode .= $self->_gcodegen->extrude_loop($_, 'brim', $object->config->support_material_speed)
for @{$self->print->brim};
$self->_brim_done(1);
$self->_gcodegen->avoid_crossing_perimeters->set_use_external_mp(0);
# allow a straight travel move to the first object point
$self->_gcodegen->avoid_crossing_perimeters->set_disable_once(1);
}
for my $copy (@$object_copies) {
# when starting a new object, use the external motion planner for the first travel move
$self->_gcodegen->avoid_crossing_perimeters->set_use_external_mp_once(1) if ($self->_last_obj_copy // '') ne "$copy";
$self->_last_obj_copy("$copy");
$self->_gcodegen->set_origin(Slic3r::Pointf->new(map unscale $copy->[$_], X,Y));
# extrude support material before other things because it might use a lower Z
# and also because we avoid travelling on other things when printing it
if ($layer->isa('Slic3r::Layer::Support')) {
if ($layer->support_interface_fills->count > 0) {
$gcode .= $self->_gcodegen->set_extruder($object->config->support_material_interface_extruder-1);
$gcode .= $self->_gcodegen->extrude_path($_, 'support material interface', $object->config->get_abs_value('support_material_interface_speed'))
for @{$layer->support_interface_fills->chained_path_from($self->_gcodegen->last_pos, 0)};
}
if ($layer->support_fills->count > 0) {
$gcode .= $self->_gcodegen->set_extruder($object->config->support_material_extruder-1);
$gcode .= $self->_gcodegen->extrude_path($_, 'support material', $object->config->get_abs_value('support_material_speed'))
for @{$layer->support_fills->chained_path_from($self->_gcodegen->last_pos, 0)};
}
}
# We now define a strategy for building perimeters and fills. The separation
# between regions doesn't matter in terms of printing order, as we follow
# another logic instead:
# - we group all extrusions by extruder so that we minimize toolchanges
# - we start from the last used extruder
# - for each extruder, we group extrusions by island
# - for each island, we extrude perimeters first, unless user set the infill_first
# option
# (Still, we have to keep track of regions because we need to apply their config)
# group extrusions by extruder and then by island
my %by_extruder = (); # extruder_id => [ { perimeters => \@perimeters, infill => \@infill } ]
foreach my $region_id (0..($self->print->region_count-1)) {
my $layerm = $layer->regions->[$region_id] or next;
my $region = $self->print->get_region($region_id);
# process perimeters
{
my $extruder_id = $region->config->perimeter_extruder-1;
foreach my $perimeter_coll (@{$layerm->perimeters}) {
next if $perimeter_coll->empty; # this shouldn't happen but first_point() would fail
# init by_extruder item only if we actually use the extruder
$by_extruder{$extruder_id} //= [];
# $perimeter_coll is an ExtrusionPath::Collection object representing a single slice
for my $i (0 .. $#{$layer->slices}) {
if ($i == $#{$layer->slices}
|| $layer->slices->[$i]->contour->contains_point($perimeter_coll->first_point)) {
$by_extruder{$extruder_id}[$i] //= { perimeters => {} };
$by_extruder{$extruder_id}[$i]{perimeters}{$region_id} //= [];
push @{ $by_extruder{$extruder_id}[$i]{perimeters}{$region_id} }, @$perimeter_coll;
last;
}
}
}
}
# process infill
# $layerm->fills is a collection of ExtrusionPath::Collection objects, each one containing
# the ExtrusionPath objects of a certain infill "group" (also called "surface"
# throughout the code). We can redefine the order of such Collections but we have to
# do each one completely at once.
foreach my $fill (@{$layerm->fills}) {
next if $fill->empty; # this shouldn't happen but first_point() would fail
# init by_extruder item only if we actually use the extruder
my $extruder_id = $fill->[0]->is_solid_infill
? $region->config->solid_infill_extruder-1
: $region->config->infill_extruder-1;
$by_extruder{$extruder_id} //= [];
# $fill is an ExtrusionPath::Collection object
for my $i (0 .. $#{$layer->slices}) {
if ($i == $#{$layer->slices}
|| $layer->slices->[$i]->contour->contains_point($fill->first_point)) {
$by_extruder{$extruder_id}[$i] //= { infill => {} };
$by_extruder{$extruder_id}[$i]{infill}{$region_id} //= [];
push @{ $by_extruder{$extruder_id}[$i]{infill}{$region_id} }, $fill;
last;
}
}
}
}
# tweak extruder ordering to save toolchanges
my @extruders = sort keys %by_extruder;
if (@extruders > 1) {
my $last_extruder_id = $self->_gcodegen->writer->extruder->id;
if (exists $by_extruder{$last_extruder_id}) {
@extruders = (
$last_extruder_id,
grep $_ != $last_extruder_id, @extruders,
);
}
}
foreach my $extruder_id (@extruders) {
$gcode .= $self->_gcodegen->set_extruder($extruder_id);
foreach my $island (@{ $by_extruder{$extruder_id} }) {
if ($self->print->config->infill_first) {
$gcode .= $self->_extrude_infill($island->{infill} // {});
$gcode .= $self->_extrude_perimeters($island->{perimeters} // {});
} else {
$gcode .= $self->_extrude_perimeters($island->{perimeters} // {});
$gcode .= $self->_extrude_infill($island->{infill} // {});
}
}
}
}
# apply spiral vase post-processing if this layer contains suitable geometry
# (we must feed all the G-code into the post-processor, including the first
# bottom non-spiral layers otherwise it will mess with positions)
# we apply spiral vase at this stage because it requires a full layer
$gcode = $self->_spiral_vase->process_layer($gcode)
if defined $self->_spiral_vase;
# apply cooling logic; this may alter speeds
$gcode = $self->_cooling_buffer->append(
$gcode,
$layer->object->ptr . ref($layer), # differentiate $obj_id between normal layers and support layers
$layer->id,
$layer->print_z,
) if defined $self->_cooling_buffer;
print {$self->fh} $self->filter($gcode);
}
sub _extrude_perimeters {
my ($self, $entities_by_region) = @_;
my $gcode = "";
foreach my $region_id (sort keys %$entities_by_region) {
$self->_gcodegen->config->apply_static($self->print->get_region($region_id)->config);
$gcode .= $self->_gcodegen->extrude($_, 'perimeter', -1)
for @{ $entities_by_region->{$region_id} };
}
return $gcode;
}
sub _extrude_infill {
my ($self, $entities_by_region) = @_;
my $gcode = "";
foreach my $region_id (sort keys %$entities_by_region) {
$self->_gcodegen->config->apply_static($self->print->get_region($region_id)->config);
my $collection = Slic3r::ExtrusionPath::Collection->new(@{ $entities_by_region->{$region_id} });
for my $fill (@{$collection->chained_path_from($self->_gcodegen->last_pos, 0)}) {
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
$gcode .= $self->_gcodegen->extrude($_, 'infill', -1)
for @{$fill->chained_path_from($self->_gcodegen->last_pos, 0)};
} else {
$gcode .= $self->_gcodegen->extrude($fill, 'infill', -1) ;
}
}
}
return $gcode;
}
sub flush_filters {
my ($self) = @_;
print {$self->fh} $self->filter($self->_cooling_buffer->flush, 1);
}
sub filter {
my ($self, $gcode, $flush) = @_;
# apply vibration limit if enabled;
# this injects pauses according to time (thus depends on actual speeds)
$gcode = $self->_vibration_limit->process($gcode)
if defined $self->_vibration_limit;
# apply pressure regulation if enabled;
# this depends on actual speeds
$gcode = $self->_pressure_regulator->process($gcode, $flush)
if defined $self->_pressure_regulator;
# apply arc fitting if enabled;
# this does not depend on speeds but changes G1 XY commands into G2/G2 IJ
$gcode = $self->_arc_fitting->process($gcode)
if defined $self->_arc_fitting;
return $gcode;
}
1;

View File

@ -4,9 +4,9 @@ use warnings;
use List::Util qw(min max sum first);
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(X Y Z PI scale unscale deg2rad rad2deg scaled_epsilon chained_path);
use Slic3r::Geometry qw(X Y Z PI scale unscale chained_path epsilon);
use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex
offset offset_ex offset2 offset2_ex CLIPPER_OFFSET_SCALE JT_MITER);
offset offset_ex offset2 offset2_ex intersection_ppl CLIPPER_OFFSET_SCALE JT_MITER);
use Slic3r::Print::State ':steps';
use Slic3r::Surface ':types';
@ -32,68 +32,6 @@ sub support_layers {
return [ map $self->get_support_layer($_), 0..($self->support_layer_count - 1) ];
}
# TODO: translate to C++, then call it from constructor (see also
# Print->add_model_object)
sub _trigger_copies {
my $self = shift;
# TODO: should this mean point is 0,0?
return if !defined $self->_copies_shift;
# order copies with a nearest neighbor search and translate them by _copies_shift
$self->set_shifted_copies([
map {
my $c = $_->clone;
$c->translate(@{ $self->_copies_shift });
$c;
} @{$self->copies}[@{chained_path($self->copies)}]
]);
$self->print->invalidate_step(STEP_SKIRT);
$self->print->invalidate_step(STEP_BRIM);
}
# in unscaled coordinates
sub add_copy {
my ($self, $x, $y) = @_;
my @copies = @{$self->copies};
push @copies, Slic3r::Point->new_scale($x, $y);
$self->set_copies(\@copies);
$self->_trigger_copies;
}
sub delete_last_copy {
my ($self) = @_;
my @copies = $self->copies;
pop @copies;
$self->set_copies(\@copies);
$self->_trigger_copies;
}
sub delete_all_copies {
my ($self) = @_;
$self->set_copies([]);
$self->_trigger_copies;
}
# this is the *total* layer count (including support layers)
# this value is not supposed to be compared with $layer->id
# since they have different semantics
sub total_layer_count {
my $self = shift;
return $self->layer_count + $self->support_layer_count;
}
sub bounding_box {
my $self = shift;
# since the object is aligned to origin, bounding box coincides with size
return Slic3r::Geometry::BoundingBox->new_from_points([
Slic3r::Point->new(0,0),
map Slic3r::Point->new($_->x, $_->y), $self->size #))
]);
}
# this should be idempotent
sub slice {
my $self = shift;
@ -107,8 +45,9 @@ sub slice {
$self->clear_layers;
# make layers taking custom heights into account
my $print_z = my $slice_z = my $height = my $cusp_height = my $id = 0;
my $first_object_layer_height = -1;
my $id = 0;
my $print_z = 0;
my $first_object_layer_height = -1;
my $first_object_layer_distance = -1;
# add raft layers
@ -117,20 +56,40 @@ sub slice {
# raise first object layer Z by the thickness of the raft itself
# plus the extra distance required by the support material logic
$print_z += $self->config->get_value('first_layer_height');
$print_z += $self->config->layer_height * ($self->config->raft_layers - 1);
my $first_layer_height = $self->config->get_value('first_layer_height');
$print_z += $first_layer_height;
# use a large height
my $support_material_layer_height;
{
my @nozzle_diameters = (
map $self->print->config->get_at('nozzle_diameter', $_),
$self->config->support_material_extruder-1,
$self->config->support_material_interface_extruder-1,
);
$support_material_layer_height = 0.75 * min(@nozzle_diameters);
}
$print_z += $support_material_layer_height * ($self->config->raft_layers - 1);
# at this stage we don't know which nozzles are actually used for the first layer
# so we compute the average of all of them
my $nozzle_diameter = sum(@{$self->print->config->nozzle_diameter})/@{$self->print->config->nozzle_diameter};
my $distance = Slic3r::Print::SupportMaterial::contact_distance($nozzle_diameter);
# compute the average of all nozzles used for printing the object
my $nozzle_diameter;
{
my @nozzle_diameters = (
map $self->print->config->get_at('nozzle_diameter', $_), @{$self->print->object_extruders}
);
$nozzle_diameter = sum(@nozzle_diameters)/@nozzle_diameters;
}
$first_object_layer_distance = $self->_support_material->contact_distance($self->config->layer_height, $nozzle_diameter);
# force first layer print_z according to the contact distance
# (the loop below will raise print_z by such height)
$first_object_layer_height = $nozzle_diameter;
$first_object_layer_distance = $distance;
$first_object_layer_height = $first_object_layer_distance - $self->config->support_material_contact_distance;
}
my $slice_z = 0;
my $height = 0;
my $cusp_height = 0;
# create stateful objects and variables for the adaptive slicing process
my @adaptive_slicing;
my $min_height = 0;
@ -184,11 +143,11 @@ sub slice {
}
}else{
# assign the default height to the layer according to the general settings
$height = ($id == 0)
? $self->config->get_value('first_layer_height')
: $self->config->layer_height;
}
# assign the default height to the layer according to the general settings
$height = ($id == 0)
? $self->config->get_value('first_layer_height')
: $self->config->layer_height;
}
# look for an applicable custom range
if (my $range = first { $_->[0] <= $slice_z && $_->[1] > $slice_z } @{$self->layer_height_ranges}) {
@ -390,11 +349,10 @@ sub slice {
);
$layerm->slices->clear;
$layerm->slices->append(
map Slic3r::Surface->new
$layerm->slices->append($_)
for map Slic3r::Surface->new
(expolygon => $_, surface_type => S_TYPE_INTERNAL),
@$diff
);
@$diff;
}
# update layer slices after repairing the single regions
@ -403,9 +361,9 @@ sub slice {
# remove empty layers from bottom
while (@{$self->layers} && !@{$self->get_layer(0)->slices}) {
shift @{$self->layers};
$self->delete_layer(0);
for (my $i = 0; $i <= $#{$self->layers}; $i++) {
$self->get_layer($i)->id( $self->get_layer($i)->id-1 );
$self->get_layer($i)->set_id( $self->get_layer($i)->id-1 );
}
}
@ -414,7 +372,7 @@ sub slice {
$self->_simplify_slices(scale($self->print->config->resolution));
}
die "No layers were detected. You might want to repair your STL file(s) or check their size and retry.\n"
die "No layers were detected. You might want to repair your STL file(s) or check their size or thickness and retry.\n"
if !@{$self->layers};
$self->set_typed_slices(0);
@ -446,7 +404,7 @@ sub _slice_region {
# consider the first one
$self->model_object->instances->[0]->transform_mesh($mesh, 1);
# align mesh to Z = 0 and apply XY shift
# align mesh to Z = 0 (it should be already aligned actually) and apply XY shift
$mesh->translate((map unscale(-$_), @{$self->_copies_shift}), -$self->model_object->bounding_box->z_min);
# perform actual slicing
@ -457,7 +415,6 @@ sub make_perimeters {
my $self = shift;
# prerequisites
$self->print->init_extruders;
$self->slice;
return if $self->step_done(STEP_PERIMETERS);
@ -482,51 +439,59 @@ sub make_perimeters {
my $region = $self->print->regions->[$region_id];
my $region_perimeters = $region->config->perimeters;
if ($region->config->extra_perimeters && $region_perimeters > 0 && $region->config->fill_density > 0) {
for my $i (0 .. ($self->layer_count - 2)) {
my $layerm = $self->get_layer($i)->regions->[$region_id];
my $upper_layerm = $self->get_layer($i+1)->regions->[$region_id];
my $perimeter_spacing = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_spacing;
my $ext_perimeter_spacing = $layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER)->scaled_spacing;
my $overlap = $perimeter_spacing; # one perimeter
my $diff = diff(
offset([ map @{$_->expolygon}, @{$layerm->slices} ], -($ext_perimeter_spacing + ($region_perimeters-1) * $perimeter_spacing)),
offset([ map @{$_->expolygon}, @{$upper_layerm->slices} ], -$overlap),
);
next if !@$diff;
# if we need more perimeters, $diff should contain a narrow region that we can collapse
# we use a higher miterLimit here to handle areas with acute angles
# in those cases, the default miterLimit would cut the corner and we'd
# get a triangle that would trigger a non-needed extra perimeter
$diff = diff(
$diff,
offset2($diff, -$perimeter_spacing, +$perimeter_spacing, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
1,
);
next if !@$diff;
# diff contains the collapsed area
foreach my $slice (@{$layerm->slices}) {
my $extra_perimeters = 0;
CYCLE: while (1) {
# compute polygons representing the thickness of the hypotetical new internal perimeter
# of our slice
$extra_perimeters++;
my $hypothetical_perimeter = diff(
offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters-1))),
offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters))),
next if !$region->config->extra_perimeters;
next if $region_perimeters == 0;
next if $region->config->fill_density == 0;
for my $i (0 .. ($self->layer_count - 2)) {
my $layerm = $self->get_layer($i)->get_region($region_id);
my $upper_layerm = $self->get_layer($i+1)->get_region($region_id);
my $perimeter_spacing = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_spacing;
my $ext_perimeter_flow = $layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER);
my $ext_perimeter_width = $ext_perimeter_flow->scaled_width;
my $ext_perimeter_spacing = $ext_perimeter_flow->scaled_spacing;
foreach my $slice (@{$layerm->slices}) {
while (1) {
# compute the total thickness of perimeters
my $perimeters_thickness = $ext_perimeter_width/2 + $ext_perimeter_spacing/2
+ ($region_perimeters-1 + $slice->extra_perimeters) * $perimeter_spacing;
# define a critical area where we don't want the upper slice to fall into
# (it should either lay over our perimeters or outside this area)
my $critical_area_depth = $perimeter_spacing*1.5;
my $critical_area = diff(
offset($slice->expolygon->arrayref, -$perimeters_thickness),
offset($slice->expolygon->arrayref, -($perimeters_thickness + $critical_area_depth)),
);
# check whether a portion of the upper slices falls inside the critical area
my $intersection = intersection_ppl(
[ map $_->p, @{$upper_layerm->slices} ],
$critical_area,
);
# only add an additional loop if at least 30% of the slice loop would benefit from it
my $total_loop_length = sum(map $_->length, map $_->p, @{$upper_layerm->slices}) // 0;
my $total_intersection_length = sum(map $_->length, @$intersection) // 0;
last unless $total_intersection_length > $total_loop_length*0.3;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"extra.svg",
no_arrows => 1,
expolygons => union_ex($critical_area),
polylines => [ map $_->split_at_first_point, map $_->p, @{$upper_layerm->slices} ],
);
last CYCLE if !@$hypothetical_perimeter; # no extra perimeter is possible
# only add the perimeter if there's an intersection with the collapsed area
last CYCLE if !@{ intersection($diff, $hypothetical_perimeter) };
Slic3r::debugf " adding one more perimeter at layer %d\n", $layerm->id;
$slice->extra_perimeters($extra_perimeters);
}
$slice->extra_perimeters($slice->extra_perimeters + 1);
}
Slic3r::debugf " adding %d more perimeter(s) at layer %d\n",
$slice->extra_perimeters, $layerm->layer->id
if $slice->extra_perimeters > 0;
}
}
}
@ -540,7 +505,6 @@ sub make_perimeters {
$self->get_layer($i)->make_perimeters;
}
},
collect_cb => sub {},
no_threads_cb => sub {
$_->make_perimeters for @{$self->layers};
},
@ -604,27 +568,16 @@ sub infill {
Slic3r::parallelize(
threads => $self->print->config->threads,
items => sub {
my @items = (); # [layer_id, region_id]
for my $region_id (0 .. ($self->print->region_count-1)) {
push @items, map [$_, $region_id], 0..($self->layer_count - 1);
}
@items;
},
items => sub { 0..$#{$self->layers} },
thread_cb => sub {
my $q = shift;
while (defined (my $obj_layer = $q->dequeue)) {
my ($i, $region_id) = @$obj_layer;
my $layerm = $self->get_layer($i)->regions->[$region_id];
$layerm->fills->clear;
$layerm->fills->append( $self->fill_maker->make_fill($layerm) );
while (defined (my $i = $q->dequeue)) {
$self->get_layer($i)->make_fill;
}
},
collect_cb => sub {},
no_threads_cb => sub {
foreach my $layerm (map @{$_->regions}, @{$self->layers}) {
$layerm->fills->clear;
$layerm->fills->append($self->fill_maker->make_fill($layerm));
foreach my $layer (@{$self->layers}) {
$layer->make_fill;
}
},
);
@ -639,7 +592,6 @@ sub generate_support_material {
my $self = shift;
# prerequisites
$self->print->init_extruders;
$self->slice;
return if $self->step_done(STEP_SUPPORTMATERIAL);
@ -653,8 +605,16 @@ sub generate_support_material {
}
$self->print->status_cb->(85, "Generating support material");
$self->_support_material->generate($self);
$self->set_step_done(STEP_SUPPORTMATERIAL);
}
sub _support_material {
my ($self) = @_;
my $first_layer_flow = Slic3r::Flow->new_from_width(
width => ($self->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width),
width => ($self->print->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width),
role => FLOW_ROLE_SUPPORT_MATERIAL,
nozzle_diameter => $self->print->config->nozzle_diameter->[ $self->config->support_material_extruder-1 ]
// $self->print->config->nozzle_diameter->[0],
@ -662,16 +622,13 @@ sub generate_support_material {
bridge_flow_ratio => 0,
);
my $s = Slic3r::Print::SupportMaterial->new(
return Slic3r::Print::SupportMaterial->new(
print_config => $self->print->config,
object_config => $self->config,
first_layer_flow => $first_layer_flow,
flow => $self->support_material_flow,
interface_flow => $self->support_material_flow(FLOW_ROLE_SUPPORT_MATERIAL_INTERFACE),
);
$s->generate($self);
$self->set_step_done(STEP_SUPPORTMATERIAL);
}
sub detect_surfaces_type {
@ -688,6 +645,7 @@ sub detect_surfaces_type {
my $diff = diff(
[ map @$_, @$subject ],
[ map @$_, @$clip ],
1,
);
# collapse very narrow parts (using the safety offset in the diff is not enough)
@ -732,6 +690,11 @@ sub detect_surfaces_type {
S_TYPE_BOTTOMBRIDGE,
);
# if we have soluble support material, don't bridge
if ($self->config->support_material && $self->config->support_material_contact_distance == 0) {
$_->surface_type(S_TYPE_BOTTOM) for @bottom;
}
# if user requested internal shells, we need to identify surfaces
# lying on other slices not belonging to this region
if ($self->config->interface_shells) {
@ -751,7 +714,14 @@ sub detect_surfaces_type {
# if no lower layer, all surfaces of this one are solid
# we clone surfaces because we're going to clear the slices collection
@bottom = map $_->clone, @{$layerm->slices};
$_->surface_type(S_TYPE_BOTTOM) for @bottom;
# if we have raft layers, consider bottom layer as a bridge
# just like any other bottom surface lying on the void
if ($self->config->raft_layers > 0 && $self->config->support_material_contact_distance > 0) {
$_->surface_type(S_TYPE_BOTTOMBRIDGE) for @bottom;
} else {
$_->surface_type(S_TYPE_BOTTOM) for @bottom;
}
}
# now, if the object contained a thin membrane, we could have overlapping bottom
@ -759,7 +729,7 @@ sub detect_surfaces_type {
# as bottom surfaces (to allow for bridge detection)
if (@top && @bottom) {
my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping)
Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->layer->id, scalar(@$overlapping)
if $Slic3r::debug;
@top = $difference->([map $_->expolygon, @top], $overlapping, S_TYPE_TOP);
}
@ -773,10 +743,10 @@ sub detect_surfaces_type {
# save surfaces to layer
$layerm->slices->clear;
$layerm->slices->append(@bottom, @top, @internal);
$layerm->slices->append($_) for (@bottom, @top, @internal);
Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
$layerm->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
$layerm->layer->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
}
# clip surfaces to the fill boundaries
@ -785,7 +755,8 @@ sub detect_surfaces_type {
# Note: this method should be idempotent, but fill_surfaces gets modified
# in place. However we're now only using its boundaries (which are invariant)
# so we're safe
# so we're safe. This guarantees idempotence of prepare_infill() also in case
# that combine_infill() turns some fill_surface into VOID surfaces.
my $fill_boundaries = [ map $_->clone->p, @{$layerm->fill_surfaces} ];
$layerm->fill_surfaces->clear;
foreach my $surface (@{$layerm->slices}) {
@ -793,14 +764,16 @@ sub detect_surfaces_type {
[ $surface->p ],
$fill_boundaries,
);
$layerm->fill_surfaces->append(map Slic3r::Surface->new
(expolygon => $_, surface_type => $surface->surface_type),
@$intersection);
$layerm->fill_surfaces->append($_)
for map Slic3r::Surface->new(expolygon => $_, surface_type => $surface->surface_type),
@$intersection;
}
}
}
}
# Idempotence of this method is guaranteed by the fact that we don't remove things from
# fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
sub clip_fill_surfaces {
my $self = shift;
return unless $self->config->infill_only_where_needed;
@ -808,138 +781,94 @@ sub clip_fill_surfaces {
# We only want infill under ceilings; this is almost like an
# internal support material.
my $additional_margin = scale 3*0;
my $overhangs = []; # arrayref of polygons
for my $layer_id (reverse 0..($self->layer_count - 1)) {
my $layer = $self->get_layer($layer_id);
my @layer_internal = (); # arrayref of Surface objects
my @new_internal = (); # arrayref of Surface objects
# proceed top-down skipping bottom layer
my $upper_internal = [];
for my $layer_id (reverse 1..($self->layer_count - 1)) {
my $layer = $self->get_layer($layer_id);
my $lower_layer = $self->get_layer($layer_id-1);
# clip this layer's internal surfaces to @overhangs
foreach my $layerm (@{$layer->regions}) {
# we assume that this step is run before bridge_over_infill() and combine_infill()
# so these are the only internal types we might have
# detect things that we need to support
my $overhangs = []; # Polygons
# we need to support any solid surface
push @$overhangs, map $_->p,
grep $_->is_solid, map @{$_->fill_surfaces}, @{$layer->regions};
# we also need to support perimeters when there's at least one full
# unsupported loop
{
# get perimeters area as the difference between slices and fill_surfaces
my $perimeters = diff(
[ map @$_, @{$layer->slices} ],
[ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
);
# only consider the area that is not supported by lower perimeters
$perimeters = intersection(
$perimeters,
[ map $_->p, map @{$_->fill_surfaces}, @{$lower_layer->regions} ],
1,
);
# only consider perimeter areas that are at least one extrusion width thick
my $pw = min(map $_->flow(FLOW_ROLE_PERIMETER)->scaled_width, @{$layer->regions});
$perimeters = offset2($perimeters, -$pw, +$pw);
# append such thick perimeters to the areas that need support
push @$overhangs, @$perimeters;
}
# find new internal infill
$upper_internal = my $new_internal = intersection(
[
@$overhangs,
@$upper_internal,
],
[
# our current internal fill boundaries
map $_->p,
grep $_->surface_type == S_TYPE_INTERNAL || $_->surface_type == S_TYPE_INTERNALVOID,
map @{$_->fill_surfaces}, @{$lower_layer->regions}
],
);
# apply new internal infill to regions
foreach my $layerm (@{$lower_layer->regions}) {
my (@internal, @other) = ();
foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
$surface->surface_type == S_TYPE_INTERNAL
? push @internal, $surface
: push @other, $surface;
if ($surface->surface_type == S_TYPE_INTERNAL || $surface->surface_type == S_TYPE_INTERNALVOID) {
push @internal, $surface;
} else {
push @other, $surface;
}
}
# keep all the original internal surfaces to detect overhangs in this layer
push @layer_internal, @internal;
push @new_internal, my @new = map Slic3r::Surface->new(
my @new = map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNAL,
),
@{intersection_ex(
$overhangs,
[ map $_->p, @internal ],
)};
@{intersection_ex(
[ map $_->p, @internal ],
$new_internal,
1,
)};
push @other, map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNALVOID,
),
@{diff_ex(
[ map $_->p, @internal ],
$new_internal,
1,
)};
# If there are voids it means that our internal infill is not adjacent to
# perimeters. In this case it would be nice to add a loop around infill to
# make it more robust and nicer. TODO.
$layerm->fill_surfaces->clear;
$layerm->fill_surfaces->append(@new, @other);
}
# get this layer's overhangs defined as the full slice minus the internal infill
# (thus we also consider perimeters)
if ($layer_id > 0) {
my $solid = diff(
[ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
[ map $_->p, @layer_internal ],
);
$overhangs = offset($solid, +$additional_margin);
push @$overhangs, map $_->p, @new_internal; # propagate upper overhangs
}
}
}
sub bridge_over_infill {
my $self = shift;
for my $region_id (0..($self->print->region_count - 1)) {
my $fill_density = $self->print->regions->[$region_id]->config->fill_density;
next if $fill_density == 100 || $fill_density == 0;
for my $layer_id (1..($self->layer_count - 1)) {
my $layer = $self->get_layer($layer_id);
my $layerm = $layer->regions->[$region_id];
my $lower_layer = $self->get_layer($layer_id-1);
# compute the areas needing bridge math
my @internal_solid = @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNALSOLID)};
my @lower_internal = map @{$_->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)}, @{$lower_layer->regions};
my $to_bridge = intersection_ex(
[ map $_->p, @internal_solid ],
[ map $_->p, @lower_internal ],
);
next unless @$to_bridge;
Slic3r::debugf "Bridging %d internal areas at layer %d\n", scalar(@$to_bridge), $layer_id;
# build the new collection of fill_surfaces
{
my @new_surfaces = map $_->clone, grep $_->surface_type != S_TYPE_INTERNALSOLID, @{$layerm->fill_surfaces};
push @new_surfaces, map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNALBRIDGE,
), @$to_bridge;
push @new_surfaces, map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNALSOLID,
), @{diff_ex(
[ map $_->p, @internal_solid ],
[ map @$_, @$to_bridge ],
1,
)};
$layerm->fill_surfaces->clear;
$layerm->fill_surfaces->append(@new_surfaces);
}
# exclude infill from the layers below if needed
# see discussion at https://github.com/alexrj/Slic3r/issues/240
# Update: do not exclude any infill. Sparse infill is able to absorb the excess material.
if (0) {
my $excess = $layerm->extruders->{infill}->bridge_flow->width - $layerm->height;
for (my $i = $layer_id-1; $excess >= $self->get_layer($i)->height; $i--) {
Slic3r::debugf " skipping infill below those areas at layer %d\n", $i;
foreach my $lower_layerm (@{$self->get_layer($i)->regions}) {
my @new_surfaces = ();
# subtract the area from all types of surfaces
foreach my $group (@{$lower_layerm->fill_surfaces->group}) {
push @new_surfaces, map $group->[0]->clone(expolygon => $_),
@{diff_ex(
[ map $_->p, @$group ],
[ map @$_, @$to_bridge ],
)};
push @new_surfaces, map Slic3r::Surface->new(
expolygon => $_,
surface_type => S_TYPE_INTERNALVOID,
), @{intersection_ex(
[ map $_->p, @$group ],
[ map @$_, @$to_bridge ],
)};
}
$lower_layerm->fill_surfaces->clear;
$lower_layerm->fill_surfaces->append(@new_surfaces);
}
$excess -= $self->get_layer($i)->height;
}
}
}
}
}
sub process_external_surfaces {
my ($self) = @_;
for my $region_id (0 .. ($self->print->region_count-1)) {
$self->get_layer(0)->regions->[$region_id]->process_external_surfaces(undef);
for my $i (1 .. ($self->layer_count - 1)) {
$self->get_layer($i)->regions->[$region_id]->process_external_surfaces($self->get_layer($i-1));
$layerm->fill_surfaces->append($_) for (@new, @other);
}
}
}
@ -953,9 +882,10 @@ sub discover_horizontal_shells {
for (my $i = 0; $i < $self->layer_count; $i++) {
my $layerm = $self->get_layer($i)->regions->[$region_id];
if ($layerm->config->solid_infill_every_layers && $layerm->config->fill_density > 0
&& ($i % $layerm->config->solid_infill_every_layers) == 0) {
$_->surface_type(S_TYPE_INTERNALSOLID) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
if ($layerm->region->config->solid_infill_every_layers && $layerm->region->config->fill_density > 0
&& ($i % $layerm->region->config->solid_infill_every_layers) == 0) {
my $type = $layerm->region->config->fill_density == 100 ? S_TYPE_INTERNALSOLID : S_TYPE_INTERNALBRIDGE;
$_->surface_type($type) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
}
EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE) {
@ -976,8 +906,8 @@ sub discover_horizontal_shells {
Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
my $solid_layers = ($type == S_TYPE_TOP)
? $layerm->config->top_solid_layers
: $layerm->config->bottom_solid_layers;
? $layerm->region->config->top_solid_layers
: $layerm->region->config->bottom_solid_layers;
NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1;
abs($n - $i) <= $solid_layers-1;
($type == S_TYPE_TOP) ? $n-- : $n++) {
@ -1005,7 +935,7 @@ sub discover_horizontal_shells {
);
next EXTERNAL if !@$new_internal_solid;
if ($layerm->config->fill_density == 0) {
if ($layerm->region->config->fill_density == 0) {
# if we're printing a hollow object we discard any solid shell thinner
# than a perimeter width, since it's probably just crossing a sloping wall
# and it's not wanted in a hollow print even if it would make sense when
@ -1045,7 +975,12 @@ sub discover_horizontal_shells {
# make sure our grown surfaces don't exceed the fill area
my @grown = @{intersection(
offset($too_narrow, +$margin),
[ map $_->p, @neighbor_fill_surfaces ],
# Discard bridges as they are grown for anchoring and we can't
# remove such anchors. (This may happen when a bridge is being
# anchored onto a wall where little space remains after the bridge
# is grown, and that little space is an internal solid shell so
# it triggers this too_narrow logic.)
[ map $_->p, grep { $_->is_internal && !$_->is_bridge } @neighbor_fill_surfaces ],
)};
$new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
}
@ -1069,12 +1004,14 @@ sub discover_horizontal_shells {
# assign resulting internal surfaces to layer
$neighbor_fill_surfaces->clear;
$neighbor_fill_surfaces->append(map Slic3r::Surface->new
(expolygon => $_, surface_type => S_TYPE_INTERNAL), @$internal);
$neighbor_fill_surfaces->append($_)
for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL),
@$internal;
# assign new internal-solid surfaces to layer
$neighbor_fill_surfaces->append(map Slic3r::Surface->new
(expolygon => $_, surface_type => S_TYPE_INTERNALSOLID), @$internal_solid);
$neighbor_fill_surfaces->append($_)
for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALSOLID),
@$internal_solid;
# assign top and bottom surfaces to layer
foreach my $s (@{Slic3r::Surface::Collection->new(grep { ($_->surface_type == S_TYPE_TOP) || $_->is_bottom } @neighbor_fill_surfaces)->group}) {
@ -1083,7 +1020,8 @@ sub discover_horizontal_shells {
[ map @$_, @$internal_solid, @$internal ],
1,
);
$neighbor_fill_surfaces->append(map $s->[0]->clone(expolygon => $_), @$solid_surfaces);
$neighbor_fill_surfaces->append($_)
for map $s->[0]->clone(expolygon => $_), @$solid_surfaces;
}
}
}
@ -1092,42 +1030,60 @@ sub discover_horizontal_shells {
}
# combine fill surfaces across layers
# Idempotence of this method is guaranteed by the fact that we don't remove things from
# fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
sub combine_infill {
my $self = shift;
return unless defined first { $_->config->infill_every_layers > 1 && $_->config->fill_density > 0 } @{$self->print->regions};
my @layer_heights = map $_->height, @{$self->layers};
# define the type used for voids
my %voidtype = (
&S_TYPE_INTERNAL() => S_TYPE_INTERNALVOID,
);
# work on each region separately
for my $region_id (0 .. ($self->print->region_count-1)) {
my $region = $self->print->regions->[$region_id];
my $region = $self->print->get_region($region_id);
my $every = $region->config->infill_every_layers;
next unless $every > 1 && $region->config->fill_density > 0;
# limit the number of combined layers to the maximum height allowed by this regions' nozzle
my $nozzle_diameter = $self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1);
my $nozzle_diameter = min(
$self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1),
$self->print->config->get_at('nozzle_diameter', $region->config->solid_infill_extruder-1),
);
# define the combinations
my @combine = (); # layer_id => thickness in layers
my %combine = (); # layer_idx => number of additional combined lower layers
{
my $current_height = my $layers = 0;
for my $layer_id (1 .. $#layer_heights) {
my $height = $self->get_layer($layer_id)->height;
for my $layer_idx (0 .. ($self->layer_count-1)) {
my $layer = $self->get_layer($layer_idx);
next if $layer->id == 0; # skip first print layer (which may not be first layer in array because of raft)
my $height = $layer->height;
if ($current_height + $height >= $nozzle_diameter || $layers >= $every) {
$combine[$layer_id-1] = $layers;
# check whether the combination of this layer with the lower layers' buffer
# would exceed max layer height or max combined layer count
if ($current_height + $height >= $nozzle_diameter + epsilon || $layers >= $every) {
# append combination to lower layer
$combine{$layer_idx-1} = $layers;
$current_height = $layers = 0;
}
$current_height += $height;
$layers++;
}
# append lower layers (if any) to uppermost layer
$combine{$self->layer_count-1} = $layers;
}
# skip bottom layer
for my $layer_id (1 .. $#combine) {
next unless ($combine[$layer_id] // 1) > 1;
my @layerms = map $self->get_layer($_)->regions->[$region_id],
($layer_id - ($combine[$layer_id]-1) .. $layer_id);
# loop through layers to which we have assigned layers to combine
for my $layer_idx (sort keys %combine) {
next unless $combine{$layer_idx} > 1;
# get all the LayerRegion objects to be combined
my @layerms = map $self->get_layer($_)->get_region($region_id),
($layer_idx - ($combine{$layer_idx}-1) .. $layer_idx);
# only combine internal infill
for my $type (S_TYPE_INTERNAL) {
@ -1150,7 +1106,7 @@ sub combine_infill {
Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
scalar(@$intersection),
($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
$layer_id-($every-1), $layer_id;
$layer_idx-($every-1), $layer_idx;
# $intersection now contains the regions that can be combined across the full amount of layers
# so let's remove those areas from all layers
@ -1160,8 +1116,8 @@ sub combine_infill {
+ $layerms[-1]->flow(FLOW_ROLE_PERIMETER)->scaled_width / 2
# Because fill areas for rectilinear and honeycomb are grown
# later to overlap perimeters, we need to counteract that too.
+ (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|honeycomb)/)
? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width * &Slic3r::INFILL_OVERLAP_OVER_SPACING
+ (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|grid|line|honeycomb)/)
? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width
: 0)
)}, @$intersection;
@ -1177,19 +1133,19 @@ sub combine_infill {
)};
# apply surfaces back with adjusted depth to the uppermost layer
if ($layerm->id == $layer_id) {
if ($layerm->layer->id == $self->get_layer($layer_idx)->id) {
push @new_this_type,
map Slic3r::Surface->new(
expolygon => $_,
surface_type => $type,
thickness => sum(map $_->height, @layerms),
thickness => sum(map $_->layer->height, @layerms),
thickness_layers => scalar(@layerms),
),
@$intersection;
} else {
# save void surfaces
push @this_type,
map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALVOID),
push @new_this_type,
map Slic3r::Surface->new(expolygon => $_, surface_type => $voidtype{$type}),
@{intersection_ex(
[ map @{$_->expolygon}, @this_type ],
[ @intersection_with_clearance ],
@ -1197,7 +1153,7 @@ sub combine_infill {
}
$layerm->fill_surfaces->clear;
$layerm->fill_surfaces->append(@new_this_type, @other_types);
$layerm->fill_surfaces->append($_) for (@new_this_type, @other_types);
}
}
}

View File

@ -38,7 +38,7 @@ has 'status_cb' => (
has 'print_center' => (
is => 'rw',
default => sub { [100,100] },
default => sub { Slic3r::Pointf->new(100,100) },
);
has 'output_file' => (
@ -68,6 +68,7 @@ sub set_model {
# if all input objects have defined position(s) apply duplication to the whole model
$model->duplicate($self->duplicate, $self->_print->config->min_object_distance);
}
$_->translate(0,0,-$_->bounding_box->z_min) for @{$model->objects};
$model->center_instances_around_point($self->print_center);
foreach my $model_object (@{$model->objects}) {

View File

@ -4,7 +4,7 @@ use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(STEP_INIT_EXTRUDERS STEP_SLICE STEP_PERIMETERS STEP_PREPARE_INFILL
our @EXPORT_OK = qw(STEP_SLICE STEP_PERIMETERS STEP_PREPARE_INFILL
STEP_INFILL STEP_SUPPORTMATERIAL STEP_SKIRT STEP_BRIM);
our %EXPORT_TAGS = (steps => \@EXPORT_OK);

View File

@ -4,7 +4,7 @@ use Moo;
use List::Util qw(sum min max);
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Flow ':roles';
use Slic3r::Geometry qw(scale scaled_epsilon PI rad2deg deg2rad convex_hull);
use Slic3r::Geometry qw(epsilon scale scaled_epsilon PI rad2deg deg2rad convex_hull);
use Slic3r::Geometry::Clipper qw(offset diff union union_ex intersection offset_ex offset2
intersection_pl offset2_ex diff_pl);
use Slic3r::Surface ':types';
@ -17,9 +17,6 @@ has 'interface_flow' => (is => 'rw', required => 1);
use constant DEBUG_CONTACT_ONLY => 0;
# how much we extend support around the actual contact area
use constant MARGIN => 1.5;
# increment used to reach MARGIN in steps to avoid trespassing thin objects
use constant MARGIN_STEP => MARGIN/3;
@ -69,13 +66,17 @@ sub generate {
$self->clip_with_object($base, $support_z, $object);
$self->clip_with_shape($base, $shape) if @$shape;
# Detect what part of base support layers are "reverse interfaces" because they
# lie above object's top surfaces.
$self->generate_bottom_interface_layers($support_z, $base, $top, $interface);
# Install support layers into object.
for my $i (0 .. $#$support_z) {
$object->add_support_layer(
$i, # id
($i == 0) ? $support_z->[$i] : ($support_z->[$i] - $support_z->[$i-1]), # height
$support_z->[$i], # print_z
-1); # slice_z
);
if ($i >= 1) {
$object->support_layers->[-2]->set_upper_layer($object->support_layers->[-1]);
$object->support_layers->[-1]->set_lower_layer($object->support_layers->[-2]);
@ -177,7 +178,7 @@ sub contact_area {
# TODO: split_at_first_point() could split a bridge mid-way
my @overhang_perimeters =
map { $_->isa('Slic3r::ExtrusionLoop') ? $_->polygon->split_at_first_point : $_->polyline->clone }
@{$layerm->perimeters};
map @$_, @{$layerm->perimeters};
# workaround for Clipper bug, see Slic3r::Polygon::clip_as_polyline()
$_->[0]->translate(1,0) for @overhang_perimeters;
@ -234,7 +235,7 @@ sub contact_area {
# just remove bridged areas
$diff = diff(
$diff,
[ map @$_, @{$layerm->bridged} ],
$layerm->bridged,
1,
);
}
@ -265,15 +266,14 @@ sub contact_area {
{
# get the average nozzle diameter used on this layer
my @nozzle_diameters = map $self->print_config->get_at('nozzle_diameter', $_),
map { $_->config->perimeter_extruder-1, $_->config->infill_extruder-1 }
@{$layer->regions};
map { $_->config->perimeter_extruder-1, $_->config->infill_extruder-1, $_->config->solid_infill_extruder-1 }
map $_->region, @{$layer->regions};
my $nozzle_diameter = sum(@nozzle_diameters)/@nozzle_diameters;
my $contact_z = $layer->print_z - contact_distance($nozzle_diameter);
###$contact_z = $layer->print_z - $layer->height;
my $contact_z = $layer->print_z - $self->contact_distance($layer->height, $nozzle_diameter);
# ignore this contact area if it's too low
next if $contact_z < $self->object_config->get_value('first_layer_height');
next if $contact_z < $self->object_config->get_value('first_layer_height') - epsilon;
$contact{$contact_z} = [ @contact ];
$overhang{$contact_z} = [ @overhang ];
@ -338,7 +338,13 @@ sub support_layers_z {
# layer_height > nozzle_diameter * 0.75
my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $self->object_config->support_material_extruder-1);
my $support_material_height = max($max_object_layer_height, $nozzle_diameter * 0.75);
my @z = sort { $a <=> $b } @$contact_z, @$top_z, (map $_ + $nozzle_diameter, @$top_z);
my $contact_distance = $self->contact_distance($support_material_height, $nozzle_diameter);
# initialize known, fixed, support layers
my @z = sort { $a <=> $b }
@$contact_z,
@$top_z, # TODO: why we have this?
(map $_ + $contact_distance, @$top_z);
# enforce first layer height
my $first_layer_height = $self->object_config->get_value('first_layer_height');
@ -350,13 +356,16 @@ sub support_layers_z {
if ($self->object_config->raft_layers > 1 && @z >= 2) {
# $z[1] is last raft layer (contact layer for the first layer object)
my $height = ($z[1] - $z[0]) / ($self->object_config->raft_layers - 1);
# since we already have two raft layers ($z[0] and $z[1]) we need to insert
# raft_layers-2 more
splice @z, 1, 0,
map { sprintf "%.2f", $_ }
map { $z[0] + $height * $_ }
0..($self->object_config->raft_layers - 1);
1..($self->object_config->raft_layers - 2);
}
for (my $i = $#z; $i >= 0; $i--) {
# create other layers (skip raft layers as they're already done and use thicker layers)
for (my $i = $#z; $i >= $self->object_config->raft_layers; $i--) {
my $target_height = $support_material_height;
if ($i > 0 && $top{ $z[$i-1] }) {
$target_height = $nozzle_diameter;
@ -418,6 +427,50 @@ sub generate_interface_layers {
return \%interface;
}
sub generate_bottom_interface_layers {
my ($self, $support_z, $base, $top, $interface) = @_;
my $area_threshold = $self->interface_flow->scaled_spacing ** 2;
# loop through object's top surfaces
foreach my $top_z (sort keys %$top) {
my $this = $top->{$top_z};
# keep a count of the interface layers we generated for this top surface
my $interface_layers = 0;
# loop through support layers until we find the one(s) right above the top
# surface
foreach my $layer_id (0 .. $#$support_z) {
my $z = $support_z->[$layer_id];
next unless $z > $top_z;
if ($base->{$layer_id}) {
# get the support material area that should be considered interface
my $interface_area = intersection(
$base->{$layer_id},
$this,
);
# discard too small areas
$interface_area = [ grep abs($_->area) >= $area_threshold, @$interface_area ];
# subtract new interface area from base
$base->{$layer_id} = diff(
$base->{$layer_id},
$interface_area,
);
# add new interface area to interface
push @{$interface->{$layer_id}}, @$interface_area;
}
$interface_layers++;
last if $interface_layers == $self->object_config->support_material_interface_layers;
}
}
}
sub generate_base_layers {
my ($self, $support_z, $contact, $interface, $top) = @_;
@ -521,6 +574,12 @@ sub generate_toolpaths {
my $layer = $object->support_layers->[$layer_id];
my $z = $layer->print_z;
# we redefine flows locally by applying this layer's height
my $_flow = $flow->clone;
my $_interface_flow = $interface_flow->clone;
$_flow->set_height($layer->height);
$_interface_flow->set_height($layer->height);
my $overhang = $overhang->{$z} || [];
my $contact = $contact->{$z} || [];
my $interface = $interface->{$layer_id} || [];
@ -552,7 +611,7 @@ sub generate_toolpaths {
# generate the outermost loop
# find centerline of the external loop (or any other kind of extrusions should the loop be skipped)
$contact = offset($contact, -$interface_flow->scaled_width/2);
$contact = offset($contact, -$_interface_flow->scaled_width/2);
my @loops0 = ();
{
@ -561,7 +620,7 @@ sub generate_toolpaths {
# only consider the loops facing the overhang
{
my $overhang_with_margin = offset($overhang, +$interface_flow->scaled_width/2);
my $overhang_with_margin = offset($overhang, +$_interface_flow->scaled_width/2);
@external_loops = grep {
@{intersection_pl(
[ $_->split_at_first_point ],
@ -581,8 +640,8 @@ sub generate_toolpaths {
# make more loops
my @loops = @loops0;
for my $i (2..$contact_loops) {
my $d = ($i-1) * $interface_flow->scaled_spacing;
push @loops, @{offset2(\@loops0, -$d -0.5*$interface_flow->scaled_spacing, +0.5*$interface_flow->scaled_spacing)};
my $d = ($i-1) * $_interface_flow->scaled_spacing;
push @loops, @{offset2(\@loops0, -$d -0.5*$_interface_flow->scaled_spacing, +0.5*$_interface_flow->scaled_spacing)};
}
# clip such loops to the side oriented towards the object
@ -602,12 +661,12 @@ sub generate_toolpaths {
);
# transform loops into ExtrusionPath objects
my $mm3_per_mm = $interface_flow->mm3_per_mm;
my $mm3_per_mm = $_interface_flow->mm3_per_mm;
@loops = map Slic3r::ExtrusionPath->new(
polyline => $_,
role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
mm3_per_mm => $mm3_per_mm,
width => $interface_flow->width,
width => $_interface_flow->width,
height => $layer->height,
), @loops;
@ -617,9 +676,10 @@ sub generate_toolpaths {
# interface and contact infill
if (@$interface || @$contact_infill) {
$fillers{interface}->angle($interface_angle);
$fillers{interface}->spacing($_interface_flow->spacing);
# find centerline of the external loop
$interface = offset2($interface, +scaled_epsilon, -(scaled_epsilon + $interface_flow->scaled_width/2));
$interface = offset2($interface, +scaled_epsilon, -(scaled_epsilon + $_interface_flow->scaled_width/2));
# join regions by offsetting them to ensure they're merged
$interface = offset([ @$interface, @$contact_infill ], scaled_epsilon);
@ -642,20 +702,19 @@ sub generate_toolpaths {
my @paths = ();
foreach my $expolygon (@{union_ex($interface)}) {
my ($params, @p) = $fillers{interface}->fill_surface(
my @p = $fillers{interface}->fill_surface(
Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
density => $interface_density,
flow => $interface_flow,
density => $interface_density,
layer_height => $layer->height,
complete => 1,
complete => 1,
);
my $mm3_per_mm = $params->{flow}->mm3_per_mm;
my $mm3_per_mm = $_interface_flow->mm3_per_mm;
push @paths, map Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polyline->new(@$_),
role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
mm3_per_mm => $mm3_per_mm,
width => $params->{flow}->width,
width => $_interface_flow->width,
height => $layer->height,
), @p;
}
@ -667,11 +726,16 @@ sub generate_toolpaths {
if (@$base) {
my $filler = $fillers{support};
$filler->angle($angles[ ($layer_id) % @angles ]);
# We don't use $base_flow->spacing because we need a constant spacing
# value that guarantees that all layers are correctly aligned.
$filler->spacing($flow->spacing);
my $density = $support_density;
my $base_flow = $flow;
my $base_flow = $_flow;
# find centerline of the external loop/extrusions
my $to_infill = offset2_ex($base, +scaled_epsilon, -(scaled_epsilon + $flow->scaled_width/2));
my $to_infill = offset2_ex($base, +scaled_epsilon, -(scaled_epsilon + $_flow->scaled_width/2));
my @paths = ();
@ -681,37 +745,40 @@ sub generate_toolpaths {
$filler->angle($self->object_config->support_material_angle + 90);
$density = 0.5;
$base_flow = $self->first_layer_flow;
# use the proper spacing for first layer as we don't need to align
# its pattern to the other layers
$filler->spacing($base_flow->spacing);
} else {
# draw a perimeter all around support infill
# TODO: use brim ordering algorithm
my $mm3_per_mm = $flow->mm3_per_mm;
my $mm3_per_mm = $_flow->mm3_per_mm;
push @paths, map Slic3r::ExtrusionPath->new(
polyline => $_->split_at_first_point,
role => EXTR_ROLE_SUPPORTMATERIAL,
mm3_per_mm => $mm3_per_mm,
width => $flow->width,
width => $_flow->width,
height => $layer->height,
), map @$_, @$to_infill;
# TODO: use offset2_ex()
$to_infill = offset_ex([ map @$_, @$to_infill ], -$flow->scaled_spacing);
$to_infill = offset_ex([ map @$_, @$to_infill ], -$_flow->scaled_spacing);
}
my $mm3_per_mm = $base_flow->mm3_per_mm;
foreach my $expolygon (@$to_infill) {
my ($params, @p) = $filler->fill_surface(
my @p = $filler->fill_surface(
Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
density => $density,
flow => $base_flow,
layer_height => $layer->height,
complete => 1,
);
my $mm3_per_mm = $params->{flow}->mm3_per_mm;
push @paths, map Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polyline->new(@$_),
role => EXTR_ROLE_SUPPORTMATERIAL,
mm3_per_mm => $mm3_per_mm,
width => $params->{flow}->width,
width => $base_flow->width,
height => $layer->height,
), @p;
}
@ -850,10 +917,15 @@ sub overlapping_layers {
} 0..$#$support_z;
}
# class method
sub contact_distance {
my ($nozzle_diameter) = @_;
return $nozzle_diameter * 1.5;
my ($self, $layer_height, $nozzle_diameter) = @_;
my $extra = $self->object_config->support_material_contact_distance;
if ($extra == 0) {
return $layer_height;
} else {
return $nozzle_diameter + $extra;
}
}
1;

View File

@ -34,6 +34,13 @@ sub mesh {
$facets = [
[0,1,2],[2,1,3],[1,0,4],[5,1,4],[6,7,4],[8,2,9],[0,2,8],[10,8,9],[0,8,6],[0,6,4],[4,7,9],[7,10,9],[2,3,9],[9,3,11],[12,1,5],[13,3,12],[14,12,5],[3,1,12],[11,3,13],[11,15,5],[11,13,15],[15,14,5],[5,4,9],[11,5,9],[8,13,12],[6,8,12],[10,15,13],[8,10,13],[15,10,14],[14,10,7],[14,7,12],[12,7,6]
],
} elsif ($name eq 'cube_with_concave_hole') {
$vertices = [
[-10,-10,-5],[-10,-10,5],[-10,10,-5],[-10,10,5],[10,-10,-5],[10,-10,5],[-5,-5,-5],[5,-5,-5],[5,5,-5],[5,10,-5],[-5,5,-5],[3.06161699911402e-16,5,-5],[5,0,-5],[0,0,-5],[10,5,-5],[5,10,5],[-5,-5,5],[5,0,5],[5,-5,5],[-5,5,5],[10,5,5],[5,5,5],[3.06161699911402e-16,5,5],[0,0,5]
];
$facets = [
[0,1,2],[2,1,3],[1,0,4],[5,1,4],[6,7,4],[8,2,9],[10,2,11],[11,12,13],[0,2,10],[0,10,6],[0,6,4],[11,2,8],[4,7,12],[4,12,8],[12,11,8],[14,4,8],[2,3,9],[9,3,15],[16,1,5],[17,18,5],[19,3,16],[20,21,5],[18,16,5],[3,1,16],[22,3,19],[21,3,22],[21,17,5],[21,22,17],[21,15,3],[23,17,22],[5,4,14],[20,5,14],[20,14,21],[21,14,8],[9,15,21],[8,9,21],[10,19,16],[6,10,16],[11,22,19],[10,11,19],[13,23,11],[11,23,22],[23,13,12],[17,23,12],[17,12,18],[18,12,7],[18,7,16],[16,7,6]
],
} elsif ($name eq 'V') {
$vertices = [
[-14,0,20],[-14,15,20],[0,0,0],[0,15,0],[-4,0,20],[-4,15,20],[5,0,7.14286],[10,0,0],[24,0,20],[14,0,20],[10,15,0],[5,15,7.14286],[14,15,20],[24,15,20]
@ -111,6 +118,13 @@ sub mesh {
$facets = [
[0,1,2],[3,4,5],[2,1,4],[2,4,3],[2,3,5],[2,5,0],[5,4,1],[5,1,0]
];
} elsif ($name eq 'bridge') {
$vertices = [
[75,84.5,8],[125,84.5,8],[75,94.5,8],[120,84.5,5],[125,94.5,8],[75,84.5,0],[80,84.5,5],[125,84.5,0],[125,94.5,0],[80,94.5,5],[75,94.5,0],[120,94.5,5],[120,84.5,0],[80,94.5,0],[80,84.5,0],[120,94.5,0]
];
$facets = [
[0,1,2],[1,0,3],[2,1,4],[2,5,0],[0,6,3],[1,3,7],[1,8,4],[4,9,2],[10,5,2],[5,6,0],[6,11,3],[3,12,7],[7,8,1],[4,8,11],[4,11,9],[9,10,2],[10,13,5],[14,6,5],[9,11,6],[11,12,3],[12,8,7],[11,8,15],[13,10,9],[5,13,14],[14,13,6],[6,13,9],[15,12,11],[15,8,12]
];
} elsif ($name eq 'slopy_cube') {
$vertices = [
[-10,-10,0], [-10,-10,20], [-10,10,0], [-10,10,20], [0,-10,10], [10,-10,0], [2.92893,-10,10], [10,-10,2.92893],
@ -128,7 +142,7 @@ sub mesh {
my $mesh = Slic3r::TriangleMesh->new;
$mesh->ReadFromPerl($vertices, $facets);
$mesh->repair;
$mesh->scale_xyz($params{scale_xyz}) if $params{scale_xyz};
$mesh->scale_xyz(Slic3r::Pointf3->new(@{$params{scale_xyz}})) if $params{scale_xyz};
$mesh->translate(@{$params{translate}}) if $params{translate};
return $mesh;
}
@ -166,7 +180,7 @@ sub init_print {
$model->duplicate($params{duplicate} // 1, $print->config->min_object_distance);
}
$model->arrange_objects($print->config->min_object_distance);
$model->center_instances_around_point($params{print_center} // [100,100]);
$model->center_instances_around_point($params{print_center} ? Slic3r::Pointf->new(@{$params{print_center}}) : Slic3r::Pointf->new(100,100));
foreach my $model_object (@{$model->objects}) {
$print->auto_assign_extruders($model_object);
$print->add_model_object($model_object);

View File

@ -1,72 +1,66 @@
package Slic3r::Test::SectionCut;
use Moo;
use List::Util qw(first max);
use Slic3r::Geometry qw(X Y A B X1 Y1 X2 Y2 unscale);
use Slic3r::Geometry::Clipper qw(union_ex intersection_pl);
use List::Util qw(first min max);
use Slic3r::Geometry qw(unscale);
use Slic3r::Geometry::Clipper qw(intersection_pl);
use SVG;
use Slic3r::SVG;
use Slic3r::SVG;
has 'scale' => (is => 'ro', default => sub {30});
has 'print' => (is => 'ro', required => 1);
has 'y_percent' => (is => 'ro', default => sub {0.5});
has 'line' => (is => 'rw');
has 'height' => (is => 'rw');
has 'print' => (is => 'ro', required => 1);
has 'scale' => (is => 'ro', default => sub { 30 });
has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor
has 'line' => (is => 'rw');
has '_height' => (is => 'rw');
has '_svg' => (is => 'rw');
has '_svg_style' => (is => 'rw', default => sub { {} });
sub BUILD {
my $self = shift;
# calculate the Y coordinate of the section line
my $bb = $self->print->bounding_box;
my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
my $line = Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]);
$self->line($line);
# store our section line
$self->line(Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]));
}
sub export_svg {
my $self = shift;
my ($filename) = @_;
my $print_size = $self->print->size;
$self->height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
my $svg = SVG->new(
width => $self->scale * unscale($print_size->[X]),
height => $self->scale * $self->height,
);
# get bounding box of print and its height
# (Print should return a BoundingBox3 object instead)
my $bb = $self->print->bounding_box;
my $print_size = $bb->size;
$self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
my $group = sub {
my %p = @_;
my $g = $svg->group(style => $p{style});
my $items = $self->_plot($p{filter});
$g->rectangle(%$_) for @{ $items->{rectangles} };
$g->circle(%$_) for @{ $items->{circles} };
};
# initialize the SVG canvas
$self->_svg(my $svg = SVG->new(
width => $self->scale * unscale($print_size->x),
height => $self->scale * $self->_height,
));
$group->(
filter => sub { map @{$_->perimeters}, @{$_[0]->regions} },
style => {
'stroke-width' => 1,
'stroke' => 'grey',
'fill' => 'red',
},
);
# set default styles
$self->_svg_style->{'stroke-width'} = 1;
$self->_svg_style->{'fill-opacity'} = 0.5;
$self->_svg_style->{'stroke-opacity'} = 0.2;
$group->(
filter => sub { map @{$_->fills}, @{$_[0]->regions} },
style => {
'stroke-width' => 1,
'stroke' => '#444444',
'fill' => 'grey',
},
);
# plot perimeters
$self->_svg_style->{'stroke'} = '#EE0000';
$self->_svg_style->{'fill'} = '#FF0000';
$self->_plot_group(sub { map @{$_->perimeters}, @{$_[0]->regions} });
$group->(
filter => sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills, $_[0]->support_interface_fills) : () },
style => {
'stroke-width' => 1,
'stroke' => '#444444',
'fill' => '#22FF00',
},
);
# plot infill
$self->_svg_style->{'stroke'} = '#444444';
$self->_svg_style->{'fill'} = '#454545';
$self->_plot_group(sub { map @{$_->fills}, @{$_[0]->regions} });
# plot support material
$self->_svg_style->{'stroke'} = '#12EF00';
$self->_svg_style->{'fill'} = '#22FF00';
$self->_plot_group(sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills, $_[0]->support_interface_fills) : () });
Slic3r::open(\my $fh, '>', $filename);
print $fh $svg->xmlify;
@ -74,34 +68,36 @@ sub export_svg {
printf "Section cut SVG written to %s\n", $filename;
}
sub _plot {
sub _plot_group {
my $self = shift;
my ($filter) = @_;
my (@rectangles, @circles) = ();
my $bb = $self->print->bounding_box;
my $g = $self->_svg->group(style => { %{$self->_svg_style} });
foreach my $object (@{$self->print->objects}) {
foreach my $copy (@{$object->_shifted_copies}) {
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
# get all ExtrusionPath objects
my @paths =
map { $_->isa('Slic3r::ExtrusionLoop') ? $_->split_at_first_point : $_->clone }
map { $_->isa('Slic3r::ExtrusionPath::Collection') ? @$_ : $_ }
my @paths = map $_->clone,
map { ($_->isa('Slic3r::ExtrusionLoop') || $_->isa('Slic3r::ExtrusionPath::Collection')) ? @$_ : $_ }
grep defined $_,
$filter->($layer);
# move paths to location of copy
$_->polyline->translate(@$copy) for @paths;
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"line.svg",
no_arrows => 1,
#polygon => $line->grow(Slic3r::Geometry::scale $path->width/2),
polygons => [ $object->bounding_box->polygon ],
lines => [ $self->line ],
red_polylines => [ map $_->polyline, @paths ],
);
exit;
if (0) {
# export plan with section line and exit
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"line.svg",
no_arrows => 1,
lines => [ $self->line ],
red_polylines => [ map $_->polyline, @paths ],
);
exit;
}
foreach my $path (@paths) {
foreach my $line (@{$path->lines}) {
@ -110,61 +106,69 @@ sub _plot {
$line->grow(Slic3r::Geometry::scale $path->width/2),
)};
die "Intersection has more than two points!\n" if first { @$_ > 2 } @intersections;
die "Intersection has more than two points!\n"
if defined first { @$_ > 2 } @intersections;
# turn intersections to lines
my @lines = map Slic3r::Line->new(@$_), @intersections;
# align intersections to canvas
$_->translate(-$bb->x_min, 0) for @lines;
# we want lines oriented from left to right in order to draw
# rectangles correctly
foreach my $line (@lines) {
$line->reverse if $line->a->x > $line->b->x;
}
if ($path->is_bridge) {
foreach my $line (@intersections) {
foreach my $line (@lines) {
my $radius = $path->width / 2;
my $width = unscale abs($line->[B][X] - $line->[A][X]);
if ((10 * Slic3r::Geometry::scale $radius) < $width) {
my $width = unscale abs($line->b->x - $line->a->x);
if ((10 * $radius) < $width) {
# we're cutting the path in the longitudinal direction, so we've got a rectangle
push @rectangles, {
'x' => $self->scale * unscale $line->[A][X],
$g->rectangle(
'x' => $self->scale * unscale($line->a->x),
'y' => $self->scale * $self->_y($layer->print_z),
'width' => $self->scale * $width,
'height' => $self->scale * $radius * 2,
'rx' => $self->scale * $radius * 0.35,
'ry' => $self->scale * $radius * 0.35,
};
);
} else {
push @circles, {
'cx' => $self->scale * (unscale($line->[A][X]) + $radius),
$g->circle(
'cx' => $self->scale * (unscale($line->a->x) + $radius),
'cy' => $self->scale * $self->_y($layer->print_z - $radius),
'r' => $self->scale * $radius,
};
);
}
}
} else {
push @rectangles, map {
foreach my $line (@lines) {
my $height = $path->height;
$height = $layer->height if $height == -1;
{
'x' => $self->scale * unscale $_->[A][X],
$g->rectangle(
'x' => $self->scale * unscale($line->a->x),
'y' => $self->scale * $self->_y($layer->print_z),
'width' => $self->scale * unscale(abs($_->[B][X] - $_->[A][X])),
'width' => $self->scale * unscale($line->b->x - $line->a->x),
'height' => $self->scale * $height,
'rx' => $self->scale * $height * 0.35,
'ry' => $self->scale * $height * 0.35,
};
} @intersections;
'rx' => $self->scale * $height * 0.5,
'ry' => $self->scale * $height * 0.5,
);
}
}
}
}
}
}
}
return {
rectangles => \@rectangles,
circles => \@circles,
};
}
sub _y {
my $self = shift;
my ($y) = @_;
return $y;
return $self->height - $y;
return $self->_height - $y;
}
1;

View File

@ -15,6 +15,7 @@ use POSIX qw(setlocale LC_NUMERIC);
use Slic3r;
use Time::HiRes qw(gettimeofday tv_interval);
$|++;
binmode STDOUT, ':utf8';
our %opt = ();
my %cli_options = ();
@ -31,6 +32,7 @@ my %cli_options = ();
'load=s@' => \$opt{load},
'autosave=s' => \$opt{autosave},
'ignore-nonexistent-config' => \$opt{ignore_nonexistent_config},
'no-controller' => \$opt{no_controller},
'no-plater' => \$opt{no_plater},
'gui-mode=s' => \$opt{gui_mode},
'datadir=s' => \$opt{datadir},
@ -53,6 +55,7 @@ my %cli_options = ();
$options{ "$opt_key|$cli" } = \$cli_options{$opt_key};
}
@ARGV = grep !/^-psn_\d/, @ARGV if $^O eq 'darwin';
GetOptions(%options) or usage(1);
}
@ -60,6 +63,7 @@ my %cli_options = ();
my @external_configs = ();
if ($opt{load}) {
foreach my $configfile (@{$opt{load}}) {
$configfile = Slic3r::decode_path($configfile);
if (-e $configfile) {
push @external_configs, Slic3r::Config->load($configfile);
} elsif (-e "$FindBin::Bin/$configfile") {
@ -80,7 +84,11 @@ foreach my $c (@external_configs, Slic3r::Config->new_from_cli(%cli_options)) {
# save configuration
if ($opt{save}) {
$cli_config->save($opt{save});
if (@{$cli_config->get_keys} > 0) {
$cli_config->save($opt{save});
} else {
Slic3r::Config->new_from_defaults->save($opt{save});
}
}
# apply command line config on top of default config
@ -89,18 +97,23 @@ $config->apply($cli_config);
# launch GUI
my $gui;
if (!@ARGV && !$opt{save} && eval "require Slic3r::GUI; 1") {
if ((!@ARGV || $opt{gui}) && !$opt{save} && eval "require Slic3r::GUI; 1") {
{
no warnings 'once';
$Slic3r::GUI::datadir = $opt{datadir};
$Slic3r::GUI::no_plater = $opt{no_plater};
$Slic3r::GUI::mode = $opt{gui_mode};
$Slic3r::GUI::autosave = $opt{autosave};
$Slic3r::GUI::datadir = Slic3r::decode_path($opt{datadir} // '');
$Slic3r::GUI::no_controller = $opt{no_controller};
$Slic3r::GUI::no_plater = $opt{no_plater};
$Slic3r::GUI::mode = $opt{gui_mode};
$Slic3r::GUI::autosave = $opt{autosave};
}
$gui = Slic3r::GUI->new;
setlocale(LC_NUMERIC, 'C');
$gui->{mainframe}->load_config_file($_) for @{$opt{load}};
$gui->{mainframe}->load_config($cli_config);
foreach my $input_file (@ARGV) {
$input_file = Slic3r::decode_path($input_file);
$gui->{mainframe}{plater}->load_file($input_file) unless $opt{no_plater};
}
$gui->MainLoop;
exit;
}
@ -111,6 +124,7 @@ if (@ARGV) { # slicing from command line
if ($opt{repair}) {
foreach my $file (@ARGV) {
$file = Slic3r::decode_path($file);
die "Repair is currently supported only on STL files\n"
if $file !~ /\.stl$/i;
@ -126,6 +140,7 @@ if (@ARGV) { # slicing from command line
if ($opt{cut}) {
foreach my $file (@ARGV) {
$file = Slic3r::decode_path($file);
my $model = Slic3r::Model->read_from_file($file);
$model->add_default_instances;
my $mesh = $model->mesh;
@ -145,6 +160,7 @@ if (@ARGV) { # slicing from command line
if ($opt{split}) {
foreach my $file (@ARGV) {
$file = Slic3r::decode_path($file);
my $model = Slic3r::Model->read_from_file($file);
$model->add_default_instances;
my $mesh = $model->mesh;
@ -161,6 +177,7 @@ if (@ARGV) { # slicing from command line
}
while (my $input_file = shift @ARGV) {
$input_file = Slic3r::decode_path($input_file);
my $model;
if ($opt{merge}) {
my @models = map Slic3r::Model->read_from_file($_), $input_file, (splice @ARGV, 0);
@ -178,7 +195,7 @@ if (@ARGV) { # slicing from command line
$opt{duplicate_grid} = [ split /[,x]/, $opt{duplicate_grid}, 2 ];
}
if (defined $opt{print_center}) {
$opt{print_center} = [ split /[,x]/, $opt{print_center}, 2 ];
$opt{print_center} = Slic3r::Pointf->new(split /[,x]/, $opt{print_center}, 2);
}
my $sprint = Slic3r::Print::Simple->new(
@ -186,7 +203,7 @@ if (@ARGV) { # slicing from command line
rotate => $opt{rotate} // 0,
duplicate => $opt{duplicate} // 1,
duplicate_grid => $opt{duplicate_grid} // [1,1],
print_center => $opt{print_center} // [100,100],
print_center => $opt{print_center} // Slic3r::Pointf->new(100,100),
status_cb => sub {
my ($percent, $message) = @_;
printf "=> %s\n", $message;
@ -256,6 +273,8 @@ Usage: slic3r.pl [ OPTIONS ] [ file.stl ] [ file2.stl ] ...
$j
GUI options:
--gui Forces the GUI launch instead of command line slicing (if you
supply a model file, it will be loaded into the plater)
--no-plater Disable the plater tab
--gui-mode Overrides the configured mode (simple/expert)
--autosave <file> Automatically export current configuration to the specified file
@ -277,17 +296,18 @@ $j
(default: 100,100)
--z-offset Additional height in mm to add to vertical coordinates
(+/-, default: $config->{z_offset})
--gcode-flavor The type of G-code to generate (reprap/teacup/makerware/sailfish/mach3/no-extrusion,
--gcode-flavor The type of G-code to generate (reprap/teacup/makerware/sailfish/mach3/machinekit/no-extrusion,
default: $config->{gcode_flavor})
--use-relative-e-distances Enable this to get relative E values (default: no)
--use-firmware-retraction Enable firmware-controlled retraction using G10/G11 (default: no)
--use-volumetric-e Express E in cubic millimeters and prepend M200 (default: no)
--gcode-arcs Use G2/G3 commands for native arcs (experimental, not supported
by all firmwares)
--g0 Use G0 commands for retraction (experimental, not supported by all
firmwares)
--gcode-comments Make G-code verbose by adding comments (default: no)
--vibration-limit Limit the frequency of moves on X and Y axes (Hz, set zero to disable;
default: $config->{vibration_limit})
--pressure-advance Adjust pressure using the experimental advance algorithm (K constant,
set zero to disable; default: $config->{pressure_advance})
Filament options:
--filament-diameter Diameter in mm of your raw filament (default: $config->{filament_diameter}->[0])
@ -341,7 +361,7 @@ $j
to disable; default: $config->{first_layer_acceleration})
--default-acceleration
Acceleration will be reset to this value after the specific settings above
have been applied. (mm/s^2, set zero to disable; default: $config->{travel_speed})
have been applied. (mm/s^2, set zero to disable; default: $config->{default_acceleration})
Accuracy options:
--layer-height Layer height in mm (default: $config->{layer_height})
@ -359,12 +379,13 @@ $j
--fill-density Infill density (range: 0%-100%, default: $config->{fill_density}%)
--fill-angle Infill angle in degrees (range: 0-90, default: $config->{fill_angle})
--fill-pattern Pattern to use to fill non-solid layers (default: $config->{fill_pattern})
--solid-fill-pattern Pattern to use to fill solid layers (default: $config->{solid_fill_pattern})
--external-fill-pattern Pattern to use to fill solid layers (default: $config->{external_fill_pattern})
--start-gcode Load initial G-code from the supplied file. This will overwrite
the default command (home all axes [G28]).
--end-gcode Load final G-code from the supplied file. This will overwrite
the default commands (turn off temperature [M104 S0],
home X axis [G28 X], disable motors [M84]).
--before-layer-gcode Load before-layer-change G-code from the supplied file (default: nothing).
--layer-gcode Load layer-change G-code from the supplied file (default: nothing).
--toolchange-gcode Load tool-change G-code from the supplied file (default: nothing).
--seam-position Position of loop starting points (random/nearest/aligned, default: $config->{seam_position}).
@ -399,6 +420,8 @@ $j
Spacing between pattern lines (mm, default: $config->{support_material_spacing})
--support-material-angle
Support material angle in degrees (range: 0-90, default: $config->{support_material_angle})
--support-material-contact-distance
Vertical distance between object and support material (0+, default: $config->{support_material_contact_distance})
--support-material-interface-layers
Number of perpendicular layers between support material and object (0+, default: $config->{support_material_interface_layers})
--support-material-interface-spacing
@ -419,16 +442,18 @@ $j
--retract-before-travel
Only retract before travel moves of this length in mm (default: $config->{retract_before_travel}[0])
--retract-lift Lift Z by the given distance in mm when retracting (default: $config->{retract_lift}[0])
--retract-lift-above Only lift Z when above the specified height (default: $config->{retract_lift_above}[0])
--retract-lift-below Only lift Z when below the specified height (default: $config->{retract_lift_below}[0])
--retract-layer-change
Enforce a retraction before each Z move (default: yes)
Enforce a retraction before each Z move (default: no)
--wipe Wipe the nozzle while doing a retraction (default: no)
Retraction options for multi-extruder setups:
--retract-length-toolchange
Length of retraction in mm when disabling tool (default: $config->{retract_length}[0])
--retract-restart-extra-toolchnage
Length of retraction in mm when disabling tool (default: $config->{retract_length_toolchange}[0])
--retract-restart-extra-toolchange
Additional amount of filament in mm to push after
switching tool (default: $config->{retract_restart_extra}[0])
switching tool (default: $config->{retract_restart_extra_toolchange}[0])
Cooling options:
--cooling Enable fan and cooling control
@ -492,16 +517,18 @@ $j
Set a different extrusion width for top infill
--support-material-extrusion-width
Set a different extrusion width for support material
--infill-overlap Overlap between infill and perimeters (default: $config->{infill_overlap})
--bridge-flow-ratio Multiplier for extrusion when bridging (> 0, default: $config->{bridge_flow_ratio})
Multiple extruder options:
--extruder-offset Offset of each extruder, if firmware doesn't handle the displacement
(can be specified multiple times, default: 0x0)
--perimeter-extruder
Extruder to use for perimeters (1+, default: $config->{perimeter_extruder})
Extruder to use for perimeters and brim (1+, default: $config->{perimeter_extruder})
--infill-extruder Extruder to use for infill (1+, default: $config->{infill_extruder})
--solid-infill-extruder Extruder to use for solid infill (1+, default: $config->{solid_infill_extruder})
--support-material-extruder
Extruder to use for support material (1+, default: $config->{support_material_extruder})
Extruder to use for support material, raft and skirt (1+, default: $config->{support_material_extruder})
--support-material-interface-extruder
Extruder to use for support material interface (1+, default: $config->{support_material_interface_extruder})
--ooze-prevention Drop temperature and park extruders outside a full skirt for automatic wiping

View File

@ -0,0 +1,21 @@
use Test::More tests => 1;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
}
use List::Util qw(first sum);
use Slic3r;
use Slic3r::Test;
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('avoid_crossing_perimeters', 2);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
ok my $gcode = Slic3r::Test::gcode($print), "no crash with avoid_crossing_perimeters and multiple objects";
}
__END__

View File

@ -1,4 +1,4 @@
use Test::More tests => 14;
use Test::More tests => 16;
use strict;
use warnings;
@ -84,17 +84,18 @@ use Slic3r::Test;
sub check_angle {
my ($lower, $bridge, $expected, $tolerance, $expected_coverage) = @_;
if (ref($lower) eq 'ARRAY') {
$lower = Slic3r::ExPolygon::Collection->new(@$lower);
}
$expected_coverage //= -1;
$expected_coverage = $bridge->area if $expected_coverage == -1;
my $bd = Slic3r::Layer::BridgeDetector->new(
expolygon => $bridge,
lower_slices => $lower,
extrusion_width => scale 0.5,
);
my $bd = Slic3r::BridgeDetector->new($bridge, $lower, scale 0.5);
$tolerance //= rad2deg($bd->resolution) + epsilon;
my $result = $bd->detect_angle;
$bd->detect_angle;
my $result = $bd->angle;
my $coverage = $bd->coverage;
is sum(map $_->area, @$coverage), $expected_coverage, 'correct coverage area';
@ -106,4 +107,30 @@ sub check_angle {
return defined $result && $result>=0 && abs($delta) < $tolerance;
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('top_solid_layers', 0); # to prevent bridging on sparse infill
$config->set('bridge_speed', 99);
my $print = Slic3r::Test::init_print('bridge', config => $config);
my %extrusions = (); # angle => length
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G1' && ($args->{F} // $self->F)/60 == $config->bridge_speed) {
my $line = Slic3r::Line->new_scale(
[ $self->X, $self->Y ],
[ $info->{new_X}, $info->{new_Y} ],
);
my $angle = $line->direction;
$extrusions{$angle} //= 0;
$extrusions{$angle} += $line->length;
}
});
ok !!%extrusions, "bridge is generated";
my ($main_angle) = sort { $extrusions{$b} <=> $extrusions{$a} } keys %extrusions;
is $main_angle, 0, "bridge has the expected direction";
}
__END__

View File

@ -9,9 +9,67 @@ BEGIN {
use List::Util qw(first);
use Slic3r;
use Slic3r::Surface ':types';
use Slic3r::Test;
plan tests => 2;
plan tests => 8;
{
my $test = sub {
my ($config) = @_;
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
ok my $gcode = Slic3r::Test::gcode($print), "infill_every_layers does not crash";
my $tool = undef;
my %layers = (); # layer_z => 1
my %layer_infill = (); # layer_z => has_infill
Slic3r::GCode::Reader->new->parse($gcode, sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd =~ /^T(\d+)/) {
$tool = $1;
} elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0 && $tool != $config->support_material_extruder-1) {
$layer_infill{$self->Z} //= 0;
if ($tool == $config->infill_extruder-1) {
$layer_infill{$self->Z} = 1;
}
}
$layers{$args->{Z}} = 1 if $cmd eq 'G1' && $info->{dist_Z} > 0;
});
my $layers_with_perimeters = scalar(keys %layer_infill);
my $layers_with_infill = grep $_ > 0, values %layer_infill;
is scalar(keys %layers), $layers_with_perimeters+$config->raft_layers, 'expected number of layers';
# first infill layer is never combined, so we don't consider it
$layers_with_infill--;
$layers_with_perimeters--;
# we expect that infill is generated for half the number of combined layers
# plus for each single layer that was not combined (remainder)
is $layers_with_infill,
int($layers_with_perimeters/$config->infill_every_layers) + ($layers_with_perimeters % $config->infill_every_layers),
'infill is only present in correct number of layers';
};
my $config = Slic3r::Config->new_from_defaults;
$config->set('layer_height', 0.2);
$config->set('first_layer_height', 0.2);
$config->set('nozzle_diameter', [0.5]);
$config->set('infill_every_layers', 2);
$config->set('perimeter_extruder', 1);
$config->set('infill_extruder', 2);
$config->set('support_material_extruder', 3);
$config->set('support_material_interface_extruder', 3);
$config->set('top_solid_layers', 0);
$config->set('bottom_solid_layers', 0);
$test->($config);
$config->set('skirts', 0); # prevent usage of perimeter_extruder in raft layers
$config->set('raft_layers', 5);
$test->($config);
}
{
my $config = Slic3r::Config->new_from_defaults;
@ -19,29 +77,22 @@ plan tests => 2;
$config->set('first_layer_height', 0.2);
$config->set('nozzle_diameter', [0.5]);
$config->set('infill_every_layers', 2);
$config->set('infill_extruder', 2);
$config->set('top_solid_layers', 0);
$config->set('bottom_solid_layers', 0);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
ok my $gcode = Slic3r::Test::gcode($print), "infill_every_layers does not crash";
my $tool = undef;
my %layer_infill = (); # layer_z => has_infill
Slic3r::GCode::Reader->new->parse($gcode, sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd =~ /^T(\d+)/) {
$tool = $1;
} elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
$layer_infill{$self->Z} //= 0;
if ($tool == $config->infill_extruder-1) {
$layer_infill{$self->Z} = 1;
}
}
});
my $layers_with_infill = grep $_, values %layer_infill;
$layers_with_infill--; # first layer is never combined
is $layers_with_infill, scalar(keys %layer_infill)/2, 'infill is only present in correct number of layers';
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
$print->process;
ok defined(first { @{$_->get_region(0)->fill_surfaces->filter_by_type(S_TYPE_INTERNALVOID)} > 0 }
@{$print->print->get_object(0)->layers}),
'infill combination produces internal void surfaces';
# we disable combination after infill has been generated
$config->set('infill_every_layers', 1);
$print->apply_config($config);
$print->process;
ok !(defined first { @{$_->get_region(0)->fill_surfaces} == 0 }
@{$print->print->get_object(0)->layers}),
'infill combination is idempotent';
}
# the following needs to be adapted to the new API
@ -73,7 +124,6 @@ if (0) {
# copy of Print::export_gcode() up to the point
# after fill surfaces are combined
$self->init_extruders;
$_->slice for @{$self->objects};
$_->make_perimeters for @{$self->objects};
$_->detect_surfaces_type for @{$self->objects};

View File

@ -1,4 +1,4 @@
use Test::More tests => 2;
use Test::More tests => 1;
use strict;
use warnings;
@ -10,13 +10,6 @@ BEGIN {
use Slic3r;
use Slic3r::Test;
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('layer_height', 0.123);
$config->setenv;
is $ENV{SLIC3R_LAYER_HEIGHT}, '0.123', 'setenv';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('perimeter_extrusion_width', '250%');

View File

@ -18,9 +18,12 @@ sub buffer {
my $print_config = Slic3r::Config::Print->new;
$print_config->apply_dynamic($config);
my $gcodegen = Slic3r::GCode->new;
$gcodegen->apply_print_config($print_config);
$gcodegen->set_layer_count(10);
my $buffer = Slic3r::GCode::CoolingBuffer->new(
config => $print_config,
gcodegen => Slic3r::GCode->new(print_config => $print_config, layer_count => 10, extruders => []),
gcodegen => $gcodegen,
);
return $buffer;
}
@ -30,15 +33,21 @@ $config->set('disable_fan_first_layers', 0);
{
my $buffer = buffer($config);
$buffer->gcodegen->elapsed_time($buffer->config->slowdown_below_layer_time + 1);
my $gcode = $buffer->append('G1 X100 E1 F3000', 0, 0, 0.4) . $buffer->flush;
$buffer->gcodegen->set_elapsed_time($buffer->config->slowdown_below_layer_time + 1);
my $gcode = $buffer->append('G1 F3000;_EXTRUDE_SET_SPEED\nG1 X100 E1', 0, 0, 0.4) . $buffer->flush;
like $gcode, qr/F3000/, 'speed is not altered when elapsed time is greater than slowdown threshold';
}
{
my $buffer = buffer($config);
$buffer->gcodegen->elapsed_time($buffer->config->slowdown_below_layer_time - 1);
my $gcode = $buffer->append("G1 X50 F2500\nG1 X100 E1 F3000\nG1 E4 F400", 0, 0, 0.4) . $buffer->flush;
$buffer->gcodegen->set_elapsed_time($buffer->config->slowdown_below_layer_time - 1);
my $gcode = $buffer->append(
"G1 X50 F2500\n" .
"G1 F3000;_EXTRUDE_SET_SPEED\n" .
"G1 X100 E1\n" .
"G1 E4 F400",
0, 0, 0.4
) . $buffer->flush;
unlike $gcode, qr/F3000/, 'speed is altered when elapsed time is lower than slowdown threshold';
like $gcode, qr/F2500/, 'speed is not altered for travel moves';
like $gcode, qr/F400/, 'speed is not altered for extruder-only moves';
@ -46,7 +55,7 @@ $config->set('disable_fan_first_layers', 0);
{
my $buffer = buffer($config);
$buffer->gcodegen->elapsed_time($buffer->config->fan_below_layer_time + 1);
$buffer->gcodegen->set_elapsed_time($buffer->config->fan_below_layer_time + 1);
my $gcode = $buffer->append('G1 X100 E1 F3000', 0, 0, 0.4) . $buffer->flush;
unlike $gcode, qr/M106/, 'fan is not activated when elapsed time is greater than fan threshold';
}
@ -56,7 +65,7 @@ $config->set('disable_fan_first_layers', 0);
my $gcode = "";
for my $obj_id (0 .. 1) {
# use an elapsed time which is < the slowdown threshold but greater than it when summed twice
$buffer->gcodegen->elapsed_time($buffer->config->slowdown_below_layer_time - 1);
$buffer->gcodegen->set_elapsed_time($buffer->config->slowdown_below_layer_time - 1);
$gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, 0, 0.4);
}
$gcode .= $buffer->flush;
@ -69,7 +78,7 @@ $config->set('disable_fan_first_layers', 0);
for my $layer_id (0 .. 1) {
for my $obj_id (0 .. 1) {
# use an elapsed time which is < the threshold but greater than it when summed twice
$buffer->gcodegen->elapsed_time($buffer->config->fan_below_layer_time - 1);
$buffer->gcodegen->set_elapsed_time($buffer->config->fan_below_layer_time - 1);
$gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, $layer_id, 0.4 + 0.4*$layer_id + 0.1*$obj_id); # print same layer at distinct heights
}
}
@ -83,7 +92,7 @@ $config->set('disable_fan_first_layers', 0);
for my $layer_id (0 .. 1) {
for my $obj_id (0 .. 1) {
# use an elapsed time which is < the threshold even when summed twice
$buffer->gcodegen->elapsed_time($buffer->config->fan_below_layer_time/2 - 1);
$buffer->gcodegen->set_elapsed_time($buffer->config->fan_below_layer_time/2 - 1);
$gcode .= $buffer->append("G1 X100 E1 F3000\n", $obj_id, $layer_id, 0.4 + 0.4*$layer_id + 0.1*$obj_id); # print same layer at distinct heights
}
}

View File

@ -1,4 +1,4 @@
use Test::More tests => 13;
use Test::More tests => 15;
use strict;
use warnings;
@ -7,6 +7,7 @@ BEGIN {
use lib "$FindBin::Bin/../lib";
}
use List::Util qw(first);
use Slic3r;
use Slic3r::Test;
@ -46,7 +47,8 @@ use Slic3r::Test;
{
my $parser = Slic3r::GCode::PlaceholderParser->new;
$parser->apply_config(my $config = Slic3r::Config->new_from_defaults);
is $parser->process('[temperature_[foo]]', { foo => '1' }),
$parser->set('foo' => '0');
is $parser->process('[temperature_[foo]]'),
$config->temperature->[0],
"nested config options";
}
@ -104,4 +106,29 @@ use Slic3r::Test;
}
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('before_layer_gcode', ';BEFORE [layer_num]');
$config->set('layer_gcode', ';CHANGE [layer_num]');
$config->set('support_material', 1);
$config->set('layer_height', 0.2);
my $print = Slic3r::Test::init_print('overhang', config => $config);
my $gcode = Slic3r::Test::gcode($print);
my @before = ();
my @change = ();
foreach my $line (split /\R+/, $gcode) {
if ($line =~ /;BEFORE (\d+)/) {
push @before, $1;
} elsif ($line =~ /;CHANGE (\d+)/) {
push @change, $1;
fail 'inconsistent layer_num before and after layer change'
if $1 != $before[-1];
}
}
is_deeply \@before, \@change, 'layer_num is consistent before and after layer changes';
ok !defined(first { $change[$_] != $change[$_-1]+1 } 1..$#change),
'layer_num grows continously'; # i.e. no duplicates or regressions
}
__END__

View File

@ -12,7 +12,7 @@ BEGIN {
use List::Util qw(first sum);
use Slic3r;
use Slic3r::Geometry qw(X Y scale unscale convex_hull);
use Slic3r::Geometry::Clipper qw(union diff_ex offset);
use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex);
use Slic3r::Surface qw(:types);
use Slic3r::Test;
@ -20,7 +20,6 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
{
my $print = Slic3r::Print->new;
$print->init_extruders;
my $filler = Slic3r::Fill::Rectilinear->new(
print => $print,
bounding_box => Slic3r::Geometry::BoundingBox->new_from_points([ Slic3r::Point->new(0, 0), Slic3r::Point->new(10, 10) ]),
@ -49,9 +48,10 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
height => 0.4,
nozzle_diameter => 0.50,
);
$filler->spacing($flow->spacing);
foreach my $angle (0, 45) {
$surface->expolygon->rotate(Slic3r::Geometry::deg2rad($angle), [0,0]);
my ($params, @paths) = $filler->fill_surface($surface, flow => $flow, layer_height => 0.4, density => 0.4);
my @paths = $filler->fill_surface($surface, layer_height => 0.4, density => 0.4);
is scalar @paths, 1, 'one continuous path';
}
}
@ -73,15 +73,15 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
height => 0.4,
nozzle_diameter => $flow_spacing,
);
my ($params, @paths) = $filler->fill_surface(
$filler->spacing($flow->spacing);
my @paths = $filler->fill_surface(
$surface,
flow => $flow,
layer_height => $flow->height,
density => $density // 1,
);
# check whether any part was left uncovered
my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $params->{flow}->spacing/2)}, @paths;
my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $filler->spacing/2)}, @paths;
my $uncovered = diff_ex([ @$expolygon ], [ @grown_paths ], 1);
# ignore very small dots
@ -171,7 +171,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
my $config = Slic3r::Config->new_from_defaults;
$config->set('fill_pattern', $pattern);
$config->set('solid_fill_pattern', $pattern);
$config->set('external_fill_pattern', $pattern);
$config->set('perimeters', 1);
$config->set('skirts', 0);
$config->set('fill_density', 20);
@ -205,6 +205,7 @@ for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
$config->set('bottom_solid_layers', 0);
$config->set('infill_extruder', 2);
$config->set('infill_extrusion_width', 0.5);
$config->set('fill_density', 40);
$config->set('cooling', 0); # for preventing speeds from being altered
$config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
@ -247,18 +248,27 @@ for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('skirts', 0);
$config->set('perimeters', 0);
$config->set('perimeters', 1);
$config->set('fill_density', 0);
$config->set('top_solid_layers', 0);
$config->set('bottom_solid_layers', 0);
$config->set('solid_infill_below_area', 20000000);
$config->set('solid_infill_every_layers', 2);
$config->set('perimeter_speed', 99);
$config->set('external_perimeter_speed', 99);
$config->set('cooling', 0);
$config->set('first_layer_speed', '100%');
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my %layers_with_extrusion = ();
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
$layers_with_extrusion{$self->Z} = 1 if $info->{extruding};
if ($cmd eq 'G1' && $info->{dist_XY} > 0 && $info->{extruding}) {
if (($args->{F} // $self->F) != $config->perimeter_speed*60) {
$layers_with_extrusion{$self->Z} = ($args->{F} // $self->F);
}
}
});
ok !%layers_with_extrusion,
@ -274,6 +284,7 @@ for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
$config->set('first_layer_height', 0.2);
$config->set('nozzle_diameter', [0.35]);
$config->set('infill_extruder', 2);
$config->set('solid_infill_extruder', 2);
$config->set('infill_extrusion_width', 0.52);
$config->set('solid_infill_extrusion_width', 0.52);
$config->set('first_layer_extrusion_width', 0);
@ -300,7 +311,9 @@ for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
my $grow_d = scale($config->infill_extrusion_width)/2;
my $layer0_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.2} } ]);
my $layer1_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.4} } ]);
my $diff = [ grep { $_->area > 2*(($grow_d*2)**2) } @{diff_ex($layer0_infill, $layer1_infill)} ];
my $diff = diff($layer0_infill, $layer1_infill);
$diff = offset2_ex($diff, -$grow_d, +$grow_d);
$diff = [ grep { $_->area > 2*(($grow_d*2)**2) } @$diff ];
is scalar(@$diff), 0, 'no missing parts in solid shell when fill_density is 0';
}

View File

@ -34,7 +34,8 @@ use Slic3r::Test;
}
});
my $E_per_mm_avg = sum(@E_per_mm) / @E_per_mm;
ok !(defined first { abs($_ - $E_per_mm_avg) > 0.01 } @E_per_mm),
# allow some tolerance because solid rectilinear infill might be adjusted/stretched
ok !(defined first { abs($_ - $E_per_mm_avg) > 0.015 } @E_per_mm),
'first_layer_extrusion_width applies to everything on first layer';
}

104
t/gcode.t
View File

@ -1,4 +1,4 @@
use Test::More tests => 11;
use Test::More tests => 23;
use strict;
use warnings;
@ -13,25 +13,33 @@ use Slic3r::Geometry qw(scale convex_hull);
use Slic3r::Test;
{
my $gcodegen = Slic3r::GCode->new(
layer_count => 1,
extruders => [],
);
$gcodegen->set_shift(10, 10);
my $gcodegen = Slic3r::GCode->new();
$gcodegen->set_layer_count(1);
$gcodegen->set_origin(Slic3r::Pointf->new(10, 10));
is_deeply $gcodegen->last_pos->arrayref, [scale -10, scale -10], 'last_pos is shifted correctly';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('wipe', [1]);
$config->set('retract_layer_change', [0]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $have_wipe = 0;
my @retract_speeds = ();
my $extruded_on_this_layer = 0;
my $wiping_on_new_layer = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{retracting} && $info->{dist_XY} > 0) {
if ($info->{travel} && $info->{dist_Z}) {
# changing layer
$extruded_on_this_layer = 0;
} elsif ($info->{extruding} && $info->{dist_XY}) {
$extruded_on_this_layer = 1;
} elsif ($info->{retracting} && $info->{dist_XY} > 0) {
$have_wipe = 1;
$wiping_on_new_layer = 1 if !$extruded_on_this_layer;
my $move_time = $info->{dist_XY} / ($args->{F} // $self->F);
push @retract_speeds, abs($info->{dist_E}) / $move_time;
}
@ -39,6 +47,35 @@ use Slic3r::Test;
ok $have_wipe, "wipe";
ok !defined (first { abs($_ - $config->retract_speed->[0]*60) < 5 } @retract_speeds), 'wipe moves don\'t retract faster than configured speed';
ok !$wiping_on_new_layer, 'no wiping after layer change';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('z_offset', 5);
$config->set('start_gcode', '');
my $test = sub {
my ($comment) = @_;
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $moves_below_z_offset = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{travel} && exists $args->{Z}) {
$moves_below_z_offset++ if $args->{Z} < $config->z_offset;
}
});
is $moves_below_z_offset, 0, "no Z moves below Z offset ($comment)";
};
$test->("no lift");
$config->set('retract_lift', [3]);
$test->("lift < z_offset");
$config->set('retract_lift', [6]);
$test->("lift > z_offset");
}
{
@ -47,6 +84,7 @@ use Slic3r::Test;
# - no hard-coded "E" are generated
# - Z moves are correctly generated for both objects
# - no travel moves go outside skirt
# - temperatures are set correctly
my $config = Slic3r::Config->new_from_defaults;
$config->set('gcode_comments', 1);
$config->set('complete_objects', 1);
@ -54,11 +92,14 @@ use Slic3r::Test;
$config->set('start_gcode', ''); # prevent any default extra Z move
$config->set('layer_height', 0.4);
$config->set('first_layer_height', 0.4);
$config->set('temperature', [200]);
$config->set('first_layer_temperature', [210]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
ok my $gcode = Slic3r::Test::gcode($print), "complete_objects";
my @z_moves = ();
my @travel_moves = (); # array of scaled points
my @extrusions = (); # array of scaled points
my @temps = ();
Slic3r::GCode::Reader->new->parse($gcode, sub {
my ($self, $cmd, $args, $info) = @_;
fail 'unexpected E argument' if defined $args->{E};
@ -73,6 +114,8 @@ use Slic3r::Test;
push @travel_moves, Slic3r::Point->new_scale($info->{new_X}, $info->{new_Y})
if @extrusions; # skip initial travel move to first skirt point
}
} elsif ($cmd eq 'M104' || $cmd eq 'M109') {
push @temps, $args->{S} if !@temps || $args->{S} != $temps[-1];
}
});
my $layer_count = 20/0.4; # cube is 20mm tall
@ -81,6 +124,8 @@ use Slic3r::Test;
my $convex_hull = convex_hull(\@extrusions);
ok !(defined first { !$convex_hull->contains_point($_) } @travel_moves), 'all travel moves happen within skirt';
is_deeply \@temps, [210, 200, 210, 200, 0], 'expected temperature changes';
}
{
@ -88,11 +133,7 @@ use Slic3r::Test;
$config->set('retract_length', [1000000]);
$config->set('use_relative_e_distances', 1);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
});
Slic3r::Test::gcode($print);
ok $print->print->total_used_filament > 0, 'final retraction is not considered in total used filament';
}
@ -101,15 +142,22 @@ use Slic3r::Test;
my ($print, $comment) = @_;
my @percent = ();
my $got_100 = 0;
my $extruding_after_100 = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'M73') {
push @percent, $args->{P};
$got_100 = 1 if $args->{P} eq '100';
}
if ($info->{extruding} && $got_100) {
$extruding_after_100 = 1;
}
});
# the extruder heater is turned off when M73 P100 is reached
ok !(defined first { $_ > 100 } @percent), "M73 is never given more than 100% ($comment)";
ok !$extruding_after_100, "no extrusions after M73 P100 ($comment)";
};
{
@ -133,6 +181,38 @@ use Slic3r::Test;
my $print = Slic3r::Test::init_print(['20mm_cube','20mm_cube'], config => $config);
$test->($print, 'two objects');
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('gcode_flavor', 'sailfish');
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale_xyz => [1,1, 1/(20/$config->layer_height) ]);
$test->($print, 'one layer object');
}
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('start_gcode', 'START:[input_filename]');
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $gcode = Slic3r::Test::gcode($print);
like $gcode, qr/START:20mm_cube/, '[input_filename] is also available in custom G-code';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('spiral_vase', 1);
my $print = Slic3r::Test::init_print('cube_with_hole', config => $config);
my $spiral = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G1' && exists $args->{E} && exists $args->{Z}) {
$spiral = 1;
}
});
ok !$spiral, 'spiral vase is correctly disabled on layers with multiple loops';
}
__END__

View File

@ -2,7 +2,7 @@ use Test::More;
use strict;
use warnings;
plan tests => 33;
plan tests => 42;
BEGIN {
use FindBin;
@ -182,7 +182,7 @@ my $polygons = [
{
my $line = Slic3r::Line->new([0, 0], [20, 0]);
is +Slic3r::Point->new(10, 10)->distance_to_line($line), 10, 'distance_to';
is +Slic3r::Point->new(50, 10)->distance_to_line($line), 10, 'distance_to';
is +Slic3r::Point->new(50, 0)->distance_to_line($line), 30, 'distance_to';
is +Slic3r::Point->new(0, 0)->distance_to_line($line), 0, 'distance_to';
is +Slic3r::Point->new(20, 0)->distance_to_line($line), 0, 'distance_to';
is +Slic3r::Point->new(10, 0)->distance_to_line($line), 0, 'distance_to';
@ -190,6 +190,21 @@ my $polygons = [
#==========================================================
{
my $square = Slic3r::Polygon->new_scale(
[100,100],
[200,100],
[200,200],
[100,200],
);
is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in ccw square';
is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in ccw square';
$square->make_clockwise;
is scalar(@{$square->concave_points(PI*4/3)}), 4, 'fuor concave vertices detected in cw square';
is scalar(@{$square->convex_points(PI*2/3)}), 0, 'no convex vertices detected in cw square';
}
{
my $square = Slic3r::Polygon->new_scale(
[150,100],
@ -213,3 +228,29 @@ my $polygons = [
is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in convex polygon';
is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in square';
}
{
my $triangle = Slic3r::Polygon->new(
[16000170,26257364], [714223,461012], [31286371,461008],
);
is scalar(@{$triangle->concave_points(PI*4/3)}), 0, 'no concave vertices detected in triangle';
is scalar(@{$triangle->convex_points(PI*2/3)}), 3, 'three convex vertices detected in triangle';
}
{
my $triangle = Slic3r::Polygon->new(
[16000170,26257364], [714223,461012], [20000000,461012], [31286371,461012],
);
is scalar(@{$triangle->concave_points(PI*4/3)}), 0, 'no concave vertices detected in triangle having collinear point';
is scalar(@{$triangle->convex_points(PI*2/3)}), 3, 'three convex vertices detected in triangle having collinear point';
}
{
my $triangle = Slic3r::Polygon->new(
[16000170,26257364], [714223,461012], [31286371,461008],
);
my $simplified = $triangle->simplify(250000)->[0];
is scalar(@$simplified), 3, 'triangle is never simplified to less than 3 points';
}
__END__

View File

@ -17,23 +17,25 @@ use Slic3r::Test;
my $config = Slic3r::Config->new_from_defaults;
$config->set('raft_layers', 2);
$config->set('infill_extruder', 2);
$config->set('support_material_extruder', 3);
$config->set('solid_infill_extruder', 3);
$config->set('support_material_extruder', 4);
$config->set('ooze_prevention', 1);
$config->set('extruder_offset', [ [0,0], [20,0], [0,20] ]);
$config->set('temperature', [200, 180, 170]);
$config->set('first_layer_temperature', [206, 186, 166]);
$config->set('extruder_offset', [ [0,0], [20,0], [0,20], [20,20] ]);
$config->set('temperature', [200, 180, 170, 160]);
$config->set('first_layer_temperature', [206, 186, 166, 156]);
$config->set('toolchange_gcode', ';toolchange'); # test that it doesn't crash when this is supplied
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $tool = undef;
my @tool_temp = (0,0,0);
my @tool_temp = (0,0,0,0);
my @toolchange_points = ();
my @extrusion_points = ();
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd =~ /^T(\d+)/) {
# ignore initial toolchange
if (defined $tool) {
my $expected_temp = $self->Z == ($config->get_value('first_layer_height') + $config->z_offset)
? $config->first_layer_temperature->[$tool]
@ -41,8 +43,7 @@ use Slic3r::Test;
die 'standby temperature was not set before toolchange'
if $tool_temp[$tool] != $expected_temp + $config->standby_temperature_delta;
# ignore initial toolchange
push @toolchange_points, Slic3r::Point->new_scale($self->X, $self->Y);
push @toolchange_points, my $point = Slic3r::Point->new_scale($self->X, $self->Y);
}
$tool = $1;
} elsif ($cmd eq 'M104' || $cmd eq 'M109') {
@ -54,11 +55,30 @@ use Slic3r::Test;
$tool_temp[$t] = $args->{S};
} elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
push @extrusion_points, my $point = Slic3r::Point->new_scale($args->{X}, $args->{Y});
$point->translate(map scale($_), @{ $config->extruder_offset->[$tool] });
$point->translate(map +scale($_), @{ $config->extruder_offset->[$tool] });
}
});
my $convex_hull = convex_hull(\@extrusion_points);
ok !(defined first { $convex_hull->contains_point($_) } @toolchange_points), 'all toolchanges happen outside skirt';
my @t = ();
foreach my $point (@toolchange_points) {
foreach my $offset (@{$config->extruder_offset}) {
push @t, my $p = $point->clone;
$p->translate(map +scale($_), @$offset);
}
}
ok !(defined first { $convex_hull->contains_point($_) } @t), 'all nozzles are outside skirt at toolchange';
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"ooze_prevention_test.svg",
no_arrows => 1,
polygons => [$convex_hull],
red_points => \@t,
points => \@toolchange_points,
);
}
# offset the skirt by the maximum displacement between extruders plus a safety extra margin
my $delta = scale(20 * sqrt(2) + 1);
@ -148,6 +168,7 @@ use Slic3r::Test;
{
my $model = stacked_cubes();
my $object = $model->objects->[0];
my $config = Slic3r::Config->new_from_defaults;
$config->set('layer_height', 0.4);
@ -155,8 +176,8 @@ use Slic3r::Test;
$config->set('skirts', 0);
my $print = Slic3r::Test::init_print($model, config => $config);
is $model->get_material('lower')->config->extruder, 1, 'auto_assign_extruders() assigned correct extruder to first volume';
is $model->get_material('upper')->config->extruder, 2, 'auto_assign_extruders() assigned correct extruder to second volume';
is $object->volumes->[0]->config->extruder, 1, 'auto_assign_extruders() assigned correct extruder to first volume';
is $object->volumes->[1]->config->extruder, 2, 'auto_assign_extruders() assigned correct extruder to second volume';
my $tool = undef;
my %T0 = my %T1 = (); # Z => 1

File diff suppressed because one or more lines are too long

36
t/pressure.t Normal file
View File

@ -0,0 +1,36 @@
use Test::More tests => 1;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
}
use List::Util qw();
use Slic3r;
use Slic3r::Geometry qw(epsilon);
use Slic3r::Test;
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('pressure_advance', 10);
$config->set('retract_length', [1]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
my $retracted = $config->retract_length->[0];
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{extruding} && !$info->{dist_XY}) {
$retracted += $info->{dist_E};
} elsif ($info->{retracting}) {
$retracted += $info->{dist_E};
}
});
ok abs($retracted) < 0.01, 'all retractions are compensated';
}
__END__

View File

@ -1,4 +1,4 @@
use Test::More tests => 18;
use Test::More tests => 22;
use strict;
use warnings;
@ -7,6 +7,7 @@ BEGIN {
use lib "$FindBin::Bin/../lib";
}
use List::Util qw(any);
use Slic3r;
use Slic3r::Test qw(_eq);
@ -17,7 +18,7 @@ use Slic3r::Test qw(_eq);
my $test = sub {
my ($conf) = @_;
$conf ||= $config;
my $print = Slic3r::Test::init_print('20mm_cube', config => $conf, duplicate => $duplicate);
my $tool = 0;
@ -44,7 +45,7 @@ use Slic3r::Test qw(_eq);
# lift move or lift + change layer
if (_eq($info->{dist_Z}, $print->print->config->get_at('retract_lift', $tool))
|| (_eq($info->{dist_Z}, $conf->layer_height + $print->print->config->get_at('retract_lift', $tool)) && $print->print->config->get_at('retract_lift', $tool) > 0)) {
fail 'only lifting while retracted' if !$retracted[$tool] && !($conf->g0 && $info->{retracting});
fail 'only lifting while retracted' if !$retracted[$tool];
fail 'double lift' if $lifted;
$lifted = 1;
}
@ -67,8 +68,6 @@ use Slic3r::Test qw(_eq);
} else {
fail 'retracted by the correct amount';
}
fail 'combining retraction and travel with G0'
if $cmd ne 'G0' && $conf->g0 && ($info->{dist_Z} || $info->{dist_XY});
}
if ($info->{extruding}) {
fail 'only extruding while not lifted' if $lifted;
@ -78,7 +77,7 @@ use Slic3r::Test qw(_eq);
$expected_amount = $print->print->config->get_at('retract_length_toolchange', $tool) + $print->print->config->get_at('retract_restart_extra_toolchange', $tool);
$changed_tool = 0;
}
fail 'unretracted by the correct amount'
fail 'unretracted by the correct amount' && exit
if !_eq($info->{dist_E}, $expected_amount);
$retracted[$tool] = 0;
$retracted_length[$tool] = 0;
@ -120,11 +119,7 @@ use Slic3r::Test qw(_eq);
$duplicate = 2;
$retract_tests->(' (duplicate)');
$config->set('g0', 1);
$retract_tests->(' (G0 and duplicate)');
$duplicate = 1;
$config->set('g0', 0);
$config->set('infill_extruder', 2);
$config->set('skirts', 4);
$config->set('skirt_height', 3);
@ -133,16 +128,21 @@ use Slic3r::Test qw(_eq);
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('start_gcode', ''); # prevent any default priming Z move from affecting our lift detection
$config->set('retract_length', [0]);
$config->set('retract_layer_change', [0]);
$config->set('retract_lift', [0.2]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $retracted = 0;
my $layer_changes_with_retraction = 0;
my $retractions = my $z_restores = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{retracting}) {
$retracted = 1;
$retractions++;
} elsif ($info->{extruding} && $retracted) {
$retracted = 0;
}
@ -150,34 +150,88 @@ use Slic3r::Test qw(_eq);
if ($info->{dist_Z} && $retracted) {
$layer_changes_with_retraction++;
}
if ($info->{dist_Z} && $args->{Z} < $self->Z) {
$z_restores++;
}
});
is $layer_changes_with_retraction, 0, 'no retraction on layer change';
is $retractions, 0, 'no retractions';
is $z_restores, 0, 'no lift';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('only_retract_when_crossing_perimeters', 1);
$config->set('fill_density', 0);
$config->set('use_firmware_retraction', 1);
my $print = Slic3r::Test::init_print('cube_with_hole', config => $config);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $retracted = 0;
my $traveling_without_retraction = 0;
my $double_retractions = my $double_unretractions = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{retracting}) {
if ($cmd eq 'G10') {
$double_retractions++ if $retracted;
$retracted = 1;
} elsif ($info->{extruding} && $retracted) {
} elsif ($cmd eq 'G11') {
$double_unretractions++ if !$retracted;
$retracted = 0;
} elsif ($info->{travel} && !$retracted) {
if ($info->{dist_XY} > $config->retract_before_travel->[0]) {
$traveling_without_retraction = 1;
}
}
});
ok !$traveling_without_retraction, 'always retract when using only_retract_when_crossing_perimeters and fill_density = 0';
is $double_retractions, 0, 'no double retractions';
is $double_unretractions, 0, 'no double unretractions';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('use_firmware_retraction', 1);
$config->set('retract_length', [0]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $retracted = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G10') {
$retracted = 1;
}
});
ok $retracted, 'retracting also when --retract-length is 0 but --use-firmware-retraction is enabled';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('start_gcode', '');
$config->set('retract_lift', [3]);
my @lifted_at = ();
my $test = sub {
my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
@lifted_at = ();
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G1' && $info->{dist_Z} < 0) {
push @lifted_at, $info->{new_Z};
}
});
};
$config->set('retract_lift_above', [0]);
$config->set('retract_lift_below', [0]);
$test->();
ok !!@lifted_at, 'lift takes place when above/below == 0';
$config->set('retract_lift_above', [5]);
$config->set('retract_lift_below', [15]);
$test->();
ok !!@lifted_at, 'lift takes place when above/below != 0';
ok !(any { $_ < $config->get_at('retract_lift_above', 0) } @lifted_at),
'Z is not lifted below the configured value';
ok !(any { $_ > $config->get_at('retract_lift_below', 0) } @lifted_at),
'Z is not lifted above the configured value';
}
__END__

View File

@ -72,6 +72,9 @@ use Slic3r::Test;
$config->set('bottom_solid_layers', 0);
ok $test->(), "no shells are applied when both top and bottom are set to zero";
$config->set('perimeters', 1);
$config->set('top_solid_layers', 3);
$config->set('bottom_solid_layers', 3);
$config->set('fill_density', 0);
ok $test->(), "proper number of shells is applied even when fill density is none";
}
@ -143,6 +146,7 @@ use Slic3r::Test;
$config->set('top_solid_layers', 3);
$config->set('solid_infill_speed', 99);
$config->set('top_solid_infill_speed', 99);
$config->set('bridge_speed', 99);
my $print = Slic3r::Test::init_print('sloping_hole', config => $config);
my %solid_layers = (); # Z => 1
@ -188,7 +192,7 @@ use Slic3r::Test;
push @z_steps, $info->{dist_Z}
if $started_extruding && $info->{dist_Z} > 0;
$travel_moves_after_first_extrusion++
if $info->{travel} && $started_extruding && !exists $args->{Z};
if $info->{travel} && $info->{dist_XY} > 0 && $started_extruding && !exists $args->{Z};
} elsif ($cmd eq 'M104') {
$first_layer_temperature_set = 1 if $args->{S} == 205;
$temperature_set = 1 if $args->{S} == 200;
@ -267,7 +271,7 @@ use Slic3r::Test;
foreach my $segment (@this_layer) {
# check that segment's dist_Z is proportioned to its dist_XY
$all_layer_segments_have_same_slope = 1
if abs($segment->[0]*$total_dist_XY/$config->layer_height - $segment->[1]) > 0.1;
if abs($segment->[0]*$total_dist_XY/$config->layer_height - $segment->[1]) > 0.2;
}
@this_layer = ();

View File

@ -1,4 +1,4 @@
use Test::More tests => 4;
use Test::More tests => 6;
use strict;
use warnings;
@ -79,6 +79,15 @@ use Slic3r::Test;
ok Slic3r::Test::gcode($print), 'successful G-code generation when skirt is smaller than brim width';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('skirts', 1);
$config->set('skirt_height', 0);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
ok Slic3r::Test::gcode($print), 'successful G-code generation when skirt_height = 0 and skirts > 0';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('layer_height', 0.4);
@ -124,4 +133,11 @@ use Slic3r::Test;
ok $skirt_length > $hull_perimeter, 'skirt lenght is large enough to contain object with support';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('min_skirt_length', 20);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
ok Slic3r::Test::gcode($print), 'no crash when using min_skirt_length';
}
__END__

View File

@ -1,4 +1,4 @@
use Test::More tests => 18;
use Test::More tests => 27;
use strict;
use warnings;
@ -20,17 +20,16 @@ use Slic3r::Test;
my $test = sub {
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
$print->print->init_extruders;
my $flow = $print->print->objects->[0]->support_material_flow;
my $support_z = Slic3r::Print::SupportMaterial
->new(
object_config => $print->print->objects->[0]->config,
print_config => $print->print->config,
flow => $flow,
interface_flow => $flow,
first_layer_flow => $flow,
)
->support_layers_z(\@contact_z, \@top_z, $config->layer_height);
my $support = Slic3r::Print::SupportMaterial->new(
object_config => $print->print->objects->[0]->config,
print_config => $print->print->config,
flow => $flow,
interface_flow => $flow,
first_layer_flow => $flow,
);
my $support_z = $support->support_layers_z(\@contact_z, \@top_z, $config->layer_height);
my $expected_top_spacing = $support->contact_distance($config->layer_height, $config->nozzle_diameter->[0]);
is $support_z->[0], $config->first_layer_height,
'first layer height is honored';
@ -44,9 +43,10 @@ use Slic3r::Test;
# find layer index of this top surface
my $layer_id = first { abs($support_z->[$_] - $top_z) < epsilon } 0..$#$support_z;
# check that first support layer above this top surface is spaced with nozzle diameter
# check that first support layer above this top surface (or the next one) is spaced with nozzle diameter
$wrong_top_spacing = 1
if ($support_z->[$layer_id+1] - $support_z->[$layer_id]) != $config->nozzle_diameter->[0];
if ($support_z->[$layer_id+1] - $support_z->[$layer_id]) != $expected_top_spacing
&& ($support_z->[$layer_id+2] - $support_z->[$layer_id]) != $expected_top_spacing;
}
ok !$wrong_top_spacing, 'layers above top surfaces are spaced correctly';
};
@ -67,12 +67,12 @@ use Slic3r::Test;
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('raft_layers', 3);
$config->set('brim_width', 6);
$config->set('brim_width', 0);
$config->set('skirts', 0);
$config->set('support_material_extruder', 2);
$config->set('support_material_interface_extruder', 2);
$config->set('layer_height', 0.4);
$config->set('first_layer_height', '100%');
$config->set('first_layer_height', 0.4);
my $print = Slic3r::Test::init_print('overhang', config => $config);
ok my $gcode = Slic3r::Test::gcode($print), 'no conflict between raft/support and brim';
@ -84,7 +84,7 @@ use Slic3r::Test;
$tool = $1;
} elsif ($info->{extruding}) {
if ($self->Z <= ($config->raft_layers * $config->layer_height)) {
fail 'not extruding raft/brim with support material extruder'
fail 'not extruding raft with support material extruder'
if $tool != ($config->support_material_extruder-1);
} else {
fail 'support material exceeds raft layers'
@ -102,10 +102,15 @@ use Slic3r::Test;
$config->set('support_material_pattern', 'honeycomb');
$config->set('support_material_extrusion_width', 0.6);
$config->set('first_layer_extrusion_width', '100%');
$config->set('bridge_speed', 99);
$config->set('cooling', 0); # prevent speed alteration
$config->set('first_layer_speed', '100%'); # prevent speed alteration
$config->set('start_gcode', ''); # prevent any unexpected Z move
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my $layer_id = 0;
my $layer_id = -1; # so that first Z move sets this to 0
my @raft = my @first_object_layer = ();
my %first_object_layer_speeds = (); # F => 1
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
@ -119,6 +124,7 @@ use Slic3r::Test;
push @raft, @path;
} else {
push @first_object_layer, @path;
$first_object_layer_speeds{ $args->{F} // $self->F } = 1;
}
}
} elsif ($cmd eq 'G1' && $info->{dist_Z} > 0) {
@ -128,6 +134,10 @@ use Slic3r::Test;
ok !@{diff(\@first_object_layer, \@raft)},
'first object layer is completely supported by raft';
is scalar(keys %first_object_layer_speeds), 1,
'only one speed used in first object layer';
ok +(keys %first_object_layer_speeds)[0] == $config->bridge_speed*60,
'bridge speed used in first object layer';
}
{
@ -169,4 +179,83 @@ use Slic3r::Test;
$test->(70);
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('brim_width', 0);
$config->set('skirts', 0);
$config->set('support_material', 1);
$config->set('top_solid_layers', 0); # so that we don't have the internal bridge over infill
$config->set('bridge_speed', 99);
$config->set('cooling', 0);
$config->set('first_layer_speed', '100%');
my $test = sub {
my $print = Slic3r::Test::init_print('overhang', config => $config);
my $has_bridge_speed = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{extruding}) {
if (($args->{F} // $self->F) == $config->bridge_speed*60) {
$has_bridge_speed = 1;
}
}
});
return $has_bridge_speed;
};
$config->set('support_material_contact_distance', 0.2);
ok $test->(), 'bridge speed is used when support_material_contact_distance > 0';
$config->set('support_material_contact_distance', 0);
ok !$test->(), 'bridge speed is not used when support_material_contact_distance == 0';
$config->set('raft_layers', 5);
$config->set('support_material_contact_distance', 0.2);
ok $test->(), 'bridge speed is used when raft_layers > 0 and support_material_contact_distance > 0';
$config->set('support_material_contact_distance', 0);
ok !$test->(), 'bridge speed is not used when raft_layers > 0 and support_material_contact_distance == 0';
}
{
my $config = Slic3r::Config->new_from_defaults;
$config->set('skirts', 0);
$config->set('start_gcode', '');
$config->set('raft_layers', 8);
$config->set('nozzle_diameter', [0.4, 1]);
$config->set('layer_height', 0.1);
$config->set('first_layer_height', 0.8);
$config->set('support_material_extruder', 2);
$config->set('support_material_interface_extruder', 2);
$config->set('support_material_contact_distance', 0);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
ok my $gcode = Slic3r::Test::gcode($print), 'first_layer_height is validated with support material extruder nozzle diameter when using raft layers';
my $tool = undef;
my @z = (0);
my %layer_heights_by_tool = (); # tool => [ lh, lh... ]
Slic3r::GCode::Reader->new->parse($gcode, sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd =~ /^T(\d+)/) {
$tool = $1;
} elsif ($cmd eq 'G1' && exists $args->{Z} && $args->{Z} != $self->Z) {
push @z, $args->{Z};
} elsif ($info->{extruding} && $info->{dist_XY} > 0) {
$layer_heights_by_tool{$tool} ||= [];
push @{ $layer_heights_by_tool{$tool} }, $z[-1] - $z[-2];
}
});
ok !defined(first { $_ > $config->nozzle_diameter->[0] + epsilon }
@{ $layer_heights_by_tool{$config->perimeter_extruder-1} }),
'no object layer is thicker than nozzle diameter';
ok !defined(first { abs($_ - $config->layer_height) < epsilon }
@{ $layer_heights_by_tool{$config->support_material_extruder-1} }),
'no support material layer is as thin as object layers';
}
__END__

View File

@ -1,4 +1,4 @@
use Test::More tests => 14;
use Test::More tests => 23;
use strict;
use warnings;
@ -8,7 +8,7 @@ BEGIN {
}
use Slic3r;
use List::Util qw(first sum);
use List::Util qw(first sum none);
use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon Y);
use Slic3r::Test;
@ -61,7 +61,7 @@ if (0) {
[160, 140],
);
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
my $res = $expolygon->medial_axis(scale 1, scale 0.5);
my $res = $expolygon->medial_axis(scale 40, scale 0.5);
is scalar(@$res), 1, 'medial axis of a square shape is a single path';
isa_ok $res->[0], 'Slic3r::Polyline', 'medial axis result is a polyline';
ok $res->[0]->first_point->coincides_with($res->[0]->last_point), 'polyline forms a closed loop';
@ -76,7 +76,7 @@ if (0) {
[120, 200],
[100, 200],
));
my $res = $expolygon->medial_axis(scale 1, scale 0.5);
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow rectangle is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
@ -91,7 +91,20 @@ if (0) {
is scalar(@$res), 1, 'medial axis of a narrow rectangle with an extra vertex is still a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has still a reasonable length';
ok !(grep { abs($_ - scale 150) < scaled_epsilon } map $_->[Y], map @$_, @$res2), "extra vertices don't influence medial axis";
}
{
my $expolygon = Slic3r::ExPolygon->new(
Slic3r::Polygon->new([1185881,829367],[1421988,1578184],[1722442,2303558],[2084981,2999998],[2506843,3662186],[2984809,4285086],[3515250,4863959],[4094122,5394400],[4717018,5872368],[5379210,6294226],[6075653,6656769],[6801033,6957229],[7549842,7193328],[8316383,7363266],[9094809,7465751],[9879211,7500000],[10663611,7465750],[11442038,7363265],[12208580,7193327],[12957389,6957228],[13682769,6656768],[14379209,6294227],[15041405,5872366],[15664297,5394401],[16243171,4863960],[16758641,4301424],[17251579,3662185],[17673439,3000000],[18035980,2303556],[18336441,1578177],[18572539,829368],[18750748,0],[19758422,0],[19727293,236479],[19538467,1088188],[19276136,1920196],[18942292,2726179],[18539460,3499999],[18070731,4235755],[17539650,4927877],[16950279,5571067],[16307090,6160437],[15614974,6691519],[14879209,7160248],[14105392,7563079],[13299407,7896927],[12467399,8159255],[11615691,8348082],[10750769,8461952],[9879211,8500000],[9007652,8461952],[8142729,8348082],[7291022,8159255],[6459015,7896927],[5653029,7563079],[4879210,7160247],[4143447,6691519],[3451331,6160437],[2808141,5571066],[2218773,4927878],[1687689,4235755],[1218962,3499999],[827499,2748020],[482284,1920196],[219954,1088186],[31126,236479],[0,0],[1005754,0]),
);
my $res = $expolygon->medial_axis(scale 1.324888, scale 0.25);
is scalar(@$res), 1, 'medial axis of a semicircumference is a single line';
# check whether turns are all CCW or all CW
my @lines = @{$res->[0]->lines};
my @angles = map { $lines[$_-1]->ccw($lines[$_]->b) } 1..$#lines;
ok !!(none { $_ < 0 } @angles) || (none { $_ > 0 } @angles),
'all medial axis segments of a semicircumference have the same orientation';
}
{
@ -101,7 +114,7 @@ if (0) {
[112, 200],
[108, 200],
));
my $res = $expolygon->medial_axis(scale 1, scale 0.5);
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow trapezoid is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
}
@ -115,7 +128,7 @@ if (0) {
[200, 200],
[100, 200],
));
my $res = $expolygon->medial_axis(scale 1, scale 0.5);
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a L shape is a single polyline';
my $len = unscale($res->[0]->length) + 20; # 20 is the thickness of the expolygon, which is subtracted from the ends
ok $len > 80*2 && $len < 100*2, 'medial axis has reasonable length';
@ -133,4 +146,39 @@ if (0) {
ok sum(map $_->length, @$polylines) > $perimeter/2/4*3, 'medial axis has a reasonable length';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[50, 100],
[1000, 102],
[50, 104],
));
my $res = $expolygon->medial_axis(scale 4, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow triangle is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
}
{
# GH #2474
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
[91294454,31032190],[11294481,31032190],[11294481,29967810],[44969182,29967810],[89909960,29967808],[91294454,29967808]
));
my $polylines = $expolygon->medial_axis(1871238, 500000);
is scalar(@$polylines), 1, 'medial axis is a single polyline';
my $polyline = $polylines->[0];
my $expected_y = $expolygon->bounding_box->center->y; #;;
ok abs(sum(map $_->y, @$polyline) / @$polyline - $expected_y) < scaled_epsilon, #,,
'medial axis is horizontal and is centered';
# order polyline from left to right
$polyline->reverse if $polyline->first_point->x > $polyline->last_point->x;
my $polyline_bb = $polyline->bounding_box;
is $polyline->first_point->x, $polyline_bb->x_min, 'expected x_min';
is $polyline->last_point->x, $polyline_bb->x_max, 'expected x_max';
is_deeply [ map $_->x, @$polyline ], [ sort map $_->x, @$polyline ],
'medial axis is not self-overlapping';
}
__END__

Some files were not shown because too many files have changed in this diff Show More