From e4ec1dc4574df02d73549f9e6ad91844a537156a Mon Sep 17 00:00:00 2001 From: Chetan Date: Wed, 7 Jun 2023 16:54:28 +0800 Subject: [PATCH] If an IFC file has an IFCRELASSOCIATESMATERIAL but did not supply an IFCMATERIAL, IfcConvert will crash. This commit prevents the crash, catches the exception and prints an error on the terminal --- src/ifcgeom/IfcGeom.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/ifcgeom/IfcGeom.cpp b/src/ifcgeom/IfcGeom.cpp index a489d3caf9..ae73bcbe3a 100644 --- a/src/ifcgeom/IfcGeom.cpp +++ b/src/ifcgeom/IfcGeom.cpp @@ -554,17 +554,26 @@ const IfcSchema::IfcMaterial* IfcGeom::Kernel::get_single_material_association(c IfcSchema::IfcMaterial* single_material = 0; IfcSchema::IfcRelAssociatesMaterial::list::ptr associated_materials = product->HasAssociations()->as(); if (associated_materials->size() == 1) { - IfcSchema::IfcMaterialSelect* associated_material = (*associated_materials->begin())->RelatingMaterial(); - single_material = associated_material->as(); - // NB: IfcMaterialLayerSets are also considered, regardless of --enable-layerset-slicing. Picking - // the first material (in accordance with other viewers) when layerset-slicing is disabled. - if (!single_material && associated_material->as()) { - IfcSchema::IfcMaterialLayerSet* layerset = associated_material->as()->ForLayerSet(); - if (getValue(GV_LAYERSET_FIRST) > 0.0 ? layerset->MaterialLayers()->size() >= 1 : layerset->MaterialLayers()->size() == 1) { - IfcSchema::IfcMaterialLayer* layer = (*layerset->MaterialLayers()->begin()); - if (layer->Material()) { - single_material = layer->Material(); + IfcSchema::IfcMaterialSelect* associated_material = nullptr; + + try { + associated_material = (*associated_materials->begin())->RelatingMaterial(); + } catch(IfcParse::IfcException& e) { + Logger::Error(e.what()); + } + + if (associated_material) { + single_material = associated_material->as(); + // NB: IfcMaterialLayerSets are also considered, regardless of --enable-layerset-slicing. Picking + // the first material (in accordance with other viewers) when layerset-slicing is disabled. + if (!single_material && associated_material->as()) { + IfcSchema::IfcMaterialLayerSet* layerset = associated_material->as()->ForLayerSet(); + if (getValue(GV_LAYERSET_FIRST) > 0.0 ? layerset->MaterialLayers()->size() >= 1 : layerset->MaterialLayers()->size() == 1) { + IfcSchema::IfcMaterialLayer* layer = (*layerset->MaterialLayers()->begin()); + if (layer->Material()) { + single_material = layer->Material(); + } } } }