Salome HOME
5323c1103ac31a0e5ca40e4679ed2e387228144f
[modules/shaper.git] / src / Selector / Selector_Selector.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 <Selector_Selector.h>
21
22 #include <Selector_NameGenerator.h>
23 #include <Selector_Algo.h>
24
25 #include <TopTools_MapOfShape.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Builder.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TNaming_NamedShape.hxx>
30
31 Selector_Selector::Selector_Selector(TDF_Label theLab, TDF_Label theBaseDocLab) :
32   myLab(theLab), myBaseDocumentLab(theBaseDocLab), myAlgo(NULL)
33 {}
34
35 Selector_Selector::~Selector_Selector()
36 {
37   if (myAlgo)
38     delete myAlgo;
39 }
40
41 bool Selector_Selector::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue,
42   const bool theGeometricalNaming)
43 {
44   if (theValue.IsNull() || theContext.IsNull())
45     return false;
46
47   myAlgo = Selector_Algo::select(theContext, theValue, myLab, myBaseDocumentLab,
48     theGeometricalNaming, true, true);
49
50   return myAlgo != NULL;
51 }
52
53 bool Selector_Selector::store(const TopoDS_Shape theContext)
54 {
55   myAlgo->store();
56   return myAlgo->solve(theContext); // to update the selection shape on the label
57 }
58
59 bool Selector_Selector::restore(const TopoDS_Shape theContext)
60 {
61   myAlgo = Selector_Algo::restoreByLab(myLab, myBaseDocumentLab);
62   if (myAlgo) {
63     return myAlgo->solve(theContext); // to update the selection shape on the label
64   }
65   return false;
66 }
67
68 TopoDS_Shape Selector_Selector::value()
69 {
70   Handle(TNaming_NamedShape) aNS;
71   if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
72     return aNS->Get();
73   return TopoDS_Shape(); // empty, error shape
74 }
75
76 std::wstring Selector_Selector::name(Selector_NameGenerator* theNameGenerator) {
77   return myAlgo->name(theNameGenerator);
78 }
79
80 TDF_Label Selector_Selector::restoreByName(
81   std::wstring theName, const TopAbs_ShapeEnum theShapeType,
82   Selector_NameGenerator* theNameGenerator, const bool theGeometricalNaming)
83 {
84   TDF_Label aResult;
85   myAlgo = Selector_Algo::restoreByName(myLab, myBaseDocumentLab, theName, theShapeType,
86     theGeometricalNaming, theNameGenerator, aResult);
87   if (myAlgo) {
88     return aResult;
89   }
90   return TDF_Label();
91 }
92
93 void Selector_Selector::combineGeometrical(const TopoDS_Shape theContext)
94 {
95   TopoDS_Shape aValue = value();
96   if (aValue.IsNull() || aValue.ShapeType() == TopAbs_COMPOUND)
97     return;
98
99   Selector_Algo* aNewAlgo = Selector_Algo::relesectWithAllGeometry(myAlgo, theContext);
100   if (aNewAlgo) {
101     aNewAlgo->store();
102     aNewAlgo->solve(theContext);
103     delete myAlgo;
104     myAlgo = aNewAlgo;
105   }
106 }
107
108 bool Selector_Selector::solve(const TopoDS_Shape theContext)
109 {
110   return myAlgo->solve(theContext);
111 }