mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-15 18:14:08 +00:00
Revive XML serializer
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user