Merge branch 'xs-config'

Conflicts:
	lib/Slic3r/Config.pm
	xs/MANIFEST
xs-bb
Alessandro Ranellucci 2013-12-22 01:41:55 +01:00
commit ab25cc4940
19 changed files with 2370 additions and 1235 deletions

View File

@ -133,6 +133,8 @@ sub parallelize {
sub thread_cleanup {
# prevent destruction of shared objects
no warnings 'redefine';
*Slic3r::Config::DESTROY = sub {};
*Slic3r::Config::Print::DESTROY = sub {};
*Slic3r::ExPolygon::DESTROY = sub {};
*Slic3r::ExPolygon::Collection::DESTROY = sub {};
*Slic3r::ExtrusionLoop::DESTROY = sub {};

File diff suppressed because it is too large Load Diff

View File

@ -381,9 +381,17 @@ sub _set_config {
my ($opt_key, $index, $value) = @_;
my ($get_m, $serialized) = $self->_config_methods($opt_key, $index);
defined $index
? $self->config->$get_m($opt_key)->[$index] = $value
: $self->config->set($opt_key, $value, $serialized);
if (defined $index) {
my $values = $self->config->$get_m($opt_key);
$values->[$index] = $value;
$self->config->set($opt_key, $values);
} else {
if ($serialized) {
$self->config->set_deserialize($opt_key, $value);
} else {
$self->config->set($opt_key, $value);
}
}
}
sub _config_methods {

View File

@ -733,6 +733,9 @@ sub export_gcode {
catch_error => sub { Slic3r::GUI::catch_error($self, @_) && $self->on_export_failed },
);
}
# this method gets executed in a separate thread by wxWidgets since it's a button handler
Slic3r::thread_cleanup() if $Slic3r::have_threads;
}
sub export_gcode2 {
@ -802,6 +805,9 @@ sub export_stl {
my $output_file = $self->_get_export_file('STL') or return;
Slic3r::Format::STL->write_file($output_file, $self->{model}, binary => 1);
$self->statusbar->SetStatusText("STL file exported to $output_file");
# this method gets executed in a separate thread by wxWidgets since it's a button handler
Slic3r::thread_cleanup() if $Slic3r::have_threads;
}
sub export_amf {
@ -810,6 +816,9 @@ sub export_amf {
my $output_file = $self->_get_export_file('AMF') or return;
Slic3r::Format::AMF->write_file($output_file, $self->{model});
$self->statusbar->SetStatusText("AMF file exported to $output_file");
# this method gets executed in a separate thread by wxWidgets since it's a menu handler
Slic3r::thread_cleanup() if $Slic3r::have_threads;
}
sub _get_export_file {
@ -853,7 +862,9 @@ sub make_thumbnail {
};
@_ = ();
$Slic3r::have_threads ? threads->create($cb)->detach : $cb->();
$Slic3r::have_threads
? threads->create(sub { $cb->(); Slic3r::thread_cleanup(); })->detach
: $cb->();
}
sub on_thumbnail_made {

View File

@ -312,7 +312,7 @@ sub load_config {
my ($config) = @_;
foreach my $tab (values %{$self->{options_tabs}}) {
$tab->set_value($_, $config->$_) for keys %$config;
$tab->set_value($_, $config->$_) for @{$config->get_keys};
}
}

View File

@ -291,7 +291,8 @@ sub set_value {
sub reload_values {
my $self = shift;
$self->set_value($_, $self->{config}->get($_)) for keys %{$self->{config}};
$self->set_value($_, $self->{config}->get($_))
for @{$self->{config}->get_keys};
}
sub update_tree {
@ -735,7 +736,9 @@ sub config {
# remove all unused values
foreach my $opt_key ($self->_extruder_options) {
splice @{ $config->{$opt_key} }, $self->{extruders_count};
my $values = $config->get($opt_key);
splice @$values, $self->{extruders_count};
$config->set($opt_key, $values);
}
return $config;

View File

@ -925,13 +925,13 @@ sub write_gcode {
$extruder->absolute_E, $extruder->extruded_volume/1000;
}
if ($Slic3r::Config->gcode_comments) {
if ($self->config->gcode_comments) {
# append full config
print $fh "\n";
foreach my $opt_key (sort keys %{$Slic3r::Config}) {
foreach my $opt_key (sort @{$self->config->get_keys}) {
next if $Slic3r::Config::Options->{$opt_key}{shortcut};
next if $Slic3r::Config::Options->{$opt_key}{gui_only};
printf $fh "; %s = %s\n", $opt_key, $Slic3r::Config->serialize($opt_key);
printf $fh "; %s = %s\n", $opt_key, $self->config->serialize($opt_key);
}
}

View File

@ -187,7 +187,7 @@ if (@ARGV) { # slicing from command line
sub usage {
my ($exit_code) = @_;
my $config = Slic3r::Config->new_from_defaults;
my $config = Slic3r::Config->new_from_defaults->as_hash;
my $j = '';
if ($Slic3r::have_threads) {

View File

@ -12,6 +12,8 @@ src/clipper.cpp
src/clipper.hpp
src/ClipperUtils.cpp
src/ClipperUtils.hpp
src/Config.cpp
src/Config.hpp
src/ExPolygon.cpp
src/ExPolygon.hpp
src/ExPolygonCollection.cpp
@ -35,6 +37,8 @@ src/Polyline.cpp
src/Polyline.hpp
src/PolylineCollection.cpp
src/PolylineCollection.hpp
src/PrintConfig.cpp
src/PrintConfig.hpp
src/PrintState.cpp
src/PrintState.hpp
src/ppport.h
@ -60,7 +64,9 @@ t/11_clipper.t
t/12_extrusionpathcollection.t
t/13_polylinecollection.t
t/14_geometry.t
t/15_config.t
xsp/Clipper.xsp
xsp/Config.xsp
xsp/ExPolygon.xsp
xsp/ExPolygonCollection.xsp
xsp/ExtrusionEntityCollection.xsp

257
xs/src/Config.cpp Normal file
View File

@ -0,0 +1,257 @@
#include "Config.hpp"
namespace Slic3r {
bool
ConfigBase::has(const t_config_option_key opt_key) {
return (this->option(opt_key, false) != NULL);
}
void
ConfigBase::apply(ConfigBase &other, bool ignore_nonexistent) {
// get list of option keys to apply
t_config_option_keys opt_keys;
other.keys(&opt_keys);
// loop through options and apply them
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
ConfigOption* my_opt = this->option(*it, true);
if (my_opt == NULL) {
if (ignore_nonexistent == false) throw "Attempt to apply non-existent option";
continue;
}
// not the most efficient way, but easier than casting pointers to subclasses
my_opt->deserialize( other.option(*it)->serialize() );
}
}
std::string
ConfigBase::serialize(const t_config_option_key opt_key) {
ConfigOption* opt = this->option(opt_key);
assert(opt != NULL);
return opt->serialize();
}
void
ConfigBase::set_deserialize(const t_config_option_key opt_key, std::string str) {
ConfigOption* opt = this->option(opt_key);
assert(opt != NULL);
opt->deserialize(str);
}
double
ConfigBase::get_abs_value(const t_config_option_key opt_key) {
// get option definition
assert(this->def->count(opt_key) != 0);
ConfigOptionDef* def = &(*this->def)[opt_key];
assert(def->type == coFloatOrPercent);
// get stored option value
ConfigOptionFloatOrPercent* opt = dynamic_cast<ConfigOptionFloatOrPercent*>(this->option(opt_key));
assert(opt != NULL);
// compute absolute value
if (opt->percent) {
ConfigOptionFloat* optbase = dynamic_cast<ConfigOptionFloat*>(this->option(def->ratio_over));
if (optbase == NULL) throw "ratio_over option not found";
return optbase->value * opt->value / 100;
} else {
return opt->value;
}
}
#ifdef SLIC3RXS
SV*
ConfigBase::as_hash() {
HV* hv = newHV();
t_config_option_keys opt_keys;
this->keys(&opt_keys);
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it)
(void)hv_store( hv, it->c_str(), it->length(), this->get(*it), 0 );
return newRV_noinc((SV*)hv);
}
SV*
ConfigBase::get(t_config_option_key opt_key) {
ConfigOption* opt = this->option(opt_key);
if (opt == NULL) return &PL_sv_undef;
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
return newSVnv(optv->value);
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<double>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSVnv(*it));
return newRV_noinc((SV*)av);
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
return newSViv(optv->value);
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<int>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSViv(*it));
return newRV_noinc((SV*)av);
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
// we don't serialize() because that would escape newlines
return newSVpvn(optv->value.c_str(), optv->value.length());
} else if (ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<std::string>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSVpvn(it->c_str(), it->length()));
return newRV_noinc((SV*)av);
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
return optv->point.to_SV_pureperl();
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
AV* av = newAV();
av_fill(av, optv->points.size()-1);
for (Pointfs::iterator it = optv->points.begin(); it != optv->points.end(); ++it)
av_store(av, it - optv->points.begin(), it->to_SV_pureperl());
return newRV_noinc((SV*)av);
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
return newSViv(optv->value ? 1 : 0);
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<bool>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSViv(*it ? 1 : 0));
return newRV_noinc((SV*)av);
} else {
std::string serialized = opt->serialize();
return newSVpvn(serialized.c_str(), serialized.length());
}
}
void
ConfigBase::set(t_config_option_key opt_key, SV* value) {
ConfigOption* opt = this->option(opt_key, true);
if (opt == NULL) CONFESS("Trying to set non-existing option");
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
optv->value = SvNV(value);
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvNV(*elem));
}
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
optv->value = SvIV(value);
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvIV(*elem));
}
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
} else if (ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(std::string(SvPV_nolen(*elem), SvCUR(*elem)));
}
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
optv->point.from_SV(value);
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
optv->points.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
Pointf point;
point.from_SV(*elem);
optv->points.push_back(point);
}
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
optv->value = SvTRUE(value);
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvTRUE(*elem));
}
} else {
opt->deserialize( std::string(SvPV_nolen(value)) );
}
}
#endif
DynamicConfig::~DynamicConfig () {
for (t_options_map::iterator it = this->options.begin(); it != this->options.end(); ++it) {
ConfigOption* opt = it->second;
if (opt != NULL) delete opt;
}
}
ConfigOption*
DynamicConfig::option(const t_config_option_key opt_key, bool create) {
if (this->options.count(opt_key) == 0) {
if (create) {
ConfigOptionDef* optdef = &(*this->def)[opt_key];
ConfigOption* opt;
if (optdef->type == coFloat) {
opt = new ConfigOptionFloat ();
} else if (optdef->type == coFloats) {
opt = new ConfigOptionFloats ();
} else if (optdef->type == coInt) {
opt = new ConfigOptionInt ();
} else if (optdef->type == coInts) {
opt = new ConfigOptionInts ();
} else if (optdef->type == coString) {
opt = new ConfigOptionString ();
} else if (optdef->type == coStrings) {
opt = new ConfigOptionStrings ();
} else if (optdef->type == coFloatOrPercent) {
opt = new ConfigOptionFloatOrPercent ();
} else if (optdef->type == coPoint) {
opt = new ConfigOptionPoint ();
} else if (optdef->type == coPoints) {
opt = new ConfigOptionPoints ();
} else if (optdef->type == coBool) {
opt = new ConfigOptionBool ();
} else if (optdef->type == coBools) {
opt = new ConfigOptionBools ();
} else if (optdef->type == coEnum) {
ConfigOptionEnumGeneric* optv = new ConfigOptionEnumGeneric ();
optv->keys_map = &optdef->enum_keys_map;
opt = static_cast<ConfigOption*>(optv);
} else {
throw "Unknown option type";
}
this->options[opt_key] = opt;
return opt;
} else {
return NULL;
}
}
return this->options[opt_key];
}
void
DynamicConfig::keys(t_config_option_keys *keys) {
for (t_options_map::const_iterator it = this->options.begin(); it != this->options.end(); ++it)
keys->push_back(it->first);
}
void
StaticConfig::keys(t_config_option_keys *keys) {
for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) {
ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first);
}
}
}

