Revive XML serializer

This commit is contained in:
Thomas Krijnen
2018-03-16 16:04:48 +01:00
parent 834780b726
commit 5082fe8be4
8 changed files with 154 additions and 40 deletions
+21 -1
View File
@@ -30,6 +30,7 @@
#include "../serializers/IgesSerializer.h"
#include "../serializers/StepSerializer.h"
#include "../serializers/WavefrontObjSerializer.h"
#include "../serializers/XmlSerializer.h"
#include "../serializers/SvgSerializer.h"
#include "../ifcgeom_schema_agnostic/IfcGeomIterator.h"
@@ -69,6 +70,7 @@ void print_usage(bool suggest_help = true)
#endif
<< " .stp STEP Standard for the Exchange of Product Data\n"
<< " .igs IGES Initial Graphics Exchange Specification\n"
<< " .xml XML Property definitions and decomposition tree\n"
<< " .svg SVG Scalable Vector Graphics (2D floor plan)\n"
<< "\n"
<< "If no output filename given, <input>." + DEFAULT_EXTENSION + " will be used as the output file.\n";
@@ -442,6 +444,24 @@ int main(int argc, char** argv)
IfcParse::IfcFile* ifc_file = 0;
if (output_extension == ".xml") {
int exit_code = EXIT_FAILURE;
try {
if (init_input_file(input_filename, ifc_file, no_progress, mmap)) {
XmlSerializer s(ifc_file, output_temp_filename);
Logger::Status("Writing XML output...");
s.finalize();
Logger::Status("Done!");
rename_file(output_temp_filename, output_filename);
exit_code = EXIT_SUCCESS;
}
} catch (const std::exception& e) {
Logger::Error(e);
}
write_log();
return exit_code;
}
/*
if (!filter_filename.empty()) {
size_t num_filters = read_filters_from_file(filter_filename, include_filter, include_traverse_filter, exclude_filter, exclude_traverse_filter);
@@ -922,4 +942,4 @@ std::vector<IfcGeom::filter_t> setup_filters(const std::vector<geom_filter>& fil
return filter_funcs;
}
*/
*/
+3 -8
View File
@@ -46,6 +46,7 @@ inline static bool ALMOST_THE_SAME(const T& a, const T& b, double tolerance=ALMO
#include <TopTools_ListOfShape.hxx>
#include <BOPAlgo_Operation.hxx>
#include "../ifcparse/macros.h"
#include "../ifcparse/IfcParse.h"
#include "../ifcparse/IfcBaseClass.h"
@@ -75,14 +76,8 @@ if ( it != cache.T.end() ) { e = it->second; return true; }
#endif
#define MAKE_TYPE_NAME__(a, b) a ## b
#define MAKE_TYPE_NAME_(a, b) MAKE_TYPE_NAME__(a, b)
#define MAKE_TYPE_NAME(t) MAKE_TYPE_NAME_(t, IfcSchema)
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define INCLUDE(x) STRINGIFY(../ifcparse/x.h)
#include INCLUDE(IfcSchema)
#define INCLUDE_PARENT_DIR(x) STRINGIFY(../ifcparse/x.h)
#include INCLUDE_PARENT_DIR(IfcSchema)
namespace IfcGeom {
+1 -5
View File
@@ -133,6 +133,7 @@
#include <Standard_Version.hxx>
#include "../ifcparse/macros.h"
#include "../ifcparse/IfcSIPrefix.h"
#include "../ifcparse/IfcFile.h"
#include "../ifcgeom/IfcGeom.h"
@@ -145,11 +146,6 @@
#endif
#endif
#define MAKE_INIT_FN__(a, b) init_ ## a ## b
#define MAKE_INIT_FN_(a, b) MAKE_INIT_FN__(a, b)
#define MAKE_INIT_FN(t) MAKE_INIT_FN_(t, IfcSchema)
namespace {
struct factory_t {
IfcGeom::Kernel* operator()(IfcParse::IfcFile* file) const {
+34
View File
@@ -0,0 +1,34 @@
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef IFCOPENSHELL_MACROS_H
#define IFCOPENSHELL_MACROS_H
#define MAKE_TYPE_NAME__(a, b) a ## b
#define MAKE_TYPE_NAME_(a, b) MAKE_TYPE_NAME__(a, b)
#define MAKE_TYPE_NAME(t) MAKE_TYPE_NAME_(t, IfcSchema)
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define MAKE_INIT_FN__(a, b) init_ ## a ## b
#define MAKE_INIT_FN_(a, b) MAKE_INIT_FN__(a, b)
#define MAKE_INIT_FN(t) MAKE_INIT_FN_(t, IfcSchema)
#endif
+35
View File
@@ -0,0 +1,35 @@
#include "XmlSerializer.h"
extern void init_XmlSerializerIfc2x3(XmlSerializerFactory::Factory*);
extern void init_XmlSerializerIfc4(XmlSerializerFactory::Factory*);
XmlSerializerFactory::Factory::Factory() {
init_XmlSerializerIfc2x3(this);
init_XmlSerializerIfc4(this);
}
void XmlSerializerFactory::Factory::bind(const std::string& schema_name, fn f) {
const std::string schema_name_lower = boost::to_lower_copy(schema_name);
this->insert(std::make_pair(schema_name_lower, f));
}
XmlSerializer* XmlSerializerFactory::Factory::construct(const std::string& schema_name, IfcParse::IfcFile* file, std::string xml_filename) {
const std::string schema_name_lower = boost::to_lower_copy(schema_name);
typename std::map<std::string, fn>::const_iterator it;
it = this->find(schema_name_lower);
if (it == this->end()) {
throw IfcParse::IfcException("No XML serializer registered for " + schema_name);
}
return it->second(file, xml_filename);
}
XmlSerializer::XmlSerializer(IfcParse::IfcFile* file, const std::string& xml_filename) {
if (file != nullptr) {
implementation_ = XmlSerializerFactory::implementations().construct(file->schema()->name(), file, xml_filename);
}
}
XmlSerializerFactory::Factory& XmlSerializerFactory::implementations() {
static XmlSerializerFactory::Factory impl;
return impl;
}
+18 -7
View File
@@ -1,29 +1,40 @@
#define SCHEMA_METHOD
#include "../serializers/Serializer.h"
#include "../ifcparse/IfcFile.h"
#include <boost/function.hpp>
#include <map>
template <typename T>
class schema_delegate {
class XmlSerializer : public Serializer {
private:
XmlSerializer* implementation_;
};
protected:
std::string xml_filename;
class XmlSerializer : public schema_delegate<> {
public:
XmlSerializer(IfcParse::IfcFile* file, const std::string& xml_filename);
virtual ~XmlSerializer() {}
bool ready() { return true; }
void writeHeader() {}
void finalize() { implementation_->finalize(); }
void setFile(IfcParse::IfcFile*) { throw IfcParse::IfcException("Should be supplied on construction"); }
};
struct XmlSerializerFactory {
typedef boost::function1<XmlSerializer*, IfcParse::IfcFile*> fn;
typedef boost::function2<XmlSerializer*, IfcParse::IfcFile*, std::string> fn;
class Factory : public std::map<std::string, fn> {
public:
Factory();
void bind(const std::string& schema_name, fn);
XmlSerializer* construct(const std::string& schema_name, IfcParse::IfcFile*);
XmlSerializer* construct(const std::string& schema_name, IfcParse::IfcFile*, std::string);
};
Factory& implementations();
static Factory& implementations();
};
@@ -32,9 +32,28 @@
using boost::property_tree::ptree;
#include "XmlSerializer.h"
namespace {
struct factory_t {
XmlSerializer* operator()(IfcParse::IfcFile* file, const std::string& xml_filename) const {
MAKE_TYPE_NAME(XmlSerializer)* s = new MAKE_TYPE_NAME(XmlSerializer)(file, xml_filename);
s->setFile(file);
return s;
}
};
}
void MAKE_INIT_FN(XmlSerializer)(XmlSerializerFactory::Factory* mapping) {
static const std::string schema_name = STRINGIFY(IfcSchema);
factory_t factory;
mapping->bind(schema_name, factory);
}
namespace {
std::map<std::string, std::string> argument_name_map;
// TODO: Make this a member of XmlSerializer?
std::map<std::string, std::string> MAKE_TYPE_NAME(argument_name_map);
// Format an IFC attribute and maybe returns as string. Only literal scalar
// values are converted. Things like entity instances and lists are omitted.
@@ -108,7 +127,7 @@ boost::optional<std::string> format_attribute(const Argument* argument, IfcUtil:
} else if (e->declaration().is(IfcSchema::IfcLocalPlacement::Class())) {
IfcSchema::IfcLocalPlacement* placement = e->as<IfcSchema::IfcLocalPlacement>();
gp_Trsf trsf;
IfcGeom::Kernel kernel;
IfcGeom::MAKE_TYPE_NAME(Kernel) kernel;
if (kernel.convert(placement, trsf)) {
std::stringstream stream;
@@ -138,8 +157,8 @@ ptree& format_entity_instance(IfcUtil::IfcBaseEntity* instance, ptree& child, pt
std::string argument_name = instance->declaration().attribute_by_index(i)->name();
std::map<std::string, std::string>::const_iterator argument_name_it;
argument_name_it = argument_name_map.find(argument_name);
if (argument_name_it != argument_name_map.end()) {
argument_name_it = MAKE_TYPE_NAME(argument_name_map).find(argument_name);
if (argument_name_it != MAKE_TYPE_NAME(argument_name_map).end()) {
argument_name = argument_name_it->second;
}
const IfcUtil::ArgumentType argument_type = instance->data().getArgument(i)->type();
@@ -326,8 +345,8 @@ void format_quantities(IfcSchema::IfcPhysicalQuantity::list::ptr quantities, ptr
} // ~unnamed namespace
void XmlSerializer::finalize() {
argument_name_map.insert(std::make_pair("GlobalId", "id"));
void MAKE_TYPE_NAME(XmlSerializer)::finalize() {
MAKE_TYPE_NAME(argument_name_map).insert(std::make_pair("GlobalId", "id"));
IfcSchema::IfcProject::list::ptr projects = file->instances_by_type<IfcSchema::IfcProject>();
if (projects->size() != 1) {
@@ -17,25 +17,29 @@
* *
********************************************************************************/
#ifndef XMLSERIALIZER_H
#define XMLSERIALIZER_H
#ifndef XMLSERIALIZERIMPL_H
#define XMLSERIALIZERIMPL_H
#include "../../serializers/Serializer.h"
#include "../../ifcparse/macros.h"
#include "../../serializers/XmlSerializer.h"
class XmlSerializer : public Serializer {
#define INCLUDE_PARENT_PARENT_DIR(x) STRINGIFY(../../ifcparse/x.h)
#include INCLUDE_PARENT_PARENT_DIR(IfcSchema)
class MAKE_TYPE_NAME(XmlSerializer) : public XmlSerializer {
private:
IfcParse::IfcFile* file;
std::string xml_filename;
public:
XmlSerializer(const std::string& xml_filename)
: Serializer()
, xml_filename(xml_filename)
{}
bool ready() { return true; }
void writeHeader() {}
public:
MAKE_TYPE_NAME(XmlSerializer)(IfcParse::IfcFile* file, const std::string& xml_filename)
: XmlSerializer(nullptr, "")
{
this->file = file;
this->xml_filename = xml_filename;
}
void finalize();
void setFile(IfcParse::IfcFile* f) { file = f; }
void setFile(IfcParse::IfcFile*) {}
};
#endif