mirror of
https://git.mirrors.martin98.com/https://github.com/slic3r/Slic3r.git
synced 2025-07-13 05:01:47 +08:00
Refactoring in PlaceholderParser
This commit is contained in:
parent
a16dda0885
commit
bf541a1fed
@ -26,8 +26,14 @@ class ConfigOption {
|
||||
virtual void setInt(int val) {};
|
||||
};
|
||||
|
||||
class ConfigOptionVectorBase : public ConfigOption {
|
||||
public:
|
||||
virtual ~ConfigOptionVectorBase() {};
|
||||
virtual std::vector<std::string> vserialize() const = 0;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class ConfigOptionVector
|
||||
class ConfigOptionVector : public ConfigOptionVectorBase
|
||||
{
|
||||
public:
|
||||
virtual ~ConfigOptionVector() {};
|
||||
@ -63,7 +69,7 @@ class ConfigOptionFloat : public ConfigOption
|
||||
};
|
||||
};
|
||||
|
||||
class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector<double>
|
||||
class ConfigOptionFloats : public ConfigOptionVector<double>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -76,6 +82,16 @@ class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector<double
|
||||
return ss.str();
|
||||
};
|
||||
|
||||
std::vector<std::string> vserialize() const {
|
||||
std::vector<std::string> vv;
|
||||
for (std::vector<double>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||
std::ostringstream ss;
|
||||
ss << *it;
|
||||
vv.push_back(ss.str());
|
||||
}
|
||||
return vv;
|
||||
};
|
||||
|
||||
bool deserialize(std::string str) {
|
||||
this->values.clear();
|
||||
std::istringstream is(str);
|
||||
@ -112,7 +128,7 @@ class ConfigOptionInt : public ConfigOption
|
||||
};
|
||||
};
|
||||
|
||||
class ConfigOptionInts : public ConfigOption, public ConfigOptionVector<int>
|
||||
class ConfigOptionInts : public ConfigOptionVector<int>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -125,6 +141,16 @@ class ConfigOptionInts : public ConfigOption, public ConfigOptionVector<int>
|
||||
return ss.str();
|
||||
};
|
||||
|
||||
std::vector<std::string> vserialize() const {
|
||||
std::vector<std::string> vv;
|
||||
for (std::vector<int>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||
std::ostringstream ss;
|
||||
ss << *it;
|
||||
vv.push_back(ss.str());
|
||||
}
|
||||
return vv;
|
||||
};
|
||||
|
||||
bool deserialize(std::string str) {
|
||||
this->values.clear();
|
||||
std::istringstream is(str);
|
||||
@ -174,7 +200,7 @@ class ConfigOptionString : public ConfigOption
|
||||
};
|
||||
|
||||
// semicolon-separated strings
|
||||
class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector<std::string>
|
||||
class ConfigOptionStrings : public ConfigOptionVector<std::string>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -187,6 +213,10 @@ class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector<std::
|
||||
return ss.str();
|
||||
};
|
||||
|
||||
std::vector<std::string> vserialize() const {
|
||||
return this->values;
|
||||
};
|
||||
|
||||
bool deserialize(std::string str) {
|
||||
this->values.clear();
|
||||
std::istringstream is(str);
|
||||
@ -279,7 +309,7 @@ class ConfigOptionPoint : public ConfigOption
|
||||
};
|
||||
};
|
||||
|
||||
class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf>
|
||||
class ConfigOptionPoints : public ConfigOptionVector<Pointf>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -294,6 +324,16 @@ class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf
|
||||
return ss.str();
|
||||
};
|
||||
|
||||
std::vector<std::string> vserialize() const {
|
||||
std::vector<std::string> vv;
|
||||
for (Pointfs::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||
std::ostringstream ss;
|
||||
ss << *it;
|
||||
vv.push_back(ss.str());
|
||||
}
|
||||
return vv;
|
||||
};
|
||||
|
||||
bool deserialize(std::string str) {
|
||||
this->values.clear();
|
||||
std::istringstream is(str);
|
||||
@ -332,7 +372,7 @@ class ConfigOptionBool : public ConfigOption
|
||||
};
|
||||
};
|
||||
|
||||
class ConfigOptionBools : public ConfigOption, public ConfigOptionVector<bool>
|
||||
class ConfigOptionBools : public ConfigOptionVector<bool>
|
||||
{
|
||||
public:
|
||||
|
||||
@ -345,6 +385,16 @@ class ConfigOptionBools : public ConfigOption, public ConfigOptionVector<bool>
|
||||
return ss.str();
|
||||
};
|
||||
|
||||
std::vector<std::string> vserialize() const {
|
||||
std::vector<std::string> vv;
|
||||
for (std::vector<bool>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
|
||||
std::ostringstream ss;
|
||||
ss << (*it ? "1" : "0");
|
||||
vv.push_back(ss.str());
|
||||
}
|
||||
return vv;
|
||||
};
|
||||
|
||||
bool deserialize(std::string str) {
|
||||
this->values.clear();
|
||||
std::istringstream is(str);
|
||||
|
@ -29,22 +29,14 @@ PlaceholderParser::update_timestamp()
|
||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_hour;
|
||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_min;
|
||||
ss << std::setw(2) << std::setfill('0') << timeinfo->tm_sec;
|
||||
this->_single["timestamp"] = ss.str();
|
||||
this->set("timestamp", ss.str());
|
||||
}
|
||||
this->_single["year"] = this->_int_to_string(1900 + timeinfo->tm_year);
|
||||
this->_single["month"] = this->_int_to_string(1 + timeinfo->tm_mon);
|
||||
this->_single["day"] = this->_int_to_string(timeinfo->tm_mday);
|
||||
this->_single["hour"] = this->_int_to_string(timeinfo->tm_hour);
|
||||
this->_single["minute"] = this->_int_to_string(timeinfo->tm_min);
|
||||
this->_single["second"] = this->_int_to_string(timeinfo->tm_sec);
|
||||
}
|
||||
|
||||
std::string
|
||||
PlaceholderParser::_int_to_string(int value) const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss << value;
|
||||
return ss.str();
|
||||
this->set("year", 1900 + timeinfo->tm_year);
|
||||
this->set("month", 1 + timeinfo->tm_mon);
|
||||
this->set("day", timeinfo->tm_mday);
|
||||
this->set("hour", timeinfo->tm_hour);
|
||||
this->set("minute", timeinfo->tm_min);
|
||||
this->set("second", timeinfo->tm_sec);
|
||||
}
|
||||
|
||||
void PlaceholderParser::apply_config(DynamicPrintConfig &config)
|
||||
@ -66,53 +58,20 @@ void PlaceholderParser::apply_config(DynamicPrintConfig &config)
|
||||
i != opt_keys.end(); ++i)
|
||||
{
|
||||
const t_config_option_key &key = *i;
|
||||
|
||||
// set placeholders for options with multiple values
|
||||
const ConfigOptionDef &def = (*config.def)[key];
|
||||
switch (def.type) {
|
||||
case coFloats:
|
||||
this->set_multiple_from_vector(key,
|
||||
*(ConfigOptionFloats*)config.option(key));
|
||||
break;
|
||||
|
||||
case coInts:
|
||||
this->set_multiple_from_vector(key,
|
||||
*(ConfigOptionInts*)config.option(key));
|
||||
break;
|
||||
|
||||
case coStrings:
|
||||
this->set_multiple_from_vector(key,
|
||||
*(ConfigOptionStrings*)config.option(key));
|
||||
break;
|
||||
|
||||
case coPoints:
|
||||
this->set_multiple_from_vector(key,
|
||||
*(ConfigOptionPoints*)config.option(key));
|
||||
break;
|
||||
|
||||
case coBools:
|
||||
this->set_multiple_from_vector(key,
|
||||
*(ConfigOptionBools*)config.option(key));
|
||||
break;
|
||||
|
||||
case coPoint:
|
||||
{
|
||||
const ConfigOptionPoint &opt =
|
||||
*(ConfigOptionPoint*)config.option(key);
|
||||
|
||||
this->_single[key] = opt.serialize();
|
||||
|
||||
Pointf val = opt;
|
||||
this->_multiple[key + "_X"] = val.x;
|
||||
this->_multiple[key + "_Y"] = val.y;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
const ConfigOption* opt = config.option(key);
|
||||
|
||||
if (const ConfigOptionVectorBase* optv = dynamic_cast<const ConfigOptionVectorBase*>(opt)) {
|
||||
// set placeholders for options with multiple values
|
||||
this->set(key, optv->vserialize());
|
||||
} else if (const ConfigOptionPoint* optp = dynamic_cast<const ConfigOptionPoint*>(opt)) {
|
||||
this->_single[key] = optp->serialize();
|
||||
|
||||
Pointf val = *optp;
|
||||
this->_multiple[key + "_X"] = val.x;
|
||||
this->_multiple[key + "_Y"] = val.y;
|
||||
} else {
|
||||
// set single-value placeholders
|
||||
this->_single[key] = config.serialize(key);
|
||||
break;
|
||||
this->_single[key] = opt->serialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -121,34 +80,30 @@ void
|
||||
PlaceholderParser::set(const std::string &key, const std::string &value)
|
||||
{
|
||||
this->_single[key] = value;
|
||||
this->_multiple.erase(key);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &stm, const Pointf &pointf)
|
||||
void
|
||||
PlaceholderParser::set(const std::string &key, int value)
|
||||
{
|
||||
return stm << pointf.x << "," << pointf.y;
|
||||
std::ostringstream ss;
|
||||
ss << value;
|
||||
this->set(key, ss.str());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void PlaceholderParser::set_multiple_from_vector(const std::string &key,
|
||||
ConfigOptionVector<T> &opt)
|
||||
void
|
||||
PlaceholderParser::set(const std::string &key, const std::vector<std::string> &values)
|
||||
{
|
||||
const std::vector<T> &vals = opt.values;
|
||||
|
||||
for (size_t i = 0; i < vals.size(); ++i) {
|
||||
std::stringstream multikey_stm;
|
||||
multikey_stm << key << "_" << i;
|
||||
|
||||
std::stringstream val_stm;
|
||||
val_stm << vals[i];
|
||||
|
||||
this->_multiple[multikey_stm.str()] = val_stm.str();
|
||||
}
|
||||
|
||||
if (vals.size() > 0) {
|
||||
std::stringstream val_stm;
|
||||
val_stm << vals[0];
|
||||
this->_multiple[key] = val_stm.str();
|
||||
for (std::vector<std::string>::const_iterator v = values.begin(); v != values.end(); ++v) {
|
||||
std::stringstream ss;
|
||||
ss << key << "_" << (v - values.begin());
|
||||
|
||||
this->_multiple[ ss.str() ] = *v;
|
||||
if (v == values.begin()) {
|
||||
this->_multiple[key] = *v;
|
||||
}
|
||||
}
|
||||
this->_single.erase(key);
|
||||
}
|
||||
|
||||
#ifdef SLIC3RXS
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <myinit.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "PrintConfig.hpp"
|
||||
|
||||
|
||||
@ -20,12 +21,8 @@ class PlaceholderParser
|
||||
void update_timestamp();
|
||||
void apply_config(DynamicPrintConfig &config);
|
||||
void set(const std::string &key, const std::string &value);
|
||||
|
||||
private:
|
||||
template<class T>
|
||||
void set_multiple_from_vector(
|
||||
const std::string &key, ConfigOptionVector<T> &opt);
|
||||
std::string _int_to_string(int value) const;
|
||||
void set(const std::string &key, int value);
|
||||
void set(const std::string &key, const std::vector<std::string> &values);
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "MultiPoint.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
@ -340,6 +339,12 @@ REGISTER_CLASS(Point3, "Point3");
|
||||
|
||||
#endif
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream &stm, const Pointf &pointf)
|
||||
{
|
||||
return stm << pointf.x << "," << pointf.y;
|
||||
}
|
||||
|
||||
void
|
||||
Pointf::scale(double factor)
|
||||
{
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
@ -77,6 +78,8 @@ class Point3 : public Point
|
||||
explicit Point3(coord_t _x = 0, coord_t _y = 0, coord_t _z = 0): Point(_x, _y), z(_z) {};
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream &stm, const Pointf &pointf);
|
||||
|
||||
class Pointf
|
||||
{
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user