Salome HOME
Fix mistake after branches merging.
[modules/shaper.git] / src / Selector / Selector_WeakName.cpp
1 // Copyright (C) 2014-2020  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_WeakName.h>
21
22 #include <Selector_NameGenerator.h>
23 #include <Selector_NExplode.h>
24
25 #include <TNaming_Tool.hxx>
26 #include <TNaming_SameShapeIterator.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TDataStd_Integer.hxx>
29
30 Selector_WeakName::Selector_WeakName() : Selector_Algo(), myRecomputeWeakIndex(false)
31 {
32 }
33
34 bool Selector_WeakName::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue)
35 {
36   myShapeType = theValue.ShapeType();
37   Selector_NExplode aNexp(theContext, myShapeType);
38   myWeakIndex = aNexp.index(theValue);
39   if (myWeakIndex != -1) {
40     // searching for context shape label to store in myFinal
41     if (TNaming_Tool::HasLabel(label(), theContext)) {
42       for(TNaming_SameShapeIterator aShapes(theContext, label()); aShapes.More(); aShapes.Next())
43       {
44         Handle(TNaming_NamedShape) aNS;
45         if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
46           TNaming_Evolution anEvolution = aNS->Evolution();
47           if (anEvolution == TNaming_PRIMITIVE || anEvolution == TNaming_GENERATED ||
48             anEvolution == TNaming_MODIFY) {
49             // check this is a new shape
50             for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
51               if (aNSIter.NewShape().IsSame(theContext)) {
52                 myContext = aNS->Label();
53                 break;
54               }
55             }
56           }
57         }
58       }
59     }
60     return true;
61   }
62   return false;
63 }
64
65 void Selector_WeakName::store()
66 {
67   static const TDF_LabelList anEmptyRefList;
68   storeType(Selector_Algo::SELTYPE_WEAK_NAMING);
69   storeBaseArray(anEmptyRefList, myContext);
70   TDataStd_Integer::Set(label(), weakID(), myWeakIndex);
71   TDataStd_Integer::Set(label(), shapeTypeID(), (int)myShapeType);
72 }
73
74 bool Selector_WeakName::restore()
75 {
76   Handle(TDataStd_Integer) aWeakInt;
77   if (!label().FindAttribute(weakID(), aWeakInt))
78     return false;
79   myWeakIndex = aWeakInt->Get();
80   Handle(TDataStd_Integer) aShapeTypeAttr;
81   if (!label().FindAttribute(shapeTypeID(), aShapeTypeAttr))
82     return false;
83   myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
84   static TDF_LabelList anEmptyRefList;
85   return restoreBaseArray(anEmptyRefList, myContext);
86 }
87
88 TDF_Label Selector_WeakName::restoreByName(std::string theName,
89   const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
90 {
91   size_t aFoundWeak = theName.find(oldPureWeakNameID());
92   std::string aWeakIndex = theName.substr(aFoundWeak + oldPureWeakNameID().size());
93   std::size_t aContextPosition = aWeakIndex.find("_");
94   myWeakIndex = atoi(aWeakIndex.c_str());
95   myRecomputeWeakIndex = aFoundWeak == 0;
96   myShapeType = theShapeType;
97   TDF_Label aContext;
98   if (aContextPosition != std::string::npos) { // context is also defined
99     std::string aContextName = aWeakIndex.substr(aContextPosition + 1);
100     if (theNameGenerator->restoreContext(aContextName, aContext, myContext)) {
101       if (myContext.IsNull())
102         aContext.Nullify();
103     }
104   }
105   return aContext;
106 }
107
108 bool Selector_WeakName::solve(const TopoDS_Shape& theContext)
109 {
110
111   TopoDS_Shape aContext;
112   if (myContext.IsNull()) {
113     aContext = theContext;
114   } else {
115     Handle(TNaming_NamedShape) aNS;
116     if (myContext.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
117       aContext = aNS->Get();
118     }
119   }
120   if (!aContext.IsNull()) {
121     Selector_NExplode aNexp(aContext, myShapeType, myRecomputeWeakIndex);
122     TopoDS_Shape aResult = aNexp.shape(myWeakIndex);
123     myRecomputeWeakIndex = false;
124     if (!aResult.IsNull()) {
125       Selector_Algo::store(aResult);
126       return true;
127     }
128   }
129   return false;
130 }
131
132 std::string Selector_WeakName::name(Selector_NameGenerator* theNameGenerator)
133 {
134   // _weak_naming_1_Context
135   std::ostringstream aWeakStr;
136   aWeakStr<<pureWeakNameID()<<myWeakIndex;
137   std::string aResult = aWeakStr.str();
138   if (!myContext.IsNull())
139     aResult += "_" + theNameGenerator->contextName(myContext);
140   return aResult;
141 }