diff --git a/lib/Slic3r/GUI/Tab.pm b/lib/Slic3r/GUI/Tab.pm index 558b8c11a..ab8fa0bde 100644 --- a/lib/Slic3r/GUI/Tab.pm +++ b/lib/Slic3r/GUI/Tab.pm @@ -461,7 +461,7 @@ sub build { my $self = shift; $self->init_config_options(qw( - adaptive_slicing cusp_value match_horizontal_surfaces + adaptive_slicing adaptive_slicing_z_gradation cusp_value match_horizontal_surfaces layer_height first_layer_height perimeters spiral_vase top_solid_layers bottom_solid_layers @@ -510,6 +510,7 @@ sub build { $optgroup->append_single_option_line('first_layer_height'); $optgroup->append_single_option_line('adaptive_slicing'); $optgroup->append_single_option_line('cusp_value'); + $optgroup->append_single_option_line('adaptive_slicing_z_gradation'); $optgroup->append_single_option_line('match_horizontal_surfaces'); } { @@ -826,7 +827,7 @@ sub _update { my $have_adaptive_slicing = $config->adaptive_slicing; $self->get_field($_)->toggle($have_adaptive_slicing) - for qw(cusp_value match_horizontal_surfaces); + for qw(cusp_value adaptive_slicing_z_gradation match_horizontal_surfaces); $self->get_field($_)->toggle(!$have_adaptive_slicing) for qw(layer_height); diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index 5f050af9b..dbf3b52b6 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -231,8 +231,15 @@ sub slice { # generate layer objects $slice_z = 0; + my $gradation = $self->config->get_value('adaptive_slicing_z_gradation'); foreach my $z (@layers) { $height = $z - $slice_z; + + # apply z-gradation + if($gradation > 0) { + $height = $height - unscale((scale($height)) % (scale($gradation))); + } + $print_z += $height; $slice_z += $height/2; @@ -245,7 +252,7 @@ sub slice { $self->get_layer($lc - 1)->set_lower_layer($self->get_layer($lc - 2)); } - $id++; + $id++; $slice_z += $height/2; # add the other half layer } } diff --git a/t/adaptive_slicing.t b/t/adaptive_slicing.t index 2a2f0b311..6637c8db6 100644 --- a/t/adaptive_slicing.t +++ b/t/adaptive_slicing.t @@ -1,4 +1,4 @@ -use Test::More tests => 8; +use Test::More tests => 9; use strict; use warnings; @@ -8,14 +8,16 @@ BEGIN { use local::lib "$FindBin::Bin/../local-lib"; } -use List::Util qw(first); +use List::Util qw(first sum); use Slic3r; use Slic3r::Test qw(_eq); use Slic3r::Geometry qw(Z PI scale unscale); +use Devel::Peek; + my $config = Slic3r::Config->new_from_defaults; -my $test = sub { +my $generate_gcode = sub { my ($conf) = @_; $conf ||= $config; @@ -32,6 +34,12 @@ my $test = sub { } }); + return (@z); +}; + +my $horizontal_feature_test = sub { + my (@z) = $generate_gcode->(); + ok (_eq($z[0], $config->get_value('first_layer_height') + $config->z_offset), 'first layer height.'); ok (_eq($z[1], $config->get_value('first_layer_height') + $config->get('max_layer_height')->[0] + $config->z_offset), 'second layer height.'); @@ -41,6 +49,17 @@ my $test = sub { 1; }; +my $height_gradation_test = sub { + my (@z) = $generate_gcode->(); + + my $gradation = $config->get('adaptive_slicing_z_gradation'); + # +1 is a "dirty fix" to avoid rounding issues with the modulo operator... + my @results = map {unscale((scale($_)+1) % scale($gradation))} @z; + + ok (_eq(sum(@results), 0), 'layer z is multiple of gradation ' . $gradation ); + + 1; +}; @@ -109,7 +128,7 @@ $config->set('cusp_value', [0.19]); subtest 'shrink to match horizontal facets' => sub { plan skip_all => 'spline smoothing currently prevents exact horizontal facet matching'; plan tests => 3; - $test->(); + $horizontal_feature_test->(); }; # widen current layer to match horizontal facet @@ -118,7 +137,15 @@ $config->set('cusp_value', [0.1]); subtest 'widen to match horizontal facets' => sub { plan skip_all => 'spline smoothing currently prevents exact horizontal facet matching'; plan tests => 3; - $test->(); + $horizontal_feature_test->(); +}; + +subtest 'layer height gradation' => sub { + plan tests => 5; + foreach my $gradation (0.001, 0.01, 0.02, 0.05, 0.08) { + $config->set('adaptive_slicing_z_gradation', $gradation); + $height_gradation_test->(); + } }; __END__ diff --git a/xs/src/libslic3r/PrintConfig.cpp b/xs/src/libslic3r/PrintConfig.cpp index 2c0a4fa61..f30eef703 100644 --- a/xs/src/libslic3r/PrintConfig.cpp +++ b/xs/src/libslic3r/PrintConfig.cpp @@ -13,6 +13,14 @@ PrintConfigDef::PrintConfigDef() def->cli = "adaptive-slicing!"; def->default_value = new ConfigOptionBool(false); + def = this->add("adaptive_slicing_z_gradation", coFloat); + def->label = "Min layer height gradation"; + def->tooltip = "Limit layer heights to a multiple of this value to avoid stepping inaccuracies at the Z-axis. Typical value for a Prusa i3, 1/16 micro-stepping is 0.004mm. Set zero do disable this option."; + def->sidetext = "mm"; + def->cli = "adaptive-slicing-z-gradation=f"; + def->min = 0; + def->default_value = new ConfigOptionFloat(0); + def = this->add("avoid_crossing_perimeters", coBool); def->label = "Avoid crossing perimeters"; def->tooltip = "Optimize travel moves in order to minimize the crossing of perimeters. This is mostly useful with Bowden extruders which suffer from oozing. This feature slows down both the print and the G-code generation."; diff --git a/xs/src/libslic3r/PrintConfig.hpp b/xs/src/libslic3r/PrintConfig.hpp index 1c418173c..3962264d0 100644 --- a/xs/src/libslic3r/PrintConfig.hpp +++ b/xs/src/libslic3r/PrintConfig.hpp @@ -140,6 +140,7 @@ class PrintObjectConfig : public virtual StaticPrintConfig { public: ConfigOptionBool adaptive_slicing; + ConfigOptionFloat adaptive_slicing_z_gradation; ConfigOptionFloat cusp_value; ConfigOptionBool dont_support_bridges; ConfigOptionFloatOrPercent extrusion_width; @@ -173,6 +174,7 @@ class PrintObjectConfig : public virtual StaticPrintConfig virtual ConfigOption* optptr(const t_config_option_key &opt_key, bool create = false) { OPT_PTR(adaptive_slicing); + OPT_PTR(adaptive_slicing_z_gradation); OPT_PTR(cusp_value); OPT_PTR(dont_support_bridges); OPT_PTR(extrusion_width); diff --git a/xs/src/libslic3r/PrintObject.cpp b/xs/src/libslic3r/PrintObject.cpp index 54b16262e..2ea2ba369 100644 --- a/xs/src/libslic3r/PrintObject.cpp +++ b/xs/src/libslic3r/PrintObject.cpp @@ -245,6 +245,7 @@ PrintObject::invalidate_state_by_config_options(const std::vector