Salome HOME
Update copyrights
[modules/shaper.git] / src / Selector / Selector_WeakName.cpp
1 // Copyright (C) 2014-2019  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()
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   std::string aWeakIndex = theName.substr(pureWeakNameID().size());
92   std::size_t aContextPosition = aWeakIndex.find("_");
93   myWeakIndex = atoi(aWeakIndex.c_str());
94   myShapeType = theShapeType;
95   TDF_Label aContext;
96   if (aContextPosition != std::string::npos) { // context is also defined
97     std::string aContextName = aWeakIndex.substr(aContextPosition + 1);
98     if (theNameGenerator->restoreContext(aContextName, aContext, myContext)) {
99       if (myContext.IsNull())
100         aContext.Nullify();
101     }
102   }
103   return aContext;
104 }
105
106 bool Selector_WeakName::solve(const TopoDS_Shape& theContext)
107 {
108
109   TopoDS_Shape aContext;
110   if (myContext.IsNull()) {
111     aContext = theContext;
112   } else {
113     Handle(TNaming_NamedShape) aNS;
114     if (myContext.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
115       aContext = aNS->Get();
116     }
117   }
118   if (!aContext.IsNull()) {
119     Selector_NExplode aNexp(aContext, myShapeType);
120     TopoDS_Shape aResult = aNexp.shape(myWeakIndex);
121     if (!aResult.IsNull()) {
122       Selector_Algo::store(aResult);
123       return true;
124     }
125   }
126   return false;
127 }
128
129 std::string Selector_WeakName::name(Selector_NameGenerator* theNameGenerator)
130 {
131   // _weak_naming_1_Context
132   std::ostringstream aWeakStr;
133   aWeakStr<<pureWeakNameID()<<myWeakIndex;
134   std::string aResult = aWeakStr.str();
135   if (!myContext.IsNull())
136     aResult += "_" + theNameGenerator->contextName(myContext);
137   return aResult;
138 }