multi.t 8.0 KB

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