1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Selector_WeakName.h>
22 #include <Selector_NameGenerator.h>
23 #include <Selector_NExplode.h>
25 #include <Locale_Convert.h>
27 #include <TNaming_Tool.hxx>
28 #include <TNaming_SameShapeIterator.hxx>
29 #include <TNaming_Iterator.hxx>
30 #include <TDataStd_Integer.hxx>
32 Selector_WeakName::Selector_WeakName() : Selector_Algo(), myRecomputeWeakIndex(false)
36 bool Selector_WeakName::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue)
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())
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();
67 void Selector_WeakName::store()
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);
76 bool Selector_WeakName::restore()
78 Handle(TDataStd_Integer) aWeakInt;
79 if (!label().FindAttribute(weakID(), aWeakInt))
81 myWeakIndex = aWeakInt->Get();
82 Handle(TDataStd_Integer) aShapeTypeAttr;
83 if (!label().FindAttribute(shapeTypeID(), aShapeTypeAttr))
85 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
86 static TDF_LabelList anEmptyRefList;
87 return restoreBaseArray(anEmptyRefList, myContext);
90 TDF_Label Selector_WeakName::restoreByName(std::wstring theName,
91 const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
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;
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())
110 bool Selector_WeakName::solve(const TopoDS_Shape& theContext)
113 TopoDS_Shape aContext;
114 if (myContext.IsNull()) {
115 aContext = theContext;
117 Handle(TNaming_NamedShape) aNS;
118 if (myContext.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
119 aContext = aNS->Get();
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);
134 std::wstring Selector_WeakName::name(Selector_NameGenerator* theNameGenerator)
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);