WIP: Moved sources int src/, separated most of the source code from Perl.

The XS was left only for the unit / integration tests, and it links
libslic3r only. No wxWidgets are allowed to be used from Perl starting
from now.
This commit is contained in:
bubnikv 2018-09-19 11:02:24 +02:00
parent 3ddaccb641
commit 0558b53493
1706 changed files with 7413 additions and 7638 deletions

2022
src/libslic3r/Format/3mf.cpp Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,19 @@
#ifndef slic3r_Format_3mf_hpp_
#define slic3r_Format_3mf_hpp_
namespace Slic3r {
class Model;
class Print;
class PresetBundle;
// Load the content of a 3mf file into the given model and preset bundle.
extern bool load_3mf(const char* path, PresetBundle* bundle, Model* model);
// Save the given model and the config data contained in the given Print into a 3mf file.
// The model could be modified during the export process if meshes are not repaired or have no shared vertices
extern bool store_3mf(const char* path, Model* model, Print* print, bool export_print_config);
}; // namespace Slic3r
#endif /* slic3r_Format_3mf_hpp_ */

View file

@ -0,0 +1,901 @@
#include <string.h>
#include <map>
#include <string>
#include <expat/expat.h>
#include <boost/nowide/cstdio.hpp>
#include "../libslic3r.h"
#include "../Model.hpp"
#include "../GCode.hpp"
#include "../Utils.hpp"
#include "../slic3r/GUI/PresetBundle.hpp"
#include "AMF.hpp"
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/nowide/fstream.hpp>
#include <miniz/miniz_zip.h>
#if 0
// Enable debugging and assert in this file.
#define DEBUG
#define _DEBUG
#undef NDEBUG
#endif
#include <assert.h>
// VERSION NUMBERS
// 0 : .amf, .amf.xml and .zip.amf files saved by older slic3r. No version definition in them.
// 1 : Introduction of amf versioning. No other change in data saved into amf files.
#if ENABLE_MODELINSTANCE_3D_OFFSET
// 2 : Added z component of offset.
const unsigned int VERSION_AMF = 2;
#else
const unsigned int VERSION_AMF = 1;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
const char* SLIC3RPE_AMF_VERSION = "slic3rpe_amf_version";
const char* SLIC3R_CONFIG_TYPE = "slic3rpe_config";
namespace Slic3r
{
struct AMFParserContext
{
AMFParserContext(XML_Parser parser, const std::string& archive_filename, PresetBundle* preset_bundle, Model *model) :
m_version(0),
m_parser(parser),
m_model(*model),
m_object(nullptr),
m_volume(nullptr),
m_material(nullptr),
m_instance(nullptr),
m_preset_bundle(preset_bundle),
m_archive_filename(archive_filename)
{
m_path.reserve(12);
}
void stop()
{
XML_StopParser(m_parser, 0);
}
void startElement(const char *name, const char **atts);
void endElement(const char *name);
void endDocument();
void characters(const XML_Char *s, int len);
static void XMLCALL startElement(void *userData, const char *name, const char **atts)
{
AMFParserContext *ctx = (AMFParserContext*)userData;
ctx->startElement(name, atts);
}
static void XMLCALL endElement(void *userData, const char *name)
{
AMFParserContext *ctx = (AMFParserContext*)userData;
ctx->endElement(name);
}
/* s is not 0 terminated. */
static void XMLCALL characters(void *userData, const XML_Char *s, int len)
{
AMFParserContext *ctx = (AMFParserContext*)userData;
ctx->characters(s, len);
}
static const char* get_attribute(const char **atts, const char *id) {
if (atts == nullptr)
return nullptr;
while (*atts != nullptr) {
if (strcmp(*(atts ++), id) == 0)
return *atts;
++ atts;
}
return nullptr;
}
enum AMFNodeType {
NODE_TYPE_INVALID = 0,
NODE_TYPE_UNKNOWN,
NODE_TYPE_AMF, // amf
// amf/metadata
NODE_TYPE_MATERIAL, // amf/material
// amf/material/metadata
NODE_TYPE_OBJECT, // amf/object
// amf/object/metadata
NODE_TYPE_MESH, // amf/object/mesh
NODE_TYPE_VERTICES, // amf/object/mesh/vertices
NODE_TYPE_VERTEX, // amf/object/mesh/vertices/vertex
NODE_TYPE_COORDINATES, // amf/object/mesh/vertices/vertex/coordinates
NODE_TYPE_COORDINATE_X, // amf/object/mesh/vertices/vertex/coordinates/x
NODE_TYPE_COORDINATE_Y, // amf/object/mesh/vertices/vertex/coordinates/y
NODE_TYPE_COORDINATE_Z, // amf/object/mesh/vertices/vertex/coordinates/z
NODE_TYPE_VOLUME, // amf/object/mesh/volume
// amf/object/mesh/volume/metadata
NODE_TYPE_TRIANGLE, // amf/object/mesh/volume/triangle
NODE_TYPE_VERTEX1, // amf/object/mesh/volume/triangle/v1
NODE_TYPE_VERTEX2, // amf/object/mesh/volume/triangle/v2
NODE_TYPE_VERTEX3, // amf/object/mesh/volume/triangle/v3
NODE_TYPE_CONSTELLATION, // amf/constellation
NODE_TYPE_INSTANCE, // amf/constellation/instance
NODE_TYPE_DELTAX, // amf/constellation/instance/deltax
NODE_TYPE_DELTAY, // amf/constellation/instance/deltay
#if ENABLE_MODELINSTANCE_3D_OFFSET
NODE_TYPE_DELTAZ, // amf/constellation/instance/deltaz
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
NODE_TYPE_RZ, // amf/constellation/instance/rz
NODE_TYPE_SCALE, // amf/constellation/instance/scale
NODE_TYPE_METADATA, // anywhere under amf/*/metadata
};
struct Instance {
#if ENABLE_MODELINSTANCE_3D_OFFSET
Instance() : deltax_set(false), deltay_set(false), deltaz_set(false), rz_set(false), scale_set(false) {}
#else
Instance() : deltax_set(false), deltay_set(false), rz_set(false), scale_set(false) {}
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
// Shift in the X axis.
float deltax;
bool deltax_set;
// Shift in the Y axis.
float deltay;
bool deltay_set;
#if ENABLE_MODELINSTANCE_3D_OFFSET
// Shift in the Z axis.
float deltaz;
bool deltaz_set;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
// Rotation around the Z axis.
float rz;
bool rz_set;
// Scaling factor
float scale;
bool scale_set;
};
struct Object {
Object() : idx(-1) {}
int idx;
std::vector<Instance> instances;
};
// Version of the amf file
unsigned int m_version;
// Current Expat XML parser instance.
XML_Parser m_parser;
// Model to receive objects extracted from an AMF file.
Model &m_model;
// Current parsing path in the XML file.
std::vector<AMFNodeType> m_path;
// Current object allocated for an amf/object XML subtree.
ModelObject *m_object;
// Map from obect name to object idx & instances.
std::map<std::string, Object> m_object_instances_map;
// Vertices parsed for the current m_object.
std::vector<float> m_object_vertices;
// Current volume allocated for an amf/object/mesh/volume subtree.
ModelVolume *m_volume;
// Faces collected for the current m_volume.
std::vector<int> m_volume_facets;
// Current material allocated for an amf/metadata subtree.
ModelMaterial *m_material;
// Current instance allocated for an amf/constellation/instance subtree.
Instance *m_instance;
// Generic string buffer for vertices, face indices, metadata etc.
std::string m_value[3];
// Pointer to preset bundle to update if config data are stored inside the amf file
PresetBundle* m_preset_bundle;
// Fullpath name of the amf file
std::string m_archive_filename;
private:
AMFParserContext& operator=(AMFParserContext&);
};
void AMFParserContext::startElement(const char *name, const char **atts)
{
AMFNodeType node_type_new = NODE_TYPE_UNKNOWN;
switch (m_path.size()) {
case 0:
// An AMF file must start with an <amf> tag.
node_type_new = NODE_TYPE_AMF;
if (strcmp(name, "amf") != 0)
this->stop();
break;
case 1:
if (strcmp(name, "metadata") == 0) {
const char *type = get_attribute(atts, "type");
if (type != nullptr) {
m_value[0] = type;
node_type_new = NODE_TYPE_METADATA;
}
} else if (strcmp(name, "material") == 0) {
const char *material_id = get_attribute(atts, "id");
m_material = m_model.add_material((material_id == nullptr) ? "_" : material_id);
node_type_new = NODE_TYPE_MATERIAL;
} else if (strcmp(name, "object") == 0) {
const char *object_id = get_attribute(atts, "id");
if (object_id == nullptr)
this->stop();
else {
assert(m_object_vertices.empty());
m_object = m_model.add_object();
m_object_instances_map[object_id].idx = int(m_model.objects.size())-1;
node_type_new = NODE_TYPE_OBJECT;
}
} else if (strcmp(name, "constellation") == 0) {
node_type_new = NODE_TYPE_CONSTELLATION;
}
break;
case 2:
if (strcmp(name, "metadata") == 0) {
if (m_path[1] == NODE_TYPE_MATERIAL || m_path[1] == NODE_TYPE_OBJECT) {
m_value[0] = get_attribute(atts, "type");
node_type_new = NODE_TYPE_METADATA;
}
} else if (strcmp(name, "mesh") == 0) {
if (m_path[1] == NODE_TYPE_OBJECT)
node_type_new = NODE_TYPE_MESH;
} else if (strcmp(name, "instance") == 0) {
if (m_path[1] == NODE_TYPE_CONSTELLATION) {
const char *object_id = get_attribute(atts, "objectid");
if (object_id == nullptr)
this->stop();
else {
m_object_instances_map[object_id].instances.push_back(AMFParserContext::Instance());
m_instance = &m_object_instances_map[object_id].instances.back();
node_type_new = NODE_TYPE_INSTANCE;
}
}
else
this->stop();
}
break;
case 3:
if (m_path[2] == NODE_TYPE_MESH) {
assert(m_object);
if (strcmp(name, "vertices") == 0)
node_type_new = NODE_TYPE_VERTICES;
else if (strcmp(name, "volume") == 0) {
assert(! m_volume);
m_volume = m_object->add_volume(TriangleMesh());
node_type_new = NODE_TYPE_VOLUME;
}
} else if (m_path[2] == NODE_TYPE_INSTANCE) {
assert(m_instance);
if (strcmp(name, "deltax") == 0)
node_type_new = NODE_TYPE_DELTAX;
else if (strcmp(name, "deltay") == 0)
node_type_new = NODE_TYPE_DELTAY;
#if ENABLE_MODELINSTANCE_3D_OFFSET
else if (strcmp(name, "deltaz") == 0)
node_type_new = NODE_TYPE_DELTAZ;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
else if (strcmp(name, "rz") == 0)
node_type_new = NODE_TYPE_RZ;
else if (strcmp(name, "scale") == 0)
node_type_new = NODE_TYPE_SCALE;
}
break;
case 4:
if (m_path[3] == NODE_TYPE_VERTICES) {
if (strcmp(name, "vertex") == 0)
node_type_new = NODE_TYPE_VERTEX;
} else if (m_path[3] == NODE_TYPE_VOLUME) {
if (strcmp(name, "metadata") == 0) {
const char *type = get_attribute(atts, "type");
if (type == nullptr)
this->stop();
else {
m_value[0] = type;
node_type_new = NODE_TYPE_METADATA;
}
} else if (strcmp(name, "triangle") == 0)
node_type_new = NODE_TYPE_TRIANGLE;
}
break;
case 5:
if (strcmp(name, "coordinates") == 0) {
if (m_path[4] == NODE_TYPE_VERTEX) {
node_type_new = NODE_TYPE_COORDINATES;
} else
this->stop();
} else if (name[0] == 'v' && name[1] >= '1' && name[1] <= '3' && name[2] == 0) {
if (m_path[4] == NODE_TYPE_TRIANGLE) {
node_type_new = AMFNodeType(NODE_TYPE_VERTEX1 + name[1] - '1');
} else
this->stop();
}
break;
case 6:
if ((name[0] == 'x' || name[0] == 'y' || name[0] == 'z') && name[1] == 0) {
if (m_path[5] == NODE_TYPE_COORDINATES)
node_type_new = AMFNodeType(NODE_TYPE_COORDINATE_X + name[0] - 'x');
else
this->stop();
}
break;
default:
break;
}
m_path.push_back(node_type_new);
}
void AMFParserContext::characters(const XML_Char *s, int len)
{
if (m_path.back() == NODE_TYPE_METADATA) {
m_value[1].append(s, len);
}
else
{
switch (m_path.size()) {
case 4:
#if ENABLE_MODELINSTANCE_3D_OFFSET
if (m_path.back() == NODE_TYPE_DELTAX ||
m_path.back() == NODE_TYPE_DELTAY ||
m_path.back() == NODE_TYPE_DELTAZ ||
m_path.back() == NODE_TYPE_RZ ||
m_path.back() == NODE_TYPE_SCALE)
#else
if (m_path.back() == NODE_TYPE_DELTAX || m_path.back() == NODE_TYPE_DELTAY || m_path.back() == NODE_TYPE_RZ || m_path.back() == NODE_TYPE_SCALE)
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
m_value[0].append(s, len);
break;
case 6:
switch (m_path.back()) {
case NODE_TYPE_VERTEX1: m_value[0].append(s, len); break;
case NODE_TYPE_VERTEX2: m_value[1].append(s, len); break;
case NODE_TYPE_VERTEX3: m_value[2].append(s, len); break;
default: break;
}
case 7:
switch (m_path.back()) {
case NODE_TYPE_COORDINATE_X: m_value[0].append(s, len); break;
case NODE_TYPE_COORDINATE_Y: m_value[1].append(s, len); break;
case NODE_TYPE_COORDINATE_Z: m_value[2].append(s, len); break;
default: break;
}
default:
break;
}
}
}
void AMFParserContext::endElement(const char * /* name */)
{
switch (m_path.back()) {
// Constellation transformation:
case NODE_TYPE_DELTAX:
assert(m_instance);
m_instance->deltax = float(atof(m_value[0].c_str()));
m_instance->deltax_set = true;
m_value[0].clear();
break;
case NODE_TYPE_DELTAY:
assert(m_instance);
m_instance->deltay = float(atof(m_value[0].c_str()));
m_instance->deltay_set = true;
m_value[0].clear();
break;
#if ENABLE_MODELINSTANCE_3D_OFFSET
case NODE_TYPE_DELTAZ:
assert(m_instance);
m_instance->deltaz = float(atof(m_value[0].c_str()));
m_instance->deltaz_set = true;
m_value[0].clear();
break;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
case NODE_TYPE_RZ:
assert(m_instance);
m_instance->rz = float(atof(m_value[0].c_str()));
m_instance->rz_set = true;
m_value[0].clear();
break;
case NODE_TYPE_SCALE:
assert(m_instance);
m_instance->scale = float(atof(m_value[0].c_str()));
m_instance->scale_set = true;
m_value[0].clear();
break;
// Object vertices:
case NODE_TYPE_VERTEX:
assert(m_object);
// Parse the vertex data
m_object_vertices.emplace_back((float)atof(m_value[0].c_str()));
m_object_vertices.emplace_back((float)atof(m_value[1].c_str()));
m_object_vertices.emplace_back((float)atof(m_value[2].c_str()));
m_value[0].clear();
m_value[1].clear();
m_value[2].clear();
break;
// Faces of the current volume:
case NODE_TYPE_TRIANGLE:
assert(m_object && m_volume);
m_volume_facets.push_back(atoi(m_value[0].c_str()));
m_volume_facets.push_back(atoi(m_value[1].c_str()));
m_volume_facets.push_back(atoi(m_value[2].c_str()));
m_value[0].clear();
m_value[1].clear();
m_value[2].clear();
break;
// Closing the current volume. Create an STL from m_volume_facets pointing to m_object_vertices.
case NODE_TYPE_VOLUME:
{
assert(m_object && m_volume);
stl_file &stl = m_volume->mesh.stl;
stl.stats.type = inmemory;
stl.stats.number_of_facets = int(m_volume_facets.size() / 3);
stl.stats.original_num_facets = stl.stats.number_of_facets;
stl_allocate(&stl);
for (size_t i = 0; i < m_volume_facets.size();) {
stl_facet &facet = stl.facet_start[i/3];
for (unsigned int v = 0; v < 3; ++ v)
memcpy(facet.vertex[v].data(), &m_object_vertices[m_volume_facets[i ++] * 3], 3 * sizeof(float));
}
stl_get_size(&stl);
m_volume->mesh.repair();
m_volume->calculate_convex_hull();
m_volume_facets.clear();
m_volume = nullptr;
break;
}
case NODE_TYPE_OBJECT:
assert(m_object);
m_object_vertices.clear();
m_object = nullptr;
break;
case NODE_TYPE_MATERIAL:
assert(m_material);
m_material = nullptr;
break;
case NODE_TYPE_INSTANCE:
assert(m_instance);
m_instance = nullptr;
break;
case NODE_TYPE_METADATA:
if ((m_preset_bundle != nullptr) && strncmp(m_value[0].c_str(), SLIC3R_CONFIG_TYPE, strlen(SLIC3R_CONFIG_TYPE)) == 0) {
m_preset_bundle->load_config_string(m_value[1].c_str(), m_archive_filename.c_str());
}
else if (strncmp(m_value[0].c_str(), "slic3r.", 7) == 0) {
const char *opt_key = m_value[0].c_str() + 7;
if (print_config_def.options.find(opt_key) != print_config_def.options.end()) {
DynamicPrintConfig *config = nullptr;
if (m_path.size() == 3) {
if (m_path[1] == NODE_TYPE_MATERIAL && m_material)
config = &m_material->config;
else if (m_path[1] == NODE_TYPE_OBJECT && m_object)
config = &m_object->config;
} else if (m_path.size() == 5 && m_path[3] == NODE_TYPE_VOLUME && m_volume)
config = &m_volume->config;
if (config)
config->set_deserialize(opt_key, m_value[1]);
} else if (m_path.size() == 3 && m_path[1] == NODE_TYPE_OBJECT && m_object && strcmp(opt_key, "layer_height_profile") == 0) {
// Parse object's layer height profile, a semicolon separated list of floats.
char *p = const_cast<char*>(m_value[1].c_str());
for (;;) {
char *end = strchr(p, ';');
if (end != nullptr)
*end = 0;
m_object->layer_height_profile.push_back(float(atof(p)));
if (end == nullptr)
break;
p = end + 1;
}
m_object->layer_height_profile_valid = true;
} else if (m_path.size() == 5 && m_path[3] == NODE_TYPE_VOLUME && m_volume) {
if (strcmp(opt_key, "modifier") == 0) {
// Is this volume a modifier volume?
// "modifier" flag comes first in the XML file, so it may be later overwritten by the "type" flag.
m_volume->set_type((atoi(m_value[1].c_str()) == 1) ? ModelVolume::PARAMETER_MODIFIER : ModelVolume::MODEL_PART);
} else if (strcmp(opt_key, "volume_type") == 0) {
m_volume->set_type(ModelVolume::type_from_string(m_value[1]));
}
}
} else if (m_path.size() == 3) {
if (m_path[1] == NODE_TYPE_MATERIAL) {
if (m_material)
m_material->attributes[m_value[0]] = m_value[1];
} else if (m_path[1] == NODE_TYPE_OBJECT) {
if (m_object && m_value[0] == "name")
m_object->name = std::move(m_value[1]);
}
} else if (m_path.size() == 5 && m_path[3] == NODE_TYPE_VOLUME) {
if (m_volume && m_value[0] == "name")
m_volume->name = std::move(m_value[1]);
}
else if (strncmp(m_value[0].c_str(), SLIC3RPE_AMF_VERSION, strlen(SLIC3RPE_AMF_VERSION)) == 0) {
m_version = (unsigned int)atoi(m_value[1].c_str());
}
m_value[0].clear();
m_value[1].clear();
break;
default:
break;
}
m_path.pop_back();
}
void AMFParserContext::endDocument()
{
for (const auto &object : m_object_instances_map) {
if (object.second.idx == -1) {
printf("Undefined object %s referenced in constellation\n", object.first.c_str());
continue;
}
for (const Instance &instance : object.second.instances)
if (instance.deltax_set && instance.deltay_set) {
ModelInstance *mi = m_model.objects[object.second.idx]->add_instance();
#if ENABLE_MODELINSTANCE_3D_OFFSET
mi->set_offset(Vec3d((double)instance.deltax, (double)instance.deltay, (double)instance.deltaz));
#else
mi->offset(0) = instance.deltax;
mi->offset(1) = instance.deltay;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
mi->rotation = instance.rz_set ? instance.rz : 0.f;
mi->scaling_factor = instance.scale_set ? instance.scale : 1.f;
}
}
}
// Load an AMF file into a provided model.
bool load_amf_file(const char *path, PresetBundle* bundle, Model *model)
{
if ((path == nullptr) || (model == nullptr))
return false;
XML_Parser parser = XML_ParserCreate(nullptr); // encoding
if (!parser) {
printf("Couldn't allocate memory for parser\n");
return false;
}
FILE *pFile = boost::nowide::fopen(path, "rt");
if (pFile == nullptr) {
printf("Cannot open file %s\n", path);
return false;
}
AMFParserContext ctx(parser, path, bundle, model);
XML_SetUserData(parser, (void*)&ctx);
XML_SetElementHandler(parser, AMFParserContext::startElement, AMFParserContext::endElement);
XML_SetCharacterDataHandler(parser, AMFParserContext::characters);
char buff[8192];
bool result = false;
for (;;) {
int len = (int)fread(buff, 1, 8192, pFile);
if (ferror(pFile)) {
printf("AMF parser: Read error\n");
break;
}
int done = feof(pFile);
if (XML_Parse(parser, buff, len, done) == XML_STATUS_ERROR) {
printf("AMF parser: Parse error at line %ul:\n%s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
break;
}
if (done) {
result = true;
break;
}
}
XML_ParserFree(parser);
::fclose(pFile);
if (result)
ctx.endDocument();
return result;
}
bool extract_model_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, const char* path, PresetBundle* bundle, Model* model, unsigned int& version)
{
if (stat.m_uncomp_size == 0)
{
printf("Found invalid size\n");
mz_zip_reader_end(&archive);
return false;
}
XML_Parser parser = XML_ParserCreate(nullptr); // encoding
if (!parser) {
printf("Couldn't allocate memory for parser\n");
mz_zip_reader_end(&archive);
return false;
}
AMFParserContext ctx(parser, path, bundle, model);
XML_SetUserData(parser, (void*)&ctx);
XML_SetElementHandler(parser, AMFParserContext::startElement, AMFParserContext::endElement);
XML_SetCharacterDataHandler(parser, AMFParserContext::characters);
void* parser_buffer = XML_GetBuffer(parser, (int)stat.m_uncomp_size);
if (parser_buffer == nullptr)
{
printf("Unable to create buffer\n");
mz_zip_reader_end(&archive);
return false;
}
mz_bool res = mz_zip_reader_extract_file_to_mem(&archive, stat.m_filename, parser_buffer, (size_t)stat.m_uncomp_size, 0);
if (res == 0)
{
printf("Error while reading model data to buffer\n");
mz_zip_reader_end(&archive);
return false;
}
if (!XML_ParseBuffer(parser, (int)stat.m_uncomp_size, 1))
{
printf("Error (%s) while parsing xml file at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
mz_zip_reader_end(&archive);
return false;
}
ctx.endDocument();
version = ctx.m_version;
return true;
}
// Load an AMF archive into a provided model.
bool load_amf_archive(const char *path, PresetBundle* bundle, Model *model)
{
if ((path == nullptr) || (model == nullptr))
return false;
unsigned int version = 0;
mz_zip_archive archive;
mz_zip_zero_struct(&archive);
mz_bool res = mz_zip_reader_init_file(&archive, path, 0);
if (res == 0)
{
printf("Unable to init zip reader\n");
return false;
}
mz_uint num_entries = mz_zip_reader_get_num_files(&archive);
mz_zip_archive_file_stat stat;
// we first loop the entries to read from the archive the .amf file only, in order to extract the version from it
for (mz_uint i = 0; i < num_entries; ++i)
{
if (mz_zip_reader_file_stat(&archive, i, &stat))
{
if (boost::iends_with(stat.m_filename, ".amf"))
{
if (!extract_model_from_archive(archive, stat, path, bundle, model, version))
{
mz_zip_reader_end(&archive);
printf("Archive does not contain a valid model");
return false;
}
break;
}
}
}
#if 0 // forward compatibility
// we then loop again the entries to read other files stored in the archive
for (mz_uint i = 0; i < num_entries; ++i)
{
if (mz_zip_reader_file_stat(&archive, i, &stat))
{
// add code to extract the file
}
}
#endif // forward compatibility
mz_zip_reader_end(&archive);
return true;
}
// Load an AMF file into a provided model.
// If bundle is not a null pointer, updates it if the amf file/archive contains config data
bool load_amf(const char *path, PresetBundle* bundle, Model *model)
{
if (boost::iends_with(path, ".amf.xml"))
// backward compatibility with older slic3r output
return load_amf_file(path, bundle, model);
else if (boost::iends_with(path, ".amf"))
{
boost::nowide::ifstream file(path, boost::nowide::ifstream::binary);
if (!file.good())
return false;
std::string zip_mask(2, '\0');
file.read(const_cast<char*>(zip_mask.data()), 2);
file.close();
return (zip_mask == "PK") ? load_amf_archive(path, bundle, model) : load_amf_file(path, bundle, model);
}
else
return false;
}
bool store_amf(const char *path, Model *model, Print* print, bool export_print_config)
{
if ((path == nullptr) || (model == nullptr) || (print == nullptr))
return false;
// forces ".zip.amf" extension
std::string export_path = path;
if (!boost::iends_with(export_path, ".zip.amf"))
export_path = boost::filesystem::path(export_path).replace_extension(".zip.amf").string();
mz_zip_archive archive;
mz_zip_zero_struct(&archive);
mz_bool res = mz_zip_writer_init_file(&archive, export_path.c_str(), 0);
if (res == 0)
return false;
std::stringstream stream;
stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
stream << "<amf unit=\"millimeter\">\n";
stream << "<metadata type=\"cad\">Slic3r " << SLIC3R_VERSION << "</metadata>\n";
stream << "<metadata type=\"" << SLIC3RPE_AMF_VERSION << "\">" << VERSION_AMF << "</metadata>\n";
if (export_print_config)
{
std::string config = "\n";
GCode::append_full_config(*print, config);
stream << "<metadata type=\"" << SLIC3R_CONFIG_TYPE << "\">" << xml_escape(config) << "</metadata>\n";
}
for (const auto &material : model->materials) {
if (material.first.empty())
continue;
// note that material-id must never be 0 since it's reserved by the AMF spec
stream << " <material id=\"" << material.first << "\">\n";
for (const auto &attr : material.second->attributes)
stream << " <metadata type=\"" << attr.first << "\">" << attr.second << "</metadata>\n";
for (const std::string &key : material.second->config.keys())
stream << " <metadata type=\"slic3r." << key << "\">" << material.second->config.serialize(key) << "</metadata>\n";
stream << " </material>\n";
}
std::string instances;
for (size_t object_id = 0; object_id < model->objects.size(); ++ object_id) {
ModelObject *object = model->objects[object_id];
stream << " <object id=\"" << object_id << "\">\n";
for (const std::string &key : object->config.keys())
stream << " <metadata type=\"slic3r." << key << "\">" << object->config.serialize(key) << "</metadata>\n";
if (!object->name.empty())
stream << " <metadata type=\"name\">" << xml_escape(object->name) << "</metadata>\n";
std::vector<double> layer_height_profile = object->layer_height_profile_valid ? object->layer_height_profile : std::vector<double>();
if (layer_height_profile.size() >= 4 && (layer_height_profile.size() % 2) == 0) {
// Store the layer height profile as a single semicolon separated list.
stream << " <metadata type=\"slic3r.layer_height_profile\">";
stream << layer_height_profile.front();
for (size_t i = 1; i < layer_height_profile.size(); ++i)
stream << ";" << layer_height_profile[i];
stream << "\n </metadata>\n";
}
//FIXME Store the layer height ranges (ModelObject::layer_height_ranges)
stream << " <mesh>\n";
stream << " <vertices>\n";
std::vector<int> vertices_offsets;
int num_vertices = 0;
for (ModelVolume *volume : object->volumes) {
vertices_offsets.push_back(num_vertices);
if (! volume->mesh.repaired)
throw std::runtime_error("store_amf() requires repair()");
auto &stl = volume->mesh.stl;
if (stl.v_shared == nullptr)
stl_generate_shared_vertices(&stl);
for (size_t i = 0; i < stl.stats.shared_vertices; ++ i) {
stream << " <vertex>\n";
stream << " <coordinates>\n";
stream << " <x>" << stl.v_shared[i](0) << "</x>\n";
stream << " <y>" << stl.v_shared[i](1) << "</y>\n";
stream << " <z>" << stl.v_shared[i](2) << "</z>\n";
stream << " </coordinates>\n";
stream << " </vertex>\n";
}
num_vertices += stl.stats.shared_vertices;
}
stream << " </vertices>\n";
for (size_t i_volume = 0; i_volume < object->volumes.size(); ++i_volume) {
ModelVolume *volume = object->volumes[i_volume];
int vertices_offset = vertices_offsets[i_volume];
if (volume->material_id().empty())
stream << " <volume>\n";
else
stream << " <volume materialid=\"" << volume->material_id() << "\">\n";
for (const std::string &key : volume->config.keys())
stream << " <metadata type=\"slic3r." << key << "\">" << volume->config.serialize(key) << "</metadata>\n";
if (!volume->name.empty())
stream << " <metadata type=\"name\">" << xml_escape(volume->name) << "</metadata>\n";
if (volume->is_modifier())
stream << " <metadata type=\"slic3r.modifier\">1</metadata>\n";
stream << " <metadata type=\"slic3r.volume_type\">" << ModelVolume::type_to_string(volume->type()) << "</metadata>\n";
for (int i = 0; i < volume->mesh.stl.stats.number_of_facets; ++i) {
stream << " <triangle>\n";
for (int j = 0; j < 3; ++j)
stream << " <v" << j + 1 << ">" << volume->mesh.stl.v_indices[i].vertex[j] + vertices_offset << "</v" << j + 1 << ">\n";
stream << " </triangle>\n";
}
stream << " </volume>\n";
}
stream << " </mesh>\n";
stream << " </object>\n";
if (!object->instances.empty()) {
for (ModelInstance *instance : object->instances) {
char buf[512];
sprintf(buf,
" <instance objectid=\"" PRINTF_ZU "\">\n"
" <deltax>%lf</deltax>\n"
" <deltay>%lf</deltay>\n"
#if ENABLE_MODELINSTANCE_3D_OFFSET
" <deltaz>%lf</deltaz>\n"
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
" <rz>%lf</rz>\n"
" <scale>%lf</scale>\n"
" </instance>\n",
object_id,
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance->get_offset(X),
instance->get_offset(Y),
instance->get_offset(Z),
#else
instance->offset(0),
instance->offset(1),
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
instance->rotation,
instance->scaling_factor);
//FIXME missing instance->scaling_factor
instances.append(buf);
}
}
}
if (! instances.empty()) {
stream << " <constellation id=\"1\">\n";
stream << instances;
stream << " </constellation>\n";
}
stream << "</amf>\n";
std::string internal_amf_filename = boost::ireplace_last_copy(boost::filesystem::path(export_path).filename().string(), ".zip.amf", ".amf");
std::string out = stream.str();
if (!mz_zip_writer_add_mem(&archive, internal_amf_filename.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION))
{
mz_zip_writer_end(&archive);
boost::filesystem::remove(export_path);
return false;
}
if (!mz_zip_writer_finalize_archive(&archive))
{
mz_zip_writer_end(&archive);
boost::filesystem::remove(export_path);
return false;
}
mz_zip_writer_end(&archive);
return true;
}
}; // namespace Slic3r

View file

@ -0,0 +1,19 @@
#ifndef slic3r_Format_AMF_hpp_
#define slic3r_Format_AMF_hpp_
namespace Slic3r {
class Model;
class Print;
class PresetBundle;
// Load the content of an amf file into the given model and preset bundle.
extern bool load_amf(const char *path, PresetBundle* bundle, Model *model);
// Save the given model and the config data contained in the given Print into an amf file.
// The model could be modified during the export process if meshes are not repaired or have no shared vertices
extern bool store_amf(const char *path, Model *model, Print* print, bool export_print_config);
}; // namespace Slic3r
#endif /* slic3r_Format_AMF_hpp_ */

View file

@ -0,0 +1,118 @@
#include "../libslic3r.h"
#include "../Model.hpp"
#include "../TriangleMesh.hpp"
#include "OBJ.hpp"
#include "objparser.hpp"
#include <string>
#ifdef _WIN32
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
namespace Slic3r {
bool load_obj(const char *path, Model *model, const char *object_name_in)
{
// Parse the OBJ file.
ObjParser::ObjData data;
if (! ObjParser::objparse(path, data)) {
// die "Failed to parse $file\n" if !-e $path;
return false;
}
// Count the faces and verify, that all faces are triangular.
size_t num_faces = 0;
size_t num_quads = 0;
for (size_t i = 0; i < data.vertices.size(); ) {
size_t j = i;
for (; j < data.vertices.size() && data.vertices[j].coordIdx != -1; ++ j) ;
if (i == j)
continue;
size_t face_vertices = j - i;
if (face_vertices != 3 && face_vertices != 4) {
// Non-triangular and non-quad faces are not supported as of now.
return false;
}
if (face_vertices == 4)
++ num_quads;
++ num_faces;
i = j + 1;
}
// Convert ObjData into STL.
TriangleMesh mesh;
stl_file &stl = mesh.stl;
stl.stats.type = inmemory;
stl.stats.number_of_facets = int(num_faces + num_quads);
stl.stats.original_num_facets = int(num_faces + num_quads);
// stl_allocate clears all the allocated data to zero, all normals are set to zeros as well.
stl_allocate(&stl);
size_t i_face = 0;
for (size_t i = 0; i < data.vertices.size(); ++ i) {
if (data.vertices[i].coordIdx == -1)
continue;
stl_facet &facet = stl.facet_start[i_face ++];
size_t num_normals = 0;
stl_normal normal(stl_normal::Zero());
for (unsigned int v = 0; v < 3; ++ v) {
const ObjParser::ObjVertex &vertex = data.vertices[i++];
memcpy(facet.vertex[v].data(), &data.coordinates[vertex.coordIdx*4], 3 * sizeof(float));
if (vertex.normalIdx != -1) {
normal(0) += data.normals[vertex.normalIdx*3];
normal(1) += data.normals[vertex.normalIdx*3+1];
normal(2) += data.normals[vertex.normalIdx*3+2];
++ num_normals;
}
}
if (data.vertices[i].coordIdx != -1) {
// This is a quad. Produce the other triangle.
stl_facet &facet2 = stl.facet_start[i_face++];
facet2.vertex[0] = facet.vertex[0];
facet2.vertex[1] = facet.vertex[2];
const ObjParser::ObjVertex &vertex = data.vertices[i++];
memcpy(facet2.vertex[2].data(), &data.coordinates[vertex.coordIdx * 4], 3 * sizeof(float));
if (vertex.normalIdx != -1) {
normal(0) += data.normals[vertex.normalIdx*3];
normal(1) += data.normals[vertex.normalIdx*3+1];
normal(2) += data.normals[vertex.normalIdx*3+2];
++ num_normals;
}
if (num_normals == 4) {
// Normalize an average normal of a quad.
float len = facet.normal.norm();
if (len > EPSILON) {
normal /= len;
facet.normal = normal;
facet2.normal = normal;
}
}
} else if (num_normals == 3) {
// Normalize an average normal of a triangle.
float len = facet.normal.norm();
if (len > EPSILON)
facet.normal = normal / len;
}
}
stl_get_size(&stl);
mesh.repair();
if (mesh.facets_count() == 0) {
// die "This STL file couldn't be read because it's empty.\n"
return false;
}
std::string object_name;
if (object_name_in == nullptr) {
const char *last_slash = strrchr(path, DIR_SEPARATOR);
object_name.assign((last_slash == nullptr) ? path : last_slash + 1);
} else
object_name.assign(object_name_in);
model->add_object(object_name.c_str(), path, std::move(mesh));
return true;
}
}; // namespace Slic3r

View file

@ -0,0 +1,14 @@
#ifndef slic3r_Format_OBJ_hpp_
#define slic3r_Format_OBJ_hpp_
namespace Slic3r {
class TriangleMesh;
class Model;
// Load an OBJ file into a provided model.
extern bool load_obj(const char *path, Model *model, const char *object_name = nullptr);
}; // namespace Slic3r
#endif /* slic3r_Format_OBJ_hpp_ */

View file

@ -0,0 +1,399 @@
#ifdef SLIC3R_PRUS
#include <string.h>
#include <boost/nowide/convert.hpp>
#include <wx/string.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <Eigen/Geometry>
#include "../libslic3r.h"
#include "../Model.hpp"
#include "PRUS.hpp"
#if 0
// Enable debugging and assert in this file.
#define DEBUG
#define _DEBUG
#undef NDEBUG
#endif
#include <assert.h>
namespace Slic3r
{
struct StlHeader
{
char comment[80];
uint32_t nTriangles;
};
static_assert(sizeof(StlHeader) == 84, "StlHeader size not correct");
// Buffered line reader for the wxInputStream.
class LineReader
{
public:
LineReader(wxInputStream &input_stream, const char *initial_data, int initial_len) :
m_input_stream(input_stream),
m_pos(0),
m_len(initial_len)
{
assert(initial_len >= 0 && initial_len < m_bufsize);
memcpy(m_buffer, initial_data, initial_len);
}
const char* next_line() {
for (;;) {
// Skip empty lines.
while (m_pos < m_len && (m_buffer[m_pos] == '\r' || m_buffer[m_pos] == '\n'))
++ m_pos;
if (m_pos == m_len) {
// Empty buffer, fill it from the input stream.
m_pos = 0;
m_input_stream.Read(m_buffer, m_bufsize - 1);
m_len = m_input_stream.LastRead();
assert(m_len >= 0 && m_len < m_bufsize);
if (m_len == 0)
// End of file.
return nullptr;
// Skip empty lines etc.
continue;
}
// The buffer is nonempty and it does not start with end of lines. Find the first end of line.
int end = m_pos + 1;
while (end < m_len && m_buffer[end] != '\r' && m_buffer[end] != '\n')
++ end;
if (end == m_len && ! m_input_stream.Eof() && m_len < m_bufsize) {
// Move the buffer content to the buffer start and fill the rest of the buffer.
assert(m_pos > 0);
memmove(m_buffer, m_buffer + m_pos, m_len - m_pos);
m_len -= m_pos;
assert(m_len >= 0 && m_len < m_bufsize);
m_pos = 0;
m_input_stream.Read(m_buffer + m_len, m_bufsize - 1 - m_len);
int new_data = m_input_stream.LastRead();
if (new_data > 0) {
m_len += new_data;
assert(m_len >= 0 && m_len < m_bufsize);
continue;
}
}
char *ptr_out = m_buffer + m_pos;
m_pos = end + 1;
m_buffer[end] = 0;
if (m_pos >= m_len) {
m_pos = 0;
m_len = 0;
}
return ptr_out;
}
}
int next_line_scanf(const char *format, ...)
{
const char *line = next_line();
if (line == nullptr)
return -1;
int result;
va_list arglist;
va_start(arglist, format);
result = vsscanf(line, format, arglist);
va_end(arglist);
return result;
}
private:
wxInputStream &m_input_stream;
static const int m_bufsize = 4096;
char m_buffer[m_bufsize];
int m_pos = 0;
int m_len = 0;
};
// Load a PrusaControl project file into a provided model.
bool load_prus(const char *path, Model *model)
{
// To receive the content of the zipped 'scene.xml' file.
std::vector<char> scene_xml_data;
wxFFileInputStream in(
#ifdef WIN32
// On Windows, convert to a 16bit unicode string.
boost::nowide::widen(path).c_str()
#else
path
#endif
);
wxZipInputStream zip(in);
std::unique_ptr<wxZipEntry> entry;
size_t num_models = 0;
std::map<int, ModelObject*> group_to_model_object;
while (entry.reset(zip.GetNextEntry()), entry.get() != NULL) {
wxString name = entry->GetName();
if (name == "scene.xml") {
if (! scene_xml_data.empty()) {
// scene.xml has been found more than once in the archive.
return false;
}
size_t size_last = 0;
size_t size_incr = 4096;
scene_xml_data.resize(size_incr);
while (! zip.Read(scene_xml_data.data() + size_last, size_incr).Eof()) {
size_last += zip.LastRead();
if (scene_xml_data.size() < size_last + size_incr)
scene_xml_data.resize(size_last + size_incr);
}
size_last += zip.LastRead();
if (scene_xml_data.size() == size_last)
scene_xml_data.resize(size_last + 1);
else if (scene_xml_data.size() > size_last + 1)
scene_xml_data.erase(scene_xml_data.begin() + size_last + 1, scene_xml_data.end());
scene_xml_data[size_last] = 0;
}
else if (name.EndsWith(".stl") || name.EndsWith(".STL")) {
// Find the model entry in the XML data.
const wxScopedCharBuffer name_utf8 = name.ToUTF8();
char model_name_tag[1024];
sprintf(model_name_tag, "<model name=\"%s\">", name_utf8.data());
const char *model_xml = strstr(scene_xml_data.data(), model_name_tag);
const char *zero_tag = "<zero>";
const char *zero_xml = strstr(scene_xml_data.data(), zero_tag);
float trafo[3][4] = { 0 };
double instance_rotation = 0.;
double instance_scaling_factor = 1.f;
#if ENABLE_MODELINSTANCE_3D_OFFSET
Vec3d instance_offset = Vec3d::Zero();
#else
Vec2d instance_offset(0., 0.);
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
bool trafo_set = false;
unsigned int group_id = (unsigned int)-1;
unsigned int extruder_id = (unsigned int)-1;
ModelObject *model_object = nullptr;
if (model_xml != nullptr) {
model_xml += strlen(model_name_tag);
const char *position_tag = "<position>";
const char *position_xml = strstr(model_xml, position_tag);
const char *rotation_tag = "<rotation>";
const char *rotation_xml = strstr(model_xml, rotation_tag);
const char *scale_tag = "<scale>";
const char *scale_xml = strstr(model_xml, scale_tag);
float position[3], rotation[3], scale[3], zero[3];
if (position_xml != nullptr && rotation_xml != nullptr && scale_xml != nullptr && zero_xml != nullptr &&
sscanf(position_xml+strlen(position_tag),
"[%f, %f, %f]", position, position+1, position+2) == 3 &&
sscanf(rotation_xml+strlen(rotation_tag),
"[%f, %f, %f]", rotation, rotation+1, rotation+2) == 3 &&
sscanf(scale_xml+strlen(scale_tag),
"[%f, %f, %f]", scale, scale+1, scale+2) == 3 &&
sscanf(zero_xml+strlen(zero_tag),
"[%f, %f, %f]", zero, zero+1, zero+2) == 3) {
if (scale[0] == scale[1] && scale[1] == scale[2]) {
instance_scaling_factor = scale[0];
scale[0] = scale[1] = scale[2] = 1.;
}
if (rotation[0] == 0. && rotation[1] == 0.) {
instance_rotation = - rotation[2];
rotation[2] = 0.;
}
Eigen::Matrix3f mat_rot, mat_scale, mat_trafo;
mat_rot = Eigen::AngleAxisf(-rotation[2], Eigen::Vector3f::UnitZ()) *
Eigen::AngleAxisf(-rotation[1], Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(-rotation[0], Eigen::Vector3f::UnitX());
mat_scale = Eigen::Scaling(scale[0], scale[1], scale[2]);
mat_trafo = mat_rot * mat_scale;
for (size_t r = 0; r < 3; ++ r) {
for (size_t c = 0; c < 3; ++ c)
trafo[r][c] += mat_trafo(r, c);
}
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance_offset = Vec3d((double)(position[0] - zero[0]), (double)(position[1] - zero[1]), (double)(position[2] - zero[2]));
#else
instance_offset(0) = position[0] - zero[0];
instance_offset(1) = position[1] - zero[1];
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
trafo[2][3] = position[2] / instance_scaling_factor;
trafo_set = true;
}
const char *group_tag = "<group>";
const char *group_xml = strstr(model_xml, group_tag);
const char *extruder_tag = "<extruder>";
const char *extruder_xml = strstr(model_xml, extruder_tag);
if (group_xml != nullptr) {
int group = atoi(group_xml + strlen(group_tag));
if (group > 0) {
group_id = group;
auto it = group_to_model_object.find(group_id);
if (it != group_to_model_object.end())
model_object = it->second;
}
}
if (extruder_xml != nullptr) {
int e = atoi(extruder_xml + strlen(extruder_tag));
if (e > 0)
extruder_id = e;
}
}
if (trafo_set) {
// Extract the STL.
StlHeader header;
TriangleMesh mesh;
bool mesh_valid = false;
bool stl_ascii = false;
if (!zip.Read((void*)&header, sizeof(StlHeader)).Eof()) {
if (strncmp(header.comment, "solid ", 6) == 0)
stl_ascii = true;
else {
// Header has been extracted. Now read the faces.
stl_file &stl = mesh.stl;
stl.error = 0;
stl.stats.type = inmemory;
stl.stats.number_of_facets = header.nTriangles;
stl.stats.original_num_facets = header.nTriangles;
stl_allocate(&stl);
if (header.nTriangles > 0 && zip.ReadAll((void*)stl.facet_start, 50 * header.nTriangles)) {
if (sizeof(stl_facet) > SIZEOF_STL_FACET) {
// The stl.facet_start is not packed tightly. Unpack the array of stl_facets.
unsigned char *data = (unsigned char*)stl.facet_start;
for (size_t i = header.nTriangles - 1; i > 0; -- i)
memmove(data + i * sizeof(stl_facet), data + i * SIZEOF_STL_FACET, SIZEOF_STL_FACET);
}
// All the faces have been read.
stl_get_size(&stl);
mesh.repair();
// Transform the model.
stl_transform(&stl, &trafo[0][0]);
if (std::abs(stl.stats.min(2)) < EPSILON)
stl.stats.min(2) = 0.;
// Add a mesh to a model.
if (mesh.facets_count() > 0)
mesh_valid = true;
}
}
} else
stl_ascii = true;
if (stl_ascii) {
// Try to parse ASCII STL.
char normal_buf[3][32];
stl_facet facet;
std::vector<stl_facet> facets;
LineReader line_reader(zip, (char*)&header, zip.LastRead());
std::string solid_name;
facet.extra[0] = facet.extra[1] = 0;
for (;;) {
const char *line = line_reader.next_line();
if (line == nullptr)
// End of file.
break;
if (strncmp(line, "solid", 5) == 0) {
// Opening the "solid" block.
if (! solid_name.empty()) {
// Error, solid block is already open.
facets.clear();
break;
}
solid_name = line + 5;
if (solid_name.empty())
solid_name = "unknown";
continue;
}
if (strncmp(line, "endsolid", 8) == 0) {
// Closing the "solid" block.
if (solid_name.empty()) {
// Error, no solid block is open.
facets.clear();
break;
}
solid_name.clear();
continue;
}
// Line has to start with the word solid.
int res_normal = sscanf(line, " facet normal %31s %31s %31s", normal_buf[0], normal_buf[1], normal_buf[2]);
assert(res_normal == 3);
int res_outer_loop = line_reader.next_line_scanf(" outer loop");
assert(res_outer_loop == 0);
int res_vertex1 = line_reader.next_line_scanf(" vertex %f %f %f", &facet.vertex[0](0), &facet.vertex[0](1), &facet.vertex[0](2));
assert(res_vertex1 == 3);
int res_vertex2 = line_reader.next_line_scanf(" vertex %f %f %f", &facet.vertex[1](0), &facet.vertex[1](1), &facet.vertex[1](2));
assert(res_vertex2 == 3);
int res_vertex3 = line_reader.next_line_scanf(" vertex %f %f %f", &facet.vertex[2](0), &facet.vertex[2](1), &facet.vertex[2](2));
assert(res_vertex3 == 3);
int res_endloop = line_reader.next_line_scanf(" endloop");
assert(res_endloop == 0);
int res_endfacet = line_reader.next_line_scanf(" endfacet");
if (res_normal != 3 || res_outer_loop != 0 || res_vertex1 != 3 || res_vertex2 != 3 || res_vertex3 != 3 || res_endloop != 0 || res_endfacet != 0) {
// perror("Something is syntactically very wrong with this ASCII STL!");
facets.clear();
break;
}
// The facet normal has been parsed as a single string as to workaround for not a numbers in the normal definition.
if (sscanf(normal_buf[0], "%f", &facet.normal(0)) != 1 ||
sscanf(normal_buf[1], "%f", &facet.normal(1)) != 1 ||
sscanf(normal_buf[2], "%f", &facet.normal(2)) != 1) {
// Normal was mangled. Maybe denormals or "not a number" were stored?
// Just reset the normal and silently ignore it.
memset(&facet.normal, 0, sizeof(facet.normal));
}
facets.emplace_back(facet);
}
if (! facets.empty() && solid_name.empty()) {
stl_file &stl = mesh.stl;
stl.stats.type = inmemory;
stl.stats.number_of_facets = facets.size();
stl.stats.original_num_facets = facets.size();
stl_allocate(&stl);
memcpy((void*)stl.facet_start, facets.data(), facets.size() * 50);
stl_get_size(&stl);
mesh.repair();
// Transform the model.
stl_transform(&stl, &trafo[0][0]);
// Add a mesh to a model.
if (mesh.facets_count() > 0)
mesh_valid = true;
}
}
if (mesh_valid) {
// Add this mesh to the model.
ModelVolume *volume = nullptr;
if (model_object == nullptr) {
// This is a first mesh of a group. Create a new object & volume.
model_object = model->add_object(name_utf8.data(), path, std::move(mesh));
volume = model_object->volumes.front();
ModelInstance *instance = model_object->add_instance();
instance->rotation = instance_rotation;
instance->scaling_factor = instance_scaling_factor;
#if ENABLE_MODELINSTANCE_3D_OFFSET
instance->set_offset(instance_offset);
#else
instance->offset = instance_offset;
#endif // ENABLE_MODELINSTANCE_3D_OFFSET
++num_models;
if (group_id != (size_t)-1)
group_to_model_object[group_id] = model_object;
} else {
// This is not the 1st mesh of a group. Add it to the ModelObject.
volume = model_object->add_volume(std::move(mesh));
volume->name = name_utf8.data();
}
// Set the extruder to the volume.
if (extruder_id != (unsigned int)-1) {
char str_extruder[64];
sprintf(str_extruder, "%ud", extruder_id);
volume->config.set_deserialize("extruder", str_extruder);
}
}
}
}
}
return num_models > 0;
}
}; // namespace Slic3r
#endif /* SLIC3R_PRUS */

View file

@ -0,0 +1,14 @@
#if defined(SLIC3R_PRUS) && ! defined(slic3r_Format_PRUS_hpp_)
#define slic3r_Format_PRUS_hpp_
namespace Slic3r {
class TriangleMesh;
class Model;
// Load a PrusaControl project file into a provided model.
extern bool load_prus(const char *path, Model *model);
}; // namespace Slic3r
#endif /* SLIC3R_PRUS && ! defined(slic3r_Format_PRUS_hpp_) */

View file

@ -0,0 +1,58 @@
#include "../libslic3r.h"
#include "../Model.hpp"
#include "../TriangleMesh.hpp"
#include "STL.hpp"
#include <string>
#ifdef _WIN32
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
namespace Slic3r {
bool load_stl(const char *path, Model *model, const char *object_name_in)
{
TriangleMesh mesh;
mesh.ReadSTLFile(path);
if (mesh.stl.error) {
// die "Failed to open $file\n" if !-e $path;
return false;
}
mesh.repair();
if (mesh.facets_count() == 0) {
// die "This STL file couldn't be read because it's empty.\n"
return false;
}
std::string object_name;
if (object_name_in == nullptr) {
const char *last_slash = strrchr(path, DIR_SEPARATOR);
object_name.assign((last_slash == nullptr) ? path : last_slash + 1);
} else
object_name.assign(object_name_in);
model->add_object(object_name.c_str(), path, std::move(mesh));
return true;
}
bool store_stl(const char *path, TriangleMesh *mesh, bool binary)
{
if (binary)
mesh->write_binary(path);
else
mesh->write_ascii(path);
//FIXME returning false even if write failed.
return true;
}
bool store_stl(const char *path, ModelObject *model_object, bool binary)
{
TriangleMesh mesh = model_object->mesh();
return store_stl(path, &mesh, binary);
}
}; // namespace Slic3r

View file

@ -0,0 +1,17 @@
#ifndef slic3r_Format_STL_hpp_
#define slic3r_Format_STL_hpp_
namespace Slic3r {
class TriangleMesh;
class ModelObject;
// Load an STL file into a provided model.
extern bool load_stl(const char *path, Model *model, const char *object_name = nullptr);
extern bool store_stl(const char *path, TriangleMesh *mesh, bool binary);
extern bool store_stl(const char *path, ModelObject *model_object, bool binary);
}; // namespace Slic3r
#endif /* slic3r_Format_STL_hpp_ */

View file

@ -0,0 +1,540 @@
#include <stdlib.h>
#include <string.h>
#include <boost/nowide/cstdio.hpp>
#include "objparser.hpp"
namespace ObjParser {
static bool obj_parseline(const char *line, ObjData &data)
{
#define EATWS() while (*line == ' ' || *line == '\t') ++ line
if (*line == 0)
return true;
// Ignore whitespaces at the beginning of the line.
//FIXME is this a good idea?
EATWS();
char c1 = *line ++;
switch (c1) {
case '#':
// Comment, ignore the rest of the line.
break;
case 'v':
{
// Parse vertex geometry (position, normal, texture coordinates)
char c2 = *line ++;
switch (c2) {
case 't':
{
// vt - vertex texture parameter
// u v [w], w == 0 (or w == 1)
char c2 = *line ++;
if (c2 != ' ' && c2 != '\t')
return false;
EATWS();
char *endptr = 0;
double u = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double v = 0;
if (*line != 0) {
v = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
}
double w = 0;
if (*line != 0) {
w = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
}
if (*line != 0)
return false;
data.textureCoordinates.push_back((float)u);
data.textureCoordinates.push_back((float)v);
data.textureCoordinates.push_back((float)w);
break;
}
case 'n':
{
// vn - vertex normal
// x y z
char c2 = *line ++;
if (c2 != ' ' && c2 != '\t')
return false;
EATWS();
char *endptr = 0;
double x = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
if (*line != 0)
return false;
data.normals.push_back((float)x);
data.normals.push_back((float)y);
data.normals.push_back((float)z);
break;
}
case 'p':
{
// vp - vertex parameter
char c2 = *line ++;
if (c2 != ' ' && c2 != '\t')
return false;
EATWS();
char *endptr = 0;
double u = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
double v = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
double w = 0;
if (*line != 0) {
w = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
}
if (*line != 0)
return false;
data.parameters.push_back((float)u);
data.parameters.push_back((float)v);
data.parameters.push_back((float)w);
break;
}
default:
{
// v - vertex geometry
if (c2 != ' ' && c2 != '\t')
return false;
EATWS();
char *endptr = 0;
double x = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
double w = 1.0;
if (*line != 0) {
w = strtod(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
}
if (*line != 0)
return false;
data.coordinates.push_back((float)x);
data.coordinates.push_back((float)y);
data.coordinates.push_back((float)z);
data.coordinates.push_back((float)w);
break;
}
}
break;
}
case 'f':
{
// face
EATWS();
if (*line == 0)
return false;
// number of vertices of this face
int n = 0;
// current vertex to be parsed
ObjVertex vertex;
char *endptr = 0;
while (*line != 0) {
// Parse a single vertex reference.
vertex.coordIdx = 0;
vertex.normalIdx = 0;
vertex.textureCoordIdx = 0;
vertex.coordIdx = strtol(line, &endptr, 10);
// Coordinate has to be defined
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != '/' && *endptr != 0))
return false;
line = endptr;
if (*line == '/') {
++ line;
// Texture coordinate index may be missing after a 1st slash, but then the normal index has to be present.
if (*line != '/') {
// Parse the texture coordinate index.
vertex.textureCoordIdx = strtol(line, &endptr, 10);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != '/' && *endptr != 0))
return false;
line = endptr;
}
if (*line == '/') {
// Parse normal index.
++ line;
vertex.normalIdx = strtol(line, &endptr, 10);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
}
}
if (vertex.coordIdx < 0)
vertex.coordIdx += data.coordinates.size() / 4;
else
-- vertex.coordIdx;
if (vertex.normalIdx < 0)
vertex.normalIdx += data.normals.size() / 3;
else
-- vertex.normalIdx;
if (vertex.textureCoordIdx < 0)
vertex.textureCoordIdx += data.textureCoordinates.size() / 3;
else
-- vertex.textureCoordIdx;
data.vertices.push_back(vertex);
EATWS();
}
vertex.coordIdx = -1;
vertex.normalIdx = -1;
vertex.textureCoordIdx = -1;
data.vertices.push_back(vertex);
break;
}
case 'm':
{
if (*(line ++) != 't' ||
*(line ++) != 'l' ||
*(line ++) != 'l' ||
*(line ++) != 'i' ||
*(line ++) != 'b')
return false;
// mtllib [external .mtl file name]
// printf("mtllib %s\r\n", line);
EATWS();
data.mtllibs.push_back(std::string(line));
break;
}
case 'u':
{
if (*(line ++) != 's' ||
*(line ++) != 'e' ||
*(line ++) != 'm' ||
*(line ++) != 't' ||
*(line ++) != 'l')
return false;
// usemtl [material name]
// printf("usemtl %s\r\n", line);
EATWS();
ObjUseMtl usemtl;
usemtl.vertexIdxFirst = data.vertices.size();
usemtl.name = line;
data.usemtls.push_back(usemtl);
break;
}
case 'o':
{
// o [object name]
EATWS();
const char *name = line;
while (*line != ' ' && *line != '\t' && *line != 0)
++ line;
// copy name to line.
EATWS();
if (*line != 0)
return false;
ObjObject object;
object.vertexIdxFirst = data.vertices.size();
object.name = line;
data.objects.push_back(object);
break;
}
case 'g':
{
// g [group name]
// printf("group %s\r\n", line);
ObjGroup group;
group.vertexIdxFirst = data.vertices.size();
group.name = line;
data.groups.push_back(group);
break;
}
case 's':
{
// s 1 / off
char c2 = *line ++;
if (c2 != ' ' && c2 != '\t')
return false;
EATWS();
char *endptr = 0;
long g = strtol(line, &endptr, 10);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
if (*line != 0)
return false;
ObjSmoothingGroup group;
group.vertexIdxFirst = data.vertices.size();
group.smoothingGroupID = g;
data.smoothingGroups.push_back(group);
break;
}
default:
printf("ObjParser: Unknown command: %c\r\n", c1);
break;
}
return true;
}
bool objparse(const char *path, ObjData &data)
{
FILE *pFile = boost::nowide::fopen(path, "rt");
if (pFile == 0)
return false;
try {
char buf[65536 * 2];
size_t len = 0;
size_t lenPrev = 0;
while ((len = ::fread(buf + lenPrev, 1, 65536, pFile)) != 0) {
len += lenPrev;
size_t lastLine = 0;
for (size_t i = 0; i < len; ++ i)
if (buf[i] == '\r' || buf[i] == '\n') {
buf[i] = 0;
char *c = buf + lastLine;
while (*c == ' ' || *c == '\t')
++ c;
obj_parseline(c, data);
lastLine = i + 1;
}
lenPrev = len - lastLine;
memmove(buf, buf + lastLine, lenPrev);
}
} catch (std::bad_alloc &ex) {
printf("Out of memory\r\n");
}
::fclose(pFile);
// printf("vertices: %d\r\n", data.vertices.size() / 4);
// printf("coords: %d\r\n", data.coordinates.size());
return true;
}
template<typename T>
bool savevector(FILE *pFile, const std::vector<T> &v)
{
size_t cnt = v.size();
::fwrite(&cnt, 1, sizeof(cnt), pFile);
//FIXME sizeof(T) works for data types leaving no gaps in the allocated vector because of alignment of the T type.
if (! v.empty())
::fwrite(&v.front(), 1, sizeof(T) * cnt, pFile);
return true;
}
bool savevector(FILE *pFile, const std::vector<std::string> &v)
{
size_t cnt = v.size();
::fwrite(&cnt, 1, sizeof(cnt), pFile);
for (size_t i = 0; i < cnt; ++ i) {
size_t len = v[i].size();
::fwrite(&len, 1, sizeof(cnt), pFile);
::fwrite(v[i].c_str(), 1, len, pFile);
}
return true;
}
template<typename T>
bool savevectornameidx(FILE *pFile, const std::vector<T> &v)
{
size_t cnt = v.size();
::fwrite(&cnt, 1, sizeof(cnt), pFile);
for (size_t i = 0; i < cnt; ++ i) {
::fwrite(&v[i].vertexIdxFirst, 1, sizeof(int), pFile);
size_t len = v[i].name.size();
::fwrite(&len, 1, sizeof(cnt), pFile);
::fwrite(v[i].name.c_str(), 1, len, pFile);
}
return true;
}
template<typename T>
bool loadvector(FILE *pFile, std::vector<T> &v)
{
v.clear();
size_t cnt = 0;
if (::fread(&cnt, sizeof(cnt), 1, pFile) != 1)
return false;
//FIXME sizeof(T) works for data types leaving no gaps in the allocated vector because of alignment of the T type.
if (cnt != 0) {
v.assign(cnt, T());
if (::fread(&v.front(), sizeof(T), cnt, pFile) != cnt)
return false;
}
return true;
}
bool loadvector(FILE *pFile, std::vector<std::string> &v)
{
v.clear();
size_t cnt = 0;
if (::fread(&cnt, sizeof(cnt), 1, pFile) != 1)
return false;
v.reserve(cnt);
for (size_t i = 0; i < cnt; ++ i) {
size_t len = 0;
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
std::string s(" ", len);
if (::fread(const_cast<char*>(s.c_str()), 1, len, pFile) != len)
return false;
v.push_back(std::move(s));
}
return true;
}
template<typename T>
bool loadvectornameidx(FILE *pFile, std::vector<T> &v)
{
v.clear();
size_t cnt = 0;
if (::fread(&cnt, sizeof(cnt), 1, pFile) != 1)
return false;
v.assign(cnt, T());
for (size_t i = 0; i < cnt; ++ i) {
if (::fread(&v[i].vertexIdxFirst, sizeof(int), 1, pFile) != 1)
return false;
size_t len = 0;
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
v[i].name.assign(" ", len);
if (::fread(const_cast<char*>(v[i].name.c_str()), 1, len, pFile) != len)
return false;
}
return true;
}
bool objbinsave(const char *path, const ObjData &data)
{
FILE *pFile = boost::nowide::fopen(path, "wb");
if (pFile == 0)
return false;
size_t version = 1;
::fwrite(&version, 1, sizeof(version), pFile);
bool result =
savevector(pFile, data.coordinates) &&
savevector(pFile, data.textureCoordinates) &&
savevector(pFile, data.normals) &&
savevector(pFile, data.parameters) &&
savevector(pFile, data.mtllibs) &&
savevectornameidx(pFile, data.usemtls) &&
savevectornameidx(pFile, data.objects) &&
savevectornameidx(pFile, data.groups) &&
savevector(pFile, data.smoothingGroups) &&
savevector(pFile, data.vertices);
::fclose(pFile);
return result;
}
bool objbinload(const char *path, ObjData &data)
{
FILE *pFile = boost::nowide::fopen(path, "rb");
if (pFile == 0)
return false;
data.version = 0;
if (::fread(&data.version, sizeof(data.version), 1, pFile) != 1)
return false;
if (data.version != 1)
return false;
bool result =
loadvector(pFile, data.coordinates) &&
loadvector(pFile, data.textureCoordinates) &&
loadvector(pFile, data.normals) &&
loadvector(pFile, data.parameters) &&
loadvector(pFile, data.mtllibs) &&
loadvectornameidx(pFile, data.usemtls) &&
loadvectornameidx(pFile, data.objects) &&
loadvectornameidx(pFile, data.groups) &&
loadvector(pFile, data.smoothingGroups) &&
loadvector(pFile, data.vertices);
::fclose(pFile);
return result;
}
template<typename T>
bool vectorequal(const std::vector<T> &v1, const std::vector<T> &v2)
{
if (v1.size() != v2.size())
return false;
for (size_t i = 0; i < v1.size(); ++ i)
if (! (v1[i] == v2[i]))
return false;
return true;
}
bool vectorequal(const std::vector<std::string> &v1, const std::vector<std::string> &v2)
{
if (v1.size() != v2.size())
return false;
for (size_t i = 0; i < v1.size(); ++ i)
if (v1[i].compare(v2[i]) != 0)
return false;
return true;
}
extern bool objequal(const ObjData &data1, const ObjData &data2)
{
//FIXME ignore version number
// version;
return
vectorequal(data1.coordinates, data2.coordinates) &&
vectorequal(data1.textureCoordinates, data2.textureCoordinates) &&
vectorequal(data1.normals, data2.normals) &&
vectorequal(data1.parameters, data2.parameters) &&
vectorequal(data1.mtllibs, data2.mtllibs) &&
vectorequal(data1.usemtls, data2.usemtls) &&
vectorequal(data1.objects, data2.objects) &&
vectorequal(data1.groups, data2.groups) &&
vectorequal(data1.vertices, data2.vertices);
}
} // namespace ObjParser

