SectionCut.pm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # 2D cut in the XZ plane through the toolpaths.
  2. # For debugging purposes.
  3. package Slic3r::Test::SectionCut;
  4. use Moo;
  5. use List::Util qw(any min max);
  6. use Slic3r::Geometry qw(unscale);
  7. use Slic3r::Geometry::Clipper qw(intersection_pl);
  8. use SVG;
  9. use Slic3r::SVG;
  10. has 'print' => (is => 'ro', required => 1);
  11. has 'scale' => (is => 'ro', default => sub { 30 });
  12. has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor
  13. has '_line' => (is => 'lazy');
  14. has '_height' => (is => 'rw');
  15. has '_svg' => (is => 'rw');
  16. has '_svg_style' => (is => 'rw', default => sub { {} });
  17. has '_bb' => (is => 'lazy');
  18. sub _build__line {
  19. my $self = shift;
  20. # calculate the Y coordinate of the section line
  21. my $bb = $self->_bb;
  22. my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
  23. # store our section line
  24. return Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]);
  25. }
  26. sub _build__bb {
  27. my ($self) = @_;
  28. return $self->print->bounding_box;
  29. }
  30. sub export_svg {
  31. my $self = shift;
  32. my ($filename) = @_;
  33. # get bounding box of print and its height
  34. # (Print should return a BoundingBox3 object instead)
  35. my $print_size = $self->_bb->size;
  36. $self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
  37. # initialize the SVG canvas
  38. $self->_svg(my $svg = SVG->new(
  39. width => $self->scale * unscale($print_size->x),
  40. height => $self->scale * $self->_height,
  41. ));
  42. # set default styles
  43. $self->_svg_style->{'stroke-width'} = 1;
  44. $self->_svg_style->{'fill-opacity'} = 0.5;
  45. $self->_svg_style->{'stroke-opacity'} = 0.2;
  46. # plot perimeters
  47. $self->_svg_style->{'stroke'} = '#EE0000';
  48. $self->_svg_style->{'fill'} = '#FF0000';
  49. $self->_plot_group(sub { map @{$_->perimeters}, @{$_[0]->regions} });
  50. # plot infill
  51. $self->_svg_style->{'stroke'} = '#444444';
  52. $self->_svg_style->{'fill'} = '#454545';
  53. $self->_plot_group(sub { map @{$_->fills}, @{$_[0]->regions} });
  54. # plot support material
  55. $self->_svg_style->{'stroke'} = '#12EF00';
  56. $self->_svg_style->{'fill'} = '#22FF00';
  57. $self->_plot_group(sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills, $_[0]->support_interface_fills) : () });
  58. Slic3r::open(\my $fh, '>', $filename);
  59. print $fh $svg->xmlify;
  60. close $fh;
  61. printf "Section cut SVG written to %s\n", $filename;
  62. }
  63. sub _plot_group {
  64. my $self = shift;
  65. my ($filter) = @_;
  66. foreach my $object (@{$self->print->objects}) {
  67. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  68. my @paths = map $_->clone, map @{$_->flatten}, grep defined $_, $filter->($layer);
  69. my $name = sprintf "%s %d (z = %f)",
  70. ($layer->isa('Slic3r::Layer::Support') ? 'Support Layer' : 'Layer'),
  71. $layer->id,
  72. $layer->print_z;
  73. my $g = $self->_svg->getElementByID($name)
  74. || $self->_svg->group(id => $name, style => { %{$self->_svg_style} });
  75. foreach my $copy (@{$object->_shifted_copies}) {
  76. if (0) {
  77. # export plan with section line and exit
  78. my @grown = map @{$_->grow}, @paths;
  79. $_->translate(@$copy) for @paths;
  80. require "Slic3r/SVG.pm";
  81. Slic3r::SVG::output(
  82. "line.svg",
  83. no_arrows => 1,
  84. polygons => \@grown,
  85. red_lines => [ $self->_line ],
  86. );
  87. exit;
  88. }
  89. $self->_plot_path($_, $g, $copy, $layer) for @paths;
  90. }
  91. }
  92. }
  93. }
  94. sub _plot_path {
  95. my ($self, $path, $g, $copy, $layer) = @_;
  96. my $grown = $path->grow;
  97. $_->translate(@$copy) for @$grown;
  98. my $intersections = intersection_pl(
  99. [ $self->_line->as_polyline ],
  100. $grown,
  101. );
  102. if (0 && @$intersections) {
  103. # export plan with section line and exit
  104. require "Slic3r/SVG.pm";
  105. Slic3r::SVG::output(
  106. "intersections.svg",
  107. no_arrows => 1,
  108. polygons => $grown,
  109. red_lines => [ $self->_line ],
  110. );
  111. exit;
  112. }
  113. # turn intersections to lines
  114. die "Intersection has more than two points!\n"
  115. if any { @$_ > 2 } @$intersections;
  116. my @lines = map Slic3r::Line->new(@$_), @$intersections;
  117. my $is_bridge = $path->isa('Slic3r::ExtrusionPath')
  118. ? $path->is_bridge
  119. : any { $_->is_bridge } @$path;
  120. foreach my $line (@lines) {
  121. my $this_path = $path;
  122. if ($path->isa('Slic3r::ExtrusionLoop')) {
  123. # FIXME: find the actual ExtrusionPath of this intersection
  124. $this_path = $path->[0];
  125. }
  126. # align to canvas
  127. $line->translate(-$self->_bb->x_min, 0);
  128. # we want lines oriented from left to right in order to draw rectangles correctly
  129. $line->reverse if $line->a->x > $line->b->x;
  130. if ($is_bridge) {
  131. my $radius = $this_path->width / 2;
  132. my $width = unscale abs($line->b->x - $line->a->x);
  133. if ((10 * $radius) < $width) {
  134. # we're cutting the path in the longitudinal direction, so we've got a rectangle
  135. $g->rectangle(
  136. 'x' => $self->scale * unscale($line->a->x),
  137. 'y' => $self->scale * $self->_y($layer->print_z),
  138. 'width' => $self->scale * $width,
  139. 'height' => $self->scale * $radius * 2,
  140. 'rx' => $self->scale * $radius * 0.35,
  141. 'ry' => $self->scale * $radius * 0.35,
  142. );
  143. } else {
  144. $g->circle(
  145. 'cx' => $self->scale * (unscale($line->a->x) + $radius),
  146. 'cy' => $self->scale * $self->_y($layer->print_z - $radius),
  147. 'r' => $self->scale * $radius,
  148. );
  149. }
  150. } else {
  151. my $height = $this_path->height != -1 ? $this_path->height : $layer->height;
  152. $g->rectangle(
  153. 'x' => $self->scale * unscale($line->a->x),
  154. 'y' => $self->scale * $self->_y($layer->print_z),
  155. 'width' => $self->scale * unscale($line->b->x - $line->a->x),
  156. 'height' => $self->scale * $height,
  157. 'rx' => $self->scale * $height * 0.5,
  158. 'ry' => $self->scale * $height * 0.5,
  159. );
  160. }
  161. }
  162. }
  163. sub _y {
  164. my $self = shift;
  165. my ($y) = @_;
  166. return $self->_height - $y;
  167. }
  168. 1;