Config.pm 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Extends C++ class Slic3r::DynamicPrintConfig
  2. # This perl class does not keep any perl class variables,
  3. # all the storage is handled by the underlying C++ code.
  4. package Slic3r::Config;
  5. use strict;
  6. use warnings;
  7. use utf8;
  8. use List::Util qw(first max);
  9. # C++ Slic3r::PrintConfigDef exported as a Perl hash of hashes.
  10. # The C++ counterpart is a constant singleton.
  11. our $Options = print_config_def();
  12. # Generate accessors.
  13. {
  14. no strict 'refs';
  15. for my $opt_key (keys %$Options) {
  16. *{$opt_key} = sub {
  17. #print "Slic3r::Config::accessor $opt_key\n";
  18. $_[0]->get($opt_key)
  19. };
  20. }
  21. }
  22. # From command line parameters, used by slic3r.pl
  23. sub new_from_cli {
  24. my $class = shift;
  25. my %args = @_;
  26. # Delete hash keys with undefined value.
  27. delete $args{$_} for grep !defined $args{$_}, keys %args;
  28. # Replace the start_gcode, end_gcode ... hash values
  29. # with the content of the files they reference.
  30. for (qw(start end layer toolchange)) {
  31. my $opt_key = "${_}_gcode";
  32. if ($args{$opt_key}) {
  33. if (-e $args{$opt_key}) {
  34. Slic3r::open(\my $fh, "<", $args{$opt_key})
  35. or die "Failed to open $args{$opt_key}\n";
  36. binmode $fh, ':utf8';
  37. $args{$opt_key} = do { local $/; <$fh> };
  38. close $fh;
  39. }
  40. }
  41. }
  42. my $self = $class->new;
  43. foreach my $opt_key (keys %args) {
  44. my $opt_def = $Options->{$opt_key};
  45. # we use set_deserialize() for bool options since GetOpt::Long doesn't handle
  46. # arrays of boolean values
  47. if ($opt_key =~ /^(?:bed_shape|duplicate_grid|extruder_offset)$/ || $opt_def->{type} eq 'bool') {
  48. $self->set_deserialize($opt_key, $args{$opt_key});
  49. } elsif (my $shortcut = $opt_def->{shortcut}) {
  50. $self->set($_, $args{$opt_key}) for @$shortcut;
  51. } else {
  52. $self->set($opt_key, $args{$opt_key});
  53. }
  54. }
  55. return $self;
  56. }
  57. package Slic3r::Config::Static;
  58. use parent 'Slic3r::Config';
  59. sub Slic3r::Config::GCode::new { Slic3r::Config::Static::new_GCodeConfig }
  60. sub Slic3r::Config::Print::new { Slic3r::Config::Static::new_PrintConfig }
  61. sub Slic3r::Config::PrintObject::new { Slic3r::Config::Static::new_PrintObjectConfig }
  62. sub Slic3r::Config::PrintRegion::new { Slic3r::Config::Static::new_PrintRegionConfig }
  63. sub Slic3r::Config::Full::new { Slic3r::Config::Static::new_FullPrintConfig }
  64. 1;