2012-08-29 14:49:38 +00:00
|
|
|
|
package Slic3r::Model;
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
use List::Util qw(first max);
|
2014-04-25 15:50:03 +00:00
|
|
|
|
use Slic3r::Geometry qw(X Y Z move_points);
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
2012-08-29 17:37:27 +00:00
|
|
|
|
sub read_from_file {
|
|
|
|
|
my $class = shift;
|
|
|
|
|
my ($input_file) = @_;
|
|
|
|
|
|
|
|
|
|
my $model = $input_file =~ /\.stl$/i ? Slic3r::Format::STL->read_file($input_file)
|
|
|
|
|
: $input_file =~ /\.obj$/i ? Slic3r::Format::OBJ->read_file($input_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";
|
|
|
|
|
|
2014-04-29 23:04:49 +00:00
|
|
|
|
$_->set_input_file($input_file) for @{$model->objects};
|
2012-08-29 17:37:27 +00:00
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
sub merge {
|
|
|
|
|
my $class = shift;
|
|
|
|
|
my @models = @_;
|
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
|
my $new_model = ref($class)
|
|
|
|
|
? $class
|
|
|
|
|
: $class->new;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
$new_model->add_object($_) for map @{$_->objects}, @models;
|
|
|
|
|
return $new_model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub add_object {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
if (@_ == 1) {
|
|
|
|
|
# we have a Model::Object
|
|
|
|
|
my ($object) = @_;
|
2014-05-09 12:24:35 +00:00
|
|
|
|
return $self->_add_object_clone($object);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
} else {
|
2014-04-29 23:04:49 +00:00
|
|
|
|
my (%args) = @_;
|
2014-05-09 12:24:35 +00:00
|
|
|
|
|
|
|
|
|
my $new_object = $self->_add_object;
|
|
|
|
|
|
2014-07-12 09:20:57 +00:00
|
|
|
|
$new_object->set_name($args{name})
|
|
|
|
|
if defined $args{name};
|
2014-05-09 12:24:35 +00:00
|
|
|
|
$new_object->set_input_file($args{input_file})
|
|
|
|
|
if defined $args{input_file};
|
|
|
|
|
$new_object->config->apply($args{config})
|
|
|
|
|
if defined $args{config};
|
|
|
|
|
$new_object->set_layer_height_ranges($args{layer_height_ranges})
|
|
|
|
|
if defined $args{layer_height_ranges};
|
|
|
|
|
$new_object->set_origin_translation($args{origin_translation})
|
|
|
|
|
if defined $args{origin_translation};
|
|
|
|
|
|
|
|
|
|
return $new_object;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-22 17:04:36 +00:00
|
|
|
|
sub set_material {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($material_id, $attributes) = @_;
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
|
my $material = $self->add_material($material_id);
|
|
|
|
|
$material->apply($attributes // {});
|
2014-05-06 22:22:56 +00:00
|
|
|
|
return $material;
|
2014-04-19 09:43:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub duplicate_objects_grid {
|
|
|
|
|
my ($self, $grid, $distance) = @_;
|
2014-04-29 23:04:49 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
die "Grid duplication is not supported with multiple objects\n"
|
|
|
|
|
if @{$self->objects} > 1;
|
2014-04-29 23:04:49 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my $object = $self->objects->[0];
|
2014-04-29 23:04:49 +00:00
|
|
|
|
$object->clear_instances;
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my $size = $object->bounding_box->size;
|
|
|
|
|
for my $x_copy (1..$grid->[X]) {
|
|
|
|
|
for my $y_copy (1..$grid->[Y]) {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
$object->add_instance(
|
2014-05-09 12:24:35 +00:00
|
|
|
|
offset => Slic3r::Pointf->new(
|
2013-12-12 19:19:33 +00:00
|
|
|
|
($size->[X] + $distance) * ($x_copy-1),
|
|
|
|
|
($size->[Y] + $distance) * ($y_copy-1),
|
2014-05-09 12:24:35 +00:00
|
|
|
|
),
|
2013-12-12 19:19:33 +00:00
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# this will append more instances to each object
|
|
|
|
|
# and then automatically rearrange everything
|
|
|
|
|
sub duplicate_objects {
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my ($self, $copies_num, $distance, $bb) = @_;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
|
my @instances = @{$object->instances};
|
|
|
|
|
foreach my $instance (@instances) {
|
2014-05-09 12:24:35 +00:00
|
|
|
|
$object->add_instance($instance) for 2..$copies_num;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
2013-12-23 23:30:51 +00:00
|
|
|
|
$self->arrange_objects($distance, $bb);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# arrange objects preserving their instance count
|
|
|
|
|
# but altering their instance positions
|
|
|
|
|
sub arrange_objects {
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my ($self, $distance, $bb) = @_;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# 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};
|
|
|
|
|
}
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
2014-04-29 23:04:49 +00:00
|
|
|
|
$_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$object->update_bounding_box;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# duplicate the entire model preserving instance relative positions
|
|
|
|
|
sub duplicate {
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my ($self, $copies_num, $distance, $bb) = @_;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
my $model_size = $self->bounding_box->size;
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my @positions = $self->_arrange([ map $model_size, 2..$copies_num ], $distance, $bb);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
# note that this will leave the object count unaltered
|
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
2013-12-24 00:13:02 +00:00
|
|
|
|
my @instances = @{$object->instances}; # store separately to avoid recursion from add_instance() below
|
|
|
|
|
foreach my $instance (@instances) {
|
2013-12-12 19:19:33 +00:00
|
|
|
|
foreach my $pos (@positions) {
|
|
|
|
|
$object->add_instance(
|
2014-05-09 12:24:35 +00:00
|
|
|
|
offset => Slic3r::Pointf->new($instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y]),
|
2013-12-12 19:19:33 +00:00
|
|
|
|
rotation => $instance->rotation,
|
|
|
|
|
scaling_factor => $instance->scaling_factor,
|
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$object->update_bounding_box;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub _arrange {
|
2013-12-23 23:30:51 +00:00
|
|
|
|
my ($self, $sizes, $distance, $bb) = @_;
|
|
|
|
|
|
2014-06-16 12:05:22 +00:00
|
|
|
|
# we supply unscaled data to arrange()
|
2013-12-23 23:30:51 +00:00
|
|
|
|
return Slic3r::Geometry::arrange(
|
|
|
|
|
scalar(@$sizes), # number of parts
|
2014-01-07 11:48:09 +00:00
|
|
|
|
max(map $_->x, @$sizes), # cell width
|
|
|
|
|
max(map $_->y, @$sizes), # cell height ,
|
2013-12-23 23:30:51 +00:00
|
|
|
|
$distance, # distance between cells
|
|
|
|
|
$bb, # bounding box of the area to fill (can be undef)
|
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
sub print_info {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
$_->print_info for @{$self->objects};
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-25 17:52:32 +00:00
|
|
|
|
sub get_material_name {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($material_id) = @_;
|
|
|
|
|
|
|
|
|
|
my $name;
|
2014-04-29 23:04:49 +00:00
|
|
|
|
if ($self->has_material($material_id)) {
|
|
|
|
|
$name //= $self->get_material($material_id)
|
|
|
|
|
->attributes->{$_} for qw(Name name);
|
2013-08-25 17:52:32 +00:00
|
|
|
|
}
|
|
|
|
|
$name //= $material_id;
|
|
|
|
|
return $name;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 17:29:10 +00:00
|
|
|
|
package Slic3r::Model::Material;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
|
sub apply {
|
|
|
|
|
my ($self, $attributes) = @_;
|
|
|
|
|
$self->set_attribute($_, $attributes{$_}) for keys %$attributes;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
package Slic3r::Model::Object;
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
use File::Basename qw(basename);
|
2013-08-25 16:01:59 +00:00
|
|
|
|
use List::Util qw(first sum);
|
2014-04-25 15:50:03 +00:00
|
|
|
|
use Slic3r::Geometry qw(X Y Z rad2deg);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
sub add_volume {
|
|
|
|
|
my $self = shift;
|
2014-04-29 23:04:49 +00:00
|
|
|
|
|
2014-01-18 17:43:55 +00:00
|
|
|
|
my $new_volume;
|
|
|
|
|
if (@_ == 1) {
|
|
|
|
|
# we have a Model::Volume
|
|
|
|
|
my ($volume) = @_;
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
|
$new_volume = $self->_add_volume_clone($volume);
|
2014-01-18 17:43:55 +00:00
|
|
|
|
|
2014-07-12 09:20:57 +00:00
|
|
|
|
if ($volume->material_id ne '') {
|
2014-03-22 17:02:58 +00:00
|
|
|
|
# merge material attributes and config (should we rename materials in case of duplicates?)
|
2014-04-29 23:04:49 +00:00
|
|
|
|
if (my $material = $volume->object->model->get_material($volume->material_id)) {
|
2014-01-18 17:43:55 +00:00
|
|
|
|
my %attributes = %{ $material->attributes };
|
2014-04-29 23:04:49 +00:00
|
|
|
|
if ($self->model->has_material($volume->material_id)) {
|
|
|
|
|
%attributes = (%attributes, %{ $self->model->get_material($volume->material_id)->attributes })
|
2014-01-18 17:43:55 +00:00
|
|
|
|
}
|
2014-03-22 17:02:58 +00:00
|
|
|
|
my $new_material = $self->model->set_material($volume->material_id, {%attributes});
|
|
|
|
|
$new_material->config->apply($material->config);
|
2014-01-18 17:43:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
my %args = @_;
|
2014-05-09 12:24:35 +00:00
|
|
|
|
|
|
|
|
|
$new_volume = $self->_add_volume($args{mesh});
|
|
|
|
|
|
2014-07-12 09:20:57 +00:00
|
|
|
|
$new_volume->set_name($args{name})
|
|
|
|
|
if defined $args{name};
|
2014-05-09 12:24:35 +00:00
|
|
|
|
$new_volume->set_material_id($args{material_id})
|
|
|
|
|
if defined $args{material_id};
|
|
|
|
|
$new_volume->set_modifier($args{modifier})
|
|
|
|
|
if defined $args{modifier};
|
2014-07-12 09:20:57 +00:00
|
|
|
|
$new_volume->config->apply($args{config})
|
|
|
|
|
if defined $args{config};
|
2014-01-18 17:43:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-12 09:20:57 +00:00
|
|
|
|
if ($new_volume->material_id ne '' && !defined $self->model->get_material($new_volume->material_id)) {
|
2014-04-19 09:43:41 +00:00
|
|
|
|
# TODO: this should be a trigger on Volume::material_id
|
|
|
|
|
$self->model->set_material($new_volume->material_id);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 12:24:35 +00:00
|
|
|
|
$self->invalidate_bounding_box;
|
2014-01-18 17:43:55 +00:00
|
|
|
|
|
|
|
|
|
return $new_volume;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub add_instance {
|
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my %params = @_;
|
2014-05-09 12:24:35 +00:00
|
|
|
|
|
|
|
|
|
if (@_ == 1) {
|
|
|
|
|
# we have a Model::Instance
|
|
|
|
|
my ($instance) = @_;
|
|
|
|
|
return $self->_add_instance_clone($instance);
|
|
|
|
|
} else {
|
|
|
|
|
my (%args) = @_;
|
|
|
|
|
|
|
|
|
|
my $new_instance = $self->_add_instance;
|
|
|
|
|
|
|
|
|
|
$new_instance->set_rotation($args{rotation})
|
|
|
|
|
if defined $args{rotation};
|
|
|
|
|
$new_instance->set_scaling_factor($args{scaling_factor})
|
|
|
|
|
if defined $args{scaling_factor};
|
|
|
|
|
$new_instance->set_offset($args{offset})
|
|
|
|
|
if defined $args{offset};
|
|
|
|
|
|
|
|
|
|
return $new_instance;
|
|
|
|
|
}
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-14 18:52:21 +00:00
|
|
|
|
sub rotate {
|
|
|
|
|
my ($self, $angle, $axis) = @_;
|
2014-04-25 15:50:03 +00:00
|
|
|
|
|
|
|
|
|
# we accept angle in radians but mesh currently uses degrees
|
|
|
|
|
$angle = rad2deg($angle);
|
|
|
|
|
|
2014-06-14 18:52:21 +00:00
|
|
|
|
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};
|
|
|
|
|
}
|
2014-04-29 23:04:49 +00:00
|
|
|
|
$self->invalidate_bounding_box;
|
2014-04-25 15:50:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-14 19:14:33 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
|
sub mesh_stats {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
# TODO: sum values from all volumes
|
|
|
|
|
return $self->volumes->[0]->mesh->stats;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
sub print_info {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
printf "Info about %s:\n", basename($self->input_file);
|
2014-01-14 20:46:39 +00:00
|
|
|
|
printf " size: x=%.3f y=%.3f z=%.3f\n", @{$self->raw_mesh->bounding_box->size};
|
2013-07-31 13:10:11 +00:00
|
|
|
|
if (my $stats = $self->mesh_stats) {
|
|
|
|
|
printf " number of facets: %d\n", $stats->{number_of_facets};
|
|
|
|
|
printf " number of shells: %d\n", $stats->{number_of_parts};
|
|
|
|
|
printf " volume: %.3f\n", $stats->{volume};
|
|
|
|
|
if ($self->needed_repair) {
|
|
|
|
|
printf " needed repair: yes\n";
|
|
|
|
|
printf " degenerate facets: %d\n", $stats->{degenerate_facets};
|
|
|
|
|
printf " edges fixed: %d\n", $stats->{edges_fixed};
|
|
|
|
|
printf " facets removed: %d\n", $stats->{facets_removed};
|
|
|
|
|
printf " facets added: %d\n", $stats->{facets_added};
|
|
|
|
|
printf " facets reversed: %d\n", $stats->{facets_reversed};
|
|
|
|
|
printf " backwards edges: %d\n", $stats->{backwards_edges};
|
|
|
|
|
} else {
|
|
|
|
|
printf " needed repair: no\n";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-01-07 14:40:38 +00:00
|
|
|
|
printf " number of facets: %d\n", scalar(map @{$_->facets}, grep !$_->modifier, @{$self->volumes});
|
2013-07-31 13:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
1;
|