Skip to content

Commit 9c1660a

Browse files
hypirionaothms
authored andcommitted
Catch and inform about errors
The changes in how specular roughness and transparency is read in was done to ensure type safety. If you added "transparency": "0.8" to the file, then that would be silently ignored. In the new style, it instead errors out if the type cannot be converted to a double.
1 parent 7e9e56c commit 9c1660a

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/ifcconvert/IfcConvert.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,15 @@ int main(int argc, char** argv)
497497
}
498498
}
499499

500-
if (!default_material_filename.empty()) {
501-
IfcGeom::set_default_style(default_material_filename);
502-
}
500+
if (!default_material_filename.empty()) {
501+
try {
502+
IfcGeom::set_default_style(default_material_filename);
503+
} catch (const std::exception& e) {
504+
std::cerr << "[Error] Could not read default material file " << default_material_filename << ":" << std::endl;
505+
std::cerr << e.what() << std::endl;
506+
return EXIT_FAILURE;
507+
}
508+
}
503509

504510
/// @todo Clean up this filter code further.
505511
std::vector<geom_filter> used_filters;

src/ifcgeom/IfcGeomRenderStyles.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ void InitDefaultMaterials() {
176176
default_materials_initialized = true;
177177
}
178178

179+
boost::optional<IfcGeom::SurfaceStyle::ColorComponent> read_colour_component(const boost::optional<pt::ptree&> list) {
180+
if (!list) {
181+
return boost::none;
182+
}
183+
double rgb[3];
184+
int i = 0;
185+
for (pt::ptree::value_type &colour : list.get()) {
186+
if (3 <= i) {
187+
throw std::runtime_error("rgb array over 3 elements large");
188+
}
189+
rgb[i] = colour.second.get_value<double>();
190+
i++;
191+
}
192+
if (i != 3) {
193+
throw std::runtime_error("rgb array less than 3 elements large (was " + std::to_string(i) + ")");
194+
}
195+
return IfcGeom::SurfaceStyle::ColorComponent(rgb[0], rgb[1], rgb[2]);
196+
}
197+
179198
void IfcGeom::set_default_style(const std::string& json_file) {
180199
if (!default_materials_initialized) InitDefaultMaterials();
181200
default_materials.clear();
@@ -189,30 +208,17 @@ void IfcGeom::set_default_style(const std::string& json_file) {
189208

190209
pt::ptree material = material_pair.second;
191210
boost::optional<pt::ptree&> diffuse = material.get_child_optional("diffuse");
192-
if (diffuse) {
193-
double rgb[3];
194-
int i = 0;
195-
for (pt::ptree::value_type &colour : diffuse.get()) {
196-
rgb[i] = colour.second.get_value<double>();
197-
i++;
198-
}
199-
default_materials[name].Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(rgb[0], rgb[1], rgb[2]));
200-
}
211+
default_materials[name].Diffuse() = read_colour_component(diffuse);
212+
201213
boost::optional<pt::ptree&> specular = material.get_child_optional("specular");
202-
if (specular) {
203-
double rgb[3];
204-
int i = 0;
205-
for (pt::ptree::value_type &colour : specular.get()) {
206-
rgb[i] = colour.second.get_value<double>();
207-
i++;
208-
}
209-
default_materials[name].Specular().reset(IfcGeom::SurfaceStyle::ColorComponent(rgb[0], rgb[1], rgb[2]));
214+
default_materials[name].Specular() = read_colour_component(specular);
215+
216+
if (material.get_child_optional("specular-roughness")) {
217+
default_materials[name].Specularity().reset(1.0 / material.get<double>("specular-roughness"));
210218
}
211-
boost::optional<double> specular_roughness = material.get_optional<double>("specular-roughness");
212-
if (specular_roughness) {
213-
default_materials[name].Specularity().reset(1.0 / specular_roughness.get());
219+
if (material.get_child_optional("transparency")) {
220+
default_materials[name].Transparency() = material.get<double>("transparency");
214221
}
215-
default_materials[name].Transparency() = material.get_optional<double>("transparency");
216222
}
217223

218224
// Is "*" present? If yes, remove it and make it the default style.

0 commit comments

Comments
 (0)