Simple.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 'status_cb' => (
  34. is => 'rw',
  35. default => sub { sub {} },
  36. );
  37. has 'print_center' => (
  38. is => 'rw',
  39. default => sub { Slic3r::Pointf->new(100,100) },
  40. );
  41. has 'dont_arrange' => (
  42. is => 'rw',
  43. default => sub { 0 },
  44. );
  45. has 'output_file' => (
  46. is => 'rw',
  47. );
  48. sub set_model {
  49. # $model is of type Slic3r::Model
  50. my ($self, $model) = @_;
  51. # make method idempotent so that the object is reusable
  52. $self->_print->clear_objects;
  53. # make sure all objects have at least one defined instance
  54. my $need_arrange = $model->add_default_instances && ! $self->dont_arrange;
  55. # apply scaling and rotation supplied from command line if any
  56. foreach my $instance (map @{$_->instances}, @{$model->objects}) {
  57. $instance->set_scaling_factor($instance->scaling_factor * $self->scale);
  58. $instance->set_rotation($instance->rotation + $self->rotate);
  59. }
  60. if ($self->duplicate_grid->[X] > 1 || $self->duplicate_grid->[Y] > 1) {
  61. $model->duplicate_objects_grid($self->duplicate_grid->[X], $self->duplicate_grid->[Y], $self->_print->config->duplicate_distance);
  62. } elsif ($need_arrange) {
  63. $model->duplicate_objects($self->duplicate, $self->_print->config->min_object_distance);
  64. } elsif ($self->duplicate > 1) {
  65. # if all input objects have defined position(s) apply duplication to the whole model
  66. $model->duplicate($self->duplicate, $self->_print->config->min_object_distance);
  67. }
  68. $_->translate(0,0,-$_->bounding_box->z_min) for @{$model->objects};
  69. $model->center_instances_around_point($self->print_center) if (! $self->dont_arrange);
  70. foreach my $model_object (@{$model->objects}) {
  71. $self->_print->auto_assign_extruders($model_object);
  72. $self->_print->add_model_object($model_object);
  73. }
  74. }
  75. sub _before_export {
  76. my ($self) = @_;
  77. $self->_print->set_status_cb($self->status_cb);
  78. $self->_print->validate;
  79. }
  80. sub _after_export {
  81. my ($self) = @_;
  82. $self->_print->set_status_cb(undef);
  83. }
  84. sub export_gcode {
  85. my ($self) = @_;
  86. $self->_before_export;
  87. $self->_print->export_gcode(output_file => $self->output_file);
  88. $self->_after_export;
  89. }
  90. sub export_svg {
  91. my ($self) = @_;
  92. $self->_before_export;
  93. $self->_print->export_svg(output_file => $self->output_file);
  94. $self->_after_export;
  95. }
  96. 1;