1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_SketchBuilder.cpp
4 // Created: 02 Jun 2014
5 // Author: Artem ZHIDKOV
7 #include <GeomAlgoAPI_SketchBuilder.h>
8 #include <GeomAPI_PlanarEdges.h>
10 #include <BOPAlgo_Builder.hxx>
11 #include <BRep_Builder.hxx>
12 #include <BRepTopAdaptor_FClass2d.hxx>
13 #include <Geom_Plane.hxx>
14 #include <Precision.hxx>
16 #include <TopExp_Explorer.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
24 static TopoDS_Vertex findStartVertex(const TopoDS_Face& aFace)
26 TopExp_Explorer anExp(aFace, TopAbs_VERTEX);
27 TopoDS_Vertex aStart = TopoDS::Vertex(anExp.Current());
28 gp_Pnt aStartPnt(BRep_Tool::Pnt(aStart));
29 TopoDS_Vertex aCurrent;
32 for (anExp.Next(); anExp.More(); anExp.Next()) {
33 aCurrent = TopoDS::Vertex(anExp.Current());
34 aCurrentPnt = BRep_Tool::Pnt(aCurrent);
35 if ((aCurrentPnt.X() > aStartPnt.X()) ||
36 (aCurrentPnt.X() == aStartPnt.X() && aCurrentPnt.Y() > aStartPnt.Y()) ||
37 (aCurrentPnt.X() == aStartPnt.X() && aCurrentPnt.Y() == aStartPnt.Y() &&
38 aCurrentPnt.Z() > aStartPnt.Z())) {
40 aStartPnt = aCurrentPnt;
46 void GeomAlgoAPI_SketchBuilder::createFaces(
47 const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
48 const std::shared_ptr<GeomAPI_Dir>& theDirX,
49 const std::shared_ptr<GeomAPI_Dir>& theNorm,
50 const std::list<std::shared_ptr<GeomAPI_Shape> >& theFeatures,
51 std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces)
53 if (theFeatures.empty())
56 BRep_Builder aBuilder;
57 // Planar face, where the sketch was built
58 Handle(Geom_Surface) aPlane(new Geom_Plane(theOrigin->impl<gp_Pnt>(), theNorm->impl<gp_Dir>()));
60 aBuilder.MakeFace(aPlnFace, aPlane, Precision::Confusion());
62 // Use General Fuse algorithm to prepare all subfaces, bounded by given list of edges
64 aBB.AddArgument(aPlnFace);
66 BOPCol_ListOfShape anEdges;
67 BOPCol_ListIteratorOfListOfShape aShapeIt;
68 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theFeatures.begin();
69 for (; aFeatIt != theFeatures.end(); aFeatIt++) {
70 std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
71 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
72 if (anEdge.ShapeType() == TopAbs_EDGE)
73 aBB.AddArgument(anEdge);
76 if (aBB.ErrorStatus())
80 const TopTools_ListOfShape& anAreas = aBB.Modified(aPlnFace);
81 TopTools_ListIteratorOfListOfShape anIt(anAreas);
82 for (; anIt.More(); anIt.Next()) {
83 TopoDS_Face aFace = TopoDS::Face(anIt.Value());
84 // avoid infinite faces
85 BRepTopAdaptor_FClass2d aFClass(aFace, Precision::Confusion());
86 if (aFClass.PerformInfinitePoint() == TopAbs_IN)
89 // to make faces equal on different platforms, we will find
90 // a vertex with greater coordinates and start wire from it
91 TopoDS_Vertex aStartVertex = findStartVertex(aFace);
93 aBuilder.MakeWire(aNewWire);
94 std::list<TopoDS_Edge> aSkippedEdges;
95 bool aStartFound = false;
97 // remove internal edges from faces and make wire start from found vertex
98 TopExp_Explorer anExp(aFace, TopAbs_EDGE);
99 for (; anExp.More(); anExp.Next()) {
100 if (anExp.Current().Orientation() == TopAbs_INTERNAL)
103 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
104 TopoDS_Vertex aV1, aV2;
105 TopExp::Vertices(anEdge, aV1, aV2);
106 if (aV1.IsSame(aStartVertex) == Standard_True)
109 aSkippedEdges.push_back(anEdge);
112 aBuilder.Add(aNewWire, anExp.Current());
114 // add skipped edges to the end of wire
115 std::list<TopoDS_Edge>::const_iterator aSkIt = aSkippedEdges.begin();
116 for (; aSkIt != aSkippedEdges.end(); ++aSkIt)
117 aBuilder.Add(aNewWire, *aSkIt);
120 TopoDS_Face aNewFace;
121 aBuilder.MakeFace(aNewFace, aPlane, Precision::Confusion());
122 aBuilder.Add(aNewFace, aNewWire);
126 std::shared_ptr<GeomAPI_Shape> aResFace(new GeomAPI_Shape);
127 aResFace->setImpl(new TopoDS_Face(aFace));
128 theResultFaces.push_back(aResFace);
132 void GeomAlgoAPI_SketchBuilder::createFaces(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
133 const std::shared_ptr<GeomAPI_Dir>& theDirX,
134 const std::shared_ptr<GeomAPI_Dir>& theNorm,
135 const std::shared_ptr<GeomAPI_Shape>& theWire,
136 std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces)
138 std::shared_ptr<GeomAPI_PlanarEdges> aWire =
139 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theWire);
141 // Filter wires, return only faces.
142 createFaces(theOrigin, theDirX, theNorm, aWire->getEdges(), theResultFaces);
143 } else { // it may be only one circle
144 std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(theWire);
146 std::list<std::shared_ptr<GeomAPI_Shape> > aList;
147 aList.push_back(anEdge);
148 createFaces(theOrigin, theDirX, theNorm, aList, theResultFaces);