1 // Copyright (C) 2014-2019 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 <TNaming_Tool.hxx>
26 #include <TNaming_SameShapeIterator.hxx>
27 #include <TNaming_Iterator.hxx>
28 #include <TDataStd_Integer.hxx>
30 Selector_WeakName::Selector_WeakName() : Selector_Algo()
34 bool Selector_WeakName::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue)
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())
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();
65 void Selector_WeakName::store()
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);
74 bool Selector_WeakName::restore()
76 Handle(TDataStd_Integer) aWeakInt;
77 if (!label().FindAttribute(weakID(), aWeakInt))
79 myWeakIndex = aWeakInt->Get();
80 Handle(TDataStd_Integer) aShapeTypeAttr;
81 if (!label().FindAttribute(shapeTypeID(), aShapeTypeAttr))
83 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
84 static TDF_LabelList anEmptyRefList;
85 return restoreBaseArray(anEmptyRefList, myContext);
88 TDF_Label Selector_WeakName::restoreByName(std::string theName,
89 const TopAbs_ShapeEnum theShapeType, Selector_NameGenerator* theNameGenerator)
91 std::string aWeakIndex = theName.substr(pureWeakNameID().size());
92 std::size_t aContextPosition = aWeakIndex.find("_");
93 myWeakIndex = atoi(aWeakIndex.c_str());
94 myShapeType = theShapeType;
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())
106 bool Selector_WeakName::solve(const TopoDS_Shape& theContext)
109 TopoDS_Shape aContext;
110 if (myContext.IsNull()) {
111 aContext = theContext;
113 Handle(TNaming_NamedShape) aNS;
114 if (myContext.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
115 aContext = aNS->Get();
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);
129 std::string Selector_WeakName::name(Selector_NameGenerator* theNameGenerator)
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);