427
xs/src/Config.hpp Normal file
View File

@ -0,0 +1,427 @@
#ifndef slic3r_Config_hpp_
#define slic3r_Config_hpp_
#include <myinit.h>
#include <map>
#include <sstream>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Point.hpp"
namespace Slic3r {
typedef std::string t_config_option_key;
typedef std::vector<std::string> t_config_option_keys;
class ConfigOption {
public:
virtual ~ConfigOption() {};
virtual std::string serialize() = 0;
virtual void deserialize(std::string str) = 0;
};
class ConfigOptionFloat : public ConfigOption
{
public:
double value; // use double instead of float for preserving compatibility with values coming from Perl
ConfigOptionFloat() : value(0) {};
operator double() const { return this->value; };
std::string serialize() {
std::ostringstream ss;
ss << this->value;
return ss.str();
};
void deserialize(std::string str) {
this->value = ::atof(str.c_str());
};
};
class ConfigOptionFloats : public ConfigOption
{
public:
std::vector<double> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<double>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(::atof(item_str.c_str()));
}
};
};
class ConfigOptionInt : public ConfigOption
{
public:
int value;
ConfigOptionInt() : value(0) {};
operator int() const { return this->value; };
std::string serialize() {
std::ostringstream ss;
ss << this->value;
return ss.str();
};
void deserialize(std::string str) {
this->value = ::atoi(str.c_str());
};
};
class ConfigOptionInts : public ConfigOption
{
public:
std::vector<int> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<int>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(::atoi(item_str.c_str()));
}
};
};
class ConfigOptionString : public ConfigOption
{
public:
std::string value;
ConfigOptionString() : value("") {};
operator std::string() const { return this->value; };
std::string serialize() {
std::string str = this->value;
// s/\R/\\n/g
size_t pos = 0;
while ((pos = str.find("\n", pos)) != std::string::npos || (pos = str.find("\r", pos)) != std::string::npos) {
str.replace(pos, 1, "\\n");
pos += 2; // length of "\\n"
}
return str;
};
void deserialize(std::string str) {
// s/\\n/\n/g
size_t pos = 0;
while ((pos = str.find("\\n", pos)) != std::string::npos) {
str.replace(pos, 2, "\n");
pos += 1; // length of "\n"
}
this->value = str;
};
};
// semicolon-separated strings
class ConfigOptionStrings : public ConfigOption
{
public:
std::vector<std::string> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<std::string>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ";";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ';')) {
this->values.push_back(item_str);
}
};
};
class ConfigOptionFloatOrPercent : public ConfigOption
{
public:
double value;
bool percent;
ConfigOptionFloatOrPercent() : value(0), percent(false) {};
std::string serialize() {
std::ostringstream ss;
ss << this->value;
std::string s(ss.str());
if (this->percent) s += "%";
return s;
};
void deserialize(std::string str) {
if (str.find_first_of("%") != std::string::npos) {
sscanf(str.c_str(), "%lf%%", &this->value);
this->percent = true;
} else {
this->value = ::atof(str.c_str());
this->percent = false;
}
};
};
class ConfigOptionPoint : public ConfigOption
{
public:
Pointf point;
ConfigOptionPoint() : point(Pointf(0,0)) {};
operator Pointf() const { return this->point; };
std::string serialize() {
std::ostringstream ss;
ss << this->point.x;
ss << ",";
ss << this->point.y;
return ss.str();
};
void deserialize(std::string str) {
sscanf(str.c_str(), "%lf%*1[,x]%lf", &this->point.x, &this->point.y);
};
};
class ConfigOptionPoints : public ConfigOption
{
public:
Pointfs points;
std::string serialize() {
std::ostringstream ss;
for (Pointfs::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
if (it - this->points.begin() != 0) ss << ",";
ss << it->x;
ss << "x";
ss << it->y;
}
return ss.str();
};
void deserialize(std::string str) {
this->points.clear();
std::istringstream is(str);
std::string point_str;
while (std::getline(is, point_str, ',')) {
Pointf point;
sscanf(point_str.c_str(), "%lfx%lf", &point.x, &point.y);
this->points.push_back(point);
}
};
};
class ConfigOptionBool : public ConfigOption
{
public:
bool value;
ConfigOptionBool() : value(false) {};
operator bool() const { return this->value; };
std::string serialize() {
return std::string(this->value ? "1" : "0");
};
void deserialize(std::string str) {
this->value = (str.compare("1") == 0);
};
};
class ConfigOptionBools : public ConfigOption
{
public:
std::vector<bool> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<bool>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << (*it ? "1" : "0");
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(item_str.compare("1") == 0);
}
};
};
typedef std::map<std::string,int> t_config_enum_values;
template <class T>
class ConfigOptionEnum : public ConfigOption
{
public:
T value;
operator T() const { return this->value; };
std::string serialize() {
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
for (t_config_enum_values::iterator it = enum_keys_map.begin(); it != enum_keys_map.end(); ++it) {
if (it->second == static_cast<int>(this->value)) return it->first;
}
return "";
};
void deserialize(std::string str) {
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
assert(enum_keys_map.count(str) > 0);
this->value = static_cast<T>(enum_keys_map[str]);
};
static t_config_enum_values get_enum_values();
};
/* We use this one in DynamicConfig objects, otherwise it's better to use
the specialized ConfigOptionEnum<T> containers. */
class ConfigOptionEnumGeneric : public ConfigOption
{
public:
int value;
t_config_enum_values* keys_map;
operator int() const { return this->value; };
std::string serialize() {
for (t_config_enum_values::iterator it = this->keys_map->begin(); it != this->keys_map->end(); ++it) {
if (it->second == this->value) return it->first;
}
return "";
};
void deserialize(std::string str) {
assert(this->keys_map->count(str) != 0);
this->value = (*this->keys_map)[str];
};
};
enum ConfigOptionType {
coFloat,
coFloats,
coInt,
coInts,
coString,
coStrings,
coFloatOrPercent,
coPoint,
coPoints,
coBool,
coBools,
coEnum,
};
class ConfigOptionDef
{
public:
ConfigOptionType type;
std::string label;
std::string category;
std::string tooltip;
std::string sidetext;
std::string cli;
std::string scope;
t_config_option_key ratio_over;
bool multiline;
bool full_label;
bool full_width;
bool readonly;
int height;
int width;
int min;
int max;
std::vector<t_config_option_key> aliases;
std::vector<t_config_option_key> shortcut;
std::vector<std::string> enum_values;
std::vector<std::string> enum_labels;
t_config_enum_values enum_keys_map;
ConfigOptionDef() : multiline(false), full_label(false), full_width(false), readonly(false),
height(-1), width(-1), min(INT_MIN), max(INT_MAX) {};
};
typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
class ConfigBase
{
public:
t_optiondef_map* def;
ConfigBase() : def(NULL) {};
bool has(const t_config_option_key opt_key);
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
virtual void keys(t_config_option_keys *keys) = 0;
void apply(ConfigBase &other, bool ignore_nonexistent = false);
std::string serialize(const t_config_option_key opt_key);
void set_deserialize(const t_config_option_key opt_key, std::string str);
double get_abs_value(const t_config_option_key opt_key);
#ifdef SLIC3RXS
SV* as_hash();
SV* get(t_config_option_key opt_key);
void set(t_config_option_key opt_key, SV* value);
#endif
};
class DynamicConfig : public ConfigBase
{
public:
DynamicConfig() {};
~DynamicConfig();
ConfigOption* option(const t_config_option_key opt_key, bool create = false);
void keys(t_config_option_keys *keys);
private:
DynamicConfig(const DynamicConfig& other); // we disable this by making it private and unimplemented
DynamicConfig& operator= (const DynamicConfig& other); // we disable this by making it private and unimplemented
typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
t_options_map options;
};
class StaticConfig : public ConfigBase
{
public:
void keys(t_config_option_keys *keys);
};
}
#endif

