shells.t 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. use Test::More tests => 20;
  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', $config->layer_height);
  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', $config->layer_height);
  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', $config->layer_height);
  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. $config->set('filament_diameter', [ 3.0 ]);
  136. $config->set('nozzle_diameter', [ 0.5 ]);
  137. my $print = Slic3r::Test::init_print('sloping_hole', config => $config);
  138. my %solid_layers = (); # Z => 1
  139. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  140. my ($self, $cmd, $args, $info) = @_;
  141. $solid_layers{$self->Z} = 1
  142. if $info->{extruding} && ($args->{F} // $self->F) == $config->solid_infill_speed*60;
  143. });
  144. is scalar(keys %solid_layers), $config->bottom_solid_layers + $config->top_solid_layers,
  145. "no superfluous shells are generated";
  146. }
  147. {
  148. my $config = Slic3r::Config::new_from_defaults;
  149. $config->set('perimeters', 1);
  150. $config->set('fill_density', 0);
  151. $config->set('top_solid_layers', 0);
  152. $config->set('spiral_vase', 1);
  153. $config->set('bottom_solid_layers', 0);
  154. $config->set('skirts', 0);
  155. $config->set('first_layer_height', $config->layer_height);
  156. $config->set('start_gcode', '');
  157. $config->set('temperature', [200]);
  158. $config->set('first_layer_temperature', [205]);
  159. # TODO: this needs to be tested with a model with sloping edges, where starting
  160. # points of each layer are not aligned - in that case we would test that no
  161. # travel moves are left to move to the new starting point - in a cube, end
  162. # points coincide with next layer starting points (provided there's no clipping)
  163. my $test = sub {
  164. my ($model_name, $description) = @_;
  165. my $print = Slic3r::Test::init_print($model_name, config => $config);
  166. my $travel_moves_after_first_extrusion = 0;
  167. my $started_extruding = 0;
  168. my $first_layer_temperature_set = 0;
  169. my $temperature_set = 0;
  170. my @z_steps = ();
  171. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  172. my ($self, $cmd, $args, $info) = @_;
  173. if ($cmd eq 'G1') {
  174. $started_extruding = 1 if $info->{extruding};
  175. push @z_steps, $info->{dist_Z}
  176. if $started_extruding && $info->{dist_Z} > 0;
  177. $travel_moves_after_first_extrusion++
  178. if $info->{travel} && $info->{dist_XY} > 0 && $started_extruding && !exists $args->{Z};
  179. } elsif ($cmd eq 'M104') {
  180. $first_layer_temperature_set = 1 if $args->{S} == 205;
  181. $temperature_set = 1 if $args->{S} == 200;
  182. }
  183. });
  184. ok $first_layer_temperature_set, 'first layer temperature is preserved';
  185. ok $temperature_set, 'temperature is preserved';
  186. # we allow one travel move after first extrusion: i.e. when moving to the first
  187. # spiral point after moving to second layer (bottom layer had loop clipping, so
  188. # we're slightly distant from the starting point of the loop)
  189. ok $travel_moves_after_first_extrusion <= 1, "no gaps in spiral vase ($description)";
  190. ok !(grep { $_ > $config->layer_height + epsilon } @z_steps), "no gaps in Z ($description)";
  191. };
  192. $test->('20mm_cube', 'solid model');
  193. $config->set('z_offset', -10);
  194. $test->('20mm_cube', 'solid model with negative z-offset');
  195. ### Disabled because the current unreliable medial axis code doesn't
  196. ### always produce valid loops.
  197. ###$test->('40x10', 'hollow model with negative z-offset');
  198. }
  199. {
  200. my $config = Slic3r::Config::new_from_defaults;
  201. $config->set('spiral_vase', 1);
  202. $config->set('perimeters', 1);
  203. $config->set('fill_density', 0);
  204. $config->set('top_solid_layers', 0);
  205. $config->set('bottom_solid_layers', 0);
  206. $config->set('retract_layer_change', [0]);
  207. $config->set('skirts', 0);
  208. $config->set('layer_height', 0.4);
  209. $config->set('first_layer_height', $config->layer_height);
  210. $config->set('start_gcode', '');
  211. # $config->set('use_relative_e_distances', 1);
  212. $config->validate;
  213. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  214. my $z_moves = 0;
  215. my @this_layer = (); # [ dist_Z, dist_XY ], ...
  216. my $bottom_layer_not_flat = 0;
  217. my $null_z_moves_not_layer_changes = 0;
  218. my $null_z_moves_not_multiples_of_layer_height = 0;
  219. my $sum_of_partial_z_equals_to_layer_height = 0;
  220. my $all_layer_segments_have_same_slope = 0;
  221. my $horizontal_extrusions = 0;
  222. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  223. my ($self, $cmd, $args, $info) = @_;
  224. if ($cmd eq 'G1') {
  225. if ($z_moves < 2) {
  226. # skip everything up to the second Z move
  227. # (i.e. start of second layer)
  228. if (exists $args->{Z}) {
  229. $z_moves++;
  230. $bottom_layer_not_flat = 1
  231. if $info->{dist_Z} > 0 && $info->{dist_Z} != $config->layer_height;
  232. }
  233. } elsif ($info->{dist_Z} == 0 && $args->{Z}) {
  234. $null_z_moves_not_layer_changes = 1
  235. if $info->{dist_XY} != 0;
  236. # % doesn't work easily with floats
  237. $null_z_moves_not_multiples_of_layer_height = 1
  238. if abs(($args->{Z} / $config->layer_height) * $config->layer_height - $args->{Z}) > epsilon;
  239. my $total_dist_XY = sum(map $_->[1], @this_layer);
  240. $sum_of_partial_z_equals_to_layer_height = 1
  241. if abs(sum(map $_->[0], @this_layer) - $config->layer_height) >
  242. # The first segment on the 2nd layer has extrusion interpolated from zero
  243. # and the 1st segment has such a low extrusion assigned, that it is effectively zero, thus the move
  244. # is considered non-extruding and a higher epsilon is required.
  245. ($z_moves == 2 ? 0.0021 : epsilon);
  246. #printf ("Total height: %f, layer height: %f, good: %d\n", sum(map $_->[0], @this_layer), $config->layer_height, $sum_of_partial_z_equals_to_layer_height);
  247. foreach my $segment (@this_layer) {
  248. # check that segment's dist_Z is proportioned to its dist_XY
  249. $all_layer_segments_have_same_slope = 1
  250. if abs($segment->[0]*$total_dist_XY/$config->layer_height - $segment->[1]) > 0.2;
  251. }
  252. @this_layer = ();
  253. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  254. $horizontal_extrusions = 1
  255. if $info->{dist_Z} == 0;
  256. #printf("Pushing dist_z: %f, dist_xy: %f\n", $info->{dist_Z}, $info->{dist_XY});
  257. push @this_layer, [ $info->{dist_Z}, $info->{dist_XY} ];
  258. }
  259. }
  260. });
  261. ok !$bottom_layer_not_flat, 'bottom layer is flat when using spiral vase';
  262. ok !$null_z_moves_not_layer_changes, 'null Z moves are layer changes';
  263. ok !$null_z_moves_not_multiples_of_layer_height, 'null Z moves are multiples of layer height';
  264. ok !$sum_of_partial_z_equals_to_layer_height, 'sum of partial Z increments equals to a full layer height';
  265. ok !$all_layer_segments_have_same_slope, 'all layer segments have the same slope';
  266. ok !$horizontal_extrusions, 'no horizontal extrusions';
  267. }
  268. # The current Spiral Vase slicing code removes the holes and all but the largest contours from each slice,
  269. # therefore the following test is no more valid.
  270. #{
  271. # my $config = Slic3r::Config::new_from_defaults;
  272. # $config->set('perimeters', 1);
  273. # $config->set('fill_density', 0);
  274. # $config->set('top_solid_layers', 0);
  275. # $config->set('spiral_vase', 1);
  276. # $config->set('bottom_solid_layers', 0);
  277. # $config->set('skirts', 0);
  278. # $config->set('first_layer_height', $config->layer_height);
  279. # $config->set('start_gcode', '');
  280. #
  281. # my $print = Slic3r::Test::init_print('two_hollow_squares', config => $config);
  282. # my $diagonal_moves = 0;
  283. # Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  284. # my ($self, $cmd, $args, $info) = @_;
  285. #
  286. # if ($cmd eq 'G1') {
  287. # if ($info->{extruding} && $info->{dist_XY} > 0) {
  288. # if ($info->{dist_Z} > 0) {
  289. # $diagonal_moves++;
  290. # }
  291. # }
  292. # }
  293. # });
  294. # is $diagonal_moves, 0, 'no spiral moves on two-island object';
  295. #}
  296. __END__