shells.t 13 KB

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