Files
IfcOpenShell/src/ifcgeom/GeometrySerializer.h
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
4.2 KiB
C++
Raw Normal View History

/********************************************************************************
* *
* 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"
#include "../ifcparse/logger.h"
2023-10-24 14:12:42 +02:00
#include <fstream>
2026-04-14 13:50:23 +02:00
class IFC_GEOM_API stream_or_filename {
private:
std::shared_ptr<std::ofstream> ofs_;
std::shared_ptr<std::ostringstream> oss_;
std::optional<std::string> filename_;
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()))
, filename_(fn)
, stream(*ofs_)
{}
stream_or_filename()
: oss_(new std::ostringstream)
, stream(*oss_)
{}
std::string get_value() const {
return oss_->str();
}
std::optional<std::string> filename() const {
return filename_;
}
bool is_ready() {
if (ofs_) {
return ofs_->is_open();
} else {
return true;
}
}
};
namespace ifcopenshell::geom {
2026-08-08 07:42:45 +02:00
class IFC_GEOM_API geometry_serializer : public serializer {
public:
enum read_type { READ_BREP, READ_TRIANGULATION };
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() {}
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;
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_; }
/// 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)
{
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();
}
protected:
2026-08-08 07:42:45 +02:00
ifcopenshell::geom::settings settings_;
};
2026-08-08 07:42:45 +02:00
class IFC_GEOM_API write_only_geometry_serializer : public geometry_serializer {
public:
write_only_geometry_serializer(const ifcopenshell::geom::settings& settings, ifcopenshell::logger* logger = nullptr) : geometry_serializer(settings, logger) {}
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) {
throw std::runtime_error("Not supported");
};
};
} // namespace ifcopenshell::geom
#endif