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