Salome HOME
075d4641ec12df3f00827a11d4fd8166a8080ba9
[modules/shaper.git] / src / GeomAPI / GeomAPI_PlanarEdges.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAPI_Interface.h>
21 #include <GeomAPI_PlanarEdges.h>
22
23 #include <Standard_TypeDef.hxx>
24 #include <TopAbs_ShapeEnum.hxx>
25
26 #include <TopoDS.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepTools_WireExplorer.hxx>
31 #include <TopExp_Explorer.hxx>
32
33 #include <list>
34
35 GeomAPI_PlanarEdges::GeomAPI_PlanarEdges() : GeomAPI_Shape()
36 {
37   TopoDS_Compound aBigWireImpl;
38   BRep_Builder aBuilder;
39   aBuilder.MakeCompound(aBigWireImpl);
40   this->setImpl(new TopoDS_Shape(aBigWireImpl));
41 }
42
43 void GeomAPI_PlanarEdges::addEdge(std::shared_ptr<GeomAPI_Shape> theEdge)
44 {
45   const TopoDS_Edge& anEdge = theEdge->impl<TopoDS_Edge>();
46   if (anEdge.ShapeType() != TopAbs_EDGE)
47     return;
48   TopoDS_Shape& aWire = const_cast<TopoDS_Shape&>(impl<TopoDS_Shape>());
49   BRep_Builder aBuilder;
50   aBuilder.Add(aWire, anEdge);
51 }
52
53 std::list<std::shared_ptr<GeomAPI_Shape> > GeomAPI_PlanarEdges::getEdges()
54 {
55   TopoDS_Shape& aShape = const_cast<TopoDS_Shape&>(impl<TopoDS_Shape>());
56   TopExp_Explorer aWireExp(aShape, TopAbs_EDGE);
57   std::list<std::shared_ptr<GeomAPI_Shape> > aResult;
58   for (; aWireExp.More(); aWireExp.Next()) {
59     std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
60     anEdge->setImpl(new TopoDS_Shape(aWireExp.Current()));
61     aResult.push_back(anEdge);
62   }
63   return aResult;
64 }
65
66 bool GeomAPI_PlanarEdges::hasPlane() const {
67   return myPlane.get() != NULL;
68 }
69
70 bool GeomAPI_PlanarEdges::isVertex() const {
71   return false;
72 }
73
74 bool GeomAPI_PlanarEdges::isEdge() const {
75   return false;
76 }
77
78 std::shared_ptr<GeomAPI_Pnt> GeomAPI_PlanarEdges::origin() const
79 {
80   if (hasPlane())
81     return myPlane->origin();
82   return std::shared_ptr<GeomAPI_Pnt>();
83 }
84
85 std::shared_ptr<GeomAPI_Dir> GeomAPI_PlanarEdges::dirX() const
86 {
87   if (hasPlane())
88     return myPlane->dirX();
89   return std::shared_ptr<GeomAPI_Dir>();
90 }
91
92 std::shared_ptr<GeomAPI_Dir> GeomAPI_PlanarEdges::dirY() const
93 {
94   if (hasPlane())
95     return myPlane->dirY();
96   return std::shared_ptr<GeomAPI_Dir>();
97 }
98
99 std::shared_ptr<GeomAPI_Dir> GeomAPI_PlanarEdges::norm() const
100 {
101   if (hasPlane())
102     return myPlane->normal();
103   return std::shared_ptr<GeomAPI_Dir>();
104 }
105
106 bool GeomAPI_PlanarEdges::isPlanar() const
107 {
108   return true;
109 }
110
111 void GeomAPI_PlanarEdges::setPlane(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
112                                    const std::shared_ptr<GeomAPI_Dir>& theDirX,
113                                    const std::shared_ptr<GeomAPI_Dir>& theNorm)
114 {
115   myPlane = std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(theOrigin, theDirX, theNorm));
116 }
117
118 bool GeomAPI_PlanarEdges::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
119 {
120   if (!theShape.get())
121     return false;
122   TopoDS_Shape& aMyShape = const_cast<TopoDS_Shape&>(impl<TopoDS_Shape>());
123   TopoDS_Shape aTheShape = theShape->impl<TopoDS_Shape>();
124   if (aMyShape.ShapeType() != aTheShape.ShapeType()) // to don't confuse by the face of same edges
125     return false;
126   TopExp_Explorer aMyExp(aMyShape, TopAbs_EDGE);
127   TopExp_Explorer aTheExp(aTheShape, TopAbs_EDGE);
128   for (; aMyExp.More() && aTheExp.More(); aMyExp.Next(), aTheExp.Next()) {
129     // check that edge by edge all geometrically matches
130     std::shared_ptr<GeomAPI_Edge> aMyEdge(new GeomAPI_Edge);
131     aMyEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aMyExp.Current()));
132     std::shared_ptr<GeomAPI_Edge> aTheEdge(new GeomAPI_Edge);
133     aTheEdge->setImpl<TopoDS_Shape>(new TopoDS_Shape(aTheExp.Current()));
134     if (!aMyEdge->isEqual(aTheEdge))
135       return false;
136   }
137   return !(aMyExp.More() || aTheExp.More());
138 }