shells.t 14 KB

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