Simple.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # A simple wrapper to quickly print a single model without a GUI.
  2. # Used by the command line slic3r.pl, by command line utilities pdf-slic3s.pl and view-toolpaths.pl,
  3. # and by the quick slice menu of the Slic3r GUI.
  4. #
  5. # It creates and owns an instance of Slic3r::Print to perform the slicing
  6. # and it accepts an instance of Slic3r::Model from the outside.
  7. package Slic3r::Print::Simple;
  8. use Moo;
  9. use Slic3r::Geometry qw(X Y);
  10. use Slic3r::Geometry::Clipper qw(diff);
  11. has '_print' => (
  12. is => 'ro',
  13. default => sub { Slic3r::Print->new },
  14. handles => [qw(apply_config config extruders output_filepath
  15. total_used_filament total_extruded_volume
  16. placeholder_parser process)],
  17. );
  18. has 'duplicate' => (
  19. is => 'rw',
  20. default => sub { 1 },
  21. );
  22. has 'scale' => (
  23. is => 'rw',
  24. default => sub { 1 },
  25. );
  26. has 'rotate' => (
  27. is => 'rw',
  28. default => sub { 0 },
  29. );
  30. has 'duplicate_grid' => (
  31. is => 'rw',
  32. default => sub { [1,1] },
  33. );
  34. has 'status_cb' => (
  35. is => 'rw',
  36. default => sub { sub {} },
  37. );
  38. has 'print_center' => (
  39. is => 'rw',
  40. );
  41. has 'dont_arrange' => (
  42. is => 'rw',
  43. default => sub { 0 },
  44. );
  45. has 'output_file' => (
  46. is => 'rw',
  47. );
  48. sub _bed_polygon {
  49. my ($self) = @_;
  50. my $bed_shape = $self->_print->config->bed_shape;
  51. return Slic3r::Polygon->new_scale(@$bed_shape);
  52. }
  53. sub set_model {
  54. # $model is of type Slic3r::Model
  55. my ($self, $model) = @_;
  56. # make method idempotent so that the object is reusable
  57. $self->_print->clear_objects;
  58. # make sure all objects have at least one defined instance
  59. my $need_arrange = $model->add_default_instances && ! $self->dont_arrange;
  60. # apply scaling and rotation supplied from command line if any
  61. foreach my $instance (map @{$_->instances}, @{$model->objects}) {
  62. $instance->set_scaling_factor($instance->scaling_factor * $self->scale);
  63. $instance->set_rotation($instance->rotation + $self->rotate);
  64. }
  65. my $bed_shape = $self->_print->config->bed_shape;
  66. my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($bed_shape);
  67. if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) {
  68. $model->duplicate_objects_grid($self->duplicate_grid->[X], $self->duplicate_grid->[Y], $self->_print->config->duplicate_distance);
  69. } elsif ($need_arrange) {
  70. $model->duplicate_objects($self->duplicate, $self->_print->config->min_object_distance, $bb);
  71. } elsif ($self->duplicate > 1) {
  72. # if all input objects have defined position(s) apply duplication to the whole model
  73. $model->duplicate($self->duplicate, $self->_print->config->min_object_distance, $bb);
  74. }
  75. $_->translate(0,0,-$_->bounding_box->z_min) for @{$model->objects} ;
  76. if (!$self->dont_arrange) {
  77. my $print_center = $self->print_center
  78. // Slic3r::Pointf->new_unscale(@{ $self->_bed_polygon->centroid });
  79. $model->center_instances_around_point($print_center);
  80. }
  81. foreach my $model_object (@{$model->objects}) {
  82. $self->_print->auto_assign_extruders($model_object);
  83. $self->_print->add_model_object($model_object);
  84. }
  85. }
  86. sub _before_export {
  87. my ($self) = @_;
  88. $self->_print->set_status_cb($self->status_cb);
  89. $self->_print->validate;
  90. }
  91. sub _after_export {
  92. my ($self) = @_;
  93. # check that all parts fit in bed shape, and warn if they don't
  94. # TODO: use actual toolpaths instead of total bounding box
  95. if (@{diff([$self->_print->bounding_box->polygon], [$self->_bed_polygon])}) {
  96. warn "Warning: the supplied parts might not fit in the configured bed shape. "
  97. . "You might want to review the result before printing.\n";
  98. }
  99. $self->_print->set_status_cb(undef);
  100. }
  101. sub export_gcode {
  102. my ($self) = @_;
  103. $self->_before_export;
  104. $self->_print->export_gcode(output_file => $self->output_file);
  105. $self->_after_export;
  106. }
  107. sub export_svg {
  108. my ($self) = @_;
  109. $self->_before_export;
  110. $self->_print->export_svg(output_file => $self->output_file);
  111. $self->_after_export;
  112. }
  113. 1;