diff --git a/src/examples/IfcOpenHouse.cpp b/src/examples/IfcOpenHouse.cpp index 997ef8b58c..af9de2bebe 100644 --- a/src/examples/IfcOpenHouse.cpp +++ b/src/examples/IfcOpenHouse.cpp @@ -195,43 +195,46 @@ int main() { file.addBuildingProduct(north_wall); file.setSurfaceColour(north_wall->Representation(), wall_colour); - IfcSchema::IfcShapeRepresentation* clipped_wall_body_rep = file.addEmptyRepresentation(); - file.addBox(clipped_wall_body_rep, 5000, 360, 6000); - // The east wall geometry is clipped using two IfcHalfSpaceSolids, created from an - // 'axis 3d placement' that specifies the plane against which the geometry is clipped. - file.clipRepresentation(clipped_wall_body_rep, file.addPlacement3d(-2500, 0, 3000, -1, 0, 1), false); - file.clipRepresentation(clipped_wall_body_rep, file.addPlacement3d(2500, 0, 3000, 1, 0, 1), false); + // Two identical representations are created for the two remaining walls. Mapped items + // are not used, because it is not allowed by the standard for wall body representations. + // MappedItems are not allowed for Axis representations as per CV-2x3-161 + IfcSchema::IfcProductDefinitionShape* clipped_wall_body_reps[2]; + for (int i = 0; i < 2; ++i) { + IfcSchema::IfcShapeRepresentation* body = file.addEmptyRepresentation(); + file.addBox(body, 5000, 360, 6000); + // The wall geometry is clipped using two IfcHalfSpaceSolids, created from an + // 'axis 3d placement' that specifies the plane against which the geometry is clipped. + file.clipRepresentation(body, file.addPlacement3d(-2500, 0, 3000, -1, 0, 1), false); + file.clipRepresentation(body, file.addPlacement3d(2500, 0, 3000, 1, 0, 1), false); + file.setSurfaceColour(body, wall_colour); + + IfcSchema::IfcShapeRepresentation* axis = file.addEmptyRepresentation("Axis", "Curve2D"); + file.addAxis(axis, 5000); + + IfcSchema::IfcRepresentation::list::ptr reps(new IfcSchema::IfcRepresentation::list); + reps->push(body); + reps->push(axis); + clipped_wall_body_reps[i] = new IfcSchema::IfcProductDefinitionShape(null, null, reps); + } // Now create a wall on the east of the building, again starting with just a box shape IfcSchema::IfcWallStandardCase* east_wall = new IfcSchema::IfcWallStandardCase(guid(), file.getSingle(), - S("East wall"), null, null, file.addLocalPlacement(storey_placement, 4820, 2500, 0, 0, 0, 1, 0, 1, 0), file.addMappedItem(clipped_wall_body_rep), null + S("East wall"), null, null, file.addLocalPlacement(storey_placement, 4820, 2500, 0, 0, 0, 1, 0, 1, 0), clipped_wall_body_reps[0], null #ifdef USE_IFC4 , IfcSchema::IfcWallTypeEnum::IfcWallType_STANDARD #endif ); file.addBuildingProduct(east_wall); - file.setSurfaceColour(clipped_wall_body_rep, wall_colour); - // The east wall is copied to the west location of the house IfcSchema::IfcWallStandardCase* west_wall = new IfcSchema::IfcWallStandardCase(guid(), file.getSingle(), - S("West wall"), null, null, file.addLocalPlacement(storey_placement, -4820, 2500, 0, 0, 0, 1, 0, -1, 0), file.addMappedItem(clipped_wall_body_rep), null + S("West wall"), null, null, file.addLocalPlacement(storey_placement, -4820, 2500, 0, 0, 0, 1, 0, -1, 0), clipped_wall_body_reps[1], null #ifdef USE_IFC4 , IfcSchema::IfcWallTypeEnum::IfcWallType_STANDARD #endif ); file.addBuildingProduct(west_wall); - for (int i = 0; i < 2; ++i) { - // CV-2x3-161: MappedItems are not allowed for Axis representations - IfcSchema::IfcWallStandardCase* wall = i == 0 ? east_wall : west_wall; - IfcSchema::IfcShapeRepresentation* wall_axis_rep = file.addEmptyRepresentation("Axis", "Curve2D"); - file.addAxis(wall_axis_rep, 5000); - IfcSchema::IfcRepresentation::list::ptr reps = wall->Representation()->Representations(); - reps->push(wall_axis_rep); - wall->Representation()->setRepresentations(reps); - } - // The west wall is assigned an opening element we created for the south wall, opening elements are // not shared accross building elements, even if they share the same representation. Hence, the east // wall will not feature this opening. diff --git a/src/ifcconvert/IfcConvert.cpp b/src/ifcconvert/IfcConvert.cpp index 6cc649bad2..c500be38f3 100644 --- a/src/ifcconvert/IfcConvert.cpp +++ b/src/ifcconvert/IfcConvert.cpp @@ -32,6 +32,7 @@ #include #include +#include #include "../ifcgeom/IfcGeomIterator.h" @@ -199,9 +200,8 @@ int main(int argc, char** argv) { // Gets the set ifc types to be ignored from the command line. std::set entities; for (std::vector::const_iterator it = entity_vector.begin(); it != entity_vector.end(); ++it) { - std::string lowercase_type = *it; - std::transform(lowercase_type.begin(), lowercase_type.end(), lowercase_type.begin(), ::tolower); - entities.insert(lowercase_type); + const std::string& mixed_case_type = *it; + entities.insert(boost::to_lower_copy(mixed_case_type)); } const std::string input_filename = vmap["input-file"].as(); @@ -217,7 +217,7 @@ int main(int argc, char** argv) { } std::string output_extension = output_filename.substr(output_filename.size()-4); - std::transform(output_extension.begin(), output_extension.end(), output_extension.begin(), ::tolower); + boost::to_lower(output_extension); // If no entities are specified these are the defaults to skip from output if (entity_vector.empty()) { diff --git a/src/ifcconvert/IgesSerializer.h b/src/ifcconvert/IgesSerializer.h index 7536008d44..5e11e7b324 100644 --- a/src/ifcconvert/IgesSerializer.h +++ b/src/ifcconvert/IgesSerializer.h @@ -45,6 +45,7 @@ public: void setUnitNameAndMagnitude(const std::string& /*name*/, float magnitude) { const char* symbol = getSymbolForUnitMagnitude(magnitude); if (symbol) { + Interface_Static::SetCVal("xstep.cascade.unit", symbol); Interface_Static::SetCVal("write.iges.unit", symbol); } } diff --git a/src/ifcconvert/OpenCascadeBasedSerializer.cpp b/src/ifcconvert/OpenCascadeBasedSerializer.cpp index 77bc7e7fc9..2dbb642c3d 100644 --- a/src/ifcconvert/OpenCascadeBasedSerializer.cpp +++ b/src/ifcconvert/OpenCascadeBasedSerializer.cpp @@ -40,20 +40,14 @@ void OpenCascadeBasedSerializer::write(const IfcGeom::BRepElement* o) { for (IfcGeom::IfcRepresentationShapeItems::const_iterator it = o->geometry().begin(); it != o->geometry().end(); ++ it) { gp_GTrsf gtrsf = it->Placement(); - const std::vector& matrix = o->transformation().matrix().data(); - - // Convert the matrix back into a transformation object. The tolerance values - // are taken into consideration to reconstruct the form of the transformation. - gp_Trsf o_trsf; - o_trsf.SetValues( - matrix[0], matrix[3], matrix[6], matrix[ 9], - matrix[1], matrix[4], matrix[7], matrix[10], - matrix[2], matrix[5], matrix[8], matrix[11] -#if OCC_VERSION_HEX < 0x60800 - , Precision::Angular(), Precision::Confusion() -#endif - ); + const gp_Trsf& o_trsf = o->transformation().data(); gtrsf.PreMultiply(o_trsf); + + if (o->geometry().settings().convert_back_units()) { + gp_Trsf scale; + scale.SetScaleFactor(1.0 / o->geometry().settings().unit_magnitude()); + gtrsf.PreMultiply(scale); + } const TopoDS_Shape& s = it->Shape(); diff --git a/src/ifcconvert/StepSerializer.h b/src/ifcconvert/StepSerializer.h index 5e1493a020..ff53fa4ae4 100644 --- a/src/ifcconvert/StepSerializer.h +++ b/src/ifcconvert/StepSerializer.h @@ -51,6 +51,7 @@ public: void setUnitNameAndMagnitude(const std::string& /*name*/, float magnitude) { const char* symbol = getSymbolForUnitMagnitude(magnitude); if (symbol) { + Interface_Static::SetCVal("xstep.cascade.unit", symbol); Interface_Static::SetCVal("write.step.unit", symbol); } } diff --git a/src/ifcconvert/SvgSerializer.cpp b/src/ifcconvert/SvgSerializer.cpp index 28e01fe170..32827ec2a7 100644 --- a/src/ifcconvert/SvgSerializer.cpp +++ b/src/ifcconvert/SvgSerializer.cpp @@ -217,17 +217,7 @@ void SvgSerializer::write(const IfcGeom::BRepElement* o) { for (IfcGeom::IfcRepresentationShapeItems::const_iterator it = o->geometry().begin(); it != o->geometry().end(); ++ it) { gp_GTrsf gtrsf = it->Placement(); - gp_Trsf o_trsf; - const std::vector& matrix = o->transformation().matrix().data(); - o_trsf.SetValues( - matrix[0], matrix[3], matrix[6], matrix[ 9], - matrix[1], matrix[4], matrix[7], matrix[10], - matrix[2], matrix[5], matrix[8], matrix[11] -#if OCC_VERSION_HEX < 0x60800 - , Precision::Angular(), Precision::Confusion() -#endif - ); - gtrsf.PreMultiply(o_trsf); + const gp_Trsf& o_trsf = o->transformation().data(); const TopoDS_Shape& s = it->Shape(); bool trsf_valid = false; diff --git a/src/ifcconvert/WavefrontObjSerializer.cpp b/src/ifcconvert/WavefrontObjSerializer.cpp index a71b47b7ee..4bd3f243df 100644 --- a/src/ifcconvert/WavefrontObjSerializer.cpp +++ b/src/ifcconvert/WavefrontObjSerializer.cpp @@ -17,12 +17,13 @@ * * ********************************************************************************/ +#include +#include + #include "../ifcgeom/IfcGeomRenderStyles.h" #include "WavefrontObjSerializer.h" -#include - bool WaveFrontOBJSerializer::ready() { return obj_stream.is_open() && mtl_stream.is_open(); } @@ -70,6 +71,8 @@ void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement* obj_stream << "g " << o->unique_id() << "\n"; obj_stream << "s 1" << "\n"; + obj_stream << std::setprecision(std::numeric_limits::digits10); + const IfcGeom::Representation::Triangulation& mesh = o->geometry(); const int vcount = (int)mesh.verts().size() / 3; diff --git a/src/ifcgeom/IfcGeom.h b/src/ifcgeom/IfcGeom.h index 78d8ded544..94f6f51290 100644 --- a/src/ifcgeom/IfcGeom.h +++ b/src/ifcgeom/IfcGeom.h @@ -121,7 +121,7 @@ public: double face_area(const TopoDS_Face& f); void apply_tolerance(TopoDS_Shape& s, double t); void setValue(GeomValue var, double value); - double getValue(GeomValue var); + double getValue(GeomValue var) const; bool fill_nonmanifold_wires_with_planar_faces(TopoDS_Shape& shape); void remove_redundant_points_from_loop(TColgp_SequenceOfPnt& polygon, bool closed, double tol=-1.); diff --git a/src/ifcgeom/IfcGeomFunctions.cpp b/src/ifcgeom/IfcGeomFunctions.cpp index f05988aab0..ecf9e2b3b9 100644 --- a/src/ifcgeom/IfcGeomFunctions.cpp +++ b/src/ifcgeom/IfcGeomFunctions.cpp @@ -160,10 +160,15 @@ bool IfcGeom::Kernel::convert_openings(const IfcSchema::IfcProduct* entity, cons IfcSchema::IfcRelVoidsElement* v = *it; IfcSchema::IfcFeatureElementSubtraction* fes = v->RelatedOpeningElement(); if ( fes->is(IfcSchema::Type::IfcOpeningElement) ) { + if (!fes->hasRepresentation()) continue; // Convert the IfcRepresentation of the IfcOpeningElement gp_Trsf opening_trsf; - IfcGeom::Kernel::convert(fes->ObjectPlacement(),opening_trsf); + if (fes->hasObjectPlacement()) { + try { + convert(fes->ObjectPlacement(),opening_trsf); + } catch (...) {} + } // Move the opening into the coordinate system of the IfcProduct opening_trsf.PreMultiply(entity_trsf.Inverted()); @@ -302,10 +307,15 @@ bool IfcGeom::Kernel::convert_openings_fast(const IfcSchema::IfcProduct* entity, IfcSchema::IfcRelVoidsElement* v = *it; IfcSchema::IfcFeatureElementSubtraction* fes = v->RelatedOpeningElement(); if ( fes->is(IfcSchema::Type::IfcOpeningElement) ) { + if (!fes->hasRepresentation()) continue; // Convert the IfcRepresentation of the IfcOpeningElement gp_Trsf opening_trsf; - IfcGeom::Kernel::convert(fes->ObjectPlacement(),opening_trsf); + if (fes->hasObjectPlacement()) { + try { + convert(fes->ObjectPlacement(),opening_trsf); + } catch (...) {} + } // Move the opening into the coordinate system of the IfcProduct opening_trsf.PreMultiply(entity_trsf.Inverted()); @@ -553,7 +563,7 @@ void IfcGeom::Kernel::setValue(GeomValue var, double value) { } } -double IfcGeom::Kernel::getValue(GeomValue var) { +double IfcGeom::Kernel::getValue(GeomValue var) const { switch (var) { case GV_DEFLECTION_TOLERANCE: return deflection_tolerance; @@ -774,8 +784,14 @@ bool IfcGeom::Kernel::flatten_shape_list(const IfcGeom::IfcRepresentationShapeIt _trsf = trsf.Trsf(); trsf_valid = true; } catch (...) {} - const TopoDS_Shape moved_shape = trsf_valid ? merged.Moved(_trsf) : - BRepBuilderAPI_GTransform(merged,trsf,true).Shape(); + + const TopoDS_Shape moved_shape = trsf.Form() == gp_Identity + ? merged + : ( + trsf_valid + ? merged.Moved(_trsf) + : BRepBuilderAPI_GTransform(merged,trsf,true).Shape() + ); if (shapes.size() == 1) { result = moved_shape; @@ -807,6 +823,10 @@ bool IfcGeom::Kernel::flatten_shape_list(const IfcGeom::IfcRepresentationShapeIt } } + if (!fuse) { + result = compound; + } + const bool success = !result.IsNull(); if (success) { const double precision = getValue(GV_PRECISION); diff --git a/src/ifcgeom/IfcGeomIterator.h b/src/ifcgeom/IfcGeomIterator.h index 9dc498e938..30bf6fe9cb 100644 --- a/src/ifcgeom/IfcGeomIterator.h +++ b/src/ifcgeom/IfcGeomIterator.h @@ -180,13 +180,15 @@ namespace IfcGeom { // by the parent's context inverse attributes. continue; } - if (context->hasContextType()) { - std::string context_type_lc = context->ContextType(); - std::transform(context_type_lc.begin(), context_type_lc.end(), context_type_lc.begin(), ::tolower); - if (context_types.find(context_type_lc) != context_types.end()) { - filtered_contexts->push(context); + try { + if (context->hasContextType()) { + std::string context_type = context->ContextType(); + boost::to_lower(context_type); + if (context_types.find(context_type) != context_types.end()) { + filtered_contexts->push(context); + } } - } + } catch (const IfcParse::IfcException&) {} } // In case no contexts are identified based on their ContextType, all contexts are @@ -204,10 +206,12 @@ namespace IfcGeom { IfcSchema::IfcGeometricRepresentationContext* context = *it; representations->push(context->RepresentationsInContext()); - if (context->hasPrecision() && context->Precision() < lowest_precision_encountered) { - lowest_precision_encountered = context->Precision(); - any_precision_encountered = true; - } + try { + if (context->hasPrecision() && context->Precision() < lowest_precision_encountered) { + lowest_precision_encountered = context->Precision(); + any_precision_encountered = true; + } + } catch (const IfcParse::IfcException&) {} IfcSchema::IfcGeometricRepresentationSubContext::list::ptr sub_contexts = context->HasSubContexts(); for (jt = sub_contexts->begin(); jt != sub_contexts->end(); ++jt) { representations->push((*jt)->RepresentationsInContext()); diff --git a/src/ifcgeom/IfcGeomShapes.cpp b/src/ifcgeom/IfcGeomShapes.cpp index 489a1ee83b..af74b4369f 100644 --- a/src/ifcgeom/IfcGeomShapes.cpp +++ b/src/ifcgeom/IfcGeomShapes.cpp @@ -99,10 +99,15 @@ #include "../ifcgeom/IfcGeom.h" bool IfcGeom::Kernel::convert(const IfcSchema::IfcExtrudedAreaSolid* l, TopoDS_Shape& shape) { + const double height = l->Depth() * getValue(GV_LENGTH_UNIT); + if (height < getValue(GV_PRECISION)) { + Logger::Message(Logger::LOG_ERROR, "Non-positive extrusion height encountered for:", l->entity); + return false; + } + TopoDS_Shape face; if ( !convert_face(l->SweptArea(),face) ) return false; - const double height = l->Depth() * getValue(GV_LENGTH_UNIT); gp_Trsf trsf; IfcGeom::Kernel::convert(l->Position(),trsf); diff --git a/src/ifcgeom/IfcGeomWires.cpp b/src/ifcgeom/IfcGeomWires.cpp index a06194b46c..058302222b 100644 --- a/src/ifcgeom/IfcGeomWires.cpp +++ b/src/ifcgeom/IfcGeomWires.cpp @@ -255,7 +255,7 @@ bool IfcGeom::Kernel::convert(const IfcSchema::IfcTrimmedCurve* l, TopoDS_Wire& flts[1] -= M_PI / 2.; } } - if ( isConic && ALMOST_THE_SAME(fmod(flts[1]-flts[0],(double)(M_PI*2.0)),0.0f) ) { + if ( isConic && ALMOST_THE_SAME(fmod(flts[1]-flts[0],M_PI*2.),0.) ) { w.Add(BRepBuilderAPI_MakeEdge(curve)); } else { BRepBuilderAPI_MakeEdge e (curve,flts[0],flts[1]); diff --git a/src/ifcgeom/IfcRegister.cpp b/src/ifcgeom/IfcRegister.cpp index e00654da5d..2b7d836c63 100644 --- a/src/ifcgeom/IfcRegister.cpp +++ b/src/ifcgeom/IfcRegister.cpp @@ -24,6 +24,15 @@ using namespace IfcSchema; using namespace IfcUtil; bool IfcGeom::Kernel::convert_shapes(const IfcBaseClass* l, IfcRepresentationShapeItems& r) { + if (shape_type(l) != ST_SHAPELIST) { + TopoDS_Shape shp; + if (convert_shape(l, shp)) { + r.push_back(IfcGeom::IfcRepresentationShapeItem(shp, get_style(l->as()))); + return true; + } + return false; + } + #include "IfcRegisterConvertShapes.h" Logger::Message(Logger::LOG_ERROR,"No operation defined for:",l->entity); return false; diff --git a/src/ifcopenshell-python/ifcopenshell/geom/__init__.py b/src/ifcopenshell-python/ifcopenshell/geom/__init__.py index 8550f4013d..f4d05f86dc 100644 --- a/src/ifcopenshell-python/ifcopenshell/geom/__init__.py +++ b/src/ifcopenshell-python/ifcopenshell/geom/__init__.py @@ -31,7 +31,7 @@ def has_occ(): has_occ = has_occ() wrap_shape_creation = lambda settings, shape: shape if has_occ: - import occ_utils as utils + from . import occ_utils as utils wrap_shape_creation = lambda settings, shape: utils.create_shape_from_serialization(shape) if getattr(settings, 'use_python_opencascade', False) else shape diff --git a/src/ifcopenshell-python/ifcopenshell/geom/occ_utils.py b/src/ifcopenshell-python/ifcopenshell/geom/occ_utils.py index be5c93e8b3..591b0b1909 100644 --- a/src/ifcopenshell-python/ifcopenshell/geom/occ_utils.py +++ b/src/ifcopenshell-python/ifcopenshell/geom/occ_utils.py @@ -52,8 +52,14 @@ def get_bounding_box_center(bbox): def create_shape_from_serialization(brep_object): brep_data, occ_shape = None, None - try: brep_data = brep_object.geometry.brep_data - except: pass + is_product_shape = True + try: + brep_data = brep_object.geometry.brep_data + except: + try: + brep_data = brep_object.brep_data + is_product_shape = False + except: pass if not brep_data: return tuple(brep_object, None) try: @@ -62,5 +68,8 @@ def create_shape_from_serialization(brep_object): occ_shape = ss.Shape(ss.NbShapes()) except: pass - return tuple(brep_object, occ_shape) + if is_product_shape: + return tuple(brep_object, occ_shape) + else: + return occ_shape diff --git a/src/ifcparse/IfcParse.cpp b/src/ifcparse/IfcParse.cpp index f5bcb67237..764d5143fb 100644 --- a/src/ifcparse/IfcParse.cpp +++ b/src/ifcparse/IfcParse.cpp @@ -970,7 +970,7 @@ bool IfcFile::Init(IfcParse::IfcSpfStream* s) { const std::string guid = ifc_root->GlobalId(); if ( byguid.find(guid) != byguid.end() ) { std::stringstream ss; - ss << "Overwriting entity with guid " << guid; + ss << "Instance encountered with non-unique GlobalId " << guid; Logger::Message(Logger::LOG_WARNING,ss.str()); } byguid[guid] = ifc_root; @@ -992,7 +992,7 @@ bool IfcFile::Init(IfcParse::IfcSpfStream* s) { if ( byid.find(currentId) != byid.end() ) { std::stringstream ss; - ss << "Overwriting entity with id " << currentId; + ss << "Overwriting instance with name #" << currentId; Logger::Message(Logger::LOG_WARNING,ss.str()); } byid[currentId] = entity; diff --git a/src/ifcparse/IfcUtil.h b/src/ifcparse/IfcUtil.h index cc0548d149..f0347cffe2 100644 --- a/src/ifcparse/IfcUtil.h +++ b/src/ifcparse/IfcUtil.h @@ -89,6 +89,13 @@ namespace IfcUtil { ? static_cast(this) : static_cast(0); } + + template + const T* as() const { + return is(T::Class()) + ? static_cast(this) + : static_cast(0); + } }; class IfcBaseEntity : public IfcBaseClass { diff --git a/src/ifcparse/IfcWrite.cpp b/src/ifcparse/IfcWrite.cpp index fd7739ebed..c834e60972 100644 --- a/src/ifcparse/IfcWrite.cpp +++ b/src/ifcparse/IfcWrite.cpp @@ -19,6 +19,7 @@ #include #include +#include #include @@ -328,7 +329,7 @@ private: std::string format_double(const double& d) { std::ostringstream oss; oss.imbue(std::locale::classic()); - oss << std::setprecision(15) << d; + oss << std::setprecision(std::numeric_limits::digits10) << d; const std::string str = oss.str(); oss.str(""); std::string::size_type e = str.find('e'); diff --git a/src/ifcwrap/IfcGeomWrapper.i b/src/ifcwrap/IfcGeomWrapper.i index 1069f6ba30..84a435b69e 100644 --- a/src/ifcwrap/IfcGeomWrapper.i +++ b/src/ifcwrap/IfcGeomWrapper.i @@ -72,17 +72,35 @@ } } +// A visitor +%{ +struct ShapeRTTI : public boost::static_visitor +{ + PyObject* operator()(IfcGeom::Element* elem) const { + IfcGeom::SerializedElement* serialized_elem = dynamic_cast*>(elem); + IfcGeom::TriangulationElement* triangulation_elem = dynamic_cast*>(elem); + if (triangulation_elem) { + return SWIG_NewPointerObj(SWIG_as_voidptr(triangulation_elem), SWIGTYPE_p_IfcGeom__TriangulationElementT_double_t, SWIG_POINTER_OWN); + } else if (serialized_elem) { + return SWIG_NewPointerObj(SWIG_as_voidptr(serialized_elem), SWIGTYPE_p_IfcGeom__SerializedElementT_double_t, SWIG_POINTER_OWN); + } + } + PyObject* operator()(IfcGeom::Representation::Representation* representation) const { + IfcGeom::Representation::Serialization* serialized_representation = dynamic_cast(representation); + IfcGeom::Representation::Triangulation* triangulated_representation = dynamic_cast*>(representation); + if (serialized_representation) { + return SWIG_NewPointerObj(SWIG_as_voidptr(serialized_representation), SWIGTYPE_p_IfcGeom__Representation__Serialization, SWIG_POINTER_OWN); + } else if (triangulated_representation) { + return SWIG_NewPointerObj(SWIG_as_voidptr(triangulated_representation), SWIGTYPE_p_IfcGeom__Representation__TriangulationT_double_t, SWIG_POINTER_OWN); + } + } +}; +%} + // Note that these elements ARE to be owned by SWIG/Python %typemap(out) boost::variant*, IfcGeom::Representation::Representation*> { // See which type is set and return appropriate - IfcGeom::Element* elem = boost::get*>($1); - IfcGeom::SerializedElement* serialized_elem = dynamic_cast*>(elem); - IfcGeom::TriangulationElement* triangulation_elem = dynamic_cast*>(elem); - if (triangulation_elem) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(triangulation_elem), SWIGTYPE_p_IfcGeom__TriangulationElementT_double_t, SWIG_POINTER_OWN); - } else if (serialized_elem) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(serialized_elem), SWIGTYPE_p_IfcGeom__SerializedElementT_double_t, SWIG_POINTER_OWN); - } + $result = boost::apply_visitor(ShapeRTTI(), $1); } // This does not seem to work: @@ -218,25 +236,25 @@ %inline %{ boost::variant*, IfcGeom::Representation::Representation*> create_shape(IfcGeom::IteratorSettings& settings, IfcParse::IfcLateBoundEntity* instance, IfcParse::IfcLateBoundEntity* representation = 0) { + IfcParse::IfcFile* file = instance->entity->file; + IfcSchema::IfcProject::list::ptr projects = file->entitiesByType(); + if (projects->size() != 1) { + throw IfcParse::IfcException("Not a single IfcProject instance"); + } + IfcSchema::IfcProject* project = *projects->begin(); + + IfcGeom::Kernel kernel; + kernel.setValue(IfcGeom::Kernel::GV_MAX_FACES_TO_SEW, settings.sew_shells() ? 1000 : -1); + kernel.setValue(IfcGeom::Kernel::GV_DIMENSIONALITY, (settings.include_curves() ? (settings.exclude_solids_and_surfaces() ? -1. : 0.) : +1.)); + std::pair length_unit = kernel.initializeUnits(project->UnitsInContext()); + if (instance->is(IfcSchema::Type::IfcProduct)) { if (representation) { if (!representation->is(IfcSchema::Type::IfcRepresentation)) { throw IfcParse::IfcException("Supplied representation not of type IfcRepresentation"); } } - - IfcParse::IfcFile* file = instance->entity->file; - - IfcSchema::IfcProject::list::ptr projects = file->entitiesByType(); - if (projects->size() != 1) { - throw IfcParse::IfcException("Not a single IfcProject instance"); - } - IfcSchema::IfcProject* project = *projects->begin(); - - IfcGeom::Kernel kernel; - kernel.setValue(IfcGeom::Kernel::GV_MAX_FACES_TO_SEW, settings.sew_shells() ? 1000 : -1); - kernel.setValue(IfcGeom::Kernel::GV_DIMENSIONALITY, (settings.include_curves() ? (settings.exclude_solids_and_surfaces() ? -1. : 0.) : +1.)); - + IfcSchema::IfcProduct* product = (IfcSchema::IfcProduct*) instance; if (!representation && !product->hasRepresentation()) { @@ -318,7 +336,6 @@ if (context->hasPrecision()) { precision = context->Precision(); } - std::pair length_unit = kernel.initializeUnits(project->UnitsInContext()); precision *= length_unit.second; // Some arbitrary factor that has proven to work better for the models in the set of test files. @@ -342,7 +359,28 @@ throw IfcParse::IfcException("No element to return based on provided settings"); } } else { - throw IfcParse::IfcException("Only obtaining representations for IfcProduct instances is currently supported"); + if (!representation) { + if (instance->is(IfcSchema::Type::IfcRepresentationItem) || instance->is(IfcSchema::Type::IfcRepresentation)) { + IfcGeom::IfcRepresentationShapeItems shapes; + if (kernel.convert_shapes(instance, shapes)) { + IfcGeom::ElementSettings element_settings(settings, kernel.getValue(IfcGeom::Kernel::GV_LENGTH_UNIT), IfcSchema::Type::ToString(instance->type())); + IfcGeom::Representation::BRep brep(element_settings, instance->entity->id(), shapes); + try { + if (settings.use_brep_data()) { + return new IfcGeom::Representation::Serialization(brep); + } else if (!settings.disable_triangulation()) { + return new IfcGeom::Representation::Triangulation(brep); + } + } catch (...) { + throw IfcParse::IfcException("Error during shape serialization"); + } + } else { + throw IfcParse::IfcException("Geometrical element not understood"); + } + } + } else { + throw IfcParse::IfcException("Invalid additional representation specified"); + } } } %}