Simple.pm 2.7 KB

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