2011-10-03 09:55:32 +00:00
|
|
|
package Slic3r::Skein;
|
|
|
|
use Moo;
|
|
|
|
|
2012-01-28 15:27:52 +00:00
|
|
|
use Config;
|
2011-12-26 16:20:26 +00:00
|
|
|
use File::Basename qw(basename fileparse);
|
2011-12-20 14:29:15 +00:00
|
|
|
use Slic3r::Geometry qw(PI);
|
2011-10-03 09:55:32 +00:00
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
2011-10-04 20:27:45 +00:00
|
|
|
use XXX;
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2011-12-26 16:20:26 +00:00
|
|
|
# full path (relative or absolute) to the input file
|
2011-10-03 09:55:32 +00:00
|
|
|
has 'input_file' => (is => 'ro', required => 1);
|
2011-12-26 16:20:26 +00:00
|
|
|
|
|
|
|
# full path (relative or absolute) to the output file; it may contain
|
|
|
|
# formatting variables like [layer_height] etc.
|
|
|
|
has 'output_file' => (is => 'rw', required => 0);
|
|
|
|
|
2011-11-26 15:19:30 +00:00
|
|
|
has 'status_cb' => (is => 'rw', required => 0, default => sub { sub {} });
|
|
|
|
has 'processing_time' => (is => 'rw', required => 0);
|
2011-10-03 09:55:32 +00:00
|
|
|
|
|
|
|
sub go {
|
|
|
|
my $self = shift;
|
|
|
|
my $t0 = [gettimeofday];
|
2011-10-07 17:07:57 +00:00
|
|
|
|
|
|
|
# skein the STL into layers
|
2011-10-09 17:47:21 +00:00
|
|
|
# each layer has surfaces with holes
|
2012-02-21 16:26:54 +00:00
|
|
|
$self->status_cb->(5, "Processing input file " . $self->input_file);
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(10, "Processing triangulated mesh");
|
2011-11-27 10:40:03 +00:00
|
|
|
my $print;
|
2012-02-20 15:43:45 +00:00
|
|
|
if ($self->input_file =~ /\.stl$/i) {
|
2012-02-25 16:35:25 +00:00
|
|
|
my $mesh = Slic3r::Format::STL->read_file($self->input_file);
|
2011-11-27 10:40:03 +00:00
|
|
|
$mesh->check_manifoldness;
|
|
|
|
$print = Slic3r::Print->new_from_mesh($mesh);
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ( $self->input_file =~ /\.amf(\.xml)?$/i) {
|
2012-02-25 16:35:25 +00:00
|
|
|
my ($materials, $meshes_by_material) = Slic3r::Format::AMF->read_file($self->input_file);
|
2012-02-20 15:43:45 +00:00
|
|
|
$_->check_manifoldness for values %$meshes_by_material;
|
|
|
|
$print = Slic3r::Print->new_from_mesh($meshes_by_material->{_} || +(values %$meshes_by_material)[0]);
|
|
|
|
} else {
|
|
|
|
die "Input file must have .stl or .amf(.xml) extension\n";
|
2011-11-27 10:40:03 +00:00
|
|
|
}
|
2011-10-07 17:07:57 +00:00
|
|
|
|
2011-11-23 11:29:27 +00:00
|
|
|
# make perimeters
|
|
|
|
# this will add a set of extrusion loops to each layer
|
|
|
|
# as well as generate infill boundaries
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(20, "Generating perimeters");
|
2011-11-23 11:29:27 +00:00
|
|
|
{
|
|
|
|
my $perimeter_maker = Slic3r::Perimeter->new;
|
|
|
|
$perimeter_maker->make_perimeter($_) for @{$print->layers};
|
|
|
|
}
|
|
|
|
|
2011-11-27 09:12:44 +00:00
|
|
|
# this will clip $layer->surfaces to the infill boundaries
|
|
|
|
# and split them in top/bottom/internal surfaces;
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(30, "Detecting solid surfaces");
|
2011-10-09 17:47:21 +00:00
|
|
|
$print->detect_surfaces_type;
|
|
|
|
|
2011-12-03 17:31:31 +00:00
|
|
|
# decide what surfaces are to be filled
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(35, "Preparing infill surfaces");
|
2011-11-30 19:32:28 +00:00
|
|
|
$_->prepare_fill_surfaces for @{$print->layers};
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
# this will remove unprintable surfaces
|
|
|
|
# (those that are too tight for extrusion)
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(40, "Cleaning up");
|
2011-11-16 08:52:09 +00:00
|
|
|
$_->remove_small_surfaces for @{$print->layers};
|
2011-10-07 17:07:57 +00:00
|
|
|
|
2011-11-23 11:29:27 +00:00
|
|
|
# this will detect bridges and reverse bridges
|
|
|
|
# and rearrange top/bottom/internal surfaces
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(45, "Detect bridges");
|
2011-11-16 08:52:09 +00:00
|
|
|
$_->process_bridges for @{$print->layers};
|
2011-10-07 17:07:57 +00:00
|
|
|
|
|
|
|
# this will remove unprintable perimeter loops
|
|
|
|
# (those that are too tight for extrusion)
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(50, "Cleaning up the perimeters");
|
2011-11-16 08:52:09 +00:00
|
|
|
$_->remove_small_perimeters for @{$print->layers};
|
2011-10-07 17:07:57 +00:00
|
|
|
|
|
|
|
# detect which fill surfaces are near external layers
|
|
|
|
# they will be split in internal and internal-solid surfaces
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(60, "Generating horizontal shells");
|
2011-10-04 20:27:45 +00:00
|
|
|
$print->discover_horizontal_shells;
|
|
|
|
|
2012-01-28 20:52:31 +00:00
|
|
|
# free memory
|
|
|
|
@{$_->surfaces} = () for @{$print->layers};
|
|
|
|
|
2011-10-18 13:57:53 +00:00
|
|
|
# combine fill surfaces to honor the "infill every N layers" option
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(70, "Combining infill");
|
2011-10-18 13:57:53 +00:00
|
|
|
$print->infill_every_layers;
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
# this will generate extrusion paths for each layer
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(80, "Infilling layers");
|
2011-11-23 11:29:27 +00:00
|
|
|
{
|
|
|
|
my $fill_maker = Slic3r::Fill->new('print' => $print);
|
2012-01-28 15:27:52 +00:00
|
|
|
|
|
|
|
if ($Config{useithreads} && $Slic3r::threads > 1 && eval "use threads; use Thread::Queue; 1") {
|
|
|
|
my $q = Thread::Queue->new;
|
|
|
|
$q->enqueue(0..($print->layer_count-1), (map undef, 1..$Slic3r::threads));
|
|
|
|
|
|
|
|
my $thread_cb = sub {
|
|
|
|
$Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
|
|
|
|
my $fills = {};
|
|
|
|
while (defined (my $layer_id = $q->dequeue)) {
|
|
|
|
$fills->{$layer_id} = [ $fill_maker->make_fill($print->layers->[$layer_id]) ];
|
|
|
|
}
|
|
|
|
return $fills;
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach my $th (map threads->create($thread_cb), 1..$Slic3r::threads) {
|
|
|
|
my $fills = $th->join;
|
|
|
|
foreach my $layer_id (keys %$fills) {
|
|
|
|
@{$print->layers->[$layer_id]->fills} = @{$fills->{$layer_id}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach my $layer (@{$print->layers}) {
|
|
|
|
@{$layer->fills} = $fill_maker->make_fill($layer);
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 11:29:27 +00:00
|
|
|
}
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2012-02-19 11:03:36 +00:00
|
|
|
# generate support material
|
|
|
|
if ($Slic3r::support_material) {
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(85, "Generating support material");
|
2012-02-19 11:03:36 +00:00
|
|
|
$print->generate_support_material;
|
|
|
|
}
|
|
|
|
|
2012-02-25 20:39:01 +00:00
|
|
|
# free memory (note that support material needs fill_surfaces)
|
|
|
|
@{$_->fill_surfaces} = () for @{$print->layers};
|
|
|
|
|
2012-02-19 11:03:36 +00:00
|
|
|
# make skirt
|
2012-02-20 11:50:05 +00:00
|
|
|
$self->status_cb->(88, "Generating skirt");
|
2012-02-19 11:03:36 +00:00
|
|
|
$print->extrude_skirt;
|
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
# output everything to a GCODE file
|
2012-02-20 11:50:05 +00:00
|
|
|
my $output_file = $self->expanded_output_filepath;
|
2012-02-21 16:26:54 +00:00
|
|
|
$self->status_cb->(90, "Exporting GCODE to $output_file");
|
2012-02-20 11:50:05 +00:00
|
|
|
$print->export_gcode($output_file);
|
|
|
|
|
|
|
|
# run post-processing scripts
|
|
|
|
if (@$Slic3r::post_process) {
|
|
|
|
$self->status_cb->(95, "Running post-processing scripts");
|
|
|
|
for (@$Slic3r::post_process) {
|
|
|
|
Slic3r::debugf " '%s' '%s'\n", $_, $output_file;
|
|
|
|
system($_, $output_file);
|
|
|
|
}
|
|
|
|
}
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2011-10-07 17:07:57 +00:00
|
|
|
# output some statistics
|
2011-11-26 15:19:30 +00:00
|
|
|
$self->processing_time(tv_interval($t0));
|
2011-10-03 09:55:32 +00:00
|
|
|
printf "Done. Process took %d minutes and %.3f seconds\n",
|
2011-11-26 15:19:30 +00:00
|
|
|
int($self->processing_time/60),
|
|
|
|
$self->processing_time - int($self->processing_time/60)*60;
|
2011-10-07 17:07:57 +00:00
|
|
|
|
|
|
|
# TODO: more statistics!
|
2011-12-20 14:29:15 +00:00
|
|
|
printf "Filament required: %.1fmm (%.1fcm3)\n",
|
|
|
|
$print->total_extrusion_length, $print->total_extrusion_volume;
|
2011-10-03 09:55:32 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 16:20:26 +00:00
|
|
|
# this method will return the value of $self->output_file after expanding its
|
|
|
|
# format variables with their values
|
|
|
|
sub expanded_output_filepath {
|
2011-12-26 09:20:45 +00:00
|
|
|
my $self = shift;
|
2011-12-26 16:20:26 +00:00
|
|
|
|
|
|
|
my $path = $self->output_file;
|
|
|
|
|
|
|
|
# if no explicit output file was defined, we take the input
|
|
|
|
# file directory and append the specified filename format
|
|
|
|
$path ||= (fileparse($self->input_file))[1] . $Slic3r::output_filename_format;
|
|
|
|
|
|
|
|
my $input_basename = basename($self->input_file);
|
|
|
|
$path =~ s/\[input_filename\]/$input_basename/g;
|
2012-02-19 14:14:54 +00:00
|
|
|
$input_basename =~ s/\.(?:stl|amf(?:\.xml)?)$//i;
|
2011-12-26 16:20:26 +00:00
|
|
|
$path =~ s/\[input_filename_base\]/$input_basename/g;
|
|
|
|
|
|
|
|
# build a regexp to match the available options
|
|
|
|
my $options = join '|',
|
|
|
|
grep !$Slic3r::Config::Options->{$_}{multiline},
|
|
|
|
keys %$Slic3r::Config::Options;
|
|
|
|
|
|
|
|
# use that regexp to search and replace option names with option values
|
|
|
|
$path =~ s/\[($options)\]/Slic3r::Config->serialize($1)/eg;
|
|
|
|
return $path;
|
2011-12-26 09:20:45 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 09:55:32 +00:00
|
|
|
1;
|