custom_gcode.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. use Test::More tests => 81;
  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. my $config = Slic3r::Config::new_from_defaults;
  39. $config->set('printer_notes', ' PRINTER_VENDOR_PRUSA3D PRINTER_MODEL_MK2 ');
  40. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  41. $parser->apply_config($config);
  42. $parser->set('foo' => 0);
  43. $parser->set('bar' => 2);
  44. $parser->set('num_extruders' => 4);
  45. is $parser->process('[temperature_[foo]]'),
  46. $config->temperature->[0],
  47. "nested config options (legacy syntax)";
  48. is $parser->process('{temperature[foo]}'),
  49. $config->temperature->[0],
  50. "array reference";
  51. is $parser->process("test [ temperature_ [foo] ] \n hu"),
  52. "test " . $config->temperature->[0] . " \n hu",
  53. "whitespaces and newlines are maintained";
  54. is $parser->process('{2*3}'), '6', 'math: 2*3';
  55. is $parser->process('{2*3/6}'), '1', 'math: 2*3/6';
  56. is $parser->process('{2*3/12}'), '0', 'math: 2*3/12';
  57. ok abs($parser->process('{2.*3/12}') - 0.5) < 1e-7, 'math: 2.*3/12';
  58. is $parser->process('{10%2.5}') '0', 'math: 10 % 2.5';
  59. is $parser->process('{11/2.5-1}') '1', 'math: 11 % 2.5';
  60. is $parser->process('{2*(3-12)}'), '-18', 'math: 2*(3-12)';
  61. is $parser->process('{2*foo*(3-12)}'), '0', 'math: 2*foo*(3-12)';
  62. is $parser->process('{2*bar*(3-12)}'), '-36', 'math: 2*bar*(3-12)';
  63. ok abs($parser->process('{2.5*bar*(3-12)}') - -45) < 1e-7, 'math: 2.5*bar*(3-12)';
  64. is $parser->process('{min(12, 14)}'), '12', 'math: min(12, 14)';
  65. is $parser->process('{max(12, 14)}'), '14', 'math: max(12, 14)';
  66. is $parser->process('{min(13.4, -1238.1)}'), '-1238.1', 'math: min(13.4, -1238.1)';
  67. is $parser->process('{max(13.4, -1238.1)}'), '13.4', 'math: max(13.4, -1238.1)';
  68. is $parser->process('{int(13.4)}'), '13', 'math: int(13.4)';
  69. # Test the boolean expression parser.
  70. is $parser->evaluate_boolean_expression('12 == 12'), 1, 'boolean expression parser: 12 == 12';
  71. is $parser->evaluate_boolean_expression('12 != 12'), 0, 'boolean expression parser: 12 != 12';
  72. is $parser->evaluate_boolean_expression('"has some PATTERN embedded" =~ /.*PATTERN.*/'), 1, 'boolean expression parser: regex matches';
  73. is $parser->evaluate_boolean_expression('"has some PATTERN embedded" =~ /.*PTRN.*/'), 0, 'boolean expression parser: regex does not match';
  74. is $parser->evaluate_boolean_expression('foo + 2 == bar'), 1, 'boolean expression parser: accessing variables, equal';
  75. is $parser->evaluate_boolean_expression('foo + 3 == bar'), 0, 'boolean expression parser: accessing variables, not equal';
  76. is $parser->evaluate_boolean_expression('(12 == 12) and (13 != 14)'), 1, 'boolean expression parser: (12 == 12) and (13 != 14)';
  77. is $parser->evaluate_boolean_expression('(12 == 12) && (13 != 14)'), 1, 'boolean expression parser: (12 == 12) && (13 != 14)';
  78. is $parser->evaluate_boolean_expression('(12 == 12) or (13 == 14)'), 1, 'boolean expression parser: (12 == 12) or (13 == 14)';
  79. is $parser->evaluate_boolean_expression('(12 == 12) || (13 == 14)'), 1, 'boolean expression parser: (12 == 12) || (13 == 14)';
  80. is $parser->evaluate_boolean_expression('(12 == 12) and not (13 == 14)'), 1, 'boolean expression parser: (12 == 12) and not (13 == 14)';
  81. is $parser->evaluate_boolean_expression('(12 == 12) ? (1 - 1 == 0) : (2 * 2 == 3)'), 1, 'boolean expression parser: ternary true';
  82. is $parser->evaluate_boolean_expression('(12 == 21/2) ? (1 - 1 == 0) : (2 * 2 == 3)'), 0, 'boolean expression parser: ternary false';
  83. is $parser->evaluate_boolean_expression('(12 == 13) ? (1 - 1 == 3) : (2 * 2 == 4)'), 1, 'boolean expression parser: ternary false';
  84. is $parser->evaluate_boolean_expression('(12 == 2 * 6) ? (1 - 1 == 3) : (2 * 2 == 4)'), 0, 'boolean expression parser: ternary true';
  85. is $parser->evaluate_boolean_expression('12 < 3'), 0, 'boolean expression parser: lower than - false';
  86. is $parser->evaluate_boolean_expression('12 < 22'), 1, 'boolean expression parser: lower than - true';
  87. is $parser->evaluate_boolean_expression('12 > 3'), 1, 'boolean expression parser: greater than - true';
  88. is $parser->evaluate_boolean_expression('12 > 22'), 0, 'boolean expression parser: greater than - false';
  89. is $parser->evaluate_boolean_expression('12 <= 3'), 0, 'boolean expression parser: lower than or equal- false';
  90. is $parser->evaluate_boolean_expression('12 <= 22'), 1, 'boolean expression parser: lower than or equal - true';
  91. is $parser->evaluate_boolean_expression('12 >= 3'), 1, 'boolean expression parser: greater than or equal - true';
  92. is $parser->evaluate_boolean_expression('12 >= 22'), 0, 'boolean expression parser: greater than or equal - false';
  93. is $parser->evaluate_boolean_expression('12 <= 12'), 1, 'boolean expression parser: lower than or equal (same values) - true';
  94. is $parser->evaluate_boolean_expression('12 >= 12'), 1, 'boolean expression parser: greater than or equal (same values) - true';
  95. is $parser->evaluate_boolean_expression('printer_notes=~/.*PRINTER_VENDOR_PRUSA3D.*/ and printer_notes=~/.*PRINTER_MODEL_MK2.*/ and nozzle_diameter[0]==0.6 and num_extruders>1'), 1, 'complex expression';
  96. is $parser->evaluate_boolean_expression('printer_notes=~/.*PRINTER_VEwerfNDOR_PRUSA3D.*/ or printer_notes=~/.*PRINTertER_MODEL_MK2.*/ or (nozzle_diameter[0]==0.6 and num_extruders>1)'), 1, 'complex expression2';
  97. is $parser->evaluate_boolean_expression('printer_notes=~/.*PRINTER_VEwerfNDOR_PRUSA3D.*/ or printer_notes=~/.*PRINTertER_MODEL_MK2.*/ or (nozzle_diameter[0]==0.3 and num_extruders>1)'), 0, 'complex expression3';
  98. }
  99. {
  100. my $config = Slic3r::Config::new_from_defaults;
  101. $config->set('output_filename_format', 'ts_[travel_speed]_lh_[layer_height].gcode');
  102. $config->set('start_gcode', "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n");
  103. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  104. my $output_file = $print->print->output_filepath;
  105. my ($t, $h) = map $config->$_, qw(travel_speed layer_height);
  106. ok $output_file =~ /ts_${t}_/, 'print config options are replaced in output filename';
  107. ok $output_file =~ /lh_$h\./, 'region config options are replaced in output filename';
  108. my $gcode = Slic3r::Test::gcode($print);
  109. ok $gcode =~ /TRAVEL:$t/, 'print config options are replaced in custom G-code';
  110. ok $gcode =~ /HEIGHT:$h/, 'region config options are replaced in custom G-code';
  111. }
  112. {
  113. my $config = Slic3r::Config->new;
  114. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  115. $config->set('extruder', 2);
  116. $config->set('first_layer_temperature', [200,205]);
  117. {
  118. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  119. my $gcode = Slic3r::Test::gcode($print);
  120. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for non-zero yet single extruder';
  121. ok $gcode !~ /M104 S\d+ T0/, 'unused extruder correctly ignored';
  122. }
  123. $config->set('infill_extruder', 1);
  124. {
  125. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  126. my $gcode = Slic3r::Test::gcode($print);
  127. ok $gcode =~ /M104 S200 T0/, 'temperature set correctly for first extruder';
  128. ok $gcode =~ /M104 S205 T1/, 'temperature set correctly for second extruder';
  129. }
  130. my @start_gcode = (qq!
  131. ;__temp0:[first_layer_temperature_0]__
  132. ;__temp1:[first_layer_temperature_1]__
  133. ;__temp2:[first_layer_temperature_2]__
  134. !, qq!
  135. ;__temp0:{first_layer_temperature[0]}__
  136. ;__temp1:{first_layer_temperature[1]}__
  137. ;__temp2:{first_layer_temperature[2]}__
  138. !);
  139. my @syntax_description = (' (legacy syntax)', ' (new syntax)');
  140. for my $i (0, 1) {
  141. $config->set('start_gcode', $start_gcode[$i]);
  142. {
  143. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  144. my $gcode = Slic3r::Test::gcode($print);
  145. # we use the [infill_extruder] placeholder to make sure this test doesn't
  146. # catch a false positive caused by the unparsed start G-code option itself
  147. # being embedded in the G-code
  148. ok $gcode =~ /temp0:200/, 'temperature placeholder for first extruder correctly populated' . $syntax_description[$i];
  149. ok $gcode =~ /temp1:205/, 'temperature placeholder for second extruder correctly populated' . $syntax_description[$i];
  150. ok $gcode =~ /temp2:200/, 'temperature placeholder for unused extruder populated with first value' . $syntax_description[$i];
  151. }
  152. }
  153. $config->set('start_gcode', qq!
  154. ;substitution:{if infill_extruder==1}extruder1
  155. {elsif infill_extruder==2}extruder2
  156. {else}extruder3{endif}
  157. !);
  158. {
  159. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  160. my $gcode = Slic3r::Test::gcode($print);
  161. ok $gcode =~ /substitution:extruder1/, 'if / else / endif - first block returned';
  162. }
  163. }
  164. {
  165. my $config = Slic3r::Config::new_from_defaults;
  166. $config->set('before_layer_gcode', ';BEFORE [layer_num]');
  167. $config->set('layer_gcode', ';CHANGE [layer_num]');
  168. $config->set('support_material', 1);
  169. $config->set('layer_height', 0.2);
  170. my $print = Slic3r::Test::init_print('overhang', config => $config);
  171. my $gcode = Slic3r::Test::gcode($print);
  172. my @before = ();
  173. my @change = ();
  174. foreach my $line (split /\R+/, $gcode) {
  175. if ($line =~ /;BEFORE (\d+)/) {
  176. push @before, $1;
  177. } elsif ($line =~ /;CHANGE (\d+)/) {
  178. push @change, $1;
  179. fail 'inconsistent layer_num before and after layer change'
  180. if $1 != $before[-1];
  181. }
  182. }
  183. is_deeply \@before, \@change, 'layer_num is consistent before and after layer changes';
  184. ok !defined(first { $change[$_] != $change[$_-1]+1 } 1..$#change),
  185. 'layer_num grows continously'; # i.e. no duplicates or regressions
  186. }
  187. {
  188. my $config = Slic3r::Config->new;
  189. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6,0.6]);
  190. $config->set('start_gcode', qq!
  191. ;substitution:{if infill_extruder==1}if block
  192. {elsif infill_extruder==2}elsif block 1
  193. {elsif infill_extruder==3}elsif block 2
  194. {elsif infill_extruder==4}elsif block 3
  195. {else}endif block{endif}
  196. !);
  197. my @returned = ('', 'if block', 'elsif block 1', 'elsif block 2', 'elsif block 3', 'endif block');
  198. for my $i (1,2,3,4,5) {
  199. $config->set('infill_extruder', $i);
  200. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  201. my $gcode = Slic3r::Test::gcode($print);
  202. my $found_other = 0;
  203. for my $j (1,2,3,4,5) {
  204. next if $i == $j;
  205. $found_other = 1 if $gcode =~ /substitution:$returned[$j]/;
  206. }
  207. ok $gcode =~ /substitution:$returned[$i]/, 'if / else / endif - ' . $returned[$i] . ' returned';
  208. ok !$found_other, 'if / else / endif - only ' . $returned[$i] . ' returned';
  209. }
  210. }
  211. {
  212. my $config = Slic3r::Config->new;
  213. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  214. $config->set('start_gcode',
  215. ';substitution:{if infill_extruder==1}{if perimeter_extruder==1}block11{else}block12{endif}' .
  216. '{elsif infill_extruder==2}{if perimeter_extruder==1}block21{else}block22{endif}' .
  217. '{else}{if perimeter_extruder==1}block31{else}block32{endif}{endif}:end');
  218. for my $i (1,2,3) {
  219. $config->set('infill_extruder', $i);
  220. for my $j (1,2) {
  221. $config->set('perimeter_extruder', $j);
  222. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  223. my $gcode = Slic3r::Test::gcode($print);
  224. ok $gcode =~ /substitution:block$i$j:end/, "two level if / else / endif - block$i$j returned";
  225. }
  226. }
  227. }
  228. {
  229. my $config = Slic3r::Config->new;
  230. $config->set('start_gcode',
  231. ';substitution:{if notes=="MK2"}MK2{elsif notes=="MK3"}MK3{else}MK1{endif}:end');
  232. for my $printer_name ("MK2", "MK3", "MK1") {
  233. $config->set('notes', $printer_name);
  234. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  235. my $gcode = Slic3r::Test::gcode($print);
  236. ok $gcode =~ /substitution:$printer_name:end/, "printer name $printer_name matched";
  237. }
  238. }
  239. {
  240. my $config = Slic3r::Config::new_from_defaults;
  241. $config->set('complete_objects', 1);
  242. $config->set('between_objects_gcode', '_MY_CUSTOM_GCODE_');
  243. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 3);
  244. my $gcode = Slic3r::Test::gcode($print);
  245. is scalar(() = $gcode =~ /^_MY_CUSTOM_GCODE_/gm), 2, 'between_objects_gcode is applied correctly';
  246. }
  247. __END__