diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 9719f1d32..198a52825 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -61,8 +61,6 @@ use Slic3r::ExPolygon; use Slic3r::ExtrusionLoop; use Slic3r::ExtrusionPath; use Slic3r::Flow; -use Slic3r::Format::AMF; -use Slic3r::Format::STL; use Slic3r::GCode::ArcFitting; use Slic3r::GCode::MotionPlanner; use Slic3r::GCode::PressureRegulator; diff --git a/lib/Slic3r/Format/AMF.pm b/lib/Slic3r/Format/AMF.pm deleted file mode 100644 index 603a4014a..000000000 --- a/lib/Slic3r/Format/AMF.pm +++ /dev/null @@ -1,116 +0,0 @@ -package Slic3r::Format::AMF; -use Moo; - -use Slic3r::Geometry qw(X Y Z); - -sub write_file { - my $self = shift; - my ($file, $model, %params) = @_; - - my %vertices_offset = (); - - Slic3r::open(\my $fh, '>', $file); - binmode $fh, ':utf8'; - printf $fh qq{\n}; - printf $fh qq{\n}; - printf $fh qq{ Slic3r %s\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{ \n}, $material_id; - for (keys %{$material->attributes}) { - printf $fh qq{ %s\n}, $_, $material->attributes->{$_}; - } - my $config = $material->config; - foreach my $opt_key (@{$config->get_keys}) { - printf $fh qq{ %s\n}, $opt_key, $config->serialize($opt_key); - } - printf $fh qq{ \n}; - } - my $instances = ''; - for my $object_id (0 .. $#{ $model->objects }) { - my $object = $model->objects->[$object_id]; - printf $fh qq{ \n}, $object_id; - - my $config = $object->config; - foreach my $opt_key (@{$config->get_keys}) { - printf $fh qq{ %s\n}, $opt_key, $config->serialize($opt_key); - } - if ($object->name) { - printf $fh qq{ %s\n}, $object->name; - } - my $layer_height_profile = $object->layer_height_profile(); - my $layer_height_profile_pts = int(@{$layer_height_profile}); - if ($layer_height_profile_pts >= 4 && $layer_height_profile_pts % 2 == 0) { - # Store the layer height profile as a single semicolon separated list. - print $fh ' ', join(';', @{$layer_height_profile}), "\n"; - } - #FIXME Store the layer height ranges (ModelObject::layer_height_ranges) - - printf $fh qq{ \n}; - printf $fh qq{ \n}; - my @vertices_offset = (); - { - my $vertices_offset = 0; - foreach my $volume (@{ $object->volumes }) { - push @vertices_offset, $vertices_offset; - my $vertices = $volume->mesh->vertices; - foreach my $vertex (@$vertices) { - printf $fh qq{ \n}; - printf $fh qq{ \n}; - printf $fh qq{ %s\n}, $vertex->[X]; - printf $fh qq{ %s\n}, $vertex->[Y]; - printf $fh qq{ %s\n}, $vertex->[Z]; - printf $fh qq{ \n}; - printf $fh qq{ \n}; - } - $vertices_offset += scalar(@$vertices); - } - } - printf $fh qq{ \n}; - foreach my $volume (@{ $object->volumes }) { - my $vertices_offset = shift @vertices_offset; - printf $fh qq{ \n}, - ($volume->material_id eq '') ? '' : (sprintf ' materialid="%s"', $volume->material_id); - - my $config = $volume->config; - foreach my $opt_key (@{$config->get_keys}) { - printf $fh qq{ %s\n}, $opt_key, $config->serialize($opt_key); - } - if ($volume->name) { - printf $fh qq{ %s\n}, $volume->name; - } - if ($volume->modifier) { - printf $fh qq{ 1\n}; - } - - foreach my $facet (@{$volume->mesh->facets}) { - printf $fh qq{ \n}; - printf $fh qq{ %d\n}, $_, $facet->[$_-1] + $vertices_offset, $_ for 1..3; - printf $fh qq{ \n}; - } - printf $fh qq{ \n}; - } - printf $fh qq{ \n}; - printf $fh qq{ \n}; - if ($object->instances) { - foreach my $instance (@{$object->instances}) { - $instances .= sprintf qq{ \n}, $object_id; - $instances .= sprintf qq{ %s\n}, $instance->offset->[X]; - $instances .= sprintf qq{ %s\n}, $instance->offset->[Y]; - $instances .= sprintf qq{ %s\n}, $instance->rotation; - $instances .= sprintf qq{ \n}; - } - } - } - if ($instances) { - printf $fh qq{ \n}; - printf $fh $instances; - printf $fh qq{ \n}; - } - printf $fh qq{\n}; - close $fh; -} - -1; diff --git a/lib/Slic3r/Format/STL.pm b/lib/Slic3r/Format/STL.pm deleted file mode 100644 index a6a1836e4..000000000 --- a/lib/Slic3r/Format/STL.pm +++ /dev/null @@ -1,19 +0,0 @@ -package Slic3r::Format::STL; -use Moo; - -use File::Basename qw(basename); - -sub write_file { - my $self = shift; - my ($file, $mesh, %params) = @_; - - $mesh = $mesh->mesh if $mesh->isa('Slic3r::Model'); - - my $path = Slic3r::encode_path($file); - - $params{binary} - ? $mesh->write_binary($path) - : $mesh->write_ascii($path); -} - -1; diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index a59fa7e31..17fa1aafe 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -1499,7 +1499,7 @@ sub export_stl { return if !@{$self->{objects}}; my $output_file = $self->_get_export_file('STL') or return; - Slic3r::Format::STL->write_file($output_file, $self->{model}, binary => 1); + $self->{model}->store_stl(Slic3r::encode_path($output_file), 1); $self->statusbar->SetStatusText("STL file exported to $output_file"); } @@ -1546,7 +1546,7 @@ sub export_object_stl { my $model_object = $self->{model}->objects->[$obj_idx]; my $output_file = $self->_get_export_file('STL') or return; - Slic3r::Format::STL->write_file($output_file, $model_object->mesh, binary => 1); + $model_object->mesh->write_binary(Slic3r::encode_path($output_file)); $self->statusbar->SetStatusText("STL file exported to $output_file"); } @@ -1556,7 +1556,7 @@ sub export_amf { return if !@{$self->{objects}}; my $output_file = $self->_get_export_file('AMF') or return; - Slic3r::Format::AMF->write_file($output_file, $self->{model}); + $self->{model}->store_amf(Slic3r::encode_path($output_file)); $self->statusbar->SetStatusText("AMF file exported to $output_file"); } diff --git a/slic3r.pl b/slic3r.pl index 1c9900147..0cd1ab80d 100755 --- a/slic3r.pl +++ b/slic3r.pl @@ -172,7 +172,7 @@ if (@ARGV) { # slicing from command line foreach my $new_mesh (@{$mesh->split}) { my $output_file = sprintf '%s_%02d.stl', $file, ++$part_count; printf "Writing to %s\n", basename($output_file); - Slic3r::Format::STL->write_file($output_file, $new_mesh, binary => 1); + $new_mesh->write_binary(Slic3r::encode_path($output_file)); } } exit; diff --git a/utils/amf-to-stl.pl b/utils/amf-to-stl.pl index 8b26cedb3..64910dc31 100755 --- a/utils/amf-to-stl.pl +++ b/utils/amf-to-stl.pl @@ -30,7 +30,7 @@ my %opt = (); $output_file =~ s/\.amf(?:\.xml)?$/\.stl/i; printf "Writing to %s\n", basename($output_file); - Slic3r::Format::STL->write_file($output_file, $model, binary => !$opt{ascii}); + $model->store_stl(Slic3r::encode_path($output_file), binary => !$opt{ascii}); } diff --git a/utils/dump-stl.pl b/utils/dump-stl.pl index f96e3f5bb..12746feab 100644 --- a/utils/dump-stl.pl +++ b/utils/dump-stl.pl @@ -27,7 +27,7 @@ if (-e $ARGV[0]) { exit 0; } elsif ((my $model = Slic3r::Test::model($ARGV[0]))) { $ARGV[1] or die "Missing writeable destination as second argument\n"; - Slic3r::Format::STL->write_file($ARGV[1], $model); + $model->store_stl(Slic3r::encode_path($ARGV[1]), 1); printf "Model $ARGV[0] written to $ARGV[1]\n"; exit 0; } else { diff --git a/utils/split_stl.pl b/utils/split_stl.pl index 23fc91b26..1c46e9729 100755 --- a/utils/split_stl.pl +++ b/utils/split_stl.pl @@ -43,7 +43,7 @@ my %opt = (); my $output_file = sprintf '%s_%02d.stl', $basename, ++$part_count; printf "Writing to %s\n", basename($output_file); - Slic3r::Format::STL->write_file($output_file, $new_model, binary => !$opt{ascii}); + $new_model->store_stl(Slic3r::encode_path($output_file), !$opt{ascii}); } } diff --git a/utils/stl-to-amf.pl b/utils/stl-to-amf.pl index 525dcb52a..072fe96fc 100755 --- a/utils/stl-to-amf.pl +++ b/utils/stl-to-amf.pl @@ -53,7 +53,7 @@ my %opt = (); } printf "Writing to %s\n", basename($output_file); - Slic3r::Format::AMF->write_file($output_file, $new_model); + $new_model->store_amf(Slic3r::encode_path($output_file)); } diff --git a/xs/src/libslic3r/Format/STL.hpp b/xs/src/libslic3r/Format/STL.hpp index aecb45183..2fd32324c 100644 --- a/xs/src/libslic3r/Format/STL.hpp +++ b/xs/src/libslic3r/Format/STL.hpp @@ -10,7 +10,7 @@ class ModelObject; extern bool load_stl(const char *path, Model *model, const char *object_name = nullptr); extern bool store_stl(const char *path, TriangleMesh *mesh, bool binary); -extern bool store_stl(const char *path, ModelObject *model_object); +extern bool store_stl(const char *path, ModelObject *model_object, bool binary); }; // namespace Slic3r diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index d84c1cb29..1b3dd0209 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -76,6 +76,11 @@ void duplicate_objects(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL); void duplicate_objects_grid(unsigned int x, unsigned int y, double dist); + bool store_stl(char *path, bool binary) + %code%{ TriangleMesh mesh = THIS->mesh(); RETVAL = Slic3r::store_stl(path, &mesh, binary); %}; + bool store_amf(char *path) + %code%{ RETVAL = Slic3r::store_amf(path, THIS); %}; + %{ Model*