View File

@ -174,6 +174,23 @@ Point::from_SV_check(SV* point_sv)
this->from_SV(point_sv);
}
}
SV*
Pointf::to_SV_pureperl() const {
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSVnv(this->x));
av_store(av, 1, newSVnv(this->y));
return newRV_noinc((SV*)av);
}
void
Pointf::from_SV(SV* point_sv)
{
AV* point_av = (AV*)SvRV(point_sv);
this->x = SvNV(*av_fetch(point_av, 0, 0));
this->y = SvNV(*av_fetch(point_av, 1, 0));
}
#endif
}

View File

@ -9,8 +9,10 @@ namespace Slic3r {
class Line;
class Point;
class Pointf;
typedef std::vector<Point> Points;
typedef std::vector<Point*> PointPtrs;
typedef std::vector<Pointf> Pointfs;
class Point
{
@ -42,6 +44,19 @@ class Point
#endif
};
class Pointf
{
public:
double x;
double y;
explicit Pointf(double _x = 0, double _y = 0): x(_x), y(_y) {};
#ifdef SLIC3RXS
void from_SV(SV* point_sv);
SV* to_SV_pureperl() const;
#endif
};
}
#endif

7
xs/src/PrintConfig.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "PrintConfig.hpp"
namespace Slic3r {
t_optiondef_map PrintConfig::PrintConfigDef = PrintConfig::build_def();
}

