diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index adef54113..610d4aace 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -79,7 +79,30 @@ sub export_gcode { # output everything to a G-code file my $output_file = $self->output_filepath($params{output_file} // ''); $self->status_cb->(90, "Exporting G-code" . ($output_file ? " to $output_file" : "")); - $self->write_gcode($params{output_fh} || $output_file); + + { + # open output gcode file if we weren't supplied a file-handle + my ($fh, $tempfile); + if ($params{output_fh}) { + $fh = $params{output_fh}; + } else { + $tempfile = "$output_file.tmp"; + Slic3r::open(\$fh, ">", $tempfile) + or die "Failed to open $tempfile for writing\n"; + + # enable UTF-8 output since user might have entered Unicode characters in fields like notes + binmode $fh, ':utf8'; + } + + Slic3r::Print::GCode->new( + print => $self, + fh => $fh, + )->export; + + # close our gcode file + close $fh; + rename $tempfile, $output_file if $tempfile; + } # run post-processing scripts if (@{$self->config->post_process}) { @@ -287,45 +310,6 @@ sub make_brim { $self->set_step_done(STEP_BRIM); } -sub write_gcode { - my $self = shift; - my ($file) = @_; - - my $tempfile; - - # open output gcode file if we weren't supplied a file-handle - my $fh; - if (ref $file eq 'IO::Scalar') { - $fh = $file; - } else { - $tempfile = "$file.tmp"; - Slic3r::open(\$fh, ">", $tempfile) - or die "Failed to open $tempfile for writing\n"; - - # enable UTF-8 output since user might have entered Unicode characters in fields like notes - binmode $fh, ':utf8'; - } - - my $exporter = Slic3r::Print::GCode->new( - print => $self, - fh => $fh, - ); - $exporter->export; - - # close our gcode file - close $fh; - - if ($tempfile) { - my $i; - for ($i = 0; $i < 5; $i += 1) { - last if (rename Slic3r::encode_path($tempfile), Slic3r::encode_path($file)); - # Wait for 1/4 seconds and try to rename once again. - select(undef, undef, undef, 0.25); - } - Slic3r::debugf "Failed to remove the output G-code file from $tempfile to $file. Is $tempfile locked?\n" if ($i == 5); - } -} - # Wrapper around the C++ Slic3r::Print::validate() # to produce a Perl exception without a hang-up on some Strawberry perls. sub validate