Support bool options in XS
This commit is contained in:
parent
64e549a46f
commit
f274287b0e
@ -75,6 +75,8 @@ ConfigBase::get(t_config_option_key opt_key) {
|
|||||||
return newSVpvn(optv->value.c_str(), optv->value.length());
|
return newSVpvn(optv->value.c_str(), optv->value.length());
|
||||||
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
||||||
return optv->point.to_SV_pureperl();
|
return optv->point.to_SV_pureperl();
|
||||||
|
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
|
||||||
|
return newSViv(optv->value ? 1 : 0);
|
||||||
} else {
|
} else {
|
||||||
std::string serialized = opt->serialize();
|
std::string serialized = opt->serialize();
|
||||||
return newSVpvn(serialized.c_str(), serialized.length());
|
return newSVpvn(serialized.c_str(), serialized.length());
|
||||||
@ -94,6 +96,8 @@ ConfigBase::set(t_config_option_key opt_key, SV* value) {
|
|||||||
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
|
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
|
||||||
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
||||||
optv->point.from_SV(value);
|
optv->point.from_SV(value);
|
||||||
|
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
|
||||||
|
optv->value = SvTRUE(value);
|
||||||
} else {
|
} else {
|
||||||
opt->deserialize( std::string(SvPV_nolen(value)) );
|
opt->deserialize( std::string(SvPV_nolen(value)) );
|
||||||
}
|
}
|
||||||
@ -123,6 +127,8 @@ DynamicConfig::option(const t_config_option_key opt_key, bool create) {
|
|||||||
opt = new ConfigOptionFloatOrPercent ();
|
opt = new ConfigOptionFloatOrPercent ();
|
||||||
} else if (Options[opt_key].type == coPoint) {
|
} else if (Options[opt_key].type == coPoint) {
|
||||||
opt = new ConfigOptionPoint ();
|
opt = new ConfigOptionPoint ();
|
||||||
|
} else if (Options[opt_key].type == coBool) {
|
||||||
|
opt = new ConfigOptionBool ();
|
||||||
} else {
|
} else {
|
||||||
throw "Unknown option type";
|
throw "Unknown option type";
|
||||||
}
|
}
|
||||||
|
@ -126,6 +126,8 @@ class ConfigOptionPoint : public ConfigOption
|
|||||||
Pointf point;
|
Pointf point;
|
||||||
ConfigOptionPoint() : point(Pointf(0,0)) {};
|
ConfigOptionPoint() : point(Pointf(0,0)) {};
|
||||||
|
|
||||||
|
operator Pointf() const { return this->point; };
|
||||||
|
|
||||||
std::string serialize() {
|
std::string serialize() {
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss << this->point.x;
|
ss << this->point.x;
|
||||||
@ -139,12 +141,62 @@ class ConfigOptionPoint : public ConfigOption
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
enum GCodeFlavor {
|
||||||
|
gcfRepRap, gcfTeacup, gcfMakerWare, gcfSailfish, gcfMach3, gcfNoExtrusion,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ConfigOptionGCodeFlavor : public ConfigOption
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GCodeFlavor value;
|
||||||
|
ConfigOptionGCodeFlavor() : value(gcfRepRap) {};
|
||||||
|
|
||||||
|
operator GCodeFlavor() const { return this->value; };
|
||||||
|
|
||||||
|
std::string serialize() {
|
||||||
|
if (this->value == gfcRepRap) { return std::string("reprap"); }
|
||||||
|
else if (this->value == gcfTeacup) { return std::string("teacup"); }
|
||||||
|
else if (this->value == gcfMakerWare) { return std::string("makerware"); }
|
||||||
|
else if (this->value == gcfSailfish) { return std::string("sailfish"); }
|
||||||
|
else if (this->value == gcfMach3) { return std::string("mach3"); }
|
||||||
|
else if (this->value == gcfNoExtrusion) { return std::string("no-extrusion"); }
|
||||||
|
};
|
||||||
|
|
||||||
|
void deserialize(std::string str) {
|
||||||
|
if (str.compare("reprap") == 0) { this->value = gfcRepRap; }
|
||||||
|
else if (str.compare("teacup") == 0) { this->value = gcfTeacup; }
|
||||||
|
else if (str.compare("makerware") == 0) { this->value = gcfMakerWare; }
|
||||||
|
else if (str.compare("sailfish") == 0) { this->value = gcfSailfish; }
|
||||||
|
else if (str.compare("mach3") == 0) { this->value = gcfMach3; }
|
||||||
|
else if (str.compare("no-extrusion") == 0) { this->value = gcfNoExtrusion; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
enum ConfigOptionType {
|
enum ConfigOptionType {
|
||||||
coFloat,
|
coFloat,
|
||||||
coInt,
|
coInt,
|
||||||
coString,
|
coString,
|
||||||
coFloatOrPercent,
|
coFloatOrPercent,
|
||||||
coPoint,
|
coPoint,
|
||||||
|
coBool,
|
||||||
|
coGCodeFlavor,
|
||||||
};
|
};
|
||||||
|
|
||||||
class ConfigOptionDef
|
class ConfigOptionDef
|
||||||
@ -201,12 +253,13 @@ class StaticConfig : public ConfigBase
|
|||||||
class FullConfig : public StaticConfig
|
class FullConfig : public StaticConfig
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ConfigOptionFloat layer_height;
|
ConfigOptionFloat layer_height;
|
||||||
ConfigOptionFloatOrPercent first_layer_height;
|
ConfigOptionFloatOrPercent first_layer_height;
|
||||||
ConfigOptionInt perimeters;
|
ConfigOptionInt perimeters;
|
||||||
ConfigOptionString extrusion_axis;
|
ConfigOptionString extrusion_axis;
|
||||||
ConfigOptionPoint print_center;
|
ConfigOptionPoint print_center;
|
||||||
ConfigOptionString notes;
|
ConfigOptionString notes;
|
||||||
|
ConfigOptionBool use_relative_e_distances;
|
||||||
|
|
||||||
ConfigOption* option(const t_config_option_key opt_key, bool create = false) {
|
ConfigOption* option(const t_config_option_key opt_key, bool create = false) {
|
||||||
assert(!create); // can't create options in StaticConfig
|
assert(!create); // can't create options in StaticConfig
|
||||||
@ -216,6 +269,7 @@ class FullConfig : public StaticConfig
|
|||||||
if (opt_key == "extrusion_axis") return &this->extrusion_axis;
|
if (opt_key == "extrusion_axis") return &this->extrusion_axis;
|
||||||
if (opt_key == "print_center") return &this->print_center;
|
if (opt_key == "print_center") return &this->print_center;
|
||||||
if (opt_key == "notes") return &this->notes;
|
if (opt_key == "notes") return &this->notes;
|
||||||
|
if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances;
|
||||||
return NULL;
|
return NULL;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -239,19 +293,22 @@ static t_optiondef_map _build_optiondef_map () {
|
|||||||
|
|
||||||
Options["notes"].type = coString;
|
Options["notes"].type = coString;
|
||||||
|
|
||||||
|
Options["use_relative_e_distances"].type = coBool;
|
||||||
|
|
||||||
return Options;
|
return Options;
|
||||||
}
|
}
|
||||||
|
|
||||||
static FullConfig _build_default_config () {
|
static FullConfig _build_default_config () {
|
||||||
FullConfig defconf;
|
FullConfig defconf;
|
||||||
|
|
||||||
defconf.layer_height.value = 0.4;
|
defconf.layer_height.value = 0.4;
|
||||||
defconf.first_layer_height.value = 0.35;
|
defconf.first_layer_height.value = 0.35;
|
||||||
defconf.first_layer_height.percent = false;
|
defconf.first_layer_height.percent = false;
|
||||||
defconf.perimeters.value = 3;
|
defconf.perimeters.value = 3;
|
||||||
defconf.extrusion_axis.value = "E";
|
defconf.extrusion_axis.value = "E";
|
||||||
defconf.print_center.point = Pointf(100,100);
|
defconf.print_center.point = Pointf(100,100);
|
||||||
defconf.notes.value = "";
|
defconf.notes.value = "";
|
||||||
|
defconf.use_relative_e_distances.value = false;
|
||||||
|
|
||||||
return defconf;
|
return defconf;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use Slic3r::XS;
|
use Slic3r::XS;
|
||||||
use Test::More tests => 16;
|
use Test::More tests => 18;
|
||||||
|
|
||||||
{
|
{
|
||||||
my $config = Slic3r::Config->new;
|
my $config = Slic3r::Config->new;
|
||||||
@ -40,6 +40,10 @@ use Test::More tests => 16;
|
|||||||
is $config->serialize('print_center'), '50,80', 'serialize point';
|
is $config->serialize('print_center'), '50,80', 'serialize point';
|
||||||
$config->set_deserialize('print_center', '20,10');
|
$config->set_deserialize('print_center', '20,10');
|
||||||
is_deeply $config->get('print_center'), [20,10], 'deserialize point';
|
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';
|
||||||
}
|
}
|
||||||
|
|
||||||
__END__
|
__END__
|
||||||
|
Loading…
Reference in New Issue
Block a user