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