SectionCut.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(first 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 => 'rw');
  14. has '_height' => (is => 'rw');
  15. has '_svg' => (is => 'rw');
  16. has '_svg_style' => (is => 'rw', default => sub { {} });
  17. sub BUILD {
  18. my $self = shift;
  19. # calculate the Y coordinate of the section line
  20. my $bb = $self->print->bounding_box;
  21. my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
  22. # store our section line
  23. $self->line(Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]));
  24. }
  25. sub export_svg {
  26. my $self = shift;
  27. my ($filename) = @_;
  28. # get bounding box of print and its height
  29. # (Print should return a BoundingBox3 object instead)
  30. my $bb = $self->print->bounding_box;
  31. my $print_size = $bb->size;
  32. $self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
  33. # initialize the SVG canvas
  34. $self->_svg(my $svg = SVG->new(
  35. width => $self->scale * unscale($print_size->x),
  36. height => $self->scale * $self->_height,
  37. ));
  38. # set default styles
  39. $self->_svg_style->{'stroke-width'} = 1;
  40. $self->_svg_style->{'fill-opacity'} = 0.5;
  41. $self->_svg_style->{'stroke-opacity'} = 0.2;
  42. # plot perimeters
  43. $self->_svg_style->{'stroke'} = '#EE0000';
  44. $self->_svg_style->{'fill'} = '#FF0000';
  45. $self->_plot_group(sub { map @{$_->perimeters}, @{$_[0]->regions} });
  46. # plot infill
  47. $self->_svg_style->{'stroke'} = '#444444';
  48. $self->_svg_style->{'fill'} = '#454545';
  49. $self->_plot_group(sub { map @{$_->fills}, @{$_[0]->regions} });
  50. # plot support material
  51. $self->_svg_style->{'stroke'} = '#12EF00';
  52. $self->_svg_style->{'fill'} = '#22FF00';
  53. $self->_plot_group(sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills) : () });
  54. Slic3r::open(\my $fh, '>', $filename);
  55. print $fh $svg->xmlify;
  56. close $fh;
  57. printf "Section cut SVG written to %s\n", $filename;
  58. }
  59. sub _plot_group {
  60. my $self = shift;
  61. my ($filter) = @_;
  62. my $bb = $self->print->bounding_box;
  63. my $g = $self->_svg->group(style => { %{$self->_svg_style} });
  64. foreach my $object (@{$self->print->objects}) {
  65. foreach my $copy (@{$object->_shifted_copies}) {
  66. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  67. # get all ExtrusionPath objects
  68. my @paths = map $_->clone,
  69. map { ($_->isa('Slic3r::ExtrusionLoop') || $_->isa('Slic3r::ExtrusionPath::Collection')) ? @$_ : $_ }
  70. grep defined $_,
  71. $filter->($layer);
  72. # move paths to location of copy
  73. $_->polyline->translate(@$copy) for @paths;
  74. if (0) {
  75. # export plan with section line and exit
  76. require "Slic3r/SVG.pm";
  77. Slic3r::SVG::output(
  78. "line.svg",
  79. no_arrows => 1,
  80. lines => [ $self->line ],
  81. red_polylines => [ map $_->polyline, @paths ],
  82. );
  83. exit;
  84. }
  85. foreach my $path (@paths) {
  86. foreach my $line (@{$path->lines}) {
  87. my @intersections = @{intersection_pl(
  88. [ $self->line->as_polyline ],
  89. $line->grow(Slic3r::Geometry::scale $path->width/2),
  90. )};
  91. die "Intersection has more than two points!\n"
  92. if defined first { @$_ > 2 } @intersections;
  93. # turn intersections to lines
  94. my @lines = map Slic3r::Line->new(@$_), @intersections;
  95. # align intersections to canvas
  96. $_->translate(-$bb->x_min, 0) for @lines;
  97. # we want lines oriented from left to right in order to draw
  98. # rectangles correctly
  99. foreach my $line (@lines) {
  100. $line->reverse if $line->a->x > $line->b->x;
  101. }
  102. if ($path->is_bridge) {
  103. foreach my $line (@lines) {
  104. my $radius = $path->width / 2;
  105. my $width = unscale abs($line->b->x - $line->a->x);
  106. if ((10 * $radius) < $width) {
  107. # we're cutting the path in the longitudinal direction, so we've got a rectangle
  108. $g->rectangle(
  109. 'x' => $self->scale * unscale($line->a->x),
  110. 'y' => $self->scale * $self->_y($layer->print_z),
  111. 'width' => $self->scale * $width,
  112. 'height' => $self->scale * $radius * 2,
  113. 'rx' => $self->scale * $radius * 0.35,
  114. 'ry' => $self->scale * $radius * 0.35,
  115. );
  116. } else {
  117. $g->circle(
  118. 'cx' => $self->scale * (unscale($line->a->x) + $radius),
  119. 'cy' => $self->scale * $self->_y($layer->print_z - $radius),
  120. 'r' => $self->scale * $radius,
  121. );
  122. }
  123. }
  124. } else {
  125. foreach my $line (@lines) {
  126. my $height = $path->height;
  127. $height = $layer->height if $height == -1;
  128. $g->rectangle(
  129. 'x' => $self->scale * unscale($line->a->x),
  130. 'y' => $self->scale * $self->_y($layer->print_z),
  131. 'width' => $self->scale * unscale($line->b->x - $line->a->x),
  132. 'height' => $self->scale * $height,
  133. 'rx' => $self->scale * $height * 0.5,
  134. 'ry' => $self->scale * $height * 0.5,
  135. );
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. sub _y {
  145. my $self = shift;
  146. my ($y) = @_;
  147. return $self->_height - $y;
  148. }
  149. 1;