mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-24 21:39:59 +00:00
Revive XML serializer
This commit is contained in:
@@ -30,6 +30,7 @@
|
|||||||
#include "../serializers/IgesSerializer.h"
|
#include "../serializers/IgesSerializer.h"
|
||||||
#include "../serializers/StepSerializer.h"
|
#include "../serializers/StepSerializer.h"
|
||||||
#include "../serializers/WavefrontObjSerializer.h"
|
#include "../serializers/WavefrontObjSerializer.h"
|
||||||
|
#include "../serializers/XmlSerializer.h"
|
||||||
#include "../serializers/SvgSerializer.h"
|
#include "../serializers/SvgSerializer.h"
|
||||||
|
|
||||||
#include "../ifcgeom_schema_agnostic/IfcGeomIterator.h"
|
#include "../ifcgeom_schema_agnostic/IfcGeomIterator.h"
|
||||||
@@ -69,6 +70,7 @@ void print_usage(bool suggest_help = true)
|
|||||||
#endif
|
#endif
|
||||||
<< " .stp STEP Standard for the Exchange of Product Data\n"
|
<< " .stp STEP Standard for the Exchange of Product Data\n"
|
||||||
<< " .igs IGES Initial Graphics Exchange Specification\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"
|
<< " .svg SVG Scalable Vector Graphics (2D floor plan)\n"
|
||||||
<< "\n"
|
<< "\n"
|
||||||
<< "If no output filename given, <input>." + DEFAULT_EXTENSION + " will be used as the output file.\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;
|
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()) {
|
if (!filter_filename.empty()) {
|
||||||
size_t num_filters = read_filters_from_file(filter_filename, include_filter, include_traverse_filter, exclude_filter, exclude_traverse_filter);
|
size_t num_filters = read_filters_from_file(filter_filename, include_filter, include_traverse_filter, exclude_filter, exclude_traverse_filter);
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ inline static bool ALMOST_THE_SAME(const T& a, const T& b, double tolerance=ALMO
|
|||||||
#include <TopTools_ListOfShape.hxx>
|
#include <TopTools_ListOfShape.hxx>
|
||||||
#include <BOPAlgo_Operation.hxx>
|
#include <BOPAlgo_Operation.hxx>
|
||||||
|
|
||||||
|
#include "../ifcparse/macros.h"
|
||||||
#include "../ifcparse/IfcParse.h"
|
#include "../ifcparse/IfcParse.h"
|
||||||
#include "../ifcparse/IfcBaseClass.h"
|
#include "../ifcparse/IfcBaseClass.h"
|
||||||
|
|
||||||
@@ -75,14 +76,8 @@ if ( it != cache.T.end() ) { e = it->second; return true; }
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAKE_TYPE_NAME__(a, b) a ## b
|
#define INCLUDE_PARENT_DIR(x) STRINGIFY(../ifcparse/x.h)
|
||||||
#define MAKE_TYPE_NAME_(a, b) MAKE_TYPE_NAME__(a, b)
|
#include INCLUDE_PARENT_DIR(IfcSchema)
|
||||||
#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)
|
|
||||||
|
|
||||||
namespace IfcGeom {
|
namespace IfcGeom {
|
||||||
|
|
||||||
|
|||||||
@@ -133,6 +133,7 @@
|
|||||||
|
|
||||||
#include <Standard_Version.hxx>
|
#include <Standard_Version.hxx>
|
||||||
|
|
||||||
|
#include "../ifcparse/macros.h"
|
||||||
#include "../ifcparse/IfcSIPrefix.h"
|
#include "../ifcparse/IfcSIPrefix.h"
|
||||||
#include "../ifcparse/IfcFile.h"
|
#include "../ifcparse/IfcFile.h"
|
||||||
#include "../ifcgeom/IfcGeom.h"
|
#include "../ifcgeom/IfcGeom.h"
|
||||||
@@ -145,11 +146,6 @@
|
|||||||
#endif
|
#endif
|
||||||
#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 {
|
namespace {
|
||||||
struct factory_t {
|
struct factory_t {
|
||||||
IfcGeom::Kernel* operator()(IfcParse::IfcFile* file) const {
|
IfcGeom::Kernel* operator()(IfcParse::IfcFile* file) const {
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
#define SCHEMA_METHOD
|
||||||
|
|
||||||
|
#include "../serializers/Serializer.h"
|
||||||
#include "../ifcparse/IfcFile.h"
|
#include "../ifcparse/IfcFile.h"
|
||||||
|
|
||||||
#include <boost/function.hpp>
|
#include <boost/function.hpp>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
template <typename T>
|
class XmlSerializer : public Serializer {
|
||||||
class schema_delegate {
|
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 {
|
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> {
|
class Factory : public std::map<std::string, fn> {
|
||||||
public:
|
public:
|
||||||
Factory();
|
Factory();
|
||||||
void bind(const std::string& schema_name, fn);
|
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;
|
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 {
|
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
|
// Format an IFC attribute and maybe returns as string. Only literal scalar
|
||||||
// values are converted. Things like entity instances and lists are omitted.
|
// 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())) {
|
} else if (e->declaration().is(IfcSchema::IfcLocalPlacement::Class())) {
|
||||||
IfcSchema::IfcLocalPlacement* placement = e->as<IfcSchema::IfcLocalPlacement>();
|
IfcSchema::IfcLocalPlacement* placement = e->as<IfcSchema::IfcLocalPlacement>();
|
||||||
gp_Trsf trsf;
|
gp_Trsf trsf;
|
||||||
IfcGeom::Kernel kernel;
|
IfcGeom::MAKE_TYPE_NAME(Kernel) kernel;
|
||||||
|
|
||||||
if (kernel.convert(placement, trsf)) {
|
if (kernel.convert(placement, trsf)) {
|
||||||
std::stringstream stream;
|
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::string argument_name = instance->declaration().attribute_by_index(i)->name();
|
||||||
std::map<std::string, std::string>::const_iterator argument_name_it;
|
std::map<std::string, std::string>::const_iterator argument_name_it;
|
||||||
argument_name_it = argument_name_map.find(argument_name);
|
argument_name_it = MAKE_TYPE_NAME(argument_name_map).find(argument_name);
|
||||||
if (argument_name_it != argument_name_map.end()) {
|
if (argument_name_it != MAKE_TYPE_NAME(argument_name_map).end()) {
|
||||||
argument_name = argument_name_it->second;
|
argument_name = argument_name_it->second;
|
||||||
}
|
}
|
||||||
const IfcUtil::ArgumentType argument_type = instance->data().getArgument(i)->type();
|
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
|
} // ~unnamed namespace
|
||||||
|
|
||||||
void XmlSerializer::finalize() {
|
void MAKE_TYPE_NAME(XmlSerializer)::finalize() {
|
||||||
argument_name_map.insert(std::make_pair("GlobalId", "id"));
|
MAKE_TYPE_NAME(argument_name_map).insert(std::make_pair("GlobalId", "id"));
|
||||||
|
|
||||||
IfcSchema::IfcProject::list::ptr projects = file->instances_by_type<IfcSchema::IfcProject>();
|
IfcSchema::IfcProject::list::ptr projects = file->instances_by_type<IfcSchema::IfcProject>();
|
||||||
if (projects->size() != 1) {
|
if (projects->size() != 1) {
|
||||||
|
|||||||
@@ -17,25 +17,29 @@
|
|||||||
* *
|
* *
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
#ifndef XMLSERIALIZER_H
|
#ifndef XMLSERIALIZERIMPL_H
|
||||||
#define XMLSERIALIZER_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:
|
private:
|
||||||
IfcParse::IfcFile* file;
|
IfcParse::IfcFile* file;
|
||||||
std::string xml_filename;
|
|
||||||
public:
|
|
||||||
XmlSerializer(const std::string& xml_filename)
|
|
||||||
: Serializer()
|
|
||||||
, xml_filename(xml_filename)
|
|
||||||
{}
|
|
||||||
|
|
||||||
bool ready() { return true; }
|
public:
|
||||||
void writeHeader() {}
|
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 finalize();
|
||||||
void setFile(IfcParse::IfcFile* f) { file = f; }
|
void setFile(IfcParse::IfcFile*) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user