Salome HOME
Fix for the issue #1202 : update the shape of the referenced plane
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_SketchBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_SketchBuilder.cpp
4 // Created:     02 Jun 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAlgoAPI_SketchBuilder.h>
8 #include <GeomAPI_PlanarEdges.h>
9
10 #include <BOPAlgo_Builder.hxx>
11 #include <BRep_Builder.hxx>
12 #include <BRepTopAdaptor_FClass2d.hxx>
13 #include <Geom_Plane.hxx>
14 #include <Precision.hxx>
15 #include <TopExp.hxx>
16 #include <TopExp_Explorer.hxx>
17 #include <TopoDS.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
20
21 #include <list>
22
23
24 static TopoDS_Vertex findStartVertex(const TopoDS_Face& aFace)
25 {
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;
30   gp_Pnt aCurrentPnt;
31
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())) {
39       aStart = aCurrent;
40       aStartPnt = aCurrentPnt;
41     }
42   }
43   return aStart;
44 }
45
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)
52 {
53   if (theFeatures.empty())
54     return;
55
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>()));
59   TopoDS_Face aPlnFace;
60   aBuilder.MakeFace(aPlnFace, aPlane, Precision::Confusion());
61
62   // Use General Fuse algorithm to prepare all subfaces, bounded by given list of edges
63   BOPAlgo_Builder aBB;
64   aBB.AddArgument(aPlnFace);
65
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);
74   }
75   aBB.Perform();
76   if (aBB.ErrorStatus())
77     return;
78
79   // Collect faces
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)
87       continue;
88
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);
92     TopoDS_Wire aNewWire;
93     aBuilder.MakeWire(aNewWire);
94     std::list<TopoDS_Edge> aSkippedEdges;
95     bool aStartFound = false;
96
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)
101         continue;
102       if (!aStartFound) {
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)
107           aStartFound = true;
108         else
109           aSkippedEdges.push_back(anEdge);
110       }
111       if (aStartFound)
112         aBuilder.Add(aNewWire, anExp.Current());
113     }
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);
118
119     // rebuild face
120     TopoDS_Face aNewFace;
121     aBuilder.MakeFace(aNewFace, aPlane, Precision::Confusion());
122     aBuilder.Add(aNewFace, aNewWire);
123     aFace = aNewFace;
124
125     // store face
126     std::shared_ptr<GeomAPI_Shape> aResFace(new GeomAPI_Shape);
127     aResFace->setImpl(new TopoDS_Face(aFace));
128     theResultFaces.push_back(aResFace);
129   }
130 }
131
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)
137 {
138   std::shared_ptr<GeomAPI_PlanarEdges> aWire = 
139     std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theWire);
140   if(aWire) {
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);
145     if (anEdge) {
146       std::list<std::shared_ptr<GeomAPI_Shape> > aList;
147       aList.push_back(anEdge);
148       createFaces(theOrigin, theDirX, theNorm, aList, theResultFaces);
149     }
150   }
151 }