Salome HOME
Fixes for the issue #2375 model with dump/import script
[modules/shaper.git] / src / Selector / Selector_Selector.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_Selector.h>
22
23 #include <Selector_NameGenerator.h>
24 #include <Selector_NExplode.h>
25
26 #include <TDF_ChildIDIterator.hxx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopoDS_Builder.hxx>
29 #include <TopExp_Explorer.hxx>
30 #include <TNaming_Tool.hxx>
31 #include <TNaming_NewShapeIterator.hxx>
32 #include <TNaming_OldShapeIterator.hxx>
33 #include <TNaming_SameShapeIterator.hxx>
34 #include <TNaming_Iterator.hxx>
35 #include <TNaming_Builder.hxx>
36 #include <TopTools_MapOfShape.hxx>
37
38 #include <TDataStd_Integer.hxx>
39 #include <TDataStd_ReferenceArray.hxx>
40 #include <TDataStd_IntegerArray.hxx>
41 #include <TDataStd_Name.hxx>
42
43 #include <list>
44
45 /// type of the selection, integerm keeps the Selector_Type value
46 static const Standard_GUID kSEL_TYPE("db174d59-c2e3-4a90-955e-55544df090d6");
47 /// type of the shape, stored in case it is intersection or container
48 static const Standard_GUID kSHAPE_TYPE("864b3267-cb9d-4107-bf58-c3ce1775b171");
49 //  reference attribute that contains the reference to labels where the "from" or "base" shapes
50 // of selection are located
51 static const Standard_GUID kBASE_ARRAY("7c515b1a-9549-493d-9946-a4933a22f45f");
52 // array of the neighbor levels
53 static const Standard_GUID kLEVELS_ARRAY("ee4c4b45-e859-4e86-aa4f-6eac68e0ca1f");
54 // weak index (integer) of the sub-shape
55 static const Standard_GUID kWEAK_INDEX("e9373a61-cabc-4ee8-aabf-aea47c62ed87");
56
57 Selector_Selector::Selector_Selector(TDF_Label theLab) : myLab(theLab)
58 {
59   myWeakIndex = -1;
60 }
61
62 TDF_Label Selector_Selector::label()
63 {
64   return myLab;
65 }
66
67 // adds to theResult all labels that contain initial shapes for theValue located in theFinal
68 static void findBases(Handle(TNaming_NamedShape) theFinal, const TopoDS_Shape& theValue,
69   bool aMustBeAtFinal, TDF_LabelList& theResult)
70 {
71   TNaming_SameShapeIterator aLabIter(theValue, theFinal->Label());
72   for(; aLabIter.More(); aLabIter.Next()) {
73     Handle(TNaming_NamedShape) aNS;
74     aLabIter.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS);
75     if (aMustBeAtFinal && aNS != theFinal)
76       continue; // looking for old at the same final label only
77     TNaming_Evolution anEvolution = aNS->Evolution();
78     if (anEvolution == TNaming_PRIMITIVE) {
79       // check that this is not in the results already
80       const TDF_Label aResult = aNS->Label();
81       TDF_LabelList::Iterator aResIter(theResult);
82       for(; aResIter.More(); aResIter.Next()) {
83         if (aResIter.Value().IsEqual(aResult))
84           break;
85       }
86       if (!aResIter.More()) // not found, so add this new
87         theResult.Append(aResult);
88     }
89     if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
90       for(TNaming_Iterator aThisIter(aNS); aThisIter.More(); aThisIter.Next()) {
91         if (aThisIter.NewShape().IsSame(theValue)) {
92           // continue recursively, null NS means that any NS are ok
93           findBases(theFinal, aThisIter.OldShape(), false, theResult);
94         }
95       }
96     }
97   }
98 }
99
100 // returns the sub-shapes of theSubType which belong to all theShapes (so, common or intersection)
101 static void commonShapes(const TopoDS_ListOfShape& theShapes, TopAbs_ShapeEnum theSubType,
102   TopoDS_ListOfShape& theResults)
103 {
104   TopoDS_ListOfShape::iterator aSubSel = theShapes.begin();
105   for(; aSubSel != theShapes.end(); aSubSel++) {
106     TopTools_MapOfShape aCurrentMap;
107     for(TopExp_Explorer anExp(*aSubSel, theSubType); anExp.More(); anExp.Next()) {
108       if (aCurrentMap.Add(anExp.Current()) && aSubSel == theShapes.begin())
109         theResults.Append(anExp.Current());
110     }
111     if (aSubSel != theShapes.begin()) { // remove from common shapes not in aCurrentMap
112       for(TopoDS_ListOfShape::Iterator aComIter(theResults); aComIter.More(); ) {
113         if (aCurrentMap.Contains(aComIter.Value()))
114           aComIter.Next();
115         else
116           theResults.Remove(aComIter);
117       }
118     }
119   }
120 }
121
122 /// Searches neighbor of theLevel of neighborhood to theValue in theContex
123 static void findNeighbors(const TopoDS_Shape theContext, const TopoDS_Shape theValue,
124   const int theLevel, TopTools_MapOfShape& theResult)
125 {
126   TopAbs_ShapeEnum aConnectorType = TopAbs_VERTEX; // type of the connector sub-shapes
127   if (theValue.ShapeType() == TopAbs_FACE)
128     aConnectorType = TopAbs_EDGE;
129   TopTools_MapOfShape aNBConnectors; // connector shapes that already belong to neighbors
130   for(TopExp_Explorer aValExp(theValue, aConnectorType); aValExp.More(); aValExp.Next()) {
131     aNBConnectors.Add(aValExp.Current());
132   }
133
134   TopTools_MapOfShape alreadyProcessed;
135   alreadyProcessed.Add(theValue);
136
137   for(int aLevel = 1; aLevel <= theLevel; aLevel++) {
138     TopoDS_ListOfShape aGoodCandidates;
139     TopExp_Explorer aCandidate(theContext, theValue.ShapeType());
140     for(; aCandidate.More(); aCandidate.Next()) {
141       if (alreadyProcessed.Contains(aCandidate.Current()))
142         continue;
143       TopExp_Explorer aCandConnector(aCandidate.Current(), aConnectorType);
144       for(; aCandConnector.More(); aCandConnector.Next()) {
145         if (aNBConnectors.Contains(aCandConnector.Current())) // candidate is neighbor
146           break;
147       }
148       if (aCandConnector.More()) {
149         if (aLevel == theLevel) { // add a NB into result: it is connected to other neighbors
150           theResult.Add(aCandidate.Current());
151         } else { // add to the NB of the current level
152           aGoodCandidates.Append(aCandidate.Current());
153         }
154       }
155     }
156     if (aLevel != theLevel) { // good candidates are added to neighbor of this level by connectors
157       for(TopoDS_ListOfShape::Iterator aGood(aGoodCandidates); aGood.More(); aGood.Next()) {
158         TopExp_Explorer aGoodConnector(aGood.Value(), aConnectorType);
159         for(; aGoodConnector.More(); aGoodConnector.Next()) {
160           aNBConnectors.Add(aGoodConnector.Current());
161         }
162         alreadyProcessed.Add(aGood.Value());
163       }
164     }
165   }
166 }
167
168 /// Searches the neighbor shape by neighbors defined in theNB in theContext shape
169 static const TopoDS_Shape findNeighbor(const TopoDS_Shape theContext,
170   const std::list<std::pair<TopoDS_Shape, int> >& theNB)
171 {
172   // searching for neighbors with minimum level
173   int aMinLevel = 0;
174   std::list<std::pair<TopoDS_Shape, int> >::const_iterator aNBIter = theNB.cbegin();
175   for(; aNBIter != theNB.cend(); aNBIter++) {
176     if (aMinLevel == 0 || aNBIter->second < aMinLevel) {
177       aMinLevel = aNBIter->second;
178     }
179   }
180   // collect all neighbors which are neighbors of sub-shapes with minimum level
181   bool aFirst = true;
182   TopoDS_ListOfShape aMatches;
183   for(aNBIter = theNB.cbegin(); aNBIter != theNB.cend(); aNBIter++) {
184     if (aNBIter->second == aMinLevel) {
185       TopTools_MapOfShape aThisNBs;
186       findNeighbors(theContext, aNBIter->first, aMinLevel, aThisNBs);
187       // aMatches must contain common part of all NBs lists
188       for(TopTools_MapOfShape::Iterator aThisNB(aThisNBs); aThisNB.More(); aThisNB.Next()) {
189         if (aFirst) {
190           aMatches.Append(aThisNB.Value());
191         } else {
192           // remove all in aMatches which are not in this NBs
193           for(TopoDS_ListOfShape::Iterator aMatch(aMatches); aMatch.More(); ) {
194             if (aThisNBs.Contains(aMatch.Value())) {
195               aMatch.Next();
196             } else {
197               aMatches.Remove(aMatch);
198             }
199           }
200         }
201       }
202       aFirst = false;
203     }
204   }
205   if (aMatches.IsEmpty())
206     return TopoDS_Shape(); // not found any candidate
207   if (aMatches.Extent() == 1)
208     return aMatches.First(); // already found good candidate
209   // iterate all matches to find by other (higher level) neighbors the best candidate
210   TopoDS_Shape aGoodCandidate;
211   for(TopoDS_ListOfShape::Iterator aCandidate(aMatches); aCandidate.More(); aCandidate.Next()) {
212     bool aValidCadidate = true;
213     for(int aLevel = aMinLevel + 1; true; aLevel++) {
214       bool aFooundHigherLevel = false;
215       TopoDS_ListOfShape aLevelNBs;
216       for(aNBIter = theNB.cbegin(); aNBIter != theNB.cend(); aNBIter++) {
217         if (aNBIter->second == aLevel)
218           aLevelNBs.Append(aNBIter->first);
219         else if (aNBIter->second >= aLevel)
220           aFooundHigherLevel = true;
221       }
222       if (!aFooundHigherLevel && aLevelNBs.IsEmpty()) { // iterated all, so, good candidate
223         if (aGoodCandidate.IsNull()) {
224           aGoodCandidate = aCandidate.Value();
225         } else { // too many good candidates
226           return TopoDS_Shape();
227         }
228       }
229       if (!aLevelNBs.IsEmpty()) {
230         TopTools_MapOfShape aNBsOfCandidate;
231         findNeighbors(theContext, aCandidate.Value(), aLevel, aNBsOfCandidate);
232         // check all stored neighbors are in the map of real neighbors
233         for(TopoDS_ListOfShape::Iterator aLevIter(aLevelNBs); aLevIter.More(); aLevIter.Next()) {
234           if (!aNBsOfCandidate.Contains(aLevIter.Value())) {
235             aValidCadidate = false;
236             break;
237           }
238         }
239       }
240       if (!aValidCadidate) // candidate is not valid, break the checking
241         break;
242     }
243   }
244   return aGoodCandidate;
245 }
246
247 bool Selector_Selector::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue,
248   const bool theUseNeighbors)
249 {
250   if (theValue.IsNull() || theContext.IsNull())
251     return false;
252   // check the value shape can be named as it is, or it is needed to construct it from the
253   // higher level shapes (like a box vertex by faces that form this vertex)
254   bool aIsFound = TNaming_Tool::HasLabel(myLab, theValue);
255   if (aIsFound) { // additional check for selection and delete evolution only: also could not use
256     aIsFound = false;
257     for(TNaming_SameShapeIterator aShapes(theValue, myLab); aShapes.More(); aShapes.Next())
258     {
259       Handle(TNaming_NamedShape) aNS;
260       if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
261         if (aNS->Evolution() == TNaming_MODIFY || aNS->Evolution() == TNaming_GENERATED ||
262             aNS->Evolution() == TNaming_PRIMITIVE) {
263           aIsFound = true;
264           break;
265         }
266       }
267     }
268   }
269   if (!aIsFound) {
270     TopAbs_ShapeEnum aSelectionType = theValue.ShapeType();
271     myShapeType = aSelectionType;
272     if (aSelectionType == TopAbs_COMPOUND || aSelectionType == TopAbs_COMPSOLID ||
273         aSelectionType == TopAbs_SHELL || aSelectionType == TopAbs_WIRE)
274     { // iterate all sub-shapes and select them on sublabels
275       for(TopoDS_Iterator aSubIter(theValue); aSubIter.More(); aSubIter.Next()) {
276         if (!selectBySubSelector(theContext, aSubIter.Value())) {
277           return false; // if some selector is failed, everything is failed
278         }
279       }
280       myType = SELTYPE_CONTAINER;
281       return true;
282     }
283
284     // try to find the shape of the higher level type in the context shape
285     bool aFacesTried = false; // for identification of vertices, faces are tried, then edges
286     TopoDS_ListOfShape aLastCommon; // to store the commons not good, but may be used for weak
287     TopTools_MapOfShape aLastIntersectors;
288     while(aSelectionType != TopAbs_FACE || !aFacesTried) {
289       if (aSelectionType == TopAbs_FACE) {
290         if (theValue.ShapeType() != TopAbs_VERTEX)
291           break;
292         aFacesTried = true;
293         aSelectionType = TopAbs_EDGE;
294       } else
295         aSelectionType = TopAbs_FACE;
296       TopTools_MapOfShape anIntersectors; // shapes of aSelectionType that contain theValue
297       TopoDS_ListOfShape anIntList; // same as anIntersectors
298       for(TopExp_Explorer aSelExp(theContext, aSelectionType); aSelExp.More(); aSelExp.Next()) {
299         TopExp_Explorer aSubExp(aSelExp.Current(), theValue.ShapeType());
300         for(; aSubExp.More(); aSubExp.Next()) {
301           if (aSubExp.Current().IsSame(theValue)) {
302             if (anIntersectors.Add(aSelExp.Current()))
303               anIntList.Append(aSelExp.Current());
304             break;
305           }
306         }
307       }
308       // check that solution is only one
309       TopoDS_ListOfShape aCommon;
310       commonShapes(anIntList, theValue.ShapeType(), aCommon);
311       if (aCommon.Extent() == 1 && aCommon.First().IsSame(theValue)) {
312         // name the intersectors
313         mySubSelList.clear();
314         TopTools_MapOfShape::Iterator anInt(anIntersectors);
315         for (; anInt.More(); anInt.Next()) {
316           if (!selectBySubSelector(theContext, anInt.Value())) {
317             break; // if some selector is failed, stop and search another solution
318           }
319         }
320         if (!anInt.More()) { // all intersectors were correctly named
321           myType = SELTYPE_INTERSECT;
322           return true;
323           }
324       } else if (aCommon.Extent() > 1 && aLastCommon.IsEmpty())  {
325         aLastCommon = aCommon;
326         aLastIntersectors = anIntersectors;
327       }
328     }
329
330     if (!theUseNeighbors)
331       return false;
332
333     // searching by neighbours
334     std::list<std::pair<TopoDS_Shape, int> > aNBs; /// neighbor sub-shape -> level of neighborhood
335     for(int aLevel = 1; true; aLevel++) {
336       TopTools_MapOfShape aNewNB;
337       findNeighbors(theContext, theValue, aLevel, aNewNB);
338       if (aNewNB.Extent() == 0) { // there are no neighbors of the given level, stop iteration
339         break;
340       }
341       // check which can be named correctly, without by neighbors type
342       for(TopTools_MapOfShape::Iterator aNBIter(aNewNB); aNBIter.More(); ) {
343         Selector_Selector aSelector(myLab.FindChild(1));
344         if (aSelector.select(theContext, aNBIter.Value(), false)) { // add to the list of good NBs
345           aNBs.push_back(std::pair<TopoDS_Shape, int>(aNBIter.Value(), aLevel));
346         }
347         aNewNB.Remove(aNBIter.Key());
348         aNBIter.Initialize(aNewNB);
349       }
350       TopoDS_Shape aResult = findNeighbor(theContext, aNBs);
351       if (!aResult.IsNull() && aResult.IsSame(theValue)) {
352         std::list<std::pair<TopoDS_Shape, int> >::iterator aNBIter = aNBs.begin();
353         for(; aNBIter != aNBs.end(); aNBIter++) {
354           if (!selectBySubSelector(theContext, aNBIter->first, false)) {
355             return false; // something is wrong because before this selection was ok
356           }
357           myNBLevel.push_back(aNBIter->second);
358
359         }
360         myType = SELTYPE_FILTER_BY_NEIGHBOR;
361         return true;
362       }
363     }
364
365     // weak naming to distinguish commons coming from intersection
366     if (aLastCommon.Extent() > 1) {
367       Selector_NExplode aNexp(aLastCommon);
368       myWeakIndex = aNexp.index(theValue);
369       if (myWeakIndex != -1) {
370         // name the intersectors
371         mySubSelList.clear();
372         TopTools_MapOfShape::Iterator anInt(aLastIntersectors);
373         for (; anInt.More(); anInt.Next()) {
374           if (!selectBySubSelector(theContext, anInt.Value())) {
375             break; // if some selector is failed, stop and search another solution
376           }
377         }
378         if (!anInt.More()) { // all intersectors were correctly named
379           myType = SELTYPE_INTERSECT;
380           return true;
381         }
382       }
383     }
384
385     // pure weak naming
386     myType = SELTYPE_WEAK_NAMING;
387     Selector_NExplode aNexp(theContext, theValue.ShapeType());
388     myWeakIndex = aNexp.index(theValue);
389     if (myWeakIndex != -1) {
390       myShapeType = theValue.ShapeType();
391       // searching for context shape label to store in myFinal
392       myFinal.Nullify();
393       if (TNaming_Tool::HasLabel(myLab, theContext)) {
394         for(TNaming_SameShapeIterator aShapes(theContext, myLab); aShapes.More(); aShapes.Next())
395         {
396           Handle(TNaming_NamedShape) aNS;
397           if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
398             TNaming_Evolution anEvolution = aNS->Evolution();
399             if (anEvolution == TNaming_PRIMITIVE || anEvolution == TNaming_GENERATED ||
400                 anEvolution == TNaming_MODIFY) {
401               // check this is a new shape
402               for(TNaming_Iterator aNSIter(aNS); aNSIter.More(); aNSIter.Next()) {
403                 if (aNSIter.NewShape().IsSame(theContext)) {
404                   myFinal = aNS->Label();
405                   break;
406                 }
407               }
408             }
409           }
410         }
411       }
412       return true; // could be final empty (in case it is called recursively) or not
413     }
414
415     return false;
416   }
417   // searching for the base shapes of the value
418   Handle(TNaming_NamedShape) aPrimitiveNS;
419   NCollection_List<Handle(TNaming_NamedShape)> aModifList;
420   for(TNaming_SameShapeIterator aShapes(theValue, myLab); aShapes.More(); aShapes.Next())
421   {
422     Handle(TNaming_NamedShape) aNS;
423     if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
424       TNaming_Evolution anEvolution = aNS->Evolution();
425       if (anEvolution == TNaming_PRIMITIVE) { // the value shape is declared as PRIMITIVE
426         aPrimitiveNS = aNS;
427         break;
428       } else if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
429         // check this is a new shape
430         TNaming_Iterator aNSIter(aNS);
431         for(; aNSIter.More(); aNSIter.Next())
432           if (aNSIter.NewShape().IsSame(theValue))
433             break;
434         if (aNSIter.More()) // new was found
435           aModifList.Append(aNS);
436       }
437     }
438   }
439
440   if (!aPrimitiveNS.IsNull()) {
441     myType = SELTYPE_PRIMITIVE;
442     myFinal = aPrimitiveNS->Label();
443     return true;
444   }
445
446   if (aModifList.Extent() > 1) { // searching for the best modification result: by context
447     Handle(TNaming_NamedShape) aCandidate;
448     NCollection_List<Handle(TNaming_NamedShape)>::Iterator aModIter(aModifList);
449     for(; !aModifList.IsEmpty() && aModIter.More(); aModIter.Next()) {
450        aCandidate = aModIter.Value();
451       TDF_Label aFatherLab = aCandidate->Label().Father();
452       Handle(TNaming_NamedShape) aFatherNS;
453       if (aFatherLab.FindAttribute(TNaming_NamedShape::GetID(), aFatherNS)) {
454         for(TNaming_Iterator anIter(aFatherNS); anIter.More(); anIter.Next()) {
455           if (theContext.IsSame(anIter.NewShape())) { // found the best modification
456             aModifList.Clear();
457             break;
458           }
459         }
460       }
461     }
462     // take the best candidate, or the last in the iteration
463     aModifList.Clear();
464     aModifList.Append(aCandidate);
465   }
466
467   if (!aModifList.IsEmpty()) {
468     // searching for all the base shapes of this modification
469     findBases(aModifList.First(), theValue, true, myBases);
470     if (!myBases.IsEmpty()) {
471       myFinal = aModifList.First()->Label();
472       TopoDS_ListOfShape aCommon;
473       findModificationResult(aCommon);
474       // trying to search by neighbours
475       if (aCommon.Extent() > 1) { // more complicated selection
476         if (!theUseNeighbors)
477           return false;
478
479         // searching by neighbours
480         std::list<std::pair<TopoDS_Shape, int> > aNBs;//neighbor sub-shape -> level of neighborhood
481         for(int aLevel = 1; true; aLevel++) {
482           TopTools_MapOfShape aNewNB;
483           findNeighbors(theContext, theValue, aLevel, aNewNB);
484           if (aNewNB.Extent() == 0) { // there are no neighbors of the given level, stop iteration
485             break;
486           }
487           // check which can be named correctly, without by neighbors type
488           for(TopTools_MapOfShape::Iterator aNBIter(aNewNB); aNBIter.More(); ) {
489             Selector_Selector aSelector(myLab.FindChild(1));
490             if (aSelector.select(theContext, aNBIter.Value(), false)) {// add to list of good NBs
491               aNBs.push_back(std::pair<TopoDS_Shape, int>(aNBIter.Value(), aLevel));
492             }
493             aNewNB.Remove(aNBIter.Key());
494             aNBIter.Initialize(aNewNB);
495           }
496           TopoDS_Shape aResult = findNeighbor(theContext, aNBs);
497           if (!aResult.IsNull() && aResult.IsSame(theValue)) {
498             std::list<std::pair<TopoDS_Shape, int> >::iterator aNBIter = aNBs.begin();
499             for(; aNBIter != aNBs.end(); aNBIter++) {
500               if (!selectBySubSelector(theContext, aNBIter->first)) {
501                 return false; // something is wrong because before this selection was ok
502               }
503               myNBLevel.push_back(aNBIter->second);
504
505             }
506             myType = SELTYPE_FILTER_BY_NEIGHBOR;
507             return true;
508           }
509         }
510         // filter by neighbours did not help
511         if (aCommon.Extent() > 1) { // weak naming between the common results
512           Selector_NExplode aNexp(aCommon);
513           myWeakIndex = aNexp.index(theValue);
514           if (myWeakIndex == -1)
515             return false;
516         }
517       }
518     }
519     myType = SELTYPE_MODIFICATION;
520     if (myBases.IsEmpty()) { // selection based on the external shape, weak name by finals compound
521       TopoDS_ListOfShape aCommon;
522       myFinal = aModifList.First()->Label();
523       Handle(TNaming_NamedShape) aNS;
524       myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS);
525       for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
526         const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
527         if (!aNewShape.IsNull())
528           aCommon.Append(aNewShape);
529       }
530       Selector_NExplode aNexp(aCommon);
531       myWeakIndex = aNexp.index(theValue);
532       if (myWeakIndex == -1)
533         return false;
534     }
535     return true;
536   }
537
538   // not found a good result
539   return false;
540 }
541
542 void Selector_Selector::store()
543 {
544   myLab.ForgetAllAttributes(true); // remove old naming data
545   TDataStd_Integer::Set(myLab, kSEL_TYPE, (int)myType);
546   switch(myType) {
547   case SELTYPE_CONTAINER:
548   case SELTYPE_INTERSECT: {
549     TDataStd_Integer::Set(myLab, kSHAPE_TYPE, (int)myShapeType);
550     // store also all sub-selectors
551     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
552     for(; aSubSel != mySubSelList.end(); aSubSel++) {
553       aSubSel->store();
554     }
555     if (myWeakIndex != -1) {
556       TDataStd_Integer::Set(myLab, kWEAK_INDEX, myWeakIndex);
557     }
558     break;
559   }
560   case SELTYPE_PRIMITIVE: {
561     Handle(TDataStd_ReferenceArray) anArray =
562       TDataStd_ReferenceArray::Set(myLab, kBASE_ARRAY, 0, 0);
563     anArray->SetValue(0, myFinal);
564     break;
565   }
566   case SELTYPE_MODIFICATION: {
567     Handle(TDataStd_ReferenceArray) anArray =
568       TDataStd_ReferenceArray::Set(myLab, kBASE_ARRAY, 0, myBases.Extent());
569     TDF_LabelList::Iterator aBIter(myBases);
570     for(int anIndex = 0; aBIter.More(); aBIter.Next(), anIndex++) {
571       anArray->SetValue(anIndex, aBIter.Value());
572     }
573     anArray->SetValue(myBases.Extent(), myFinal); // final is in the end of array
574     if (myWeakIndex != -1) {
575       TDataStd_Integer::Set(myLab, kWEAK_INDEX, myWeakIndex);
576     }
577     break;
578   }
579   case SELTYPE_FILTER_BY_NEIGHBOR: {
580     TDataStd_Integer::Set(myLab, kSHAPE_TYPE, (int)myShapeType);
581     // store numbers of levels corresponded to the neighbors in sub-selectors
582     Handle(TDataStd_IntegerArray) anArray =
583       TDataStd_IntegerArray::Set(myLab, kLEVELS_ARRAY, 0, int(myNBLevel.size()) - 1);
584     std::list<int>::iterator aLevel = myNBLevel.begin();
585     for(int anIndex = 0; aLevel != myNBLevel.end(); aLevel++, anIndex++) {
586       anArray->SetValue(anIndex, *aLevel);
587     }
588     // store all sub-selectors
589     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
590     for(; aSubSel != mySubSelList.end(); aSubSel++) {
591       aSubSel->store();
592     }
593     break;
594   }
595   case SELTYPE_WEAK_NAMING: {
596     TDataStd_Integer::Set(myLab, kWEAK_INDEX, myWeakIndex);
597     TDataStd_Integer::Set(myLab, kSHAPE_TYPE, (int)myShapeType);
598     // store myFinal in the base array
599     if (!myFinal.IsNull()) {
600       Handle(TDataStd_ReferenceArray) anArray =
601         TDataStd_ReferenceArray::Set(myLab, kBASE_ARRAY, 0, 0);
602       anArray->SetValue(0, myFinal);
603     }
604     break;
605   }
606   default: { // unknown case
607     break;
608   }
609   }
610 }
611
612 bool Selector_Selector::restore()
613 {
614   Handle(TDataStd_Integer) aTypeAttr;
615   if (!myLab.FindAttribute(kSEL_TYPE, aTypeAttr))
616     return false;
617   myType = Selector_Type(aTypeAttr->Get());
618   switch(myType) {
619   case SELTYPE_CONTAINER:
620   case SELTYPE_INTERSECT: {
621     Handle(TDataStd_Integer) aShapeTypeAttr;
622     if (!myLab.FindAttribute(kSHAPE_TYPE, aShapeTypeAttr))
623       return false;
624     myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
625     // restore sub-selectors
626     bool aSubResult = true;
627     mySubSelList.clear();
628     for(TDF_ChildIDIterator aSub(myLab, kSEL_TYPE, false); aSub.More(); aSub.Next()) {
629       mySubSelList.push_back(Selector_Selector(aSub.Value()->Label()));
630       if (!mySubSelList.back().restore())
631         aSubResult = false;
632     }
633     Handle(TDataStd_Integer) aWeakInt;
634     if (myLab.FindAttribute(kWEAK_INDEX, aWeakInt)) {
635       myWeakIndex = aWeakInt->Get();
636     }
637     return aSubResult;
638   }
639   case SELTYPE_PRIMITIVE: {
640     Handle(TDataStd_ReferenceArray) anArray;
641     if (myLab.FindAttribute(kBASE_ARRAY, anArray)) {
642       myFinal = anArray->Value(0);
643       return true;
644     }
645     return false;
646   }
647   case SELTYPE_MODIFICATION: {
648     Handle(TDataStd_ReferenceArray) anArray;
649     if (myLab.FindAttribute(kBASE_ARRAY, anArray)) {
650       int anUpper = anArray->Upper();
651       for(int anIndex = 0; anIndex < anUpper; anIndex++) {
652         myBases.Append(anArray->Value(anIndex));
653       }
654       myFinal = anArray->Value(anUpper);
655       Handle(TDataStd_Integer) aWeakInt;
656       if (myLab.FindAttribute(kWEAK_INDEX, aWeakInt)) {
657         myWeakIndex = aWeakInt->Get();
658       }
659       return true;
660     }
661     return false;
662   }
663   case SELTYPE_FILTER_BY_NEIGHBOR: {
664     Handle(TDataStd_Integer) aShapeTypeAttr;
665     if (!myLab.FindAttribute(kSHAPE_TYPE, aShapeTypeAttr))
666       return false;
667     myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
668     // restore sub-selectors
669     bool aSubResult = true;
670     mySubSelList.clear();
671     for(TDF_ChildIDIterator aSub(myLab, kSEL_TYPE, false); aSub.More(); aSub.Next()) {
672       mySubSelList.push_back(Selector_Selector(aSub.Value()->Label()));
673       if (!mySubSelList.back().restore())
674         aSubResult = false;
675     }
676     // restore levels indices
677     Handle(TDataStd_IntegerArray) anArray;
678     if (!myLab.FindAttribute(kLEVELS_ARRAY, anArray))
679       return false;
680     for(int anIndex = 0; anIndex <= anArray->Upper(); anIndex++) {
681       myNBLevel.push_back(anArray->Value(anIndex));
682     }
683     return true;
684   }
685   case SELTYPE_WEAK_NAMING: {
686     Handle(TDataStd_Integer) aWeakInt;
687     if (!myLab.FindAttribute(kWEAK_INDEX, aWeakInt))
688       return false;
689     myWeakIndex = aWeakInt->Get();
690     Handle(TDataStd_Integer) aShapeTypeAttr;
691     if (!myLab.FindAttribute(kSHAPE_TYPE, aShapeTypeAttr))
692       return false;
693     myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
694     Handle(TDataStd_ReferenceArray) anArray;
695     if (myLab.FindAttribute(kBASE_ARRAY, anArray)) {
696       myFinal = anArray->Value(0);
697     }
698     return true;
699   }
700   default: { // unknown case
701   }
702   }
703   return false;
704 }
705
706 /// Returns in theResults all shapes with history started in theBase and ended in theFinal
707 static void findFinals(const TopoDS_Shape& theBase, const TDF_Label& theFinal,
708   TopTools_MapOfShape& theResults)
709 {
710   for(TNaming_NewShapeIterator aBaseIter(theBase, theFinal); aBaseIter.More(); aBaseIter.Next()) {
711     TNaming_Evolution anEvolution = aBaseIter.NamedShape()->Evolution();
712     if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
713       if (aBaseIter.NamedShape()->Label().IsEqual(theFinal)) {
714         theResults.Add(aBaseIter.Shape());
715       } else {
716         findFinals(aBaseIter.Shape(), theFinal, theResults);
717       }
718     }
719   }
720 }
721
722 void Selector_Selector::findModificationResult(TopoDS_ListOfShape& theCommon) {
723   for(TDF_LabelList::Iterator aBase(myBases); aBase.More(); aBase.Next()) {
724     TopTools_MapOfShape aFinals;
725     for(TNaming_Iterator aBaseShape(aBase.Value()); aBaseShape.More(); aBaseShape.Next())
726       findFinals(aBaseShape.NewShape(), myFinal, aFinals);
727     if (!aFinals.IsEmpty()) {
728       if (theCommon.IsEmpty()) { // just copy all to common
729         for(TopTools_MapOfShape::Iterator aFinal(aFinals); aFinal.More(); aFinal.Next()) {
730           theCommon.Append(aFinal.Key());
731         }
732       } else { // keep only shapes presented in both lists
733         for(TopoDS_ListOfShape::Iterator aCommon(theCommon); aCommon.More(); ) {
734           if (aFinals.Contains(aCommon.Value())) {
735             aCommon.Next();
736           } else { // common is not found, remove it
737             theCommon.Remove(aCommon);
738           }
739         }
740       }
741     }
742   }
743 }
744
745 bool Selector_Selector::solve(const TopoDS_Shape& theContext)
746 {
747   TopoDS_Shape aResult; // null if invalid
748   switch(myType) {
749   case SELTYPE_CONTAINER: {
750     TopoDS_Builder aBuilder;
751     switch(myShapeType) {
752     case TopAbs_COMPOUND: {
753       TopoDS_Compound aComp;
754       aBuilder.MakeCompound(aComp);
755       aResult = aComp;
756       break;
757       }
758     case TopAbs_COMPSOLID: {
759       TopoDS_CompSolid aComp;
760       aBuilder.MakeCompSolid(aComp);
761       aResult = aComp;
762       break;
763     }
764     case TopAbs_SHELL: {
765       TopoDS_Shell aShell;
766       aBuilder.MakeShell(aShell);
767       aResult = aShell;
768       break;
769     }
770     case TopAbs_WIRE: {
771       TopoDS_Wire aWire;
772       aBuilder.MakeWire(aWire);
773       aResult = aWire;
774       break;
775     }
776     }
777     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
778     for(; aSubSel != mySubSelList.end(); aSubSel++) {
779       if (!aSubSel->solve(theContext)) {
780         return false;
781       }
782       aBuilder.Add(aResult, aSubSel->value());
783     }
784     break;
785   }
786   case SELTYPE_INTERSECT: {
787     TopoDS_ListOfShape aSubSelectorShapes;
788     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
789     for(; aSubSel != mySubSelList.end(); aSubSel++) {
790       if (!aSubSel->solve(theContext)) {
791         return false;
792       }
793       aSubSelectorShapes.Append(aSubSel->value());
794     }
795     TopoDS_ListOfShape aCommon; // common sub shapes in each sub-selector (a result)
796     commonShapes(aSubSelectorShapes, myShapeType, aCommon);
797     if (aCommon.Extent() != 1) {
798       if (myWeakIndex != -1) {
799         Selector_NExplode aNexp(aCommon);
800         aResult = aNexp.shape(myWeakIndex);
801       } else {
802         return false;
803       }
804     } else {
805       aResult = aCommon.First();
806     }
807     break;
808   }
809   case SELTYPE_PRIMITIVE: {
810     Handle(TNaming_NamedShape) aNS;
811     if (myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
812       aResult = aNS->Get();
813     }
814     break;
815   }
816   case SELTYPE_MODIFICATION: {
817     if (myBases.IsEmpty() && myWeakIndex) { // weak name by the final shapes index
818       TopoDS_ListOfShape aCommon;
819       Handle(TNaming_NamedShape) aNS;
820       myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS);
821       for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
822         const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
823         if (!aNewShape.IsNull())
824           aCommon.Append(aNewShape);
825       }
826       Selector_NExplode aNexp(aCommon);
827       aResult = aNexp.shape(myWeakIndex);
828     } else { // standard case
829       TopoDS_ListOfShape aFinalsCommon; // final shapes presented in all results from bases
830       findModificationResult(aFinalsCommon);
831       if (aFinalsCommon.Extent() == 1) // only in this case result is valid: found only one shape
832         aResult = aFinalsCommon.First();
833       else if (aFinalsCommon.Extent() > 1 && myWeakIndex) {
834         Selector_NExplode aNExp(aFinalsCommon);
835         aResult = aNExp.shape(myWeakIndex);
836       }
837     }
838     break;
839   }
840   case SELTYPE_FILTER_BY_NEIGHBOR: {
841     std::list<std::pair<TopoDS_Shape, int> > aNBs; /// neighbor sub-shape -> level of neighborhood
842     std::list<int>::iterator aLevel = myNBLevel.begin();
843     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
844     for(; aSubSel != mySubSelList.end(); aSubSel++, aLevel++) {
845       if (!aSubSel->solve(theContext)) {
846         return false;
847       }
848       aNBs.push_back(std::pair<TopoDS_Shape, int>(aSubSel->value(), *aLevel));
849     }
850     aResult = findNeighbor(theContext, aNBs);
851     break;
852   }
853   case SELTYPE_WEAK_NAMING: {
854     TopoDS_Shape aContext;
855     if (myFinal.IsNull()) {
856       aContext = theContext;
857     } else {
858       Handle(TNaming_NamedShape) aNS;
859       if (myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
860         aContext = aNS->Get();
861       }
862     }
863     if (!aContext.IsNull()) {
864       Selector_NExplode aNexp(aContext, myShapeType);
865       aResult = aNexp.shape(myWeakIndex);
866     }
867   }
868   default: { // unknown case
869   }
870   }
871
872   TNaming_Builder aBuilder(myLab);
873   if (!aResult.IsNull()) {
874     aBuilder.Select(aResult, aResult);
875     return true;
876   }
877   return false; // builder just erases the named shape in case of error
878 }
879
880 TopoDS_Shape Selector_Selector::value()
881 {
882   Handle(TNaming_NamedShape) aNS;
883   if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
884     return aNS->Get();
885   return TopoDS_Shape(); // empty, error shape
886 }
887
888 static const std::string kWEAK_NAME_IDENTIFIER = "weak_name_";
889 static const std::string kPUREWEAK_NAME_IDENTIFIER = "_weak_name_";
890
891 std::string Selector_Selector::name(Selector_NameGenerator* theNameGenerator) {
892   switch(myType) {
893   case SELTYPE_CONTAINER:
894   case SELTYPE_INTERSECT: {
895     std::string aResult;
896     // add names of sub-components one by one in "[]" +optionally [weak_name_1]
897     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
898     for(; aSubSel != mySubSelList.end(); aSubSel++) {
899       aResult += '[';
900       aResult += aSubSel->name(theNameGenerator);
901       aResult += ']';
902     }
903     if (myWeakIndex != -1) {
904       std::ostringstream aWeakStr;
905       aWeakStr<<"["<<kWEAK_NAME_IDENTIFIER<<myWeakIndex<<"]";
906       aResult += aWeakStr.str();
907     }
908     return aResult;
909   }
910   case SELTYPE_PRIMITIVE: {
911     Handle(TDataStd_Name) aName;
912     if (!myFinal.FindAttribute(TDataStd_Name::GetID(), aName))
913       return "";
914     return theNameGenerator->contextName(myFinal) + "/" +
915       std::string(TCollection_AsciiString(aName->Get()).ToCString());
916   }
917   case SELTYPE_MODIFICATION: {
918     // final&base1&base2 +optionally: [weak_name_1]
919     std::string aResult;
920     Handle(TDataStd_Name) aName;
921     if (!myFinal.FindAttribute(TDataStd_Name::GetID(), aName))
922       return "";
923     aResult += theNameGenerator->contextName(myFinal) + "/" +
924       std::string(TCollection_AsciiString(aName->Get()).ToCString());
925     for(TDF_LabelList::iterator aBase = myBases.begin(); aBase != myBases.end(); aBase++) {
926       if (!aBase->FindAttribute(TDataStd_Name::GetID(), aName))
927         return "";
928       aResult += "&";
929       aResult += theNameGenerator->contextName(*aBase) + "/" +
930         std::string(TCollection_AsciiString(aName->Get()).ToCString());
931     }
932     if (myWeakIndex != -1) {
933       std::ostringstream aWeakStr;
934       aWeakStr<<"&"<<kWEAK_NAME_IDENTIFIER<<myWeakIndex;
935       aResult += aWeakStr.str();
936     }
937     return aResult;
938   }
939   case SELTYPE_FILTER_BY_NEIGHBOR: {
940     // (nb1)level_if_more_than_1(nb2)level_if_more_than_1(nb3)level_if_more_than_1
941     std::string aResult;
942     std::list<int>::iterator aLevel = myNBLevel.begin();
943     std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
944     for(; aSubSel != mySubSelList.end(); aSubSel++, aLevel++) {
945       aResult += "(" + aSubSel->name(theNameGenerator) + ")";
946       if (*aLevel > 1) {
947         std::ostringstream aLevelStr;
948         aLevelStr<<*aLevel;
949         aResult += aLevelStr.str();
950       }
951     }
952     return aResult;
953   }
954   case SELTYPE_WEAK_NAMING: {
955     // _weak_naming_1_Context
956     std::ostringstream aWeakStr;
957     aWeakStr<<kPUREWEAK_NAME_IDENTIFIER<<myWeakIndex;
958     std::string aResult = aWeakStr.str();
959     if (!myFinal.IsNull())
960       aResult += "_" + theNameGenerator->contextName(myFinal);
961     return aResult;
962   }
963   default: { // unknown case
964   }
965   };
966   return "";
967 }
968
969 TDF_Label Selector_Selector::restoreByName(
970   std::string theName, const TopAbs_ShapeEnum theShapeType,
971   Selector_NameGenerator* theNameGenerator)
972 {
973   if (theName[0] == '[') { // intersection or container
974     switch(theShapeType) {
975     case TopAbs_COMPOUND:
976     case TopAbs_COMPSOLID:
977     case TopAbs_SHELL:
978     case TopAbs_WIRE:
979       myType = SELTYPE_CONTAINER;
980       break;
981     case TopAbs_VERTEX:
982     case TopAbs_EDGE:
983     case TopAbs_FACE:
984       myType = SELTYPE_INTERSECT;
985       break;
986     default:
987       return TDF_Label(); // unknown case
988     }
989     myShapeType = theShapeType;
990     TDF_Label aContext;
991     for(size_t aStart = 0; aStart != std::string::npos;
992         aStart = theName.find('[', aStart + 1)) {
993       size_t anEndPos = theName.find(']', aStart + 1);
994       if (anEndPos != std::string::npos) {
995         std::string aSubStr = theName.substr(aStart + 1, anEndPos - aStart - 1);
996         if (aSubStr.find(kWEAK_NAME_IDENTIFIER) == 0) { // weak name identifier
997           std::string aWeakIndex = aSubStr.substr(kWEAK_NAME_IDENTIFIER.size());
998           myWeakIndex = atoi(aWeakIndex.c_str());
999           continue;
1000         }
1001         mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
1002         TDF_Label aSubContext =
1003           mySubSelList.back().restoreByName(aSubStr, theShapeType, theNameGenerator);
1004         if (aSubContext.IsNull())
1005           return aSubContext; // invalid sub-selection parsing
1006         if (!aContext.IsNull() && !aContext.IsEqual(aSubContext)) {
1007           if (!theNameGenerator->isLater(aContext, aSubContext))
1008             aContext = aSubContext;
1009         } else {
1010           aContext = aSubContext;
1011         }
1012       } else
1013         return TDF_Label(); // invalid parentheses
1014     }
1015     return aContext;
1016   } else if (theName[0] == '(') { // filter by neighbours
1017     myType = SELTYPE_FILTER_BY_NEIGHBOR;
1018     TDF_Label aContext;
1019     for(size_t aStart = 0; aStart != std::string::npos;
1020       aStart = theName.find('(', aStart + 1)) {
1021       size_t anEndPos = theName.find(')', aStart + 1);
1022       if (anEndPos != std::string::npos) {
1023         std::string aSubStr = theName.substr(aStart + 1, anEndPos - aStart - 1);
1024         mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
1025         TDF_Label aSubContext =
1026           mySubSelList.back().restoreByName(aSubStr, theShapeType, theNameGenerator);
1027         if (aSubContext.IsNull())
1028           return aSubContext; // invalid sub-selection parsing
1029         if (!aContext.IsNull() && !aContext.IsEqual(aSubContext)) {
1030           if (!theNameGenerator->isLater(aContext, aSubContext))
1031             aContext = aSubContext;
1032         } else {
1033           aContext = aSubContext;
1034         }
1035         // searching for the level index
1036         std::string aLevel;
1037         for(anEndPos++; anEndPos != std::string::npos &&
1038                         theName[anEndPos] != '(' && theName[anEndPos] != 0;
1039             anEndPos++) {
1040           aLevel += theName[anEndPos];
1041         }
1042         if (aLevel.empty())
1043           myNBLevel.push_back(1); // by default it is 1
1044         else {
1045           int aNum = atoi(aLevel.c_str());
1046           if (aNum > 0)
1047             myNBLevel.push_back(aNum);
1048           else
1049             return TDF_Label(); // invalid number
1050         }
1051       } else
1052         return TDF_Label(); // invalid parentheses
1053     }
1054     return aContext;
1055   } if (theName.find(kPUREWEAK_NAME_IDENTIFIER) == 0) { // weak naming identifier
1056     myType = SELTYPE_WEAK_NAMING;
1057     std::string aWeakIndex = theName.substr(kPUREWEAK_NAME_IDENTIFIER.size());
1058     std::size_t aContextPosition = aWeakIndex.find("_");
1059     myWeakIndex = atoi(aWeakIndex.c_str());
1060     myShapeType = theShapeType;
1061     TDF_Label aContext;
1062     if (aContextPosition != std::string::npos) { // context is also defined
1063       std::string aContextName = aWeakIndex.substr(aContextPosition + 1);
1064       theNameGenerator->restoreContext(aContextName, aContext, myFinal);
1065     }
1066     return aContext;
1067   } else if (theName.find('&') == std::string::npos) { // wihtout '&' it can be only primitive
1068     myType = SELTYPE_PRIMITIVE;
1069     TDF_Label aContext;
1070     if (theNameGenerator->restoreContext(theName, aContext, myFinal)) {
1071       if (!myFinal.IsNull())
1072         return aContext;
1073     }
1074   } else { // modification
1075     myType = SELTYPE_MODIFICATION;
1076     TDF_Label aContext;
1077     for(size_t anEnd, aStart = 0; aStart != std::string::npos; aStart = anEnd) {
1078       if (aStart != 0)
1079         aStart++;
1080       anEnd = theName.find('&', aStart);
1081       std::string aSubStr =
1082         theName.substr(aStart, anEnd == std::string::npos ? anEnd : anEnd - aStart);
1083       if (aSubStr.find(kWEAK_NAME_IDENTIFIER) == 0) { // weak name identifier
1084         std::string aWeakIndex = aSubStr.substr(kWEAK_NAME_IDENTIFIER.size());
1085         myWeakIndex = atoi(aWeakIndex.c_str());
1086         continue;
1087       }
1088       TDF_Label aSubContext, aValue;
1089       if (!theNameGenerator->restoreContext(aSubStr, aSubContext, aValue))
1090         return TDF_Label(); // can not restore
1091       if(aSubContext.IsNull() || aValue.IsNull())
1092         return TDF_Label(); // can not restore
1093       if (myFinal.IsNull()) {
1094         myFinal = aValue;
1095         aContext = aSubContext;
1096       } else
1097         myBases.Append(aValue);
1098     }
1099     return aContext;
1100   }
1101   return TDF_Label();
1102 }
1103
1104 bool Selector_Selector::selectBySubSelector(
1105   const TopoDS_Shape theContext, const TopoDS_Shape theValue, const bool theUseNeighbors)
1106 {
1107   mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
1108   if (!mySubSelList.back().select(theContext, theValue, theUseNeighbors)) {
1109     mySubSelList.clear(); // if one of the selector is failed, all become invalid
1110     return false;
1111   }
1112   return true;
1113 }