1307
xs/src/PrintConfig.hpp Normal file

File diff suppressed because it is too large Load Diff

102
xs/t/15_config.t Normal file
View File

@ -0,0 +1,102 @@
#!/usr/bin/perl
use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 76;
foreach my $config (Slic3r::Config->new, Slic3r::Config::Print->new) {
$config->set('layer_height', 0.3);
ok abs($config->get('layer_height') - 0.3) < 1e-4, 'set/get float';
is $config->serialize('layer_height'), '0.3', 'serialize float';
$config->set('perimeters', 2);
is $config->get('perimeters'), 2, 'set/get int';
is $config->serialize('perimeters'), '2', 'serialize int';
$config->set('extrusion_axis', 'A');
is $config->get('extrusion_axis'), 'A', 'set/get string';
is $config->serialize('extrusion_axis'), 'A', 'serialize string';
$config->set('notes', "foo\nbar");
is $config->get('notes'), "foo\nbar", 'set/get string with newline';
is $config->serialize('notes'), 'foo\nbar', 'serialize string with newline';
$config->set_deserialize('notes', 'bar\nbaz');
is $config->get('notes'), "bar\nbaz", 'deserialize string with newline';
$config->set('first_layer_height', 0.3);
ok abs($config->get('first_layer_height') - 0.3) < 1e-4, 'set/get absolute floatOrPercent';
is $config->serialize('first_layer_height'), '0.3', 'serialize absolute floatOrPercent';
$config->set('first_layer_height', '50%');
ok abs($config->get_abs_value('first_layer_height') - 0.15) < 1e-4, 'set/get relative floatOrPercent';
is $config->serialize('first_layer_height'), '50%', 'serialize relative floatOrPercent';
$config->set('print_center', [50,80]);
is_deeply $config->get('print_center'), [50,80], 'set/get point';
is $config->serialize('print_center'), '50,80', 'serialize point';
$config->set_deserialize('print_center', '20,10');
is_deeply $config->get('print_center'), [20,10], 'deserialize point';
$config->set('use_relative_e_distances', 1);
is $config->get('use_relative_e_distances'), 1, 'set/get bool';
is $config->serialize('use_relative_e_distances'), '1', 'serialize bool';
$config->set('gcode_flavor', 'teacup');
is $config->get('gcode_flavor'), 'teacup', 'set/get enum';
is $config->serialize('gcode_flavor'), 'teacup', 'serialize enum';
$config->set_deserialize('gcode_flavor', 'mach3');
is $config->get('gcode_flavor'), 'mach3', 'deserialize enum';
$config->set('extruder_offset', [[10,20],[30,45]]);
is_deeply $config->get('extruder_offset'), [[10,20],[30,45]], 'set/get points';
is $config->serialize('extruder_offset'), '10x20,30x45', 'serialize points';
$config->set_deserialize('extruder_offset', '20x10');
is_deeply $config->get('extruder_offset'), [[20,10]], 'deserialize points';
# truncate ->get() to first decimal digit
$config->set('nozzle_diameter', [0.2,0.3]);
is_deeply [ map int($_*10)/10, @{$config->get('nozzle_diameter')} ], [0.2,0.3], 'set/get floats';
is $config->serialize('nozzle_diameter'), '0.2,0.3', 'serialize floats';
$config->set_deserialize('nozzle_diameter', '0.1,0.4');
is_deeply [ map int($_*10)/10, @{$config->get('nozzle_diameter')} ], [0.1,0.4], 'deserialize floats';
$config->set('temperature', [180,210]);
is_deeply $config->get('temperature'), [180,210], 'set/get ints';
is $config->serialize('temperature'), '180,210', 'serialize ints';
$config->set_deserialize('temperature', '195,220');
is_deeply $config->get('temperature'), [195,220], 'deserialize ints';
$config->set('wipe', [1,0]);
is_deeply $config->get('wipe'), [1,0], 'set/get bools';
is $config->serialize('wipe'), '1,0', 'serialize bools';
$config->set_deserialize('wipe', '0,1,1');
is_deeply $config->get('wipe'), [0,1,1], 'deserialize bools';
$config->set('post_process', ['foo','bar']);
is_deeply $config->get('post_process'), ['foo','bar'], 'set/get strings';
is $config->serialize('post_process'), 'foo;bar', 'serialize strings';
$config->set_deserialize('post_process', 'bar;baz');
is_deeply $config->get('post_process'), ['bar','baz'], 'deserialize strings';
is_deeply [ sort @{$config->get_keys} ], [ sort keys %{$config->as_hash} ], 'get_keys and as_hash';
}
{
my $config = Slic3r::Config->new;
$config->set('perimeters', 2);
my $config2 = Slic3r::Config::Print->new;
$config2->apply_dynamic($config);
is $config2->get('perimeters'), 2, 'apply_dynamic';
}
{
my $config = Slic3r::Config::Print->new;
my $config2 = Slic3r::Config->new;
$config2->apply_static($config);
is $config2->get('perimeters'), Slic3r::Config::print_config_def()->{perimeters}{default}, 'apply_static and print_config_def';
}
__END__

