2011-09-01 19:06:28 +00:00
|
|
|
package Slic3r::Print;
|
2011-09-06 09:50:43 +00:00
|
|
|
use Moo;
|
2011-09-01 19:06:28 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
use File::Basename qw(basename fileparse);
|
2012-08-07 21:37:16 +00:00
|
|
|
use File::Spec;
|
2011-11-13 17:41:12 +00:00
|
|
|
use Math::ConvexHull 1.0.4 qw(convex_hull);
|
2012-05-19 13:40:11 +00:00
|
|
|
use Slic3r::ExtrusionPath ':roles';
|
2012-04-30 12:56:01 +00:00
|
|
|
use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 PI scale unscale move_points);
|
2012-08-06 18:26:08 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(diff_ex union_ex intersection_ex offset JT_ROUND JT_SQUARE);
|
2012-04-30 12:56:01 +00:00
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
2011-09-25 20:11:56 +00:00
|
|
|
|
2012-07-29 15:02:46 +00:00
|
|
|
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1);
|
2012-04-30 12:56:01 +00:00
|
|
|
has 'objects' => (is => 'rw', default => sub {[]});
|
|
|
|
has 'copies' => (is => 'rw', default => sub {[]}); # obj_idx => [copies...]
|
2012-04-29 10:51:20 +00:00
|
|
|
has 'total_extrusion_length' => (is => 'rw');
|
2012-04-30 12:56:01 +00:00
|
|
|
has 'processing_time' => (is => 'rw', required => 0);
|
2011-09-03 18:47:38 +00:00
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
# ordered collection of extrusion paths to build skirt loops
|
|
|
|
has 'skirt' => (
|
2011-09-01 19:06:28 +00:00
|
|
|
is => 'rw',
|
2012-04-29 10:51:20 +00:00
|
|
|
#isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
|
2011-09-01 19:06:28 +00:00
|
|
|
default => sub { [] },
|
|
|
|
);
|
|
|
|
|
2012-06-23 19:31:29 +00:00
|
|
|
# ordered collection of extrusion paths to build a brim
|
|
|
|
has 'brim' => (
|
|
|
|
is => 'rw',
|
|
|
|
#isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
|
|
|
|
default => sub { [] },
|
|
|
|
);
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
sub BUILD {
|
|
|
|
my $self = shift;
|
|
|
|
|
2012-07-29 15:02:46 +00:00
|
|
|
# call this manually because the 'default' coderef doesn't trigger the trigger
|
|
|
|
$self->_trigger_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _trigger_config {
|
|
|
|
my $self = shift;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
# store config in a handy place
|
|
|
|
$Slic3r::Config = $self->config;
|
|
|
|
|
|
|
|
# initialize extruder(s)
|
|
|
|
$Slic3r::extruders = [];
|
|
|
|
for my $t (0, map $_-1, map $self->config->get($_), qw(perimeter_extruder infill_extruder support_material_extruder)) {
|
|
|
|
$Slic3r::extruders->[$t] ||= Slic3r::Extruder->new(
|
|
|
|
map { $_ => $self->config->get($_)->[$t] // $self->config->get($_)->[0] } #/
|
2012-08-07 18:50:54 +00:00
|
|
|
@{&Slic3r::Extruder::OPTIONS}
|
2012-07-27 19:13:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# calculate flow
|
|
|
|
$Slic3r::flow = $Slic3r::extruders->[0]->make_flow(width => $self->config->extrusion_width);
|
|
|
|
if ($self->config->first_layer_extrusion_width) {
|
|
|
|
$Slic3r::first_layer_flow = $Slic3r::extruders->[0]->make_flow(
|
|
|
|
layer_height => $self->config->get_value('first_layer_height'),
|
|
|
|
width => $self->config->first_layer_extrusion_width,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (qw(perimeter infill support_material)) {
|
|
|
|
no strict 'refs';
|
|
|
|
${"Slic3r::${_}_flow"} = $Slic3r::extruders->[ $self->config->get("${_}_extruder")-1 ]
|
|
|
|
->make_flow(width => $self->config->get("${_}_extrusion_width") || $self->config->extrusion_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slic3r::debugf "Default flow width = %s (spacing = %s)\n",
|
|
|
|
$Slic3r::flow->width, $Slic3r::flow->spacing;
|
|
|
|
|
|
|
|
# G-code flavors
|
|
|
|
$self->config->set('extrusion_axis', 'A') if $self->config->gcode_flavor eq 'mach3';
|
|
|
|
$self->config->set('extrusion_axis', '') if $self->config->gcode_flavor eq 'no-extrusion';
|
|
|
|
|
|
|
|
# legacy with existing config files
|
|
|
|
$self->config->set_ifndef('small_perimeter_speed', $self->config->perimeter_speed);
|
|
|
|
$self->config->set_ifndef('bridge_speed', $self->config->infill_speed);
|
|
|
|
$self->config->set_ifndef('solid_infill_speed', $self->config->infill_speed);
|
|
|
|
$self->config->set_ifndef('top_solid_infill_speed', $self->config->solid_infill_speed);
|
|
|
|
}
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
sub add_objects_from_file {
|
2012-04-30 12:56:01 +00:00
|
|
|
my $self = shift;
|
|
|
|
my ($input_file) = @_;
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
my @print_objects = $self->add_model($model);
|
|
|
|
$_->input_file($input_file) for @print_objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_model {
|
|
|
|
my $self = shift;
|
|
|
|
my ($model) = @_;
|
|
|
|
|
|
|
|
my @print_objects = ();
|
|
|
|
foreach my $object (@{ $model->objects }) {
|
|
|
|
my $mesh = $object->volumes->[0]->mesh;
|
2012-05-20 09:40:37 +00:00
|
|
|
$mesh->check_manifoldness;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
|
|
if ($object->instances) {
|
|
|
|
# we ignore the per-instance rotation currently and only
|
|
|
|
# consider the first one
|
|
|
|
$mesh->rotate($object->instances->[0]->rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @print_objects, $self->add_object_from_mesh($mesh);
|
|
|
|
|
|
|
|
if ($object->instances) {
|
|
|
|
# replace the default [0,0] instance with the custom ones
|
|
|
|
@{$self->copies->[-1]} = map [ scale $_->offset->[X], scale $_->offset->[X] ], @{$object->instances};
|
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
|
|
return @print_objects;
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
sub add_object_from_mesh {
|
|
|
|
my $self = shift;
|
2011-11-27 10:40:03 +00:00
|
|
|
my ($mesh) = @_;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
$mesh->rotate($Slic3r::Config->rotate);
|
|
|
|
$mesh->scale($Slic3r::Config->scale / &Slic3r::SCALING_FACTOR);
|
2012-02-20 10:47:21 +00:00
|
|
|
$mesh->align_to_origin;
|
2011-11-27 10:40:03 +00:00
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
# initialize print object
|
|
|
|
my $object = Slic3r::Print::Object->new(
|
2012-08-25 18:14:01 +00:00
|
|
|
mesh => $mesh,
|
|
|
|
size => [ $mesh->size ],
|
2011-11-27 10:40:03 +00:00
|
|
|
);
|
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
push @{$self->objects}, $object;
|
2012-04-30 12:56:01 +00:00
|
|
|
push @{$self->copies}, [[0, 0]];
|
2012-04-29 10:51:20 +00:00
|
|
|
return $object;
|
2011-10-02 07:57:37 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 09:47:52 +00:00
|
|
|
sub validate {
|
|
|
|
my $self = shift;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->complete_objects) {
|
2012-05-23 09:47:52 +00:00
|
|
|
# check horizontal clearance
|
|
|
|
{
|
|
|
|
my @a = ();
|
|
|
|
for my $obj_idx (0 .. $#{$self->objects}) {
|
|
|
|
my $clearance;
|
|
|
|
{
|
|
|
|
my @points = map [ @$_[X,Y] ], @{$self->objects->[$obj_idx]->mesh->vertices};
|
|
|
|
my $convex_hull = Slic3r::Polygon->new(convex_hull(\@points));
|
2012-07-27 19:13:03 +00:00
|
|
|
$clearance = +($convex_hull->offset(scale $Slic3r::Config->extruder_clearance_radius / 2, 1, JT_ROUND))[0];
|
2012-05-23 09:47:52 +00:00
|
|
|
}
|
|
|
|
for my $copy (@{$self->copies->[$obj_idx]}) {
|
|
|
|
my $copy_clearance = $clearance->clone;
|
|
|
|
$copy_clearance->translate(@$copy);
|
|
|
|
if (@{ intersection_ex(\@a, [$copy_clearance]) }) {
|
|
|
|
die "Some objects are too close; your extruder will collide with them.\n";
|
|
|
|
}
|
|
|
|
@a = map @$_, @{union_ex([ @a, $copy_clearance ])};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# check vertical clearance
|
|
|
|
{
|
|
|
|
my @obj_copies = $self->object_copies;
|
|
|
|
pop @obj_copies; # ignore the last copy: its height doesn't matter
|
2012-07-27 19:13:03 +00:00
|
|
|
if (grep { +($self->objects->[$_->[0]]->mesh->size)[Z] > scale $Slic3r::Config->extruder_clearance_height } @obj_copies) {
|
2012-05-23 09:47:52 +00:00
|
|
|
die "Some objects are too tall and cannot be printed without extruder collisions.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub object_copies {
|
|
|
|
my $self = shift;
|
|
|
|
my @oc = ();
|
|
|
|
for my $obj_idx (0 .. $#{$self->objects}) {
|
|
|
|
push @oc, map [ $obj_idx, $_ ], @{$self->copies->[$obj_idx]};
|
|
|
|
}
|
|
|
|
return @oc;
|
|
|
|
}
|
|
|
|
|
2012-05-02 13:20:05 +00:00
|
|
|
sub cleanup {
|
|
|
|
my $self = shift;
|
|
|
|
$_->cleanup for @{$self->objects};
|
|
|
|
@{$self->skirt} = ();
|
|
|
|
$self->total_extrusion_length(0);
|
|
|
|
$self->processing_time(0);
|
|
|
|
}
|
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
sub layer_count {
|
|
|
|
my $self = shift;
|
|
|
|
my $count = 0;
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
$count = @{$object->layers} if @{$object->layers} > $count;
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub duplicate {
|
2012-02-19 09:48:58 +00:00
|
|
|
my $self = shift;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->duplicate_grid->[X] > 1 || $Slic3r::Config->duplicate_grid->[Y] > 1) {
|
2012-04-29 10:51:20 +00:00
|
|
|
if (@{$self->objects} > 1) {
|
|
|
|
die "Grid duplication is not supported with multiple objects\n";
|
|
|
|
}
|
|
|
|
my $object = $self->objects->[0];
|
2012-03-06 03:55:21 +00:00
|
|
|
|
|
|
|
# generate offsets for copies
|
2012-07-27 19:13:03 +00:00
|
|
|
my $dist = scale $Slic3r::Config->duplicate_distance;
|
2012-04-30 12:56:01 +00:00
|
|
|
@{$self->copies->[0]} = ();
|
2012-07-27 19:13:03 +00:00
|
|
|
for my $x_copy (1..$Slic3r::Config->duplicate_grid->[X]) {
|
|
|
|
for my $y_copy (1..$Slic3r::Config->duplicate_grid->[Y]) {
|
2012-04-29 10:51:20 +00:00
|
|
|
push @{$self->copies->[0]}, [
|
2012-08-25 18:14:01 +00:00
|
|
|
($object->size->[X] + $dist) * ($x_copy-1),
|
|
|
|
($object->size->[Y] + $dist) * ($y_copy-1),
|
2012-03-06 03:55:21 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 19:13:03 +00:00
|
|
|
} elsif ($Slic3r::Config->duplicate > 1) {
|
2012-04-30 12:56:01 +00:00
|
|
|
foreach my $copies (@{$self->copies}) {
|
2012-07-27 19:13:03 +00:00
|
|
|
@$copies = map [0,0], 1..$Slic3r::Config->duplicate;
|
2012-04-11 18:44:09 +00:00
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
$self->arrange_objects;
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 18:44:09 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub arrange_objects {
|
|
|
|
my $self = shift;
|
2012-03-06 03:55:21 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
my $total_parts = scalar map @$_, @{$self->copies};
|
|
|
|
my $partx = my $party = 0;
|
|
|
|
foreach my $object (@{$self->objects}) {
|
2012-08-25 18:14:01 +00:00
|
|
|
$partx = $object->size->[X] if $object->size->[X] > $partx;
|
|
|
|
$party = $object->size->[Y] if $object->size->[Y] > $party;
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
2012-05-29 07:38:53 +00:00
|
|
|
|
|
|
|
# object distance is max(duplicate_distance, clearance_radius)
|
2012-07-27 19:13:03 +00:00
|
|
|
my $distance = $Slic3r::Config->complete_objects && $Slic3r::Config->extruder_clearance_radius > $Slic3r::Config->duplicate_distance
|
|
|
|
? $Slic3r::Config->extruder_clearance_radius
|
|
|
|
: $Slic3r::Config->duplicate_distance;
|
2012-05-29 07:38:53 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
my @positions = Slic3r::Geometry::arrange
|
2012-07-30 17:30:41 +00:00
|
|
|
($total_parts, $partx, $party, (map scale $_, @{$Slic3r::Config->bed_size}), scale $distance);
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
for my $obj_idx (0..$#{$self->objects}) {
|
|
|
|
@{$self->copies->[$obj_idx]} = splice @positions, 0, scalar @{$self->copies->[$obj_idx]};
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 03:55:21 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub bounding_box {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my @points = ();
|
|
|
|
foreach my $obj_idx (0 .. $#{$self->objects}) {
|
|
|
|
my $object = $self->objects->[$obj_idx];
|
|
|
|
foreach my $copy (@{$self->copies->[$obj_idx]}) {
|
|
|
|
push @points,
|
|
|
|
[ $copy->[X], $copy->[Y] ],
|
2012-08-25 18:14:01 +00:00
|
|
|
[ $copy->[X] + $object->size->[X], $copy->[Y] ],
|
|
|
|
[ $copy->[X] + $object->size->[X], $copy->[Y] + $object->size->[Y] ],
|
|
|
|
[ $copy->[X], $copy->[Y] + $object->size->[Y] ];
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Slic3r::Geometry::bounding_box(\@points);
|
|
|
|
}
|
2012-03-06 03:55:21 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub size {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my @bb = $self->bounding_box;
|
|
|
|
return [ $bb[X2] - $bb[X1], $bb[Y2] - $bb[Y1] ];
|
|
|
|
}
|
2012-03-06 03:55:21 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub export_gcode {
|
|
|
|
my $self = shift;
|
|
|
|
my %params = @_;
|
|
|
|
|
|
|
|
my $status_cb = $params{status_cb} || sub {};
|
|
|
|
my $t0 = [gettimeofday];
|
|
|
|
|
|
|
|
# skein the STL into layers
|
|
|
|
# each layer has surfaces with holes
|
|
|
|
$status_cb->(5, "Processing input file");
|
|
|
|
$status_cb->(10, "Processing triangulated mesh");
|
2012-05-28 17:58:27 +00:00
|
|
|
$_->slice(keep_meshes => $params{keep_meshes}) for @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# make perimeters
|
|
|
|
# this will add a set of extrusion loops to each layer
|
|
|
|
# as well as generate infill boundaries
|
|
|
|
$status_cb->(20, "Generating perimeters");
|
2012-05-05 14:36:10 +00:00
|
|
|
$_->make_perimeters for @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
|
2012-06-23 18:08:08 +00:00
|
|
|
# simplify slices, we only need the max resolution for perimeters
|
2012-07-27 19:13:03 +00:00
|
|
|
$_->simplify(scale &Slic3r::RESOLUTION)
|
2012-06-23 18:08:08 +00:00
|
|
|
for map @{$_->expolygon}, map @{$_->slices}, map @{$_->layers}, @{$self->objects};
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
# this will clip $layer->surfaces to the infill boundaries
|
|
|
|
# and split them in top/bottom/internal surfaces;
|
|
|
|
$status_cb->(30, "Detecting solid surfaces");
|
|
|
|
$_->detect_surfaces_type for @{$self->objects};
|
|
|
|
|
|
|
|
# decide what surfaces are to be filled
|
|
|
|
$status_cb->(35, "Preparing infill surfaces");
|
|
|
|
$_->prepare_fill_surfaces for map @{$_->layers}, @{$self->objects};
|
|
|
|
|
|
|
|
# this will detect bridges and reverse bridges
|
|
|
|
# and rearrange top/bottom/internal surfaces
|
|
|
|
$status_cb->(45, "Detect bridges");
|
|
|
|
$_->process_bridges for map @{$_->layers}, @{$self->objects};
|
|
|
|
|
|
|
|
# detect which fill surfaces are near external layers
|
|
|
|
# they will be split in internal and internal-solid surfaces
|
|
|
|
$status_cb->(60, "Generating horizontal shells");
|
|
|
|
$_->discover_horizontal_shells for @{$self->objects};
|
|
|
|
|
|
|
|
# free memory
|
2012-07-22 18:48:38 +00:00
|
|
|
$_->surfaces(undef) for map @{$_->layers}, @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# combine fill surfaces to honor the "infill every N layers" option
|
|
|
|
$status_cb->(70, "Combining infill");
|
2012-07-22 18:48:38 +00:00
|
|
|
$_->combine_infill for @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# this will generate extrusion paths for each layer
|
|
|
|
$status_cb->(80, "Infilling layers");
|
|
|
|
{
|
|
|
|
my $fill_maker = Slic3r::Fill->new('print' => $self);
|
2012-03-06 03:55:21 +00:00
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
my @items = (); # [obj_idx, layer_id]
|
|
|
|
foreach my $obj_idx (0 .. $#{$self->objects}) {
|
|
|
|
push @items, map [$obj_idx, $_], 0..$#{$self->objects->[$obj_idx]->layers};
|
|
|
|
}
|
|
|
|
Slic3r::parallelize(
|
|
|
|
items => [@items],
|
|
|
|
thread_cb => sub {
|
|
|
|
my $q = shift;
|
|
|
|
$Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
|
|
|
|
my $fills = {};
|
|
|
|
while (defined (my $obj_layer = $q->dequeue)) {
|
|
|
|
my ($obj_idx, $layer_id) = @$obj_layer;
|
|
|
|
$fills->{$obj_idx} ||= {};
|
|
|
|
$fills->{$obj_idx}{$layer_id} = [ $fill_maker->make_fill($self->objects->[$obj_idx]->layers->[$layer_id]) ];
|
|
|
|
}
|
|
|
|
return $fills;
|
|
|
|
},
|
|
|
|
collect_cb => sub {
|
|
|
|
my $fills = shift;
|
|
|
|
foreach my $obj_idx (keys %$fills) {
|
|
|
|
foreach my $layer_id (keys %{$fills->{$obj_idx}}) {
|
2012-07-22 18:48:38 +00:00
|
|
|
$self->objects->[$obj_idx]->layers->[$layer_id]->fills($fills->{$obj_idx}{$layer_id});
|
2012-03-06 03:55:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
},
|
|
|
|
no_threads_cb => sub {
|
|
|
|
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
|
2012-07-22 18:48:38 +00:00
|
|
|
$layer->fills([ $fill_maker->make_fill($layer) ]);
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
# generate support material
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->support_material) {
|
2012-04-30 12:56:01 +00:00
|
|
|
$status_cb->(85, "Generating support material");
|
2012-04-30 21:36:49 +00:00
|
|
|
$_->generate_support_material(print => $self) for @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# free memory (note that support material needs fill_surfaces)
|
2012-07-22 18:48:38 +00:00
|
|
|
$_->fill_surfaces(undef) for map @{$_->layers}, @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# make skirt
|
|
|
|
$status_cb->(88, "Generating skirt");
|
|
|
|
$self->make_skirt;
|
2012-06-23 19:31:29 +00:00
|
|
|
$self->make_brim;
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# output everything to a G-code file
|
|
|
|
my $output_file = $self->expanded_output_filepath($params{output_file});
|
|
|
|
$status_cb->(90, "Exporting G-code to $output_file");
|
|
|
|
$self->write_gcode($output_file);
|
|
|
|
|
|
|
|
# run post-processing scripts
|
2012-07-27 19:13:03 +00:00
|
|
|
if (@{$Slic3r::Config->post_process}) {
|
2012-04-30 12:56:01 +00:00
|
|
|
$status_cb->(95, "Running post-processing scripts");
|
2012-07-27 19:13:03 +00:00
|
|
|
$Slic3r::Config->setenv;
|
|
|
|
for (@{$Slic3r::Config->post_process}) {
|
2012-04-30 12:56:01 +00:00
|
|
|
Slic3r::debugf " '%s' '%s'\n", $_, $output_file;
|
|
|
|
system($_, $output_file);
|
2012-02-19 09:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# output some statistics
|
|
|
|
$self->processing_time(tv_interval($t0));
|
|
|
|
printf "Done. Process took %d minutes and %.3f seconds\n",
|
|
|
|
int($self->processing_time/60),
|
|
|
|
$self->processing_time - int($self->processing_time/60)*60;
|
|
|
|
|
|
|
|
# TODO: more statistics!
|
|
|
|
printf "Filament required: %.1fmm (%.1fcm3)\n",
|
|
|
|
$self->total_extrusion_length, $self->total_extrusion_volume;
|
2012-02-19 09:48:58 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub export_svg {
|
2011-09-18 17:28:12 +00:00
|
|
|
my $self = shift;
|
2012-04-30 12:56:01 +00:00
|
|
|
my %params = @_;
|
|
|
|
|
2012-05-30 10:06:01 +00:00
|
|
|
$_->slice(keep_meshes => $params{keep_meshes}) for @{$self->objects};
|
2012-04-30 12:56:01 +00:00
|
|
|
$self->arrange_objects;
|
|
|
|
|
|
|
|
my $output_file = $self->expanded_output_filepath($params{output_file});
|
|
|
|
$output_file =~ s/\.gcode$/.svg/i;
|
|
|
|
|
|
|
|
open my $fh, ">", $output_file or die "Failed to open $output_file for writing\n";
|
|
|
|
print "Exporting to $output_file...";
|
|
|
|
my $print_size = $self->size;
|
|
|
|
print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
|
|
|
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
|
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
|
|
|
|
<svg width="%s" height="%s" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:slic3r="http://slic3r.org/namespaces/slic3r">
|
|
|
|
<!--
|
|
|
|
Generated using Slic3r $Slic3r::VERSION
|
|
|
|
http://slic3r.org/
|
|
|
|
-->
|
|
|
|
EOF
|
|
|
|
|
|
|
|
my $print_polygon = sub {
|
|
|
|
my ($polygon, $type) = @_;
|
|
|
|
printf $fh qq{ <polygon slic3r:type="%s" points="%s" style="fill: %s" />\n},
|
|
|
|
$type, (join ' ', map { join ',', map unscale $_, @$_ } @$polygon),
|
2012-05-21 16:29:19 +00:00
|
|
|
($type eq 'contour' ? 'white' : 'black');
|
2012-04-30 12:56:01 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 12:47:48 +00:00
|
|
|
my @previous_layer_slices = ();
|
2012-04-30 12:56:01 +00:00
|
|
|
for my $layer_id (0..$self->layer_count-1) {
|
|
|
|
my @layers = map $_->layers->[$layer_id], @{$self->objects};
|
|
|
|
printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale +(grep defined $_, @layers)[0]->slice_z;
|
|
|
|
|
2012-06-11 12:47:48 +00:00
|
|
|
my @current_layer_slices = ();
|
2012-06-11 11:50:38 +00:00
|
|
|
for my $obj_idx (0 .. $#{$self->objects}) {
|
|
|
|
my $layer = $self->objects->[$obj_idx]->layers->[$layer_id] or next;
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
# sort slices so that the outermost ones come first
|
|
|
|
my @slices = sort { $a->expolygon->contour->encloses_point($b->expolygon->contour->[0]) ? 0 : 1 } @{$layer->slices};
|
|
|
|
foreach my $copy (@{$self->copies->[$obj_idx]}) {
|
|
|
|
foreach my $slice (@slices) {
|
|
|
|
my $expolygon = $slice->expolygon->clone;
|
|
|
|
$expolygon->translate(@$copy);
|
|
|
|
$print_polygon->($expolygon->contour, 'contour');
|
|
|
|
$print_polygon->($_, 'hole') for $expolygon->holes;
|
2012-06-11 12:47:48 +00:00
|
|
|
push @current_layer_slices, $expolygon;
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-11 12:47:48 +00:00
|
|
|
# generate support material
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->support_material && $layer_id > 0) {
|
2012-06-11 12:47:48 +00:00
|
|
|
my (@supported_slices, @unsupported_slices) = ();
|
|
|
|
foreach my $expolygon (@current_layer_slices) {
|
|
|
|
my $intersection = intersection_ex(
|
|
|
|
[ map @$_, @previous_layer_slices ],
|
|
|
|
$expolygon,
|
|
|
|
);
|
|
|
|
@$intersection
|
|
|
|
? push @supported_slices, $expolygon
|
|
|
|
: push @unsupported_slices, $expolygon;
|
|
|
|
}
|
|
|
|
my @supported_points = map @$_, @$_, @supported_slices;
|
|
|
|
foreach my $expolygon (@unsupported_slices) {
|
|
|
|
# look for the nearest point to this island among all
|
|
|
|
# supported points
|
|
|
|
my $support_point = nearest_point($expolygon->contour->[0], \@supported_points);
|
|
|
|
my $anchor_point = nearest_point($support_point, $expolygon->contour->[0]);
|
2012-06-11 18:42:39 +00:00
|
|
|
printf $fh qq{ <line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke-width: 2; stroke: white" />\n},
|
2012-06-11 12:47:48 +00:00
|
|
|
map @$_, $support_point, $anchor_point;
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 12:56:01 +00:00
|
|
|
print $fh qq{ </g>\n};
|
2012-06-11 12:47:48 +00:00
|
|
|
@previous_layer_slices = @current_layer_slices;
|
2012-04-30 12:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print $fh "</svg>\n";
|
|
|
|
close $fh;
|
|
|
|
print "Done.\n";
|
2011-09-25 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
sub make_skirt {
|
2011-11-13 17:41:12 +00:00
|
|
|
my $self = shift;
|
2012-07-27 19:13:03 +00:00
|
|
|
return unless $Slic3r::Config->skirts > 0;
|
2011-11-13 17:41:12 +00:00
|
|
|
|
|
|
|
# collect points from all layers contained in skirt height
|
2012-07-27 19:13:03 +00:00
|
|
|
my $skirt_height = $Slic3r::Config->skirt_height;
|
2011-12-22 10:57:26 +00:00
|
|
|
$skirt_height = $self->layer_count if $skirt_height > $self->layer_count;
|
2012-04-29 10:51:20 +00:00
|
|
|
my @points = ();
|
|
|
|
foreach my $obj_idx (0 .. $#{$self->objects}) {
|
2012-08-26 15:47:00 +00:00
|
|
|
my @layers = map $self->objects->[$obj_idx]->layer($_), 0..($skirt_height-1);
|
2012-04-29 10:51:20 +00:00
|
|
|
my @layer_points = (
|
|
|
|
(map @$_, map @{$_->expolygon}, map @{$_->slices}, @layers),
|
|
|
|
(map @$_, map @{$_->thin_walls}, @layers),
|
2012-07-20 12:39:07 +00:00
|
|
|
(map @{$_->unpack->polyline}, map @{$_->support_fills->paths}, grep $_->support_fills, @layers),
|
2012-04-29 10:51:20 +00:00
|
|
|
);
|
|
|
|
push @points, map move_points($_, @layer_points), @{$self->copies->[$obj_idx]};
|
|
|
|
}
|
2012-03-31 16:32:53 +00:00
|
|
|
return if @points < 3; # at least three points required for a convex hull
|
2011-11-13 17:41:12 +00:00
|
|
|
|
|
|
|
# find out convex hull
|
2012-04-29 10:51:20 +00:00
|
|
|
my $convex_hull = convex_hull(\@points);
|
2011-11-13 17:41:12 +00:00
|
|
|
|
|
|
|
# draw outlines from outside to inside
|
2012-06-23 19:31:29 +00:00
|
|
|
my $flow = $Slic3r::first_layer_flow || $Slic3r::flow;
|
2012-04-29 10:51:20 +00:00
|
|
|
my @skirt = ();
|
2012-07-27 19:13:03 +00:00
|
|
|
for (my $i = $Slic3r::Config->skirts; $i > 0; $i--) {
|
|
|
|
my $distance = scale ($Slic3r::Config->skirt_distance + ($flow->spacing * $i));
|
2012-08-25 18:04:29 +00:00
|
|
|
my $outline = Math::Clipper::offset([$convex_hull], $distance, &Slic3r::SCALING_FACTOR * 100, JT_ROUND);
|
2012-07-20 12:39:07 +00:00
|
|
|
push @skirt, Slic3r::ExtrusionLoop->pack(
|
2011-12-30 18:59:51 +00:00
|
|
|
polygon => Slic3r::Polygon->new(@{$outline->[0]}),
|
2012-05-19 13:40:11 +00:00
|
|
|
role => EXTR_ROLE_SKIRT,
|
2011-12-30 18:59:51 +00:00
|
|
|
);
|
2011-11-13 17:41:12 +00:00
|
|
|
}
|
2012-06-23 15:41:44 +00:00
|
|
|
unshift @{$self->skirt}, @skirt;
|
2012-02-19 11:03:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-23 19:31:29 +00:00
|
|
|
sub make_brim {
|
|
|
|
my $self = shift;
|
2012-07-27 19:13:03 +00:00
|
|
|
return unless $Slic3r::Config->brim_width > 0;
|
2012-06-23 19:31:29 +00:00
|
|
|
|
2012-08-07 20:04:58 +00:00
|
|
|
my $flow = $Slic3r::first_layer_flow || $Slic3r::flow;
|
|
|
|
my $grow_distance = scale $flow->width / 2;
|
2012-06-23 19:31:29 +00:00
|
|
|
my @islands = (); # array of polygons
|
|
|
|
foreach my $obj_idx (0 .. $#{$self->objects}) {
|
2012-08-06 18:54:49 +00:00
|
|
|
my $layer0 = $self->objects->[$obj_idx]->layers->[0];
|
|
|
|
my @object_islands = (
|
|
|
|
(map $_->contour, @{$layer0->slices}),
|
2012-08-07 20:04:58 +00:00
|
|
|
(map { $_->isa('Slic3r::Polygon') ? $_ : $_->grow($grow_distance) } @{$layer0->thin_walls}),
|
|
|
|
(map $_->unpack->polyline->grow($grow_distance), map @{$_->support_fills->paths}, grep $_->support_fills, $layer0),
|
2012-08-06 18:54:49 +00:00
|
|
|
);
|
2012-06-23 19:31:29 +00:00
|
|
|
foreach my $copy (@{$self->copies->[$obj_idx]}) {
|
|
|
|
push @islands, map $_->clone->translate(@$copy), @object_islands;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
my $num_loops = sprintf "%.0f", $Slic3r::Config->brim_width / $flow->width;
|
2012-06-23 19:31:29 +00:00
|
|
|
for my $i (reverse 1 .. $num_loops) {
|
2012-08-06 18:26:08 +00:00
|
|
|
# JT_SQUARE ensures no vertex is outside the given offset distance
|
2012-07-20 12:39:07 +00:00
|
|
|
push @{$self->brim}, Slic3r::ExtrusionLoop->pack(
|
2012-06-23 19:31:29 +00:00
|
|
|
polygon => Slic3r::Polygon->new($_),
|
|
|
|
role => EXTR_ROLE_SKIRT,
|
2012-08-06 18:26:08 +00:00
|
|
|
) for @{Math::Clipper::offset(\@islands, $i * scale $flow->spacing, 100, JT_SQUARE)};
|
2012-06-23 19:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
sub write_gcode {
|
2011-09-03 18:47:38 +00:00
|
|
|
my $self = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
|
|
|
# open output gcode file
|
|
|
|
open my $fh, ">", $file
|
|
|
|
or die "Failed to open $file for writing\n";
|
|
|
|
|
2011-12-01 21:20:48 +00:00
|
|
|
# write some information
|
|
|
|
my @lt = localtime;
|
2012-05-01 13:01:56 +00:00
|
|
|
printf $fh "; generated by Slic3r $Slic3r::VERSION on %04d-%02d-%02d at %02d:%02d:%02d\n\n",
|
2011-12-30 17:57:58 +00:00
|
|
|
$lt[5] + 1900, $lt[4]+1, $lt[3], $lt[2], $lt[1], $lt[0];
|
2012-02-05 19:55:17 +00:00
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
print $fh "; $_\n" foreach split /\R/, $Slic3r::Config->notes;
|
|
|
|
print $fh "\n" if $Slic3r::Config->notes;
|
2011-12-01 21:20:48 +00:00
|
|
|
|
2012-06-28 14:22:11 +00:00
|
|
|
for (qw(layer_height perimeters solid_layers fill_density perimeter_speed infill_speed travel_speed scale)) {
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh "; %s = %s\n", $_, $Slic3r::Config->$_;
|
2011-12-01 21:20:48 +00:00
|
|
|
}
|
2012-06-28 14:22:11 +00:00
|
|
|
for (qw(nozzle_diameter filament_diameter extrusion_multiplier)) {
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh "; %s = %s\n", $_, $Slic3r::Config->$_->[0];
|
2012-06-28 14:22:11 +00:00
|
|
|
}
|
2012-06-06 16:05:03 +00:00
|
|
|
printf $fh "; single wall width = %.2fmm\n", $Slic3r::flow->width;
|
|
|
|
printf $fh "; first layer single wall width = %.2fmm\n", $Slic3r::first_layer_flow->width
|
|
|
|
if $Slic3r::first_layer_flow;
|
2011-12-01 21:20:48 +00:00
|
|
|
print $fh "\n";
|
|
|
|
|
2012-05-20 18:07:39 +00:00
|
|
|
# set up our extruder object
|
2012-06-28 12:44:54 +00:00
|
|
|
my $gcodegen = Slic3r::GCode->new;
|
2012-07-27 19:13:03 +00:00
|
|
|
my $min_print_speed = 60 * $Slic3r::Config->min_print_speed;
|
2012-06-28 12:44:54 +00:00
|
|
|
my $dec = $gcodegen->dec;
|
2012-08-22 17:20:34 +00:00
|
|
|
print $fh $gcodegen->set_tool(0);
|
2012-07-27 19:13:03 +00:00
|
|
|
print $fh $gcodegen->set_fan(0, 1) if $Slic3r::Config->cooling && $Slic3r::Config->disable_fan_first_layers;
|
2012-05-20 18:07:39 +00:00
|
|
|
|
2012-08-22 18:31:03 +00:00
|
|
|
# this spits out some platic at start from each extruder when they are first used;
|
|
|
|
# the primary extruder will compensate by the normal retraction length, while
|
|
|
|
# the others will compensate for their toolchange length + restart extra.
|
|
|
|
# this is a temporary solution as all extruders should use some kind of skirt
|
|
|
|
# to be put into a consistent state.
|
|
|
|
$_->retracted($_->retract_length_toolchange + $_->retract_restart_extra_toolchange)
|
|
|
|
for @{$Slic3r::extruders}[1 .. $#{$Slic3r::extruders}];
|
2012-08-22 16:57:03 +00:00
|
|
|
$gcodegen->retract;
|
|
|
|
|
2011-09-03 18:47:38 +00:00
|
|
|
# write start commands to file
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh $gcodegen->set_bed_temperature($Slic3r::Config->first_layer_bed_temperature, 1),
|
2012-08-22 15:58:38 +00:00
|
|
|
if $Slic3r::Config->first_layer_bed_temperature && $Slic3r::Config->start_gcode !~ /M190/i;
|
|
|
|
my $print_first_layer_temperature = sub {
|
|
|
|
for my $t (grep $Slic3r::extruders->[$_], 0 .. $#{$Slic3r::Config->first_layer_temperature}) {
|
|
|
|
printf $fh $gcodegen->set_temperature($Slic3r::extruders->[$t]->first_layer_temperature, 0, $t)
|
|
|
|
if $Slic3r::extruders->[$t]->first_layer_temperature;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$print_first_layer_temperature->();
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->start_gcode);
|
|
|
|
for my $t (grep $Slic3r::extruders->[$_], 0 .. $#{$Slic3r::Config->first_layer_temperature}) {
|
2012-06-28 14:22:11 +00:00
|
|
|
printf $fh $gcodegen->set_temperature($Slic3r::extruders->[$t]->first_layer_temperature, 1, $t)
|
2012-07-27 19:13:03 +00:00
|
|
|
if $Slic3r::extruders->[$t]->first_layer_temperature && $Slic3r::Config->start_gcode !~ /M109/i;
|
2012-06-28 14:22:11 +00:00
|
|
|
}
|
2011-09-03 18:47:38 +00:00
|
|
|
print $fh "G90 ; use absolute coordinates\n";
|
|
|
|
print $fh "G21 ; set units to millimeters\n";
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->gcode_flavor =~ /^(?:reprap|teacup)$/) {
|
2012-07-06 17:57:58 +00:00
|
|
|
printf $fh $gcodegen->reset_e;
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->gcode_flavor =~ /^(?:reprap|makerbot)$/) {
|
|
|
|
if ($Slic3r::Config->use_relative_e_distances) {
|
2012-02-24 22:12:16 +00:00
|
|
|
print $fh "M83 ; use relative distances for extrusion\n";
|
|
|
|
} else {
|
|
|
|
print $fh "M82 ; use absolute distances for extrusion\n";
|
|
|
|
}
|
2012-02-20 10:44:30 +00:00
|
|
|
}
|
2011-09-03 18:47:38 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 09:48:58 +00:00
|
|
|
# calculate X,Y shift to center print around specified origin
|
2012-04-30 12:56:01 +00:00
|
|
|
my @print_bb = $self->bounding_box;
|
2012-02-19 09:48:58 +00:00
|
|
|
my @shift = (
|
2012-07-27 19:13:03 +00:00
|
|
|
$Slic3r::Config->print_center->[X] - (unscale ($print_bb[X2] - $print_bb[X1]) / 2) - unscale $print_bb[X1],
|
|
|
|
$Slic3r::Config->print_center->[Y] - (unscale ($print_bb[Y2] - $print_bb[Y1]) / 2) - unscale $print_bb[Y1],
|
2011-09-26 09:42:08 +00:00
|
|
|
);
|
2011-09-05 10:21:27 +00:00
|
|
|
|
2012-05-20 18:07:39 +00:00
|
|
|
# prepare the logic to print one layer
|
2012-05-31 07:37:39 +00:00
|
|
|
my $skirt_done = 0; # count of skirt layers done
|
2012-06-30 14:14:45 +00:00
|
|
|
my $brim_done = 0;
|
2012-05-20 18:07:39 +00:00
|
|
|
my $extrude_layer = sub {
|
|
|
|
my ($layer_id, $object_copies) = @_;
|
|
|
|
my $gcode = "";
|
|
|
|
|
2012-05-01 11:18:01 +00:00
|
|
|
if ($layer_id == 1) {
|
2012-07-27 19:13:03 +00:00
|
|
|
for my $t (grep $Slic3r::extruders->[$_], 0 .. $#{$Slic3r::Config->temperature}) {
|
2012-06-28 14:22:11 +00:00
|
|
|
$gcode .= $gcodegen->set_temperature($Slic3r::extruders->[$t]->temperature, 0, $t)
|
|
|
|
if $Slic3r::extruders->[$t]->temperature && $Slic3r::extruders->[$t]->temperature != $Slic3r::extruders->[$t]->first_layer_temperature;
|
|
|
|
}
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $gcodegen->set_bed_temperature($Slic3r::Config->bed_temperature)
|
|
|
|
if $Slic3r::Config->first_layer_bed_temperature && $Slic3r::Config->bed_temperature != $Slic3r::Config->first_layer_bed_temperature;
|
2012-02-26 13:54:38 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 18:07:39 +00:00
|
|
|
# go to layer (just use the first one, we only need Z from it)
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->change_layer($self->objects->[$object_copies->[0][0]]->layers->[$layer_id]);
|
|
|
|
$gcodegen->elapsed_time(0);
|
2012-02-25 20:01:00 +00:00
|
|
|
|
2012-04-29 10:51:20 +00:00
|
|
|
# extrude skirt
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($skirt_done < $Slic3r::Config->skirt_height) {
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcodegen->shift_x($shift[X]);
|
|
|
|
$gcodegen->shift_y($shift[Y]);
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $gcodegen->set_acceleration($Slic3r::Config->perimeter_acceleration);
|
2012-06-23 19:31:29 +00:00
|
|
|
# skip skirt if we have a large brim
|
2012-08-06 18:26:08 +00:00
|
|
|
if ($layer_id < $Slic3r::Config->skirt_height && ($layer_id != 0 || $Slic3r::Config->skirt_distance + (($Slic3r::Config->skirts - 1) * $Slic3r::flow->spacing) > $Slic3r::Config->brim_width)) {
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->extrude_loop($_, 'skirt') for @{$self->skirt};
|
2012-05-20 18:07:39 +00:00
|
|
|
}
|
2012-05-31 07:37:39 +00:00
|
|
|
$skirt_done++;
|
2012-04-29 10:51:20 +00:00
|
|
|
}
|
2011-09-05 18:00:59 +00:00
|
|
|
|
2012-06-23 19:31:29 +00:00
|
|
|
# extrude brim
|
2012-06-30 14:14:45 +00:00
|
|
|
if ($layer_id == 0 && !$brim_done) {
|
2012-08-19 18:30:17 +00:00
|
|
|
$gcodegen->shift_x($shift[X]);
|
|
|
|
$gcodegen->shift_y($shift[Y]);
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->extrude_loop($_, 'brim') for @{$self->brim};
|
2012-06-30 14:14:45 +00:00
|
|
|
$brim_done = 1;
|
2012-06-23 19:31:29 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 18:07:39 +00:00
|
|
|
for my $obj_copy (@$object_copies) {
|
|
|
|
my ($obj_idx, $copy) = @$obj_copy;
|
2012-05-01 11:18:01 +00:00
|
|
|
my $layer = $self->objects->[$obj_idx]->layers->[$layer_id];
|
2012-05-20 18:07:39 +00:00
|
|
|
|
|
|
|
# retract explicitely because changing the shift_[xy] properties below
|
|
|
|
# won't always trigger the automatic retraction
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->retract;
|
2012-05-20 18:07:39 +00:00
|
|
|
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcodegen->shift_x($shift[X] + unscale $copy->[X]);
|
|
|
|
$gcodegen->shift_y($shift[Y] + unscale $copy->[Y]);
|
2012-05-20 18:07:39 +00:00
|
|
|
|
|
|
|
# extrude perimeters
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $gcodegen->set_tool($Slic3r::Config->perimeter_extruder-1);
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->extrude($_, 'perimeter') for @{ $layer->perimeters };
|
2012-05-20 18:07:39 +00:00
|
|
|
|
|
|
|
# extrude fills
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $gcodegen->set_tool($Slic3r::Config->infill_extruder-1);
|
|
|
|
$gcode .= $gcodegen->set_acceleration($Slic3r::Config->infill_acceleration);
|
2012-05-20 18:07:39 +00:00
|
|
|
for my $fill (@{ $layer->fills }) {
|
2012-07-20 13:02:25 +00:00
|
|
|
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
|
|
|
|
$gcode .= $gcodegen->extrude($_, 'fill')
|
|
|
|
for $fill->shortest_path($gcodegen->last_pos);
|
|
|
|
} else {
|
|
|
|
$gcode .= $gcodegen->extrude($fill, 'fill') ;
|
|
|
|
}
|
2012-05-20 18:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# extrude support material
|
|
|
|
if ($layer->support_fills) {
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode .= $gcodegen->set_tool($Slic3r::Config->support_material_extruder-1);
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode .= $gcodegen->extrude_path($_, 'support material')
|
|
|
|
for $layer->support_fills->shortest_path($gcodegen->last_pos);
|
2012-02-19 11:03:36 +00:00
|
|
|
}
|
2011-09-26 08:52:58 +00:00
|
|
|
}
|
2012-06-23 16:31:24 +00:00
|
|
|
return if !$gcode;
|
2012-02-25 20:01:00 +00:00
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
my $fan_speed = $Slic3r::Config->fan_always_on ? $Slic3r::Config->min_fan_speed : 0;
|
2012-02-25 20:01:00 +00:00
|
|
|
my $speed_factor = 1;
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->cooling) {
|
2012-06-28 12:44:54 +00:00
|
|
|
my $layer_time = $gcodegen->elapsed_time;
|
2012-05-01 11:18:01 +00:00
|
|
|
Slic3r::debugf "Layer %d estimated printing time: %d seconds\n", $layer_id, $layer_time;
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($layer_time < $Slic3r::Config->slowdown_below_layer_time) {
|
|
|
|
$fan_speed = $Slic3r::Config->max_fan_speed;
|
|
|
|
$speed_factor = $layer_time / $Slic3r::Config->slowdown_below_layer_time;
|
|
|
|
} elsif ($layer_time < $Slic3r::Config->fan_below_layer_time) {
|
|
|
|
$fan_speed = $Slic3r::Config->max_fan_speed - ($Slic3r::Config->max_fan_speed - $Slic3r::Config->min_fan_speed)
|
|
|
|
* ($layer_time - $Slic3r::Config->slowdown_below_layer_time)
|
|
|
|
/ ($Slic3r::Config->fan_below_layer_time - $Slic3r::Config->slowdown_below_layer_time); #/
|
2012-02-25 20:01:00 +00:00
|
|
|
}
|
2012-02-25 20:56:36 +00:00
|
|
|
Slic3r::debugf " fan = %d%%, speed = %d%%\n", $fan_speed, $speed_factor * 100;
|
|
|
|
|
|
|
|
if ($speed_factor < 1) {
|
2012-05-20 18:07:39 +00:00
|
|
|
$gcode =~ s/^(?=.*? [XY])(?=.*? E)(G1 .*?F)(\d+(?:\.\d+)?)/
|
2012-02-25 20:56:36 +00:00
|
|
|
my $new_speed = $2 * $speed_factor;
|
|
|
|
$1 . sprintf("%.${dec}f", $new_speed < $min_print_speed ? $min_print_speed : $new_speed)
|
|
|
|
/gexm;
|
|
|
|
}
|
2012-07-27 19:13:03 +00:00
|
|
|
$fan_speed = 0 if $layer_id < $Slic3r::Config->disable_fan_first_layers;
|
2012-02-25 20:01:00 +00:00
|
|
|
}
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode = $gcodegen->set_fan($fan_speed) . $gcode;
|
2012-02-25 20:01:00 +00:00
|
|
|
|
|
|
|
# bridge fan speed
|
2012-07-27 19:13:03 +00:00
|
|
|
if (!$Slic3r::Config->cooling || $Slic3r::Config->bridge_fan_speed == 0 || $layer_id < $Slic3r::Config->disable_fan_first_layers) {
|
2012-05-20 18:07:39 +00:00
|
|
|
$gcode =~ s/^;_BRIDGE_FAN_(?:START|END)\n//gm;
|
2012-02-25 20:01:00 +00:00
|
|
|
} else {
|
2012-07-27 19:13:03 +00:00
|
|
|
$gcode =~ s/^;_BRIDGE_FAN_START\n/ $gcodegen->set_fan($Slic3r::Config->bridge_fan_speed, 1) /gmex;
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcode =~ s/^;_BRIDGE_FAN_END\n/ $gcodegen->set_fan($fan_speed, 1) /gmex;
|
2012-05-20 18:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
};
|
|
|
|
|
|
|
|
# do all objects for each layer
|
2012-07-27 19:13:03 +00:00
|
|
|
if ($Slic3r::Config->complete_objects) {
|
2012-05-20 18:07:39 +00:00
|
|
|
|
2012-05-21 18:19:30 +00:00
|
|
|
# print objects from the smallest to the tallest to avoid collisions
|
|
|
|
# when moving onto next object starting point
|
2012-05-21 18:23:17 +00:00
|
|
|
my @obj_idx = sort { $self->objects->[$a]->layer_count <=> $self->objects->[$b]->layer_count } 0..$#{$self->objects};
|
2012-02-25 20:01:00 +00:00
|
|
|
|
2012-05-20 18:07:39 +00:00
|
|
|
my $finished_objects = 0;
|
2012-05-21 18:19:30 +00:00
|
|
|
for my $obj_idx (@obj_idx) {
|
2012-05-20 18:07:39 +00:00
|
|
|
for my $copy (@{ $self->copies->[$obj_idx] }) {
|
|
|
|
# move to the origin position for the copy we're going to print.
|
|
|
|
# this happens before Z goes down to layer 0 again, so that
|
|
|
|
# no collision happens hopefully.
|
|
|
|
if ($finished_objects > 0) {
|
2012-06-28 12:44:54 +00:00
|
|
|
$gcodegen->shift_x($shift[X] + unscale $copy->[X]);
|
|
|
|
$gcodegen->shift_y($shift[Y] + unscale $copy->[Y]);
|
|
|
|
print $fh $gcodegen->retract;
|
|
|
|
print $fh $gcodegen->G0(Slic3r::Point->new(0,0), undef, 0, 'move to origin position for next object');
|
2012-05-20 18:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for my $layer_id (0..$#{$self->objects->[$obj_idx]->layers}) {
|
|
|
|
# if we are printing the bottom layer of an object, and we have already finished
|
|
|
|
# another one, set first layer temperatures. this happens before the Z move
|
|
|
|
# is triggered, so machine has more time to reach such temperatures
|
|
|
|
if ($layer_id == 0 && $finished_objects > 0) {
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh $gcodegen->set_bed_temperature($Slic3r::Config->first_layer_bed_temperature),
|
|
|
|
if $Slic3r::Config->first_layer_bed_temperature;
|
2012-08-22 15:58:38 +00:00
|
|
|
$print_first_layer_temperature->();
|
2012-05-20 18:07:39 +00:00
|
|
|
}
|
|
|
|
print $fh $extrude_layer->($layer_id, [[ $obj_idx, $copy ]]);
|
|
|
|
}
|
|
|
|
$finished_objects++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for my $layer_id (0..$self->layer_count-1) {
|
|
|
|
my @object_copies = ();
|
|
|
|
for my $obj_idx (grep $self->objects->[$_]->layers->[$layer_id], 0..$#{$self->objects}) {
|
|
|
|
push @object_copies, map [ $obj_idx, $_ ], @{ $self->copies->[$obj_idx] };
|
|
|
|
}
|
|
|
|
print $fh $extrude_layer->($layer_id, \@object_copies);
|
|
|
|
}
|
2011-09-03 18:47:38 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 14:29:15 +00:00
|
|
|
# save statistic data
|
2012-06-28 12:44:54 +00:00
|
|
|
$self->total_extrusion_length($gcodegen->total_extrusion_length);
|
2011-12-20 14:29:15 +00:00
|
|
|
|
2011-09-03 18:47:38 +00:00
|
|
|
# write end commands to file
|
2012-06-28 12:44:54 +00:00
|
|
|
print $fh $gcodegen->retract;
|
|
|
|
print $fh $gcodegen->set_fan(0);
|
2012-07-27 19:13:03 +00:00
|
|
|
print $fh "M501 ; reset acceleration\n" if $Slic3r::Config->acceleration;
|
|
|
|
printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->end_gcode);
|
2011-09-03 18:47:38 +00:00
|
|
|
|
2011-12-20 14:29:15 +00:00
|
|
|
printf $fh "; filament used = %.1fmm (%.1fcm3)\n",
|
|
|
|
$self->total_extrusion_length, $self->total_extrusion_volume;
|
|
|
|
|
2011-09-03 18:47:38 +00:00
|
|
|
# close our gcode file
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:29:15 +00:00
|
|
|
sub total_extrusion_volume {
|
|
|
|
my $self = shift;
|
2012-06-28 12:44:54 +00:00
|
|
|
return $self->total_extrusion_length * ($Slic3r::extruders->[0]->filament_diameter**2) * PI/4 / 1000;
|
2011-12-20 14:29:15 +00:00
|
|
|
}
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
# this method will return the value of $self->output_file after expanding its
|
|
|
|
# format variables with their values
|
|
|
|
sub expanded_output_filepath {
|
|
|
|
my $self = shift;
|
|
|
|
my ($path) = @_;
|
|
|
|
|
2012-08-01 15:12:16 +00:00
|
|
|
# if output path is an existing directory, we take that and append
|
|
|
|
# the specified filename format
|
|
|
|
$path = File::Spec->join($path, $Slic3r::Config->output_filename_format) if ($path && -d $path);
|
|
|
|
|
2012-04-30 12:56:01 +00:00
|
|
|
# if no explicit output file was defined, we take the input
|
|
|
|
# file directory and append the specified filename format
|
|
|
|
my $input_file = $self->objects->[0]->input_file;
|
2012-07-27 19:13:03 +00:00
|
|
|
$path ||= (fileparse($input_file))[1] . $Slic3r::Config->output_filename_format;
|
2012-04-30 12:56:01 +00:00
|
|
|
|
|
|
|
my $input_filename = my $input_filename_base = basename($input_file);
|
|
|
|
$input_filename_base =~ s/\.(?:stl|amf(?:\.xml)?)$//i;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
return $Slic3r::Config->replace_options($path, {
|
2012-04-30 12:56:01 +00:00
|
|
|
input_filename => $input_filename,
|
|
|
|
input_filename_base => $input_filename_base,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-09-01 19:06:28 +00:00
|
|
|
1;
|