View file

@ -0,0 +1,109 @@
#ifndef slic3r_Format_objparser_hpp_
#define slic3r_Format_objparser_hpp_
#include <string>
#include <vector>
namespace ObjParser {
struct ObjVertex
{
int coordIdx;
int textureCoordIdx;
int normalIdx;
};
inline bool operator==(const ObjVertex &v1, const ObjVertex &v2)
{
return
v1.coordIdx == v2.coordIdx &&
v1.textureCoordIdx == v2.textureCoordIdx &&
v1.normalIdx == v2.normalIdx;
}
struct ObjUseMtl
{
int vertexIdxFirst;
std::string name;
};
inline bool operator==(const ObjUseMtl &v1, const ObjUseMtl &v2)
{
return
v1.vertexIdxFirst == v2.vertexIdxFirst &&
v1.name.compare(v2.name) == 0;
}
struct ObjObject
{
int vertexIdxFirst;
std::string name;
};
inline bool operator==(const ObjObject &v1, const ObjObject &v2)
{
return
v1.vertexIdxFirst == v2.vertexIdxFirst &&
v1.name.compare(v2.name) == 0;
}
struct ObjGroup
{
int vertexIdxFirst;
std::string name;
};
inline bool operator==(const ObjGroup &v1, const ObjGroup &v2)
{
return
v1.vertexIdxFirst == v2.vertexIdxFirst &&
v1.name.compare(v2.name) == 0;
}
struct ObjSmoothingGroup
{
int vertexIdxFirst;
int smoothingGroupID;
};
inline bool operator==(const ObjSmoothingGroup &v1, const ObjSmoothingGroup &v2)
{
return
v1.vertexIdxFirst == v2.vertexIdxFirst &&
v1.smoothingGroupID == v2.smoothingGroupID;
}
struct ObjData {
// Version of the data structure for load / store in the private binary format.
int version;
// x, y, z, w
std::vector<float> coordinates;
// u, v, w
std::vector<float> textureCoordinates;
// x, y, z
std::vector<float> normals;
// u, v, w
std::vector<float> parameters;
std::vector<std::string> mtllibs;
std::vector<ObjUseMtl> usemtls;
std::vector<ObjObject> objects;
std::vector<ObjGroup> groups;
std::vector<ObjSmoothingGroup> smoothingGroups;
// List of faces, delimited by an ObjVertex with all members set to -1.
std::vector<ObjVertex> vertices;
};
extern bool objparse(const char *path, ObjData &data);
extern bool objbinsave(const char *path, const ObjData &data);
extern bool objbinload(const char *path, ObjData &data);
extern bool objequal(const ObjData &data1, const ObjData &data2);
} // namespace ObjParser
#endif /* slic3r_Format_objparser_hpp_ */