diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm index 2c41f4c3a..b0894fb88 100644 --- a/lib/Slic3r/GCode.pm +++ b/lib/Slic3r/GCode.pm @@ -646,7 +646,7 @@ sub set_extruder { # append custom toolchange G-code if (defined $self->extruder && $self->config->toolchange_gcode) { - $gcode .= sprintf "%s\n", $self->config->replace_options($self->config->toolchange_gcode, { + $gcode .= sprintf "%s\n", $self->print->replace_variables($self->config->toolchange_gcode, { previous_extruder => $self->extruder->id, next_extruder => $extruder->id, }); diff --git a/lib/Slic3r/GCode/Layer.pm b/lib/Slic3r/GCode/Layer.pm index 5cb858e68..1fd709693 100644 --- a/lib/Slic3r/GCode/Layer.pm +++ b/lib/Slic3r/GCode/Layer.pm @@ -66,7 +66,7 @@ sub process_layer { # set new layer - this will change Z and force a retraction if retract_layer_change is enabled $gcode .= $self->gcodegen->change_layer($layer); - $gcode .= $Slic3r::Config->replace_options($Slic3r::Config->layer_gcode, { + $gcode .= $self->print->replace_variables($Slic3r::Config->layer_gcode, { layer_num => $self->gcodegen->layer->id, }) . "\n" if $Slic3r::Config->layer_gcode; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index f1c645cd5..f9283f2d7 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -156,6 +156,15 @@ sub add_model { layer_height_ranges => $object->layer_height_ranges, ); } + + if (!defined $self->extra_variables->{input_filename}) { + if (defined (my $input_file = $self->objects->[0]->input_file)) { + my $input_filename = my $input_filename_base = basename($input_file); + $input_filename_base =~ s/\.(?:stl|amf(?:\.xml)?)$//i; + $self->extra_variables->{input_filename} = $input_file; + $self->extra_variables->{input_filename_base} = $input_filename_base; + } + } } sub validate { @@ -743,7 +752,7 @@ sub write_gcode { } }; $print_first_layer_temperature->(0); - printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->start_gcode); + printf $fh "%s\n", $self->replace_variables($Slic3r::Config->start_gcode); $print_first_layer_temperature->(1); # set other general things @@ -890,7 +899,7 @@ sub write_gcode { # write end commands to file print $fh $gcodegen->retract if $gcodegen->extruder; # empty prints don't even set an extruder print $fh $gcodegen->set_fan(0); - printf $fh "%s\n", $Slic3r::Config->replace_options($Slic3r::Config->end_gcode); + printf $fh "%s\n", $self->replace_variables($Slic3r::Config->end_gcode); foreach my $extruder (@{$self->extruders}) { printf $fh "; filament used = %.1fmm (%.1fcm3)\n", @@ -929,14 +938,12 @@ sub expanded_output_filepath { # file directory and append the specified filename format $path ||= (fileparse($input_file))[1] . $Slic3r::Config->output_filename_format; - my $input_filename = my $input_filename_base = basename($input_file); - $input_filename_base =~ s/\.(?:stl|amf(?:\.xml)?)$//i; - - return $Slic3r::Config->replace_options($path, { - input_filename => $input_filename, - input_filename_base => $input_filename_base, - %{ $self->extra_variables }, - }); + return $self->replace_variables($path); +} + +sub replace_variables { + my ($self, $string, $extra) = @_; + return $self->config->replace_options($string, { %{$self->extra_variables}, %{ $extra || {} } }); } 1;