From 3bc79e80d5d0618633d1ce556f561774acc59588 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 27 Oct 2017 16:11:06 +0200 Subject: [PATCH] Fixed configuration & validate C++ ports. --- lib/Slic3r/Config.pm | 17 ----------------- lib/Slic3r/Print.pm | 9 --------- xs/src/libslic3r/Config.cpp | 8 ++++++++ xs/src/libslic3r/Config.hpp | 1 + xs/src/libslic3r/PrintConfig.cpp | 2 +- xs/t/15_config.t | 3 +-- xs/xsp/Config.xsp | 30 ++++++++++++++++++++++++------ xs/xsp/Print.xsp | 3 +-- 8 files changed, 36 insertions(+), 37 deletions(-) diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index 5af2fa992..4b6e5d453 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -94,23 +94,6 @@ sub merge { return $config; } -# Load a flat ini file without a category into the underlying C++ Slic3r::DynamicConfig class, -# convert legacy configuration names. -sub load { - my ($class, $file) = @_; - # Instead of using the /i modifier for case-insensitive matching, the case insensitivity is expressed - # explicitely to avoid having to bundle the UTF8 Perl library. - if ($file =~ /\.[gG][cC][oO][dD][eE]/ || $file =~ /\.[gG]/) { - my $config = $class->new; - $config->_load_from_gcode($file); - return $config; - } else { - my $config = $class->new; - $config->_load($file); - return $config; - } -} - sub clone { my $self = shift; my $new = (ref $self)->new; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index 160f70e06..1613b7e45 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -255,13 +255,4 @@ sub make_wipe_tower { $self->set_step_done(STEP_WIPE_TOWER); } -# Wrapper around the C++ Slic3r::Print::validate() -# to produce a Perl exception without a hang-up on some Strawberry perls. -sub validate -{ - my $self = shift; - my $err = $self->_validate; - die $err . "\n" if (defined($err) && $err ne ''); -} - 1; diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index b8954cc07..c8f923e89 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -320,6 +320,14 @@ void ConfigBase::setenv_() } void ConfigBase::load(const std::string &file) +{ + if (boost::iends_with(file, ".gcode") || boost::iends_with(file, ".g")) + this->load_from_gcode(file); + else + this->load_from_ini(file); +} + +void ConfigBase::load_from_ini(const std::string &file) { namespace pt = boost::property_tree; pt::ptree tree; diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 3808e9bb2..5cf24d8ed 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -952,6 +952,7 @@ public: double get_abs_value(const t_config_option_key &opt_key, double ratio_over) const; void setenv_(); void load(const std::string &file); + void load_from_ini(const std::string &file); void load_from_gcode(const std::string &file); void save(const std::string &file) const; diff --git a/xs/src/libslic3r/PrintConfig.cpp b/xs/src/libslic3r/PrintConfig.cpp index c8a87022e..420bd4490 100644 --- a/xs/src/libslic3r/PrintConfig.cpp +++ b/xs/src/libslic3r/PrintConfig.cpp @@ -1825,7 +1825,7 @@ std::string FullPrintConfig::validate() // --nozzle-diameter for (double nd : this->nozzle_diameter.values) - if (nd < 1) + if (nd < 0.005) return "Invalid value for --nozzle-diameter"; // --perimeters diff --git a/xs/t/15_config.t b/xs/t/15_config.t index 946297358..6e73c8a9a 100644 --- a/xs/t/15_config.t +++ b/xs/t/15_config.t @@ -244,9 +244,8 @@ foreach my $config (Slic3r::Config->new, Slic3r::Config::Static::new_FullPrintCo { use Cwd qw(abs_path); use File::Basename qw(dirname); - my $class = Slic3r::Config->new; my $path = abs_path($0); - my $config = $class->_load(dirname($path)."/inc/22_config_bad_config_options.ini"); + my $config = Slic3r::Config::load(dirname($path)."/inc/22_config_bad_config_options.ini"); ok 1, 'did not crash on reading invalid items in config'; } diff --git a/xs/xsp/Config.xsp b/xs/xsp/Config.xsp index 0e76b4cf1..2f8946e8b 100644 --- a/xs/xsp/Config.xsp +++ b/xs/xsp/Config.xsp @@ -38,16 +38,24 @@ void normalize(); %name{setenv} void setenv_(); double min_object_distance() %code{% RETVAL = PrintConfig::min_object_distance(THIS); %}; - %name{_load} void load(std::string file); - %name{_load_from_gcode} void load_from_gcode(std::string input_file) + static DynamicPrintConfig* load(char *path) %code%{ + auto config = new DynamicPrintConfig(); try { - THIS->load_from_gcode(input_file); + config->load(path); + RETVAL = config; } catch (std::exception& e) { - croak("Error extracting configuration from a g-code %s:\n%s\n", input_file.c_str(), e.what()); + delete config; + croak("Error extracting configuration from %s:\n%s\n", path, e.what()); } %}; void save(std::string file); + int validate() %code%{ + std::string err = THIS->validate(); + if (! err.empty()) + croak("Configuration is not valid: %s\n", err.c_str()); + RETVAL = 1; + %}; }; %name{Slic3r::Config::Static} class StaticPrintConfig { @@ -94,8 +102,18 @@ %}; %name{setenv} void setenv_(); double min_object_distance() %code{% RETVAL = PrintConfig::min_object_distance(THIS); %}; - %name{_load} void load(std::string file); - %name{_load_from_gcode} void load_from_gcode(std::string file); + static StaticPrintConfig* load(char *path) + %code%{ + auto config = new FullPrintConfig(); + try { + config->load(path); + RETVAL = static_cast(config); + } catch (std::exception& e) { + delete config; + croak("Error extracting configuration from %s:\n%s\n", path, e.what()); + } + %}; + void save(std::string file); }; diff --git a/xs/xsp/Print.xsp b/xs/xsp/Print.xsp index b937fe6e8..7565de474 100644 --- a/xs/xsp/Print.xsp +++ b/xs/xsp/Print.xsp @@ -215,8 +215,7 @@ _constant() bool has_infinite_skirt(); bool has_skirt(); std::vector extruders() const; - std::string _validate() - %code%{ RETVAL = THIS->validate(); %}; + void validate() %code%{ std::string err = THIS->validate(); if (! err.empty()) throw std::exception(err.c_str()); %}; Clone bounding_box(); Clone total_bounding_box(); double skirt_first_layer_height();