Salome HOME
10c8b6b711ef1635862c26a24a6e6d7238e51988
[modules/shaper.git] / src / Selector / Selector_NExplode.cpp
1 // Copyright (C) 2017-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 "Selector_NExplode.h"
21
22 #include <GeomAPI_Shape.h>
23 #include <GeomAlgoAPI_NExplode.h>
24
25 ////#include <TopoDS_Shape.hxx>
26 ////#include <BRep_Tool.hxx>
27 ////#include <TopoDS.hxx>
28 ////#include <GProp_GProps.hxx>
29 ////#include <BRepGProp.hxx>
30 ////#include <NCollection_DataMap.hxx>
31 ////#include <Precision.hxx>
32 ////#include <Bnd_Box.hxx>
33 ////#include <BRepBndLib.hxx>
34 ////#include <TopExp_Explorer.hxx>
35 ////#include <TopTools_MapOfShape.hxx>
36
37 static GeomAlgoAPI_NExplode::ShapeOrder getOrder(const bool isOld)
38 {
39   return isOld ? GeomAlgoAPI_NExplode::ORDER_BY_HASH_VALUE
40                : GeomAlgoAPI_NExplode::ORDER_BY_MIDDLE_POINT;
41 }
42
43 static GeomShapePtr convertShape(const TopoDS_Shape& theShape)
44 {
45   GeomShapePtr aNewShape(new GeomAPI_Shape);
46   aNewShape->setImpl(new TopoDS_Shape(theShape));
47   return aNewShape;
48 }
49
50 Selector_NExplode::Selector_NExplode(const TopoDS_ListOfShape& theShapes, const bool theOldOrder)
51   : myToBeReordered(theOldOrder)
52 {
53   ListOfShape aShapes;
54   for (TopoDS_ListOfShape::Iterator anIt(theShapes); anIt.More(); anIt.Next())
55     aShapes.push_back(convertShape(anIt.Value()));
56
57   mySorted = std::make_shared<GeomAlgoAPI_NExplode>(aShapes, getOrder(theOldOrder));
58 }
59
60 Selector_NExplode::Selector_NExplode(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType,
61                                      const bool theOldOrder)
62   : myToBeReordered(theOldOrder)
63 {
64   GeomShapePtr aShape = convertShape(theShape);
65   GeomAPI_Shape::ShapeType aType = (GeomAPI_Shape::ShapeType)theType;
66   mySorted = std::make_shared<GeomAlgoAPI_NExplode>(aShape, aType, getOrder(theOldOrder));
67 }
68
69
70 int Selector_NExplode::index(const TopoDS_Shape& theSubShape)
71 {
72   int anIndex = mySorted->index(convertShape(theSubShape));
73   return anIndex > 0 ? anIndex : -1; // -1 if not found
74 }
75
76 TopoDS_Shape Selector_NExplode::shape(int& theIndex)
77 {
78   TopoDS_Shape aResult;
79   GeomShapePtr aShape = mySorted->shape(theIndex);
80   if (aShape) {
81     aResult = aShape->impl<TopoDS_Shape>();
82     if (myToBeReordered) {
83       mySorted->reorder(GeomAlgoAPI_NExplode::ORDER_BY_MIDDLE_POINT);
84       theIndex = mySorted->index(aShape);
85     }
86   }
87   return aResult;
88 }