1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Selector_WeakName.h>
23 #include <Selector_NameGenerator.h>
24 #include <Selector_NExplode.h>
26 #include <TNaming_Tool.hxx>
27 #include <TNaming_SameShapeIterator.hxx>
28 #include <TNaming_Iterator.hxx>
29 #include <TDataStd_Integer.hxx>
31 Selector_WeakName::Selector_WeakName() : Selector_Algo()
35 bool Selector_WeakName::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue)
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())
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();
66 void Selector_WeakName::store()
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);
75 bool Selector_WeakName::restore()
77 Handle(TDataStd_Integer) aWeakInt;
78 if (!label().FindAttribute(weakID(), aWeakInt))
80 myWeakIndex = aWeakInt->Get();
81 Handle(TDataStd_Integer) aShapeTypeAttr;
82 if (!label().FindAttribute(shapeTypeID(), aShapeTypeAttr))
84 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
85 static TDF_LabelList anEmptyRefList;
86 return restoreBaseArray(anEmptyRefList, myContext);
89 TDF_Label Selector_WeakName::restoreByName(std::string theName,
90 const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
92 std::string aWeakIndex = theName.substr(pureWeakNameID().size());
93 std::size_t aContextPosition = aWeakIndex.find("_");
94 myWeakIndex = atoi(aWeakIndex.c_str());
95 myShapeType = theShapeType;
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())
107 bool Selector_WeakName::solve(const TopoDS_Shape& theContext)
110 TopoDS_Shape aContext;
111 if (myContext.IsNull()) {
112 aContext = theContext;
114 Handle(TNaming_NamedShape) aNS;
115 if (myContext.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
116 aContext = aNS->Get();
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);
130 std::string Selector_WeakName::name(Selector_NameGenerator* theNameGenerator)
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);