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_Selector.h>
23 #include <Selector_NameGenerator.h>
24 #include <Selector_NExplode.h>
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>
38 #include <TDataStd_Integer.hxx>
39 #include <TDataStd_ReferenceArray.hxx>
40 #include <TDataStd_IntegerArray.hxx>
41 #include <TDataStd_Name.hxx>
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");
57 Selector_Selector::Selector_Selector(TDF_Label theLab) : myLab(theLab)
62 TDF_Label Selector_Selector::label()
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)
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))
86 if (!aResIter.More()) // not found, so add this new
87 theResult.Append(aResult);
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);
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)
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());
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()))
116 theResults.Remove(aComIter);
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)
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());
134 TopTools_MapOfShape alreadyProcessed;
135 alreadyProcessed.Add(theValue);
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()))
143 TopExp_Explorer aCandConnector(aCandidate.Current(), aConnectorType);
144 for(; aCandConnector.More(); aCandConnector.Next()) {
145 if (aNBConnectors.Contains(aCandConnector.Current())) // candidate is neighbor
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());
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());
162 alreadyProcessed.Add(aGood.Value());
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)
172 // searching for neighbors with minimum level
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;
180 // collect all neighbors which are neighbors of sub-shapes with minimum level
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()) {
190 aMatches.Append(aThisNB.Value());
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())) {
197 aMatches.Remove(aMatch);
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;
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();
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;
240 if (!aValidCadidate) // candidate is not valid, break the checking
244 return aGoodCandidate;
247 bool Selector_Selector::select(const TopoDS_Shape theContext, const TopoDS_Shape theValue,
248 const bool theUseNeighbors)
250 if (theValue.IsNull() || theContext.IsNull())
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 be used
257 for(TNaming_SameShapeIterator aShapes(theValue, myLab); aShapes.More(); aShapes.Next())
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) {
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
280 myType = SELTYPE_CONTAINER;
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 while(aSelectionType != TopAbs_FACE || !aFacesTried) {
287 if (aSelectionType == TopAbs_FACE) {
288 if (theValue.ShapeType() != TopAbs_VERTEX)
291 aSelectionType = TopAbs_EDGE;
293 aSelectionType = TopAbs_FACE;
294 TopTools_MapOfShape anIntersectors; // shapes of aSelectionType that contain theValue
295 TopoDS_ListOfShape anIntList; // same as anIntersectors
296 for(TopExp_Explorer aSelExp(theContext, aSelectionType); aSelExp.More(); aSelExp.Next()) {
297 TopExp_Explorer aSubExp(aSelExp.Current(), theValue.ShapeType());
298 for(; aSubExp.More(); aSubExp.Next()) {
299 if (aSubExp.Current().IsSame(theValue)) {
300 if (anIntersectors.Add(aSelExp.Current()))
301 anIntList.Append(aSelExp.Current());
306 // check that solution is only one
307 TopoDS_ListOfShape aCommon;
308 commonShapes(anIntList, theValue.ShapeType(), aCommon);
309 if (aCommon.Extent() == 1 && aCommon.First().IsSame(theValue)) {
310 // name the intersectors
311 std::list<Selector_Selector> aSubSelList;
312 TopTools_MapOfShape::Iterator anInt(anIntersectors);
313 for (; anInt.More(); anInt.Next()) {
314 if (!selectBySubSelector(theContext, anInt.Value())) {
315 break; // if some selector is failed, stop and search another solution
318 if (!anInt.More()) { // all intersectors were correctly named
319 myType = SELTYPE_INTERSECT;
325 if (!theUseNeighbors)
328 // searching by neighbours
329 std::list<std::pair<TopoDS_Shape, int> > aNBs; /// neighbor sub-shape -> level of neighborhood
330 for(int aLevel = 1; true; aLevel++) {
331 TopTools_MapOfShape aNewNB;
332 findNeighbors(theContext, theValue, aLevel, aNewNB);
333 if (aNewNB.Extent() == 0) { // there are no neighbors of the given level, stop iteration
336 // check which can be named correctly, without by neighbors type
337 for(TopTools_MapOfShape::Iterator aNBIter(aNewNB); aNBIter.More(); ) {
338 Selector_Selector aSelector(myLab.FindChild(1));
339 if (aSelector.select(theContext, aNBIter.Value(), false)) { // add to the list of good NBs
340 aNBs.push_back(std::pair<TopoDS_Shape, int>(aNBIter.Value(), aLevel));
342 aNewNB.Remove(aNBIter.Key());
343 aNBIter.Initialize(aNewNB);
345 TopoDS_Shape aResult = findNeighbor(theContext, aNBs);
346 if (!aResult.IsNull() && aResult.IsSame(theValue)) {
347 std::list<std::pair<TopoDS_Shape, int> >::iterator aNBIter = aNBs.begin();
348 for(; aNBIter != aNBs.end(); aNBIter++) {
349 if (!selectBySubSelector(theContext, aNBIter->first, false)) {
350 return false; // something is wrong because before this selection was ok
352 myNBLevel.push_back(aNBIter->second);
355 myType = SELTYPE_FILTER_BY_NEIGHBOR;
361 // searching for the base shapes of the value
362 Handle(TNaming_NamedShape) aPrimitiveNS;
363 NCollection_List<Handle(TNaming_NamedShape)> aModifList;
364 for(TNaming_SameShapeIterator aShapes(theValue, myLab); aShapes.More(); aShapes.Next())
366 Handle(TNaming_NamedShape) aNS;
367 if (aShapes.Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
368 TNaming_Evolution anEvolution = aNS->Evolution();
369 if (anEvolution == TNaming_PRIMITIVE) { // the value shape is declared as PRIMITIVE
372 } else if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
373 // check this is a new shape
374 TNaming_Iterator aNSIter(aNS);
375 for(; aNSIter.More(); aNSIter.Next())
376 if (aNSIter.NewShape().IsSame(theValue))
378 if (aNSIter.More()) // new was found
379 aModifList.Append(aNS);
384 if (!aPrimitiveNS.IsNull()) {
385 myType = SELTYPE_PRIMITIVE;
386 myFinal = aPrimitiveNS->Label();
390 if (aModifList.Extent() > 1) { // searching for the best modification result: by context
391 Handle(TNaming_NamedShape) aCandidate;
392 NCollection_List<Handle(TNaming_NamedShape)>::Iterator aModIter(aModifList);
393 for(; !aModifList.IsEmpty() && aModIter.More(); aModIter.Next()) {
394 aCandidate = aModIter.Value();
395 TDF_Label aFatherLab = aCandidate->Label().Father();
396 Handle(TNaming_NamedShape) aFatherNS;
397 if (aFatherLab.FindAttribute(TNaming_NamedShape::GetID(), aFatherNS)) {
398 for(TNaming_Iterator anIter(aFatherNS); anIter.More(); anIter.Next()) {
399 if (theContext.IsSame(anIter.NewShape())) { // found the best modification
406 // take the best candidate, or the last in the iteration
408 aModifList.Append(aCandidate);
411 if (!aModifList.IsEmpty()) {
412 // searching for all the base shapes of this modification
413 findBases(aModifList.First(), theValue, true, myBases);
414 if (!myBases.IsEmpty()) {
415 myFinal = aModifList.First()->Label();
416 TopoDS_ListOfShape aCommon;
417 findModificationResult(aCommon);
418 // trying to search by neighbours
419 if (aCommon.Extent() > 1) { // more complicated selection
420 if (!theUseNeighbors)
423 // searching by neighbours
424 std::list<std::pair<TopoDS_Shape, int> > aNBs; /// neighbor sub-shape -> level of neighborhood
425 for(int aLevel = 1; true; aLevel++) {
426 TopTools_MapOfShape aNewNB;
427 findNeighbors(theContext, theValue, aLevel, aNewNB);
428 if (aNewNB.Extent() == 0) { // there are no neighbors of the given level, stop iteration
431 // check which can be named correctly, without by neighbors type
432 for(TopTools_MapOfShape::Iterator aNBIter(aNewNB); aNBIter.More(); ) {
433 Selector_Selector aSelector(myLab.FindChild(1));
434 if (aSelector.select(theContext, aNBIter.Value(), false)) { // add to the list of good NBs
435 aNBs.push_back(std::pair<TopoDS_Shape, int>(aNBIter.Value(), aLevel));
437 aNewNB.Remove(aNBIter.Key());
438 aNBIter.Initialize(aNewNB);
440 TopoDS_Shape aResult = findNeighbor(theContext, aNBs);
441 if (!aResult.IsNull() && aResult.IsSame(theValue)) {
442 std::list<std::pair<TopoDS_Shape, int> >::iterator aNBIter = aNBs.begin();
443 for(; aNBIter != aNBs.end(); aNBIter++) {
444 if (!selectBySubSelector(theContext, aNBIter->first)) {
445 return false; // something is wrong because before this selection was ok
447 myNBLevel.push_back(aNBIter->second);
450 myType = SELTYPE_FILTER_BY_NEIGHBOR;
454 // filter by neighbours did not help
455 if (aCommon.Extent() > 1) { // weak naming between the common results
456 Selector_NExplode aNexp(aCommon);
457 myWeakIndex = aNexp.index(theValue);
458 if (myWeakIndex == -1)
463 myType = SELTYPE_MODIFICATION;
464 if (myBases.IsEmpty()) { // selection based on the external shape, weak name by finals compound
465 TopoDS_ListOfShape aCommon;
466 myFinal = aModifList.First()->Label();
467 Handle(TNaming_NamedShape) aNS;
468 myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS);
469 for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
470 const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
471 if (!aNewShape.IsNull())
472 aCommon.Append(aNewShape);
474 Selector_NExplode aNexp(aCommon);
475 myWeakIndex = aNexp.index(theValue);
476 if (myWeakIndex == -1)
482 // not found a good result
486 void Selector_Selector::store()
488 myLab.ForgetAllAttributes(true); // remove old naming data
489 TDataStd_Integer::Set(myLab, kSEL_TYPE, (int)myType);
491 case SELTYPE_CONTAINER:
492 case SELTYPE_INTERSECT: {
493 TDataStd_Integer::Set(myLab, kSHAPE_TYPE, (int)myShapeType);
494 // store also all sub-selectors
495 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
496 for(; aSubSel != mySubSelList.end(); aSubSel++) {
501 case SELTYPE_PRIMITIVE: {
502 Handle(TDataStd_ReferenceArray) anArray =
503 TDataStd_ReferenceArray::Set(myLab, kBASE_ARRAY, 0, 0);
504 anArray->SetValue(0, myFinal);
507 case SELTYPE_MODIFICATION: {
508 Handle(TDataStd_ReferenceArray) anArray =
509 TDataStd_ReferenceArray::Set(myLab, kBASE_ARRAY, 0, myBases.Extent());
510 TDF_LabelList::Iterator aBIter(myBases);
511 for(int anIndex = 0; aBIter.More(); aBIter.Next(), anIndex++) {
512 anArray->SetValue(anIndex, aBIter.Value());
514 anArray->SetValue(myBases.Extent(), myFinal); // final is in the end of array
515 if (myWeakIndex != -1) {
516 TDataStd_Integer::Set(myLab, kWEAK_INDEX, myWeakIndex);
520 case SELTYPE_FILTER_BY_NEIGHBOR: {
521 TDataStd_Integer::Set(myLab, kSHAPE_TYPE, (int)myShapeType);
522 // store numbers of levels corresponded to the neighbors in sub-selectors
523 Handle(TDataStd_IntegerArray) anArray =
524 TDataStd_IntegerArray::Set(myLab, kLEVELS_ARRAY, 0, int(myNBLevel.size()) - 1);
525 std::list<int>::iterator aLevel = myNBLevel.begin();
526 for(int anIndex = 0; aLevel != myNBLevel.end(); aLevel++, anIndex++) {
527 anArray->SetValue(anIndex, *aLevel);
529 // store all sub-selectors
530 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
531 for(; aSubSel != mySubSelList.end(); aSubSel++) {
535 default: { // unknown case
541 bool Selector_Selector::restore()
543 Handle(TDataStd_Integer) aTypeAttr;
544 if (!myLab.FindAttribute(kSEL_TYPE, aTypeAttr))
546 myType = Selector_Type(aTypeAttr->Get());
548 case SELTYPE_CONTAINER:
549 case SELTYPE_INTERSECT: {
550 Handle(TDataStd_Integer) aShapeTypeAttr;
551 if (!myLab.FindAttribute(kSHAPE_TYPE, aShapeTypeAttr))
553 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
554 // restore sub-selectors
555 bool aSubResult = true;
556 mySubSelList.clear();
557 for(TDF_ChildIDIterator aSub(myLab, kSEL_TYPE, false); aSub.More(); aSub.Next()) {
558 mySubSelList.push_back(Selector_Selector(aSub.Value()->Label()));
559 if (!mySubSelList.back().restore())
564 case SELTYPE_PRIMITIVE: {
565 Handle(TDataStd_ReferenceArray) anArray;
566 if (myLab.FindAttribute(kBASE_ARRAY, anArray)) {
567 myFinal = anArray->Value(0);
572 case SELTYPE_MODIFICATION: {
573 Handle(TDataStd_ReferenceArray) anArray;
574 if (myLab.FindAttribute(kBASE_ARRAY, anArray)) {
575 int anUpper = anArray->Upper();
576 for(int anIndex = 0; anIndex < anUpper; anIndex++) {
577 myBases.Append(anArray->Value(anIndex));
579 myFinal = anArray->Value(anUpper);
580 Handle(TDataStd_Integer) aWeakInt;
581 if (myLab.FindAttribute(kWEAK_INDEX, aWeakInt)) {
582 myWeakIndex = aWeakInt->Get();
588 case SELTYPE_FILTER_BY_NEIGHBOR: {
589 Handle(TDataStd_Integer) aShapeTypeAttr;
590 if (!myLab.FindAttribute(kSHAPE_TYPE, aShapeTypeAttr))
592 myShapeType = TopAbs_ShapeEnum(aShapeTypeAttr->Get());
593 // restore sub-selectors
594 bool aSubResult = true;
595 mySubSelList.clear();
596 for(TDF_ChildIDIterator aSub(myLab, kSEL_TYPE, false); aSub.More(); aSub.Next()) {
597 mySubSelList.push_back(Selector_Selector(aSub.Value()->Label()));
598 if (!mySubSelList.back().restore())
601 // restore levels indices
602 Handle(TDataStd_IntegerArray) anArray;
603 if (!myLab.FindAttribute(kLEVELS_ARRAY, anArray))
605 for(int anIndex = 0; anIndex <= anArray->Upper(); anIndex++) {
606 myNBLevel.push_back(anArray->Value(anIndex));
610 default: { // unknown case
616 /// Returns in theResults all shapes with history started in theBase and ended in theFinal
617 static void findFinals(const TopoDS_Shape& theBase, const TDF_Label& theFinal,
618 TopTools_MapOfShape& theResults)
620 for(TNaming_NewShapeIterator aBaseIter(theBase, theFinal); aBaseIter.More(); aBaseIter.Next()) {
621 TNaming_Evolution anEvolution = aBaseIter.NamedShape()->Evolution();
622 if (anEvolution == TNaming_GENERATED || anEvolution == TNaming_MODIFY) {
623 if (aBaseIter.NamedShape()->Label().IsEqual(theFinal)) {
624 theResults.Add(aBaseIter.Shape());
626 findFinals(aBaseIter.Shape(), theFinal, theResults);
632 void Selector_Selector::findModificationResult(TopoDS_ListOfShape& theCommon) {
633 for(TDF_LabelList::Iterator aBase(myBases); aBase.More(); aBase.Next()) {
634 TopTools_MapOfShape aFinals;
635 for(TNaming_Iterator aBaseShape(aBase.Value()); aBaseShape.More(); aBaseShape.Next())
636 findFinals(aBaseShape.NewShape(), myFinal, aFinals);
637 if (!aFinals.IsEmpty()) {
638 if (theCommon.IsEmpty()) { // just copy all to common
639 for(TopTools_MapOfShape::Iterator aFinal(aFinals); aFinal.More(); aFinal.Next()) {
640 theCommon.Append(aFinal.Key());
642 } else { // keep only shapes presented in both lists
643 for(TopoDS_ListOfShape::Iterator aCommon(theCommon); aCommon.More(); ) {
644 if (aFinals.Contains(aCommon.Value())) {
646 } else { // common is not found, remove it
647 theCommon.Remove(aCommon);
655 bool Selector_Selector::solve(const TopoDS_Shape& theContext)
657 TopoDS_Shape aResult; // null if invalid
659 case SELTYPE_CONTAINER: {
660 TopoDS_Builder aBuilder;
661 switch(myShapeType) {
662 case TopAbs_COMPOUND: {
663 TopoDS_Compound aComp;
664 aBuilder.MakeCompound(aComp);
668 case TopAbs_COMPSOLID: {
669 TopoDS_CompSolid aComp;
670 aBuilder.MakeCompSolid(aComp);
676 aBuilder.MakeShell(aShell);
682 aBuilder.MakeWire(aWire);
687 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
688 for(; aSubSel != mySubSelList.end(); aSubSel++) {
689 if (!aSubSel->solve(theContext)) {
692 aBuilder.Add(aResult, aSubSel->value());
696 case SELTYPE_INTERSECT: {
697 TopoDS_ListOfShape aSubSelectorShapes;
698 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
699 for(; aSubSel != mySubSelList.end(); aSubSel++) {
700 if (!aSubSel->solve(theContext)) {
703 aSubSelectorShapes.Append(aSubSel->value());
705 TopoDS_ListOfShape aCommon; // common sub shapes in each sub-selector (a result)
706 commonShapes(aSubSelectorShapes, myShapeType, aCommon);
707 if (aCommon.Extent() != 1)
709 aResult = aCommon.First();
712 case SELTYPE_PRIMITIVE: {
713 Handle(TNaming_NamedShape) aNS;
714 if (myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
715 aResult = aNS->Get();
719 case SELTYPE_MODIFICATION: {
720 if (myBases.IsEmpty() && myWeakIndex) { // weak name by the final shapes index
721 TopoDS_ListOfShape aCommon;
722 Handle(TNaming_NamedShape) aNS;
723 myFinal.FindAttribute(TNaming_NamedShape::GetID(), aNS);
724 for(TNaming_Iterator aFinalIter(aNS); aFinalIter.More(); aFinalIter.Next()) {
725 const TopoDS_Shape& aNewShape = aFinalIter.NewShape();
726 if (!aNewShape.IsNull())
727 aCommon.Append(aNewShape);
729 Selector_NExplode aNexp(aCommon);
730 aResult = aNexp.shape(myWeakIndex);
731 } else { // standard case
732 TopoDS_ListOfShape aFinalsCommon; // final shapes presented in all results from bases
733 findModificationResult(aFinalsCommon);
734 if (aFinalsCommon.Extent() == 1) // only in this case result is valid: found only one shape
735 aResult = aFinalsCommon.First();
736 else if (aFinalsCommon.Extent() > 1 && myWeakIndex) {
737 Selector_NExplode aNExp(aFinalsCommon);
738 aResult = aNExp.shape(myWeakIndex);
743 case SELTYPE_FILTER_BY_NEIGHBOR: {
744 std::list<std::pair<TopoDS_Shape, int> > aNBs; /// neighbor sub-shape -> level of neighborhood
745 std::list<int>::iterator aLevel = myNBLevel.begin();
746 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
747 for(; aSubSel != mySubSelList.end(); aSubSel++, aLevel++) {
748 if (!aSubSel->solve(theContext)) {
751 aNBs.push_back(std::pair<TopoDS_Shape, int>(aSubSel->value(), *aLevel));
753 aResult = findNeighbor(theContext, aNBs);
755 default: { // unknown case
759 TNaming_Builder aBuilder(myLab);
760 if (!aResult.IsNull()) {
761 aBuilder.Select(aResult, aResult);
764 return false; // builder just erases the named shape in case of error
767 TopoDS_Shape Selector_Selector::value()
769 Handle(TNaming_NamedShape) aNS;
770 if (myLab.FindAttribute(TNaming_NamedShape::GetID(), aNS))
772 return TopoDS_Shape(); // empty, error shape
775 static const std::string kWEAK_NAME_IDENTIFIER = "weak_name_";
777 std::string Selector_Selector::name(Selector_NameGenerator* theNameGenerator) {
779 case SELTYPE_CONTAINER:
780 case SELTYPE_INTERSECT: {
782 // add names of sub-components one by one in "[]"
783 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
784 for(; aSubSel != mySubSelList.end(); aSubSel++) {
786 aResult += aSubSel->name(theNameGenerator);
791 case SELTYPE_PRIMITIVE: {
792 Handle(TDataStd_Name) aName;
793 if (!myFinal.FindAttribute(TDataStd_Name::GetID(), aName))
795 return theNameGenerator->contextName(myFinal) + "/" +
796 std::string(TCollection_AsciiString(aName->Get()).ToCString());
798 case SELTYPE_MODIFICATION: {
799 // final&base1&base2 +optionally: [weak_name_1]
801 Handle(TDataStd_Name) aName;
802 if (!myFinal.FindAttribute(TDataStd_Name::GetID(), aName))
804 aResult += theNameGenerator->contextName(myFinal) + "/" +
805 std::string(TCollection_AsciiString(aName->Get()).ToCString());
806 for(TDF_LabelList::iterator aBase = myBases.begin(); aBase != myBases.end(); aBase++) {
807 if (!aBase->FindAttribute(TDataStd_Name::GetID(), aName))
809 aResult += theNameGenerator->contextName(*aBase) + "/" +
810 std::string(TCollection_AsciiString(aName->Get()).ToCString());
812 if (myWeakIndex != -1) {
813 std::ostringstream aWeakStr;
814 aWeakStr<<"&"<<kWEAK_NAME_IDENTIFIER<<myWeakIndex;
815 aResult += aWeakStr.str();
819 case SELTYPE_FILTER_BY_NEIGHBOR: {
820 // (nb1)level_if_more_than_1(nb2)level_if_more_than_1(nb3)level_if_more_than_1
822 std::list<int>::iterator aLevel = myNBLevel.begin();
823 std::list<Selector_Selector>::iterator aSubSel = mySubSelList.begin();
824 for(; aSubSel != mySubSelList.end(); aSubSel++, aLevel++) {
825 aResult += "(" + aSubSel->name(theNameGenerator) + ")";
831 default: { // unknown case
837 TDF_Label Selector_Selector::restoreByName(
838 std::string theName, const TopAbs_ShapeEnum theShapeType,
839 Selector_NameGenerator* theNameGenerator)
841 if (theName[0] == '[') { // intersection or container
842 switch(theShapeType) {
843 case TopAbs_COMPOUND:
844 case TopAbs_COMPSOLID:
847 myType = SELTYPE_CONTAINER;
852 myType = SELTYPE_INTERSECT;
855 return TDF_Label(); // unknown case
857 myShapeType = theShapeType;
859 for(size_t aStart = 0; aStart != std::string::npos;
860 aStart = theName.find('[', aStart + 1)) {
861 size_t anEndPos = theName.find(']', aStart + 1);
862 if (anEndPos != std::string::npos) {
863 std::string aSubStr = theName.substr(aStart + 1, anEndPos - aStart - 1);
864 mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
865 TDF_Label aSubContext =
866 mySubSelList.back().restoreByName(aSubStr, theShapeType, theNameGenerator);
867 if (aSubContext.IsNull())
868 return aSubContext; // invalid sub-selection parsing
869 if (!aContext.IsNull() && !aContext.IsEqual(aSubContext)) {
870 if (!theNameGenerator->isLater(aContext, aSubContext))
871 aContext = aSubContext;
873 aContext = aSubContext;
876 return TDF_Label(); // invalid parentheses
879 } else if (theName[0] == '(') { // filter by neighbours
880 myType = SELTYPE_FILTER_BY_NEIGHBOR;
882 for(size_t aStart = 0; aStart != std::string::npos;
883 aStart = theName.find('(', aStart + 1)) {
884 size_t anEndPos = theName.find(')', aStart + 1);
885 if (anEndPos != std::string::npos) {
886 std::string aSubStr = theName.substr(aStart + 1, anEndPos - aStart - 1);
887 mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
888 TDF_Label aSubContext =
889 mySubSelList.back().restoreByName(aSubStr, theShapeType, theNameGenerator);
890 if (aSubContext.IsNull())
891 return aSubContext; // invalid sub-selection parsing
892 if (!aContext.IsNull() && !aContext.IsEqual(aSubContext)) {
893 if (!theNameGenerator->isLater(aContext, aSubContext))
894 aContext = aSubContext;
896 aContext = aSubContext;
898 // searching for the level index
900 for(anEndPos++; anEndPos != std::string::npos &&
901 theName[anEndPos] != '(' && theName[anEndPos] != 0;
903 aLevel += theName[anEndPos];
906 myNBLevel.push_back(1); // by default it is 1
908 int aNum = atoi(aLevel.c_str());
910 myNBLevel.push_back(aNum);
912 return TDF_Label(); // invalid number
915 return TDF_Label(); // invalid parentheses
918 } else if (theName.find('&') == std::string::npos) { // wihtout '&' it can be only primitive
919 myType = SELTYPE_PRIMITIVE;
921 if (theNameGenerator->restoreContext(theName, aContext, myFinal)) {
922 if (!myFinal.IsNull())
925 } else { // modification
926 myType = SELTYPE_MODIFICATION;
928 for(size_t anEnd, aStart = 0; aStart != std::string::npos; aStart = anEnd) {
931 anEnd = theName.find('&', aStart);
932 std::string aSubStr =
933 theName.substr(aStart, anEnd == std::string::npos ? anEnd : anEnd - aStart);
934 if (aSubStr.find(kWEAK_NAME_IDENTIFIER) == 0) { // weak name identifier
935 std::string aWeakIndex = aSubStr.substr(kWEAK_NAME_IDENTIFIER.size());
936 myWeakIndex = atoi(aWeakIndex.c_str());
939 TDF_Label aSubContext, aValue;
940 if (!theNameGenerator->restoreContext(aSubStr, aSubContext, aValue))
941 return TDF_Label(); // can not restore
942 if(aSubContext.IsNull() || aValue.IsNull())
943 return TDF_Label(); // can not restore
944 if (myFinal.IsNull()) {
946 aContext = aSubContext;
948 myBases.Append(aValue);
955 bool Selector_Selector::selectBySubSelector(
956 const TopoDS_Shape theContext, const TopoDS_Shape theValue, const bool theUseNeighbors)
958 mySubSelList.push_back(Selector_Selector(myLab.FindChild(int(mySubSelList.size()) + 1)));
959 if (!mySubSelList.back().select(theContext, theValue, theUseNeighbors)) {
960 mySubSelList.clear(); // if one of the selector is failed, all become invalid