custom_gcode.t 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. use Test::More tests => 16;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use local::lib "$FindBin::Bin/../local-lib";
  8. }
  9. use List::Util qw(first);
  10. use Slic3r;
  11. use Slic3r::Test;
  12. {
  13. my $config = Slic3r::Config->new_from_defaults;
  14. my $test = sub {
  15. my ($conf) = @_;
  16. $conf ||= $config;
  17. my $print = Slic3r::Test::init_print('2x20x10', config => $conf);
  18. my $last_move_was_z_change = 0;
  19. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  20. my ($self, $cmd, $args, $info) = @_;
  21. if ($last_move_was_z_change && $cmd ne $config->layer_gcode) {
  22. fail 'custom layer G-code was not applied after Z change';
  23. }
  24. if (!$last_move_was_z_change && $cmd eq $config->layer_gcode) {
  25. fail 'custom layer G-code was not applied after Z change';
  26. }
  27. $last_move_was_z_change = (defined $info->{dist_Z} && $info->{dist_Z} > 0);
  28. });
  29. 1;
  30. };
  31. $config->set('start_gcode', '_MY_CUSTOM_START_GCODE_'); # to avoid dealing with the nozzle lift in start G-code
  32. $config->set('layer_gcode', '_MY_CUSTOM_LAYER_GCODE_');
  33. ok $test->(), "custom layer G-code is applied after Z move and before other moves";
  34. }
  35. #==========================================================
  36. {
  37. my $parser = Slic3r::GCode::PlaceholderParser->new;
  38. $parser->apply_config(my $config = Slic3r::Config->new_from_defaults);
  39. $parser->set('foo' => '0');
  40. is $parser->process('[temperature_[foo]]'),
  41. $config->temperature->[0],
  42. "nested config options";
  43. }
  44. {
  45. my $config = Slic3r::Config->new_from_defaults;
  46. $config->set('output_filename_format', 'ts_[travel_speed]_lh_[layer_height].gcode');
  47. $config->set('start_gcode', "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n");
  48. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  49. my $output_file = $print->print->output_filepath;
  50. my ($t, $h) = map $config->$_, qw(travel_speed layer_height);
  51. ok $output_file =~ /ts_${t}_/, 'print config options are replaced in output filename';
  52. ok $output_file =~ /lh_$h\./, 'region config options are replaced in output filename';
  53. my $gcode = Slic3r::Test::gcode($print);
  54. ok $gcode =~ /TRAVEL:$t/, 'print config options are replaced in custom G-code';
  55. ok $gcode =~ /HEIGHT:$h/, 'region config options are replaced in custom G-code';
  56. }
  57. {
  58. my $config = Slic3r::Config->new;
  59. $config->set('extruder', 2);
  60. $config->set('first_layer_temperature', [200,205]);
  61. {
  62. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  63. my $gcode = Slic3r::Test::gcode($print);
  64. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for non-zero yet single extruder';
  65. ok $gcode !~ /M104 S\d+ T0/, 'unused extruder correctly ignored';
  66. }
  67. $config->set('infill_extruder', 1);
  68. {
  69. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  70. my $gcode = Slic3r::Test::gcode($print);
  71. ok $gcode =~ /M104 S200 T0/, 'temperature set correctly for first extruder';
  72. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for second extruder';
  73. }
  74. $config->set('start_gcode', qq!
  75. ;__temp0:[first_layer_temperature_0]__
  76. ;__temp1:[first_layer_temperature_1]__
  77. ;__temp2:[first_layer_temperature_2]__
  78. !);
  79. {
  80. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  81. my $gcode = Slic3r::Test::gcode($print);
  82. # we use the [infill_extruder] placeholder to make sure this test doesn't
  83. # catch a false positive caused by the unparsed start G-code option itself
  84. # being embedded in the G-code
  85. ok $gcode =~ /temp0:200/, 'temperature placeholder for first extruder correctly populated';
  86. ok $gcode =~ /temp1:205/, 'temperature placeholder for second extruder correctly populated';
  87. ok $gcode =~ /temp2:200/, 'temperature placeholder for unused extruder populated with first value';
  88. }
  89. }
  90. {
  91. my $config = Slic3r::Config->new_from_defaults;
  92. $config->set('before_layer_gcode', ';BEFORE [layer_num]');
  93. $config->set('layer_gcode', ';CHANGE [layer_num]');
  94. $config->set('support_material', 1);
  95. $config->set('layer_height', 0.2);
  96. my $print = Slic3r::Test::init_print('overhang', config => $config);
  97. my $gcode = Slic3r::Test::gcode($print);
  98. my @before = ();
  99. my @change = ();
  100. foreach my $line (split /\R+/, $gcode) {
  101. if ($line =~ /;BEFORE (\d+)/) {
  102. push @before, $1;
  103. } elsif ($line =~ /;CHANGE (\d+)/) {
  104. push @change, $1;
  105. fail 'inconsistent layer_num before and after layer change'
  106. if $1 != $before[-1];
  107. }
  108. }
  109. is_deeply \@before, \@change, 'layer_num is consistent before and after layer changes';
  110. ok !defined(first { $change[$_] != $change[$_-1]+1 } 1..$#change),
  111. 'layer_num grows continously'; # i.e. no duplicates or regressions
  112. }
  113. {
  114. my $config = Slic3r::Config->new_from_defaults;
  115. $config->set('complete_objects', 1);
  116. $config->set('between_objects_gcode', '_MY_CUSTOM_GCODE_');
  117. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 3);
  118. my $gcode = Slic3r::Test::gcode($print);
  119. is scalar(() = $gcode =~ /^_MY_CUSTOM_GCODE_/gm), 2, 'between_objects_gcode is applied correctly';
  120. }
  121. __END__