shells.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. use Test::More tests => 21;
  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 sum);
  10. use Slic3r;
  11. use Slic3r::Geometry qw(epsilon);
  12. use Slic3r::Test;
  13. {
  14. my $config = Slic3r::Config->new_from_defaults;
  15. $config->set('skirts', 0);
  16. $config->set('perimeters', 0);
  17. $config->set('solid_infill_speed', 99);
  18. $config->set('top_solid_infill_speed', 99);
  19. $config->set('bridge_speed', 72);
  20. $config->set('first_layer_speed', '100%');
  21. $config->set('cooling', [ 0 ]);
  22. my $test = sub {
  23. my ($conf) = @_;
  24. $conf ||= $config;
  25. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  26. my %z = (); # Z => 1
  27. my %layers_with_solid_infill = (); # Z => $count
  28. my %layers_with_bridge_infill = (); # Z => $count
  29. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  30. my ($self, $cmd, $args, $info) = @_;
  31. if ($self->Z > 0) {
  32. $z{ $self->Z } = 1;
  33. if ($info->{extruding} && $info->{dist_XY} > 0) {
  34. my $F = $args->{F} // $self->F;
  35. $layers_with_solid_infill{$self->Z} = 1
  36. if $F == $config->solid_infill_speed*60;
  37. $layers_with_bridge_infill{$self->Z} = 1
  38. if $F == $config->bridge_speed*60;
  39. }
  40. }
  41. });
  42. my @z = sort { $a <=> $b } keys %z;
  43. my @shells = map $layers_with_solid_infill{$_} || $layers_with_bridge_infill{$_}, @z;
  44. fail "insufficient number of bottom solid layers"
  45. unless !defined(first { !$_ } @shells[0..$config->bottom_solid_layers-1]);
  46. fail "excessive number of bottom solid layers"
  47. unless scalar(grep $_, @shells[0 .. $#shells/2]) == $config->bottom_solid_layers;
  48. fail "insufficient number of top solid layers"
  49. unless !defined(first { !$_ } @shells[-$config->top_solid_layers..-1]);
  50. fail "excessive number of top solid layers"
  51. unless scalar(grep $_, @shells[($#shells/2)..$#shells]) == $config->top_solid_layers;
  52. if ($config->top_solid_layers > 0) {
  53. fail "unexpected solid infill speed in first solid layer over sparse infill"
  54. if $layers_with_solid_infill{ $z[-$config->top_solid_layers] };
  55. die "bridge speed not used in first solid layer over sparse infill"
  56. if !$layers_with_bridge_infill{ $z[-$config->top_solid_layers] };
  57. }
  58. 1;
  59. };
  60. $config->set('top_solid_layers', 3);
  61. $config->set('bottom_solid_layers', 3);
  62. ok $test->(), "proper number of shells is applied";
  63. $config->set('top_solid_layers', 0);
  64. $config->set('bottom_solid_layers', 0);
  65. ok $test->(), "no shells are applied when both top and bottom are set to zero";
  66. $config->set('perimeters', 1);
  67. $config->set('top_solid_layers', 3);
  68. $config->set('bottom_solid_layers', 3);
  69. $config->set('fill_density', 0);
  70. ok $test->(), "proper number of shells is applied even when fill density is none";
  71. }
  72. # issue #1161
  73. {
  74. my $config = Slic3r::Config->new_from_defaults;
  75. $config->set('layer_height', 0.3);
  76. $config->set('first_layer_height', '100%');
  77. $config->set('bottom_solid_layers', 0);
  78. $config->set('top_solid_layers', 3);
  79. $config->set('cooling', [ 0 ]);
  80. $config->set('bridge_speed', 99);
  81. $config->set('solid_infill_speed', 99);
  82. $config->set('top_solid_infill_speed', 99);
  83. $config->set('first_layer_speed', '100%');
  84. my $print = Slic3r::Test::init_print('V', config => $config);
  85. my %layers_with_solid_infill = (); # Z => 1
  86. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  87. my ($self, $cmd, $args, $info) = @_;
  88. $layers_with_solid_infill{$self->Z} = 1
  89. if $info->{extruding} && ($args->{F} // $self->F) == $config->solid_infill_speed*60;
  90. });
  91. is scalar(map $layers_with_solid_infill{$_}, grep $_ <= 7.2, keys %layers_with_solid_infill), 3,
  92. "correct number of top solid shells is generated in V-shaped object";
  93. }
  94. {
  95. my $config = Slic3r::Config->new_from_defaults;
  96. # we need to check against one perimeter because this test is calibrated
  97. # (shape, extrusion_width) so that perimeters cover the bottom surfaces of
  98. # their lower layer - the test checks that shells are not generated on the
  99. # above layers (thus 'across' the shadow perimeter)
  100. # the test is actually calibrated to leave a narrow bottom region for each
  101. # layer - we test that in case of fill_density = 0 such narrow shells are
  102. # discarded instead of grown
  103. $config->set('perimeters', 1);
  104. $config->set('fill_density', 0);
  105. $config->set('cooling', [ 0 ]); # prevent speed alteration
  106. $config->set('first_layer_speed', '100%'); # prevent speed alteration
  107. $config->set('layer_height', 0.4);
  108. $config->set('first_layer_height', '100%');
  109. $config->set('extrusion_width', 0.55);
  110. $config->set('bottom_solid_layers', 3);
  111. $config->set('top_solid_layers', 0);
  112. $config->set('solid_infill_speed', 99);
  113. my $print = Slic3r::Test::init_print('V', config => $config);
  114. my %layers = (); # Z => 1
  115. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  116. my ($self, $cmd, $args, $info) = @_;
  117. $layers{$self->Z} = 1
  118. if $info->{extruding} && ($args->{F} // $self->F) == $config->solid_infill_speed*60;
  119. });
  120. is scalar(keys %layers), $config->bottom_solid_layers,
  121. "shells are not propagated across perimeters of the neighbor layer";
  122. }
  123. {
  124. my $config = Slic3r::Config->new_from_defaults;
  125. $config->set('perimeters', 3);
  126. $config->set('cooling', [ 0 ]); # prevent speed alteration
  127. $config->set('first_layer_speed', '100%'); # prevent speed alteration
  128. $config->set('layer_height', 0.4);
  129. $config->set('first_layer_height', '100%');
  130. $config->set('bottom_solid_layers', 3);
  131. $config->set('top_solid_layers', 3);
  132. $config->set('solid_infill_speed', 99);
  133. $config->set('top_solid_infill_speed', 99);
  134. $config->set('bridge_speed', 99);
  135. my $print = Slic3r::Test::init_print('sloping_hole', config => $config);
  136. my %solid_layers = (); # Z => 1
  137. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  138. my ($self, $cmd, $args, $info) = @_;
  139. $solid_layers{$self->Z} = 1
  140. if $info->{extruding} && ($args->{F} // $self->F) == $config->solid_infill_speed*60;
  141. });
  142. is scalar(keys %solid_layers), $config->bottom_solid_layers + $config->top_solid_layers,
  143. "no superfluous shells are generated";
  144. }
  145. {
  146. my $config = Slic3r::Config->new_from_defaults;
  147. $config->set('perimeters', 1);
  148. $config->set('fill_density', 0);
  149. $config->set('top_solid_layers', 0);
  150. $config->set('spiral_vase', 1);
  151. $config->set('bottom_solid_layers', 0);
  152. $config->set('skirts', 0);
  153. $config->set('first_layer_height', '100%');
  154. $config->set('start_gcode', '');
  155. $config->set('temperature', [200]);
  156. $config->set('first_layer_temperature', [205]);
  157. # TODO: this needs to be tested with a model with sloping edges, where starting
  158. # points of each layer are not aligned - in that case we would test that no
  159. # travel moves are left to move to the new starting point - in a cube, end
  160. # points coincide with next layer starting points (provided there's no clipping)
  161. my $test = sub {
  162. my ($model_name, $description) = @_;
  163. my $print = Slic3r::Test::init_print($model_name, config => $config);
  164. my $travel_moves_after_first_extrusion = 0;
  165. my $started_extruding = 0;
  166. my $first_layer_temperature_set = 0;
  167. my $temperature_set = 0;
  168. my @z_steps = ();
  169. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  170. my ($self, $cmd, $args, $info) = @_;
  171. if ($cmd eq 'G1') {
  172. $started_extruding = 1 if $info->{extruding};
  173. push @z_steps, $info->{dist_Z}
  174. if $started_extruding && $info->{dist_Z} > 0;
  175. $travel_moves_after_first_extrusion++
  176. if $info->{travel} && $info->{dist_XY} > 0 && $started_extruding && !exists $args->{Z};
  177. } elsif ($cmd eq 'M104') {
  178. $first_layer_temperature_set = 1 if $args->{S} == 205;
  179. $temperature_set = 1 if $args->{S} == 200;
  180. }
  181. });
  182. ok $first_layer_temperature_set, 'first layer temperature is preserved';
  183. ok $temperature_set, 'temperature is preserved';
  184. # we allow one travel move after first extrusion: i.e. when moving to the first
  185. # spiral point after moving to second layer (bottom layer had loop clipping, so
  186. # we're slightly distant from the starting point of the loop)
  187. ok $travel_moves_after_first_extrusion <= 1, "no gaps in spiral vase ($description)";
  188. ok !(grep { $_ > $config->layer_height + epsilon } @z_steps), "no gaps in Z ($description)";
  189. };
  190. $test->('20mm_cube', 'solid model');
  191. $config->set('z_offset', -10);
  192. $test->('20mm_cube', 'solid model with negative z-offset');
  193. ### Disabled because the current unreliable medial axis code doesn't
  194. ### always produce valid loops.
  195. ###$test->('40x10', 'hollow model with negative z-offset');
  196. }
  197. {
  198. my $config = Slic3r::Config->new_from_defaults;
  199. $config->set('spiral_vase', 1);
  200. $config->set('perimeters', 1);
  201. $config->set('fill_density', 0);
  202. $config->set('top_solid_layers', 0);
  203. $config->set('bottom_solid_layers', 0);
  204. $config->set('retract_layer_change', [0]);
  205. $config->set('skirts', 0);
  206. $config->set('first_layer_height', '100%');
  207. $config->set('layer_height', 0.4);
  208. $config->set('start_gcode', '');
  209. $config->validate;
  210. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  211. my $z_moves = 0;
  212. my @this_layer = (); # [ dist_Z, dist_XY ], ...
  213. my $bottom_layer_not_flat = 0;
  214. my $null_z_moves_not_layer_changes = 0;
  215. my $null_z_moves_not_multiples_of_layer_height = 0;
  216. my $sum_of_partial_z_equals_to_layer_height = 0;
  217. my $all_layer_segments_have_same_slope = 0;
  218. my $horizontal_extrusions = 0;
  219. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  220. my ($self, $cmd, $args, $info) = @_;
  221. if ($cmd eq 'G1') {
  222. if ($z_moves < 2) {
  223. # skip everything up to the second Z move
  224. # (i.e. start of second layer)
  225. if (exists $args->{Z}) {
  226. $z_moves++;
  227. $bottom_layer_not_flat = 1
  228. if $info->{dist_Z} > 0 && $info->{dist_Z} != $config->layer_height;
  229. }
  230. } elsif ($info->{dist_Z} == 0 && $args->{Z}) {
  231. $null_z_moves_not_layer_changes = 1
  232. if $info->{dist_XY} != 0;
  233. # % doesn't work easily with floats
  234. $null_z_moves_not_multiples_of_layer_height = 1
  235. if abs(($args->{Z} / $config->layer_height) * $config->layer_height - $args->{Z}) > epsilon;
  236. my $total_dist_XY = sum(map $_->[1], @this_layer);
  237. $sum_of_partial_z_equals_to_layer_height = 1
  238. if abs(sum(map $_->[0], @this_layer) - $config->layer_height) > epsilon;
  239. foreach my $segment (@this_layer) {
  240. # check that segment's dist_Z is proportioned to its dist_XY
  241. $all_layer_segments_have_same_slope = 1
  242. if abs($segment->[0]*$total_dist_XY/$config->layer_height - $segment->[1]) > 0.2;
  243. }
  244. @this_layer = ();
  245. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  246. $horizontal_extrusions = 1
  247. if $info->{dist_Z} == 0;
  248. push @this_layer, [ $info->{dist_Z}, $info->{dist_XY} ];
  249. }
  250. }
  251. });
  252. ok !$bottom_layer_not_flat, 'bottom layer is flat when using spiral vase';
  253. ok !$null_z_moves_not_layer_changes, 'null Z moves are layer changes';
  254. ok !$null_z_moves_not_multiples_of_layer_height, 'null Z moves are multiples of layer height';
  255. ok !$sum_of_partial_z_equals_to_layer_height, 'sum of partial Z increments equals to a full layer height';
  256. ok !$all_layer_segments_have_same_slope, 'all layer segments have the same slope';
  257. ok !$horizontal_extrusions, 'no horizontal extrusions';
  258. }
  259. {
  260. my $config = Slic3r::Config->new_from_defaults;
  261. $config->set('perimeters', 1);
  262. $config->set('fill_density', 0);
  263. $config->set('top_solid_layers', 0);
  264. $config->set('spiral_vase', 1);
  265. $config->set('bottom_solid_layers', 0);
  266. $config->set('skirts', 0);
  267. $config->set('first_layer_height', '100%');
  268. $config->set('start_gcode', '');
  269. my $print = Slic3r::Test::init_print('two_hollow_squares', config => $config);
  270. my $diagonal_moves = 0;
  271. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  272. my ($self, $cmd, $args, $info) = @_;
  273. if ($cmd eq 'G1') {
  274. if ($info->{extruding} && $info->{dist_XY} > 0) {
  275. if ($info->{dist_Z} > 0) {
  276. $diagonal_moves++;
  277. }
  278. }
  279. }
  280. });
  281. is $diagonal_moves, 0, 'no spiral moves on two-island object';
  282. }
  283. __END__