custom_gcode.t 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. use Test::More tests => 39;
  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 (legacy syntax)";
  43. is $parser->process('{temperature[foo]}'),
  44. $config->temperature->[0],
  45. "array reference";
  46. }
  47. {
  48. my $config = Slic3r::Config::new_from_defaults;
  49. $config->set('output_filename_format', 'ts_[travel_speed]_lh_[layer_height].gcode');
  50. $config->set('start_gcode', "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n");
  51. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  52. my $output_file = $print->print->output_filepath;
  53. my ($t, $h) = map $config->$_, qw(travel_speed layer_height);
  54. ok $output_file =~ /ts_${t}_/, 'print config options are replaced in output filename';
  55. ok $output_file =~ /lh_$h\./, 'region config options are replaced in output filename';
  56. my $gcode = Slic3r::Test::gcode($print);
  57. ok $gcode =~ /TRAVEL:$t/, 'print config options are replaced in custom G-code';
  58. ok $gcode =~ /HEIGHT:$h/, 'region config options are replaced in custom G-code';
  59. }
  60. {
  61. my $config = Slic3r::Config->new;
  62. $config->set('extruder', 2);
  63. $config->set('first_layer_temperature', [200,205]);
  64. {
  65. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  66. my $gcode = Slic3r::Test::gcode($print);
  67. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for non-zero yet single extruder';
  68. ok $gcode !~ /M104 S\d+ T0/, 'unused extruder correctly ignored';
  69. }
  70. $config->set('infill_extruder', 1);
  71. {
  72. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  73. my $gcode = Slic3r::Test::gcode($print);
  74. ok $gcode =~ /M104 S200 T0/, 'temperature set correctly for first extruder';
  75. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for second extruder';
  76. }
  77. my @start_gcode = (qq!
  78. ;__temp0:[first_layer_temperature_0]__
  79. ;__temp1:[first_layer_temperature_1]__
  80. ;__temp2:[first_layer_temperature_2]__
  81. !, qq!
  82. ;__temp0:{first_layer_temperature[0]}__
  83. ;__temp1:{first_layer_temperature[1]}__
  84. ;__temp2:{first_layer_temperature[2]}__
  85. !);
  86. my @syntax_description = (' (legacy syntax)', ' (new syntax)');
  87. for my $i (0, 1) {
  88. $config->set('start_gcode', $start_gcode[$i]);
  89. {
  90. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  91. my $gcode = Slic3r::Test::gcode($print);
  92. # we use the [infill_extruder] placeholder to make sure this test doesn't
  93. # catch a false positive caused by the unparsed start G-code option itself
  94. # being embedded in the G-code
  95. ok $gcode =~ /temp0:200/, 'temperature placeholder for first extruder correctly populated' . $syntax_description[$i];
  96. ok $gcode =~ /temp1:205/, 'temperature placeholder for second extruder correctly populated' . $syntax_description[$i];
  97. ok $gcode =~ /temp2:200/, 'temperature placeholder for unused extruder populated with first value' . $syntax_description[$i];
  98. }
  99. }
  100. $config->set('start_gcode', qq!
  101. ;substitution:{if infill_extruder==1}extruder1
  102. {elsif infill_extruder==2}extruder2
  103. {else}extruder3{endif}
  104. !);
  105. {
  106. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  107. my $gcode = Slic3r::Test::gcode($print);
  108. ok $gcode =~ /substitution:extruder1/, 'if / else / endif - first block returned';
  109. }
  110. }
  111. {
  112. my $config = Slic3r::Config::new_from_defaults;
  113. $config->set('before_layer_gcode', ';BEFORE [layer_num]');
  114. $config->set('layer_gcode', ';CHANGE [layer_num]');
  115. $config->set('support_material', 1);
  116. $config->set('layer_height', 0.2);
  117. my $print = Slic3r::Test::init_print('overhang', config => $config);
  118. my $gcode = Slic3r::Test::gcode($print);
  119. my @before = ();
  120. my @change = ();
  121. foreach my $line (split /\R+/, $gcode) {
  122. if ($line =~ /;BEFORE (\d+)/) {
  123. push @before, $1;
  124. } elsif ($line =~ /;CHANGE (\d+)/) {
  125. push @change, $1;
  126. fail 'inconsistent layer_num before and after layer change'
  127. if $1 != $before[-1];
  128. }
  129. }
  130. is_deeply \@before, \@change, 'layer_num is consistent before and after layer changes';
  131. ok !defined(first { $change[$_] != $change[$_-1]+1 } 1..$#change),
  132. 'layer_num grows continously'; # i.e. no duplicates or regressions
  133. }
  134. {
  135. my $config = Slic3r::Config->new;
  136. $config->set('start_gcode', qq!
  137. ;substitution:{if infill_extruder==1}if block
  138. {elsif infill_extruder==2}elsif block 1
  139. {elsif infill_extruder==3}elsif block 2
  140. {elsif infill_extruder==4}elsif block 3
  141. {else}endif block{endif}
  142. !);
  143. my @returned = ('', 'if block', 'elsif block 1', 'elsif block 2', 'elsif block 3', 'endif block');
  144. for my $i (1,2,3,4,5) {
  145. $config->set('infill_extruder', $i);
  146. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  147. my $gcode = Slic3r::Test::gcode($print);
  148. my $found_other = 0;
  149. for my $j (1,2,3,4,5) {
  150. next if $i == $j;
  151. $found_other = 1 if $gcode =~ /substitution:$returned[$j]/;
  152. }
  153. ok $gcode =~ /substitution:$returned[$i]/, 'if / else / endif - ' . $returned[$i] . ' returned';
  154. ok !$found_other, 'if / else / endif - only ' . $returned[$i] . ' returned';
  155. }
  156. }
  157. {
  158. my $config = Slic3r::Config->new;
  159. $config->set('start_gcode',
  160. ';substitution:{if infill_extruder==1}{if perimeter_extruder==1}block11{else}block12{endif}' .
  161. '{elsif infill_extruder==2}{if perimeter_extruder==1}block21{else}block22{endif}' .
  162. '{else}{if perimeter_extruder==1}block31{else}block32{endif}{endif}:end');
  163. for my $i (1,2,3) {
  164. $config->set('infill_extruder', $i);
  165. for my $j (1,2) {
  166. $config->set('perimeter_extruder', $j);
  167. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  168. my $gcode = Slic3r::Test::gcode($print);
  169. ok $gcode =~ /substitution:block$i$j:end/, "two level if / else / endif - block$i$j returned";
  170. }
  171. }
  172. }
  173. {
  174. my $config = Slic3r::Config->new;
  175. $config->set('start_gcode',
  176. ';substitution:{if notes=="MK2"}MK2{elsif notes=="MK3"}MK3{else}MK1{endif}:end');
  177. for my $printer_name ("MK2", "MK3", "MK1") {
  178. $config->set('notes', $printer_name);
  179. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  180. my $gcode = Slic3r::Test::gcode($print);
  181. ok $gcode =~ /substitution:$printer_name:end/, "printer name $printer_name matched";
  182. }
  183. }
  184. __END__