multi.t 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. use Test::More tests => 13;
  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::Geometry qw(scale convex_hull);
  12. use Slic3r::Geometry::Clipper qw(offset);
  13. use Slic3r::Test;
  14. {
  15. my $config = Slic3r::Config::new_from_defaults;
  16. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  17. $config->set('raft_layers', 2);
  18. $config->set('infill_extruder', 2);
  19. $config->set('solid_infill_extruder', 3);
  20. $config->set('support_material_extruder', 4);
  21. $config->set('ooze_prevention', 1);
  22. $config->set('extruder_offset', [ [0,0], [20,0], [0,20], [20,20] ]);
  23. $config->set('temperature', [200, 180, 170, 160]);
  24. $config->set('first_layer_temperature', [206, 186, 166, 156]);
  25. $config->set('toolchange_gcode', 'T[next_extruder] ;toolchange'); # test that it doesn't crash when this is supplied
  26. # Since July 2019, PrusaSlicer only emits automatic Tn command in case that the toolchange_gcode is empty
  27. # The "T[next_extruder]" is therefore needed in this test.
  28. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  29. my $tool = undef;
  30. my @tool_temp = (0,0,0,0);
  31. my @toolchange_points = ();
  32. my @extrusion_points = ();
  33. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  34. my ($self, $cmd, $args, $info) = @_;
  35. if ($cmd =~ /^T(\d+)/) {
  36. # ignore initial toolchange
  37. if (defined $tool) {
  38. my $expected_temp = $self->Z == ($config->get_value('first_layer_height') + $config->z_offset)
  39. ? $config->first_layer_temperature->[$tool]
  40. : $config->temperature->[$tool];
  41. die 'standby temperature was not set before toolchange'
  42. if $tool_temp[$tool] != $expected_temp + $config->standby_temperature_delta;
  43. push @toolchange_points, my $point = Slic3r::Point->new_scale($self->X, $self->Y);
  44. }
  45. $tool = $1;
  46. } elsif ($cmd eq 'M104' || $cmd eq 'M109') {
  47. my $t = $args->{T} // $tool;
  48. if ($tool_temp[$t] == 0) {
  49. fail 'initial temperature is not equal to first layer temperature + standby delta'
  50. unless $args->{S} == $config->first_layer_temperature->[$t] + $config->standby_temperature_delta;
  51. }
  52. $tool_temp[$t] = $args->{S};
  53. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  54. push @extrusion_points, my $point = Slic3r::Point->new_scale($args->{X}, $args->{Y});
  55. $point->translate(map +scale($_), @{ $config->extruder_offset->[$tool] });
  56. }
  57. });
  58. my $convex_hull = convex_hull(\@extrusion_points);
  59. my @t = ();
  60. foreach my $point (@toolchange_points) {
  61. foreach my $offset (@{$config->extruder_offset}) {
  62. push @t, my $p = $point->clone;
  63. $p->translate(map +scale($_), @$offset);
  64. }
  65. }
  66. ok !(defined first { $convex_hull->contains_point($_) } @t), 'all nozzles are outside skirt at toolchange';
  67. if (0) {
  68. require "Slic3r/SVG.pm";
  69. Slic3r::SVG::output(
  70. "ooze_prevention_test.svg",
  71. no_arrows => 1,
  72. polygons => [$convex_hull],
  73. red_points => \@t,
  74. points => \@toolchange_points,
  75. );
  76. }
  77. # offset the skirt by the maximum displacement between extruders plus a safety extra margin
  78. my $delta = scale(20 * sqrt(2) + 1);
  79. my $outer_convex_hull = offset([$convex_hull], +$delta)->[0];
  80. ok !(defined first { !$outer_convex_hull->contains_point($_) } @toolchange_points), 'all toolchanges happen within expected area';
  81. }
  82. {
  83. my $config = Slic3r::Config::new_from_defaults;
  84. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  85. $config->set('support_material_extruder', 3);
  86. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  87. ok Slic3r::Test::gcode($print), 'no errors when using non-consecutive extruders';
  88. }
  89. {
  90. my $config = Slic3r::Config->new;
  91. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  92. $config->set('extruder', 2);
  93. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  94. like Slic3r::Test::gcode($print), qr/ T1/, 'extruder shortcut';
  95. }
  96. {
  97. my $config = Slic3r::Config->new;
  98. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  99. $config->set('perimeter_extruder', 2);
  100. $config->set('infill_extruder', 2);
  101. $config->set('support_material_extruder', 2);
  102. $config->set('support_material_interface_extruder', 2);
  103. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  104. ok Slic3r::Test::gcode($print), 'no errors when using multiple skirts with a single, non-zero, extruder';
  105. }
  106. {
  107. my $model = stacked_cubes();
  108. my $lower_config = $model->get_material('lower')->config;
  109. my $upper_config = $model->get_material('upper')->config;
  110. $lower_config->set('extruder', 1);
  111. $lower_config->set('bottom_solid_layers', 0);
  112. $lower_config->set('top_solid_layers', 1);
  113. $upper_config->set('extruder', 2);
  114. $upper_config->set('bottom_solid_layers', 1);
  115. $upper_config->set('top_solid_layers', 0);
  116. my $config = Slic3r::Config::new_from_defaults;
  117. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  118. $config->set('fill_density', 0);
  119. $config->set('solid_infill_speed', 99);
  120. $config->set('top_solid_infill_speed', 99);
  121. $config->set('cooling', [ 0 ]); # for preventing speeds from being altered
  122. $config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
  123. my $test = sub {
  124. my $print = Slic3r::Test::init_print($model, config => $config);
  125. my $tool = undef;
  126. my %T0_shells = my %T1_shells = (); # Z => 1
  127. Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
  128. my ($self, $cmd, $args, $info) = @_;
  129. if ($cmd =~ /^T(\d+)/) {
  130. $tool = $1;
  131. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  132. if (($args->{F} // $self->F) == $config->solid_infill_speed*60) {
  133. if ($tool == 0) {
  134. $T0_shells{$self->Z} = 1;
  135. } elsif ($tool == 1) {
  136. $T1_shells{$self->Z} = 1;
  137. }
  138. }
  139. }
  140. });
  141. return [ sort keys %T0_shells ], [ sort keys %T1_shells ];
  142. };
  143. {
  144. my ($t0, $t1) = $test->();
  145. is scalar(@$t0), 0, 'no interface shells';
  146. is scalar(@$t1), 0, 'no interface shells';
  147. }
  148. {
  149. $config->set('interface_shells', 1);
  150. my ($t0, $t1) = $test->();
  151. is scalar(@$t0), $lower_config->top_solid_layers, 'top interface shells';
  152. is scalar(@$t1), $upper_config->bottom_solid_layers, 'bottom interface shells';
  153. }
  154. }
  155. {
  156. my $model = stacked_cubes();
  157. my $object = $model->objects->[0];
  158. my $config = Slic3r::Config::new_from_defaults;
  159. $config->set('nozzle_diameter', [0.6,0.6,0.6,0.6]);
  160. $config->set('layer_height', 0.4);
  161. $config->set('first_layer_height', $config->layer_height);
  162. $config->set('skirts', 0);
  163. my $print = Slic3r::Test::init_print($model, config => $config);
  164. is $object->volumes->[0]->config->extruder, 1, 'auto_assign_extruders() assigned correct extruder to first volume';
  165. is $object->volumes->[1]->config->extruder, 2, 'auto_assign_extruders() assigned correct extruder to second volume';
  166. my $tool = undef;
  167. my %T0 = my %T1 = (); # Z => 1
  168. Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
  169. my ($self, $cmd, $args, $info) = @_;
  170. if ($cmd =~ /^T(\d+)/) {
  171. $tool = $1;
  172. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  173. if ($tool == 0) {
  174. $T0{$self->Z} = 1;
  175. } elsif ($tool == 1) {
  176. $T1{$self->Z} = 1;
  177. }
  178. }
  179. });
  180. ok !(defined first { $_ > 20 } keys %T0), 'T0 is never used for upper object';
  181. ok !(defined first { $_ < 20 } keys %T1), 'T1 is never used for lower object';
  182. }
  183. sub stacked_cubes {
  184. my $model = Slic3r::Model->new;
  185. my $object = $model->add_object;
  186. $object->add_volume(mesh => Slic3r::Test::mesh('20mm_cube'), material_id => 'lower');
  187. $object->add_volume(mesh => Slic3r::Test::mesh('20mm_cube', translate => [0,0,20]), material_id => 'upper');
  188. $object->add_instance(offset => Slic3r::Pointf->new(0,0));
  189. return $model;
  190. }
  191. __END__