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