2016-09-14 09:22:41 +00:00
|
|
|
|
# Cut an object at a Z position, keep either the top or the bottom of the object.
|
|
|
|
|
# This dialog gets opened with the "Cut..." button above the platter.
|
|
|
|
|
|
2014-04-25 12:54:08 +00:00
|
|
|
|
package Slic3r::GUI::Plater::ObjectCutDialog;
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use utf8;
|
|
|
|
|
|
2014-06-14 18:52:21 +00:00
|
|
|
|
use Slic3r::Geometry qw(PI X);
|
2015-12-19 22:15:37 +00:00
|
|
|
|
use Wx qw(wxTheApp :dialog :id :misc :sizer wxTAB_TRAVERSAL);
|
2014-04-25 12:54:08 +00:00
|
|
|
|
use Wx::Event qw(EVT_CLOSE EVT_BUTTON);
|
2018-05-18 11:02:47 +00:00
|
|
|
|
#==============================================================================================================================
|
|
|
|
|
use List::Util qw(max);
|
|
|
|
|
#==============================================================================================================================
|
2014-04-25 12:54:08 +00:00
|
|
|
|
use base 'Wx::Dialog';
|
|
|
|
|
|
|
|
|
|
sub new {
|
2017-07-12 21:28:11 +00:00
|
|
|
|
my ($class, $parent, %params) = @_;
|
2014-04-25 12:54:08 +00:00
|
|
|
|
my $self = $class->SUPER::new($parent, -1, $params{object}->name, wxDefaultPosition, [500,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
|
|
|
|
|
$self->{model_object} = $params{model_object};
|
|
|
|
|
$self->{new_model_objects} = [];
|
2016-06-03 08:35:39 +00:00
|
|
|
|
# Mark whether the mesh cut is valid.
|
|
|
|
|
# If not, it needs to be recalculated by _update() on wxTheApp->CallAfter() or on exit of the dialog.
|
|
|
|
|
$self->{mesh_cut_valid} = 0;
|
|
|
|
|
# Note whether the window was already closed, so a pending update is not executed.
|
|
|
|
|
$self->{already_closed} = 0;
|
2014-04-25 12:54:08 +00:00
|
|
|
|
|
|
|
|
|
# cut options
|
|
|
|
|
$self->{cut_options} = {
|
2014-04-25 15:50:03 +00:00
|
|
|
|
z => 0,
|
|
|
|
|
keep_upper => 1,
|
|
|
|
|
keep_lower => 1,
|
|
|
|
|
rotate_lower => 1,
|
2016-11-13 22:35:56 +00:00
|
|
|
|
# preview => 1,
|
|
|
|
|
# Disabled live preview by default as it is not stable and/or the calculation takes too long for interactive usage.
|
|
|
|
|
preview => 0,
|
2014-04-25 12:54:08 +00:00
|
|
|
|
};
|
2014-07-01 14:40:56 +00:00
|
|
|
|
|
|
|
|
|
my $optgroup;
|
2014-07-01 16:22:19 +00:00
|
|
|
|
$optgroup = $self->{optgroup} = Slic3r::GUI::OptionsGroup->new(
|
2014-07-01 14:40:56 +00:00
|
|
|
|
parent => $self,
|
|
|
|
|
title => 'Cut',
|
|
|
|
|
on_change => sub {
|
|
|
|
|
my ($opt_id) = @_;
|
2016-06-03 08:35:39 +00:00
|
|
|
|
# There seems to be an issue with wxWidgets 3.0.2/3.0.3, where the slider
|
|
|
|
|
# genates tens of events for a single value change.
|
|
|
|
|
# Only trigger the recalculation if the value changes
|
|
|
|
|
# or a live preview was activated and the mesh cut is not valid yet.
|
|
|
|
|
if ($self->{cut_options}{$opt_id} != $optgroup->get_value($opt_id) ||
|
|
|
|
|
! $self->{mesh_cut_valid} && $self->_life_preview_active()) {
|
|
|
|
|
$self->{cut_options}{$opt_id} = $optgroup->get_value($opt_id);
|
|
|
|
|
$self->{mesh_cut_valid} = 0;
|
|
|
|
|
wxTheApp->CallAfter(sub {
|
|
|
|
|
$self->_update;
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-04-25 12:54:08 +00:00
|
|
|
|
},
|
2014-07-01 14:40:56 +00:00
|
|
|
|
label_width => 120,
|
2014-04-25 12:54:08 +00:00
|
|
|
|
);
|
2014-07-01 14:40:56 +00:00
|
|
|
|
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
|
|
|
|
|
opt_id => 'z',
|
|
|
|
|
type => 'slider',
|
|
|
|
|
label => 'Z',
|
|
|
|
|
default => $self->{cut_options}{z},
|
|
|
|
|
min => 0,
|
2018-04-13 12:40:42 +00:00
|
|
|
|
max => $self->{model_object}->bounding_box->size->z,
|
2014-07-04 10:04:42 +00:00
|
|
|
|
full_width => 1,
|
2014-07-01 14:40:56 +00:00
|
|
|
|
));
|
|
|
|
|
{
|
|
|
|
|
my $line = Slic3r::GUI::OptionsGroup::Line->new(
|
|
|
|
|
label => 'Keep',
|
|
|
|
|
);
|
|
|
|
|
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
|
|
|
|
|
opt_id => 'keep_upper',
|
|
|
|
|
type => 'bool',
|
|
|
|
|
label => 'Upper part',
|
|
|
|
|
default => $self->{cut_options}{keep_upper},
|
|
|
|
|
));
|
|
|
|
|
$line->append_option(Slic3r::GUI::OptionsGroup::Option->new(
|
|
|
|
|
opt_id => 'keep_lower',
|
|
|
|
|
type => 'bool',
|
|
|
|
|
label => 'Lower part',
|
|
|
|
|
default => $self->{cut_options}{keep_lower},
|
|
|
|
|
));
|
|
|
|
|
$optgroup->append_line($line);
|
|
|
|
|
}
|
|
|
|
|
$optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
|
|
|
|
|
opt_id => 'rotate_lower',
|
|
|
|
|
label => 'Rotate lower part upwards',
|
|
|
|
|
type => 'bool',
|
|
|
|
|
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},
|
|
|
|
|
));
|
2015-12-13 17:13:49 +00:00
|
|
|
|
$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},
|
|
|
|
|
));
|
2014-07-01 14:40:56 +00:00
|
|
|
|
{
|
|
|
|
|
my $cut_button_sizer = Wx::BoxSizer->new(wxVERTICAL);
|
|
|
|
|
$self->{btn_cut} = Wx::Button->new($self, -1, "Perform cut", wxDefaultPosition, wxDefaultSize);
|
|
|
|
|
$cut_button_sizer->Add($self->{btn_cut}, 0, wxALIGN_RIGHT | wxALL, 10);
|
|
|
|
|
$optgroup->append_line(Slic3r::GUI::OptionsGroup::Line->new(
|
|
|
|
|
sizer => $cut_button_sizer,
|
|
|
|
|
));
|
|
|
|
|
}
|
2014-04-25 12:54:08 +00:00
|
|
|
|
|
|
|
|
|
# left pane with tree
|
|
|
|
|
my $left_sizer = Wx::BoxSizer->new(wxVERTICAL);
|
|
|
|
|
$left_sizer->Add($optgroup->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
|
|
|
|
|
|
|
|
|
|
# right pane with preview canvas
|
|
|
|
|
my $canvas;
|
|
|
|
|
if ($Slic3r::GUI::have_OpenGL) {
|
2015-01-09 00:30:04 +00:00
|
|
|
|
$canvas = $self->{canvas} = Slic3r::GUI::3DScene->new($self);
|
2016-12-12 17:02:24 +00:00
|
|
|
|
$canvas->load_object($self->{model_object}, undef, undef, [0]);
|
2018-05-15 13:38:25 +00:00
|
|
|
|
#==============================================================================================================================
|
|
|
|
|
Slic3r::GUI::_3DScene::set_auto_bed_shape($canvas);
|
2018-05-18 11:02:47 +00:00
|
|
|
|
Slic3r::GUI::_3DScene::set_axes_length($canvas, 2.0 * max(@{ $canvas->volumes_bounding_box->size }));
|
2018-05-15 13:38:25 +00:00
|
|
|
|
# $canvas->set_auto_bed_shape;
|
|
|
|
|
#==============================================================================================================================
|
2014-04-25 12:54:08 +00:00
|
|
|
|
$canvas->SetSize([500,500]);
|
|
|
|
|
$canvas->SetMinSize($canvas->GetSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
|
|
|
|
|
$self->{sizer}->Add($left_sizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
|
|
|
|
|
$self->{sizer}->Add($canvas, 1, wxEXPAND | wxALL, 0) if $canvas;
|
|
|
|
|
|
|
|
|
|
$self->SetSizer($self->{sizer});
|
|
|
|
|
$self->SetMinSize($self->GetSize);
|
|
|
|
|
$self->{sizer}->SetSizeHints($self);
|
|
|
|
|
|
2015-12-13 17:13:49 +00:00
|
|
|
|
EVT_BUTTON($self, $self->{btn_cut}, sub {
|
2016-06-03 08:35:39 +00:00
|
|
|
|
# Recalculate the cut if the preview was not active.
|
|
|
|
|
$self->_perform_cut() unless $self->{mesh_cut_valid};
|
|
|
|
|
|
|
|
|
|
# Adjust position / orientation of the split object halves.
|
2015-12-19 22:15:37 +00:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 08:35:39 +00:00
|
|
|
|
# Note that the window was already closed, so a pending update will not be executed.
|
|
|
|
|
$self->{already_closed} = 1;
|
2015-12-19 19:41:47 +00:00
|
|
|
|
$self->EndModal(wxID_OK);
|
2018-05-09 08:47:04 +00:00
|
|
|
|
#=============================================================================================================================
|
|
|
|
|
$self->{canvas}->Destroy;
|
|
|
|
|
#=============================================================================================================================
|
2016-06-03 08:35:39 +00:00
|
|
|
|
$self->Destroy();
|
2015-12-13 17:13:49 +00:00
|
|
|
|
});
|
2016-06-03 08:35:39 +00:00
|
|
|
|
|
|
|
|
|
EVT_CLOSE($self, sub {
|
|
|
|
|
# Note that the window was already closed, so a pending update will not be executed.
|
|
|
|
|
$self->{already_closed} = 1;
|
|
|
|
|
$self->EndModal(wxID_CANCEL);
|
2018-05-09 08:47:04 +00:00
|
|
|
|
#=============================================================================================================================
|
|
|
|
|
$self->{canvas}->Destroy;
|
|
|
|
|
#=============================================================================================================================
|
2016-06-03 08:35:39 +00:00
|
|
|
|
$self->Destroy();
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-01 16:22:19 +00:00
|
|
|
|
$self->_update;
|
|
|
|
|
|
2014-04-25 12:54:08 +00:00
|
|
|
|
return $self;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 08:35:39 +00:00
|
|
|
|
# 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
|
|
|
|
|
sub _mesh_slice_z_pos
|
|
|
|
|
{
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
return $self->{cut_options}{z} / $self->{model_object}->instances->[0]->scaling_factor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Only perform live preview if just a single part of the object shall survive.
|
|
|
|
|
sub _life_preview_active
|
|
|
|
|
{
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
return $self->{cut_options}{preview} && ($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Slice the mesh, keep the top / bottom part.
|
|
|
|
|
sub _perform_cut
|
|
|
|
|
{
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
|
|
# Early exit. If the cut is valid, don't recalculate it.
|
|
|
|
|
return if $self->{mesh_cut_valid};
|
|
|
|
|
|
|
|
|
|
my $z = $self->_mesh_slice_z_pos();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$self->{mesh_cut_valid} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 16:22:19 +00:00
|
|
|
|
sub _update {
|
|
|
|
|
my ($self) = @_;
|
2016-06-03 08:35:39 +00:00
|
|
|
|
|
|
|
|
|
# Don't update if the window was already closed.
|
|
|
|
|
# We are not sure whether the action planned by wxTheApp->CallAfter() may be triggered after the window is closed.
|
|
|
|
|
# Probably not, but better be safe than sorry, which is espetially true on multiple platforms.
|
|
|
|
|
return if $self->{already_closed};
|
|
|
|
|
|
|
|
|
|
# Only recalculate the cut, if the live cut preview is active.
|
|
|
|
|
my $life_preview_active = $self->_life_preview_active();
|
|
|
|
|
$self->_perform_cut() if $life_preview_active;
|
|
|
|
|
|
2015-12-19 22:15:37 +00:00
|
|
|
|
{
|
|
|
|
|
# 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
|
2016-06-03 08:35:39 +00:00
|
|
|
|
my $z = $self->_mesh_slice_z_pos();
|
|
|
|
|
|
2015-12-19 22:15:37 +00:00
|
|
|
|
|
|
|
|
|
# update canvas
|
|
|
|
|
if ($self->{canvas}) {
|
|
|
|
|
# get volumes to render
|
|
|
|
|
my @objects = ();
|
2016-06-03 08:35:39 +00:00
|
|
|
|
if ($life_preview_active) {
|
2015-12-19 22:15:37 +00:00
|
|
|
|
push @objects, values %{$self->{new_model_objects}};
|
|
|
|
|
} else {
|
|
|
|
|
push @objects, $self->{model_object};
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 13:16:39 +00:00
|
|
|
|
my $z_cut = $z + $self->{model_object}->bounding_box->z_min;
|
|
|
|
|
|
2015-12-19 22:15:37 +00:00
|
|
|
|
# get section contour
|
|
|
|
|
my @expolygons = ();
|
|
|
|
|
foreach my $volume (@{$self->{model_object}->volumes}) {
|
|
|
|
|
next if !$volume->mesh;
|
|
|
|
|
next if $volume->modifier;
|
2018-04-25 13:16:39 +00:00
|
|
|
|
my $expp = $volume->mesh->slice([ $z_cut ])->[0];
|
2015-12-19 22:15:37 +00:00
|
|
|
|
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;
|
2016-12-12 17:02:24 +00:00
|
|
|
|
$self->{canvas}->load_object($_, undef, undef, [0]) for @objects;
|
2015-12-19 22:15:37 +00:00
|
|
|
|
$self->{canvas}->SetCuttingPlane(
|
2018-04-13 12:40:42 +00:00
|
|
|
|
$self->{cut_options}{z},
|
2015-12-19 22:15:37 +00:00
|
|
|
|
[@expolygons],
|
|
|
|
|
);
|
2018-04-05 10:52:29 +00:00
|
|
|
|
$self->{canvas}->update_volumes_colors_by_extruder($self->GetParent->{config});
|
2015-12-19 22:15:37 +00:00
|
|
|
|
$self->{canvas}->Render;
|
2015-12-13 17:13:49 +00:00
|
|
|
|
}
|
2014-07-01 16:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# update controls
|
2015-12-19 22:15:37 +00:00
|
|
|
|
{
|
|
|
|
|
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});
|
2016-11-13 22:35:56 +00:00
|
|
|
|
# Disabled live preview by default as it is not stable and/or the calculation takes too long for interactive usage.
|
|
|
|
|
# $optgroup->get_field('preview')->toggle($self->{cut_options}{keep_upper} != $self->{cut_options}{keep_lower});
|
2014-08-03 08:51:01 +00:00
|
|
|
|
|
2015-12-19 22:15:37 +00:00
|
|
|
|
# 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;
|
2014-04-25 15:50:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-25 12:54:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub NewModelObjects {
|
|
|
|
|
my ($self) = @_;
|
2015-12-19 22:15:37 +00:00
|
|
|
|
return values %{ $self->{new_model_objects} };
|
2014-04-25 12:54:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|