Files
IfcOpenShell/src/serializers/SvgSerializer.h
T

112 lines
5.3 KiB
C++
Raw Normal View History

/********************************************************************************
* *
* Copyright 2015 IfcOpenShell and ROOT B.V. *
* *
* 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 SVGSERIALIZER_H
#define SVGSERIALIZER_H
2018-03-16 13:49:11 +01:00
#include "../serializers/GeometrySerializer.h"
#include "../serializers/util.h"
2019-02-08 15:18:55 +01:00
#include "../ifcparse/utils.h"
#include <sstream>
#include <string>
#include <limits>
2020-05-10 11:48:39 +02:00
struct storey_sorter {
bool operator()(IfcUtil::IfcBaseEntity* a, IfcUtil::IfcBaseEntity* b) const {
const bool a_is_storey = a->declaration().is("IfcBuildingStorey");
const bool b_is_storey = b->declaration().is("IfcBuildingStorey");
if (a_is_storey && b_is_storey) {
boost::optional<double> a_elev, b_elev;
try {
a_elev = static_cast<double>(*a->get("Elevation"));
b_elev = static_cast<double>(*b->get("Elevation"));
} catch (...) {};
if (a_elev && b_elev) {
return std::less<double>()(*a_elev, *b_elev);
}
boost::optional<std::string> a_name, b_name;
try {
a_name = static_cast<std::string>(*a->get("Name"));
b_name = static_cast<std::string>(*b->get("Name"));
} catch (...) {};
if (a_name && b_name) {
return std::less<std::string>()(*a_name, *b_name);
}
}
return std::less<IfcUtil::IfcBaseEntity*>()(a, b);
}
};
class SvgSerializer : public GeometrySerializer {
public:
typedef std::pair<std::string, std::vector<util::string_buffer> > path_object;
protected:
std::ofstream svg_file;
double xmin, ymin, xmax, ymax, width, height;
2020-07-11 13:39:09 +02:00
boost::optional<std::vector<std::pair<double, IfcUtil::IfcBaseEntity*>>> section_heights;
2020-07-11 23:26:21 +02:00
bool rescale, print_space_names_, print_space_areas_, draw_door_arcs_;
2020-05-10 11:48:39 +02:00
std::multimap<IfcUtil::IfcBaseEntity*, path_object, storey_sorter> paths;
2016-01-25 14:42:41 +01:00
std::vector< boost::shared_ptr<util::string_buffer::float_item> > xcoords;
std::vector< boost::shared_ptr<util::string_buffer::float_item> > ycoords;
std::vector< boost::shared_ptr<util::string_buffer::float_item> > radii;
IfcParse::IfcFile* file;
2017-12-20 12:40:15 +01:00
IfcUtil::IfcBaseEntity* storey_;
public:
SvgSerializer(const std::string& out_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
2019-02-08 15:18:55 +01:00
, svg_file(IfcUtil::path::from_utf8(out_filename).c_str())
, xmin(+std::numeric_limits<double>::infinity())
, ymin(+std::numeric_limits<double>::infinity())
2016-05-15 15:47:21 +02:00
, xmax(-std::numeric_limits<double>::infinity())
, ymax(-std::numeric_limits<double>::infinity())
, rescale(false)
, file(0)
, storey_(0)
{}
void addXCoordinate(const boost::shared_ptr<util::string_buffer::float_item>& fi) { xcoords.push_back(fi); }
void addYCoordinate(const boost::shared_ptr<util::string_buffer::float_item>& fi) { ycoords.push_back(fi); }
void addSizeComponent(const boost::shared_ptr<util::string_buffer::float_item>& fi) { radii.push_back(fi); }
void growBoundingBox(double x, double y) { if (x < xmin) xmin = x; if (x > xmax) xmax = x; if (y < ymin) ymin = y; if (y > ymax) ymax = y; }
void writeHeader();
bool ready();
void write(const IfcGeom::TriangulationElement<real_t>* /*o*/) {}
void write(const IfcGeom::BRepElement<real_t>* o);
void write(path_object& p, const TopoDS_Wire& wire);
2017-12-20 12:40:15 +01:00
path_object& start_path(IfcUtil::IfcBaseEntity* storey, const std::string& id);
bool isTesselated() const { return false; }
void finalize();
void setUnitNameAndMagnitude(const std::string& /*name*/, float /*magnitude*/) {}
void setFile(IfcParse::IfcFile* f);
void setBoundingRectangle(double width, double height);
2020-07-11 13:39:09 +02:00
void setSectionHeight(double h, IfcUtil::IfcBaseEntity* storey = 0);
void setSectionHeightsFromStoreys(double offset=1.);
2020-04-01 12:19:56 +02:00
void setPrintSpaceNames(bool b) { print_space_names_ = b; }
2020-05-10 11:10:30 +02:00
void setPrintSpaceAreas(bool b) { print_space_areas_ = b; }
2020-07-11 23:26:21 +02:00
void setDrawDoorArcs(bool b) { draw_door_arcs_ = b; }
std::string nameElement(const IfcGeom::Element<real_t>* elem);
2017-12-20 12:40:15 +01:00
std::string nameElement(const IfcUtil::IfcBaseEntity* elem);
};
#endif