bool IfcGeom::Kernel::convert(const IfcSchema::IfcEdgeLoop* l, TopoDS_Wire& result) {
	IfcSchema::IfcOrientedEdge::list::ptr li = l->EdgeList();
	BRepBuilderAPI_MakeWire mw;
	for (IfcSchema::IfcOrientedEdge::list::it it = li->begin(); it != li->end(); ++it) {
		IfcSchema::IfcOrientedEdge* e = *it;

		IfcSchema::IfcPoint* pnt1 = ((IfcSchema::IfcVertexPoint*) e->EdgeStart())->VertexGeometry();
		IfcSchema::IfcPoint* pnt2 = ((IfcSchema::IfcVertexPoint*) e->EdgeEnd())->VertexGeometry();
		if (!pnt1->is(IfcSchema::Type::IfcCartesianPoint) || !pnt2->is(IfcSchema::Type::IfcCartesianPoint)) {
			Logger::Message(Logger::LOG_ERROR, "Only IfcCartesianPoints are supported for VertexGeometry", l->entity);
			return false;
		}
	
		gp_Pnt p1, p2;
		if (!IfcGeom::Kernel::convert(((IfcSchema::IfcCartesianPoint*)pnt1), p1) ||
			!IfcGeom::Kernel::convert(((IfcSchema::IfcCartesianPoint*)pnt2), p2))
		{
			return false;
		}

		mw.Add(BRepBuilderAPI_MakeEdge(p1, p2));
		continue;
		
		IfcSchema::IfcEdge* base = e->EdgeElement();
		TopoDS_Wire w;
		if (convert_wire(e->EdgeElement(), w)) {
			if (!e->Orientation()) w.Reverse();
			mw.Add(w);
		}
	}
	result = mw;
	return true;
}
예제 #2
0
bool IfcGeom::Kernel::convert(const IfcSchema::IfcOrientedEdge* l, TopoDS_Wire& result) {
	if (convert_wire(l->EdgeElement(), result)) {
		if (!l->Orientation()) {
			result.Reverse();
		}
		return true;
	} else {
		return false;
	}
}
예제 #3
0
bool IfcGeom::Kernel::convert(const IfcSchema::IfcEdgeLoop* l, TopoDS_Wire& result) {
	IfcSchema::IfcOrientedEdge::list::ptr li = l->EdgeList();
	BRepBuilderAPI_MakeWire mw;
	for (IfcSchema::IfcOrientedEdge::list::it it = li->begin(); it != li->end(); ++it) {
		TopoDS_Wire w;
		if (convert_wire(*it, w)) {
			if (!(*it)->Orientation()) w.Reverse();
			TopoDS_Iterator topoit(w, false);
			for (; topoit.More(); topoit.Next()) {
				const TopoDS_Edge& e = TopoDS::Edge(topoit.Value());
				mw.Add(e);
			}
			// mw.Add(w);
		}
	}
	result = mw;
	return true;
}
예제 #4
0
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;
}