ExtrusionLoop.pm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package Slic3r::ExtrusionLoop;
  2. use Moo;
  3. use Slic3r::Geometry qw(same_point);
  4. # the underlying Slic3r::Polygon objects holds the geometry
  5. has 'polygon' => (
  6. is => 'rw',
  7. required => 1,
  8. handles => [qw(is_printable nearest_point_index_to reverse)],
  9. );
  10. has 'flow_spacing' => (is => 'rw', required => 1);
  11. # see EXTR_ROLE_* constants in ExtrusionPath.pm
  12. has 'role' => (is => 'rw', required => 1);
  13. use constant PACK_FMT => 'fca*';
  14. # class or object method
  15. sub pack {
  16. my $self = shift;
  17. my %args = @_;
  18. if (ref $self) {
  19. %args = map { $_ => $self->$_ } qw(flow_spacing role polygon);
  20. }
  21. my $o = \ pack PACK_FMT,
  22. $args{flow_spacing} || -1,
  23. $args{role} // (die "Missing mandatory attribute 'role'"), #/
  24. $args{polygon}->serialize;
  25. bless $o, 'Slic3r::ExtrusionLoop::Packed';
  26. return $o;
  27. }
  28. sub split_at_index {
  29. my $self = shift;
  30. return Slic3r::ExtrusionPath->new(
  31. polyline => $self->polygon->split_at_index(@_),
  32. role => $self->role,
  33. flow_spacing => $self->flow_spacing,
  34. );
  35. }
  36. sub split_at {
  37. my $self = shift;
  38. return Slic3r::ExtrusionPath->new(
  39. polyline => $self->polygon->split_at(@_),
  40. role => $self->role,
  41. flow_spacing => $self->flow_spacing,
  42. );
  43. }
  44. sub split_at_first_point {
  45. my $self = shift;
  46. return $self->split_at_index(0);
  47. }
  48. # although a loop doesn't have endpoints, this method is provided to allow
  49. # ExtrusionLoop objects to be added to an ExtrusionPath::Collection and
  50. # sorted by the ->shortest_path() method
  51. sub endpoints {
  52. my $self = shift;
  53. return ($self->polygon->[0], $self->polygon->[-1]);
  54. }
  55. package Slic3r::ExtrusionLoop::Packed;
  56. sub unpack {
  57. my $self = shift;
  58. my ($flow_spacing, $role, $polygon_s)
  59. = unpack Slic3r::ExtrusionLoop::PACK_FMT, $$self;
  60. return Slic3r::ExtrusionLoop->new(
  61. flow_spacing => ($flow_spacing == -1) ? undef : $flow_spacing,
  62. role => $role,
  63. polygon => Slic3r::Polygon->deserialize($polygon_s),
  64. );
  65. }
  66. 1;