144
xs/xsp/Config.xsp Normal file
View File

@ -0,0 +1,144 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "PrintConfig.hpp"
%}
%name{Slic3r::Config} class DynamicPrintConfig {
DynamicPrintConfig();
~DynamicPrintConfig();
bool has(t_config_option_key opt_key);
SV* as_hash();
SV* get(t_config_option_key opt_key);
void set(t_config_option_key opt_key, SV* value);
void set_deserialize(t_config_option_key opt_key, std::string str);
std::string serialize(t_config_option_key opt_key);
double get_abs_value(t_config_option_key opt_key);
void apply(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
void apply_static(PrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%{
%}
};
%name{Slic3r::Config::Print} class PrintConfig {
PrintConfig();
~PrintConfig();
bool has(t_config_option_key opt_key);
SV* as_hash();
SV* get(t_config_option_key opt_key);
void set(t_config_option_key opt_key, SV* value);
void set_deserialize(t_config_option_key opt_key, std::string str);
std::string serialize(t_config_option_key opt_key);
double get_abs_value(t_config_option_key opt_key);
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%{
%}
};
%package{Slic3r::Config};
%{
PROTOTYPES: DISABLE
SV*
print_config_def()
CODE:
PrintConfig config;
t_optiondef_map* def = config.def;
HV* options_hv = newHV();
for (t_optiondef_map::iterator oit = def->begin(); oit != def->end(); ++oit) {
HV* hv = newHV();
t_config_option_key opt_key = oit->first;
ConfigOptionDef* optdef = &oit->second;
const char* opt_type;
if (optdef->type == coFloat || optdef->type == coFloats || optdef->type == coFloatOrPercent) {
opt_type = "f";
} else if (optdef->type == coInt || optdef->type == coInts) {
opt_type = "i";
} else if (optdef->type == coString) {
opt_type = "s";
} else if (optdef->type == coStrings) {
opt_type = "s@";
} else if (optdef->type == coPoint || optdef->type == coPoints) {
opt_type = "point";
} else if (optdef->type == coBool || optdef->type == coBools) {
opt_type = "bool";
} else if (optdef->type == coEnum) {
opt_type = "select";
} else {
throw "Unknown option type";
}
(void)hv_stores( hv, "type", newSVpv(opt_type, 0) );
(void)hv_stores( hv, "label", newSVpvn(optdef->label.c_str(), optdef->label.length()) );
(void)hv_stores( hv, "category", newSVpvn(optdef->category.c_str(), optdef->category.length()) );
(void)hv_stores( hv, "tooltip", newSVpvn(optdef->tooltip.c_str(), optdef->tooltip.length()) );
(void)hv_stores( hv, "sidetext", newSVpvn(optdef->sidetext.c_str(), optdef->sidetext.length()) );
(void)hv_stores( hv, "cli", newSVpvn(optdef->cli.c_str(), optdef->cli.length()) );
(void)hv_stores( hv, "scope", newSVpvn(optdef->scope.c_str(), optdef->scope.length()) );
(void)hv_stores( hv, "ratio_over", newSVpvn(optdef->ratio_over.c_str(), optdef->ratio_over.length()) );
(void)hv_stores( hv, "multiline", newSViv(optdef->multiline ? 1 : 0) );
(void)hv_stores( hv, "full_label", newSViv(optdef->full_label ? 1 : 0) );
(void)hv_stores( hv, "full_width", newSViv(optdef->full_width ? 1 : 0) );
(void)hv_stores( hv, "readonly", newSViv(optdef->readonly ? 1 : 0) );
(void)hv_stores( hv, "height", newSViv(optdef->height) );
(void)hv_stores( hv, "width", newSViv(optdef->width) );
(void)hv_stores( hv, "min", newSViv(optdef->min) );
(void)hv_stores( hv, "max", newSViv(optdef->max) );
// aliases
{
AV* av = newAV();
av_fill(av, optdef->aliases.size()-1);
for (std::vector<t_config_option_key>::iterator it = optdef->aliases.begin(); it != optdef->aliases.end(); ++it)
av_store(av, it - optdef->aliases.begin(), newSVpvn(it->c_str(), it->length()));
(void)hv_stores( hv, "aliases", newRV_noinc((SV*)av) );
}
// shortcut
{
AV* av = newAV();
av_fill(av, optdef->shortcut.size()-1);
for (std::vector<t_config_option_key>::iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it)
av_store(av, it - optdef->shortcut.begin(), newSVpvn(it->c_str(), it->length()));
(void)hv_stores( hv, "shortcut", newRV_noinc((SV*)av) );
}
// enum_values
{
AV* av = newAV();
av_fill(av, optdef->enum_values.size()-1);
for (std::vector<std::string>::iterator it = optdef->enum_values.begin(); it != optdef->enum_values.end(); ++it)
av_store(av, it - optdef->enum_values.begin(), newSVpvn(it->c_str(), it->length()));
(void)hv_stores( hv, "values", newRV_noinc((SV*)av) );
}
// enum_labels
{
AV* av = newAV();
av_fill(av, optdef->enum_labels.size()-1);
for (std::vector<std::string>::iterator it = optdef->enum_labels.begin(); it != optdef->enum_labels.end(); ++it)
av_store(av, it - optdef->enum_labels.begin(), newSVpvn(it->c_str(), it->length()));
(void)hv_stores( hv, "labels", newRV_noinc((SV*)av) );
}
(void)hv_stores( hv, "default", config.get(opt_key) );
(void)hv_store( options_hv, opt_key.c_str(), opt_key.length(), newRV_noinc((SV*)hv), 0 );
}
RETVAL = newRV_noinc((SV*)options_hv);
OUTPUT:
RETVAL
%}

View File

@ -1,5 +1,8 @@
std::vector<Points::size_type> T_STD_VECTOR_INT
t_config_option_key T_STD_STRING
DynamicPrintConfig* O_OBJECT
PrintConfig* O_OBJECT
ZTable* O_OBJECT
TriangleMesh* O_OBJECT
Point* O_OBJECT

View File

@ -1,11 +1,15 @@
%typemap{bool}{simple};
%typemap{std::string};
%typemap{t_config_option_key};
%typemap{std::vector<unsigned int>*};
%typemap{std::vector<double>};
%typemap{std::vector<double>*};
%typemap{std::vector<std::string>};
%typemap{SV*};
%typemap{AV*};
%typemap{Point*};
%typemap{DynamicPrintConfig*};
%typemap{PrintConfig*};
%typemap{ExPolygon*};
%typemap{ExPolygonCollection*};
%typemap{Line*};