From f670acb4b15fc3c2bc1cf47a2adb98b4408326c7 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 10 Mar 2013 14:58:49 +0100 Subject: [PATCH] Slice a single object with multiple layer heights. :-) --- lib/Slic3r/GUI/Plater.pm | 11 ++- lib/Slic3r/GUI/Plater/ObjectDialog.pm | 127 +++++++++++++++++++++++++- lib/Slic3r/Model.pm | 1 + lib/Slic3r/Print.pm | 3 +- lib/Slic3r/Print/Object.pm | 12 ++- 5 files changed, 141 insertions(+), 13 deletions(-) diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 58f30d652..3b86a597c 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -708,6 +708,7 @@ sub make_model { my $new_model_object = $model->add_object( vertices => $model_object->vertices, input_file => $plater_object->input_file, + layer_height_ranges => $plater_object->layer_height_ranges, ); foreach my $volume (@{$model_object->volumes}) { $new_model_object->add_volume( @@ -1073,6 +1074,7 @@ has 'rotate' => (is => 'rw', default => sub { 0 }); has 'instances' => (is => 'rw', default => sub { [] }); # upward Y axis has 'thumbnail' => (is => 'rw'); has 'thumbnail_scaling_factor' => (is => 'rw'); +has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ] # statistics has 'facets' => (is => 'rw'); @@ -1109,9 +1111,12 @@ sub free_model_object { sub get_model_object { my $self = shift; - return $self->model_object if $self->model_object; - my $model = Slic3r::Model->read_from_file($self->input_file); - return $model->objects->[$self->input_file_object_id]; + my $object = $self->model_object; + if (!$object) { + my $model = Slic3r::Model->read_from_file($self->input_file); + $object = $model->objects->[$self->input_file_object_id]; + } + return $object; } sub instances_count { diff --git a/lib/Slic3r/GUI/Plater/ObjectDialog.pm b/lib/Slic3r/GUI/Plater/ObjectDialog.pm index 3f0dd5615..82da6beb2 100644 --- a/lib/Slic3r/GUI/Plater/ObjectDialog.pm +++ b/lib/Slic3r/GUI/Plater/ObjectDialog.pm @@ -4,7 +4,7 @@ use warnings; use utf8; use Wx qw(:dialog :id :misc :sizer :systemsettings :notebook wxTAB_TRAVERSAL); -use Wx::Event qw(EVT_BUTTON EVT_TEXT_ENTER); +use Wx::Event qw(EVT_BUTTON); use base 'Wx::Dialog'; sub new { @@ -14,10 +14,19 @@ sub new { $self->{object} = $params{object}; $self->{tabpanel} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL); - $self->{tabpanel}->AddPage($self->{info} = Slic3r::GUI::Plater::ObjectDialog::InfoTab->new($self->{tabpanel}), "Info"); + $self->{tabpanel}->AddPage($self->{info} = Slic3r::GUI::Plater::ObjectDialog::InfoTab->new($self->{tabpanel}, object => $self->{object}), "Info"); + $self->{tabpanel}->AddPage($self->{layers} = Slic3r::GUI::Plater::ObjectDialog::LayersTab->new($self->{tabpanel}, object => $self->{object}), "Layers"); my $buttons = $self->CreateStdDialogButtonSizer(wxOK); - EVT_BUTTON($self, wxID_OK, sub { $self->EndModal(wxID_OK); }); + EVT_BUTTON($self, wxID_OK, sub { + # validate user input + return if !$self->{layers}->CanClose; + + # notify tabs + $self->{layers}->Closing; + + $self->EndModal(wxID_OK); + }); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{tabpanel}, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10); @@ -30,13 +39,13 @@ sub new { package Slic3r::GUI::Plater::ObjectDialog::InfoTab; use Wx qw(:dialog :id :misc :sizer :systemsettings); -use Wx::Event qw(EVT_BUTTON EVT_TEXT_ENTER); use base 'Wx::Panel'; sub new { my $class = shift; my ($parent, %params) = @_; my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize); + $self->{object} = $params{object}; my $grid_sizer = Wx::FlexGridSizer->new(3, 2, 10, 5); $grid_sizer->SetFlexibleDirection(wxHORIZONTAL); @@ -63,7 +72,7 @@ sub new { sub get_properties { my $self = shift; - my $object = $self->GetParent->GetParent->{object}; + my $object = $self->{object}; return [ ['Name' => $object->name], ['Size' => sprintf "%.2f x %.2f x %.2f", @{$object->size}], @@ -74,4 +83,112 @@ sub get_properties { ]; } +package Slic3r::GUI::Plater::ObjectDialog::LayersTab; +use Wx qw(:dialog :id :misc :sizer :systemsettings); +use Wx::Grid; +use Wx::Event qw(EVT_GRID_CELL_CHANGED); +use base 'Wx::Panel'; + +sub new { + my $class = shift; + my ($parent, %params) = @_; + my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize); + $self->{object} = $params{object}; + + my $sizer = Wx::BoxSizer->new(wxVERTICAL); + + { + my $label = Wx::StaticText->new($self, -1, "You can use this section to override the default layer height for parts of this object.", + wxDefaultPosition, [-1, 25]); + $label->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT)); + $sizer->Add($label, 0, wxEXPAND | wxALL, 10); + } + + my $grid = $self->{grid} = Wx::Grid->new($self, -1, wxDefaultPosition, wxDefaultSize); + $sizer->Add($grid, 1, wxEXPAND | wxALL, 10); + $grid->CreateGrid(0, 3); + $grid->DisableDragRowSize; + $grid->HideRowLabels; + $grid->SetColLabelValue(0, "Min Z"); + $grid->SetColLabelValue(1, "Max Z"); + $grid->SetColLabelValue(2, "Layer height"); + $grid->SetColSize($_, 100) for 0..2; + $grid->SetDefaultCellAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE); + + # load data + foreach my $range (@{ $self->{object}->layer_height_ranges }) { + $grid->AppendRows(1); + my $i = $grid->GetNumberRows-1; + $grid->SetCellValue($i, $_, $range->[$_]) for 0..2; + } + $grid->AppendRows(1); # append one empty row + + EVT_GRID_CELL_CHANGED($grid, sub { + my ($grid, $event) = @_; + + # remove any non-numeric character + my $value = $grid->GetCellValue($event->GetRow, $event->GetCol); + $value =~ s/,/./g; + $value =~ s/[^0-9.]//g; + $grid->SetCellValue($event->GetRow, $event->GetCol, $value); + + # if there's no empty row, let's append one + for my $i (0 .. $grid->GetNumberRows-1) { + if (!grep $grid->GetCellValue($i, $_), 0..2) { + return; + } + } + $grid->AppendRows(1); + }); + + $self->SetSizer($sizer); + $sizer->SetSizeHints($self); + + return $self; +} + +sub CanClose { + my $self = shift; + + # validate ranges before allowing user to dismiss the dialog + + foreach my $range ($self->_get_ranges) { + my ($min, $max, $height) = @$range; + if ($max <= $min) { + Slic3r::GUI::show_error($self, "Invalid Z range $min-$max."); + return 0; + } + if ($min < 0 || $max < 0) { + Slic3r::GUI::show_error($self, "Invalid Z range $min-$max."); + return 0; + } + if ($height <= 0) { + Slic3r::GUI::show_error($self, "Invalid layer height $height."); + return 0; + } + # TODO: check for overlapping ranges + } + + return 1; +} + +sub Closing { + my $self = shift; + + # save ranges into the plater object + $self->{object}->layer_height_ranges([ $self->_get_ranges ]); +} + +sub _get_ranges { + my $self = shift; + + my @ranges = (); + for my $i (0 .. $self->{grid}->GetNumberRows-1) { + my ($min, $max, $height) = map $self->{grid}->GetCellValue($i, $_), 0..2; + next if $min eq '' || $max eq '' || $height eq ''; + push @ranges, [ $min, $max, $height ]; + } + return sort { $a->[0] <=> $b->[0] } @ranges; +} + 1; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 67793dd14..fef1f8346 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -81,6 +81,7 @@ has 'model' => (is => 'ro', weak_ref => 1, required => 1); has 'vertices' => (is => 'ro', default => sub { [] }); has 'volumes' => (is => 'ro', default => sub { [] }); has 'instances' => (is => 'rw'); +has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ] sub add_volume { my $self = shift; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index b20547a14..d561759f8 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -119,7 +119,8 @@ sub add_model { print => $self, meshes => [ @meshes ], size => [ $complete_mesh->size ], - input_file => $object->input_file + input_file => $object->input_file, + layer_height_ranges => $object->layer_height_ranges, ); push @{$self->objects}, $print_object; diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index 26fec8186..8a914b9d2 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -1,7 +1,7 @@ package Slic3r::Print::Object; use Moo; -use List::Util qw(min sum); +use List::Util qw(min sum first); use Slic3r::ExtrusionPath ':roles'; use Slic3r::Geometry qw(Z PI scale unscale deg2rad rad2deg scaled_epsilon); use Slic3r::Geometry::Clipper qw(diff_ex intersection_ex union_ex); @@ -13,6 +13,7 @@ has 'meshes' => (is => 'rw', default => sub { [] }); # by region_id has 'size' => (is => 'rw', required => 1); has 'copies' => (is => 'rw', default => sub {[ [0,0] ]}); has 'layers' => (is => 'rw', default => sub { [] }); +has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ] sub BUILD { my $self = shift; @@ -21,9 +22,12 @@ sub BUILD { my $print_z = my $slice_z = my $raft_z = 0; while (!@{$self->layers} || $self->layers->[-1]->slice_z < $self->size->[Z]) { my $id = $#{$self->layers} + 1; - my $height = $id == 0 - ? $Slic3r::Config->get_value('first_layer_height') - : $Slic3r::Config->layer_height; + my $height = $Slic3r::Config->layer_height; + $height = $Slic3r::Config->get_value('first_layer_height') if $id == 0; + if (my $range = first { $_->[0] <= ($print_z + $_->[2]) && $_->[1] >= ($print_z + $_->[2]) } @{$self->layer_height_ranges}) { + $height = $range->[2]; + } + $print_z += $height; if ($id < $Slic3r::Config->raft_layers) {