2013-03-24 10:48:39 +00:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* 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 GEOMETRYSERIALIZER_H
|
|
|
|
|
#define GEOMETRYSERIALIZER_H
|
|
|
|
|
|
2023-03-21 20:15:01 +01:00
|
|
|
#include "../ifcgeom/Serializer.h"
|
|
|
|
|
#include "../ifcgeom/IfcGeomElement.h"
|
2026-07-22 16:04:45 +05:00
|
|
|
#include "../ifcparse/logger.h"
|
2023-10-24 14:12:42 +02:00
|
|
|
#include <fstream>
|
2013-03-24 10:48:39 +00:00
|
|
|
|
2026-04-14 13:50:23 +02:00
|
|
|
class IFC_GEOM_API stream_or_filename {
|
2021-07-11 15:01:31 +02:00
|
|
|
private:
|
|
|
|
|
std::shared_ptr<std::ofstream> ofs_;
|
|
|
|
|
std::shared_ptr<std::ostringstream> oss_;
|
2026-01-04 10:40:02 +01:00
|
|
|
std::optional<std::string> filename_;
|
2021-07-11 15:01:31 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::ostream& stream;
|
|
|
|
|
|
|
|
|
|
stream_or_filename(const std::string& fn)
|
2026-03-31 15:32:36 +02:00
|
|
|
: ofs_(new std::ofstream(ifcopenshell::path::from_utf8(fn).c_str()))
|
2026-05-09 21:03:15 +02:00
|
|
|
, filename_(fn)
|
2021-07-11 15:01:31 +02:00
|
|
|
, stream(*ofs_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
stream_or_filename()
|
|
|
|
|
: oss_(new std::ostringstream)
|
|
|
|
|
, stream(*oss_)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
std::string get_value() const {
|
|
|
|
|
return oss_->str();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-04 10:40:02 +01:00
|
|
|
std::optional<std::string> filename() const {
|
2021-07-11 15:01:31 +02:00
|
|
|
return filename_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool is_ready() {
|
|
|
|
|
if (ofs_) {
|
|
|
|
|
return ofs_->is_open();
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
namespace ifcopenshell::geom {
|
|
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
class IFC_GEOM_API geometry_serializer : public serializer {
|
2013-03-24 10:48:39 +00:00
|
|
|
public:
|
2021-10-18 11:38:01 +02:00
|
|
|
enum read_type { READ_BREP, READ_TRIANGULATION };
|
|
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
geometry_serializer(const ifcopenshell::geom::settings& settings, ifcopenshell::logger* logger = nullptr)
|
|
|
|
|
: serializer(ifcopenshell::logger_or_root(logger))
|
2023-11-15 10:32:20 +01:00
|
|
|
, settings_(settings)
|
|
|
|
|
{}
|
2026-08-08 07:42:45 +02:00
|
|
|
virtual ~geometry_serializer() {}
|
2015-02-06 13:07:50 +00:00
|
|
|
|
|
|
|
|
virtual bool isTesselated() const = 0;
|
2026-08-08 07:42:45 +02:00
|
|
|
virtual void write(const ifcopenshell::geom::triangulation_element* o) = 0;
|
|
|
|
|
virtual void write(const ifcopenshell::geom::brep_element* o) = 0;
|
2013-12-31 14:10:38 +00:00
|
|
|
virtual void setUnitNameAndMagnitude(const std::string& name, float magnitude) = 0;
|
2026-08-08 07:42:45 +02:00
|
|
|
virtual ifcopenshell::geom::element* read(ifcopenshell::file& f, const std::string& guid, const std::string& representation_id, read_type rt = READ_BREP) = 0;
|
2023-11-15 10:32:20 +01:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
const ifcopenshell::geom::settings& settings() const { return settings_; }
|
|
|
|
|
ifcopenshell::geom::settings& settings() { return settings_; }
|
2016-03-10 11:35:32 +02:00
|
|
|
|
2018-06-07 14:34:25 +03:00
|
|
|
/// Returns ID for the object depending on the used setting.
|
2026-08-08 07:42:45 +02:00
|
|
|
virtual std::string object_id(const ifcopenshell::geom::element* o)
|
2018-06-07 14:34:25 +03:00
|
|
|
{
|
2026-08-08 07:42:45 +02:00
|
|
|
if (settings_.get<ifcopenshell::geom::settings::UseElementGuids>().get()) return o->guid();
|
|
|
|
|
if (settings_.get<ifcopenshell::geom::settings::UseElementNames>().get()) return o->name();
|
|
|
|
|
if (settings_.get<ifcopenshell::geom::settings::UseElementStepIds>().get()) return "id-" + boost::lexical_cast<std::string>(o->id());
|
2020-08-16 14:35:39 +02:00
|
|
|
return o->unique_id();
|
2018-06-07 14:34:25 +03:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 11:35:32 +02:00
|
|
|
protected:
|
2026-08-08 07:42:45 +02:00
|
|
|
ifcopenshell::geom::settings settings_;
|
2013-03-24 10:48:39 +00:00
|
|
|
};
|
|
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
class IFC_GEOM_API write_only_geometry_serializer : public geometry_serializer {
|
2021-10-18 11:38:01 +02:00
|
|
|
public:
|
2026-08-08 13:58:39 +02:00
|
|
|
write_only_geometry_serializer(const ifcopenshell::geom::settings& settings, ifcopenshell::logger* logger = nullptr) : geometry_serializer(settings, logger) {}
|
2021-10-18 11:38:01 +02:00
|
|
|
|
2026-08-08 07:42:45 +02:00
|
|
|
virtual ifcopenshell::geom::element* read(ifcopenshell::file&, const std::string&, const std::string&, read_type = READ_BREP) {
|
2021-10-18 11:38:01 +02:00
|
|
|
throw std::runtime_error("Not supported");
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-08 13:58:39 +02:00
|
|
|
} // namespace ifcopenshell::geom
|
|
|
|
|
|
2016-03-10 11:35:32 +02:00
|
|
|
#endif
|