Config.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. # cemetery of old config settings
  10. our @Ignore = qw(duplicate_x duplicate_y multiply_x multiply_y support_material_tool acceleration
  11. adjust_overhang_flow standby_temperature scale rotate duplicate duplicate_grid
  12. rotate scale duplicate_grid start_perimeters_at_concave_points start_perimeters_at_non_overhang
  13. randomize_start seal_position bed_size print_center g0 vibration_limit gcode_arcs pressure_advance);
  14. # C++ Slic3r::PrintConfigDef exported as a Perl hash of hashes.
  15. # The C++ counterpart is a constant singleton.
  16. our $Options = print_config_def();
  17. # overwrite the hard-coded readonly value (this information is not available in XS)
  18. $Options->{threads}{readonly} = !$Slic3r::have_threads;
  19. # generate accessors
  20. {
  21. no strict 'refs';
  22. for my $opt_key (keys %$Options) {
  23. *{$opt_key} = sub { $_[0]->get($opt_key) };
  24. }
  25. }
  26. # Fill in the underlying C++ Slic3r::DynamicPrintConfig with the content of the defaults
  27. # provided by the C++ class Slic3r::FullPrintConfig.
  28. sub new_from_defaults {
  29. my $class = shift;
  30. my (@opt_keys) = @_;
  31. my $self = $class->new;
  32. # Instantiating the C++ class Slic3r::FullPrintConfig.
  33. my $defaults = Slic3r::Config::Full->new;
  34. if (@opt_keys) {
  35. $self->set($_, $defaults->get($_))
  36. for grep $defaults->has($_), @opt_keys;
  37. } else {
  38. $self->apply_static($defaults);
  39. }
  40. return $self;
  41. }
  42. # From command line parameters
  43. sub new_from_cli {
  44. my $class = shift;
  45. my %args = @_;
  46. # Delete hash keys with undefined value.
  47. delete $args{$_} for grep !defined $args{$_}, keys %args;
  48. # Replace the start_gcode, end_gcode ... hash values
  49. # with the content of the files they reference.
  50. for (qw(start end layer toolchange)) {
  51. my $opt_key = "${_}_gcode";
  52. if ($args{$opt_key}) {
  53. if (-e $args{$opt_key}) {
  54. Slic3r::open(\my $fh, "<", $args{$opt_key})
  55. or die "Failed to open $args{$opt_key}\n";
  56. binmode $fh, ':utf8';
  57. $args{$opt_key} = do { local $/; <$fh> };
  58. close $fh;
  59. }
  60. }
  61. }
  62. my $self = $class->new;
  63. foreach my $opt_key (keys %args) {
  64. my $opt_def = $Options->{$opt_key};
  65. # we use set_deserialize() for bool options since GetOpt::Long doesn't handle
  66. # arrays of boolean values
  67. if ($opt_key =~ /^(?:bed_shape|duplicate_grid|extruder_offset)$/ || $opt_def->{type} eq 'bool') {
  68. $self->set_deserialize($opt_key, $args{$opt_key});
  69. } elsif (my $shortcut = $opt_def->{shortcut}) {
  70. $self->set($_, $args{$opt_key}) for @$shortcut;
  71. } else {
  72. $self->set($opt_key, $args{$opt_key});
  73. }
  74. }
  75. return $self;
  76. }
  77. sub merge {
  78. my $class = shift;
  79. my $config = $class->new;
  80. $config->apply($_) for @_;
  81. return $config;
  82. }
  83. # Load a flat ini file without a category into the underlying C++ Slic3r::DynamicConfig class,
  84. # convert legacy configuration names.
  85. sub load {
  86. my $class = shift;
  87. my ($file) = @_;
  88. # Instead of using the /i modifier for case-insensitive matching, the case insensitivity is expressed
  89. # explicitely to avoid having to bundle the UTF8 Perl library.
  90. if ($file =~ /\.[gG][cC][oO][dD][eE]/ || $file =~ /\.[gG]/) {
  91. my $config = $class->new;
  92. $config->_load_from_gcode($file);
  93. return $config;
  94. } else {
  95. my $ini = __PACKAGE__->read_ini($file);
  96. return $class->load_ini_hash($ini->{_});
  97. }
  98. }
  99. # Deserialize a perl hash into the underlying C++ Slic3r::DynamicConfig class,
  100. # convert legacy configuration names.
  101. sub load_ini_hash {
  102. my $class = shift;
  103. my ($ini_hash) = @_;
  104. my $config = $class->new;
  105. foreach my $opt_key (keys %$ini_hash) {
  106. ($opt_key, my $value) = _handle_legacy($opt_key, $ini_hash->{$opt_key});
  107. next if !defined $opt_key;
  108. $config->set_deserialize($opt_key, $value);
  109. }
  110. return $config;
  111. }
  112. sub clone {
  113. my $self = shift;
  114. my $new = (ref $self)->new;
  115. $new->apply($self);
  116. return $new;
  117. }
  118. sub get_value {
  119. my $self = shift;
  120. my ($opt_key) = @_;
  121. return $Options->{$opt_key}{ratio_over}
  122. ? $self->get_abs_value($opt_key)
  123. : $self->get($opt_key);
  124. }
  125. sub _handle_legacy {
  126. my ($opt_key, $value) = @_;
  127. # handle legacy options
  128. if ($opt_key =~ /^(extrusion_width|bottom_layer_speed|first_layer_height)_ratio$/) {
  129. $opt_key = $1;
  130. $opt_key =~ s/^bottom_layer_speed$/first_layer_speed/;
  131. $value = $value =~ /^\d+(?:\.\d+)?$/ && $value != 0 ? ($value*100) . "%" : 0;
  132. }
  133. if ($opt_key eq 'threads' && !$Slic3r::have_threads) {
  134. $value = 1;
  135. }
  136. if ($opt_key eq 'gcode_flavor' && $value eq 'makerbot') {
  137. $value = 'makerware';
  138. }
  139. if ($opt_key eq 'fill_density' && defined($value) && $value !~ /%/ && $value <= 1) {
  140. # fill_density was turned into a percent value
  141. $value *= 100;
  142. $value = "$value"; # force update of the PV value, workaround for bug https://rt.cpan.org/Ticket/Display.html?id=94110
  143. }
  144. if ($opt_key eq 'randomize_start' && $value) {
  145. $opt_key = 'seam_position';
  146. $value = 'random';
  147. }
  148. if ($opt_key eq 'bed_size' && $value) {
  149. $opt_key = 'bed_shape';
  150. my ($x, $y) = split /,/, $value;
  151. $value = "0x0,${x}x0,${x}x${y},0x${y}";
  152. }
  153. return () if first { $_ eq $opt_key } @Ignore;
  154. # For historical reasons, the world's full of configs having these very low values;
  155. # to avoid unexpected behavior we need to ignore them. Banning these two hard-coded
  156. # values is a dirty hack and will need to be removed sometime in the future, but it
  157. # will avoid lots of complaints for now.
  158. if ($opt_key eq 'perimeter_acceleration' && $value == '25') {
  159. $value = 0;
  160. }
  161. if ($opt_key eq 'infill_acceleration' && $value == '50') {
  162. $value = 0;
  163. }
  164. if (!exists $Options->{$opt_key}) {
  165. my @keys = grep { $Options->{$_}{aliases} && grep $_ eq $opt_key, @{$Options->{$_}{aliases}} } keys %$Options;
  166. if (!@keys) {
  167. warn "Unknown option $opt_key\n";
  168. return ();
  169. }
  170. $opt_key = $keys[0];
  171. }
  172. return ($opt_key, $value);
  173. }
  174. # Create a hash of hashes from the underlying C++ Slic3r::DynamicPrintConfig.
  175. # The first hash key is '_' meaning no category.
  176. sub as_ini {
  177. my ($self) = @_;
  178. my $ini = { _ => {} };
  179. foreach my $opt_key (sort @{$self->get_keys}) {
  180. next if $Options->{$opt_key}{shortcut};
  181. $ini->{_}{$opt_key} = $self->serialize($opt_key);
  182. }
  183. return $ini;
  184. }
  185. # Save the content of the underlying C++ Slic3r::DynamicPrintConfig as a flat ini file without any category.
  186. sub save {
  187. my $self = shift;
  188. my ($file) = @_;
  189. __PACKAGE__->write_ini($file, $self->as_ini);
  190. }
  191. # this method is idempotent by design and only applies to ::DynamicConfig or ::Full
  192. # objects because it performs cross checks
  193. sub validate {
  194. my $self = shift;
  195. # -j, --threads
  196. die "Invalid value for --threads\n"
  197. if $self->threads < 1;
  198. # --layer-height
  199. die "Invalid value for --layer-height\n"
  200. if $self->layer_height <= 0;
  201. die "--layer-height must be a multiple of print resolution\n"
  202. if $self->layer_height / &Slic3r::SCALING_FACTOR % 1 != 0;
  203. # --first-layer-height
  204. die "Invalid value for --first-layer-height\n"
  205. if $self->first_layer_height !~ /^(?:\d*(?:\.\d+)?)%?$/;
  206. die "Invalid value for --first-layer-height\n"
  207. if $self->get_value('first_layer_height') <= 0;
  208. # --filament-diameter
  209. die "Invalid value for --filament-diameter\n"
  210. if grep $_ < 1, @{$self->filament_diameter};
  211. # --nozzle-diameter
  212. die "Invalid value for --nozzle-diameter\n"
  213. if grep $_ < 0, @{$self->nozzle_diameter};
  214. # --perimeters
  215. die "Invalid value for --perimeters\n"
  216. if $self->perimeters < 0;
  217. # --solid-layers
  218. die "Invalid value for --solid-layers\n" if defined $self->solid_layers && $self->solid_layers < 0;
  219. die "Invalid value for --top-solid-layers\n" if $self->top_solid_layers < 0;
  220. die "Invalid value for --bottom-solid-layers\n" if $self->bottom_solid_layers < 0;
  221. # --gcode-flavor
  222. die "Invalid value for --gcode-flavor\n"
  223. if !first { $_ eq $self->gcode_flavor } @{$Options->{gcode_flavor}{values}};
  224. die "--use-firmware-retraction is only supported by Marlin, Smoothie, Repetier and Machinekit firmware\n"
  225. if $self->use_firmware_retraction && $self->gcode_flavor ne 'smoothie'
  226. && $self->gcode_flavor ne 'reprap'
  227. && $self->gcode_flavor ne 'machinekit'
  228. && $self->gcode_flavor ne 'repetier';
  229. die "--use-firmware-retraction is not compatible with --wipe\n"
  230. if $self->use_firmware_retraction && first {$_} @{$self->wipe};
  231. # --fill-pattern
  232. die "Invalid value for --fill-pattern\n"
  233. if !first { $_ eq $self->fill_pattern } @{$Options->{fill_pattern}{values}};
  234. # --top-fill-pattern
  235. die "Invalid value for --top-fill-pattern\n"
  236. if !first { $_ eq $self->top_fill_pattern } @{$Options->{top_fill_pattern}{values}};
  237. # --bottom-fill-pattern
  238. die "Invalid value for --bottom-fill-pattern\n"
  239. if !first { $_ eq $self->bottom_fill_pattern } @{$Options->{bottom_fill_pattern}{values}};
  240. # --fill-density
  241. die "The selected fill pattern is not supposed to work at 100% density\n"
  242. if $self->fill_density == 100
  243. && !first { $_ eq $self->fill_pattern } @{$Options->{external_fill_pattern}{values}};
  244. # --infill-every-layers
  245. die "Invalid value for --infill-every-layers\n"
  246. if $self->infill_every_layers !~ /^\d+$/ || $self->infill_every_layers < 1;
  247. # --skirt-height
  248. die "Invalid value for --skirt-height\n"
  249. if $self->skirt_height < -1; # -1 means as tall as the object
  250. # --bridge-flow-ratio
  251. die "Invalid value for --bridge-flow-ratio\n"
  252. if $self->bridge_flow_ratio <= 0;
  253. # extruder clearance
  254. die "Invalid value for --extruder-clearance-radius\n"
  255. if $self->extruder_clearance_radius <= 0;
  256. die "Invalid value for --extruder-clearance-height\n"
  257. if $self->extruder_clearance_height <= 0;
  258. # --extrusion-multiplier
  259. die "Invalid value for --extrusion-multiplier\n"
  260. if defined first { $_ <= 0 } @{$self->extrusion_multiplier};
  261. # --default-acceleration
  262. die "Invalid zero value for --default-acceleration when using other acceleration settings\n"
  263. if ($self->perimeter_acceleration || $self->infill_acceleration || $self->bridge_acceleration || $self->first_layer_acceleration)
  264. && !$self->default_acceleration;
  265. # --spiral-vase
  266. if ($self->spiral_vase) {
  267. # Note that we might want to have more than one perimeter on the bottom
  268. # solid layers.
  269. die "Can't make more than one perimeter when spiral vase mode is enabled\n"
  270. if $self->perimeters > 1;
  271. die "Can't make less than one perimeter when spiral vase mode is enabled\n"
  272. if $self->perimeters < 1;
  273. die "Spiral vase mode can only print hollow objects, so you need to set Fill density to 0\n"
  274. if $self->fill_density > 0;
  275. die "Spiral vase mode is not compatible with top solid layers\n"
  276. if $self->top_solid_layers > 0;
  277. die "Spiral vase mode is not compatible with support material\n"
  278. if $self->support_material || $self->support_material_enforce_layers > 0;
  279. }
  280. # extrusion widths
  281. {
  282. my $max_nozzle_diameter = max(@{ $self->nozzle_diameter });
  283. die "Invalid extrusion width (too large)\n"
  284. if defined first { $_ > 10 * $max_nozzle_diameter }
  285. map $self->get_abs_value_over("${_}_extrusion_width", $max_nozzle_diameter),
  286. qw(perimeter infill solid_infill top_infill support_material first_layer);
  287. }
  288. # general validation, quick and dirty
  289. foreach my $opt_key (@{$self->get_keys}) {
  290. my $opt = $Options->{$opt_key};
  291. next unless defined $self->$opt_key;
  292. next unless defined $opt->{cli} && $opt->{cli} =~ /=(.+)$/;
  293. my $type = $1;
  294. my @values = ();
  295. if ($type =~ s/\@$//) {
  296. die "Invalid value for $opt_key\n" if ref($self->$opt_key) ne 'ARRAY';
  297. @values = @{ $self->$opt_key };
  298. } else {
  299. @values = ($self->$opt_key);
  300. }
  301. foreach my $value (@values) {
  302. if ($type eq 'i' || $type eq 'f' || $opt->{type} eq 'percent') {
  303. $value =~ s/%$// if $opt->{type} eq 'percent';
  304. die "Invalid value for $opt_key\n"
  305. if ($type eq 'i' && $value !~ /^-?\d+$/)
  306. || (($type eq 'f' || $opt->{type} eq 'percent') && $value !~ /^-?(?:\d+|\d*\.\d+)$/)
  307. || (defined $opt->{min} && $value < $opt->{min})
  308. || (defined $opt->{max} && $value > $opt->{max});
  309. } elsif ($type eq 's' && $opt->{type} eq 'select') {
  310. die "Invalid value for $opt_key\n"
  311. unless first { $_ eq $value } @{ $opt->{values} };
  312. }
  313. }
  314. }
  315. return 1;
  316. }
  317. # CLASS METHODS:
  318. # Write a "Windows" style ini file with categories enclosed in squre brackets.
  319. sub write_ini {
  320. my $class = shift;
  321. my ($file, $ini) = @_;
  322. Slic3r::open(\my $fh, '>', $file);
  323. binmode $fh, ':utf8';
  324. my $localtime = localtime;
  325. printf $fh "# generated by Slic3r $Slic3r::VERSION on %s\n", "$localtime";
  326. # make sure the _ category is the first one written
  327. foreach my $category (sort { ($a eq '_') ? -1 : ($a cmp $b) } keys %$ini) {
  328. printf $fh "\n[%s]\n", $category if $category ne '_';
  329. foreach my $key (sort keys %{$ini->{$category}}) {
  330. printf $fh "%s = %s\n", $key, $ini->{$category}{$key};
  331. }
  332. }
  333. close $fh;
  334. }
  335. # Parse a "Windows" style ini file with categories enclosed in squre brackets.
  336. # Returns a hash of hashes over strings.
  337. # {category}{name}=value
  338. # Non-categorized entries are stored under a category '_'.
  339. sub read_ini {
  340. my $class = shift;
  341. my ($file) = @_;
  342. local $/ = "\n";
  343. Slic3r::open(\my $fh, '<', $file)
  344. or die "Unable to open $file: $!\n";
  345. binmode $fh, ':utf8';
  346. my $ini = { _ => {} };
  347. my $category = '_';
  348. while (<$fh>) {
  349. s/\R+$//;
  350. next if /^\s+/;
  351. next if /^$/;
  352. next if /^\s*#/;
  353. if (/^\[(.+?)\]$/) {
  354. $category = $1;
  355. next;
  356. }
  357. /^(\w+) *= *(.*)/ or die "Unreadable configuration file (invalid data at line $.)\n";
  358. $ini->{$category}{$1} = $2;
  359. }
  360. close $fh;
  361. return $ini;
  362. }
  363. package Slic3r::Config::Static;
  364. use parent 'Slic3r::Config';
  365. sub Slic3r::Config::GCode::new { Slic3r::Config::Static::new_GCodeConfig }
  366. sub Slic3r::Config::Print::new { Slic3r::Config::Static::new_PrintConfig }
  367. sub Slic3r::Config::PrintObject::new { Slic3r::Config::Static::new_PrintObjectConfig }
  368. sub Slic3r::Config::PrintRegion::new { Slic3r::Config::Static::new_PrintRegionConfig }
  369. sub Slic3r::Config::Full::new { Slic3r::Config::Static::new_FullPrintConfig }
  370. 1;