Simple.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. has '_print' => (
  11. is => 'ro',
  12. default => sub { Slic3r::Print->new },
  13. handles => [qw(apply_config extruders output_filepath
  14. total_used_filament total_extruded_volume
  15. placeholder_parser process)],
  16. );
  17. has 'duplicate' => (
  18. is => 'rw',
  19. default => sub { 1 },
  20. );
  21. has 'scale' => (
  22. is => 'rw',
  23. default => sub { 1 },
  24. );
  25. has 'rotate' => (
  26. is => 'rw',
  27. default => sub { 0 },
  28. );
  29. has 'duplicate_grid' => (
  30. is => 'rw',
  31. default => sub { [1,1] },
  32. );
  33. has 'print_center' => (
  34. is => 'rw',
  35. default => sub { Slic3r::Pointf->new(100,100) },
  36. );
  37. has 'dont_arrange' => (
  38. is => 'rw',
  39. default => sub { 0 },
  40. );
  41. has 'output_file' => (
  42. is => 'rw',
  43. );
  44. sub set_model {
  45. # $model is of type Slic3r::Model
  46. my ($self, $model) = @_;
  47. # make method idempotent so that the object is reusable
  48. $self->_print->clear_objects;
  49. # make sure all objects have at least one defined instance
  50. my $need_arrange = $model->add_default_instances && ! $self->dont_arrange;
  51. # apply scaling and rotation supplied from command line if any
  52. foreach my $instance (map @{$_->instances}, @{$model->objects}) {
  53. $instance->set_scaling_factor($instance->scaling_factor * $self->scale);
  54. $instance->set_rotation($instance->rotation + $self->rotate);
  55. }
  56. if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) {
  57. $model->duplicate_objects_grid($self->duplicate_grid->[X], $self->duplicate_grid->[Y], $self->_print->config->duplicate_distance);
  58. } elsif ($need_arrange) {
  59. $model->duplicate_objects($self->duplicate, $self->_print->config->min_object_distance);
  60. } elsif ($self->duplicate > 1) {
  61. # if all input objects have defined position(s) apply duplication to the whole model
  62. $model->duplicate($self->duplicate, $self->_print->config->min_object_distance);
  63. }
  64. $_->translate(0,0,-$_->bounding_box->z_min) for @{$model->objects};
  65. $model->center_instances_around_point($self->print_center) if (! $self->dont_arrange);
  66. foreach my $model_object (@{$model->objects}) {
  67. $self->_print->auto_assign_extruders($model_object);
  68. $self->_print->add_model_object($model_object);
  69. }
  70. }
  71. sub export_gcode {
  72. my ($self) = @_;
  73. $self->_print->validate;
  74. $self->_print->export_gcode($self->output_file // '');
  75. }
  76. sub export_png {
  77. my ($self) = @_;
  78. $self->_before_export;
  79. $self->_print->export_png(output_file => $self->output_file);
  80. $self->_after_export;
  81. }
  82. 1;