Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAPI / GeomAPI_ShapeExplorer.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomAPI_ShapeExplorer.h>
22
23 #include <Standard_NoMoreObject.hxx>
24 #include <TopExp_Explorer.hxx>
25
26 #define MY_EXPLORER implPtr<TopExp_Explorer>()
27
28 //=================================================================================================
29 GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer()
30 : GeomAPI_Interface(new TopExp_Explorer())
31 {
32 }
33
34 // returns a type of shape to expolode, but if toFind==SHAPE, it will return the type
35 // of the first sub-element of compoud if theSHape is compound
36 static TopAbs_ShapeEnum ShapeType(const std::shared_ptr<GeomAPI_Shape>& theShape,
37                                   const GeomAPI_Shape::ShapeType toFind)
38 {
39   if (toFind == GeomAPI_Shape::SHAPE) {
40     TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
41     if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND) {
42       TopoDS_Iterator anIter(aShape);
43       if (anIter.More()) {
44         return anIter.Value().ShapeType();
45       }
46     }
47   }
48   return (TopAbs_ShapeEnum)toFind;
49 }
50
51 //=================================================================================================
52 GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer(const std::shared_ptr<GeomAPI_Shape>& theShape,
53                                              const GeomAPI_Shape::ShapeType toFind,
54                                              const GeomAPI_Shape::ShapeType toAvoid)
55 : GeomAPI_Interface(new TopExp_Explorer(theShape->impl<TopoDS_Shape>(),
56                                        ShapeType(theShape, toFind),
57                                        (TopAbs_ShapeEnum)toAvoid))
58 {
59 }
60
61 //=================================================================================================
62 void GeomAPI_ShapeExplorer::init(const std::shared_ptr<GeomAPI_Shape>& theShape,
63                                  const GeomAPI_Shape::ShapeType toFind,
64                                  const GeomAPI_Shape::ShapeType toAvoid)
65 {
66   MY_EXPLORER->Init(theShape->impl<TopoDS_Shape>(),
67                    ShapeType(theShape, toFind),
68                    (TopAbs_ShapeEnum)toAvoid);
69 }
70
71 //=================================================================================================
72 bool GeomAPI_ShapeExplorer::more() const
73 {
74   return MY_EXPLORER->More() == Standard_True;
75 }
76
77 //=================================================================================================
78 void GeomAPI_ShapeExplorer::next()
79 {
80   try {
81     MY_EXPLORER->Next();
82   } catch (Standard_NoMoreObject) {
83   }
84 }
85
86 //=================================================================================================
87 std::shared_ptr<GeomAPI_Shape> GeomAPI_ShapeExplorer::current()
88 {
89   try {
90     const TopoDS_Shape& aShape = MY_EXPLORER->Current();
91     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
92     aGeomShape->setImpl(new TopoDS_Shape(aShape));
93     return aGeomShape;
94   } catch (Standard_NoMoreObject) {
95     return std::shared_ptr<GeomAPI_Shape>();
96   }
97 }
98
99 //=================================================================================================
100 void GeomAPI_ShapeExplorer::reinit()
101 {
102   MY_EXPLORER->ReInit();
103 }
104
105 //=================================================================================================
106 int GeomAPI_ShapeExplorer::depth() const
107 {
108   return MY_EXPLORER->Depth();
109 }
110
111 //=================================================================================================
112 void GeomAPI_ShapeExplorer::clear()
113 {
114   MY_EXPLORER->Clear();
115 }