//! make a clean wire with sorted, oriented, connected, etc edges TopoDS_Wire EdgeWalker::makeCleanWire(std::vector<TopoDS_Edge> edges, double tol) { TopoDS_Wire result; BRepBuilderAPI_MakeWire mkWire; ShapeFix_ShapeTolerance sTol; Handle(ShapeExtend_WireData) wireData = new ShapeExtend_WireData(); for (auto e:edges) { wireData->Add(e); } Handle(ShapeFix_Wire) fixer = new ShapeFix_Wire; fixer->Load(wireData); fixer->Perform(); fixer->FixReorder(); fixer->SetMaxTolerance(tol); fixer->ClosedWireMode() = Standard_True; fixer->FixConnected(Precision::Confusion()); fixer->FixClosed(Precision::Confusion()); for (int i = 1; i <= wireData->NbEdges(); i ++) { TopoDS_Edge edge = fixer->WireData()->Edge(i); sTol.SetTolerance(edge, tol, TopAbs_VERTEX); mkWire.Add(edge); } result = mkWire.Wire(); return result; }
bool IfcGeom::convert_wire_to_face(const TopoDS_Wire& wire, TopoDS_Face& face) { BRepBuilderAPI_MakeFace mf(wire, false); BRepBuilderAPI_FaceError er = mf.Error(); if ( er == BRepBuilderAPI_NotPlanar ) { ShapeFix_ShapeTolerance FTol; FTol.SetTolerance(wire, 0.01, TopAbs_WIRE); mf.~BRepBuilderAPI_MakeFace(); new (&mf) BRepBuilderAPI_MakeFace(wire); er = mf.Error(); } if ( er != BRepBuilderAPI_FaceDone ) return false; face = mf.Face(); return true; }
bool IfcGeom::Kernel::convert(const IfcSchema::IfcCompositeCurve* l, TopoDS_Wire& wire) { if ( getValue(GV_PLANEANGLE_UNIT)<0 ) { Logger::Message(Logger::LOG_WARNING,"Creating a composite curve without unit information:",l->entity); // Temporarily pretend we do have unit information setValue(GV_PLANEANGLE_UNIT,1.0); bool succes_radians = false; bool succes_degrees = false; bool use_radians = false; bool use_degrees = false; // First try radians TopoDS_Wire wire_radians, wire_degrees; try { succes_radians = IfcGeom::Kernel::convert(l,wire_radians); } catch (...) {} // Now try degrees setValue(GV_PLANEANGLE_UNIT,0.0174532925199433); try { succes_degrees = IfcGeom::Kernel::convert(l,wire_degrees); } catch (...) {} // Restore to unknown unit state setValue(GV_PLANEANGLE_UNIT,-1.0); if ( succes_degrees && ! succes_radians ) { use_degrees = true; } else if ( succes_radians && ! succes_degrees ) { use_radians = true; } else if ( succes_radians && succes_degrees ) { if ( wire_degrees.Closed() && ! wire_radians.Closed() ) { use_degrees = true; } else if ( wire_radians.Closed() && ! wire_degrees.Closed() ) { use_radians = true; } else { // No heuristic left to prefer the one over the other, // apparently both variants are equally succesful. // The curve might be composed of only straight segments. // Let's go with the wire created using radians as that // at least is a SI unit. use_radians = true; } } if ( use_radians ) { Logger::Message(Logger::LOG_NOTICE,"Used radians to create composite curve"); wire = wire_radians; } else if ( use_degrees ) { Logger::Message(Logger::LOG_NOTICE,"Used degrees to create composite curve"); wire = wire_degrees; } return use_radians || use_degrees; } IfcSchema::IfcCompositeCurveSegment::list::ptr segments = l->Segments(); BRepBuilderAPI_MakeWire w; //TopoDS_Vertex last_vertex; for( IfcSchema::IfcCompositeCurveSegment::list::it it = segments->begin(); it != segments->end(); ++ it ) { IfcSchema::IfcCurve* curve = (*it)->ParentCurve(); TopoDS_Wire wire2; if ( !convert_wire(curve,wire2) ) { Logger::Message(Logger::LOG_ERROR,"Failed to convert curve:",curve->entity); continue; } if ( ! (*it)->SameSense() ) wire2.Reverse(); ShapeFix_ShapeTolerance FTol; FTol.SetTolerance(wire2, getValue(GV_WIRE_CREATION_TOLERANCE), TopAbs_WIRE); /*if ( it != segments->begin() ) { TopExp_Explorer exp (wire2,TopAbs_VERTEX); const TopoDS_Vertex& first_vertex = TopoDS::Vertex(exp.Current()); gp_Pnt first = BRep_Tool::Pnt(first_vertex); gp_Pnt last = BRep_Tool::Pnt(last_vertex); Standard_Real distance = first.Distance(last); if ( distance > ALMOST_ZERO ) { w.Add( BRepBuilderAPI_MakeEdge( last_vertex, first_vertex ) ); } }*/ w.Add(wire2); //last_vertex = w.Vertex(); if ( w.Error() != BRepBuilderAPI_WireDone ) { Logger::Message(Logger::LOG_ERROR,"Failed to join curve segments:",l->entity); return false; } } wire = w.Wire(); return true; }
bool IfcGeom::Kernel::convert(const IfcSchema::IfcTrimmedCurve* l, TopoDS_Wire& wire) { IfcSchema::IfcCurve* basis_curve = l->BasisCurve(); bool isConic = basis_curve->is(IfcSchema::Type::IfcConic); double parameterFactor = isConic ? getValue(GV_PLANEANGLE_UNIT) : getValue(GV_LENGTH_UNIT); Handle(Geom_Curve) curve; if ( !convert_curve(basis_curve,curve) ) return false; bool trim_cartesian = l->MasterRepresentation() == IfcSchema::IfcTrimmingPreference::IfcTrimmingPreference_CARTESIAN; IfcEntityList::ptr trims1 = l->Trim1(); IfcEntityList::ptr trims2 = l->Trim2(); bool trimmed1 = false; bool trimmed2 = false; unsigned sense_agreement = l->SenseAgreement() ? 0 : 1; double flts[2]; gp_Pnt pnts[2]; bool has_flts[2] = {false,false}; bool has_pnts[2] = {false,false}; BRepBuilderAPI_MakeWire w; for ( IfcEntityList::it it = trims1->begin(); it != trims1->end(); it ++ ) { IfcUtil::IfcBaseClass* i = *it; if ( i->is(IfcSchema::Type::IfcCartesianPoint) ) { IfcGeom::Kernel::convert((IfcSchema::IfcCartesianPoint*)i, pnts[sense_agreement] ); has_pnts[sense_agreement] = true; } else if ( i->is(IfcSchema::Type::IfcParameterValue) ) { const double value = *((IfcSchema::IfcParameterValue*)i); flts[sense_agreement] = value * parameterFactor; has_flts[sense_agreement] = true; } } for ( IfcEntityList::it it = trims2->begin(); it != trims2->end(); it ++ ) { IfcUtil::IfcBaseClass* i = *it; if ( i->is(IfcSchema::Type::IfcCartesianPoint) ) { IfcGeom::Kernel::convert((IfcSchema::IfcCartesianPoint*)i, pnts[1-sense_agreement] ); has_pnts[1-sense_agreement] = true; } else if ( i->is(IfcSchema::Type::IfcParameterValue) ) { const double value = *((IfcSchema::IfcParameterValue*)i); flts[1-sense_agreement] = value * parameterFactor; has_flts[1-sense_agreement] = true; } } trim_cartesian &= has_pnts[0] && has_pnts[1]; bool trim_cartesian_failed = !trim_cartesian; if ( trim_cartesian ) { if ( pnts[0].Distance(pnts[1]) < getValue(GV_WIRE_CREATION_TOLERANCE) ) { Logger::Message(Logger::LOG_WARNING,"Skipping segment with length below tolerance level:",l->entity); return false; } ShapeFix_ShapeTolerance FTol; TopoDS_Vertex v1 = BRepBuilderAPI_MakeVertex(pnts[0]); TopoDS_Vertex v2 = BRepBuilderAPI_MakeVertex(pnts[1]); FTol.SetTolerance(v1, getValue(GV_WIRE_CREATION_TOLERANCE), TopAbs_VERTEX); FTol.SetTolerance(v2, getValue(GV_WIRE_CREATION_TOLERANCE), TopAbs_VERTEX); BRepBuilderAPI_MakeEdge e (curve,v1,v2); if ( ! e.IsDone() ) { BRepBuilderAPI_EdgeError err = e.Error(); if ( err == BRepBuilderAPI_PointProjectionFailed ) { Logger::Message(Logger::LOG_WARNING,"Point projection failed for:",l->entity); trim_cartesian_failed = true; } } else { w.Add(e.Edge()); } } if ( (!trim_cartesian || trim_cartesian_failed) && (has_flts[0] && has_flts[1]) ) { // The Geom_Line is constructed from a gp_Pnt and gp_Dir, whereas the IfcLine // is defined by an IfcCartesianPoint and an IfcVector with Magnitude. Because // the vector is normalised when passed to Geom_Line constructor the magnitude // needs to be factored in with the IfcParameterValue here. if ( basis_curve->is(IfcSchema::Type::IfcLine) ) { IfcSchema::IfcLine* line = static_cast<IfcSchema::IfcLine*>(basis_curve); const double magnitude = line->Dir()->Magnitude(); flts[0] *= magnitude; flts[1] *= magnitude; } if ( basis_curve->is(IfcSchema::Type::IfcEllipse) ) { IfcSchema::IfcEllipse* ellipse = static_cast<IfcSchema::IfcEllipse*>(basis_curve); double x = ellipse->SemiAxis1() * getValue(GV_LENGTH_UNIT); double y = ellipse->SemiAxis2() * getValue(GV_LENGTH_UNIT); const bool rotated = y > x; if (rotated) { flts[0] -= M_PI / 2.; flts[1] -= M_PI / 2.; } } if ( isConic && ALMOST_THE_SAME(fmod(flts[1]-flts[0],(double)(M_PI*2.0)),0.0f) ) { w.Add(BRepBuilderAPI_MakeEdge(curve)); } else { BRepBuilderAPI_MakeEdge e (curve,flts[0],flts[1]); w.Add(e.Edge()); } } else if ( trim_cartesian_failed && (has_pnts[0] && has_pnts[1]) ) { w.Add(BRepBuilderAPI_MakeEdge(pnts[0],pnts[1])); } if ( w.IsDone() ) { wire = w.Wire(); return true; } else { return false; } }
bool IfcGeom::Kernel::convert(const IfcSchema::IfcFace* l, TopoDS_Shape& face) { IfcSchema::IfcFaceBound::list::ptr bounds = l->Bounds(); Handle(Geom_Surface) face_surface; const bool is_face_surface = l->is(IfcSchema::Type::IfcFaceSurface); if (is_face_surface) { IfcSchema::IfcFaceSurface* fs = (IfcSchema::IfcFaceSurface*) l; fs->FaceSurface(); // FIXME: Surfaces are interpreted as a TopoDS_Shape TopoDS_Shape surface_shape; if (!convert_shape(fs->FaceSurface(), surface_shape)) return false; // FIXME: Assert this obtaines the only face TopExp_Explorer exp(surface_shape, TopAbs_FACE); if (!exp.More()) return false; TopoDS_Face surface = TopoDS::Face(exp.Current()); face_surface = BRep_Tool::Surface(surface); } const int num_bounds = bounds->size(); int num_outer_bounds = 0; for (IfcSchema::IfcFaceBound::list::it it = bounds->begin(); it != bounds->end(); ++it) { IfcSchema::IfcFaceBound* bound = *it; if (bound->is(IfcSchema::Type::IfcFaceOuterBound)) num_outer_bounds ++; } // The number of outer bounds should be one according to the schema. Also Open Cascade // expects this, but it is not strictly checked. Regardless, if the number is greater, // the face will still be processed as long as there are no holes. A compound of faces // is returned in that case. if (num_bounds > 1 && num_outer_bounds > 1 && num_bounds != num_outer_bounds) { Logger::Message(Logger::LOG_ERROR, "Invalid configuration of boundaries for:", l->entity); return false; } TopoDS_Compound compound; BRep_Builder builder; if (num_outer_bounds > 1) { builder.MakeCompound(compound); } // The builder is initialized on the heap because of the various different moments // of initialization depending on the configuration of surfaces and boundaries. BRepBuilderAPI_MakeFace* mf = 0; bool success = false; int processed = 0; for (int process_interior = 0; process_interior <= 1; ++process_interior) { for (IfcSchema::IfcFaceBound::list::it it = bounds->begin(); it != bounds->end(); ++it) { IfcSchema::IfcFaceBound* bound = *it; IfcSchema::IfcLoop* loop = bound->Bound(); bool same_sense = bound->Orientation(); const bool is_interior = !bound->is(IfcSchema::Type::IfcFaceOuterBound) && (num_bounds > 1) && (num_outer_bounds < num_bounds); // The exterior face boundary is processed first if (is_interior == !process_interior) continue; TopoDS_Wire wire; if (!convert_wire(loop, wire)) { Logger::Message(Logger::LOG_ERROR, "Failed to process face boundary loop", loop->entity); delete mf; return false; } /* The approach below does not result in a significant speed-up if (loop->is(IfcSchema::Type::IfcPolyLoop) && processed == 0 && face_surface.IsNull()) { IfcSchema::IfcPolyLoop* polyloop = (IfcSchema::IfcPolyLoop*) loop; IfcSchema::IfcCartesianPoint::list::ptr points = polyloop->Polygon(); if (points->size() == 3) { // Help Open Cascade by finding the plane more efficiently IfcSchema::IfcCartesianPoint::list::it point_iterator = points->begin(); gp_Pnt a, b, c; convert(*point_iterator++, a); convert(*point_iterator++, b); convert(*point_iterator++, c); const gp_XYZ ab = (b.XYZ() - a.XYZ()); const gp_XYZ ac = (c.XYZ() - a.XYZ()); const gp_Vec cross = ab.Crossed(ac); if (cross.SquareMagnitude() > ALMOST_ZERO) { const gp_Dir n = cross; face_surface = new Geom_Plane(a, n); } } } */ if (!same_sense) { wire.Reverse(); } bool flattened_wire = false; if (!mf) { process_wire: if (face_surface.IsNull()) { mf = new BRepBuilderAPI_MakeFace(wire); } else { /// @todo check necessity of false here mf = new BRepBuilderAPI_MakeFace(face_surface, wire, false); } /* BRepBuilderAPI_FaceError er = mf->Error(); if (er == BRepBuilderAPI_NotPlanar) { ShapeFix_ShapeTolerance FTol; FTol.SetTolerance(wire, getValue(GV_PRECISION), TopAbs_WIRE); delete mf; mf = new BRepBuilderAPI_MakeFace(wire); } */ if (mf->IsDone()) { TopoDS_Face outer_face_bound = mf->Face(); // In case of (non-planar) face surface, p-curves need to be computed. // For planar faces, Open Cascade generates p-curves on the fly. if (!face_surface.IsNull()) { TopExp_Explorer exp(outer_face_bound, TopAbs_EDGE); for (; exp.More(); exp.Next()) { const TopoDS_Edge& edge = TopoDS::Edge(exp.Current()); ShapeFix_Edge fix_edge; fix_edge.FixAddPCurve(edge, outer_face_bound, false, getValue(GV_PRECISION)); } } if (BRepCheck_Face(outer_face_bound).OrientationOfWires() == BRepCheck_BadOrientationOfSubshape) { wire.Reverse(); same_sense = !same_sense; delete mf; if (face_surface.IsNull()) { mf = new BRepBuilderAPI_MakeFace(wire); } else { mf = new BRepBuilderAPI_MakeFace(face_surface, wire); } ShapeFix_Face fix(mf->Face()); fix.FixOrientation(); fix.Perform(); outer_face_bound = fix.Face(); } // If the wires are reversed the face needs to be reversed as well in order // to maintain the counter-clock-wise ordering of the bounding wire's vertices. bool all_reversed = true; TopoDS_Iterator jt(outer_face_bound, false); for (; jt.More(); jt.Next()) { const TopoDS_Wire& w = TopoDS::Wire(jt.Value()); if ((w.Orientation() != TopAbs_REVERSED) == same_sense) { all_reversed = false; } } if (all_reversed) { outer_face_bound.Reverse(); } if (num_outer_bounds > 1) { builder.Add(compound, outer_face_bound); delete mf; mf = 0; } else if (num_bounds > 1) { // Reinitialize the builder to the outer face // bound in order to add holes more robustly. delete mf; // TODO: What about the face_surface? mf = new BRepBuilderAPI_MakeFace(outer_face_bound); } else { face = outer_face_bound; success = true; } } else { const bool non_planar = mf->Error() == BRepBuilderAPI_NotPlanar; delete mf; if (!non_planar || flattened_wire || !flatten_wire(wire)) { Logger::Message(Logger::LOG_ERROR, "Failed to process face boundary", bound->entity); return false; } else { Logger::Message(Logger::LOG_ERROR, "Flattening face boundary", bound->entity); flattened_wire = true; goto process_wire; } } } else { mf->Add(wire); } processed ++; } } if (!success) { success = processed == num_bounds; if (success) { if (num_outer_bounds > 1) { face = compound; } else { success = success && mf->IsDone(); if (success) { face = mf->Face(); } } } } if (success) { ShapeFix_ShapeTolerance FTol; FTol.SetTolerance(face, getValue(GV_PRECISION), TopAbs_FACE); } delete mf; return success; }
bool IfcGeom::convert(const Ifc2x3::IfcTrimmedCurve::ptr l, TopoDS_Wire& wire) { Ifc2x3::IfcCurve::ptr basis_curve = l->BasisCurve(); bool isConic = basis_curve->is(Ifc2x3::Type::IfcConic); double parameterFactor = isConic ? IfcGeom::GetValue(GV_PLANEANGLE_UNIT) : IfcGeom::GetValue(GV_LENGTH_UNIT); Handle(Geom_Curve) curve; if ( ! IfcGeom::convert_curve(basis_curve,curve) ) return false; bool trim_cartesian = l->MasterRepresentation() == Ifc2x3::IfcTrimmingPreference::IfcTrimmingPreference_CARTESIAN; IfcUtil::IfcAbstractSelect::list trims1 = l->Trim1(); IfcUtil::IfcAbstractSelect::list trims2 = l->Trim2(); bool trimmed1 = false; bool trimmed2 = false; unsigned sense_agreement = l->SenseAgreement() ? 0 : 1; double flts[2]; gp_Pnt pnts[2]; bool has_flts[2] = {false,false}; bool has_pnts[2] = {false,false}; BRepBuilderAPI_MakeWire w; for ( IfcUtil::IfcAbstractSelect::it it = trims1->begin(); it != trims1->end(); it ++ ) { const IfcUtil::IfcAbstractSelect::ptr i = *it; if ( i->is(Ifc2x3::Type::IfcCartesianPoint) ) { IfcGeom::convert(reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcCartesianPoint>(i), pnts[sense_agreement] ); has_pnts[sense_agreement] = true; } else if ( i->is(Ifc2x3::Type::IfcParameterValue) ) { const double value = *reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,IfcUtil::IfcArgumentSelect>(i)->wrappedValue(); flts[sense_agreement] = value * parameterFactor; has_flts[sense_agreement] = true; } } for ( IfcUtil::IfcAbstractSelect::it it = trims2->begin(); it != trims2->end(); it ++ ) { const IfcUtil::IfcAbstractSelect::ptr i = *it; if ( i->is(Ifc2x3::Type::IfcCartesianPoint) ) { IfcGeom::convert(reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,Ifc2x3::IfcCartesianPoint>(i), pnts[1-sense_agreement] ); has_pnts[1-sense_agreement] = true; } else if ( i->is(Ifc2x3::Type::IfcParameterValue) ) { const double value = *reinterpret_pointer_cast<IfcUtil::IfcAbstractSelect,IfcUtil::IfcArgumentSelect>(i)->wrappedValue(); flts[1-sense_agreement] = value * parameterFactor; has_flts[1-sense_agreement] = true; } } trim_cartesian &= has_pnts[0] && has_pnts[1]; bool trim_cartesian_failed = !trim_cartesian; if ( trim_cartesian ) { if ( pnts[0].Distance(pnts[1]) < GetValue(GV_WIRE_CREATION_TOLERANCE) ) { Logger::Message(Logger::LOG_WARNING,"Skipping segment with length below tolerance level:",l->entity); return false; } ShapeFix_ShapeTolerance FTol; TopoDS_Vertex v1 = BRepBuilderAPI_MakeVertex(pnts[0]); TopoDS_Vertex v2 = BRepBuilderAPI_MakeVertex(pnts[1]); FTol.SetTolerance(v1, GetValue(GV_WIRE_CREATION_TOLERANCE), TopAbs_VERTEX); FTol.SetTolerance(v2, GetValue(GV_WIRE_CREATION_TOLERANCE), TopAbs_VERTEX); BRepBuilderAPI_MakeEdge e (curve,v1,v2); if ( ! e.IsDone() ) { BRepBuilderAPI_EdgeError err = e.Error(); if ( err == BRepBuilderAPI_PointProjectionFailed ) { Logger::Message(Logger::LOG_WARNING,"Point projection failed for:",l->entity); trim_cartesian_failed = true; } } else { w.Add(e.Edge()); } } if ( (!trim_cartesian || trim_cartesian_failed) && (has_flts[0] && has_flts[1]) ) { if ( isConic && ALMOST_THE_SAME(fmod(flts[1]-flts[0],(double)(M_PI*2.0)),0.0f) ) { w.Add(BRepBuilderAPI_MakeEdge(curve)); } else { BRepBuilderAPI_MakeEdge e (curve,flts[0],flts[1]); w.Add(e.Edge()); } } else if ( trim_cartesian_failed && (has_pnts[0] && has_pnts[1]) ) { w.Add(BRepBuilderAPI_MakeEdge(pnts[0],pnts[1])); } if ( w.IsDone() ) { wire = w.Wire(); return true; } else { return false; } }
void IfcGeom::apply_tolerance(TopoDS_Shape& s, double t) { ShapeFix_ShapeTolerance tol; tol.SetTolerance(s, t); }