Add 3mf Config by cereal

This commit is contained in:
Filip Sykala 2021-09-21 10:27:38 +02:00
parent b0ed881bd4
commit b975fe84ed
4 changed files with 100 additions and 0 deletions

View file

@ -208,6 +208,8 @@ add_library(libslic3r STATIC
Tesselate.cpp
Tesselate.hpp
TextConfiguration.hpp
TextConfigurationSerialization.cpp
TextConfigurationSerialization.hpp
TriangleMesh.cpp
TriangleMesh.hpp
TriangleMeshSlicer.cpp

View file

@ -32,6 +32,8 @@ namespace pt = boost::property_tree;
#include <Eigen/Dense>
#include "miniz_extension.hpp"
#include "TextConfigurationSerialization.hpp"
// Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter,
// https://github.com/boostorg/spirit/pull/586
// where the exported string is one digit shorter than it should be to guarantee lossless round trip.
@ -2941,6 +2943,10 @@ namespace Slic3r {
stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << VOLUME_TYPE_KEY << "\" " <<
VALUE_ATTR << "=\"" << ModelVolume::type_to_string(volume->type()) << "\"/>\n";
// stores volume's text data
if (volume->text_configuration.has_value())
stream << " " << TextConfigurationSerialization::to_string(*volume->text_configuration) << "\n";
// stores volume's local matrix
stream << " <" << METADATA_TAG << " " << TYPE_ATTR << "=\"" << VOLUME_TYPE << "\" " << KEY_ATTR << "=\"" << MATRIX_KEY << "\" " << VALUE_ATTR << "=\"";
Transform3d matrix = volume->get_matrix() * volume->source.transform.get_matrix();

View file

@ -0,0 +1,68 @@
#include "TextConfigurationSerialization.hpp"
#include <cereal/archives/xml.hpp>
#include <strstream>
using namespace Slic3r;
const std::string TextConfigurationSerialization::font_item = "FontItem";
const std::string TextConfigurationSerialization::font_prop = "FontProp";
const std::string TextConfigurationSerialization::text = "Text";
namespace cereal {
template<class Archive>
void serialize(Archive &archive, FontItem &font_item)
{
archive(
CEREAL_NVP(font_item.name),
CEREAL_NVP(font_item.path)
//,CEREAL_NVP(font_item.type)
);
}
template<class Archive>
void serialize(Archive &archive, FontProp &font_prop)
{
archive(
CEREAL_NVP(font_prop.char_gap),
CEREAL_NVP(font_prop.line_gap),
CEREAL_NVP(font_prop.flatness),
CEREAL_NVP(font_prop.size_in_mm),
CEREAL_NVP(font_prop.emboss)
);
}
template<class Archive>
void serialize(Archive &archive, TextConfiguration &text_configuration)
{
archive(CEREAL_NVP(text_configuration.font_item),
CEREAL_NVP(text_configuration.font_prop),
CEREAL_NVP(text_configuration.text));
}
} // namespace cereal
std::string TextConfigurationSerialization::to_string(
const TextConfiguration &text_configuration)
{
std::strstream ss;
{
cereal::XMLOutputArchive archive(ss);
// CEREAL_NVP - Names the output the same as the variable name
archive(CEREAL_NVP(text_configuration));
}
std::string result = ss.str();
static size_t start = std::string("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<cereal>\n\t").size();
size_t end = result.find("\n</cereal>", start);
result = result.substr(start, end - start);
return result;
}
std::optional<TextConfiguration> TextConfigurationSerialization::from_string(const std::string &data)
{
std::strstream ss;
ss << data;
cereal::XMLInputArchive archive(ss);
TextConfiguration tc;
archive(tc);
return tc;
}

View file

@ -0,0 +1,24 @@
#ifndef slic3r_TextConfigurationSerialization_hpp_
#define slic3r_TextConfigurationSerialization_hpp_
#include "TextConfiguration.hpp"
#include <optional>
namespace Slic3r {
// utility for convert TextConfiguration into string and vice versa
class TextConfigurationSerialization
{
public:
TextConfigurationSerialization() = delete; // only static functions
static std::string to_string(const TextConfiguration &text_configuration);
static std::optional<TextConfiguration> from_string(const std::string &data);
private:
static const std::string font_item;
static const std::string font_prop;
static const std::string text;
};
} // namespace Slic3r
#endif // slic3r_TextConfigurationSerialization_hpp_